Package common :: Module dbus_support
[hide private]
[frames] | no frames]

Source Code for Module common.dbus_support

  1  # -*- coding:utf-8 -*- 
  2  ## src/common/dbus_support.py 
  3  ## 
  4  ## Copyright (C) 2005 Andrew Sayman <lorien420 AT myrealbox.com> 
  5  ##                    Dimitur Kirov <dkirov AT gmail.com> 
  6  ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com> 
  7  ## Copyright (C) 2005-2010 Yann Leboulanger <asterix AT lagaule.org> 
  8  ## Copyright (C) 2006 Jean-Marie Traissard <jim AT lapin.org> 
  9  ##                    Stefan Bethge <stefan AT lanpartei.de> 
 10  ## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org> 
 11  ## 
 12  ## This file is part of Gajim. 
 13  ## 
 14  ## Gajim is free software; you can redistribute it and/or modify 
 15  ## it under the terms of the GNU General Public License as published 
 16  ## by the Free Software Foundation; version 3 only. 
 17  ## 
 18  ## Gajim is distributed in the hope that it will be useful, 
 19  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 21  ## GNU General Public License for more details. 
 22  ## 
 23  ## You should have received a copy of the GNU General Public License 
 24  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 25  ## 
 26   
 27  import os, sys 
 28   
 29  from common import gajim 
 30  from common import exceptions 
 31   
 32  _GAJIM_ERROR_IFACE = 'org.gajim.dbus.Error' 
 33   
 34  try: 
 35      import dbus 
 36      import dbus.glib 
 37  except ImportError: 
 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  else: 
 43      try: 
 44          # test if dbus-x11 is installed 
 45          bus = dbus.SystemBus() 
 46          bus = dbus.SessionBus() 
 47          supported = True # does user have D-Bus bindings? 
 48      except dbus.DBusException: 
 49          supported = False 
 50          if not os.name == 'nt': # only say that to non Windows users 
 51              print _('D-Bus does not run correctly on this machine') 
 52              print _('D-Bus capabilities of Gajim cannot be used') 
 53      except exceptions.SystemBusNotPresent: 
 54          print _('D-Bus does not run correctly on this machine: system bus not ' 
 55              'present') 
 56      except exceptions.SessionBusNotPresent: 
 57          print _('D-Bus does not run correctly on this machine: session bus not ' 
 58              'present') 
 59   
60 -class SystemBus:
61 """ 62 A Singleton for the DBus SystemBus 63 """ 64
65 - def __init__(self):
66 self.system_bus = None
67
68 - def SystemBus(self):
69 if not supported: 70 raise exceptions.DbusNotSupported 71 72 if not self.present(): 73 raise exceptions.SystemBusNotPresent 74 return self.system_bus
75
76 - def bus(self):
77 return self.SystemBus()
78
79 - def present(self):
80 if not supported: 81 return False 82 if self.system_bus is None: 83 try: 84 self.system_bus = dbus.SystemBus() 85 except dbus.DBusException: 86 self.system_bus = None 87 return False 88 if self.system_bus is None: 89 return False 90 # Don't exit Gajim when dbus is stopped 91 self.system_bus.set_exit_on_disconnect(False) 92 return True
93 94 system_bus = SystemBus() 95
96 -class SessionBus:
97 """ 98 A Singleton for the D-Bus SessionBus 99 """ 100
101 - def __init__(self):
102 self.session_bus = None
103
104 - def SessionBus(self):
105 if not supported: 106 raise exceptions.DbusNotSupported 107 108 if not self.present(): 109 raise exceptions.SessionBusNotPresent 110 return self.session_bus
111
112 - def bus(self):
113 return self.SessionBus()
114
115 - def present(self):
116 if not supported: 117 return False 118 if self.session_bus is None: 119 try: 120 self.session_bus = dbus.SessionBus() 121 except dbus.DBusException: 122 self.session_bus = None 123 return False 124 if self.session_bus is None: 125 return False 126 return True
127 128 session_bus = SessionBus() 129
130 -def get_interface(interface, path, start_service=True):
131 """ 132 Get an interface on the current SessionBus. If the interface isn't running, 133 try to start it first 134 """ 135 if not supported: 136 return None 137 if session_bus.present(): 138 bus = session_bus.SessionBus() 139 else: 140 return None 141 try: 142 obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') 143 dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus') 144 running_services = dbus_iface.ListNames() 145 started = True 146 if interface not in running_services: 147 # try to start the service 148 if start_service and dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1: 149 started = True 150 else: 151 started = False 152 if not started: 153 return None 154 obj = bus.get_object(interface, path) 155 return dbus.Interface(obj, interface) 156 except Exception, e: 157 gajim.log.debug(str(e)) 158 return None
159 160
161 -def get_notifications_interface(notif=None):
162 """ 163 Get the notifications interface 164 165 :param notif: DesktopNotification instance 166 """ 167 # try to see if KDE notifications are available 168 iface = get_interface('org.kde.VisualNotifications', '/VisualNotifications', 169 start_service=False) 170 if iface != None: 171 if notif != None: 172 notif.kde_notifications = True 173 return iface 174 # KDE notifications don't seem to be available, falling back to 175 # notification-daemon 176 else: 177 if notif != None: 178 notif.kde_notifications = False 179 return get_interface('org.freedesktop.Notifications', 180 '/org/freedesktop/Notifications')
181 182 if supported:
183 - class MissingArgument(dbus.DBusException):
184 _dbus_error_name = _GAJIM_ERROR_IFACE + '.MissingArgument'
185
186 - class InvalidArgument(dbus.DBusException):
187 '''Raised when one of the provided arguments is invalid.''' 188 _dbus_error_name = _GAJIM_ERROR_IFACE + '.InvalidArgument'
189