#!/usr/bin/env python
from gi.repository import Gtk
import dbus

class Inhibitor:
    def __init__(self):
        mainWindow = Gtk.Window()
        mainWindow.set_title ("Inhibitor")
        mainWindow.set_default_size (50, 50)
        mainWindow.set_resizable (False)
        mainWindow.connect("destroy", self.on_mainWindow_destroy)

        hintLabel = Gtk.Label ("Activate power saving inhibitor")
        hintLabel.set_line_wrap (True)
        hintLabel.set_width_chars (15)

        inhibitSwitch = Gtk.Switch ()
        inhibitSwitch.connect("notify::active", self.switch_activated_cb)

        boxLayout = Gtk.VBox (spacing=5, homogeneous=False)
        boxLayout.pack_start (hintLabel, False, False, 3)
        boxLayout.pack_start (inhibitSwitch, False, False, 3)

        mainWindow.add(boxLayout)
        mainWindow.show_all()

        bus = dbus.SessionBus()

        proxy = bus.get_object ('org.gnome.SessionManager',
                                '/org/gnome/SessionManager')

        self.sessionManager = dbus.Interface (proxy, 'org.gnome.SessionManager')
        self.cookie = 0


    def inhibit_gnome_screensaver (self, toggle):
      if toggle == True:
          self.cookie = self.sessionManager.Inhibit ("inhibtor 6000",
                                                     0,
                                                     "Manual override",
                                                     8)
      else:
        self.sessionManager.Uninhibit (self.cookie)

    def on_mainWindow_destroy(self, widget):
        Gtk.main_quit()

    def switch_activated_cb (self, switch, pspec):
       self.inhibit_gnome_screensaver (switch.get_active ())

if __name__ == "__main__":
    Inhibitor()
    Gtk.main()

