| 1 | ## systray.py |
|---|
| 2 | ## |
|---|
| 3 | ## Copyright (C) 2003-2006 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 4 | ## Copyright (C) 2003-2004 Vincent Hanquez <tab@snarc.org> |
|---|
| 5 | ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com> |
|---|
| 6 | ## Copyright (C) 2005 Dimitur Kirov <dkirov@gmail.com> |
|---|
| 7 | ## Copyright (C) 2005-2006 Travis Shirk <travis@pobox.com> |
|---|
| 8 | ## Copyright (C) 2005 Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 9 | ## |
|---|
| 10 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published |
|---|
| 12 | ## by the Free Software Foundation; version 2 only. |
|---|
| 13 | ## |
|---|
| 14 | ## This program is distributed in the hope that it will be useful, |
|---|
| 15 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | ## GNU General Public License for more details. |
|---|
| 18 | ## |
|---|
| 19 | |
|---|
| 20 | import gtk |
|---|
| 21 | import gobject |
|---|
| 22 | import os |
|---|
| 23 | |
|---|
| 24 | import dialogs |
|---|
| 25 | import config |
|---|
| 26 | import tooltips |
|---|
| 27 | import gtkgui_helpers |
|---|
| 28 | |
|---|
| 29 | from common import gajim |
|---|
| 30 | from common import helpers |
|---|
| 31 | |
|---|
| 32 | HAS_SYSTRAY_CAPABILITIES = True |
|---|
| 33 | |
|---|
| 34 | try: |
|---|
| 35 | import egg.trayicon as trayicon # gnomepythonextras trayicon |
|---|
| 36 | except: |
|---|
| 37 | try: |
|---|
| 38 | import trayicon # our trayicon |
|---|
| 39 | except: |
|---|
| 40 | gajim.log.debug('No trayicon module available') |
|---|
| 41 | HAS_SYSTRAY_CAPABILITIES = False |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class Systray: |
|---|
| 45 | '''Class for icon in the notification area |
|---|
| 46 | This class is both base class (for statusicon.py) and normal class |
|---|
| 47 | for trayicon in GNU/Linux''' |
|---|
| 48 | |
|---|
| 49 | def __init__(self): |
|---|
| 50 | self.single_message_handler_id = None |
|---|
| 51 | self.new_chat_handler_id = None |
|---|
| 52 | self.t = None |
|---|
| 53 | # click somewhere else does not popdown menu. workaround this. |
|---|
| 54 | self.added_hide_menuitem = False |
|---|
| 55 | self.img_tray = gtk.Image() |
|---|
| 56 | self.status = 'offline' |
|---|
| 57 | self.xml = gtkgui_helpers.get_glade('systray_context_menu.glade') |
|---|
| 58 | self.systray_context_menu = self.xml.get_widget('systray_context_menu') |
|---|
| 59 | self.xml.signal_autoconnect(self) |
|---|
| 60 | self.popup_menus = [] |
|---|
| 61 | |
|---|
| 62 | def set_img(self): |
|---|
| 63 | if not gajim.interface.systray_enabled: |
|---|
| 64 | return |
|---|
| 65 | if gajim.events.get_nb_systray_events(): |
|---|
| 66 | state = 'message' |
|---|
| 67 | else: |
|---|
| 68 | state = self.status |
|---|
| 69 | image = gajim.interface.roster.jabber_state_images['16'][state] |
|---|
| 70 | if image.get_storage_type() == gtk.IMAGE_ANIMATION: |
|---|
| 71 | self.img_tray.set_from_animation(image.get_animation()) |
|---|
| 72 | elif image.get_storage_type() == gtk.IMAGE_PIXBUF: |
|---|
| 73 | self.img_tray.set_from_pixbuf(image.get_pixbuf()) |
|---|
| 74 | |
|---|
| 75 | def change_status(self, global_status): |
|---|
| 76 | ''' set tray image to 'global_status' ''' |
|---|
| 77 | # change image and status, only if it is different |
|---|
| 78 | if global_status is not None and self.status != global_status: |
|---|
| 79 | self.status = global_status |
|---|
| 80 | self.set_img() |
|---|
| 81 | |
|---|
| 82 | def start_chat(self, widget, account, jid): |
|---|
| 83 | contact = gajim.contacts.get_first_contact_from_jid(account, jid) |
|---|
| 84 | if gajim.interface.msg_win_mgr.has_window(jid, account): |
|---|
| 85 | gajim.interface.msg_win_mgr.get_window(jid, account).set_active_tab( |
|---|
| 86 | jid, account) |
|---|
| 87 | gajim.interface.msg_win_mgr.get_window(jid, account).window.present() |
|---|
| 88 | elif contact: |
|---|
| 89 | gajim.interface.roster.new_chat(contact, account) |
|---|
| 90 | gajim.interface.msg_win_mgr.get_window(jid, account).set_active_tab( |
|---|
| 91 | jid, account) |
|---|
| 92 | |
|---|
| 93 | def on_single_message_menuitem_activate(self, widget, account): |
|---|
| 94 | dialogs.SingleMessageWindow(account, action = 'send') |
|---|
| 95 | |
|---|
| 96 | def on_new_chat(self, widget, account): |
|---|
| 97 | dialogs.NewChatDialog(account) |
|---|
| 98 | |
|---|
| 99 | def make_menu(self, event_button, event_time): |
|---|
| 100 | '''create chat with and new message (sub) menus/menuitems''' |
|---|
| 101 | for m in self.popup_menus: |
|---|
| 102 | m.destroy() |
|---|
| 103 | |
|---|
| 104 | chat_with_menuitem = self.xml.get_widget('chat_with_menuitem') |
|---|
| 105 | single_message_menuitem = self.xml.get_widget( |
|---|
| 106 | 'single_message_menuitem') |
|---|
| 107 | status_menuitem = self.xml.get_widget('status_menu') |
|---|
| 108 | join_gc_menuitem = self.xml.get_widget('join_gc_menuitem') |
|---|
| 109 | sounds_mute_menuitem = self.xml.get_widget('sounds_mute_menuitem') |
|---|
| 110 | |
|---|
| 111 | if self.single_message_handler_id: |
|---|
| 112 | single_message_menuitem.handler_disconnect( |
|---|
| 113 | self.single_message_handler_id) |
|---|
| 114 | self.single_message_handler_id = None |
|---|
| 115 | if self.new_chat_handler_id: |
|---|
| 116 | chat_with_menuitem.disconnect(self.new_chat_handler_id) |
|---|
| 117 | self.new_chat_handler_id = None |
|---|
| 118 | |
|---|
| 119 | sub_menu = gtk.Menu() |
|---|
| 120 | self.popup_menus.append(sub_menu) |
|---|
| 121 | status_menuitem.set_submenu(sub_menu) |
|---|
| 122 | |
|---|
| 123 | gc_sub_menu = gtk.Menu() # gc is always a submenu |
|---|
| 124 | join_gc_menuitem.set_submenu(gc_sub_menu) |
|---|
| 125 | |
|---|
| 126 | # We need our own set of status icons, let's make 'em! |
|---|
| 127 | iconset = gajim.config.get('iconset') |
|---|
| 128 | path = os.path.join(gajim.DATA_DIR, 'iconsets', iconset, '16x16') |
|---|
| 129 | state_images = gajim.interface.roster.load_iconset(path) |
|---|
| 130 | |
|---|
| 131 | if state_images.has_key('muc_active'): |
|---|
| 132 | join_gc_menuitem.set_image(state_images['muc_active']) |
|---|
| 133 | |
|---|
| 134 | for show in ('online', 'chat', 'away', 'xa', 'dnd', 'invisible'): |
|---|
| 135 | uf_show = helpers.get_uf_show(show, use_mnemonic = True) |
|---|
| 136 | item = gtk.ImageMenuItem(uf_show) |
|---|
| 137 | item.set_image(state_images[show]) |
|---|
| 138 | sub_menu.append(item) |
|---|
| 139 | item.connect('activate', self.on_show_menuitem_activate, show) |
|---|
| 140 | |
|---|
| 141 | item = gtk.SeparatorMenuItem() |
|---|
| 142 | sub_menu.append(item) |
|---|
| 143 | |
|---|
| 144 | item = gtk.ImageMenuItem(_('_Change Status Message...')) |
|---|
| 145 | path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'kbd_input.png') |
|---|
| 146 | img = gtk.Image() |
|---|
| 147 | img.set_from_file(path) |
|---|
| 148 | item.set_image(img) |
|---|
| 149 | sub_menu.append(item) |
|---|
| 150 | item.connect('activate', self.on_change_status_message_activate) |
|---|
| 151 | connected_accounts = gajim.get_number_of_connected_accounts() |
|---|
| 152 | if connected_accounts < 1: |
|---|
| 153 | item.set_sensitive(False) |
|---|
| 154 | |
|---|
| 155 | item = gtk.SeparatorMenuItem() |
|---|
| 156 | sub_menu.append(item) |
|---|
| 157 | |
|---|
| 158 | uf_show = helpers.get_uf_show('offline', use_mnemonic = True) |
|---|
| 159 | item = gtk.ImageMenuItem(uf_show) |
|---|
| 160 | item.set_image(state_images['offline']) |
|---|
| 161 | sub_menu.append(item) |
|---|
| 162 | item.connect('activate', self.on_show_menuitem_activate, 'offline') |
|---|
| 163 | |
|---|
| 164 | iskey = connected_accounts > 0 and not (connected_accounts == 1 and |
|---|
| 165 | gajim.connections[gajim.connections.keys()[0]].is_zeroconf) |
|---|
| 166 | chat_with_menuitem.set_sensitive(iskey) |
|---|
| 167 | single_message_menuitem.set_sensitive(iskey) |
|---|
| 168 | join_gc_menuitem.set_sensitive(iskey) |
|---|
| 169 | |
|---|
| 170 | if connected_accounts >= 2: # 2 or more connections? make submenus |
|---|
| 171 | account_menu_for_chat_with = gtk.Menu() |
|---|
| 172 | chat_with_menuitem.set_submenu(account_menu_for_chat_with) |
|---|
| 173 | self.popup_menus.append(account_menu_for_chat_with) |
|---|
| 174 | |
|---|
| 175 | account_menu_for_single_message = gtk.Menu() |
|---|
| 176 | single_message_menuitem.set_submenu( |
|---|
| 177 | account_menu_for_single_message) |
|---|
| 178 | self.popup_menus.append(account_menu_for_single_message) |
|---|
| 179 | |
|---|
| 180 | accounts_list = gajim.contacts.get_accounts() |
|---|
| 181 | accounts_list.sort() |
|---|
| 182 | for account in accounts_list: |
|---|
| 183 | if gajim.connections[account].is_zeroconf: |
|---|
| 184 | continue |
|---|
| 185 | if gajim.connections[account].connected > 1: |
|---|
| 186 | #for chat_with |
|---|
| 187 | item = gtk.MenuItem(_('using account %s') % account) |
|---|
| 188 | account_menu_for_chat_with.append(item) |
|---|
| 189 | item.connect('activate', self.on_new_chat, account) |
|---|
| 190 | |
|---|
| 191 | #for single message |
|---|
| 192 | item = gtk.MenuItem(_('using account %s') % account) |
|---|
| 193 | item.connect('activate', |
|---|
| 194 | self.on_single_message_menuitem_activate, account) |
|---|
| 195 | account_menu_for_single_message.append(item) |
|---|
| 196 | |
|---|
| 197 | # join gc |
|---|
| 198 | label = gtk.Label() |
|---|
| 199 | label.set_markup('<u>' + account.upper() +'</u>') |
|---|
| 200 | label.set_use_underline(False) |
|---|
| 201 | gc_item = gtk.MenuItem() |
|---|
| 202 | gc_item.add(label) |
|---|
| 203 | gc_item.connect('state-changed', |
|---|
| 204 | gtkgui_helpers.on_bm_header_changed_state) |
|---|
| 205 | gc_sub_menu.append(gc_item) |
|---|
| 206 | gajim.interface.roster.add_bookmarks_list(gc_sub_menu, |
|---|
| 207 | account) |
|---|
| 208 | |
|---|
| 209 | elif connected_accounts == 1: # one account |
|---|
| 210 | # one account connected, no need to show 'as jid' |
|---|
| 211 | for account in gajim.connections: |
|---|
| 212 | if gajim.connections[account].connected > 1: |
|---|
| 213 | self.new_chat_handler_id = chat_with_menuitem.connect( |
|---|
| 214 | 'activate', self.on_new_chat, account) |
|---|
| 215 | # for single message |
|---|
| 216 | single_message_menuitem.remove_submenu() |
|---|
| 217 | self.single_message_handler_id = single_message_menuitem.\ |
|---|
| 218 | connect('activate', |
|---|
| 219 | self.on_single_message_menuitem_activate, account) |
|---|
| 220 | |
|---|
| 221 | # join gc |
|---|
| 222 | gajim.interface.roster.add_bookmarks_list(gc_sub_menu, |
|---|
| 223 | account) |
|---|
| 224 | break # No other connected account |
|---|
| 225 | |
|---|
| 226 | sounds_mute_menuitem.set_active(not gajim.config.get('sounds_on')) |
|---|
| 227 | |
|---|
| 228 | if os.name == 'nt': |
|---|
| 229 | # see http://bugzilla.gnome.org/show_bug.cgi?id=377349 |
|---|
| 230 | #FIXME: until it is fixed, put under mouse coordinates |
|---|
| 231 | if gtk.pygtk_version >= (2, 10, 0) and gtk.gtk_version >= (2, 10, 0): |
|---|
| 232 | if self.added_hide_menuitem is False: |
|---|
| 233 | self.systray_context_menu.prepend(gtk.SeparatorMenuItem()) |
|---|
| 234 | item = gtk.MenuItem(_('Hide this menu')) |
|---|
| 235 | self.systray_context_menu.prepend(item) |
|---|
| 236 | self.added_hide_menuitem = True |
|---|
| 237 | self.systray_context_menu.popup(None, None, |
|---|
| 238 | gtk.status_icon_position_menu, event_button, |
|---|
| 239 | event_time, self.status_icon) |
|---|
| 240 | |
|---|
| 241 | else: # GNU and Unices |
|---|
| 242 | self.systray_context_menu.popup(None, None, None, event_button, |
|---|
| 243 | event_time) |
|---|
| 244 | self.systray_context_menu.show_all() |
|---|
| 245 | |
|---|
| 246 | def on_show_all_events_menuitem_activate(self, widget): |
|---|
| 247 | events = gajim.events.get_systray_events() |
|---|
| 248 | for account in events: |
|---|
| 249 | for jid in events[account]: |
|---|
| 250 | for event in events[account][jid]: |
|---|
| 251 | gajim.interface.handle_event(account, jid, event.type_) |
|---|
| 252 | |
|---|
| 253 | def on_sounds_mute_menuitem_activate(self, widget): |
|---|
| 254 | gajim.config.set('sounds_on', not widget.get_active()) |
|---|
| 255 | gajim.interface.save_config() |
|---|
| 256 | |
|---|
| 257 | def on_show_roster_menuitem_activate(self, widget): |
|---|
| 258 | win = gajim.interface.roster.window |
|---|
| 259 | win.present() |
|---|
| 260 | |
|---|
| 261 | def on_preferences_menuitem_activate(self, widget): |
|---|
| 262 | if gajim.interface.instances.has_key('preferences'): |
|---|
| 263 | gajim.interface.instances['preferences'].window.present() |
|---|
| 264 | else: |
|---|
| 265 | gajim.interface.instances['preferences'] = config.PreferencesWindow() |
|---|
| 266 | |
|---|
| 267 | def on_quit_menuitem_activate(self, widget): |
|---|
| 268 | gajim.interface.roster.on_quit_menuitem_activate(widget) |
|---|
| 269 | |
|---|
| 270 | def on_left_click(self): |
|---|
| 271 | win = gajim.interface.roster.window |
|---|
| 272 | if len(gajim.events.get_systray_events()) == 0: |
|---|
| 273 | # no pending events, so toggle visible/hidden for roster window |
|---|
| 274 | if win.get_property('visible'): # visible in ANY virtual desktop? |
|---|
| 275 | |
|---|
| 276 | # we could be in another VD right now. eg vd2 |
|---|
| 277 | # and we want to show it in vd2 |
|---|
| 278 | if not gtkgui_helpers.possibly_move_window_in_current_desktop(win): |
|---|
| 279 | win.hide() # else we hide it from VD that was visible in |
|---|
| 280 | else: |
|---|
| 281 | # in Windows (perhaps other Window Managers too) minimize state |
|---|
| 282 | # is remembered, so make sure it's not minimized (iconified) |
|---|
| 283 | # because user wants to see roster |
|---|
| 284 | win.deiconify() |
|---|
| 285 | win.present() |
|---|
| 286 | else: |
|---|
| 287 | self.handle_first_event() |
|---|
| 288 | |
|---|
| 289 | def handle_first_event(self): |
|---|
| 290 | account, jid, event = gajim.events.get_first_systray_event() |
|---|
| 291 | gajim.interface.handle_event(account, jid, event.type_) |
|---|
| 292 | |
|---|
| 293 | def on_middle_click(self): |
|---|
| 294 | '''middle click raises window to have complete focus (fe. get kbd events) |
|---|
| 295 | but if already raised, it hides it''' |
|---|
| 296 | win = gajim.interface.roster.window |
|---|
| 297 | if win.is_active(): # is it fully raised? (eg does it receive kbd events?) |
|---|
| 298 | win.hide() |
|---|
| 299 | else: |
|---|
| 300 | win.present() |
|---|
| 301 | |
|---|
| 302 | def on_clicked(self, widget, event): |
|---|
| 303 | self.on_tray_leave_notify_event(widget, None) |
|---|
| 304 | if event.type != gtk.gdk.BUTTON_PRESS: |
|---|
| 305 | return |
|---|
| 306 | if event.button == 1: # Left click |
|---|
| 307 | self.on_left_click() |
|---|
| 308 | elif event.button == 2: # middle click |
|---|
| 309 | self.on_middle_click() |
|---|
| 310 | elif event.button == 3: # right click |
|---|
| 311 | self.make_menu(event.button, event.time) |
|---|
| 312 | |
|---|
| 313 | def on_show_menuitem_activate(self, widget, show): |
|---|
| 314 | # we all add some fake (we cannot select those nor have them as show) |
|---|
| 315 | # but this helps to align with roster's status_combobox index positions |
|---|
| 316 | l = ['online', 'chat', 'away', 'xa', 'dnd', 'invisible', 'SEPARATOR', |
|---|
| 317 | 'CHANGE_STATUS_MSG_MENUITEM', 'SEPARATOR', 'offline'] |
|---|
| 318 | index = l.index(show) |
|---|
| 319 | gajim.interface.roster.status_combobox.set_active(index) |
|---|
| 320 | |
|---|
| 321 | def on_change_status_message_activate(self, widget): |
|---|
| 322 | model = gajim.interface.roster.status_combobox.get_model() |
|---|
| 323 | active = gajim.interface.roster.status_combobox.get_active() |
|---|
| 324 | status = model[active][2].decode('utf-8') |
|---|
| 325 | dlg = dialogs.ChangeStatusMessageDialog(status) |
|---|
| 326 | dlg.window.present() |
|---|
| 327 | message = dlg.run() |
|---|
| 328 | if message is not None: # None if user press Cancel |
|---|
| 329 | accounts = gajim.connections.keys() |
|---|
| 330 | for acct in accounts: |
|---|
| 331 | if not gajim.config.get_per('accounts', acct, |
|---|
| 332 | 'sync_with_global_status'): |
|---|
| 333 | continue |
|---|
| 334 | show = gajim.SHOW_LIST[gajim.connections[acct].connected] |
|---|
| 335 | gajim.interface.roster.send_status(acct, show, message) |
|---|
| 336 | |
|---|
| 337 | def show_tooltip(self, widget): |
|---|
| 338 | position = widget.window.get_origin() |
|---|
| 339 | if self.tooltip.id == position: |
|---|
| 340 | size = widget.window.get_size() |
|---|
| 341 | self.tooltip.show_tooltip('', size[1], position[1]) |
|---|
| 342 | |
|---|
| 343 | def on_tray_motion_notify_event(self, widget, event): |
|---|
| 344 | position = widget.window.get_origin() |
|---|
| 345 | if self.tooltip.timeout > 0: |
|---|
| 346 | if self.tooltip.id != position: |
|---|
| 347 | self.tooltip.hide_tooltip() |
|---|
| 348 | if self.tooltip.timeout == 0 and \ |
|---|
| 349 | self.tooltip.id != position: |
|---|
| 350 | self.tooltip.id = position |
|---|
| 351 | self.tooltip.timeout = gobject.timeout_add(500, |
|---|
| 352 | self.show_tooltip, widget) |
|---|
| 353 | |
|---|
| 354 | def on_tray_leave_notify_event(self, widget, event): |
|---|
| 355 | position = widget.window.get_origin() |
|---|
| 356 | if self.tooltip.timeout > 0 and \ |
|---|
| 357 | self.tooltip.id == position: |
|---|
| 358 | self.tooltip.hide_tooltip() |
|---|
| 359 | |
|---|
| 360 | def on_tray_destroyed(self, widget): |
|---|
| 361 | '''re-add trayicon when systray is destroyed''' |
|---|
| 362 | self.t = None |
|---|
| 363 | if gajim.interface.systray_enabled: |
|---|
| 364 | self.show_icon() |
|---|
| 365 | |
|---|
| 366 | def show_icon(self): |
|---|
| 367 | if not self.t: |
|---|
| 368 | self.t = trayicon.TrayIcon('Gajim') |
|---|
| 369 | self.t.connect('destroy', self.on_tray_destroyed) |
|---|
| 370 | eb = gtk.EventBox() |
|---|
| 371 | # avoid draw seperate bg color in some gtk themes |
|---|
| 372 | eb.set_visible_window(False) |
|---|
| 373 | eb.set_events(gtk.gdk.POINTER_MOTION_MASK) |
|---|
| 374 | eb.connect('button-press-event', self.on_clicked) |
|---|
| 375 | eb.connect('motion-notify-event', self.on_tray_motion_notify_event) |
|---|
| 376 | eb.connect('leave-notify-event', self.on_tray_leave_notify_event) |
|---|
| 377 | self.tooltip = tooltips.NotificationAreaTooltip() |
|---|
| 378 | |
|---|
| 379 | self.img_tray = gtk.Image() |
|---|
| 380 | eb.add(self.img_tray) |
|---|
| 381 | self.t.add(eb) |
|---|
| 382 | self.set_img() |
|---|
| 383 | self.t.show_all() |
|---|
| 384 | |
|---|
| 385 | def hide_icon(self): |
|---|
| 386 | if self.t: |
|---|
| 387 | self.t.destroy() |
|---|
| 388 | self.t = None |
|---|