123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #!/usr/bin/python
- # Author: Gabriele Bozzola (sbozzolo)
- # Email: sbozzolator@gmail.com
- # Date: 11.05.2016
- # Last Edit: 12.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",
- 'MainFormText' : "Questo script ti selezionare il tuo gestore "
- + "di interfaccia grafica (Windows Manager)."
- + " Per selezionarlo premi invio.",
- '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' Kebab",
- 'xm' : "xmonad e' un'interfaccia per utenti molto esperti",
- 'x4' : "XFCE e' un'interfaccia semplice e user friendly. "
- + "Se sei alle prime armi seleziona questo.",
- 'flux' : "Fluxbox e' Gelato",
- 'lxde' : "LXDE e' Sushi",
- 'aw' : "Awesome e' Piadina",
- 'i3' : "I3 e' Banana",
- '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"
- }
-
- ########################################################################
- # #
- # 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['MainFormText'], 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.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.lxde.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();
|