2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/dbus_support.py
|
2005-12-10 01:56:38 +01:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005 Andrew Sayman <lorien420 AT myrealbox.com>
|
|
|
|
## Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Stefan Bethge <stefan AT lanpartei.de>
|
|
|
|
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
|
2005-12-10 01:56:38 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-12-10 01:56:38 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-12-10 01:56:38 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-12-10 01:56:38 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-12-10 01:56:38 +01:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-12-10 01:56:38 +01:00
|
|
|
|
2016-12-22 17:47:53 +01:00
|
|
|
import os
|
2005-12-10 01:56:38 +01:00
|
|
|
|
2005-12-26 15:59:36 +01:00
|
|
|
from common import gajim
|
2005-12-10 01:56:38 +01:00
|
|
|
from common import exceptions
|
|
|
|
|
2006-09-29 22:49:15 +02:00
|
|
|
_GAJIM_ERROR_IFACE = 'org.gajim.dbus.Error'
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
import dbus
|
2012-08-11 22:46:39 +02:00
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
DBusGMainLoop(set_as_default=True)
|
2005-12-10 01:56:38 +01:00
|
|
|
except ImportError:
|
2010-02-08 15:08:40 +01:00
|
|
|
supported = False
|
|
|
|
if not os.name == 'nt': # only say that to non Windows users
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('D-Bus python bindings are missing in this computer'))
|
|
|
|
print(_('D-Bus capabilities of Gajim cannot be used'))
|
2009-08-28 12:09:18 +02:00
|
|
|
else:
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
# test if dbus-x11 is installed
|
2010-02-25 15:33:27 +01:00
|
|
|
bus = dbus.SystemBus()
|
2010-02-08 15:08:40 +01:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
supported = True # does user have D-Bus bindings?
|
|
|
|
except dbus.DBusException:
|
|
|
|
supported = False
|
|
|
|
if not os.name == 'nt': # only say that to non Windows users
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('D-Bus does not run correctly on this machine'))
|
|
|
|
print(_('D-Bus capabilities of Gajim cannot be used'))
|
2010-02-25 15:33:27 +01:00
|
|
|
except exceptions.SystemBusNotPresent:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('D-Bus does not run correctly on this machine: system bus not '
|
|
|
|
'present'))
|
2010-02-25 15:33:27 +01:00
|
|
|
except exceptions.SessionBusNotPresent:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('D-Bus does not run correctly on this machine: session bus not '
|
|
|
|
'present'))
|
2005-12-12 15:05:01 +01:00
|
|
|
|
2006-10-21 02:49:30 +02:00
|
|
|
class SystemBus:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
A Singleton for the DBus SystemBus
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.system_bus = None
|
|
|
|
|
|
|
|
def SystemBus(self):
|
|
|
|
if not supported:
|
|
|
|
raise exceptions.DbusNotSupported
|
|
|
|
|
|
|
|
if not self.present():
|
|
|
|
raise exceptions.SystemBusNotPresent
|
|
|
|
return self.system_bus
|
|
|
|
|
|
|
|
def bus(self):
|
|
|
|
return self.SystemBus()
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
if not supported:
|
|
|
|
return False
|
|
|
|
if self.system_bus is None:
|
|
|
|
try:
|
|
|
|
self.system_bus = dbus.SystemBus()
|
|
|
|
except dbus.DBusException:
|
|
|
|
self.system_bus = None
|
|
|
|
return False
|
|
|
|
if self.system_bus is None:
|
|
|
|
return False
|
|
|
|
# Don't exit Gajim when dbus is stopped
|
|
|
|
self.system_bus.set_exit_on_disconnect(False)
|
|
|
|
return True
|
2006-10-21 02:49:30 +02:00
|
|
|
|
|
|
|
system_bus = SystemBus()
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
class SessionBus:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
A Singleton for the D-Bus SessionBus
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.session_bus = None
|
|
|
|
|
|
|
|
def SessionBus(self):
|
|
|
|
if not supported:
|
|
|
|
raise exceptions.DbusNotSupported
|
|
|
|
|
|
|
|
if not self.present():
|
|
|
|
raise exceptions.SessionBusNotPresent
|
|
|
|
return self.session_bus
|
|
|
|
|
|
|
|
def bus(self):
|
|
|
|
return self.SessionBus()
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
if not supported:
|
|
|
|
return False
|
|
|
|
if self.session_bus is None:
|
|
|
|
try:
|
|
|
|
self.session_bus = dbus.SessionBus()
|
|
|
|
except dbus.DBusException:
|
|
|
|
self.session_bus = None
|
|
|
|
return False
|
|
|
|
if self.session_bus is None:
|
|
|
|
return False
|
|
|
|
return True
|
2005-12-10 01:56:38 +01:00
|
|
|
|
|
|
|
session_bus = SessionBus()
|
|
|
|
|
2009-02-01 11:28:48 +01:00
|
|
|
def get_interface(interface, path, start_service=True):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Get an interface on the current SessionBus. If the interface isn't running,
|
|
|
|
try to start it first
|
|
|
|
"""
|
|
|
|
if not supported:
|
|
|
|
return None
|
|
|
|
if session_bus.present():
|
|
|
|
bus = session_bus.SessionBus()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
|
|
|
|
dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus')
|
|
|
|
running_services = dbus_iface.ListNames()
|
|
|
|
started = True
|
|
|
|
if interface not in running_services:
|
|
|
|
# try to start the service
|
|
|
|
if start_service and dbus_iface.StartServiceByName(interface, dbus.UInt32(0)) == 1:
|
|
|
|
started = True
|
|
|
|
else:
|
|
|
|
started = False
|
|
|
|
if not started:
|
|
|
|
return None
|
|
|
|
obj = bus.get_object(interface, path)
|
|
|
|
return dbus.Interface(obj, interface)
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.log.debug(str(e))
|
|
|
|
return None
|
2005-12-10 01:56:38 +01:00
|
|
|
|
|
|
|
|
2009-02-01 11:28:48 +01:00
|
|
|
def get_notifications_interface(notif=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Get the notifications interface
|
|
|
|
|
|
|
|
:param notif: DesktopNotification instance
|
|
|
|
"""
|
|
|
|
# try to see if KDE notifications are available
|
|
|
|
iface = get_interface('org.kde.VisualNotifications', '/VisualNotifications',
|
|
|
|
start_service=False)
|
|
|
|
if iface != None:
|
|
|
|
if notif != None:
|
|
|
|
notif.kde_notifications = True
|
|
|
|
return iface
|
|
|
|
# KDE notifications don't seem to be available, falling back to
|
|
|
|
# notification-daemon
|
|
|
|
else:
|
|
|
|
if notif != None:
|
|
|
|
notif.kde_notifications = False
|
|
|
|
return get_interface('org.freedesktop.Notifications',
|
|
|
|
'/org/freedesktop/Notifications')
|
2006-09-29 22:49:15 +02:00
|
|
|
|
|
|
|
if supported:
|
2010-02-08 15:08:40 +01:00
|
|
|
class MissingArgument(dbus.DBusException):
|
|
|
|
_dbus_error_name = _GAJIM_ERROR_IFACE + '.MissingArgument'
|
2008-12-03 22:56:12 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
class InvalidArgument(dbus.DBusException):
|
|
|
|
'''Raised when one of the provided arguments is invalid.'''
|
|
|
|
_dbus_error_name = _GAJIM_ERROR_IFACE + '.InvalidArgument'
|