def __init__(self):
     """init"""
     gtk.VBox.__init__(self, spacing=5)
     self._dbus_loop()
     self.iface = ServiceIface()
     self.items = []
     self.container = ServiceBox(self.on_item)
     self.iface.listen(self.listen_comar)
     self._create_ui()
     self.services = sorted(self.iface.services())
     self.add_items()
     self.iface.services(self.get_infos)
class ServiceManager(gtk.VBox):
    """Main widget of service_manager_gtk"""
    def __init__(self):
        """init"""
        gtk.VBox.__init__(self, spacing=5)
        self._dbus_loop()
        self.iface = ServiceIface()
        self.items = []
        self.container = ServiceBox(self.on_item)
        self.iface.listen(self.listen_comar)
        self._create_ui()
        self.services = sorted(self.iface.services())
        self.add_items()
        self.iface.services(self.get_infos)
    def _dbus_loop(self):
        #runs dbus main loop
        DBusGMainLoop(set_as_default = True)
    def _create_ui(self):
        self.pack_start(self.container,
                        fill=True, expand=True)
        self.container.show()
    def add_items(self):
        """adds services to ServiceBox"""
        for service in self.services:
            self.container.add_item(service)
    def get_infos(self, name, exception, args):
        """get service info

        Arguments:
        - `name`: service name
        - `exception`: exception
        - `args`: args
        """
        if not exception:
            self.container.set_item(name, args)
    def on_item(self, widget, data):
        """listen ServiceItem signals

        Arguments:
        - `widget`: widget
        - `data': {action:(start|stop|restart|auto),
                   name:...
                   item:ServiceItem object
        """
        action = data['action']
        service_name = data['name']
        item = data['item']
        try:
            if action == 'start':
                if not item.is_running():
                    self.iface.start(service_name)
            elif action == 'stop':
                if item.is_running():
                    self.iface.stop(service_name)
            elif action == 'restart':
                if item.is_running():
                    self.iface.restart(service_name)
            elif action == 'auto':
                self.iface.setEnable(service_name,
                                     not widget.get_active())
        except Exception, e:
            if "Comar.PolicyKit" in e._dbus_error_name:
                open_error_dialog(_('Access Denied'))
            else:
                open_error_dialog(unicode(e))