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

Revision 3456, 13.1 kB (checked in by dkirov, 3 years ago)

fix: show vcard on contact_info

Line 
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
20import gobject
21import gtk
22import os
23
24from common import gajim
25from time import time
26from common import i18n
27
28_ = i18n._
29
30try:
31        import dbus
32        _version = getattr(dbus, 'version', (0, 20, 0)) 
33except ImportError:
34        _version = (0, 0, 0)
35       
36if _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
40elif _version >= (0, 20, 0):
41        DbusPrototype = dbus.Object
42else: #dbus is not defined
43        DbusPrototype = str 
44
45INTERFACE = 'org.gajim.dbus.RemoteInterface'
46OBJ_PATH = '/org/gajim/dbus/RemoteObject'
47SERVICE = 'org.gajim.dbus'
48
49class 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
79class 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                [jid] = self._get_real_arguments(args, 1)
282                if not isinstance(jid, unicode):
283                        jid = unicode(jid)
284                if not jid:
285                        # FIXME: raise exception for missing argument (0.3+)
286                        return None
287
288                accounts = gajim.contacts.keys()
289               
290                for account in accounts:
291                        if gajim.contacts[account].__contains__(jid):
292                                self.vcard_account =  account
293                                gajim.connections[account].request_vcard(jid)
294                                break
295                return None
296
297        def list_accounts(self, *args):
298                ''' list register accounts '''
299                if self.disabled:
300                        return
301                if gajim.contacts:
302                        result = gajim.contacts.keys()
303                        if result and len(result) > 0:
304                                result_array = []
305                                for account in result:
306                                        result_array.append(account.encode('utf-8'))
307                                return result_array
308                return None
309
310
311        def list_contacts(self, *args):
312                if self.disabled:
313                        return
314                ''' list all contacts in the roster. If the first argument is specified,
315                then return the contacts for the specified account '''
316                [for_account] = self._get_real_arguments(args, 1)
317                result = []
318                if not gajim.contacts or len(gajim.contacts) == 0:
319                        return None
320                if for_account:
321                        if gajim.contacts.has_key(for_account):
322                                for jid in gajim.contacts[for_account]:
323                                        item = self._serialized_contacts(
324                                                gajim.contacts[for_account][jid])
325                                        if item:
326                                                result.append(item)
327                        else:
328                                # 'for_account: is not recognised:',
329                                return None
330                else:
331                        for account in gajim.contacts:
332                                for jid in gajim.contacts[account]:
333                                        item = self._serialized_contacts(gajim.contacts[account][jid])
334                                        if item:
335                                                result.append(item)
336                # dbus 0.40 does not support return result as empty list
337                if result == []:
338                        return None
339                return result
340
341        def toggle_roster_appearance(self, *args):
342                ''' shows/hides the roster window '''
343                if self.disabled:
344                        return
345                win = self.plugin.roster.window
346                if win.get_property('visible'):
347                        gobject.idle_add(win.hide)
348                else:
349                        win.present()
350                        # preserve the 'steal focus preservation'
351                        if self._is_first():
352                                win.window.focus()
353                        else:
354                                win.window.focus(long(time()))
355
356        def _is_first(self):
357                if self.first_show:
358                        self.first_show = False
359                        return True
360                return False
361
362        def _get_real_arguments(self, args, desired_length):
363                # supresses the first 'message' argument, which is set in dbus 0.23
364                if _version[1] == 20:
365                        args=args[1:]
366                if desired_length > 0:
367                        args = list(args)
368                        args.extend([None] * (desired_length - len(args)))
369                        args = args[:desired_length]
370                return args
371
372        def _serialized_contacts(self, contacts):
373                ''' get info from list of Contact objects and create a serialized
374                dict for sending it over dbus '''
375                if not contacts:
376                        return None
377                prim_contact = None # primary contact
378                for contact in contacts:
379                        if prim_contact == None or contact.priority > prim_contact.priority:
380                                prim_contact = contact
381                contact_dict = {}
382                contact_dict['name'] = prim_contact.name
383                contact_dict['show'] = prim_contact.show
384                contact_dict['jid'] = prim_contact.jid
385                if prim_contact.keyID:
386                        keyID = None
387                        if len(prim_contact.keyID) == 8:
388                                keyID = prim_contact.keyID
389                        elif len(prim_contact.keyID) == 16:
390                                keyID = prim_contact.keyID[8:]
391                        if keyID:
392                                contact_dict['openpgp'] = keyID
393                contact_dict['resources'] = []
394                for contact in contacts:
395                        contact_dict['resources'].append(tuple([contact.resource, 
396                                contact.priority, contact.status]))
397                return repr(contact_dict)
398       
399       
400        if _version[1] >= 30 and _version[1] <= 40:
401                method = dbus.method
402                signal = dbus.signal
403        elif _version[1] >= 41:
404                method = dbus.service.method
405                signal = dbus.service.signal
406
407        if _version[1] >= 30:
408                # prevent using decorators, because they are not supported
409                # on python < 2.4
410                # FIXME: use decorators when python2.3 (and dbus 0.23) is OOOOOOLD
411                toggle_roster_appearance = method(INTERFACE)(toggle_roster_appearance)
412                list_contacts = method(INTERFACE)(list_contacts)
413                list_accounts = method(INTERFACE)(list_accounts)
414                show_next_unread = method(INTERFACE)(show_next_unread)
415                change_status = method(INTERFACE)(change_status)
416                open_chat = method(INTERFACE)(open_chat)
417                contact_info = method(INTERFACE)(contact_info)
418                send_message = method(INTERFACE)(send_message)
419                send_file = method(INTERFACE)(send_file)
420                VcardInfo = signal(INTERFACE)(VcardInfo)
421
422class SessionBusNotPresent(Exception):
423        ''' This exception indicates that there is no session daemon '''
424        def __init__(self):
425                Exception.__init__(self)
426
427        def __str__(self):
428                return _('Session bus is not available')
429
430class DbusNotSupported(Exception):
431        ''' D-Bus is not installed or python bindings are missing '''
432        def __init__(self):
433                Exception.__init__(self)
434
435        def __str__(self):
436                return _('D-Bus is not present on this machine')
Note: See TracBrowser for help on using the browser.