wmchoose 10 KB

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