home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / gdmactions.py < prev    next >
Encoding:
Python Source  |  2007-04-09  |  5.7 KB  |  206 lines

  1. import dbus, dbus.glib
  2. from gettext import gettext as _
  3. import deskbar, deskbar.Indexer, deskbar.Handler, deskbar.Utils
  4. from deskbar.defs import VERSION
  5. import gdmclient
  6. import gtk, gnome, gnome.ui
  7.  
  8. HANDLERS = {
  9.     "GdmHandler" : {
  10.         "name": _("Computer Actions"),
  11.         "description": _("Logoff, shutdown, restart, suspend and related actions."),
  12.         "version": VERSION,
  13.     }
  14. }
  15.  
  16. class GpmMatch(deskbar.Match.Match):
  17.     def __init__(self, backend, name=None, **args):
  18.         deskbar.Match.Match.__init__(self, backend, name=name, **args)
  19.         bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  20.         obj = bus.get_object('org.gnome.PowerManager', '/org/gnome/PowerManager')
  21.         self._gpm = dbus.Interface (obj, "org.gnome.PowerManager")
  22.  
  23.     def get_category(self):
  24.         return "actions"
  25.  
  26.  
  27. class SuspendMatch(GpmMatch):
  28.     def __init__(self, backend, name=None, **args):
  29.         GpmMatch.__init__(self, backend, name)
  30.         self._icon = deskbar.Utils.load_icon("gpm-suspend-to-ram.png")
  31.  
  32.     def action(self, text=None):
  33.         try:
  34.             self._gpm.Suspend()
  35.         except dbus.DBusException:
  36.             # this will trigger a method timeout exception.
  37.             # As a workaround we swallow it silently
  38.             pass
  39.  
  40.     def get_category(self):
  41.         return "actions"
  42.  
  43.     def get_verb(self):
  44.         return _("Suspend the machine")
  45.  
  46. class HibernateMatch(GpmMatch):
  47.     def __init__(self, backend, name=None, **args):
  48.         GpmMatch.__init__(self, backend, name)
  49.         self._icon = deskbar.Utils.load_icon("gpm-suspend-to-disk.png")
  50.  
  51.     def action(self, text=None):
  52.         try:
  53.             self._gpm.Hibernate()
  54.         except dbus.DBusException:
  55.             # this will trigger a method timeout exception.
  56.             # As a workaround we swallow it silently
  57.             pass
  58.  
  59.     def get_verb(self):
  60.         return _("Hibernate the machine")
  61.  
  62. class ShutdownMatch(GpmMatch):
  63.     def __init__(self, backend, name=None, **args):
  64.         GpmMatch.__init__(self, backend, name)
  65.         self._icon = deskbar.Utils.load_icon(gtk.STOCK_QUIT) 
  66.  
  67.     def action(self, text=None):
  68.         try:
  69.             self._gpm.Shutdown()
  70.         except dbus.DBusException:
  71.             # this will trigger a method timeout exception.
  72.             # As a workaround we swallow it silently
  73.             pass
  74.  
  75.     def get_verb(self):
  76.         return _("Shutdown the machine")
  77.  
  78. class LockScreenMatch(deskbar.Match.Match):
  79.     def __init__(self, backend, name=None, **args):
  80.         deskbar.Match.Match.__init__(self, backend, name=name, **args)
  81.         self._icon = deskbar.Utils.load_icon(gtk.STOCK_FULLSCREEN)
  82.         
  83.         bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  84.         obj = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
  85.         # FIXME : This timeouts ?
  86.         self._scrsvr = dbus.Interface (obj, "org.gnome.ScreenSaver")
  87.  
  88.     def action(self, text=None):
  89.         try:
  90.             self._scrsvr.Lock()
  91.         except dbus.DBusException:
  92.             # this will trigger a method timeout exception.
  93.             # As a workaround we swallow it silently
  94.             pass
  95.  
  96.     def get_category(self):
  97.         return "actions"
  98.  
  99.     def get_verb(self):
  100.         return _("Lock the screen")
  101.  
  102. class GdmMatch(deskbar.Match.Match):
  103.     def __init__(self, backend, name, **args):
  104.         deskbar.Match.Match.__init__(self, backend, name=name, **args)
  105.         self.logout_reentrance = 0
  106.         
  107.     def get_category(self):
  108.         return "actions"
  109.  
  110.     def request_logout(self):
  111.         if self.logout_reentrance == 0:
  112.             self.logout_reentrance += 1
  113.  
  114.             client = gnome.ui.master_client()
  115.             if client:
  116.                 client.request_save(gnome.ui.SAVE_GLOBAL,
  117.                     True, # Shutdown?
  118.                     gnome.ui.INTERACT_ANY,
  119.                     True, # Fast?
  120.                     True) # Global?
  121.  
  122.                 self.logout_reentrance -= 1
  123.             
  124. class GdmShutdownMatch(GdmMatch):
  125.     def __init__(self, backend, **args):
  126.         GdmMatch.__init__(self, backend, _("Shut Down"), **args)
  127.         
  128.     def action(self, text=None):
  129.         gdmclient.set_logout_action(gdmclient.LOGOUT_ACTION_SHUTDOWN)
  130.         self.request_logout()
  131.         
  132.     def get_verb(self):
  133.         return _("Turn off the computer")
  134.  
  135. class GdmLogoutMatch(GdmMatch):
  136.     def __init__(self, backend, **args):
  137.         GdmMatch.__init__(self, backend, _("Log Out"), **args)
  138.         
  139.     def action(self, text=None):
  140.         gdmclient.set_logout_action(gdmclient.LOGOUT_ACTION_NONE)
  141.         self.request_logout()
  142.         
  143.     def get_verb(self):
  144.         return _("Log out")
  145.         
  146. class GdmRebootMatch(GdmMatch):
  147.     def __init__(self, backend, **args):
  148.         GdmMatch.__init__(self, backend, _("Restart"), **args)
  149.         
  150.     def action(self, text=None):
  151.         gdmclient.set_logout_action(gdmclient.LOGOUT_ACTION_REBOOT)
  152.         self.request_logout()
  153.         
  154.     def get_verb(self):
  155.         return _("Restart the computer")
  156.         
  157. class GdmSwitchUserMatch(GdmMatch):
  158.     def __init__(self, backend, **args):
  159.         GdmMatch.__init__(self, backend, _("Switch User"), **args)
  160.         
  161.     def action(self, text=None):
  162.         gdmclient.new_login()
  163.         
  164.     def get_verb(self):
  165.         return _("Switch User")
  166.                 
  167. class GdmHandler(deskbar.Handler.Handler):
  168.     def __init__(self):
  169.         deskbar.Handler.Handler.__init__(self, "gpm-suspend-to-ram.png")    
  170.         self.indexer = deskbar.Indexer.Indexer()
  171.         
  172.     def initialize(self):
  173.         for klass in (GdmShutdownMatch,GdmSwitchUserMatch,GdmRebootMatch,GdmLogoutMatch):
  174.             match = klass(self)
  175.             self.indexer.add(match.get_verb(), match)
  176.         
  177.         self.init_gpm_matches()
  178.         self.init_screensaver_matches()
  179.  
  180.     def init_screensaver_matches(self):
  181.         try:
  182.             bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  183.             obj = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
  184.             scrsvr = dbus.Interface (obj, "org.gnome.ScreenSaver")
  185.             self.indexer.add(_("Lock"), LockScreenMatch(self))
  186.             return True
  187.         except dbus.DBusException:
  188.             return False
  189.  
  190.     def init_gpm_matches(self):
  191.         try:
  192.             bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  193.             obj = bus.get_object('org.gnome.PowerManager', '/org/gnome/PowerManager')
  194.             gpm = dbus.Interface (obj, "org.gnome.PowerManager")
  195.             if gpm.AllowedSuspend():
  196.                 self.indexer.add(_("Suspend"), SuspendMatch(self))
  197.             if gpm.AllowedHibernate():
  198.                 self.indexer.add(_("Hibernate"), HibernateMatch(self))
  199.             if gpm.AllowedShutdown():
  200.                 self.indexer.add(_("Shutdown"), ShutdownMatch(self))
  201.         except dbus.DBusException:
  202.             return False
  203.             
  204.     def query(self, query):
  205.         return self.indexer.look_up(query)
  206.