class BackendManager(): def __init__(self): self.cfg = PythmConfig() def get_backends(self): return self.cfg.get_backends() def disconnect(self,backend): if backend != None and backend.is_connected(): backend.disconnect() def connect(self,backend): self.disconnect(self.cfg.get_backend()) if not backend.is_connected(): backend.reconnect() self.cfg.set_active_backend(backend) backend.populate() def start(self,backend,callback): self.disconnect(self.cfg.get_backend()) return backend.startup(callback) def stop(self,backend): if backend.is_started(): backend.shutdown()
class Page(gtk.VBox): def __init__(self): gtk.VBox.__init__(self) self.cfg = PythmConfig() self.cfg.get_backend().connect(Signals.STATE_CHANGED,self.check_disabled) self.cfg.get_backend().connect(Signals.COMMAND_STATE,self.check_cmdstate) self.add(self.content()) self.btnbox = gtk.HBox() self.pack_start(self.btnbox,False,False,0) self.set_sensitive(False) self.cmdimg = gtk.image_new_from_stock(gtk.STOCK_NETWORK, gtk.ICON_SIZE_LARGE_TOOLBAR) self.cmdimg.set_padding(3,3) self.cmdimg.set_sensitive(False) #to be loaded only when mpd activated (to be put in conf file? #self.btnbox.pack_start(self.cmdimg,False,False,0) def check_cmdstate(self,state): self.cmdimg.set_sensitive(not state) def check_disabled(self,newstate): if newstate == State.DISABLED: self.set_sensitive(False) else: self.set_sensitive(True)
class PageBackend(Page): def __init__(self): self.model = gtk.ListStore(object,str,str,str) Page.__init__(self) self.cfg = PythmConfig() self.btn_connect = ImageButton(gtk.STOCK_CONNECT) self.btnbox.add(self.btn_connect) self.btn_connect.connect("clicked",self.btn_connect_clicked) self.btn_start = ImageButton(gtk.STOCK_EXECUTE) self.btnbox.add(self.btn_start) self.btn_start.connect("clicked",self.btn_start_clicked) self.btn_stop = ImageButton(gtk.STOCK_STOP) self.btnbox.add(self.btn_stop) self.btn_stop.connect("clicked",self.btn_stop_clicked) self.btn_refresh = ImageButton(gtk.STOCK_REFRESH) self.btnbox.add(self.btn_refresh) self.btn_refresh.connect("clicked",self.btn_refresh_clicked) self.mgr = BackendManager() if self.cfg.get_backend() != None: self.start_backend(self.cfg.get_backend()) self.refresh() self.row_selected(None) self.set_sensitive(True) def start_backend(self,backend): if self.mgr.start(backend,self.gtkcallback): self.mgr.connect(backend) def btn_start_clicked(self,btn): self.start_backend(self.get_selected()) self.refresh() def btn_stop_clicked(self,btn): self.mgr.stop(self.get_selected()) self.refresh() def btn_connect_clicked(self,btn): self.mgr.connect(self.get_selected()) self.refresh() def btn_refresh_clicked(self,btn): self.refresh() def refresh(self): self.model.clear() for backend in self.mgr.get_backends(): status = "inactive" if backend.is_started(): status = "active" conn = "connected" if not backend.is_connected(): conn ="disconnected" self.model.append([backend,backend.name,status,conn]) def row_selected(self,path): backend = self.get_selected() if backend != None: self.btn_start.set_sensitive(not backend.is_started()) self.btn_stop.set_sensitive(backend.is_started()) self.btn_connect.set_sensitive(backend.is_started() and not backend.is_connected()) else: self.btn_start.set_sensitive(False) self.btn_stop.set_sensitive(False) self.btn_connect.set_sensitive(False) def check_disabled(self,state): """overwritten""" pass def get_selected(self): iter = self.tv.get_selection().get_selected()[1] if iter is not None: return self.model.get_value(iter,0) return None def content(self): vbox = gtk.VBox() self.tv = gtk.TreeView(self.model) self.tv.get_selection().connect("changed", self.row_selected) colname = gtk.TreeViewColumn("Backend",gtk.CellRendererText(),text=1) colstatus = gtk.TreeViewColumn("State",gtk.CellRendererText(),text=2) colconnected = gtk.TreeViewColumn("Connected",gtk.CellRendererText(),text=3) self.tv.append_column(colname) self.tv.append_column(colstatus) self.tv.append_column(colconnected) sc = gtk.ScrolledWindow() sc.set_property("vscrollbar-policy",gtk.POLICY_AUTOMATIC) sc.set_property("hscrollbar-policy",gtk.POLICY_AUTOMATIC) sc.add(self.tv) vbox.pack_start(sc,False,False,0) return vbox def gtkcallback(self,func,*args): """ callback method for signals of backend (out of gtk-thread) """ gobject.idle_add(func,*args)