#!/usr/bin/python # Author: Gabriele Bozzola (sbozzolo) # Email: sbozzolator@gmail.com # Date: 11.05.2016 # Last Edit: 30.05.2016 (sbozzolo) #~ This module is used to draw the interface import npyscreen as nps #~ To rename xinitrc and to get the HOME from os import rename as rename from os import getenv as getenv #~ This dictionary is intended to contain every string of Userconf, #~ so it is easier to modify text and maintain the code compact words = {'MainFormName' : "wmchoose", 'ExitText' : "Esci", 'MenuButtonText' : "Seleziona WM", 'MainFormText1' : "Questo script ti permette di selezionare il " + "tuo gestore " + "di interfaccia grafica (Windows Manager).", 'MainFormText2' : "Per selezionare o deselezionare premi spazio.", 'ConfirmExit' : "Sicuro di voler uscire?", 'ExitUserconf' : "Esci da wmchoose", 'SomethingWrong' : "Qualcosa e' andato storto! Accipigna!", 'Success' : "Successo!", 'OpSuccess' : "L'operazione e' stata portata a termine con " + "successo!\nRicorda che per avviare " + "l'interfaccia grafica devi utilizzare il " + "comando startx.", 'kde' : "KDE e' un'interfaccia ricca che ricorda Windows, " + "ma e' piu' pesante di LXDE.", 'xm' : "xmonad e' un'interfaccia per utenti molto esperti. " + "Selezionala solo se sai cosa stai facendo.", 'x4' : "XFCE e' un'interfaccia semplice e user friendly, ma completo. " + "Se sei alle prime armi seleziona questo.", 'flux' : "Fluxbox e' un'interfaccia leggera con funzionalita' minime " + "altamente personalizzabile.", 'lxde' : "LXDE e' un'interfaccia semplice e leggera che ricorda Windows, " + "puoi selezionare questo se non sei esperto.", 'aw' : "Awesome e' un'interfaccia orientato all'utilizzo di shell multipli " + "senza finestre floating o tiling.", 'i3' : "I3 e' un'interfaccia orientata all'utilizzo di shell multiple " + "con finestre floting, tiling e tabbing.", 'Warning' : "Attenzione!", 'MoreThanOne' : "Non puoi selezionare piu' di un manager!", 'AtLeastOne' : "Devi operare una scelta!", 'Customization' : "Il tuo file .xinitrc e' stato personalizzato." + " Vuoi conservare le personalizzazioni?", 'Backupped' : "Il tuo file precedente e' salvato in .xinitOLD", 'ForExperts' : "Per utenti esperti: " } ######################################################################## # # # Box: Overloads nps.BoxBasic to contain only a # # checkox widget, not a multiline # # # ######################################################################## class Box (nps.BoxTitle): _contained_widget = nps.CheckBox ######################################################################## # # # MainForm: MAIN form, call menu and other forms # # Contains menu and shortcut to every feature # # # ######################################################################## class MainForm (nps.ActionFormV2): """Class that draws the main screen of wmchoose""" #~ Rename button using Italian language OK_BUTTON_TEXT = words['ExitText'] CANCEL_BUTTON_TEXT = words['MenuButtonText'] #~ Create form elements and menus def create(self): """Add to the form the widgets""" #~ Display the options self.add(nps.FixedText, value = words['MainFormText1'], editable = False) self.add(nps.FixedText, value = words['MainFormText2'], editable = False) self.nextrely += 1 #~ Shift down the options self.x4 = self.add(Box, name = "XFCE 4", min_width = 30, max_height = 5) self.x4.entry_widget.name = words['x4'] self.lxde = self.add(Box, name = "LXDE", min_width = 30, max_height = 5) self.lxde.entry_widget.name = words['lxde'] self.kde = self.add(Box, name = "KDE", min_width = 30, max_height = 5) self.kde.entry_widget.name = words['kde'] #~ Set description self.nextrely += 1 #~ Shift down the options self.add(nps.FixedText, value = words['ForExperts'], editable = False) self.nextrely += 1 #~ Shift down the options self.flux = self.add(Box, name = "Fluxbox", min_width = 30, max_height = 5) self.flux.entry_widget.name = words['flux'] self.aw = self.add(Box, name = "Awesome", min_width = 30, max_height = 5) self.aw.entry_widget.name = words['aw'] self.i3 = self.add(Box, name = "I3", min_width = 30, max_height = 5) self.i3.entry_widget.name = words['i3'] self.xm = self.add(Box, name = "xmonad", min_width = 30, max_height = 5) self.xm.entry_widget.name = words['xm'] def on_cancel(self): """Performs checks and change .xinitrc""" #~ Check if there are more than oselection entries = (self.kde.entry_widget.value, self.xm.entry_widget.value, self.x4.entry_widget.value, self.flux.entry_widget.value, self.lxde.entry_widget.value, self.aw.entry_widget.value, self.i3.entry_widget.value) count = sum([1 for ct in entries if ct]) if (count > 1): nps.notify_confirm(words['MoreThanOne'], words['Warning']) return #~ If there are more than 1 checked retry if (count == 0): nps.notify_confirm(words['AtLeastOne'], words['Warning']) return #~ If there are less than 1 checked retry #~ Set the xinit string if (self.kde.entry_widget.value == True): text = "exec startkde\n" elif (self.xm.entry_widget.value == True): text = "exec xmonad\n" elif (self.x4.entry_widget.value == True): text = "exec startxfce4\n" elif (self.flux.entry_widget.value == True): text = "exec fluxbox\n" elif (self.lxde.entry_widget.value == True): text = "exec startlxde\n" elif (self.aw.entry_widget.value == True): text = "exec awesome\n" elif (self.i3.entry_widget.value == True): text = "exec i3\n" #~ Get the configuration file path try: xfile = getenv('HOME') + "/.xinitrc" except: nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong self.exit_application() return #~ Search for user's customizations by counting the number of lines #~ in the file. A vanilla file should have one line try: num = sum(1 for line in open(xfile)) except: nps.notify_confirm(words['SomethingWrong'], words['Warning']) #~ If something went wrong self.exit_application() return if (num <= 1): try: xinit = open(xfile, 'w') #~ Open the file in write mode xinit.truncate() #~ Erase the configuration file xinit.write(text) #~ Write configuration xinit.close() #~ Closes file nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1) self.exit_application() return except: nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong self.exit_application() return else: #~ Ask if user want to presever customizations cust = nps.notify_yes_no(words['Customization'], words['Warning'], editw = 2) if (cust): #~ If he wants delete the last line and rewrite with the new command try: xinit = open(xfile, 'r') #~ Open the file in rw mode lines = xinit.readlines() lines = lines[:-1] xinit.close() xinit = open(xfile, 'w') xinit.truncate() xinit.writelines(lines) xinit.write(text) xinit.close() nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1) self.exit_application() return except: nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) self.exit_application() return else: #~ If he doesn't want make a backup and write a new conf file try: rename(xfile, xfile + "OLD") xinit = open(xfile, 'w') #~ Open the file in append mode xinit.write("exec startxfce4\n") xinit.close() notify_confirm(words['OpSuccess'] + "\n" + words['Bakupped'], words['Success'], editw = 1) self.exit_application() return except: nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) self.exit_application() return def on_ok(self): """Ask a confirmation for exiting""" exiting = nps.notify_yes_no(words['ConfirmExit'], words['ExitText'], editw = 2) if (exiting): self.exit_application() else: pass #~ Do nothing def exit_application(self): """Closes the GUI""" self.parentApp.setNextForm(None) self.editing = False self.parentApp.switchFormNow() ######################################################################## # # # GUI: Provides every form of the interface and # # defines their ID and size # # # ######################################################################## class GUI (nps.NPSAppManaged): """Defines the whole application GUI""" def onStart( self ): """Adds the forms""" #~ Main form self.addForm('MAIN', MainForm, name = words['MainFormName']) if ( __name__ == "__main__" ): #~ Run the application gui = GUI().run();