mirror of
https://github.com/nikdoof/oblogout.git
synced 2025-12-23 15:29:25 +00:00
Abstracted out the dbus class further, and fixed class logging
This commit is contained in:
@@ -37,7 +37,7 @@ class Usage(Exception):
|
||||
def main(argv = None):
|
||||
|
||||
# Start logger instace used by the OpenboxLogout class
|
||||
logger = logging.getLogger('oblogout')
|
||||
logger = logging.getLogger()
|
||||
logout = logging.StreamHandler(sys.stdout)
|
||||
logout.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
|
||||
logger.addHandler(logout)
|
||||
|
||||
@@ -26,41 +26,79 @@ import dbus
|
||||
|
||||
class DbusController (object):
|
||||
|
||||
""" DbusController handles all DBus actions required by OBLogout and acts
|
||||
as a middle layer between the application and Dbus"""
|
||||
|
||||
@property
|
||||
def _sysbus (self):
|
||||
"""System DBus"""
|
||||
if not hasattr (DbusController, "__sysbus"):
|
||||
DbusController.__sysbus = dbus.SystemBus ()
|
||||
return DbusController.__sysbus
|
||||
|
||||
@property
|
||||
def _sessbus (self):
|
||||
"""Session DBus"""
|
||||
if not hasattr (DbusController, "__sessbus"):
|
||||
DbusController.__sessbus = dbus.SessionBus ()
|
||||
return DbusController.__sessbus
|
||||
|
||||
@property
|
||||
def _polkit (self):
|
||||
"""PolicyKit object"""
|
||||
if not hasattr (DbusController, "__polkit"):
|
||||
pk = self._sysbus.get_object ("org.freedesktop.PolicyKit", "/")
|
||||
DbusController.__polkit = dbus.Interface(pk, 'org.freedesktop.PolicyKit')
|
||||
return DbusController.__polkit
|
||||
|
||||
@property
|
||||
def _halpm (self):
|
||||
"""HAL controller object"""
|
||||
if not hasattr (DbusController, "__halpm"):
|
||||
hal = self._sysbus.get_object ("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")
|
||||
DbusController.__halpm = dbus.Interface(hal, "org.freedesktop.Hal.Device.SystemPowerManagement")
|
||||
return DbusController.__halpm
|
||||
|
||||
@property
|
||||
def _authagent (self):
|
||||
"""AuthenticationAgent object"""
|
||||
if not hasattr (DbusController, "__authagent"):
|
||||
autha = self._sessbus.get_object ("org.freedesktop.PolicyKit.AuthenticationAgent", "/")
|
||||
DbusController.__authagent = dbus.Interface(autha,'org.freedesktop.PolicyKit.AuthenticationAgent')
|
||||
|
||||
return DbusController.__authagent
|
||||
|
||||
def __init__(self):
|
||||
self.logger = logging.getLogger('oblogout.obdbus')
|
||||
self.logger = logging.getLogger(self.__class__.__name__)
|
||||
|
||||
def __check_perms(self, id):
|
||||
""" Check if we have permissions for a action """
|
||||
|
||||
self.logger.debug('Checking permissions for %s' % id)
|
||||
|
||||
kit = dbus.SystemBus().get_object('org.freedesktop.PolicyKit','/')
|
||||
if(kit == None):
|
||||
print("Error: Could not get PolicyKit D-Bus Interface\n")
|
||||
return False
|
||||
#try:
|
||||
res = self._polkit.IsProcessAuthorized(id, dbus.UInt32(os.getpid()), False)
|
||||
#except:
|
||||
# return False
|
||||
|
||||
try:
|
||||
res = kit.IsProcessAuthorized(id, dbus.UInt32(os.getpid()), False)
|
||||
except:
|
||||
return False
|
||||
|
||||
if res:
|
||||
self.logging.debug("Authorised to use %s" % id)
|
||||
if res == "yes":
|
||||
self.logger.debug("Authorised to use %s, res = %s" % (id, res))
|
||||
return True
|
||||
else:
|
||||
self.logger.debug("Not authorised to use, requires %s" % res)
|
||||
return False
|
||||
|
||||
def __auth_perms(self, id):
|
||||
""" Check if we have permissions for a action, if not, try to obtain them via PolicyKit """
|
||||
|
||||
if self.__check_perms(id):
|
||||
return True
|
||||
else:
|
||||
|
||||
kit = dbus.SessionBus().get_object('org.freedesktop.PolicyKit.AuthenticationAgent','/')
|
||||
if(kit == None):
|
||||
print("Error: Could not get PolicyKit D-Bus Interface\n")
|
||||
return False
|
||||
|
||||
return kit.ObtainAuthorization(id, dbus.UInt32(0), dbus.UInt32(os.getpid()))
|
||||
else:
|
||||
|
||||
self.logger.debug('Attempting to obtain %s' % id)
|
||||
grant = self._authagent.ObtainAuthorization(id, dbus.UInt32(0), dbus.UInt32(os.getpid()), timeout=300, dbus_interface = "org.freedesktop.PolicyKit.AuthenticationAgent")
|
||||
self.logger.debug("Result: %s" % bool(grant))
|
||||
return bool(grant)
|
||||
|
||||
def __get_sessions(self):
|
||||
""" Using DBus and ConsoleKit, get the number of sessions. This is used by PolicyKit to dictate the
|
||||
@@ -76,7 +114,6 @@ class DbusController (object):
|
||||
seat_obj = dbus.SystemBus().get_object ('org.freedesktop.ConsoleKit', sid)
|
||||
seat = dbus.Interface (seat_obj, 'org.freedesktop.ConsoleKit.Seat')
|
||||
cnt += len(seat.GetSessions())
|
||||
#print len(seat.GetSessions())
|
||||
|
||||
return cnt
|
||||
|
||||
@@ -84,15 +121,12 @@ class DbusController (object):
|
||||
def check_ability(self, action):
|
||||
"""Check if HAL can complete action type requests, for example, suspend, hiberate, and safesuspend"""
|
||||
|
||||
dbus_hal = dbus.SystemBus().get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")
|
||||
pm = dbus.Interface(dbus_hal, "org.freedesktop.Hal.Device.SystemPowerManagement")
|
||||
|
||||
if action == 'suspend':
|
||||
return pm.CanSuspend
|
||||
return self._halpm.CanSuspend
|
||||
elif action == 'hibernate':
|
||||
return pm.CanHibernate
|
||||
return self._halpm.CanHibernate
|
||||
elif action == 'safesuspend':
|
||||
if not pm.CanHibernate or not pm.CanSuspend:
|
||||
if not self._halpm.CanHibernate or not pm.CanSuspend:
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -101,37 +135,42 @@ class DbusController (object):
|
||||
"""Restart the system via HAL, if we do not have permissions to do so obtain them via PolicyKit"""
|
||||
|
||||
if self.__get_sessions() > 1:
|
||||
if self.__auth_perms("org.freedesktop.hal.power-management.reboot-multiple-sessions"):
|
||||
dbus_hal = dbus.SystemBus().get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")
|
||||
pm = dbus.Interface(dbus_hal, "org.freedesktop.Hal.Device.SystemPowerManagement")
|
||||
return pm.Reboot()
|
||||
else:
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.reboot-multiple-sessions"):
|
||||
return False
|
||||
else:
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.reboot"):
|
||||
return False
|
||||
|
||||
self.logger.debug("Rebooting...")
|
||||
return self._halpm.Reboot()
|
||||
|
||||
def shutdown(self):
|
||||
"""Shutdown the system via HAL, if we do not have permissions to do so obtain them via PolicyKit"""
|
||||
|
||||
if self.__get_sessions() > 1:
|
||||
if self.__auth_perms("org.freedesktop.hal.power-management.shutdown-multiple-sessions"):
|
||||
dbus_hal = dbus.SystemBus().get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")
|
||||
pm = dbus.Interface(dbus_hal, "org.freedesktop.Hal.Device.SystemPowerManagement")
|
||||
|
||||
return pm.Shutdown()
|
||||
else:
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.shutdown-multiple-sessions"):
|
||||
return False
|
||||
else:
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.shutdown"):
|
||||
return False
|
||||
|
||||
print
|
||||
return self._halpm.Shutdown()
|
||||
|
||||
def suspend(self):
|
||||
pass
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.suspend"):
|
||||
return False
|
||||
else:
|
||||
return self._halpm.Suspend()
|
||||
|
||||
def hibernate(self):
|
||||
pass
|
||||
if not self.__auth_perms("org.freedesktop.hal.power-management.hibernate"):
|
||||
return False
|
||||
else:
|
||||
return self._halpm.Hibernate()
|
||||
|
||||
def safesuspend(self):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
@@ -71,7 +71,7 @@ class OpenboxLogout():
|
||||
self.local_mode = False
|
||||
|
||||
# Start logger and gettext/i18n
|
||||
self.logger = logging.getLogger('oblogout')
|
||||
self.logger = logging.getLogger(self.__class__.__name__)
|
||||
|
||||
if self.local_mode:
|
||||
gettext.install('oblogout', 'mo', unicode=1)
|
||||
@@ -140,7 +140,7 @@ class OpenboxLogout():
|
||||
wh = (pb.get_width(),pb.get_height())
|
||||
pilimg = Image.fromstring("RGB", wh, pb.get_pixels())
|
||||
|
||||
pilimg = pilimg.point(lambda p: p * self.opacity / 100)
|
||||
pilimg = pilimg.point(lambda p: (p * self.opacity) / 100)
|
||||
|
||||
# "Convert" the PIL to Pixbuf via PixbufLoader
|
||||
buf = StringIO.StringIO()
|
||||
|
||||
Reference in New Issue
Block a user