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

Revision 3292, 10.9 kB (checked in by nk, 3 years ago)

fix identation

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 None: # None means windows (we explicitly popup in systraywin32.py)
201                        #fixme: make me translatable or find a good way to workaround this
202                        if self.added_hide_menuitem is False:
203                                self.systray_context_menu.prepend(gtk.SeparatorMenuItem())
204                                item = gtk.MenuItem('Hide this menu')
205                                self.systray_context_menu.prepend(item)
206                                self.added_hide_menuitem = True
207                       
208                else: # GNU and Unices
209                        self.systray_context_menu.popup(None, None, None, event.button, event.time)
210                self.systray_context_menu.show_all()
211
212        def on_preferences_menuitem_activate(self, widget):
213                if self.plugin.windows['preferences'].window.get_property('visible'):
214                        self.plugin.windows['preferences'].window.present()
215                else:
216                        self.plugin.windows['preferences'].window.show_all()
217
218        def on_quit_menuitem_activate(self, widget):   
219                self.plugin.roster.on_quit_menuitem_activate(widget)
220
221        def make_groups_submenus_for_chat_with(self, account):
222                groups_menu = gtk.Menu()
223               
224                for group in gajim.groups[account].keys():
225                        if group == _('Transports'):
226                                continue
227                        # at least one 'not offline' or 'without errors' in this group
228                        at_least_one = False
229                        item = gtk.MenuItem(group)
230                        groups_menu.append(item)
231                        contacts_menu = gtk.Menu()
232                        item.set_submenu(contacts_menu)
233                        for users in gajim.contacts[account].values():
234                                user = users[0]
235                                if group in user.groups and user.show != 'offline' and \
236                                                user.show != 'error':
237                                        at_least_one = True
238                                        show = helpers.get_uf_show(user.show)
239                                        s = user.name.replace('_', '__') + ' (' + show + ')'
240                                        item = gtk.MenuItem(s)
241                                        item.connect('activate', self.start_chat, account,\
242                                                        user.jid)
243                                        contacts_menu.append(item)
244                       
245                        if not at_least_one:
246                                message = _('All contacts in this group are offline or have errors')
247                                item = gtk.MenuItem(message)
248                                item.set_sensitive(False)
249                                contacts_menu.append(item)
250
251                return groups_menu
252
253        def on_left_click(self):
254                win = self.plugin.roster.window
255                if len(self.jids) == 0:
256                        if win.get_property('visible'):
257                                win.hide()
258                        else:
259                                win.present()
260                else:
261                        account = self.jids[0][0]
262                        jid = self.jids[0][1]
263                        acc = self.plugin.windows[account]
264                        w = None
265                        if acc['gc'].has_key(jid):
266                                w = acc['gc'][jid]
267                        elif acc['chats'].has_key(jid):
268                                w = acc['chats'][jid]
269                        else:
270                                self.plugin.roster.new_chat(
271                                        gajim.contacts[account][jid][0], account)
272                                acc['chats'][jid].set_active_tab(jid)
273                                acc['chats'][jid].window.present()
274                        if w:
275                                w.set_active_tab(jid)
276                                w.window.present()
277                                tv = w.xmls[jid].get_widget('conversation_textview')
278                                w.scroll_to_end(tv)
279       
280        def on_middle_click(self):
281                win = self.plugin.roster.window
282                if win.is_active():
283                        win.hide()
284                else:
285                        win.present()
286
287        def on_clicked(self, widget, event):
288                self.on_tray_leave_notify_event(widget, None)
289                if event.button == 1: # Left click
290                        self.on_left_click()
291                if event.button == 2: # middle click
292                        self.on_middle_click()
293                if event.button == 3: # right click
294                        self.make_menu(event)
295       
296        def on_show_menuitem_activate(self, widget, show):
297                l = ['online', 'chat', 'away', 'xa', 'dnd', 'invisible', 'offline']
298                index = l.index(show)
299                self.plugin.roster.status_combobox.set_active(index)
300       
301        def show_tooltip(self, widget):
302                position = widget.window.get_origin()
303                if self.tooltip.id == position:
304                        size = widget.window.get_size()
305                        self.tooltip.show_tooltip('', 
306                                (widget.window.get_pointer()[0], size[1]), position)
307                       
308        def on_tray_motion_notify_event(self, widget, event):
309                wireq=widget.size_request()
310                position = widget.window.get_origin()
311                if self.tooltip.timeout > 0:
312                        if self.tooltip.id != position:
313                                self.tooltip.hide_tooltip()
314                if self.tooltip.timeout == 0 and \
315                        self.tooltip.id != position:
316                        self.tooltip.id = position
317                        self.tooltip.timeout = gobject.timeout_add(500,
318                                self.show_tooltip, widget)
319       
320        def on_tray_leave_notify_event(self, widget, event):
321                position = widget.window.get_origin()
322                if self.tooltip.timeout > 0 and \
323                        self.tooltip.id == position:
324                        self.tooltip.hide_tooltip()
325               
326        def show_icon(self):
327                if not self.t:
328                        self.t = trayicon.TrayIcon('Gajim')
329                        eb = gtk.EventBox()
330                        # avoid draw seperate bg color in some gtk themes
331                        eb.set_visible_window(False)
332                        eb.set_events(gtk.gdk.POINTER_MOTION_MASK)
333                        eb.connect('button-press-event', self.on_clicked)
334                        eb.connect('motion-notify-event', self.on_tray_motion_notify_event)
335                        eb.connect('leave-notify-event', self.on_tray_leave_notify_event)
336                        self.tooltip = tooltips.NotificationAreaTooltip(self.plugin)
337
338                        self.img_tray = gtk.Image()
339                        eb.add(self.img_tray)
340                        self.t.add(eb)
341                        self.set_img()
342                self.t.show_all()
343       
344        def hide_icon(self):
345                if self.t:
346                        self.t.destroy()
347                        self.t = None
Note: See TracBrowser for help on using the browser.