root/branches/jingle/src/systray.py

Revision 8451, 13.9 kB (checked in by asterix, 16 months ago)

[blarz] ability to load iconsets from ~/.gajim/iconsets. fixes #3339

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