wmchoose 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/python
  2. # Author: Gabriele Bozzola (sbozzolo)
  3. # Email: sbozzolator@gmail.com
  4. # Date: 11.05.2016
  5. # Last Edit: 30.05.2016 (sbozzolo)
  6. #~ This module is used to draw the interface
  7. import npyscreen as nps
  8. #~ To rename xinitrc and to get the HOME
  9. from os import rename as rename
  10. from os import getenv as getenv
  11. #~ This dictionary is intended to contain every string of Userconf,
  12. #~ so it is easier to modify text and maintain the code compact
  13. words = {'MainFormName' : "wmchoose",
  14. 'ExitText' : "Esci",
  15. 'MenuButtonText' : "Seleziona WM",
  16. 'MainFormText1' : "Questo script ti permette di selezionare il "
  17. + "tuo gestore "
  18. + "di interfaccia grafica (Windows Manager).",
  19. 'MainFormText2' : "Per selezionare o deselezionare premi spazio.",
  20. 'ConfirmExit' : "Sicuro di voler uscire?",
  21. 'ExitUserconf' : "Esci da wmchoose",
  22. 'SomethingWrong' : "Qualcosa e' andato storto! Accipigna!",
  23. 'Success' : "Successo!",
  24. 'OpSuccess' : "L'operazione e' stata portata a termine con "
  25. + "successo!\nRicorda che per avviare "
  26. + "l'interfaccia grafica devi utilizzare il "
  27. + "comando startx.",
  28. 'kde' : "KDE e' un'interfaccia ricca che ricorda Windows, "
  29. + "ma e' piu' pesante di LXDE.",
  30. 'xm' : "xmonad e' un'interfaccia per utenti molto esperti. "
  31. + "Selezionala solo se sai cosa stai facendo.",
  32. 'x4' : "XFCE e' un'interfaccia semplice e user friendly, ma completo. "
  33. + "Se sei alle prime armi seleziona questo.",
  34. 'flux' : "Fluxbox e' un'interfaccia leggera con funzionalita' minime "
  35. + "altamente personalizzabile.",
  36. 'lxde' : "LXDE e' un'interfaccia semplice e leggera che ricorda Windows, "
  37. + "puoi selezionare questo se non sei esperto.",
  38. 'aw' : "Awesome e' un'interfaccia orientato all'utilizzo di shell multipli "
  39. + "senza finestre floating o tiling.",
  40. 'i3' : "I3 e' un'interfaccia orientata all'utilizzo di shell multiple "
  41. + "con finestre floting, tiling e tabbing.",
  42. 'Warning' : "Attenzione!",
  43. 'MoreThanOne' : "Non puoi selezionare piu' di un manager!",
  44. 'AtLeastOne' : "Devi operare una scelta!",
  45. 'Customization' : "Il tuo file .xinitrc e' stato personalizzato."
  46. + " Vuoi conservare le personalizzazioni?",
  47. 'Backupped' : "Il tuo file precedente e' salvato in .xinitOLD",
  48. 'ForExperts' : "Per utenti esperti: "
  49. }
  50. ########################################################################
  51. # #
  52. # Box: Overloads nps.BoxBasic to contain only a #
  53. # checkox widget, not a multiline #
  54. # #
  55. ########################################################################
  56. class Box (nps.BoxTitle):
  57. _contained_widget = nps.CheckBox
  58. ########################################################################
  59. # #
  60. # MainForm: MAIN form, call menu and other forms #
  61. # Contains menu and shortcut to every feature #
  62. # #
  63. ########################################################################
  64. class MainForm (nps.ActionFormV2):
  65. """Class that draws the main screen of wmchoose"""
  66. #~ Rename button using Italian language
  67. OK_BUTTON_TEXT = words['ExitText']
  68. CANCEL_BUTTON_TEXT = words['MenuButtonText']
  69. #~ Create form elements and menus
  70. def create(self):
  71. """Add to the form the widgets"""
  72. #~ Display the options
  73. self.add(nps.FixedText, value = words['MainFormText1'], editable = False)
  74. self.add(nps.FixedText, value = words['MainFormText2'], editable = False)
  75. self.nextrely += 1 #~ Shift down the options
  76. self.x4 = self.add(Box, name = "XFCE 4", min_width = 30, max_height = 5)
  77. self.x4.entry_widget.name = words['x4']
  78. self.lxde = self.add(Box, name = "LXDE", min_width = 30, max_height = 5)
  79. self.lxde.entry_widget.name = words['lxde']
  80. self.kde = self.add(Box, name = "KDE", min_width = 30, max_height = 5)
  81. self.kde.entry_widget.name = words['kde'] #~ Set description
  82. self.nextrely += 1 #~ Shift down the options
  83. self.add(nps.FixedText, value = words['ForExperts'], editable = False)
  84. self.nextrely += 1 #~ Shift down the options
  85. self.flux = self.add(Box, name = "Fluxbox", min_width = 30, max_height = 5)
  86. self.flux.entry_widget.name = words['flux']
  87. self.aw = self.add(Box, name = "Awesome", min_width = 30, max_height = 5)
  88. self.aw.entry_widget.name = words['aw']
  89. self.i3 = self.add(Box, name = "I3", min_width = 30, max_height = 5)
  90. self.i3.entry_widget.name = words['i3']
  91. self.xm = self.add(Box, name = "xmonad", min_width = 30, max_height = 5)
  92. self.xm.entry_widget.name = words['xm']
  93. def on_cancel(self):
  94. """Performs checks and change .xinitrc"""
  95. #~ Check if there are more than oselection
  96. entries = (self.kde.entry_widget.value,
  97. self.xm.entry_widget.value,
  98. self.x4.entry_widget.value,
  99. self.flux.entry_widget.value,
  100. self.lxde.entry_widget.value,
  101. self.aw.entry_widget.value,
  102. self.i3.entry_widget.value)
  103. count = sum([1 for ct in entries if ct])
  104. if (count > 1):
  105. nps.notify_confirm(words['MoreThanOne'], words['Warning'])
  106. return #~ If there are more than 1 checked retry
  107. if (count == 0):
  108. nps.notify_confirm(words['AtLeastOne'], words['Warning'])
  109. return #~ If there are less than 1 checked retry
  110. #~ Set the xinit string
  111. if (self.kde.entry_widget.value == True):
  112. text = "exec startkde\n"
  113. elif (self.xm.entry_widget.value == True):
  114. text = "exec xmonad\n"
  115. elif (self.x4.entry_widget.value == True):
  116. text = "exec startxfce4\n"
  117. elif (self.flux.entry_widget.value == True):
  118. text = "exec fluxbox\n"
  119. elif (self.lxde.entry_widget.value == True):
  120. text = "exec startlxde\n"
  121. elif (self.aw.entry_widget.value == True):
  122. text = "exec awesome\n"
  123. elif (self.i3.entry_widget.value == True):
  124. text = "exec i3\n"
  125. #~ Get the configuration file path
  126. try:
  127. xfile = getenv('HOME') + "/.xinitrc"
  128. except:
  129. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  130. self.exit_application()
  131. return
  132. #~ Search for user's customizations by counting the number of lines
  133. #~ in the file. A vanilla file should have one line
  134. try:
  135. num = sum(1 for line in open(xfile))
  136. except:
  137. nps.notify_confirm(words['SomethingWrong'], words['Warning']) #~ If something went wrong
  138. self.exit_application()
  139. return
  140. if (num <= 1):
  141. try:
  142. xinit = open(xfile, 'w') #~ Open the file in write mode
  143. xinit.truncate() #~ Erase the configuration file
  144. xinit.write(text) #~ Write configuration
  145. xinit.close() #~ Closes file
  146. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  147. self.exit_application()
  148. return
  149. except:
  150. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  151. self.exit_application()
  152. return
  153. else:
  154. #~ Ask if user want to presever customizations
  155. cust = nps.notify_yes_no(words['Customization'], words['Warning'], editw = 2)
  156. if (cust): #~ If he wants delete the last line and rewrite with the new command
  157. try:
  158. xinit = open(xfile, 'r') #~ Open the file in rw mode
  159. lines = xinit.readlines()
  160. lines = lines[:-1]
  161. xinit.close()
  162. xinit = open(xfile, 'w')
  163. xinit.truncate()
  164. xinit.writelines(lines)
  165. xinit.write(text)
  166. xinit.close()
  167. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  168. self.exit_application()
  169. return
  170. except:
  171. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  172. self.exit_application()
  173. return
  174. else: #~ If he doesn't want make a backup and write a new conf file
  175. try:
  176. rename(xfile, xfile + "OLD")
  177. xinit = open(xfile, 'w') #~ Open the file in append mode
  178. xinit.write("exec startxfce4\n")
  179. xinit.close()
  180. notify_confirm(words['OpSuccess'] + "\n" + words['Bakupped'],
  181. words['Success'], editw = 1)
  182. self.exit_application()
  183. return
  184. except:
  185. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  186. self.exit_application()
  187. return
  188. def on_ok(self):
  189. """Ask a confirmation for exiting"""
  190. exiting = nps.notify_yes_no(words['ConfirmExit'], words['ExitText'], editw = 2)
  191. if (exiting):
  192. self.exit_application()
  193. else:
  194. pass #~ Do nothing
  195. def exit_application(self):
  196. """Closes the GUI"""
  197. self.parentApp.setNextForm(None)
  198. self.editing = False
  199. self.parentApp.switchFormNow()
  200. ########################################################################
  201. # #
  202. # GUI: Provides every form of the interface and #
  203. # defines their ID and size #
  204. # #
  205. ########################################################################
  206. class GUI (nps.NPSAppManaged):
  207. """Defines the whole application GUI"""
  208. def onStart( self ):
  209. """Adds the forms"""
  210. #~ Main form
  211. self.addForm('MAIN', MainForm, name = words['MainFormName'])
  212. if ( __name__ == "__main__" ):
  213. #~ Run the application
  214. gui = GUI().run();