wmchoose 10 KB

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