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