#!/usr/bin/env python import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject,GLib import os import xmlrpclib import sys import pwd import time import gettext _=gettext.gettext gettext.textdomain('printa') import signal signal.signal(signal.SIGINT, signal.SIG_DFL) class PrintaPrinterConfig: def __init__(self): self.get_n4d_key() self.start_gui() Gtk.main() #def init def get_n4d_key(self): try: f=open("/etc/n4d/key") self.n4d_key=f.readline().strip("\n") f.close() except: print("[!] You need root privileges [!]") dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "PRINTA Printer Configuration can't be executed") dialog.format_secondary_text(_("You need administrator privileges.")) dialog.run() sys.exit(0) #def get_n4d_key def start_gui(self): builder=Gtk.Builder() builder.set_translation_domain("printa") builder.add_from_file("/usr/share/printa/rsrc/printa-printer-config-gui.glade") self.window=builder.get_object("window1") self.add_button=builder.get_object("add_button") self.add_button.connect("clicked",self.add_printer) self.close_button=builder.get_object("close_button") self.close_button.connect("clicked",self.close_app) self.treeview=builder.get_object("treeview") self.window.show_all() self.set_values() self.window.connect("destroy",self.close_app) #def start_gui def set_values(self): self.printers_store=Gtk.ListStore(str,bool) self.treeview.set_model(self.printers_store) column=Gtk.TreeViewColumn(_("Printer")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",0) self.treeview.append_column(column) column=Gtk.TreeViewColumn(_("Controlled")) renderer=Gtk.CellRendererToggle() column.pack_start(renderer,True) column.add_attribute(renderer,"active",1) self.treeview.append_column(column) renderer.connect("toggled",self.printer_toggled) self.populate_treeview() #def set_values def populate_treeview(self): self.printers_store.clear() client=xmlrpclib.ServerProxy("https://localhost:9779") controlled=client.get_controlled_printers("","Printa") non_controlled=client.get_non_controlled_printers("","Printa") for printer in controlled: self.printers_store.append([printer,True]) for printer in non_controlled: self.printers_store.append([printer,False]) #def populate_treeview def printer_toggled(self, widget, path): ret=False printer=self.printers_store[path][0] status=self.printers_store[path][1] client=xmlrpclib.ServerProxy("https://localhost:9779") try: if status: ret=client.disable_control(self.n4d_key,"Printa",printer) else: ret=client.enable_control(self.n4d_key,"Printa",printer) except Exception as e: dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "Error executing command") dialog.format_secondary_text(str(e)) dialog.run() if ret: self.printers_store[path][1] = not self.printers_store[path][1] if type(ret)!=type(True): dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "Error executing command") dialog.format_secondary_text(str(ret)) dialog.run() def add_printer(self,widget): self.window.set_sensitive(False) GLib.timeout_add(500,self.execute,"system-config-printer") self.window.show() #def add_printer def execute(self,cmd): os.system(cmd) self.populate_treeview() self.window.set_sensitive(True) return False def close_app(self,widget): Gtk.main_quit() #def close_app #class PrintaGUI if __name__=="__main__": p=PrintaPrinterConfig()