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

Revision 4868, 3.3 kB (checked in by nk, 3 years ago)

missing import

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