| 1 | ## roster_window.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 gobject |
|---|
| 21 | import gtk |
|---|
| 22 | import os |
|---|
| 23 | |
|---|
| 24 | from common import gajim |
|---|
| 25 | from time import time |
|---|
| 26 | from common import i18n |
|---|
| 27 | |
|---|
| 28 | _ = i18n._ |
|---|
| 29 | |
|---|
| 30 | try: |
|---|
| 31 | import dbus |
|---|
| 32 | _version = getattr(dbus, 'version', (0, 20, 0)) |
|---|
| 33 | except ImportError: |
|---|
| 34 | _version = (0, 0, 0) |
|---|
| 35 | |
|---|
| 36 | if _version >= (0, 41, 0): |
|---|
| 37 | import dbus.service |
|---|
| 38 | import dbus.glib # cause dbus 0.35+ doesn't return signal replies without it |
|---|
| 39 | DbusPrototype = dbus.service.Object |
|---|
| 40 | elif _version >= (0, 20, 0): |
|---|
| 41 | DbusPrototype = dbus.Object |
|---|
| 42 | else: #dbus is not defined |
|---|
| 43 | DbusPrototype = str |
|---|
| 44 | |
|---|
| 45 | INTERFACE = 'org.gajim.dbus.RemoteInterface' |
|---|
| 46 | OBJ_PATH = '/org/gajim/dbus/RemoteObject' |
|---|
| 47 | SERVICE = 'org.gajim.dbus' |
|---|
| 48 | |
|---|
| 49 | class Remote: |
|---|
| 50 | def __init__(self, plugin): |
|---|
| 51 | self.signal_object = None |
|---|
| 52 | if 'dbus' not in globals() and not os.name == 'nt': |
|---|
| 53 | print _('D-Bus python bindings are missing in this computer') |
|---|
| 54 | print _('D-Bus capabilities of Gajim cannot be used') |
|---|
| 55 | raise DbusNotSupported() |
|---|
| 56 | try: |
|---|
| 57 | session_bus = dbus.SessionBus() |
|---|
| 58 | except: |
|---|
| 59 | raise SessionBusNotPresent() |
|---|
| 60 | |
|---|
| 61 | if _version[1] >= 41: |
|---|
| 62 | service = dbus.service.BusName(SERVICE, bus=session_bus) |
|---|
| 63 | self.signal_object = SignalObject(service, plugin) |
|---|
| 64 | elif _version[1] <= 40 and _version[1] >= 20: |
|---|
| 65 | service=dbus.Service(SERVICE, session_bus) |
|---|
| 66 | self.signal_object = SignalObject(service, plugin) |
|---|
| 67 | |
|---|
| 68 | def set_enabled(self, status): |
|---|
| 69 | self.signal_object.disabled = not status |
|---|
| 70 | |
|---|
| 71 | def is_enabled(self): |
|---|
| 72 | return not self.signal_object.disabled |
|---|
| 73 | |
|---|
| 74 | def raise_signal(self, signal, arg): |
|---|
| 75 | if self.signal_object: |
|---|
| 76 | self.signal_object.raise_signal(signal, repr(arg)) |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | class SignalObject(DbusPrototype): |
|---|
| 80 | ''' Local object definition for /org/gajim/dbus/RemoteObject. This doc must |
|---|
| 81 | not be visible, because the clients can access only the remote object. ''' |
|---|
| 82 | |
|---|
| 83 | def __init__(self, service, plugin): |
|---|
| 84 | self.plugin = plugin |
|---|
| 85 | self.first_show = True |
|---|
| 86 | self.vcard_account = None |
|---|
| 87 | self.disabled = False |
|---|
| 88 | |
|---|
| 89 | # register our dbus API |
|---|
| 90 | if _version[1] >= 41: |
|---|
| 91 | DbusPrototype.__init__(self, service, OBJ_PATH) |
|---|
| 92 | elif _version[1] >= 30: |
|---|
| 93 | DbusPrototype.__init__(self, OBJ_PATH, service) |
|---|
| 94 | else: |
|---|
| 95 | DbusPrototype.__init__(self, OBJ_PATH, service, |
|---|
| 96 | [ self.toggle_roster_appearance, |
|---|
| 97 | self.show_next_unread, |
|---|
| 98 | self.list_contacts, |
|---|
| 99 | self.list_accounts, |
|---|
| 100 | self.change_status, |
|---|
| 101 | self.open_chat, |
|---|
| 102 | self.send_message, |
|---|
| 103 | self.contact_info, |
|---|
| 104 | self.send_file |
|---|
| 105 | ]) |
|---|
| 106 | |
|---|
| 107 | def raise_signal(self, signal, arg): |
|---|
| 108 | ''' raise a signal, with a single string message ''' |
|---|
| 109 | if self.disabled : |
|---|
| 110 | return |
|---|
| 111 | if _version[1] >= 30: |
|---|
| 112 | from dbus import dbus_bindings |
|---|
| 113 | message = dbus_bindings.Signal(OBJ_PATH, INTERFACE, signal) |
|---|
| 114 | i = message.get_iter(True) |
|---|
| 115 | i.append(arg) |
|---|
| 116 | self._connection.send(message) |
|---|
| 117 | else: |
|---|
| 118 | self.emit_signal(INTERFACE, signal, arg) |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | # signals |
|---|
| 122 | def VcardInfo(self, *vcard): |
|---|
| 123 | pass |
|---|
| 124 | |
|---|
| 125 | def send_file(self, *args): |
|---|
| 126 | ''' send_file(file_path, jid, account=None) |
|---|
| 127 | send file, located at 'file_path' to 'jid', using account |
|---|
| 128 | (optional) 'account' ''' |
|---|
| 129 | file_path, jid, account = self._get_real_arguments(args, 3) |
|---|
| 130 | accounts = gajim.contacts.keys() |
|---|
| 131 | |
|---|
| 132 | # if there is only one account in roster, take it as default |
|---|
| 133 | if not account and len(accounts) == 1: |
|---|
| 134 | account = accounts[0] |
|---|
| 135 | if account: |
|---|
| 136 | if gajim.connections[account].connected > 1: # account is online |
|---|
| 137 | connected_account = gajim.connections[account] |
|---|
| 138 | else: |
|---|
| 139 | for account in accounts: |
|---|
| 140 | if gajim.contacts[account].has_key(jid) and \ |
|---|
| 141 | gajim.connections[account].connected > 1: # account is online |
|---|
| 142 | connected_account = gajim.connections[account] |
|---|
| 143 | break |
|---|
| 144 | if gajim.contacts.has_key(account) and \ |
|---|
| 145 | gajim.contacts[account].has_key(jid): |
|---|
| 146 | contact = gajim.get_highest_prio_contact_from_contacts( |
|---|
| 147 | gajim.contacts[account][jid]) |
|---|
| 148 | else: |
|---|
| 149 | contact = jid |
|---|
| 150 | |
|---|
| 151 | if connected_account: |
|---|
| 152 | if os.path.isfile(file_path): # is it file? |
|---|
| 153 | self.plugin.windows['file_transfers'].send_file(account, |
|---|
| 154 | contact, file_path) |
|---|
| 155 | return True |
|---|
| 156 | return False |
|---|
| 157 | |
|---|
| 158 | def send_message(self, *args): |
|---|
| 159 | ''' send_message(jid, message, keyID=None, account=None) |
|---|
| 160 | send 'message' to 'jid', using account (optional) 'account'. |
|---|
| 161 | if keyID is specified, encrypt the message with the pgp key ''' |
|---|
| 162 | if self.disabled: |
|---|
| 163 | return |
|---|
| 164 | jid, message, keyID, account = self._get_real_arguments(args, 4) |
|---|
| 165 | if not jid or not message: |
|---|
| 166 | return None # or raise error |
|---|
| 167 | if not keyID: |
|---|
| 168 | keyID = '' |
|---|
| 169 | connected_account = None |
|---|
| 170 | accounts = gajim.contacts.keys() |
|---|
| 171 | |
|---|
| 172 | # if there is only one account in roster, take it as default |
|---|
| 173 | if not account and len(accounts) == 1: |
|---|
| 174 | account = accounts[0] |
|---|
| 175 | if account: |
|---|
| 176 | if gajim.connections[account].connected > 1: # account is online |
|---|
| 177 | connected_account = gajim.connections[account] |
|---|
| 178 | else: |
|---|
| 179 | for account in accounts: |
|---|
| 180 | if gajim.contacts[account].has_key(jid) and \ |
|---|
| 181 | gajim.connections[account].connected > 1: # account is online |
|---|
| 182 | connected_account = gajim.connections[account] |
|---|
| 183 | break |
|---|
| 184 | if connected_account: |
|---|
| 185 | res = connected_account.send_message(jid, message, keyID) |
|---|
| 186 | return True |
|---|
| 187 | return False |
|---|
| 188 | |
|---|
| 189 | def open_chat(self, *args): |
|---|
| 190 | ''' start_chat(jid, account=None) -> shows the tabbed window for new |
|---|
| 191 | message to 'jid', using account(optional) 'account ' ''' |
|---|
| 192 | if self.disabled: |
|---|
| 193 | return |
|---|
| 194 | jid, account = self._get_real_arguments(args, 2) |
|---|
| 195 | if not jid: |
|---|
| 196 | # FIXME: raise exception for missing argument (dbus0.35+ - released last week) |
|---|
| 197 | return None |
|---|
| 198 | if account: |
|---|
| 199 | accounts = [account] |
|---|
| 200 | else: |
|---|
| 201 | accounts = gajim.connections.keys() |
|---|
| 202 | if len(accounts) == 1: |
|---|
| 203 | account = accounts[0] |
|---|
| 204 | connected_account = None |
|---|
| 205 | for acct in accounts: |
|---|
| 206 | if gajim.connections[acct].connected > 1: # account is online |
|---|
| 207 | if self.plugin.windows[acct]['chats'].has_key(jid): |
|---|
| 208 | connected_account = acct |
|---|
| 209 | break |
|---|
| 210 | # jid is in roster |
|---|
| 211 | elif gajim.contacts[acct].has_key(jid): |
|---|
| 212 | connected_account = acct |
|---|
| 213 | break |
|---|
| 214 | # we send the message to jid not in roster, because account is specified, |
|---|
| 215 | # or there is only one account |
|---|
| 216 | elif account: |
|---|
| 217 | connected_account = acct |
|---|
| 218 | if connected_account: |
|---|
| 219 | self.plugin.roster.new_chat_from_jid(connected_account, jid) |
|---|
| 220 | # preserve the 'steal focus preservation' |
|---|
| 221 | win = self.plugin.windows[connected_account]['chats'][jid].window |
|---|
| 222 | if win.get_property('visible'): |
|---|
| 223 | win.window.focus() |
|---|
| 224 | return True |
|---|
| 225 | return False |
|---|
| 226 | |
|---|
| 227 | def change_status(self, *args, **keywords): |
|---|
| 228 | ''' change_status(status, message, account). account is optional - |
|---|
| 229 | if not specified status is changed for all accounts. ''' |
|---|
| 230 | if self.disabled: |
|---|
| 231 | return |
|---|
| 232 | status, message, account = self._get_real_arguments(args, 3) |
|---|
| 233 | if status not in ('offline', 'online', 'chat', |
|---|
| 234 | 'away', 'xa', 'dnd', 'invisible'): |
|---|
| 235 | # FIXME: raise exception for bad status (dbus0.35) |
|---|
| 236 | return None |
|---|
| 237 | if account: |
|---|
| 238 | gobject.idle_add(self.plugin.roster.send_status, account, |
|---|
| 239 | status, message) |
|---|
| 240 | else: |
|---|
| 241 | # account not specified, so change the status of all accounts |
|---|
| 242 | for acc in gajim.contacts.keys(): |
|---|
| 243 | gobject.idle_add(self.plugin.roster.send_status, acc, |
|---|
| 244 | status, message) |
|---|
| 245 | return None |
|---|
| 246 | |
|---|
| 247 | def show_next_unread(self, *args): |
|---|
| 248 | ''' Show the window(s) with next waiting messages in tabbed/group chats. ''' |
|---|
| 249 | if self.disabled: |
|---|
| 250 | return |
|---|
| 251 | #FIXME: when systray is disabled this method does nothing. |
|---|
| 252 | #FIXME: show message from GC that refer to us (like systray does) |
|---|
| 253 | if len(self.plugin.systray.jids) != 0: |
|---|
| 254 | account = self.plugin.systray.jids[0][0] |
|---|
| 255 | jid = self.plugin.systray.jids[0][1] |
|---|
| 256 | acc = self.plugin.windows[account] |
|---|
| 257 | jid_tab = None |
|---|
| 258 | if acc['gc'].has_key(jid): |
|---|
| 259 | jid_tab = acc['gc'][jid] |
|---|
| 260 | elif acc['chats'].has_key(jid): |
|---|
| 261 | jid_tab = acc['chats'][jid] |
|---|
| 262 | else: |
|---|
| 263 | self.plugin.roster.new_chat( |
|---|
| 264 | gajim.contacts[account][jid][0], account) |
|---|
| 265 | jid_tab = acc['chats'][jid] |
|---|
| 266 | if jid_tab: |
|---|
| 267 | jid_tab.set_active_tab(jid) |
|---|
| 268 | jid_tab.window.present() |
|---|
| 269 | # preserve the 'steal focus preservation' |
|---|
| 270 | if self._is_first(): |
|---|
| 271 | jid_tab.window.window.focus() |
|---|
| 272 | else: |
|---|
| 273 | jid_tab.window.window.focus(long(time())) |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | def contact_info(self, *args): |
|---|
| 277 | ''' get vcard info for a contact. This method returns nothing. |
|---|
| 278 | You have to register the 'VcardInfo' signal to get the real vcard. ''' |
|---|
| 279 | if self.disabled: |
|---|
| 280 | return |
|---|
| 281 | |
|---|
| 282 | [jid] = self._get_real_arguments(args, 1) |
|---|
| 283 | if not jid: |
|---|
| 284 | # FIXME: raise exception for missing argument (0.3+) |
|---|
| 285 | return None |
|---|
| 286 | |
|---|
| 287 | accounts = gajim.contacts.keys() |
|---|
| 288 | |
|---|
| 289 | for account in accounts: |
|---|
| 290 | if gajim.contacts[account].has_key(jid): |
|---|
| 291 | self.vcard_account = account |
|---|
| 292 | gajim.connections[account].register_handler('VCARD', |
|---|
| 293 | self._receive_vcard) |
|---|
| 294 | gajim.connections[account].request_vcard(jid) |
|---|
| 295 | break |
|---|
| 296 | return None |
|---|
| 297 | |
|---|
| 298 | def list_accounts(self, *args): |
|---|
| 299 | ''' list register accounts ''' |
|---|
| 300 | if self.disabled: |
|---|
| 301 | return |
|---|
| 302 | if gajim.contacts: |
|---|
| 303 | result = gajim.contacts.keys() |
|---|
| 304 | if result and len(result) > 0: |
|---|
| 305 | return result |
|---|
| 306 | return None |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | def list_contacts(self, *args): |
|---|
| 310 | if self.disabled: |
|---|
| 311 | return |
|---|
| 312 | ''' list all contacts in the roster. If the first argument is specified, |
|---|
| 313 | then return the contacts for the specified account ''' |
|---|
| 314 | [for_account] = self._get_real_arguments(args, 1) |
|---|
| 315 | result = [] |
|---|
| 316 | if not gajim.contacts or len(gajim.contacts) == 0: |
|---|
| 317 | return None |
|---|
| 318 | if for_account: |
|---|
| 319 | if gajim.contacts.has_key(for_account): |
|---|
| 320 | for jid in gajim.contacts[for_account]: |
|---|
| 321 | item = self._serialized_contacts( |
|---|
| 322 | gajim.contacts[for_account][jid]) |
|---|
| 323 | if item: |
|---|
| 324 | result.append(item) |
|---|
| 325 | else: |
|---|
| 326 | # 'for_account: is not recognised:', |
|---|
| 327 | # FIXME: there can be a return status for this [0.3+] |
|---|
| 328 | return None |
|---|
| 329 | else: |
|---|
| 330 | for account in gajim.contacts: |
|---|
| 331 | for jid in gajim.contacts[account]: |
|---|
| 332 | item = self._serialized_contacts(gajim.contacts[account][jid]) |
|---|
| 333 | if item: |
|---|
| 334 | result.append(item) |
|---|
| 335 | # dbus 0.40 does not support return result as empty list |
|---|
| 336 | if result == []: |
|---|
| 337 | return None |
|---|
| 338 | return result |
|---|
| 339 | |
|---|
| 340 | def toggle_roster_appearance(self, *args): |
|---|
| 341 | ''' shows/hides the roster window ''' |
|---|
| 342 | if self.disabled: |
|---|
| 343 | return |
|---|
| 344 | win = self.plugin.roster.window |
|---|
| 345 | if win.get_property('visible'): |
|---|
| 346 | gobject.idle_add(win.hide) |
|---|
| 347 | else: |
|---|
| 348 | win.present() |
|---|
| 349 | # preserve the 'steal focus preservation' |
|---|
| 350 | if self._is_first(): |
|---|
| 351 | win.window.focus() |
|---|
| 352 | else: |
|---|
| 353 | win.window.focus(long(time())) |
|---|
| 354 | |
|---|
| 355 | def _is_first(self): |
|---|
| 356 | if self.first_show: |
|---|
| 357 | self.first_show = False |
|---|
| 358 | return True |
|---|
| 359 | return False |
|---|
| 360 | |
|---|
| 361 | def _receive_vcard(self,account, array): |
|---|
| 362 | if self.vcard_account: |
|---|
| 363 | gajim.connections[self.vcard_account].unregister_handler('VCARD', |
|---|
| 364 | self._receive_vcard) |
|---|
| 365 | self.unregistered_vcard = None |
|---|
| 366 | if self.disabled: |
|---|
| 367 | return |
|---|
| 368 | if _version[1] >=30: |
|---|
| 369 | self.VcardInfo(repr(array)) |
|---|
| 370 | else: |
|---|
| 371 | self.emit_signal(INTERFACE, 'VcardInfo', |
|---|
| 372 | repr(array)) |
|---|
| 373 | |
|---|
| 374 | def _get_real_arguments(self, args, desired_length): |
|---|
| 375 | # supresses the first 'message' argument, which is set in dbus 0.23 |
|---|
| 376 | if _version[1] == 20: |
|---|
| 377 | args=args[1:] |
|---|
| 378 | if desired_length > 0: |
|---|
| 379 | args = list(args) |
|---|
| 380 | args.extend([None] * (desired_length - len(args))) |
|---|
| 381 | args = args[:desired_length] |
|---|
| 382 | return args |
|---|
| 383 | |
|---|
| 384 | def _serialized_contacts(self, contacts): |
|---|
| 385 | ''' get info from list of Contact objects and create a serialized |
|---|
| 386 | dict for sending it over dbus ''' |
|---|
| 387 | if not contacts: |
|---|
| 388 | return None |
|---|
| 389 | prim_contact = None # primary contact |
|---|
| 390 | for contact in contacts: |
|---|
| 391 | if prim_contact == None or contact.priority > prim_contact.priority: |
|---|
| 392 | prim_contact = contact |
|---|
| 393 | contact_dict = {} |
|---|
| 394 | contact_dict['name'] = prim_contact.name |
|---|
| 395 | contact_dict['show'] = prim_contact.show |
|---|
| 396 | contact_dict['jid'] = prim_contact.jid |
|---|
| 397 | if prim_contact.keyID: |
|---|
| 398 | keyID = None |
|---|
| 399 | if len(prim_contact.keyID) == 8: |
|---|
| 400 | keyID = prim_contact.keyID |
|---|
| 401 | elif len(prim_contact.keyID) == 16: |
|---|
| 402 | keyID = prim_contact.keyID[8:] |
|---|
| 403 | if keyID: |
|---|
| 404 | contact_dict['openpgp'] = keyID |
|---|
| 405 | contact_dict['resources'] = [] |
|---|
| 406 | for contact in contacts: |
|---|
| 407 | contact_dict['resources'].append(tuple([contact.resource, |
|---|
| 408 | contact.priority, contact.status])) |
|---|
| 409 | return repr(contact_dict) |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | if _version[1] >= 30 and _version[1] <= 40: |
|---|
| 413 | method = dbus.method |
|---|
| 414 | signal = dbus.signal |
|---|
| 415 | elif _version[1] >= 41: |
|---|
| 416 | method = dbus.service.method |
|---|
| 417 | signal = dbus.service.signal |
|---|
| 418 | |
|---|
| 419 | if _version[1] >= 30: |
|---|
| 420 | # prevent using decorators, because they are not supported |
|---|
| 421 | # on python < 2.4 |
|---|
| 422 | # FIXME: use decorators when python2.3 (and dbus 0.23) is OOOOOOLD |
|---|
| 423 | toggle_roster_appearance = method(INTERFACE)(toggle_roster_appearance) |
|---|
| 424 | list_contacts = method(INTERFACE)(list_contacts) |
|---|
| 425 | list_accounts = method(INTERFACE)(list_accounts) |
|---|
| 426 | show_next_unread = method(INTERFACE)(show_next_unread) |
|---|
| 427 | change_status = method(INTERFACE)(change_status) |
|---|
| 428 | open_chat = method(INTERFACE)(open_chat) |
|---|
| 429 | contact_info = method(INTERFACE)(contact_info) |
|---|
| 430 | send_message = method(INTERFACE)(send_message) |
|---|
| 431 | send_file = method(INTERFACE)(send_file) |
|---|
| 432 | VcardInfo = signal(INTERFACE)(VcardInfo) |
|---|
| 433 | |
|---|
| 434 | class SessionBusNotPresent(Exception): |
|---|
| 435 | ''' This exception indicates that there is no session daemon ''' |
|---|
| 436 | def __init__(self): |
|---|
| 437 | Exception.__init__(self) |
|---|
| 438 | |
|---|
| 439 | def __str__(self): |
|---|
| 440 | return _('Session bus is not available') |
|---|
| 441 | |
|---|
| 442 | class DbusNotSupported(Exception): |
|---|
| 443 | ''' D-Bus is not installed or python bindings are missing ''' |
|---|
| 444 | def __init__(self): |
|---|
| 445 | Exception.__init__(self) |
|---|
| 446 | |
|---|
| 447 | def __str__(self): |
|---|
| 448 | return _('D-Bus is not present on this machine') |
|---|