| 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 | ## |
|---|
| 8 | ## Copyright (C) 2003-2005 Gajim Team |
|---|
| 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 gtk.glade |
|---|
| 22 | import dialogs |
|---|
| 23 | |
|---|
| 24 | from common import gajim |
|---|
| 25 | from common import helpers |
|---|
| 26 | from common import i18n |
|---|
| 27 | |
|---|
| 28 | try: |
|---|
| 29 | import egg.trayicon as trayicon # gnomepythonextras trayicon |
|---|
| 30 | except: |
|---|
| 31 | try: |
|---|
| 32 | import trayicon # our trayicon |
|---|
| 33 | except: |
|---|
| 34 | gajim.log.debug('No trayicon module available') |
|---|
| 35 | pass |
|---|
| 36 | |
|---|
| 37 | _ = i18n._ |
|---|
| 38 | APP = i18n.APP |
|---|
| 39 | gtk.glade.bindtextdomain(APP, i18n.DIR) |
|---|
| 40 | gtk.glade.textdomain(APP) |
|---|
| 41 | |
|---|
| 42 | GTKGUI_GLADE = 'gtkgui.glade' |
|---|
| 43 | |
|---|
| 44 | class Systray: |
|---|
| 45 | """Class for icon in the systray""" |
|---|
| 46 | def set_img(self): |
|---|
| 47 | if len(self.jids) > 0: |
|---|
| 48 | status = 'message' |
|---|
| 49 | else: |
|---|
| 50 | status = self.status |
|---|
| 51 | image = self.plugin.roster.jabber_state_images[status] |
|---|
| 52 | if image.get_storage_type() == gtk.IMAGE_ANIMATION: |
|---|
| 53 | self.img_tray.set_from_animation(image.get_animation()) |
|---|
| 54 | elif image.get_storage_type() == gtk.IMAGE_PIXBUF: |
|---|
| 55 | self.img_tray.set_from_pixbuf(image.get_pixbuf()) |
|---|
| 56 | |
|---|
| 57 | def add_jid(self, jid, account): |
|---|
| 58 | list = [account, jid] |
|---|
| 59 | if not list in self.jids: |
|---|
| 60 | self.jids.append(list) |
|---|
| 61 | self.set_img() |
|---|
| 62 | #we look for the number of unread messages |
|---|
| 63 | #in roster |
|---|
| 64 | nb = self.plugin.roster.nb_unread |
|---|
| 65 | for acct in gajim.connections: |
|---|
| 66 | #in chat / groupchat windows |
|---|
| 67 | for kind in ['chats', 'gc']: |
|---|
| 68 | jids = self.plugin.windows[acct][kind] |
|---|
| 69 | for jid in jids: |
|---|
| 70 | if jid != 'tabbed': |
|---|
| 71 | nb += jids[jid].nb_unread[jid] |
|---|
| 72 | if nb > 1: |
|---|
| 73 | label = _('Gajim - %s unread messages') % nb |
|---|
| 74 | else: |
|---|
| 75 | label = _('Gajim - 1 unread message') |
|---|
| 76 | self.tip.set_tip(self.t, label) |
|---|
| 77 | |
|---|
| 78 | def remove_jid(self, jid, account): |
|---|
| 79 | list = [account, jid] |
|---|
| 80 | if list in self.jids: |
|---|
| 81 | self.jids.remove(list) |
|---|
| 82 | self.set_img() |
|---|
| 83 | #we look for the number of unread messages |
|---|
| 84 | #in roster |
|---|
| 85 | nb = self.plugin.roster.nb_unread |
|---|
| 86 | for acct in gajim.connections: |
|---|
| 87 | #in chat / groupchat windows |
|---|
| 88 | for kind in ['chats', 'gc']: |
|---|
| 89 | for jid in self.plugin.windows[acct][kind]: |
|---|
| 90 | if jid != 'tabbed': |
|---|
| 91 | nb += self.plugin.windows[acct][kind][jid].nb_unread[jid] |
|---|
| 92 | if nb > 1: |
|---|
| 93 | label = _('Gajim - %s unread messages') % nb |
|---|
| 94 | elif nb == 1: |
|---|
| 95 | label = _('Gajim - 1 unread message') |
|---|
| 96 | else: |
|---|
| 97 | label = 'Gajim' |
|---|
| 98 | self.tip.set_tip(self.t, label) |
|---|
| 99 | |
|---|
| 100 | def set_status(self, status): |
|---|
| 101 | self.status = status |
|---|
| 102 | self.set_img() |
|---|
| 103 | |
|---|
| 104 | def start_chat(self, widget, account, jid): |
|---|
| 105 | if self.plugin.windows[account]['chats'].has_key(jid): |
|---|
| 106 | self.plugin.windows[account]['chats'][jid].window.present() |
|---|
| 107 | elif self.plugin.roster.contacts[account].has_key(jid): |
|---|
| 108 | self.plugin.roster.new_chat( |
|---|
| 109 | self.plugin.roster.contacts[account][jid][0], account) |
|---|
| 110 | |
|---|
| 111 | def on_new_message_menuitem_activate(self, widget, account): |
|---|
| 112 | """When new message menuitem is activated: |
|---|
| 113 | call the New_message_dialog class""" |
|---|
| 114 | dialogs.New_message_dialog(self.plugin, account) |
|---|
| 115 | |
|---|
| 116 | def make_menu(self, event): |
|---|
| 117 | """create chat with and new message (sub) menus/menuitems""" |
|---|
| 118 | |
|---|
| 119 | chat_with_menuitem = self.xml.get_widget('chat_with_menuitem') |
|---|
| 120 | #menu.append(chat_with_menuitem) |
|---|
| 121 | new_message_menuitem = self.xml.get_widget('new_message_menuitem') |
|---|
| 122 | #menu.append(new_message_menuitem) |
|---|
| 123 | |
|---|
| 124 | iskey = len(gajim.connections.keys()) > 0 |
|---|
| 125 | chat_with_menuitem.set_sensitive(iskey) |
|---|
| 126 | new_message_menuitem.set_sensitive(iskey) |
|---|
| 127 | |
|---|
| 128 | if len(gajim.connections.keys()) >= 2: # 2 or more connections? make submenus |
|---|
| 129 | account_menu_for_chat_with = gtk.Menu() |
|---|
| 130 | chat_with_menuitem.set_submenu(account_menu_for_chat_with) |
|---|
| 131 | |
|---|
| 132 | account_menu_for_new_message = gtk.Menu() |
|---|
| 133 | new_message_menuitem.set_submenu(account_menu_for_new_message) |
|---|
| 134 | |
|---|
| 135 | for account in gajim.connections: |
|---|
| 136 | our_jid = gajim.config.get_per('accounts', account, 'name') + '@' +\ |
|---|
| 137 | gajim.config.get_per('accounts', account, 'hostname') |
|---|
| 138 | #for chat_with |
|---|
| 139 | item = gtk.MenuItem(_('as ') + our_jid) |
|---|
| 140 | account_menu_for_chat_with.append(item) |
|---|
| 141 | group_menu = self.make_groups_submenus_for_chat_with(account) |
|---|
| 142 | item.set_submenu(group_menu) |
|---|
| 143 | #for new_message |
|---|
| 144 | item = gtk.MenuItem(_('as ') + our_jid) |
|---|
| 145 | item.connect('activate',\ |
|---|
| 146 | self.on_new_message_menuitem_activate, account) |
|---|
| 147 | account_menu_for_new_message.append(item) |
|---|
| 148 | |
|---|
| 149 | elif len(gajim.connections) == 1: # one account |
|---|
| 150 | #one account, no need to show 'as jid |
|---|
| 151 | #for chat_with |
|---|
| 152 | account = gajim.connections.keys()[0] |
|---|
| 153 | |
|---|
| 154 | group_menu = self.make_groups_submenus_for_chat_with(account) |
|---|
| 155 | chat_with_menuitem.set_submenu(group_menu) |
|---|
| 156 | |
|---|
| 157 | #for new message |
|---|
| 158 | self.new_message_handler_id = new_message_menuitem.connect( |
|---|
| 159 | 'activate', self.on_new_message_menuitem_activate, account) |
|---|
| 160 | |
|---|
| 161 | self.systray_context_menu.popup(None, None, None, event.button, event.time) |
|---|
| 162 | self.systray_context_menu.show_all() |
|---|
| 163 | self.systray_context_menu.reposition() |
|---|
| 164 | |
|---|
| 165 | def on_quit_menuitem_activate(self, widget): |
|---|
| 166 | self.plugin.roster.on_quit_menuitem_activate(widget) |
|---|
| 167 | |
|---|
| 168 | def make_groups_submenus_for_chat_with(self, account): |
|---|
| 169 | groups_menu = gtk.Menu() |
|---|
| 170 | |
|---|
| 171 | for group in self.plugin.roster.groups[account].keys(): |
|---|
| 172 | if group == 'Transports': |
|---|
| 173 | continue |
|---|
| 174 | # at least one 'not offline' or 'without errors' in this group |
|---|
| 175 | at_least_one = False |
|---|
| 176 | item = gtk.MenuItem(group) |
|---|
| 177 | groups_menu.append(item) |
|---|
| 178 | contacts_menu = gtk.Menu() |
|---|
| 179 | item.set_submenu(contacts_menu) |
|---|
| 180 | for users in self.plugin.roster.contacts[account].values(): |
|---|
| 181 | user = users[0] |
|---|
| 182 | if group in user.groups and user.show != 'offline' and \ |
|---|
| 183 | user.show != 'error': |
|---|
| 184 | at_least_one = True |
|---|
| 185 | show = helpers.get_uf_show(user.show) |
|---|
| 186 | s = user.name.replace('_', '__') + ' (' + show + ')' |
|---|
| 187 | item = gtk.MenuItem(s) |
|---|
| 188 | item.connect('activate', self.start_chat, account,\ |
|---|
| 189 | user.jid) |
|---|
| 190 | contacts_menu.append(item) |
|---|
| 191 | |
|---|
| 192 | if not at_least_one: |
|---|
| 193 | message = _('All contacts in this group are offline or have errors') |
|---|
| 194 | item = gtk.MenuItem(message) |
|---|
| 195 | item.set_sensitive(False) |
|---|
| 196 | contacts_menu.append(item) |
|---|
| 197 | |
|---|
| 198 | return groups_menu |
|---|
| 199 | |
|---|
| 200 | def on_clicked(self, widget, event): |
|---|
| 201 | if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1: |
|---|
| 202 | if len(self.jids) == 0: |
|---|
| 203 | win = self.plugin.roster.window |
|---|
| 204 | if win.is_active(): |
|---|
| 205 | win.hide() |
|---|
| 206 | else: |
|---|
| 207 | win.present() |
|---|
| 208 | else: |
|---|
| 209 | account = self.jids[0][0] |
|---|
| 210 | jid = self.jids[0][1] |
|---|
| 211 | acc = self.plugin.windows[account] |
|---|
| 212 | if acc['gc'].has_key(jid): |
|---|
| 213 | acc['gc'][jid].set_active_tab(jid) |
|---|
| 214 | acc['gc'][jid].window.present() |
|---|
| 215 | elif acc['chats'].has_key(jid): |
|---|
| 216 | acc['chats'][jid].set_active_tab(jid) |
|---|
| 217 | acc['chats'][jid].window.present() |
|---|
| 218 | else: |
|---|
| 219 | self.plugin.roster.new_chat( |
|---|
| 220 | self.plugin.roster.contacts[account][jid][0], account) |
|---|
| 221 | acc['chats'][jid].set_active_tab(jid) |
|---|
| 222 | acc['chats'][jid].window.present() |
|---|
| 223 | if event.button == 3: # right click |
|---|
| 224 | self.make_menu(event) |
|---|
| 225 | |
|---|
| 226 | def on_online_menuitem_activate(self, widget): |
|---|
| 227 | self.plugin.roster.status_combobox.set_active(0) # 0 is online |
|---|
| 228 | |
|---|
| 229 | def on_free_for_chat_menuitem_activate(self, widget): |
|---|
| 230 | self.plugin.roster.status_combobox.set_active(1) # 1 is free for chat |
|---|
| 231 | |
|---|
| 232 | def on_away_menuitem_activate(self, widget): |
|---|
| 233 | self.plugin.roster.status_combobox.set_active(2) # 2 is away |
|---|
| 234 | |
|---|
| 235 | def on_xa_menuitem_activate(self, widget): |
|---|
| 236 | self.plugin.roster.status_combobox.set_active(3) # 3 is xa |
|---|
| 237 | |
|---|
| 238 | def on_dnd_menuitem_activate(self, widget): |
|---|
| 239 | self.plugin.roster.status_combobox.set_active(4) # 4 is dnd |
|---|
| 240 | |
|---|
| 241 | def on_invisible_menuitem_activate(self, widget): |
|---|
| 242 | self.plugin.roster.status_combobox.set_active(5) # 5 is invisible |
|---|
| 243 | |
|---|
| 244 | def on_offline_menuitem_activate(self, widget): |
|---|
| 245 | self.plugin.roster.status_combobox.set_active(6) # 6 is offline |
|---|
| 246 | |
|---|
| 247 | def show_icon(self): |
|---|
| 248 | if not self.t: |
|---|
| 249 | self.t = trayicon.TrayIcon('Gajim') |
|---|
| 250 | eb = gtk.EventBox() |
|---|
| 251 | eb.connect('button-press-event', self.on_clicked) |
|---|
| 252 | self.tip.set_tip(self.t, 'Gajim') |
|---|
| 253 | self.img_tray = gtk.Image() |
|---|
| 254 | eb.add(self.img_tray) |
|---|
| 255 | self.t.add(eb) |
|---|
| 256 | self.set_img() |
|---|
| 257 | self.t.show_all() |
|---|
| 258 | |
|---|
| 259 | def hide_icon(self): |
|---|
| 260 | if self.t: |
|---|
| 261 | self.t.destroy() |
|---|
| 262 | self.t = None |
|---|
| 263 | |
|---|
| 264 | def __init__(self, plugin): |
|---|
| 265 | self.plugin = plugin |
|---|
| 266 | self.jids = [] |
|---|
| 267 | self.t = None |
|---|
| 268 | self.tip = gtk.Tooltips() |
|---|
| 269 | self.img_tray = gtk.Image() |
|---|
| 270 | self.status = 'offline' |
|---|
| 271 | self.xml = gtk.glade.XML(GTKGUI_GLADE, 'systray_context_menu', APP) |
|---|
| 272 | self.systray_context_menu = self.xml.get_widget('systray_context_menu') |
|---|
| 273 | self.xml.signal_autoconnect(self) |
|---|