| 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 | |
|---|
| 25 | import os |
|---|
| 26 | import sys |
|---|
| 27 | |
|---|
| 28 | from common import exceptions |
|---|
| 29 | from common import i18n |
|---|
| 30 | _ = i18n._ |
|---|
| 31 | |
|---|
| 32 | try: |
|---|
| 33 | import dbus |
|---|
| 34 | version = getattr(dbus, 'version', (0, 20, 0)) |
|---|
| 35 | supported = True |
|---|
| 36 | except ImportError: |
|---|
| 37 | version = (0, 0, 0) |
|---|
| 38 | supported = False |
|---|
| 39 | if not os.name == 'nt': # only say that to non Windows users |
|---|
| 40 | print _('D-Bus python bindings are missing in this computer') |
|---|
| 41 | print _('D-Bus capabilities of Gajim cannot be used') |
|---|
| 42 | |
|---|
| 43 | # dbus 0.23 leads to segfault with threads_init() |
|---|
| 44 | if sys.version[:4] >= '2.4' and version[1] < 30: |
|---|
| 45 | supported = False |
|---|
| 46 | |
|---|
| 47 | if version >= (0, 41, 0): |
|---|
| 48 | import dbus.service |
|---|
| 49 | import dbus.glib # cause dbus 0.35+ doesn't return signal replies without it |
|---|
| 50 | |
|---|
| 51 | class SessionBus: |
|---|
| 52 | '''A Singleton for the DBus SessionBus''' |
|---|
| 53 | def __init__(self): |
|---|
| 54 | self.session_bus = None |
|---|
| 55 | |
|---|
| 56 | def SessionBus(self): |
|---|
| 57 | if not supported: |
|---|
| 58 | raise exceptions.DbusNotSupported |
|---|
| 59 | |
|---|
| 60 | if not self.present(): |
|---|
| 61 | raise exceptions.SessionBusNotPresent |
|---|
| 62 | return self.session_bus |
|---|
| 63 | |
|---|
| 64 | def bus(self): |
|---|
| 65 | return self.SessionBus() |
|---|
| 66 | |
|---|
| 67 | def present(self): |
|---|
| 68 | if not supported: |
|---|
| 69 | return False |
|---|
| 70 | if self.session_bus is None: |
|---|
| 71 | try: |
|---|
| 72 | self.session_bus = dbus.SessionBus() |
|---|
| 73 | except dbus.dbus_bindings.DBusException: |
|---|
| 74 | self.session_bus = None |
|---|
| 75 | return False |
|---|
| 76 | if self.session_bus is None: |
|---|
| 77 | return False |
|---|
| 78 | return True |
|---|
| 79 | |
|---|
| 80 | session_bus = SessionBus() |
|---|
| 81 | |
|---|
| 82 | def get_interface(interface, path): |
|---|
| 83 | '''Returns an interface on the current SessionBus. If the interface isn't |
|---|
| 84 | running, it tries to start it first.''' |
|---|
| 85 | if not supported: |
|---|
| 86 | return None |
|---|
| 87 | if session_bus.present(): |
|---|
| 88 | bus = session_bus.SessionBus() |
|---|
| 89 | else: |
|---|
| 90 | return None |
|---|
| 91 | try: |
|---|
| 92 | obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') |
|---|
| 93 | dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus') |
|---|
| 94 | running_services = dbus_iface.ListNames() |
|---|
| 95 | started = True |
|---|
| 96 | if interface not in running_services: |
|---|
| 97 | # try to start the service |
|---|
| 98 | if dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1: |
|---|
| 99 | started = True |
|---|
| 100 | else: |
|---|
| 101 | started = False |
|---|
| 102 | if not started: |
|---|
| 103 | return None |
|---|
| 104 | obj = bus.get_object(interface, path) |
|---|
| 105 | return dbus.Interface(obj, interface) |
|---|
| 106 | except Exception, e: |
|---|
| 107 | print >> sys.stderr, e |
|---|
| 108 | return None |
|---|
| 109 | except dbus.dbus_bindings.DBusException, e: |
|---|
| 110 | # This exception could give useful info about why notification breaks |
|---|
| 111 | print >> sys.stderr, e |
|---|
| 112 | return None |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | def get_notifications_interface(): |
|---|
| 116 | '''Returns the notifications interface.''' |
|---|
| 117 | return get_interface('org.freedesktop.Notifications','/org/freedesktop/Notifications') |
|---|