wmchoose 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 'MainFormText' : "Questo script ti permette di selezionare il "
  17. + "tuo gestore "
  18. + "di interfaccia grafica (Windows Manager)."
  19. + " 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. + "Selezionalo 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['MainFormText'], editable = False)
  74. self.nextrely += 1 #~ Shift down the options
  75. self.x4 = self.add(Box, name = "XFCE 4", min_width = 30, max_height = 5)
  76. self.x4.entry_widget.name = words['x4']
  77. self.lxde = self.add(Box, name = "LXDE", min_width = 30, max_height = 5)
  78. self.lxde.entry_widget.name = words['lxde']
  79. self.kde = self.add(Box, name = "KDE", min_width = 30, max_height = 5)
  80. self.kde.entry_widget.name = words['kde'] #~ Set description
  81. self.nextrely += 2 #~ Shift down the options
  82. self.add(nps.FixedText, value = words['ForExperts'], editable = False)
  83. self.nextrely += 2 #~ Shift down the options
  84. self.flux = self.add(Box, name = "Fluxbox", min_width = 30, max_height = 5)
  85. self.flux.entry_widget.name = words['flux']
  86. self.aw = self.add(Box, name = "Awesome", min_width = 30, max_height = 5)
  87. self.aw.entry_widget.name = words['aw']
  88. self.i3 = self.add(Box, name = "I3", min_width = 30, max_height = 5)
  89. self.i3.entry_widget.name = words['i3']
  90. self.xm = self.add(Box, name = "xmonad", min_width = 30, max_height = 5)
  91. self.xm.entry_widget.name = words['xm']
  92. def on_cancel(self):
  93. """Performs checks and change .xinitrc"""
  94. #~ Check if there are more than oselection
  95. entries = (self.kde.entry_widget.value,
  96. self.xm.entry_widget.value,
  97. self.x4.entry_widget.value,
  98. self.flux.entry_widget.value,
  99. self.lxde.entry_widget.value,
  100. self.aw.entry_widget.value,
  101. self.i3.entry_widget.value)
  102. count = sum([1 for ct in entries if ct])
  103. if (count > 1):
  104. nps.notify_confirm(words['MoreThanOne'], words['Warning'])
  105. return #~ If there are more than 1 checked retry
  106. if (count == 0):
  107. nps.notify_confirm(words['AtLeastOne'], words['Warning'])
  108. return #~ If there are less than 1 checked retry
  109. #~ Set the xinit string
  110. if (self.kde.entry_widget.value == True):
  111. text = "exec startkde\n"
  112. elif (self.xm.entry_widget.value == True):
  113. text = "exec xmonad\n"
  114. elif (self.x4.entry_widget.value == True):
  115. text = "exec startxfce4\n"
  116. elif (self.flux.entry_widget.value == True):
  117. text = "exec fluxbox\n"
  118. elif (self.lxde.entry_widget.value == True):
  119. text = "exec startlxde\n"
  120. elif (self.aw.entry_widget.value == True):
  121. text = "exec awesome\n"
  122. elif (self.i3.entry_widget.value == True):
  123. text = "exec i3\n"
  124. #~ Get the configuration file path
  125. try:
  126. xfile = getenv('HOME') + "/.xinitrc"
  127. except:
  128. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  129. self.exit_application()
  130. return
  131. #~ Search for user's customizations by counting the number of lines
  132. #~ in the file. A vanilla file should have one line
  133. try:
  134. num = sum(1 for line in open(xfile))
  135. except:
  136. nps.notify_confirm(words['SomethingWrong'], words['Warning']) #~ If something went wrong
  137. self.exit_application()
  138. return
  139. if (num <= 1):
  140. try:
  141. xinit = open(xfile, 'w') #~ Open the file in write mode
  142. xinit.truncate() #~ Erase the configuration file
  143. xinit.write(text) #~ Write configuration
  144. xinit.close() #~ Closes file
  145. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  146. self.exit_application()
  147. return
  148. except:
  149. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  150. self.exit_application()
  151. return
  152. else:
  153. #~ Ask if user want to presever customizations
  154. cust = nps.notify_yes_no(words['Customization'], words['Warning'], editw = 2)
  155. if (cust): #~ If he wants delete the last line and rewrite with the new command
  156. try:
  157. xinit = open(xfile, 'r') #~ Open the file in rw mode
  158. lines = xinit.readlines()
  159. lines = lines[:-1]
  160. xinit.close()
  161. xinit = open(xfile, 'w')
  162. xinit.truncate()
  163. xinit.writelines(lines)
  164. xinit.write(text)
  165. xinit.close()
  166. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  167. self.exit_application()
  168. return
  169. except:
  170. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  171. self.exit_application()
  172. return
  173. else: #~ If he doesn't want make a backup and write a new conf file
  174. try:
  175. rename(xfile, xfile + "OLD")
  176. xinit = open(xfile, 'w') #~ Open the file in append mode
  177. xinit.write("exec startxfce4\n")
  178. xinit.close()
  179. notify_confirm(words['OpSuccess'] + "\n" + words['Bakupped'],
  180. words['Success'], editw = 1)
  181. self.exit_application()
  182. return
  183. except:
  184. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  185. self.exit_application()
  186. return
  187. def on_ok(self):
  188. """Ask a confirmation for exiting"""
  189. exiting = nps.notify_yes_no(words['ConfirmExit'], words['ExitText'], editw = 2)
  190. if (exiting):
  191. self.exit_application()
  192. else:
  193. pass #~ Do nothing
  194. def exit_application(self):
  195. """Closes the GUI"""
  196. self.parentApp.setNextForm(None)
  197. self.editing = False
  198. self.parentApp.switchFormNow()
  199. ########################################################################
  200. # #
  201. # GUI: Provides every form of the interface and #
  202. # defines their ID and size #
  203. # #
  204. ########################################################################
  205. class GUI (nps.NPSAppManaged):
  206. """Defines the whole application GUI"""
  207. def onStart( self ):
  208. """Adds the forms"""
  209. #~ Main form
  210. self.addForm('MAIN', MainForm, name = words['MainFormName'])
  211. if ( __name__ == "__main__" ):
  212. #~ Run the application
  213. gui = GUI().run();