fix typo
[gajim.git] / src / systray.py
blob996ce733fb0da5dd608e787b9eb9ed3cdbb10a3f
1 # -*- coding:utf-8 -*-
2 ## src/systray.py
3 ##
4 ## Copyright (C) 2003-2005 Vincent Hanquez <tab AT snarc.org>
5 ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org>
6 ## Copyright (C) 2005 Norman Rasmussen <norman AT rasmussen.co.za>
7 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
8 ## Travis Shirk <travis AT pobox.com>
9 ## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
10 ## Copyright (C) 2006 Stefan Bethge <stefan AT lanpartei.de>
11 ## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
12 ## Copyright (C) 2007 Lukas Petrovicky <lukas AT petrovicky.net>
13 ## Julien Pivotto <roidelapluie AT gmail.com>
15 ## This file is part of Gajim.
17 ## Gajim is free software; you can redistribute it and/or modify
18 ## it under the terms of the GNU General Public License as published
19 ## by the Free Software Foundation; version 3 only.
21 ## Gajim is distributed in the hope that it will be useful,
22 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ## GNU General Public License for more details.
26 ## You should have received a copy of the GNU General Public License
27 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
30 import gtk
31 import gobject
32 import os
34 import dialogs
35 import config
36 import tooltips
37 import gtkgui_helpers
39 from common import gajim
40 from common import helpers
41 from common import pep
43 HAS_SYSTRAY_CAPABILITIES = True
45 try:
46 import egg.trayicon as trayicon # gnomepythonextras trayicon
47 except Exception:
48 try:
49 import trayicon # our trayicon
50 except Exception:
51 gajim.log.debug('No trayicon module available')
52 HAS_SYSTRAY_CAPABILITIES = False
55 class Systray:
56 '''Class for icon in the notification area
57 This class is both base class (for statusicon.py) and normal class
58 for trayicon in GNU/Linux'''
60 def __init__(self):
61 self.single_message_handler_id = None
62 self.new_chat_handler_id = None
63 self.t = None
64 # click somewhere else does not popdown menu. workaround this.
65 self.added_hide_menuitem = False
66 self.img_tray = gtk.Image()
67 self.status = 'offline'
68 self.xml = gtkgui_helpers.get_glade('systray_context_menu.glade')
69 self.systray_context_menu = self.xml.get_widget('systray_context_menu')
70 self.xml.signal_autoconnect(self)
71 self.popup_menus = []
73 def subscribe_events(self):
74 '''Register listeners to the events class'''
75 gajim.events.event_added_subscribe(self.on_event_added)
76 gajim.events.event_removed_subscribe(self.on_event_removed)
78 def unsubscribe_events(self):
79 '''Unregister listeners to the events class'''
80 gajim.events.event_added_unsubscribe(self.on_event_added)
81 gajim.events.event_removed_unsubscribe(self.on_event_removed)
83 def on_event_added(self, event):
84 '''Called when an event is added to the event list'''
85 if event.show_in_systray:
86 self.set_img()
88 def on_event_removed(self, event_list):
89 '''Called when one or more events are removed from the event list'''
90 self.set_img()
92 def set_img(self):
93 if not gajim.interface.systray_enabled:
94 return
95 if gajim.events.get_nb_systray_events():
96 state = 'event'
97 else:
98 state = self.status
99 if state != 'event' and gajim.config.get('trayicon') == 'on_event':
100 self.t.hide()
101 else:
102 self.t.show()
103 image = gajim.interface.jabber_state_images['16'][state]
104 if image.get_storage_type() == gtk.IMAGE_ANIMATION:
105 self.img_tray.set_from_animation(image.get_animation())
106 elif image.get_storage_type() == gtk.IMAGE_PIXBUF:
107 self.img_tray.set_from_pixbuf(image.get_pixbuf())
109 def change_status(self, global_status):
110 ''' set tray image to 'global_status' '''
111 # change image and status, only if it is different
112 if global_status is not None and self.status != global_status:
113 self.status = global_status
114 self.set_img()
116 def start_chat(self, widget, account, jid):
117 contact = gajim.contacts.get_first_contact_from_jid(account, jid)
118 if gajim.interface.msg_win_mgr.has_window(jid, account):
119 gajim.interface.msg_win_mgr.get_window(jid, account).set_active_tab(
120 jid, account)
121 elif contact:
122 gajim.interface.new_chat(contact, account)
123 gajim.interface.msg_win_mgr.get_window(jid, account).set_active_tab(
124 jid, account)
126 def on_single_message_menuitem_activate(self, widget, account):
127 dialogs.SingleMessageWindow(account, action = 'send')
129 def on_new_chat(self, widget, account):
130 dialogs.NewChatDialog(account)
132 def make_menu(self, event_button, event_time):
133 '''create chat with and new message (sub) menus/menuitems'''
134 for m in self.popup_menus:
135 m.destroy()
137 chat_with_menuitem = self.xml.get_widget('chat_with_menuitem')
138 single_message_menuitem = self.xml.get_widget(
139 'single_message_menuitem')
140 status_menuitem = self.xml.get_widget('status_menu')
141 join_gc_menuitem = self.xml.get_widget('join_gc_menuitem')
142 sounds_mute_menuitem = self.xml.get_widget('sounds_mute_menuitem')
144 if self.single_message_handler_id:
145 single_message_menuitem.handler_disconnect(
146 self.single_message_handler_id)
147 self.single_message_handler_id = None
148 if self.new_chat_handler_id:
149 chat_with_menuitem.disconnect(self.new_chat_handler_id)
150 self.new_chat_handler_id = None
152 sub_menu = gtk.Menu()
153 self.popup_menus.append(sub_menu)
154 status_menuitem.set_submenu(sub_menu)
156 gc_sub_menu = gtk.Menu() # gc is always a submenu
157 join_gc_menuitem.set_submenu(gc_sub_menu)
159 # We need our own set of status icons, let's make 'em!
160 iconset = gajim.config.get('iconset')
161 path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
162 state_images = gtkgui_helpers.load_iconset(path)
164 if 'muc_active' in state_images:
165 join_gc_menuitem.set_image(state_images['muc_active'])
167 for show in ('online', 'chat', 'away', 'xa', 'dnd', 'invisible'):
168 uf_show = helpers.get_uf_show(show, use_mnemonic = True)
169 item = gtk.ImageMenuItem(uf_show)
170 item.set_image(state_images[show])
171 sub_menu.append(item)
172 item.connect('activate', self.on_show_menuitem_activate, show)
174 item = gtk.SeparatorMenuItem()
175 sub_menu.append(item)
177 item = gtk.ImageMenuItem(_('_Change Status Message...'))
178 path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'kbd_input.png')
179 img = gtk.Image()
180 img.set_from_file(path)
181 item.set_image(img)
182 sub_menu.append(item)
183 item.connect('activate', self.on_change_status_message_activate)
185 connected_accounts = gajim.get_number_of_connected_accounts()
186 if connected_accounts < 1:
187 item.set_sensitive(False)
189 connected_accounts_with_private_storage = 0
191 item = gtk.SeparatorMenuItem()
192 sub_menu.append(item)
194 uf_show = helpers.get_uf_show('offline', use_mnemonic = True)
195 item = gtk.ImageMenuItem(uf_show)
196 item.set_image(state_images['offline'])
197 sub_menu.append(item)
198 item.connect('activate', self.on_show_menuitem_activate, 'offline')
200 iskey = connected_accounts > 0 and not (connected_accounts == 1 and
201 gajim.connections[gajim.connections.keys()[0]].is_zeroconf)
202 chat_with_menuitem.set_sensitive(iskey)
203 single_message_menuitem.set_sensitive(iskey)
204 join_gc_menuitem.set_sensitive(iskey)
206 accounts_list = sorted(gajim.contacts.get_accounts())
207 # items that get shown whether an account is zeroconf or not
208 if connected_accounts > 1: # 2 or more connections? make submenus
209 account_menu_for_chat_with = gtk.Menu()
210 chat_with_menuitem.set_submenu(account_menu_for_chat_with)
211 self.popup_menus.append(account_menu_for_chat_with)
213 for account in accounts_list:
214 if gajim.account_is_connected(account):
215 # for chat_with
216 item = gtk.MenuItem(_('using account %s') % account)
217 account_menu_for_chat_with.append(item)
218 item.connect('activate', self.on_new_chat, account)
220 elif connected_accounts == 1: # one account
221 # one account connected, no need to show 'as jid'
222 for account in gajim.connections:
223 if gajim.connections[account].connected > 1:
224 # for start chat
225 self.new_chat_handler_id = chat_with_menuitem.connect(
226 'activate', self.on_new_chat, account)
227 break # No other connected account
229 # menu items that don't apply to zeroconf connections
230 if connected_accounts == 1 or (connected_accounts == 2 and \
231 gajim.zeroconf_is_connected()):
232 # only one 'real' (non-zeroconf) account is connected, don't need
233 # submenus
234 for account in gajim.connections:
235 if gajim.account_is_connected(account) and \
236 not gajim.config.get_per('accounts', account, 'is_zeroconf'):
237 if gajim.connections[account].private_storage_supported:
238 connected_accounts_with_private_storage += 1
240 # for single message
241 single_message_menuitem.remove_submenu()
242 self.single_message_handler_id = single_message_menuitem.\
243 connect('activate',
244 self.on_single_message_menuitem_activate, account)
245 # join gc
246 gajim.interface.roster.add_bookmarks_list(gc_sub_menu,
247 account)
248 break # No other account connected
249 else:
250 # 2 or more 'real' accounts are connected, make submenus
251 account_menu_for_single_message = gtk.Menu()
252 single_message_menuitem.set_submenu(
253 account_menu_for_single_message)
254 self.popup_menus.append(account_menu_for_single_message)
256 for account in accounts_list:
257 if gajim.connections[account].is_zeroconf or \
258 not gajim.account_is_connected(account):
259 continue
260 if gajim.connections[account].private_storage_supported:
261 connected_accounts_with_private_storage += 1
262 # for single message
263 item = gtk.MenuItem(_('using account %s') % account)
264 item.connect('activate',
265 self.on_single_message_menuitem_activate, account)
266 account_menu_for_single_message.append(item)
268 # join gc
269 gc_item = gtk.MenuItem(_('using account %s') % account, False)
270 gc_sub_menu.append(gc_item)
271 gc_menuitem_menu = gtk.Menu()
272 gajim.interface.roster.add_bookmarks_list(gc_menuitem_menu,
273 account)
274 gc_item.set_submenu(gc_menuitem_menu)
275 gc_sub_menu.show_all()
277 newitem = gtk.SeparatorMenuItem() # separator
278 gc_sub_menu.append(newitem)
279 newitem = gtk.ImageMenuItem(_('_Manage Bookmarks...'))
280 img = gtk.image_new_from_stock(gtk.STOCK_PREFERENCES, gtk.ICON_SIZE_MENU)
281 newitem.set_image(img)
282 newitem.connect('activate',
283 gajim.interface.roster.on_manage_bookmarks_menuitem_activate)
284 gc_sub_menu.append(newitem)
285 if connected_accounts_with_private_storage == 0:
286 newitem.set_sensitive(False)
288 sounds_mute_menuitem.set_active(not gajim.config.get('sounds_on'))
290 if os.name == 'nt':
291 if self.added_hide_menuitem is False:
292 self.systray_context_menu.prepend(gtk.SeparatorMenuItem())
293 item = gtk.MenuItem(_('Hide this menu'))
294 self.systray_context_menu.prepend(item)
295 self.added_hide_menuitem = True
297 self.systray_context_menu.show_all()
298 self.systray_context_menu.popup(None, None, None, 0,
299 event_time)
301 def on_show_all_events_menuitem_activate(self, widget):
302 events = gajim.events.get_systray_events()
303 for account in events:
304 for jid in events[account]:
305 for event in events[account][jid]:
306 gajim.interface.handle_event(account, jid, event.type_)
308 def on_sounds_mute_menuitem_activate(self, widget):
309 gajim.config.set('sounds_on', not widget.get_active())
310 gajim.interface.save_config()
312 def on_show_roster_menuitem_activate(self, widget):
313 win = gajim.interface.roster.window
314 win.present()
316 def on_preferences_menuitem_activate(self, widget):
317 if 'preferences' in gajim.interface.instances:
318 gajim.interface.instances['preferences'].window.present()
319 else:
320 gajim.interface.instances['preferences'] = config.PreferencesWindow()
322 def on_quit_menuitem_activate(self, widget):
323 gajim.interface.roster.on_quit_request()
325 def on_left_click(self):
326 win = gajim.interface.roster.window
327 if len(gajim.events.get_systray_events()) == 0:
328 # No pending events, so toggle visible/hidden for roster window
329 if win.get_property('visible') and (win.get_property(
330 'has-toplevel-focus') or os.name == 'nt'):
331 # visible in ANY virtual desktop?
333 # we could be in another VD right now. eg vd2
334 # and we want to show it in vd2
335 if not gtkgui_helpers.possibly_move_window_in_current_desktop(win):
336 win.hide() # else we hide it from VD that was visible in
337 else:
338 win.show_all()
339 if not gajim.config.get('roster_window_skip_taskbar'):
340 win.set_property('skip-taskbar-hint', False)
341 win.present_with_time(gtk.get_current_event_time())
342 else:
343 self.handle_first_event()
345 def handle_first_event(self):
346 account, jid, event = gajim.events.get_first_systray_event()
347 if not event:
348 return
349 gajim.interface.handle_event(account, jid, event.type_)
351 def on_middle_click(self):
352 '''middle click raises window to have complete focus (fe. get kbd events)
353 but if already raised, it hides it'''
354 win = gajim.interface.roster.window
355 if win.is_active(): # is it fully raised? (eg does it receive kbd events?)
356 win.hide()
357 else:
358 win.present()
360 def on_clicked(self, widget, event):
361 self.on_tray_leave_notify_event(widget, None)
362 if event.type != gtk.gdk.BUTTON_PRESS:
363 return
364 if event.button == 1: # Left click
365 self.on_left_click()
366 elif event.button == 2: # middle click
367 self.on_middle_click()
368 elif event.button == 3: # right click
369 self.make_menu(event.button, event.time)
371 def on_show_menuitem_activate(self, widget, show):
372 # we all add some fake (we cannot select those nor have them as show)
373 # but this helps to align with roster's status_combobox index positions
374 l = ['online', 'chat', 'away', 'xa', 'dnd', 'invisible', 'SEPARATOR',
375 'CHANGE_STATUS_MSG_MENUITEM', 'SEPARATOR', 'offline']
376 index = l.index(show)
377 if not helpers.statuses_unified():
378 gajim.interface.roster.status_combobox.set_active(index + 2)
379 return
380 current = gajim.interface.roster.status_combobox.get_active()
381 if index != current:
382 gajim.interface.roster.status_combobox.set_active(index)
384 def on_change_status_message_activate(self, widget):
385 model = gajim.interface.roster.status_combobox.get_model()
386 active = gajim.interface.roster.status_combobox.get_active()
387 status = model[active][2].decode('utf-8')
388 def on_response(message, pep_dict):
389 if message is None: # None if user press Cancel
390 return
391 accounts = gajim.connections.keys()
392 for acct in accounts:
393 if not gajim.config.get_per('accounts', acct,
394 'sync_with_global_status'):
395 continue
396 show = gajim.SHOW_LIST[gajim.connections[acct].connected]
397 gajim.interface.roster.send_status(acct, show, message)
398 gajim.interface.roster.send_pep(acct, pep_dict)
399 dlg = dialogs.ChangeStatusMessageDialog(on_response, status)
400 dlg.dialog.present()
402 def show_tooltip(self, widget):
403 position = widget.window.get_origin()
404 if self.tooltip.id == position:
405 size = widget.window.get_size()
406 self.tooltip.show_tooltip('', size[1], position[1])
408 def on_tray_motion_notify_event(self, widget, event):
409 position = widget.window.get_origin()
410 if self.tooltip.timeout > 0:
411 if self.tooltip.id != position:
412 self.tooltip.hide_tooltip()
413 if self.tooltip.timeout == 0 and \
414 self.tooltip.id != position:
415 self.tooltip.id = position
416 self.tooltip.timeout = gobject.timeout_add(500,
417 self.show_tooltip, widget)
419 def on_tray_leave_notify_event(self, widget, event):
420 position = widget.window.get_origin()
421 if self.tooltip.timeout > 0 and \
422 self.tooltip.id == position:
423 self.tooltip.hide_tooltip()
425 def on_tray_destroyed(self, widget):
426 '''re-add trayicon when systray is destroyed'''
427 self.t = None
428 if gajim.interface.systray_enabled:
429 self.show_icon()
431 def show_icon(self):
432 if not self.t:
433 self.t = trayicon.TrayIcon('Gajim')
434 self.t.connect('destroy', self.on_tray_destroyed)
435 eb = gtk.EventBox()
436 # avoid draw seperate bg color in some gtk themes
437 eb.set_visible_window(False)
438 eb.set_events(gtk.gdk.POINTER_MOTION_MASK)
439 eb.connect('button-press-event', self.on_clicked)
440 eb.connect('motion-notify-event', self.on_tray_motion_notify_event)
441 eb.connect('leave-notify-event', self.on_tray_leave_notify_event)
442 self.tooltip = tooltips.NotificationAreaTooltip()
444 self.img_tray = gtk.Image()
445 eb.add(self.img_tray)
446 self.t.add(eb)
447 self.set_img()
448 self.subscribe_events()
449 self.t.show_all()
451 def hide_icon(self):
452 if self.t:
453 self.t.destroy()
454 self.t = None
455 self.unsubscribe_events()
457 # vim: se ts=3: