#!/usr/bin/env python
from gi.repository import Gtk, Gdk, GObject
import os
import xmlrpclib
import sys
import pwd
import time
import cups
import gettext
_=gettext.gettext
gettext.textdomain('printa')
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
class PrintaGUI:
def __init__(self,job):
self.client_url="https://localhost:9779"
self.client=xmlrpclib.ServerProxy(self.client_url)
self.gui_user=os.environ["USER"]
self.client.create_ticket("","NTicketsManager",self.gui_user)
f=open("/run/n4d/tickets/"+self.gui_user)
password=f.readline()
f.close()
self.gui_user_info=(self.gui_user,password)
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)
try:
'''
f=open("/tmp/printa","w")
f.write(str(os.environ))
f.close()
'''
self.job=job
self.printed=False
self.info=self.client.get_job("","Printa",self.job)
user_info=pwd.getpwnam(os.environ["USER"])
self.user_uid=str(user_info.pw_uid)
self.user_name=os.environ["USER"]
#self.start_gui()
self.start_login_gui()
Gtk.main()
except Exception as e:
f=open("/tmp/printa","a")
f.write("\n")
f.write(str(e))
f.close()
raise e
#def init
def start_login_gui(self):
builder=Gtk.Builder()
builder.set_translation_domain("printa")
builder.add_from_file("/usr/share/printa/rsrc/printa-gui.glade")
self.login_window=builder.get_object("loginwindow")
self.login_window.connect("destroy",self.close_app)
self.user_login_entry=builder.get_object("user_login_entry")
self.user_password_entry=builder.get_object("user_password_entry")
self.user_login_entry.set_text(self.user_name)
self.accept_login_button=builder.get_object("accept_login_button")
self.cancel_login_button=builder.get_object("cancel_login_button")
self.accept_login_button.connect("clicked",self.accept_login_clicked)
self.cancel_login_button.connect("clicked",self.cancel_login_clicked)
self.login_msg=builder.get_object("msg_label")
self.login_window.show()
#def start_login_gui
def accept_login_clicked(self,widget):
try:
ret=self.server.validate_user(self.user_login_entry.get_text(),self.user_password_entry.get_text())
if ret[0]:
self.login_window.hide()
self.user_name=self.user_login_entry.get_text()
self.user_password=self.user_password_entry.get_text()
self.user_info=(self.user_name,self.user_password)
self.start_gui()
if not ret[0]:
txt=""
txt+="Authentication error"
txt+=""
self.login_msg.set_markup(txt)
except Exception as e:
txt=""
txt+="Error conecting to %s"%self.server_url + " :\n" + str(e)
txt+=""
self.login_msg.set_markup(txt)
#def accept_login_clicked
def cancel_job(self):
try:
cups_conn=cups.Connection()
cups_conn.cancelJob(int(self.job))
except:
pass
#def cancel_job
def cancel_login_clicked(self,widget):
self.cancel_job()
self.close_app(None)
#def cancel_login_clicked
def start_gui(self):
builder=Gtk.Builder()
builder.set_translation_domain("printa")
builder.add_from_file("/usr/share/printa/rsrc/printa-gui.glade")
self.window=builder.get_object("window1")
self.user_entry=builder.get_object("user_entry")
self.user_quota_entry=builder.get_object("quota_entry")
self.change_user_button=builder.get_object("change_user_button")
self.cancel_button=builder.get_object("cancel_button")
self.print_button=builder.get_object("print_button")
self.main_text_label=builder.get_object("main_text_label")
self.status_text_label=builder.get_object("status_text_label")
self.treeview=builder.get_object("history_treeview")
self.password_box=builder.get_object("password_box")
self.password_entry=builder.get_object("password_entry")
self.passwd_label=builder.get_object("passwd_label")
self.window.show_all()
self.set_values()
self.window.connect("destroy",self.close_app)
self.cancel_button.connect("clicked",self.cancel_clicked)
self.print_button.connect("clicked",self.print_clicked)
self.change_user_button.connect("clicked",self.change_user_clicked)
#Gtk.main()
#def start_gui
def change_user_clicked(self,widget):
print("CHANGE USER CLICKED...")
self.window.hide()
self.start_login_gui()
#def change_user_clicked
def print_clicked(self,widget):
print("PRINTING...")
try:
if self.use_password:
ret=self.server.validate_password("","PrintaServer",self.user_name,self.password_entry.get_text())
if not ret:
txt=""
txt+="Wrong user password"
txt+=""
self.status_text_label.set_markup(txt)
else:
self.status_text_label.set_markup("")
self.client.set_response(self.gui_user_info,"Printa","",self.job,1)
self.print_button.set_sensitive(False)
self.cancel_button.set_sensitive(False)
self.printed=True
self.update_user_info()
self.close_app(None)
else:
self.client.set_response(self.gui_user_info,"Printa","",self.job,1)
self.print_button.set_sensitive(False)
self.cancel_button.set_sensitive(False)
self.printed=True
self.update_user_info()
self.close_app(None)
except Exception as e:
self.status_text_label.set_markup(str(e))
self.print_button.set_sensitive(False)
self.cancel_button.set_sensitive(False)
self.client.set_response(self.gui_user_info,"Printa","",self.job,0)
self.printed=False
self.update_user_info()
#def print_clicked
def cancel_clicked(self,widget):
try:
self.client.set_response(self.gui_user_info,"Printa","",self.job,0)
self.update_user_info()
except Exception as e:
print e
print 4
self.close_app(None)
#def cancel_clicked
def update_user_info(self):
if self.printed:
self.info["status"]="completed"
if not self.server.has_freepass("","PrintaServer",self.user_name):
self.server.substract_user_quota(self.user_info,"PrintaServer",self.user_name,self.info["pages"])
else:
self.info["status"]="cancelled"
self.server.add_job_history(self.user_info,"PrintaServer",self.user_name,self.info)
def get_error_text(self,status):
if status==-1:
return _("Not a valid user.")
if status==-2:
return _("This user is not allowed to print.")
if status==-3:
return _("User quota is not enough to print this job.")
return _("Unknown error.")
#def get_error_text
def set_values(self):
self.password_box.hide()
self.user_entry.set_text(self.user_name)
self.status_text_label.set_text("")
self.history_liststore=Gtk.ListStore(str,str,str,str,str,str)
self.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.treeview.append_column(column)
column=Gtk.TreeViewColumn(_("Job Name"))
renderer=Gtk.CellRendererText()
column.pack_start(renderer,True)
column.add_attribute(renderer,"text",1)
self.treeview.append_column(column)
column=Gtk.TreeViewColumn(_("# of pages"))
renderer=Gtk.CellRendererText()
column.pack_start(renderer,True)
column.add_attribute(renderer,"text",2)
self.treeview.append_column(column)
column=Gtk.TreeViewColumn(_("Date"))
renderer=Gtk.CellRendererText()
column.pack_start(renderer,True)
column.add_attribute(renderer,"text",3)
self.treeview.append_column(column)
column=Gtk.TreeViewColumn(_("Status"))
renderer=Gtk.CellRendererText()
column.pack_start(renderer,True)
column.add_attribute(renderer,"text",4)
self.treeview.append_column(column)
column=Gtk.TreeViewColumn(_("Printer"))
renderer=Gtk.CellRendererText()
column.pack_start(renderer,True)
column.add_attribute(renderer,"text",5)
self.treeview.append_column(column)
self.populate_treeview()
txt=""
txt+=""+_("Job #").decode("utf-8")+": " + self.info["job"] + "\n"
txt+=""+_("Job name").decode("utf-8")+": " + self.info["job_name"] + "\n"
txt+=""+_("Pages").decode("utf-8")+": " + str(self.info["pages"]) + "\n"
txt+=""+_("Printer").decode("utf-8")+": " + self.info["printer"] + "\n"
tmp_info=self.info
tmp_info["user"]=self.user_name
status,info=self.server.check_if_printable("","PrintaServer",tmp_info)
self.user_quota_entry.set_text(str(self.server.get_user_quota("","PrintaServer",self.user_name)))
self.info["status"]=info
if self.info["status"]==0:
txt+="\n"+_("Continue?").decode("utf-8")+""
ret=self.server.is_password_needed("","PrintaServer",self.user_name)
#txt+="is_password_needed %s"%str(ret)
self.use_password=False
if ret:
#txt+="\ne1e1e1e1e1"
self.use_password=True
self.password_box.show()
else:
self.print_button.set_sensitive(False)
txt+="\n\t\t"
#txt+="You are not allowed to print due to: "
txt+=self.get_error_text(self.info["status"]).decode("utf-8")
txt+=""
#txt+=str(self.info)
txt+="\n"
self.main_text_label.set_markup(txt)
#def set_values
def populate_treeview(self):
self.history_liststore.clear()
ret=self.server.get_user_history("","PrintaServer",self.user_name)
#self.status_text_label.set_text(str(ret))
lst=[]
for item in ret:
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_treeview
def close_app(self,widget):
Gtk.main_quit()
#def close_app
#class PrintaGUI
if __name__=="__main__":
p=PrintaGUI(sys.argv[1])