root/branches/gajim_0.8/src/systray.py

Revision 3064, 10.6 kB (checked in by nk, 3 years ago)

trayicon for windows is 100% ready

Line 
1##      systray.py
2##
3## Gajim Team:
4##      - Yann Le Boulanger <asterix@lagaule.org>
5##      - Vincent Hanquez <tab@snarc.org>
6##      - Nikos Kouremenos <kourem@gmail.com>
7##      - Dimitur Kirov <dkirov@gmail.com>
8##
9##      Copyright (C) 2003-2005 Gajim Team
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 gtk.glade
23import gobject
24import dialogs
25import os
26
27import tooltips
28
29from common import gajim
30from common import helpers
31from common import i18n
32
33try:
34        import egg.trayicon as trayicon # gnomepythonextras trayicon
35except:
36        try:
37                import trayicon # our trayicon
38        except:
39                gajim.log.debug('No trayicon module available')
40                pass
41
42_ = i18n._
43APP = i18n.APP
44gtk.glade.bindtextdomain(APP, i18n.DIR)
45gtk.glade.textdomain(APP)
46
47GTKGUI_GLADE = 'gtkgui.glade'
48
49class Systray:
50        '''Class for icon in the notification area
51        This class is both base class (for systraywin32.py) and normal class
52        for trayicon in GNU/Linux'''
53       
54        def __init__(self, plugin):
55                self.plugin = plugin
56                self.jids = []
57                self.new_message_handler_id = None
58                self.t = None
59                self.img_tray = gtk.Image()
60                self.status = 'offline'
61                self.xml = gtk.glade.XML(GTKGUI_GLADE, 'systray_context_menu', APP)
62                self.systray_context_menu = self.xml.get_widget('systray_context_menu')
63                self.xml.signal_autoconnect(self)
64
65        def set_img(self):
66                if len(self.jids) > 0:
67                        state = 'message'
68                else:
69                        state = self.status
70                image = self.plugin.roster.jabber_state_images[state]
71                if image.get_storage_type() == gtk.IMAGE_ANIMATION:
72                        self.img_tray.set_from_animation(image.get_animation())
73                elif image.get_storage_type() == gtk.IMAGE_PIXBUF:
74                        self.img_tray.set_from_pixbuf(image.get_pixbuf())
75
76        def add_jid(self, jid, account):
77                l = [account, jid]
78                if not l in self.jids:
79                        self.jids.append(l)
80                        self.set_img()
81                # we append to the number of unread messages
82                nb = self.plugin.roster.nb_unread
83                for acct in gajim.connections:
84                        # in chat / groupchat windows
85                        for kind in ['chats', 'gc']:
86                                jids = self.plugin.windows[acct][kind]
87                                for jid in jids:
88                                        if jid != 'tabbed':
89                                                nb += jids[jid].nb_unread[jid]
90
91        def remove_jid(self, jid, account):
92                l = [account, jid]
93                if l in self.jids:
94                        self.jids.remove(l)
95                        self.set_img()
96                # we remove from the number of unread messages
97                nb = self.plugin.roster.nb_unread
98                for acct in gajim.connections:
99                        # in chat / groupchat windows
100                        for kind in ['chats', 'gc']:
101                                for jid in self.plugin.windows[acct][kind]:
102                                        if jid != 'tabbed':
103                                                nb += self.plugin.windows[acct][kind][jid].nb_unread[jid]
104       
105        def change_status(self, global_status):
106                ''' set tray image to 'global_status' '''
107                # change image and status, only if it is different
108                if global_status is not None and self.status != global_status:
109                        self.status = global_status
110                self.set_img()
111       
112        def start_chat(self, widget, account, jid):
113                if self.plugin.windows[account]['chats'].has_key(jid):
114                        self.plugin.windows[account]['chats'][jid].window.present()
115                        self.plugin.windows[account]['chats'][jid].set_active_tab(jid)
116                elif gajim.contacts[account].has_key(jid):
117                        self.plugin.roster.new_chat(
118                                gajim.contacts[account][jid][0], account)
119                        self.plugin.windows[account]['chats'][jid].set_active_tab(jid)
120       
121        def on_new_message_menuitem_activate(self, widget, account):
122                """When new message menuitem is activated:
123                call the NewMessageDialog class"""
124                dialogs.NewMessageDialog(self.plugin, account)
125
126        def make_menu(self, event = None):
127                '''create chat with and new message (sub) menus/menuitems
128                event is None when we're in Windows
129                '''
130               
131                chat_with_menuitem = self.xml.get_widget('chat_with_menuitem')
132                new_message_menuitem = self.xml.get_widget('new_message_menuitem')
133                status_menuitem = self.xml.get_widget('status_menu')
134               
135                if self.new_message_handler_id:
136                        new_message_menuitem.handler_disconnect(
137                                self.new_message_handler_id)
138                        self.new_message_handler_id = None
139
140                sub_menu = gtk.Menu()
141                status_menuitem.set_submenu(sub_menu)
142
143                # We need our own set of status icons, let's make 'em!
144                iconset = gajim.config.get('iconset')
145                if not iconset:
146                        iconset = 'sun'
147                path = os.path.join(gajim.DATA_DIR, 'iconsets/' + iconset + '/16x16/')
148                state_images = self.plugin.roster.load_iconset(path)
149               
150                for show in ['online', 'chat', 'away', 'xa', 'dnd', 'invisible',
151                                'offline']:
152
153                        if show == 'offline': # We add a sep before offline item
154                                item = gtk.SeparatorMenuItem()
155                                sub_menu.append(item)
156
157                        uf_show = helpers.get_uf_show(show)
158                        item = gtk.ImageMenuItem(uf_show)
159                        item.set_image(state_images[show])
160                        sub_menu.append(item)
161                        item.connect('activate', self.on_show_menuitem_activate, show)
162
163                iskey = len(gajim.connections) > 0
164                chat_with_menuitem.set_sensitive(iskey)
165                new_message_menuitem.set_sensitive(iskey)
166               
167                if len(gajim.connections) >= 2: # 2 or more connections? make submenus
168                        account_menu_for_chat_with = gtk.Menu()
169                        chat_with_menuitem.set_submenu(account_menu_for_chat_with)
170
171                        account_menu_for_new_message = gtk.Menu()
172                        new_message_menuitem.set_submenu(account_menu_for_new_message)
173
174                        for account in gajim.connections:
175                                our_jid = gajim.config.get_per('accounts', account, 'name') + '@' +\
176                                        gajim.config.get_per('accounts', account, 'hostname')
177                                #for chat_with
178                                item = gtk.MenuItem(_('as ') + our_jid)
179                                account_menu_for_chat_with.append(item)
180                                group_menu = self.make_groups_submenus_for_chat_with(account)
181                                item.set_submenu(group_menu)
182                                #for new_message
183                                item = gtk.MenuItem(_('as ') + our_jid)
184                                item.connect('activate',
185                                        self.on_new_message_menuitem_activate, account)
186                                account_menu_for_new_message.append(item)
187                               
188                elif len(gajim.connections) == 1: # one account
189                        # one account, no need to show 'as jid'
190                        # for chat_with
191                        account = gajim.connections.keys()[0]
192                       
193                        group_menu = self.make_groups_submenus_for_chat_with(account)
194                        chat_with_menuitem.set_submenu(group_menu)
195                                       
196                        # for new message
197                        self.new_message_handler_id = new_message_menuitem.connect(
198                                'activate', self.on_new_message_menuitem_activate, account)
199
200                if event is not None: # None means windows (we explicitly popup in systraywin32.py)
201                        self.systray_context_menu.popup(None, None, None, event.button, event.time)
202                self.systray_context_menu.show_all()
203
204        def on_preferences_menuitem_activate(self, widget):
205                if self.plugin.windows['preferences'].window.get_property('visible'):
206                        self.plugin.windows['preferences'].window.present()
207                else:
208                        self.plugin.windows['preferences'].window.show_all()
209
210        def on_quit_menuitem_activate(self, widget):   
211                self.plugin.roster.on_quit_menuitem_activate(widget)
212
213        def make_groups_submenus_for_chat_with(self, account):
214                groups_menu = gtk.Menu()
215               
216                for group in gajim.groups[account].keys():
217                        if group == _('Transports'):
218                                continue
219                        # at least one 'not offline' or 'without errors' in this group
220                        at_least_one = False
221                        item = gtk.MenuItem(group)
222                        groups_menu.append(item)
223                        contacts_menu = gtk.Menu()
224                        item.set_submenu(contacts_menu)
225                        for users in gajim.contacts[account].values():
226                                user = users[0]
227                                if group in user.groups and user.show != 'offline' and \
228                                                user.show != 'error':
229                                        at_least_one = True
230                                        show = helpers.get_uf_show(user.show)
231                                        s = user.name.replace('_', '__') + ' (' + show + ')'
232                                        item = gtk.MenuItem(s)
233                                        item.connect('activate', self.start_chat, account,\
234                                                        user.jid)
235                                        contacts_menu.append(item)
236                       
237                        if not at_least_one:
238                                message = _('All contacts in this group are offline or have errors')
239                                item = gtk.MenuItem(message)
240                                item.set_sensitive(False)
241                                contacts_menu.append(item)
242
243                return groups_menu
244
245        def on_left_click(self):
246                win = self.plugin.roster.window
247                if len(self.jids) == 0:
248                        if win.get_property('visible'):
249                                win.hide()
250                        else:
251                                win.present()
252                else:
253                        account = self.jids[0][0]
254                        jid = self.jids[0][1]
255                        acc = self.plugin.windows[account]
256                        w = None
257                        if acc['gc'].has_key(jid):
258                                w = acc['gc'][jid]
259                        elif acc['chats'].has_key(jid):
260                                w = acc['chats'][jid]
261                        else:
262                                self.plugin.roster.new_chat(
263                                        gajim.contacts[account][jid][0], account)
264                                acc['chats'][jid].set_active_tab(jid)
265                                acc['chats'][jid].window.present()
266                        if w:
267                                w.set_active_tab(jid)
268                                w.window.present()
269                                tv = w.xmls[jid].get_widget('conversation_textview')
270                                w.scroll_to_end(tv)
271       
272        def on_middle_click(self):
273                win = self.plugin.roster.window
274                if win.is_active():
275                        win.hide()
276                else:
277                        win.present()
278
279        def on_clicked(self, widget, event):
280                self.on_tray_leave_notify_event(widget, None)
281                if event.button == 1: # Left click
282                        self.on_left_click()
283                if event.button == 2: # middle click
284                        self.on_middle_click()
285                if event.button == 3: # right click
286                        self.make_menu(event)
287       
288        def on_show_menuitem_activate(self, widget, show):
289                l = ['online', 'chat', 'away', 'xa', 'dnd', 'invisible', 'offline']
290                index = l.index(show)
291                self.plugin.roster.status_combobox.set_active(index)
292       
293        def show_tooltip(self, widget):
294                position = widget.window.get_origin()
295                if self.tooltip.id == position:
296                        size = widget.window.get_size()
297                        self.tooltip.show_tooltip('', 
298                                (widget.window.get_pointer()[0], size[1]), position)
299                       
300        def on_tray_motion_notify_event(self, widget, event):
301                wireq=widget.size_request()
302                position = widget.window.get_origin()
303                if self.tooltip.timeout > 0:
304                        if self.tooltip.id != position:
305                                self.tooltip.hide_tooltip()
306                if self.tooltip.timeout == 0 and \
307                        self.tooltip.id != position:
308                        self.tooltip.id = position
309                        self.tooltip.timeout = gobject.timeout_add(500,
310                                self.show_tooltip, widget)
311       
312        def on_tray_leave_notify_event(self, widget, event):
313                position = widget.window.get_origin()
314                if self.tooltip.timeout > 0 and \
315                        self.tooltip.id == position:
316                        self.tooltip.hide_tooltip()
317               
318        def show_icon(self):
319                if not self.t:
320                        self.t = trayicon.TrayIcon('Gajim')
321                        eb = gtk.EventBox()
322                        # avoid draw seperate bg color in some gtk themes
323                        eb.set_visible_window(False)
324                        eb.set_events(gtk.gdk.POINTER_MOTION_MASK)
325                        eb.connect('button-press-event', self.on_clicked)
326                        eb.connect('motion-notify-event', self.on_tray_motion_notify_event)
327                        eb.connect('leave-notify-event', self.on_tray_leave_notify_event)
328                        self.tooltip = tooltips.NotificationAreaTooltip(self.plugin)
329
330                        self.img_tray = gtk.Image()
331                        eb.add(self.img_tray)
332                        self.t.add(eb)
333                        self.set_img()
334                self.t.show_all()
335       
336        def hide_icon(self):
337                if self.t:
338                        self.t.destroy()
339                        self.t = None
Note: See TracBrowser for help on using the browser.