#!/usr/bin/env python # coding = utf-8 import gtk import gtk.gdk import gtk.glade import os import os.path import sys import base64 import getpass import glib import threading import sys import locale import gettext from xmlrpclib import * locale.textdomain("lliurex-homework-harvester") gettext.textdomain("lliurex-homework-harvester") _=locale.gettext WINDOW_GLADE="ClientSide.glade" class SendToTeacher: def __init__(self,file_list=None): print "[SendToTeacher] INIT" self.file_list=file_list if self.file_list==None: self.file_list=[] self.read_glade() self.sending=False self.sent_files=False self.selected_teacher=None #def init def read_glade(self): if os.path.exists("/srv/svn/leia/llx-apps/trunk/lliurex-homework-harvester/install-files/usr/share/lliurex-homework-harvester/rsrc/" + WINDOW_GLADE): path="/srv/svn/leia/llx-apps/trunk/lliurex-homework-harvester/install-files/usr/share/lliurex-homework-harvester/rsrc/" + WINDOW_GLADE else: path="/usr/share/lliurex-homework-harvester/rsrc/"+WINDOW_GLADE glade=gtk.glade.XML(path) self.window=glade.get_widget("window") self.window.connect("destroy",self.window_destroy) self.files_tv=glade.get_widget("FilesTreeview") self.teachers_tv=glade.get_widget("TeachersTreeview") self.teachers_tv.connect("button-release-event",self.teachers_tv_clicked) self.send_button=glade.get_widget("SendButton") self.send_button.connect("clicked",self.send_clicked) self.cancel_button=glade.get_widget("CancelButton") self.cancel_button.connect("clicked",self.cancel_clicked) self.add_file_button=glade.get_widget("AddFileButton") self.add_file_button.connect("clicked",self.add_file_clicked) self.remove_file_button=glade.get_widget("RemoveFileButton") self.remove_file_button.connect("clicked",self.remove_file_clicked) self.msg_label=glade.get_widget("msgLabel") self.progress_bar=glade.get_widget("sendProgressbar") self.build_files_tv() self.build_teachers_tv() self.window.show_all() #def read_glade def teachers_tv_clicked(self,widget,event): model,iter=self.teachers_tv.get_selection().get_selected() if iter!=None: self.selected_teacher=self.teachers_store.get_value(iter,1) #def teachers_tv_clicked def progress_pulse(self): self.send_button.set_sensitive(False) self.progress_bar.set_text("") if self.sending: self.progress_bar.pulse() else: if self.sent_files: self.progress_bar.set_fraction(1.0) self.progress_bar.set_text(_("Files have been sent to ") + " '" + self.teacher + "'") else: self.progress_bar.set_fraction(0) self.progress_bar.set_text("Error sending files") self.send_button.set_sensitive(True) return self.sending #def progress_pulse def build_files_tv(self): self.files_store=gtk.ListStore(str) self.files_tv.set_model(self.files_store) self.files_tv.get_selection().set_mode(gtk.SELECTION_MULTIPLE) renderer=gtk.CellRendererText() column=gtk.TreeViewColumn(_("File"),renderer) column.add_attribute(renderer,"text",0) self.files_tv.append_column(column) for item in self.file_list: self.files_store.append([item]) #def build_files_tv def build_teachers_tv(self): self.teachers_store=gtk.ListStore(str,str,str,str) self.teachers_tv.set_model(self.teachers_store) uid_renderer=gtk.CellRendererText() uid_column=gtk.TreeViewColumn(_("Where"),uid_renderer) uid_column.add_attribute(uid_renderer,"text",0) name_renderer=gtk.CellRendererText() name_column=gtk.TreeViewColumn(_("Teacher"),name_renderer) name_column.add_attribute(name_renderer,"text",1) self.teachers_tv.append_column(uid_column) self.teachers_tv.append_column(name_column) try: server = ServerProxy ("https://server:9779") self.paths=server.get_paths("","TeacherShareManager") self.teachers_store.clear() for item in self.paths: path,name,ip,port=self.paths[item] self.teachers_store.append([name,item,ip,port]) except Exception as e: print e self.msg_label.set_text("Error connecting to server") glib.timeout_add(5000,self.populate_teachers_tv) #def build_teachers_tv def window_destroy(self,args): gtk.main_quit() #def window_destroy def populate_teachers_tv(self): self.teachers_store.clear() try: server = ServerProxy ("https://server:9779") self.paths=server.get_paths("","TeacherShareManager") for item in self.paths: path,name,ip,port=self.paths[item] #print ip self.teachers_store.append([name,item,ip,port]) iter=self.teachers_store.get_iter_first() while iter: teacher=self.teachers_store.get_value(iter,1) if teacher==self.selected_teacher: self.teachers_tv.set_cursor(self.teachers_store.get_path(iter)) iter=self.teachers_store.iter_next(iter) self.msg_label.set_text("") except: self.msg_label.set_text("Error connecting to server") return True #def populate_teachers_tv def add_file_clicked(self,widget): dialog = gtk.FileChooserDialog(_("Open..."), None, gtk.FILE_CHOOSER_ACTION_OPEN,(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) dialog.set_default_response(gtk.RESPONSE_OK) response = dialog.run() if response == gtk.RESPONSE_OK: self.file_list.append(dialog.get_filename()) self.files_store.append([dialog.get_filename()]) dialog.destroy() def remove_file_clicked(self,widget): selection,row=self.files_tv.get_selection().get_selected_rows() for item in row: iter=self.files_store.get_iter(item) self.files_store.get_value(iter,0) self.files_store.remove(iter) #def remove_file_clicked def to_base64(self,path): f=open(path,'rb') list=f.readlines() f.close() buf="".join(list) data=base64.b64encode(buf) return data #def get_file def send_clicked(self,widget): model,iter=self.teachers_tv.get_selection().get_selected() if iter!=None: self.sending=True teacher=self.teachers_store.get_value(iter,1) ip=self.teachers_store.get_value(iter,2) glib.timeout_add(100,self.progress_pulse) t=threading.Thread(target=self.send_files,args=(teacher,ip,)) t.daemon=True t.start() #self.send_files(teacher,ip) else: self.msg_label.set_text(_("Please, select a destination first.")) #def send_clicked def send_files(self,teacher,ip): self.msg_label.set_text("") ret=True user=getpass.getuser() for item in self.files_store: file_name=item[0] server = ServerProxy ("https://localhost:9779") try: ret=server.send_to_teacher_net("","TeacherShare",user,teacher,file_name) if type(ret)!=type(True): print ret ret=False except Exception as e: print e self.msg_label.set_text(e.strerror) ret=False f.write(e.strerror+"/n") break self.sending=False self.sent_files=ret self.teacher=teacher return self.sent_files #def send_files def cancel_clicked(self,widget): gtk.main_quit() #def cancel_clicked #class SendToTeacher if __name__=="__main__": gtk.gdk.threads_init() gtk.gdk.threads_enter() st=SendToTeacher(sys.argv[1:]) gtk.main() gtk.gdk.threads_leave()