class MainWidget(QtGui.QWidget, Ui_MainWidget): def __init__(self, parent, embed=False): QtGui.QWidget.__init__(self, parent) if embed: self.setupUi(parent) else: self.setupUi(self) # Animation self.animator = QtCore.QTimeLine(ANIM_TIME, self) self.animationLast = ANIM_HIDE # Initialize heights of animated widgets self.slotAnimationFinished() # Backend self.iface = Interface() self.iface.listenFirewallSignals(self.firewallSignalHandler) self.iface.listenServiceSignals(self.serviceSignalHandler) # Fail if no packages provide backend self.checkBackend() # We don't need a "new" button self.hideNew() # We don't need a filter self.hideFilter() # Build item list self.buildItemList() # Set service widget self.widgetService = ServiceWidget(self) self.widgetService.setState(self.iface.getState()) self.verticalLayout.insertWidget(0, self.widgetService) # Rule edit widget # TBD # Signals self.connect(self.comboFilter, QtCore.SIGNAL("currentIndexChanged(int)"), self.slotFilterChanged) self.connect(self.pushNew, QtCore.SIGNAL("triggered(QAction*)"), self.slotOpenEdit) self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.slotSaveEdit) self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.slotCancelEdit) self.connect(self.animator, QtCore.SIGNAL("frameChanged(int)"), self.slotAnimate) self.connect(self.animator, QtCore.SIGNAL("finished()"), self.slotAnimationFinished) self.connect(self.widgetService, QtCore.SIGNAL("stateChanged(int)"), self.slotServiceChanged) def checkBackend(self): """ Check if there are packages that provide required backend. """ if not len(self.iface.getPackages()): ctx.createMessage( self, "Error", "There are no packages that provide backend for this application.\nPlease make sure that packages are installed and configured correctly.", ) return False return True def firewallSignalHandler(self, package, signal, args): if signal == "stateChanged": state = args[0] self.widgetService.setState(state == "on") elif signal == "moduleStateChanged": pass elif signal == "moduleSettingsChanged": pass def serviceSignalHandler(self, package, signal, args): if package == self.iface.package and signal == "Changed": state = self.iface.getState() self.widgetService.setState(state) def clearItemList(self): """ Clears item list. """ self.listItems.clear() def hideNew(self): """ Hides new button. """ self.pushNew.hide() def hideFilter(self): """ Hide filter. """ self.comboFilter.hide() def makeItemWidget(self, id_, title="", description="", type_=None, icon=None, state=None): """ Makes an item widget having given properties. """ widget = ItemWidget(self.listItems, id_, title, description, type_, icon, state) self.connect(widget, QtCore.SIGNAL("stateChanged(int)"), self.slotItemState) self.connect(widget, QtCore.SIGNAL("editClicked()"), self.slotItemEdit) self.connect(widget, QtCore.SIGNAL("deleteClicked()"), self.slotItemDelete) return widget def addItem(self, id_, name="", description="", icon="security-medium", state=False): """ Adds an item to list. """ type_ = "" # Build widget and widget item widget = self.makeItemWidget(id_, name, description, type_, KIcon(icon), state) widgetItem = ItemListWidgetItem(self.listItems, widget) # Rules are static widget.hideDelete() # Add to list self.listItems.setItemWidget(widgetItem, widget) # Check if a filter matches item if not self.itemMatchesFilter(widgetItem): self.listItems.setItemHidden(widgetItem, True) def buildItemList(self): """ Builds item list. """ # Clear list self.clearItemList() def handler(package, exception, args): if exception: return modules = args[0] for name in modules: title, description, icon = self.iface.moduleInfo(name) state = QtCore.Qt.Unchecked if self.iface.getModuleState(name) == "on": state = QtCore.Qt.Checked self.addItem(name, title, description, icon, state) self.iface.listModules(handler) def itemMatchesFilter(self, item): """ Checks if item matches selected filter. """ filter = str(self.comboFilter.itemData(self.comboFilter.currentIndex()).toString()) if filter == "incoming" and item.getType() != "incoming": return False elif filter == "outcoming" and item.getType() != "outcoming": return False return True def buildFilter(self): """ Builds item filter. """ self.comboFilter.clear() self.comboFilter.addItem(i18n("All"), QtCore.QVariant("all")) def buildMenu(self): """ Builds "Add New" button menu. """ # Create menu for "new" button menu = QtGui.QMenu(self.pushNew) self.pushNew.setMenu(menu) # New item action = QtGui.QAction(i18n("Action"), self) action.setData(QtCore.QVariant("action")) menu.addAction(action) def showEditBox(self, id_, type_): """ Shows edit box. """ if self.animationLast == ANIM_HIDE: self.animationLast = ANIM_SHOW # Set range self.animator.setFrameRange(ANIM_TARGET, self.height() - ANIM_TARGET) # Go go go! self.animator.start() def hideEditBox(self): """ Hides edit box. """ if self.animationLast == ANIM_SHOW: self.animationLast = ANIM_HIDE # Set range self.animator.setFrameRange(self.frameEdit.height(), ANIM_TARGET) # Go go go! self.animator.start() def slotFilterChanged(self, index): """ Filter is changed, refresh item list. """ for i in range(self.listItems.count()): widgetItem = self.listItems.item(i) if self.itemMatchesFilter(widgetItem): self.listItems.setItemHidden(widgetItem, False) else: self.listItems.setItemHidden(widgetItem, True) def slotItemState(self, state): """ Item state changed. """ widget = self.sender() try: self.iface.setModuleState(widget.getId(), state == QtCore.Qt.Checked) except Exception, e: if "Comar.PolicyKit" in e._dbus_error_name: ctx.createMessage(self, "Error", "Access denied.") else: ctx.createMessage(self, "Error", unicode(e)) self.buildItemList()
class MainWidget(QtGui.QWidget, Ui_MainWidget): def __init__(self, parent, embed=False): QtGui.QWidget.__init__(self, parent) if embed: self.setupUi(parent) else: self.setupUi(self) # Animation self.animator = QtCore.QTimeLine(ANIM_TIME, self) self.animationLast = ANIM_HIDE # Initialize heights of animated widgets self.slotAnimationFinished() # Backend self.iface = Interface() self.iface.listenFirewallSignals(self.firewallSignalHandler) self.iface.listenServiceSignals(self.serviceSignalHandler) # Fail if no packages provide backend self.checkBackend() # We don't need a "new" button self.hideNew() # We don't need a filter self.hideFilter() # Build item list self.buildItemList() # Set service widget self.widgetService = ServiceWidget(self) self.widgetService.setState(self.iface.getState()) self.verticalLayout.insertWidget(0, self.widgetService) # Rule edit widget # TBD # Signals self.connect(self.comboFilter, QtCore.SIGNAL("currentIndexChanged(int)"), self.slotFilterChanged) self.connect(self.pushNew, QtCore.SIGNAL("triggered(QAction*)"), self.slotOpenEdit) self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.slotSaveEdit) self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.slotCancelEdit) self.connect(self.animator, QtCore.SIGNAL("frameChanged(int)"), self.slotAnimate) self.connect(self.animator, QtCore.SIGNAL("finished()"), self.slotAnimationFinished) self.connect(self.widgetService, QtCore.SIGNAL("stateChanged(int)"), self.slotServiceChanged) def checkBackend(self): """ Check if there are packages that provide required backend. """ if not len(self.iface.getPackages()): ctx.createMessage( self, "Error", "There are no packages that provide backend for this application.\nPlease make sure that packages are installed and configured correctly." ) return False return True def firewallSignalHandler(self, package, signal, args): if signal == "stateChanged": state = args[0] self.widgetService.setState(state == "on") elif signal == "moduleStateChanged": pass elif signal == "moduleSettingsChanged": pass def serviceSignalHandler(self, package, signal, args): if package == self.iface.package and signal == "Changed": state = self.iface.getState() self.widgetService.setState(state) def clearItemList(self): """ Clears item list. """ self.listItems.clear() def hideNew(self): """ Hides new button. """ self.pushNew.hide() def hideFilter(self): """ Hide filter. """ self.comboFilter.hide() def makeItemWidget(self, id_, title="", description="", type_=None, icon=None, state=None): """ Makes an item widget having given properties. """ widget = ItemWidget(self.listItems, id_, title, description, type_, icon, state) self.connect(widget, QtCore.SIGNAL("stateChanged(int)"), self.slotItemState) self.connect(widget, QtCore.SIGNAL("editClicked()"), self.slotItemEdit) self.connect(widget, QtCore.SIGNAL("deleteClicked()"), self.slotItemDelete) return widget def addItem(self, id_, name="", description="", icon="security-medium", state=False): """ Adds an item to list. """ type_ = "" # Build widget and widget item widget = self.makeItemWidget(id_, name, description, type_, KIcon(icon), state) widgetItem = ItemListWidgetItem(self.listItems, widget) # Rules are static widget.hideDelete() # Add to list self.listItems.setItemWidget(widgetItem, widget) # Check if a filter matches item if not self.itemMatchesFilter(widgetItem): self.listItems.setItemHidden(widgetItem, True) def buildItemList(self): """ Builds item list. """ # Clear list self.clearItemList() def handler(package, exception, args): if exception: return modules = args[0] for name in modules: title, description, icon = self.iface.moduleInfo(name) state = QtCore.Qt.Unchecked if self.iface.getModuleState(name) == "on": state = QtCore.Qt.Checked self.addItem(name, title, description, icon, state) self.iface.listModules(handler) def itemMatchesFilter(self, item): """ Checks if item matches selected filter. """ filter = str( self.comboFilter.itemData( self.comboFilter.currentIndex()).toString()) if filter == "incoming" and item.getType() != "incoming": return False elif filter == "outcoming" and item.getType() != "outcoming": return False return True def buildFilter(self): """ Builds item filter. """ self.comboFilter.clear() self.comboFilter.addItem(i18n("All"), QtCore.QVariant("all")) def buildMenu(self): """ Builds "Add New" button menu. """ # Create menu for "new" button menu = QtGui.QMenu(self.pushNew) self.pushNew.setMenu(menu) # New item action = QtGui.QAction(i18n("Action"), self) action.setData(QtCore.QVariant("action")) menu.addAction(action) def showEditBox(self, id_, type_): """ Shows edit box. """ if self.animationLast == ANIM_HIDE: self.animationLast = ANIM_SHOW # Set range self.animator.setFrameRange(ANIM_TARGET, self.height() - ANIM_TARGET) # Go go go! self.animator.start() def hideEditBox(self): """ Hides edit box. """ if self.animationLast == ANIM_SHOW: self.animationLast = ANIM_HIDE # Set range self.animator.setFrameRange(self.frameEdit.height(), ANIM_TARGET) # Go go go! self.animator.start() def slotFilterChanged(self, index): """ Filter is changed, refresh item list. """ for i in range(self.listItems.count()): widgetItem = self.listItems.item(i) if self.itemMatchesFilter(widgetItem): self.listItems.setItemHidden(widgetItem, False) else: self.listItems.setItemHidden(widgetItem, True) def slotItemState(self, state): """ Item state changed. """ widget = self.sender() try: self.iface.setModuleState(widget.getId(), state == QtCore.Qt.Checked) except Exception, e: if "Comar.PolicyKit" in e._dbus_error_name: ctx.createMessage(self, "Error", "Access denied.") else: ctx.createMessage(self, "Error", unicode(e)) self.buildItemList()