emailconf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/python
  2. # Author: Gabriele Bozzola (sbozzolo)
  3. # Email: sbozzolator@gmail.com
  4. # Date: 28.02.2017
  5. # Last Edit: 28.02.2017 (sbozzolo)
  6. #~ This module is used to draw the interface
  7. import npyscreen as nps
  8. #~ To rename forward 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 emailconf,
  12. #~ so it is easier to modify text and maintain the code compact
  13. words = {'MainFormName' : "emailconf",
  14. 'ExitText' : "Esci",
  15. 'MenuButtonText' : "Configura",
  16. 'MainFormText1' : "Questo script ti permette di configurare la "
  17. + "tua email di LCM. ",
  18. 'MainFormText2' : "Questa e' la tua configurazione attuale, che puoi modificare"
  19. + " usando la barra spaziatrice",
  20. 'ConfirmExit' : "Sicuro di voler uscire?",
  21. 'ExitUserconf' : "Esci da emailconf",
  22. 'SomethingWrong' : "Qualcosa e' andato storto! Accipigna!",
  23. 'Success' : "Successo!",
  24. 'OpSuccess' : "L'operazione e' stata portata a termine con successo!",
  25. 'ign' : "Ignora tutte le email della casella LCM",
  26. 'forw' : "Invia alla tua casella locale e all'indirizzo sottostante",
  27. 'red' : "Invia solo all'indirizzo sottostante ",
  28. 'Warning' : "Attenzione!",
  29. 'MoreThanOne' : "Non puoi selezionare piu' di una opzione!",
  30. 'AtLeastOne' : "Devi operare una scelta!",
  31. 'Ignore' : "Ignora",
  32. 'Rederict' : "Rederict",
  33. 'Forward' : "Forward",
  34. 'ToEmail' : "Indirizzo email: "
  35. }
  36. ########################################################################
  37. # #
  38. # Box: Overloads nps.BoxBasic to contain only a #
  39. # checkox widget, not a multiline #
  40. # #
  41. ########################################################################
  42. class Box (nps.BoxTitle):
  43. _contained_widget = nps.CheckBox
  44. ########################################################################
  45. # #
  46. # MainForm: MAIN form, call menu and other forms #
  47. # Contains menu and shortcut to every feature #
  48. # #
  49. ########################################################################
  50. class MainForm (nps.ActionFormV2):
  51. """Class that draws the main screen of wmchoose"""
  52. #~ Rename button using Italian language
  53. OK_BUTTON_TEXT = words['ExitText']
  54. CANCEL_BUTTON_TEXT = words['MenuButtonText']
  55. #~ Create form elements and menus
  56. def create(self):
  57. """Add to the form the widgets"""
  58. #~ Get the configuration file path
  59. xfile = getenv('HOME') + "/.forward"
  60. try:
  61. #~ Open file and count lines
  62. forward = open(xfile,'r')
  63. try:
  64. num = sum(1 for line in forward)
  65. except:
  66. nps.notify_confirm(words['SomethingWrong'], words['Warning'])
  67. self.exit_application()
  68. return
  69. except:
  70. # If file does not exist, create it
  71. num = 0
  72. forward = open(xfile,'w')
  73. forward.close()
  74. #~ If file has zero lines user doesn't get email (ignore)
  75. #~ If file has one line user gets emails only in his other address (rederict)
  76. #~ If file has two lines user gets email both in this account and other address (forward)
  77. #~ To have the correct box checked
  78. iv = False
  79. rv = False
  80. fv = False
  81. #~ One of this values will be set to True, this will
  82. #~ activate the checkbox corresponing to the present user
  83. #~ configuration
  84. if (num == 0):
  85. old_email = ""
  86. iv = True
  87. elif (num == 1):
  88. forward = open(xfile, 'r') #~ Open the file in r mode
  89. old_email = forward.readline().strip()
  90. forward.close() #~ Closes file
  91. rv = True
  92. elif (num == 2):
  93. #~ Example of .forword with this setting:
  94. #~ sbozzolo
  95. #~ sbozzolator@gmail.com
  96. #~ The second line contains the address
  97. forward = open(xfile, 'r') #~ Open the file in r mode
  98. lines = forward.readlines()
  99. old_email = lines[1].strip()
  100. forward.close() #~ Closes file
  101. fv = True
  102. else:
  103. nps.notify_confirm(words['SomethingWrong'], words['Warning']) #~ If something went wrong
  104. self.exit_application()
  105. return
  106. #~ Display the options
  107. self.add(nps.FixedText, value = words['MainFormText1'], editable = False)
  108. self.nextrely += 1 #~ Shift down the options
  109. self.add(nps.FixedText, value = words['MainFormText2'], editable = False)
  110. self.nextrely += 1 #~ Shift down the options
  111. self.ign = self.add(Box, name = words['Ignore'], min_width = 30, max_height = 5, value = iv)
  112. self.ign.entry_widget.name = words['ign']
  113. self.nextrely += 1 #~ Shift down the options
  114. self.red = self.add(Box, name = words['Rederict'], min_width = 30, max_height = 5, value = rv)
  115. self.red.entry_widget.name = words['red']
  116. self.nextrely += 1 #~ Shift down the options
  117. self.forw = self.add(Box, name = words['Forward'], min_width = 30, max_height = 5, value = fv)
  118. self.forw.entry_widget.name = words['forw']
  119. self.nextrely += 1 #~ Shift down the options
  120. self.email = self.add(nps.TitleText, name = words['ToEmail'], value = old_email,
  121. min_width = 30, max_height = 5, use_two_lines = False,
  122. begin_entry_at = 25)
  123. def on_cancel(self):
  124. """Performs checks and change .forward"""
  125. #~ Check if there are more than oselection
  126. entries = (self.red.entry_widget.value,
  127. self.forw.entry_widget.value,
  128. self.ign.entry_widget.value)
  129. count = sum([1 for ct in entries if ct])
  130. if (count > 1):
  131. nps.notify_confirm(words['MoreThanOne'], words['Warning'])
  132. return #~ If there are more than 1 checked retry
  133. if (count == 0):
  134. nps.notify_confirm(words['AtLeastOne'], words['Warning'])
  135. return #~ If there are less than 1 checked retry
  136. #~ What to write in .forward
  137. if self.ign.entry_widget.value == True: text = ""
  138. if self.red.entry_widget.value == True: text = self.email.value + "\n"
  139. if self.forw.entry_widget.value == True: text = getenv('USER') + "\n" + self.email.value + "\n"
  140. try:
  141. xfile = getenv('HOME') + "/.forward"
  142. forward = open(xfile, 'w') #~ Open the file in write mode
  143. forward.truncate() #~ Erase the configuration file
  144. forward.write(text) #~ Write configuration
  145. forward.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. def on_ok(self):
  154. """Ask a confirmation for exiting"""
  155. exiting = nps.notify_yes_no(words['ConfirmExit'], words['ExitText'], editw = 2)
  156. if (exiting):
  157. self.exit_application()
  158. else:
  159. pass #~ Do nothing
  160. def exit_application(self):
  161. """Closes the GUI"""
  162. self.parentApp.setNextForm(None)
  163. self.editing = False
  164. self.parentApp.switchFormNow()
  165. ########################################################################
  166. # #
  167. # GUI: Provides every form of the interface and #
  168. # defines their ID and size #
  169. # #
  170. ########################################################################
  171. class GUI (nps.NPSAppManaged):
  172. """Defines the whole application GUI"""
  173. def onStart( self ):
  174. """Adds the forms"""
  175. #~ Main form
  176. self.addForm('MAIN', MainForm, name = words['MainFormName'])
  177. if ( __name__ == "__main__" ):
  178. #~ Run the application
  179. gui = GUI().run();