wmchoose 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/python
  2. # Author: Gabriele Bozzola (sbozzolo)
  3. # Email: sbozzolator@gmail.com
  4. # Date: 11.05.2016
  5. # Last Edit: 23.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' Kebab",
  29. 'xm' : "xmonad e' un'interfaccia per utenti molto esperti",
  30. 'x4' : "XFCE e' un'interfaccia semplice e user friendly. "
  31. + "Se sei alle prime armi seleziona questo.",
  32. 'flux' : "Fluxbox e' Gelato",
  33. 'lxde' : "LXDE e' Sushi",
  34. 'aw' : "Awesome e' Piadina",
  35. 'i3' : "I3 e' Banana",
  36. 'Warning' : "Attenzione!",
  37. 'MoreThanOne' : "Non puoi selezionare piu' di un manager!",
  38. 'AtLeastOne' : "Devi operare una scelta!",
  39. 'Customization' : "Il tuo file .xinitrc e' stato personalizzato."
  40. + " Vuoi conservare le personalizzazioni?",
  41. 'Backupped' : "Il tuo file precedente e' salvato in .xinitOLD"
  42. }
  43. ########################################################################
  44. # #
  45. # Box: Overloads nps.BoxBasic to contain only a #
  46. # checkox widget, not a multiline #
  47. # #
  48. ########################################################################
  49. class Box (nps.BoxTitle):
  50. _contained_widget = nps.CheckBox
  51. ########################################################################
  52. # #
  53. # MainForm: MAIN form, call menu and other forms #
  54. # Contains menu and shortcut to every feature #
  55. # #
  56. ########################################################################
  57. class MainForm (nps.ActionFormV2):
  58. """Class that draws the main screen of wmchoose"""
  59. #~ Rename button using Italian language
  60. OK_BUTTON_TEXT = words['ExitText']
  61. CANCEL_BUTTON_TEXT = words['MenuButtonText']
  62. #~ Create form elements and menus
  63. def create(self):
  64. """Add to the form the widgets"""
  65. #~ Display the options
  66. self.add(nps.FixedText, value = words['MainFormText'], editable = False)
  67. self.nextrely += 1 #~ Shift down the options
  68. self.x4 = self.add(Box, name = "XFCE 4", min_width = 30, max_height = 5)
  69. self.x4.entry_widget.name = words['x4']
  70. self.lxde = self.add(Box, name = "LXDE", min_width = 30, max_height = 5)
  71. self.lxde.entry_widget.name = words['lxde']
  72. self.kde = self.add(Box, name = "KDE", min_width = 30, max_height = 5)
  73. self.kde.entry_widget.name = words['kde'] #~ Set description
  74. self.flux = self.add(Box, name = "Fluxbox", min_width = 30, max_height = 5)
  75. self.flux.entry_widget.name = words['flux']
  76. self.aw = self.add(Box, name = "Awesome", min_width = 30, max_height = 5)
  77. self.aw.entry_widget.name = words['aw']
  78. self.i3 = self.add(Box, name = "I3", min_width = 30, max_height = 5)
  79. self.i3.entry_widget.name = words['i3']
  80. self.xm = self.add(Box, name = "xmonad", min_width = 30, max_height = 5)
  81. self.xm.entry_widget.name = words['xm']
  82. def on_cancel(self):
  83. """Performs checks and change .xinitrc"""
  84. #~ Check if there are more than oselection
  85. entries = (self.kde.entry_widget.value,
  86. self.xm.entry_widget.value,
  87. self.x4.entry_widget.value,
  88. self.flux.entry_widget.value,
  89. self.lxde.entry_widget.value,
  90. self.aw.entry_widget.value,
  91. self.i3.entry_widget.value)
  92. count = sum([1 for ct in entries if ct])
  93. if (count > 1):
  94. nps.notify_confirm(words['MoreThanOne'], words['Warning'])
  95. return #~ If there are more than 1 checked retry
  96. if (count == 0):
  97. nps.notify_confirm(words['AtLeastOne'], words['Warning'])
  98. return #~ If there are less than 1 checked retry
  99. #~ Set the xinit string
  100. if (self.kde.entry_widget.value == True):
  101. text = "exec startkde\n"
  102. elif (self.xm.entry_widget.value == True):
  103. text = "exec xmonad\n"
  104. elif (self.x4.entry_widget.value == True):
  105. text = "exec startxfce4\n"
  106. elif (self.flux.entry_widget.value == True):
  107. text = "exec fluxbox\n"
  108. elif (self.lxde.entry_widget.value == True):
  109. text = "exec startlxde\n"
  110. elif (self.aw.entry_widget.value == True):
  111. text = "exec awesome\n"
  112. elif (self.i3.entry_widget.value == True):
  113. text = "exec i3\n"
  114. #~ Get the configuration file path
  115. try:
  116. xfile = getenv('HOME') + "/.xinitrc"
  117. except:
  118. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  119. self.exit_application()
  120. return
  121. #~ Search for user's customizations by counting the number of lines
  122. #~ in the file. A vanilla file should have one line
  123. try:
  124. num = sum(1 for line in open(xfile))
  125. except:
  126. nps.notify_confirm(words['SomethingWrong'], words['Warning']) #~ If something went wrong
  127. self.exit_application()
  128. return
  129. if (num <= 1):
  130. try:
  131. xinit = open(xfile, 'w') #~ Open the file in write mode
  132. xinit.truncate() #~ Erase the configuration file
  133. xinit.write(text) #~ Write configuration
  134. xinit.close() #~ Closes file
  135. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  136. self.exit_application()
  137. return
  138. except:
  139. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1) #~ If something went wrong
  140. self.exit_application()
  141. return
  142. else:
  143. #~ Ask if user want to presever customizations
  144. cust = nps.notify_yes_no(words['Customization'], words['Warning'], editw = 2)
  145. if (cust): #~ If he wants delete the last line and rewrite with the new command
  146. try:
  147. xinit = open(xfile, 'r') #~ Open the file in rw mode
  148. lines = xinit.readlines()
  149. lines = lines[:-1]
  150. xinit.close()
  151. xinit = open(xfile, 'w')
  152. xinit.truncate()
  153. xinit.writelines(lines)
  154. xinit.write(text)
  155. xinit.close()
  156. nps.notify_confirm(words['OpSuccess'], words['Success'], editw = 1)
  157. self.exit_application()
  158. return
  159. except:
  160. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  161. self.exit_application()
  162. return
  163. else: #~ If he doesn't want make a backup and write a new conf file
  164. try:
  165. rename(xfile, xfile + "OLD")
  166. xinit = open(xfile, 'w') #~ Open the file in append mode
  167. xinit.write("exec startxfce4\n")
  168. xinit.close()
  169. notify_confirm(words['OpSuccess'] + "\n" + words['Bakupped'],
  170. words['Success'], editw = 1)
  171. self.exit_application()
  172. return
  173. except:
  174. nps.notify_confirm(words['SomethingWrong'], words['Warning'], editw = 1)
  175. self.exit_application()
  176. return
  177. def on_ok(self):
  178. """Ask a confirmation for exiting"""
  179. exiting = nps.notify_yes_no(words['ConfirmExit'], words['ExitText'], editw = 2)
  180. if (exiting):
  181. self.exit_application()
  182. else:
  183. pass #~ Do nothing
  184. def exit_application(self):
  185. """Closes the GUI"""
  186. self.parentApp.setNextForm(None)
  187. self.editing = False
  188. self.parentApp.switchFormNow()
  189. ########################################################################
  190. # #
  191. # GUI: Provides every form of the interface and #
  192. # defines their ID and size #
  193. # #
  194. ########################################################################
  195. class GUI (nps.NPSAppManaged):
  196. """Defines the whole application GUI"""
  197. def onStart( self ):
  198. """Adds the forms"""
  199. #~ Main form
  200. self.addForm('MAIN', MainForm, name = words['MainFormName'])
  201. if ( __name__ == "__main__" ):
  202. #~ Run the application
  203. gui = GUI().run();