root/branches/gajim_0.10.1/src/dbus_support.py

Revision 5827, 3.0 kB (checked in by nk, 3 years ago)

fix copyright header in dbus_support.py

Line 
1##  dbus_support.py
2##
3## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
4## Copyright (C) 2005 Nikos Kouremenos <kourem@gmail.com>
5## Copyright (C) 2005 Dimitur Kirov <dkirov@gmail.com>
6## Copyright (C) 2005 Andrew Sayman <lorien420@myrealbox.com>
7##
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published
10## by the Free Software Foundation; version 2 only.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17
18import os
19import sys
20
21from common import gajim
22from common import exceptions
23from common import i18n
24_ = i18n._
25
26try:
27        import dbus
28        version = getattr(dbus, 'version', (0, 20, 0))
29        supported = True
30except ImportError:
31        version = (0, 0, 0)
32        supported = False
33        if not os.name == 'nt': # only say that to non Windows users
34                print _('D-Bus python bindings are missing in this computer')
35                print _('D-Bus capabilities of Gajim cannot be used')
36       
37# dbus 0.23 leads to segfault with threads_init()
38if sys.version[:4] >= '2.4' and version[1] < 30:
39        supported = False
40
41if version >= (0, 41, 0):
42        import dbus.service
43        import dbus.glib # cause dbus 0.35+ doesn't return signal replies without it
44
45class SessionBus:
46        '''A Singleton for the DBus SessionBus'''
47        def __init__(self):
48                self.session_bus = None
49       
50        def SessionBus(self):
51                if not supported:
52                        raise exceptions.DbusNotSupported
53
54                if not self.present():
55                                raise exceptions.SessionBusNotPresent
56                return self.session_bus
57
58        def bus(self):
59                return self.SessionBus()
60
61        def present(self):
62                if not supported:
63                        return False
64                if self.session_bus is None:
65                        try:
66                                self.session_bus = dbus.SessionBus()
67                        except dbus.dbus_bindings.DBusException:
68                                self.session_bus = None
69                                return False
70                        if self.session_bus is None:
71                                return False
72                return True
73
74session_bus = SessionBus()
75
76def get_interface(interface, path):
77        '''Returns an interface on the current SessionBus. If the interface isn't
78        running, it tries to start it first.'''
79        if not supported:
80                return None
81        if session_bus.present():
82                bus = session_bus.SessionBus()
83        else:
84                return None
85        try:
86                obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
87                dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus')
88                running_services = dbus_iface.ListNames()
89                started = True
90                if interface not in running_services:
91                        # try to start the service
92                        if dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1:
93                                started = True
94                        else:
95                                started = False
96                if not started:
97                        return None
98                obj = bus.get_object(interface, path)
99                return dbus.Interface(obj, interface)
100        except Exception, e:
101                gajim.log.debug(str(e))
102                return None
103
104
105def get_notifications_interface():
106        '''Returns the notifications interface.'''
107        return get_interface('org.freedesktop.Notifications','/org/freedesktop/Notifications')
Note: See TracBrowser for help on using the browser.