#!/usr/bin/env python import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject import os import xmlrpclib import sys import pwd import time import signal signal.signal(signal.SIGINT, signal.SIG_DFL) import gettext _=gettext.gettext gettext.textdomain('printa') GROUPS=["adm","admins","teachers"] class PrintaManager: def __init__(self,url=None): if url==None: self.client_url="https://localhost:9779" self.client=xmlrpclib.ServerProxy(self.client_url) try: url=self.client.get_variable("","VariablesManager","PRINTACLIENTCONFIG")["server"] except Exception as e: print e url="localhost" self.server_url="https://%s:9779"%url self.server=xmlrpclib.ServerProxy(self.server_url) self.error=None try: self.user_list=self.server.get_user_list("","PrintaServer") self.info=None except Exception as e: self.error=str(e) self.start_gui() Gtk.main() #def init def get_uid(self,name): try: for user in pwd.getpwall(): if name==user.pw_name: return user.pw_uid except Exception as e: return None return None #def get_uid def start_gui(self): builder=Gtk.Builder() builder.set_translation_domain("printa") builder.add_from_file("/usr/share/printa/rsrc/printa-manager.glade") self.window=builder.get_object("window1") #self.window.show_all() self.window.connect("destroy",self.close_app) self.close_button=builder.get_object("close_button") self.close_button.connect("clicked",self.close_app) self.edit_quota_button=builder.get_object("edit_quota_button") self.edit_quota_button.connect("clicked",self.edit_clicked) self.search_box=builder.get_object("search_box") self.vseparator=builder.get_object("vseparator") self.history_treeview=builder.get_object("history_treeview") self.history_liststore=Gtk.ListStore(str,str,str,str,str,str) self.history_treeview.set_model(self.history_liststore) column=Gtk.TreeViewColumn("Job ID") renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",0) self.history_treeview.append_column(column) column=Gtk.TreeViewColumn(_("Job Name")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",1) self.history_treeview.append_column(column) column=Gtk.TreeViewColumn(_("# of pages")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",2) self.history_treeview.append_column(column) column=Gtk.TreeViewColumn(_("Date")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",3) self.history_treeview.append_column(column) column=Gtk.TreeViewColumn(_("Status")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",4) self.history_treeview.append_column(column) column=Gtk.TreeViewColumn(_("Printer")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",5) self.history_treeview.append_column(column) self.users_treeview=builder.get_object("users_treeview") self.users_liststore=Gtk.ListStore(str,str) self.users_treeview.set_model(self.users_liststore) column=Gtk.TreeViewColumn(_("UID")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",0) self.users_treeview.append_column(column) column=Gtk.TreeViewColumn(_("Name")) renderer=Gtk.CellRendererText() column.pack_start(renderer,True) column.add_attribute(renderer,"text",1) self.users_treeview.append_column(column) self.user_selection=self.users_treeview.get_selection() self.user_selection.connect("changed",self.user_selected) self.search_entry=builder.get_object("search_entry") self.search_entry.connect("changed",self.search_entry_changed) self.uid_entry=builder.get_object("uid_entry") self.login_entry=builder.get_object("login_entry") self.quota_entry=builder.get_object("quota_entry") self.locked_checkbutton=builder.get_object("locked_checkbutton") self.locked_checkbutton.connect("toggled",self.locked_changed) self.freepass_checkbutton=builder.get_object("freepass_checkbutton") self.freepass_checkbutton.connect("toggled",self.freepass_changed) self.login_window=builder.get_object("login_window") self.login_window.connect("destroy",self.close_app) self.login_user_entry=builder.get_object("login_user_entry") self.login_password_entry=builder.get_object("login_password_entry") self.login_button=builder.get_object("login_button") self.login_button.connect("clicked",self.login_clicked) self.login_msg_label=builder.get_object("login_msg_label") if self.error!=None: first="" last="" self.login_msg_label.set_markup(first+self.error+last) self.quota_window=builder.get_object("quota_window") self.quota_spinbutton=builder.get_object("quota_spinbutton") self.accept_quota_button=builder.get_object("accept_quota_button") self.accept_quota_button.connect("clicked",self.accept_quota_clicked) self.quota_window.connect("delete-event",self.on_quota_delete) self.login_window.show_all() #def start_gui def on_quota_delete(self,widget,data): return True def login_clicked(self,widget): ret=self.server.validate_user(self.login_user_entry.get_text(),self.login_password_entry.get_text()) ok=False first="" last="" if ret[0]: for group in GROUPS: if group in ret[1]: ok=True break ''' if not ok: self.login_msg_label.set_markup(first+_("Your account is not allowed to use program")+last) ''' self.login_window.hide() self.user_info=(self.login_user_entry.get_text(),self.login_password_entry.get_text()) if ok: #print("let's continue") self.populate_users_list() self.window.show_all() else: self.search_box.hide() self.vseparator.hide() self.edit_quota_button.hide() self.freepass_checkbutton.set_sensitive(False) self.locked_checkbutton.set_sensitive(False) status,uid,info=self.server.get_user_info("","PrintaServer",self.login_user_entry.get_text()) if status: info["uid"]=uid self.info=info self.set_values() self.window.show() else: self.login_msg_label.set_markup(first+_("Authentication error")+last) #def login_clicked def populate_users_list(self,filter=""): self.users_liststore.clear() try: ret=self.user_list for user in ret: uid,name=user if filter!="": if filter in uid or filter in name: self.users_liststore.append((uid,name)) else: self.users_liststore.append((uid,name)) except Exception as e: print e pass #def populate_users_list def search_entry_changed(self,widget): filter=self.search_entry.get_text() self.populate_users_list(filter) def user_selected(self,selection): model,treeiter=selection.get_selected() if treeiter!=None: user=model[treeiter][0] status,uid,info=self.server.get_user_info("","PrintaServer",user) if status: info["uid"]=uid self.info=info self.set_values() else: self.info=None self.uid_entry.set_text("") self.login_entry.set_text("") self.freepass_checkbutton.set_active(False) self.locked_checkbutton.set_active(False) self.quota_entry.set_text("") self.populate_history_list([]) #def user_selected def set_values(self): self.uid_entry.set_text(self.info["uid"]) self.login_entry.set_text(self.info["user"]) self.freepass_checkbutton.set_active(self.info["free_pass"]) self.locked_checkbutton.set_active(self.info["locked"]) self.quota_entry.set_text(str(self.info["quota"])) self.populate_history_list(self.info["jobs"]) #def set_values def populate_history_list(self,jobs): self.history_liststore.clear() lst=[] for item in jobs: try: job_id=item["job"] job_name=item["job_name"] pages=str(item["pages"]) date=time.strftime("%d/%m/%y", time.localtime(int(item["time"]))) status=item["status"] printer=item["printer"] lst.append([job_id,job_name,pages,date,status,printer]) except Exception as e: #self.status_text_label.set_text(str(e)) pass for item in lst: id,name,pages,date,status,printer=item self.history_liststore.append((id,name,pages,date,status,printer)) #def populate_history_list def locked_changed(self,widget): try: if self.info!=None: if not self.server.set_locked(self.user_info,"PrintaServer",self.info["user"],widget.get_active()): widget.set_active(not widget.get_active()) except: widget.set_active(not widget.get_active()) #def locked_changed def freepass_changed(self,widget): try: if self.info!=None: if not self.server.set_freepass(self.user_info,"PrintaServer",self.info["user"],widget.get_active()): widget.set_active(not widget.get_active()) except: widget.set_active(not widget.get_active()) #def freepass_changed def edit_clicked(self,widget): if self.info!=None: self.quota_spinbutton.set_value(int(self.quota_entry.get_text())) self.quota_window.show_all() #def edit_clicked def accept_quota_clicked(self,widget): if self.server.set_user_quota(self.user_info,"PrintaServer",self.info["user"],self.quota_spinbutton.get_value_as_int()): self.quota_entry.set_text(str(self.quota_spinbutton.get_value_as_int())) self.quota_window.hide() #def accept_quota_clicked def close_app(self,widget): Gtk.main_quit() #def close_app #class PrintaGUI if __name__=="__main__": server=None if len(sys.argv)>1: server=sys.argv[1] p=PrintaManager(server)