# -*- coding: utf-8 -*- # h-client, a client for an h-source server (such as http://www.h-node.com) # Copyright (C) 2011 Antonio Gallo # # # h-client is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # h-client is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with h-client. If not, see . import pygtk pygtk.require('2.0') import gtk from hlibrary import * class hclient: #the device that has to be displaced in the right window currentDevice = None currentDeviceCode = None #get the active text from a combo #combo: the gtk combo instance #default: default value if the active index < 0 def getActive(self, combo, default = 'not-specified'): model = combo.get_model() active = combo.get_active() if active < 0: return default else: return model[active][0] #update the current device object by taking the values from the entries def applyChanges(self,widget): self.currentDevice.setModel(self.modelNameEntry.get_text()) self.currentDevice.setYear(self.getActive(self.commYearCombo)) self.currentDevice.setInterface(self.getActive(self.interfaceCombo)) self.currentDevice.setDistributions([]) self.currentDevice.addDistributions(self.distributionEntry.get_text()) self.currentDevice.setKernel(self.kernelEntry.get_text()) self.currentDevice.setHowItWorks(self.getActive(self.howItWorksCombo)) self.currentDevice.setDriver(self.driverEntry.get_text()) descriptionBuffer = self.descriptionText.get_buffer() startiter, enditer = descriptionBuffer.get_bounds() self.currentDevice.setDescription(descriptionBuffer.get_text(startiter, enditer)) #reset the modifications def resetChanges(self,widget): self.setEntries() #set the device that has to be displaced in the right window #and fill the entries def setCurrentDevice(self, selection): model, path = selection.get_selected() if path: #get the code: code = model.get_value(path, 1) if code in self.client.devices: #set the current device self.currentDevice = self.client.devices[code][0] self.currentDeviceCode = code #get the device #device = self.client.devices[code][0] self.setEntries() #make sensitive the apply button self.applyButton.set_sensitive(True) self.resetButton.set_sensitive(True) self.submitButton.set_sensitive(True) #self.updateStatus() else: #make non sensitive the apply button self.applyButton.set_sensitive(False) self.resetButton.set_sensitive(False) self.submitButton.set_sensitive(False) self.currentDevice = None self.currentDeviceCode = None #self.updateStatus() #set the pyGTK device entries def setEntries(self): if self.currentDevice != None: #set the model entry self.setModelEntry() #set the vendorid:productid entry self.setVendorIdProductIDCode() #set the commercialization year entry self.setCommYearEntry() #set the interface entry self.setInterfaceEntry() #set the distribution entry self.setDistributionEntry() #set the kernel entry self.setKernelEntry() #set the howItWorks entry self.setHowItWorksEntry() #set the driver entry self.setDriverEntry() #set the description entry self.setDescriptionEntry() #set the model name entry def setModelEntry(self): self.modelNameEntry.set_text(self.currentDevice.getModel()) #set the vendorid:productid entry def setVendorIdProductIDCode(self): self.vendorIdProductIdEntry.set_text(self.currentDevice.getVendorId() + ':' + self.currentDevice.getProductId()) #set the year of commercialization def setCommYearEntry(self): self.commYearCombo.get_model().clear() for year in self.currentDevice.getYears(): self.commYearCombo.append_text(year) if self.currentDevice.getYear() in self.currentDevice.getYears(): index = self.currentDevice.getYears().index(self.currentDevice.getYear()) else: index = 0 self.commYearCombo.set_active(index) #set the interface def setInterfaceEntry(self): self.interfaceCombo.get_model().clear() for interface in self.currentDevice.getInterfaces(): self.interfaceCombo.append_text(interface) if self.currentDevice.getInterface() in self.currentDevice.getInterfaces(): index = self.currentDevice.getInterfaces().index(self.currentDevice.getInterface()) else: index = 0 self.interfaceCombo.set_active(index) #set the distribution entry def setDistributionEntry(self): self.distributionEntry.set_text(self.currentDevice.createDistroEntry()) #set the kernel libre entry def setKernelEntry(self): self.kernelEntry.set_text(self.currentDevice.getKernel()) #set the howItWorks entry def setHowItWorksEntry(self): self.howItWorksCombo.get_model().clear() for option in self.currentDevice.getHowItWorksOptions(): self.howItWorksCombo.append_text(option) if self.currentDevice.getHowItWorks() in self.currentDevice.getHowItWorksOptions(): index = self.currentDevice.getHowItWorksOptions().index(self.currentDevice.getHowItWorks()) else: index = 0 self.howItWorksCombo.set_active(index) #set the driver entry def setDriverEntry(self): self.driverEntry.set_text(self.currentDevice.getDriver()) #set the description entry def setDescriptionEntry(self): textbuffer = gtk.TextBuffer(table=None) textbuffer.set_text(self.currentDevice.getDescription()) self.descriptionText.set_buffer(textbuffer) #set the node def setNode(self,widget): self.client.logout() self.client.setNode(self.serverEntry.get_text()) self.updateStatus() #close the preferences window def closePref(self,widget): self.prefWindow.destroy() #login to the server def login(self,widget): self.client.login(self.usernameEntry.get_text(),self.passwordEntry.get_text()) self.updateStatus() if self._submitFlag: if self.client.isLogged() != -2: if self.client.isLogged(): self.applyChanges(None) self.client.submit(self.currentDeviceCode) self.printErrors() self._submitFlag = False #self.printErrors() self.loginWindow.destroy() #submit data to the server def submit(self,widget): self.applyChanges(None) self.licenseNoticeWindow.destroy() if self.client.isLogged() != -2: if self.client.isLogged(): self.client.submit(self.currentDeviceCode) else: self._submitFlag = True self.openLoginWindow(None) self.printErrors() #logout to the server def logout(self,widget): self.client.logout() self.updateStatus() #self.printErrors() #close the login window def closeLoginWindow(self,widget): self._submitFlag = False self.updateStatus() self.loginWindow.destroy() #close the license notice window def closeLicenseNoticeWindow(self,widget): self.licenseNoticeWindow.destroy() #close the info window def closeInfoWindowWindow(self,widget): self.infoWindow.destroy() def openInfoWindow(self,widget): self.infoWindow = gtk.Window(gtk.WINDOW_TOPLEVEL) self.infoWindow.set_title("info") self.infoWindow.set_position(gtk.WIN_POS_CENTER) self.infoWindow.set_icon_from_file("img/icon.png") #self.infoWindow.set_size_request(300, -1) vbox = gtk.VBox(False, 0) vbox.set_border_width(10) self.infoWindow.add(vbox) label = gtk.Label("h-node client") label.set_use_markup(True) vbox.pack_start(label, False, True, 5) label = gtk.Label("Copyright (C) 2011 Antonio Gallo") label.set_use_markup(True) vbox.pack_start(label, False, True, 5) label = gtk.Label("GPLv3") label.set_use_markup(True) vbox.pack_start(label, False, True, 5) closeButton = gtk.Button(stock=gtk.STOCK_CLOSE) closeButton.connect("clicked", self.closeInfoWindowWindow) vbox.pack_start(closeButton, False, True, 5) self.infoWindow.show_all() #start the window containing the license notice def openLicenseNoticeWindow(self,widget): #window for preferences self.licenseNoticeWindow = gtk.Window(gtk.WINDOW_TOPLEVEL) self.licenseNoticeWindow.set_title("license notice") self.licenseNoticeWindow.set_position(gtk.WIN_POS_CENTER) self.licenseNoticeWindow.set_icon_from_file("img/icon.png") self.licenseNoticeWindow.set_size_request(300, -1) vbox = gtk.VBox(False, 0) vbox.set_border_width(10) self.licenseNoticeWindow.add(vbox) result = self.client.getLicenseNotice(); #print result ##if result #description input sw = gtk.ScrolledWindow() #sw.set_shadow_type(gtk.SHADOW_ETCHED_IN) sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER) noticeText = gtk.TextView() #noticeText.set_decorated(False) noticeText.set_editable(False) #noticeText.modify_base(gtk.STATE_NORMAL, gtk.gdk.Color(50,100,150) ) noticeText.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("#a3a3a3") ) noticeText.set_wrap_mode(gtk.WRAP_CHAR) textbuffer = gtk.TextBuffer(table=None) textbuffer.set_text(result) noticeText.set_buffer(textbuffer) sw.add(noticeText) #sw.show() #sw.show_all() vbox.pack_start(sw, False, True, 5) hbox = gtk.HBox(False, 0) hbox.set_border_width(10) applyButton = gtk.Button(stock=gtk.STOCK_APPLY) closeButton = gtk.Button(stock=gtk.STOCK_CLOSE) applyButton.connect("clicked", self.submit) closeButton.connect("clicked", self.closeLicenseNoticeWindow) hbox.pack_end(applyButton, False, True, 0) hbox.pack_end(closeButton, False, True, 3) vbox.pack_start(hbox, False, True, 0) self.licenseNoticeWindow.show_all() #start the login window def openLoginWindow(self,widget): #window for preferences self.loginWindow = gtk.Window(gtk.WINDOW_TOPLEVEL) self.loginWindow.set_title("login") self.loginWindow.set_position(gtk.WIN_POS_CENTER) self.loginWindow.set_icon_from_file("img/icon.png") self.loginWindow.set_size_request(300, -1) #self.prefWindow.set_size_request(300, -1) vbox = gtk.VBox(False, 0) self.loginWindow.add(vbox) table = gtk.Table(2, 2, True) table.set_border_width(10) label = gtk.Label("login to the server") label.set_use_markup(True) vbox.pack_start(label, False, True, 0) vbox.pack_start(table, False, True, 0) ### username usernameLabel = gtk.Label("username:") usernameLabel.set_alignment(0.95,0.5) #add the label table.attach(usernameLabel, 0, 1, 0, 1) self.usernameEntry = gtk.Entry() #add the input to the table table.attach(self.usernameEntry, 1, 2, 0, 1) ### password passwordLabel = gtk.Label("password:") passwordLabel.set_alignment(0.95,0.5) #add the label table.attach(passwordLabel, 0, 1, 1, 2) self.passwordEntry = gtk.Entry() self.passwordEntry.set_visibility(False) #add the input to the table table.attach(self.passwordEntry, 1, 2, 1, 2) hbox = gtk.HBox(False, 0) hbox.set_border_width(10) applyButton = gtk.Button(stock=gtk.STOCK_APPLY) closeButton = gtk.Button(stock=gtk.STOCK_CLOSE) applyButton.connect("clicked", self.login) closeButton.connect("clicked", self.closeLoginWindow) hbox.pack_end(applyButton, False, True, 0) hbox.pack_end(closeButton, False, True, 3) vbox.pack_start(hbox, False, True, 0) self.loginWindow.show_all() #start the preferences window def openPrefWindow(self,widget): #window for preferences self.prefWindow = gtk.Window(gtk.WINDOW_TOPLEVEL) self.prefWindow.set_title("preferences") self.prefWindow.set_position(gtk.WIN_POS_CENTER) self.prefWindow.set_icon_from_file("img/icon.png") self.prefWindow.set_size_request(300, -1) vbox = gtk.VBox(False, 0) self.prefWindow.add(vbox) table = gtk.Table(1, 2, True) table.set_border_width(10) vbox.pack_start(table, False, True, 0) ### server serverLabel = gtk.Label("Server URL:") #add the label table.attach(serverLabel, 0, 1, 0, 1) self.serverEntry = gtk.Entry() self.serverEntry.set_text(self.client.getNode()) #add the input to the table table.attach(self.serverEntry, 1, 2, 0, 1) hbox = gtk.HBox(False, 0) hbox.set_border_width(10) applyButton = gtk.Button(stock=gtk.STOCK_APPLY) closeButton = gtk.Button(stock=gtk.STOCK_CLOSE) applyButton.connect("clicked", self.setNode) closeButton.connect("clicked", self.closePref) hbox.pack_end(applyButton, False, True, 0) hbox.pack_end(closeButton, False, True, 3) vbox.pack_start(hbox, False, True, 0) #applyButton.connect("clicked", self.applyChanges) self.prefWindow.show_all() #synchronize with the server XML database def synchronize(self,widget): self.client.sync() self.printErrors() #print self.client.errors self.setEntries() def printErrors(self): #destroy the error bar HBox if hasattr(self, "errorBarHBox"): self.errorBarHBox.destroy() if len(self.client.errors) > 0: self.client.errors = list(set(self.client.errors)) #self.errorBar.set_shadow_type(gtk.SHADOW_ETCHED_IN) self.errorBarHBox = gtk.HBox(False, 0) for error in self.client.errors: label = gtk.Label(error) self.errorBarHBox.pack_start(label, False, True, 10) self.errorBar.add_with_viewport(self.errorBarHBox) self.errorBar.show_all() self.client.errors = [] #check if the user is logged #hide or show the login/logout buttons def updateStatus(self): if self.client.isLogged() == True: self.loginButton.hide() self.logoutButton.show() info = self.client.getUserInfo() if info != False: if info != -2: self.statusLabel.set_markup("hello "+info['username']+", you are logged in") if self.currentDeviceCode != None: self.submitButton.set_sensitive(True) self.printErrors() else: self.printErrors() self.loginButton.show() self.logoutButton.hide() self.statusLabel.set_markup("you are not logged in") #def setTreeViewCell(self,column, cell_renderer, model, iter): ##img = model.get_value(iter, 3) ##imgFlag = model.get_value(iter, 5) #print model ##self.devices.clear_attributes(cell_renderer) ##if imgFlag == 'image': ###print True ##cell_renderer.set_property('pixbuf', img) #return #another callback def delete_event(self, widget, event, data=None): gtk.main_quit() return False def __init__(self): #does it have to submit after the login? self._submitFlag = False #start the client object self.client = Client('h-source') self.client.createDevices() # Create the main window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("h-client") self.window.set_icon_from_file("img/icon.png") self.window.set_size_request(550, -1) self.window.set_position(gtk.WIN_POS_CENTER) self.window.connect("delete_event", self.delete_event) #self.window.set_border_width(0) vbox = gtk.VBox(False, 0) #add the bottom box self.window.add(vbox) self.centerWindow = gtk.HBox(False, 0) self.bottomWindow = gtk.HBox(False, 0) self.errorBar = gtk.ScrolledWindow() self.errorBar.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_NEVER) self.errorBar.set_border_width(5) self.errorBar.set_size_request(-1,50) self.bottomWindow.add(self.errorBar) self.errorBarHBox = gtk.HBox(False, 0) self.errorBar.add_with_viewport(self.errorBarHBox) self.errorBar.show_all() ## build the toolbar ## toolbar = gtk.Toolbar() toolbar.set_tooltips(True) #toolbar.set_style(gtk.TOOLBAR_BOTH) pref = gtk.ToolButton(gtk.STOCK_PREFERENCES) pref.set_tooltip_text('Preferences') pref.connect("clicked", self.openPrefWindow) sync = gtk.ToolButton(gtk.STOCK_REFRESH) sync.set_tooltip_text('Synchronize with the server: this will override the entries of your devices') sync.connect("clicked", self.synchronize) info = gtk.ToolButton(gtk.STOCK_INFO) info.set_tooltip_text('Information') info.connect("clicked",self.openInfoWindow); toolbar.insert(sync, 0) toolbar.insert(pref, 1) toolbar.insert(info, 2) toolbar.show_all() vbox.pack_start(toolbar, True, True, 0) vbox.pack_start(self.centerWindow, True, True, 0) vbox.pack_start(self.bottomWindow, True, True, 0) vbox.show() ## build the left window ## #start the left vertical box self.leftWindow = gtk.VBox(False, 0) #self.leftWindow.set_border_width(5) self.centerWindow.pack_start(self.leftWindow, True, True, 0) #treeFrame.add(self.leftWindow) #self.centerWindow.pack_start(self.rframe, True, True, 5) #self.leftWindow.pack_start(gtk.Label("Your hardware:"), False, True, 5) self.tree = gtk.TreeView() self.tree.set_headers_visible(False) self.devices = gtk.TreeViewColumn("Your PCI and USB devices") device_icon = gtk.CellRendererPixbuf() self.devices.pack_start(device_icon, True) self.devices.add_attribute(device_icon, 'pixbuf', 3) #self.devices.set_cell_data_func(device_icon, self.setTreeViewCell) device_name = gtk.CellRendererText() self.devices.pack_start(device_name, True) self.devices.add_attribute(device_name, "text", 0) self.devices.add_attribute(device_name, "xpad", 4) self.devices.add_attribute(device_name, "weight", 2) self.treestore = gtk.TreeStore(str,str,int,gtk.gdk.Pixbuf,int) pci = self.treestore.append(None, ["Your PCI Devices","",800,gtk.gdk.pixbuf_new_from_file('img/title_png.png'),4]) for key,dev in self.client.devices.iteritems(): if key[0] == 'p': self.treestore.append(pci, [dev[0].getType(),key,400,gtk.gdk.pixbuf_new_from_file('img/devices/small/'+dev[0].getIcon()),4]) selection = self.tree.get_selection() selection.connect('changed', self.setCurrentDevice) self.tree.append_column(self.devices) self.tree.set_model(self.treestore) treesw = gtk.ScrolledWindow() treesw.set_size_request(110,401) treesw.set_shadow_type(gtk.SHADOW_ETCHED_IN) treesw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) treesw.add(self.tree) self.leftWindow.set_border_width(5) self.leftWindow.pack_start(treesw, False, True, 0) self.leftWindow.show_all() self.tree.expand_all() #treeFrame.add(self.leftWindow) ## build the right window ## #right top rthbox = gtk.HBox(False, 0) rthbox.set_border_width(5) #login button self.loginButton = gtk.Button("Login") self.loginButton.set_sensitive(True) self.loginButton.connect("clicked", self.openLoginWindow) rthbox.pack_start(self.loginButton, False, True, 0) #login button self.logoutButton = gtk.Button("Logout") self.logoutButton.set_sensitive(True) self.logoutButton.connect("clicked", self.logout) rthbox.pack_start(self.logoutButton, False, True, 0) #status label self.statusLabel = gtk.Label("") self.statusLabel.set_use_markup(True) rthbox.pack_end(self.statusLabel, False, True, 0) self.rightTable = gtk.Table(8, 2, True) #create the entries ### model #model name label self.modelNameLabel = gtk.Label("Model name:") self.modelNameLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.modelNameLabel, 0, 1, 0, 1) #model name input self.modelNameEntry = gtk.Entry() #add the input to the table self.rightTable.attach(self.modelNameEntry, 1, 2, 0, 1) ### vendorid:productid #vendorid:productid label self.vendorIdProductIdLabel = gtk.Label("VendorID:productID code:") self.vendorIdProductIdLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.vendorIdProductIdLabel, 0, 1, 1, 2) #vendorid:productid input self.vendorIdProductIdEntry = gtk.Entry() #set as not editable self.vendorIdProductIdEntry.set_editable(False) #add the input to the table self.rightTable.attach(self.vendorIdProductIdEntry, 1, 2, 1, 2) ###year of commercialization #year of commercialization label self.commYearLabel = gtk.Label("Year of commercialization:") self.commYearLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.commYearLabel, 0, 1, 2, 3) self.commYearCombo = gtk.combo_box_new_text() #add the combo to the table self.rightTable.attach(self.commYearCombo, 1, 2, 2, 3) ###interface #interface label self.interfaceLabel = gtk.Label("Interface:") self.interfaceLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.interfaceLabel, 0, 1, 3, 4) self.interfaceCombo = gtk.combo_box_new_text() self.interfaceCombo.append_text('not-specified') self.interfaceCombo.set_active(0) #add the combo to the table self.rightTable.attach(self.interfaceCombo, 1, 2, 3, 4) ### distribution #distribution label self.distributionLabel = gtk.Label("Distribution:") self.distributionLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.distributionLabel, 0, 1, 4, 5) #distribution input self.distributionEntry = gtk.Entry() #add the input self.rightTable.attach(self.distributionEntry, 1, 2, 4, 5) ### kernel #kernel label self.kernelLabel = gtk.Label("Kernel libre version:") self.kernelLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.kernelLabel, 0, 1, 5, 6) #kernel input self.kernelEntry = gtk.Entry() #add the input self.rightTable.attach(self.kernelEntry, 1, 2, 5, 6) ###how it works #how it works label self.howItWorksLabel = gtk.Label("Does it work?") self.howItWorksLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.howItWorksLabel, 0, 1, 6, 7) self.howItWorksCombo = gtk.combo_box_new_text() #add the combo to the table self.rightTable.attach(self.howItWorksCombo, 1, 2, 6, 7) ### driver #driver label self.driverLabel = gtk.Label("Free driver used:") self.driverLabel.set_alignment(0.95,0.5) #add the label self.rightTable.attach(self.driverLabel, 0, 1, 7, 8) #driver input self.driverEntry = gtk.Entry() #add the input self.rightTable.attach(self.driverEntry, 1, 2, 7, 8) ### description #description label self.descriptionLabel = gtk.Label("Description:") self.descriptionLabel.set_alignment(0,0.5) #description input sw = gtk.ScrolledWindow() sw.set_shadow_type(gtk.SHADOW_ETCHED_IN) sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.descriptionText = gtk.TextView() self.descriptionText.set_wrap_mode(gtk.WRAP_CHAR) sw.add(self.descriptionText) sw.show() sw.show_all() ##add the input #self.rightTable.attach(sw, 1, 2, 7, 8) self.rightTable.show_all() #apply and submit buttons hboxBelowEntries = gtk.HBox(False, 0) #apply button self.applyButton = gtk.Button(stock=gtk.STOCK_APPLY) self.applyButton.set_sensitive(False) self.applyButton.connect("clicked", self.applyChanges) self.applyButton.set_tooltip_text('apply your local modifications: no change will be applied to the server') #reset button self.resetButton = gtk.Button(stock=gtk.STOCK_REVERT_TO_SAVED) self.resetButton.set_sensitive(False) self.resetButton.connect("clicked", self.resetChanges) self.resetButton.set_tooltip_text('restore the entries') #submit button self.submitButton = gtk.Button("Submit") self.submitButton.set_sensitive(False) self.submitButton.connect("clicked", self.openLicenseNoticeWindow) self.submitButton.set_tooltip_text('submit your modifications to the server') hboxBelowEntries.pack_end(self.applyButton, False, True, 0) hboxBelowEntries.pack_end(self.resetButton, False, True, 0) hboxBelowEntries.pack_start(self.submitButton, False, True, 0) hboxBelowEntries.show_all() #start the left vertical box self.rightWindow = gtk.VBox(False, 0) self.rightWindow.pack_start(self.rightTable, False, True, 10) self.rightWindow.pack_start(self.descriptionLabel, False, True, 3) self.rightWindow.pack_start(sw, False, True, 0) self.rightWindow.pack_start(hboxBelowEntries, False, True, 10) #self.rightWindow.show_all() rhbox = gtk.HBox(False, 0) rhbox.pack_start(self.rightWindow, True, True, 5) rFrame = gtk.Frame() rFrame.add(rhbox) rFrame.set_border_width(5) rvbox = gtk.VBox(False, 0) rvbox.pack_start(rthbox, True, True, 0) rvbox.pack_start(rFrame, True, True, 0) self.centerWindow.pack_start(rvbox, True, True, 0) #self.rframe.add(self.rightWindow) #self.rframe.set_border_width(30) self.centerWindow.show_all() self.bottomWindow.show_all() self.leftWindow.show() self.window.show() self.synchronize(None) self.updateStatus() #self.logoutButton.hide() def main(): gtk.main() if __name__ == "__main__": Client = hclient() main()