root/branches/gajim_0.11.1/src/common/dbus_support.py

Revision 8589, 3.7 kB (checked in by roidelapluie, 15 months ago)

Don't stop Gajim when dbus is stopped. Fix #3032.

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
19
20from common import gajim
21from common import exceptions
22
23_GAJIM_ERROR_IFACE = 'org.gajim.dbus.Error'
24
25try:
26        import dbus
27        import dbus.service
28        import dbus.glib
29        supported = True # does use have D-Bus bindings?
30except ImportError:
31        supported = False
32        if not os.name == 'nt': # only say that to non Windows users
33                print _('D-Bus python bindings are missing in this computer')
34                print _('D-Bus capabilities of Gajim cannot be used')
35
36class SystemBus:
37        '''A Singleton for the DBus SystemBus'''
38        def __init__(self):
39                self.system_bus = None
40       
41        def SystemBus(self):
42                if not supported:
43                        raise exceptions.DbusNotSupported
44
45                if not self.present():
46                                raise exceptions.SystemBusNotPresent
47                return self.system_bus
48
49        def bus(self):
50                return self.SystemBus()
51
52        def present(self):
53                if not supported:
54                        return False
55                if self.system_bus is None:
56                        try:
57                                self.system_bus = dbus.SystemBus()
58                        except dbus.DBusException:
59                                self.system_bus = None
60                                return False
61                        if self.system_bus is None:
62                                return False
63                        # Don't exit Gajim when dbus is stopped
64                        self.system_bus.set_exit_on_disconnect(False)
65                return True
66
67system_bus = SystemBus()
68
69class SessionBus:
70        '''A Singleton for the D-Bus SessionBus'''
71        def __init__(self):
72                self.session_bus = None
73       
74        def SessionBus(self):
75                if not supported:
76                        raise exceptions.DbusNotSupported
77
78                if not self.present():
79                                raise exceptions.SessionBusNotPresent
80                return self.session_bus
81
82        def bus(self):
83                return self.SessionBus()
84
85        def present(self):
86                if not supported:
87                        return False
88                if self.session_bus is None:
89                        try:
90                                self.session_bus = dbus.SessionBus()
91                        except dbus.DBusException:
92                                self.session_bus = None
93                                return False
94                        if self.session_bus is None:
95                                return False
96                return True
97
98session_bus = SessionBus()
99
100def get_interface(interface, path):
101        '''Returns an interface on the current SessionBus. If the interface isn't
102        running, it tries to start it first.'''
103        if not supported:
104                return None
105        if session_bus.present():
106                bus = session_bus.SessionBus()
107        else:
108                return None
109        try:
110                obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
111                dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus')
112                running_services = dbus_iface.ListNames()
113                started = True
114                if interface not in running_services:
115                        # try to start the service
116                        if dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1:
117                                started = True
118                        else:
119                                started = False
120                if not started:
121                        return None
122                obj = bus.get_object(interface, path)
123                return dbus.Interface(obj, interface)
124        except Exception, e:
125                gajim.log.debug(str(e))
126                return None
127
128
129def get_notifications_interface():
130        '''Returns the notifications interface.'''
131        return get_interface('org.freedesktop.Notifications',
132                '/org/freedesktop/Notifications')
133
134if supported:
135        class MissingArgument(dbus.DBusException):
136                _dbus_error_name = _GAJIM_ERROR_IFACE + '.MissingArgument'
137       
138        class InvalidArgument(dbus.DBusException):
139                '''Raised when one of the provided arguments is invalid.'''
140                _dbus_error_name = _GAJIM_ERROR_IFACE + '.InvalidArgument'
Note: See TracBrowser for help on using the browser.