import gtk import gtk.glade import os import sys import gettext import locale locale.textdomain("lliurex-moving-editor") gettext.textdomain("lliurex-moving-editor") _=gettext.gettext from net.Lliurex.Classroom.MovingProfiles import MovingProfiles class ProfileEditor: icon_table={"mozilla":"firefox-3.5","pidgin":"pidgin","adobe":"acroread","face":"system-users","thunderbird":"thunderbird","tux":"tuxpaint","exe":"exelearning","lliurex-art":"lliurex-tux-paint"} def __init__(self): uitree=gtk.glade.XML("/usr/share/lliurex-moving-editor/rsrc/ui.glade") self.winmain=uitree.get_widget("winmain") self.winmain.connect("destroy",self.OnDestroy) self.treefiles=uitree.get_widget("treeFiles") self.treeregex=uitree.get_widget("treeRegex") self.btnquit=uitree.get_widget("btnQuit") self.btnquit.connect("clicked",self.OnBtnQuitClick) self.btnsave=uitree.get_widget("btnSave") self.btnsave.connect("clicked",self.OnBtnSaveClick) self.btnadd=uitree.get_widget("btnAdd") self.btndel=uitree.get_widget("btnDel") self.btnaddrule=uitree.get_widget("btnAddRule") self.btnadd.connect("clicked",self.OnBtnAddClick) self.btndel.connect("clicked",self.OnBtnDelClick) self.btnaddrule.connect("clicked",self.OnBtnAddRuleClick) model_files=gtk.ListStore(str,str,str) self.treefiles.set_model(model_files) model_regex=gtk.ListStore(str,str,str,str) self.treeregex.set_model(model_regex) col=gtk.TreeViewColumn(_("Save")) col.set_resizable(True) self.treefiles.append_column(col) cell=gtk.CellRendererPixbuf() col.pack_start(cell, True) col.add_attribute(cell, 'icon-name', 0) col.set_sort_column_id(0) col=gtk.TreeViewColumn() col.set_resizable(True) cell=gtk.CellRendererPixbuf() col.pack_start(cell, True) col.add_attribute(cell, 'icon-name', 1) cell.set_property("stock-size",gtk.ICON_SIZE_DND) self.treefiles.append_column(col) col=gtk.TreeViewColumn(_("File")) col.set_resizable(True) cell = gtk.CellRendererText() col.pack_start(cell, True) col.add_attribute(cell, 'text', 2) self.treefiles.append_column(col) #regex tree view type_model=gtk.ListStore(str) type_model.append(["include"]) type_model.append(["exclude"]) col=gtk.TreeViewColumn() col.set_resizable(True) cell=gtk.CellRendererPixbuf() col.pack_start(cell, True) col.add_attribute(cell, 'icon-name', 3) cell.set_property("stock-size",gtk.ICON_SIZE_DND) self.treeregex.append_column(col) col=gtk.TreeViewColumn(_("Type")) col.set_resizable(True) col.set_sort_column_id(0) cell = gtk.CellRendererCombo() cell.set_property("model",type_model) cell.set_property("editable",True) cell.set_property("text-column",0) cell.connect("edited",self.OnCellTypeEdit) col.pack_start(cell, True) col.add_attribute(cell, 'text', 0) self.treeregex.append_column(col) col=gtk.TreeViewColumn(_("Name")) col.set_resizable(True) cell = gtk.CellRendererText() cell.set_property("editable",True) cell.connect("edited",self.OnCellNameEdit) col.pack_start(cell, True) col.add_attribute(cell, 'text', 1) self.treeregex.append_column(col) col=gtk.TreeViewColumn(_("Rule")) col.set_resizable(True) cell = gtk.CellRendererText() cell.set_property("editable",True) cell.connect("edited",self.OnCellRegexEdit) col.pack_start(cell, True) col.add_attribute(cell, 'text', 2) self.treeregex.append_column(col) self.mp=MovingProfiles() self.LoadRegex() self.ComputeFiles() self.winmain.show_all() def OnDestroy(self,data): gtk.main_quit() def OnCellTypeEdit(self,cell,path,new_text): model=self.treeregex.get_model() iter=model.get_iter(path) model.set_value(iter,0,new_text) self.RecomputeRegex() def OnCellNameEdit(self,cell,path,new_text): model=self.treeregex.get_model() iter=model.get_iter(path) model.set_value(iter,1,new_text) model.set_value(iter,3,self.GetIcon(new_text)) self.RecomputeRegex() def OnCellRegexEdit(self,cell,path,new_text): model=self.treeregex.get_model() iter=model.get_iter(path) model.set_value(iter,2,new_text) self.RecomputeRegex() def OnBtnAddClick(self,data): model=self.treeregex.get_model() model.append(["include","noname","noname",self.GetIcon("")]) self.RecomputeRegex() def OnBtnDelClick(self,data): model,iter=self.treeregex.get_selection().get_selected() if not iter==None: model.remove(iter) self.RecomputeRegex() def OnBtnAddRuleClick(self,data): model,iter=self.treefiles.get_selection().get_selected() if not iter==None: filename=model.get_value(iter,2) model=self.treeregex.get_model() name=filename.replace(".","_") name=name.replace(" ","_") model.append(["include",name,filename,self.GetIcon("")]) self.RecomputeRegex() def OnBtnSaveClick(self,data): self.mp.Save() def RecomputeRegex(self): model=self.treeregex.get_model() iter=model.get_iter_first() #clear current set up self.mp.cfg.remove_section("include") self.mp.cfg.remove_section("exclude") self.mp.cfg.add_section("include") self.mp.cfg.add_section("exclude") while not iter==None: type=model.get_value(iter,0) name= model.get_value(iter,1) regex=model.get_value(iter,2) self.mp.cfg.set(type,name,regex) iter=model.iter_next(iter) self.ComputeFiles() def GetIcon(self,name): if name in ProfileEditor.icon_table: return ProfileEditor.icon_table[name] else: return "application-x-executable" def LoadRegex(self): model_regex=self.treeregex.get_model() for item in self.mp.cfg.items("include"): name,value=item icon=self.GetIcon(name) model_regex.append(["include",name,value,icon]) for item in self.mp.cfg.items("exclude"): name,value=item icon=self.GetIcon(name) model_regex.append(["exclude",name,value,icon]) def ComputeFiles(self): model_files=self.treefiles.get_model() model_files.clear() for file in os.listdir(os.path.expanduser("~")): if os.path.isdir(os.path.expanduser("~")+os.path.sep+file): mime_icon="folder" else: mime_icon="application-x-generic" if not self.mp.isInclude(file)==None: if not self.mp.isExclude(file)==None: model_files.append(["gtk-no",mime_icon,file]) else: model_files.append(["gtk-apply",mime_icon,file]) else: if not self.mp.isExclude(file)==None: model_files.append(["gtk-no",mime_icon,file]) else: model_files.append(["",mime_icon,file]) def OnBtnQuitClick(self,data): gtk.main_quit()