PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cnchi/user_info.py

https://gitlab.com/EntityOS/Cnchi
Python | 335 lines | 210 code | 65 blank | 60 comment | 35 complexity | 0e1aa64deda30ea50725adc5c0f7510c MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # user_info.py
  5. #
  6. # Copyright © 2013-2016 Antergos
  7. #
  8. # This file is part of Cnchi.
  9. #
  10. # Cnchi is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Cnchi is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # The following additional terms are in effect as per Section 7 of the license:
  21. #
  22. # The preservation of all legal notices and author attributions in
  23. # the material or in the Appropriate Legal Notices displayed
  24. # by works containing it is required.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with Cnchi; If not, see <http://www.gnu.org/licenses/>.
  28. """ User info screen """
  29. from gi.repository import Gtk
  30. import misc.validation as validation
  31. import show_message as show
  32. from gtkbasebox import GtkBaseBox
  33. ICON_OK = "emblem-default"
  34. ICON_WARNING = "dialog-warning"
  35. # import misc.camera as camera
  36. # camera.cheese_init()
  37. class UserInfo(GtkBaseBox):
  38. """ Asks for user information """
  39. def __init__(self, params, prev_page=None, next_page="slides"):
  40. super().__init__(self, params, "user_info", prev_page, next_page)
  41. self.image_is_ok = dict()
  42. self.image_is_ok['fullname'] = self.ui.get_object('fullname_ok')
  43. self.image_is_ok['hostname'] = self.ui.get_object('hostname_ok')
  44. self.image_is_ok['username'] = self.ui.get_object('username_ok')
  45. self.image_is_ok['password'] = self.ui.get_object('password_ok')
  46. self.error_label = dict()
  47. self.error_label['hostname'] = self.ui.get_object('hostname_error_label')
  48. self.error_label['username'] = self.ui.get_object('username_error_label')
  49. self.error_label['password'] = self.ui.get_object('password_error_label')
  50. self.password_strength = self.ui.get_object('password_strength')
  51. self.entry = dict()
  52. self.entry['fullname'] = self.ui.get_object('fullname')
  53. self.entry['hostname'] = self.ui.get_object('hostname')
  54. self.entry['username'] = self.ui.get_object('username')
  55. self.entry['password'] = self.ui.get_object('password')
  56. self.entry['verified_password'] = self.ui.get_object('verified_password')
  57. self.login = dict()
  58. self.login['auto'] = self.ui.get_object('login_auto')
  59. self.login['pass'] = self.ui.get_object('login_pass')
  60. self.login['encrypt'] = self.ui.get_object('login_encrypt')
  61. self.require_password = True
  62. self.encrypt_home = False
  63. # self.camera_window = self.ui.get_object('cheese_box')
  64. # self.camera = camera.CameraBox()
  65. self.camera_window = None
  66. self.camera = None
  67. if self.camera and self.camera.found():
  68. self.camera_window.add(self.camera)
  69. self.camera.show()
  70. else:
  71. pass
  72. # We don't have a camera.
  73. # Move all fields to the right (to center them).
  74. # user_info_grid = self.ui.get_object('user_info_grid')
  75. # user_info_grid.set_property('margin_start', 140)
  76. def translate_ui(self):
  77. """ Translates all ui elements """
  78. label = self.ui.get_object('fullname_label')
  79. txt = _("Your name:")
  80. label.set_markup(txt)
  81. label = self.ui.get_object('fullname')
  82. txt = _("Your name")
  83. label.set_placeholder_text(txt)
  84. label = self.ui.get_object('hostname_label')
  85. txt = _("Your computer's name:")
  86. label.set_markup(txt)
  87. label = self.ui.get_object('hostname')
  88. txt = _("Hostname")
  89. label.set_placeholder_text(txt)
  90. label = self.ui.get_object('username_label')
  91. txt = _("Pick a username:")
  92. label.set_markup(txt)
  93. label = self.ui.get_object('username')
  94. txt = _("Username")
  95. label.set_placeholder_text(txt)
  96. label = self.ui.get_object('password_label')
  97. txt = _("Choose a password:")
  98. label.set_markup(txt)
  99. label = self.ui.get_object('password')
  100. txt = _("Password")
  101. label.set_placeholder_text(txt)
  102. label = self.ui.get_object('verified_password_label')
  103. txt = _("Confirm your password:")
  104. label.set_markup(txt)
  105. label = self.ui.get_object('verified_password')
  106. txt = _("Confirm password")
  107. label.set_placeholder_text(txt)
  108. label = self.ui.get_object('hostname_extra_label')
  109. txt = _("Identifies your system to other computers and devices.")
  110. txt = '<span size="small">{0}</span>'.format(txt)
  111. label.set_markup(txt)
  112. small_dark_red = '<small><span color="darkred">{0}</span></small>'
  113. txt = _("You must enter a name")
  114. txt = small_dark_red.format(txt)
  115. self.error_label['hostname'].set_markup(txt)
  116. txt = _("You must enter a username")
  117. txt = small_dark_red.format(txt)
  118. self.error_label['username'].set_markup(txt)
  119. txt = _("You must enter a password")
  120. txt = small_dark_red.format(txt)
  121. self.error_label['password'].set_markup(txt)
  122. self.login['auto'].set_label(_("Log in automatically"))
  123. self.login['pass'].set_label(_("Require my password to log in"))
  124. self.login['encrypt'].set_label(_("Encrypt my home folder"))
  125. btn = self.ui.get_object('checkbutton_show_password')
  126. btn.set_label(_("show password"))
  127. self.header.set_subtitle(_("Create Your User Account"))
  128. # Restore forward button text (from install now! to go-next)
  129. # self.forward_button.set_label("")
  130. # image1 = Gtk.Image.new_from_icon_name(
  131. # "go-next",
  132. # Gtk.IconSize.LARGE_TOOLBAR)
  133. # self.forward_button.set_image(image1)
  134. # self.forward_button.set_always_show_image(True)
  135. def hide_widgets(self):
  136. """ Hide unused and message widgets """
  137. ok_widgets = self.image_is_ok.values()
  138. for ok_widget in ok_widgets:
  139. ok_widget.hide()
  140. error_label_widgets = self.error_label.values()
  141. for error_label in error_label_widgets:
  142. error_label.hide()
  143. self.password_strength.hide()
  144. # Hide encryption if using LUKS encryption (user must use one or
  145. # the other but not both)
  146. if self.settings.get('use_luks'):
  147. self.login['encrypt'].hide()
  148. # TODO: Fix home encryption and stop hiding its widget
  149. if not self.settings.get('z_hidden'):
  150. self.login['encrypt'].hide()
  151. def store_values(self):
  152. """ Store all user values in self.settings """
  153. # For developer testing
  154. # Do not use this, is confusing for others when testing dev version
  155. self.settings.set('fullname', self.entry['fullname'].get_text())
  156. self.settings.set('hostname', self.entry['hostname'].get_text())
  157. self.settings.set('username', self.entry['username'].get_text())
  158. self.settings.set('password', self.entry['password'].get_text())
  159. self.settings.set('require_password', self.require_password)
  160. self.settings.set('encrypt_home', False)
  161. if self.encrypt_home:
  162. message = _("Are you sure you want to encrypt your home directory?")
  163. res = show.question(self.get_main_window(), message)
  164. if res == Gtk.ResponseType.YES:
  165. self.settings.set('encrypt_home', True)
  166. # Let installer_process know that all info has been entered
  167. self.settings.set('user_info_done', True)
  168. return True
  169. def prepare(self, direction):
  170. """ Prepare screen """
  171. self.translate_ui()
  172. self.show_all()
  173. self.hide_widgets()
  174. # Disable autologin if using 'base' desktop
  175. if self.settings.get('desktop') == "base":
  176. self.login['auto'].set_sensitive(False)
  177. self.forward_button.set_label(_('Save'))
  178. self.forward_button.set_name('fwd_btn_save')
  179. self.forward_button.set_sensitive(False)
  180. def on_checkbutton_show_password_toggled(self, widget):
  181. """ show/hide user password """
  182. btn = self.ui.get_object('checkbutton_show_password')
  183. shown = btn.get_active()
  184. self.entry['password'].set_visibility(shown)
  185. self.entry['verified_password'].set_visibility(shown)
  186. def on_authentication_toggled(self, widget):
  187. """ User has changed autologin or home encrypting """
  188. if widget == self.login['auto']:
  189. if self.login['auto'].get_active():
  190. self.require_password = False
  191. else:
  192. self.require_password = True
  193. if widget == self.login['encrypt']:
  194. if self.login['encrypt'].get_active():
  195. self.encrypt_home = True
  196. else:
  197. self.encrypt_home = False
  198. def validate(self, element, value):
  199. """ Check that what the user is typing is ok """
  200. if len(value) == 0:
  201. self.image_is_ok[element].set_from_icon_name(
  202. ICON_WARNING,
  203. Gtk.IconSize.LARGE_TOOLBAR)
  204. self.image_is_ok[element].show()
  205. self.error_label[element].show()
  206. else:
  207. result = validation.check(element, value)
  208. if not result:
  209. self.image_is_ok[element].set_from_icon_name(
  210. ICON_OK,
  211. Gtk.IconSize.LARGE_TOOLBAR)
  212. self.image_is_ok[element].show()
  213. self.error_label[element].hide()
  214. else:
  215. self.image_is_ok[element].set_from_icon_name(
  216. ICON_WARNING,
  217. Gtk.IconSize.LARGE_TOOLBAR)
  218. self.image_is_ok[element].show()
  219. if validation.NAME_BADCHAR in result:
  220. txt = _("Invalid characters entered")
  221. elif validation.NAME_BADDOTS in result:
  222. txt = _("Username can't contain dots")
  223. elif validation.NAME_LENGTH in result:
  224. txt = _("Too many characters")
  225. else:
  226. txt = _("Unknown error")
  227. my_format = "<small><span color='darkred'>{0}</span></small>"
  228. txt = my_format.format(txt)
  229. self.error_label[element].set_markup(txt)
  230. self.error_label[element].show()
  231. def info_loop(self, widget):
  232. """ User has introduced new information. Check it here. """
  233. if widget == self.entry['fullname']:
  234. fullname = self.entry['fullname'].get_text()
  235. if fullname:
  236. self.image_is_ok['fullname'].set_from_icon_name(
  237. ICON_OK,
  238. Gtk.IconSize.LARGE_TOOLBAR)
  239. else:
  240. self.image_is_ok['fullname'].set_from_icon_name(
  241. ICON_WARNING,
  242. Gtk.IconSize.LARGE_TOOLBAR)
  243. self.image_is_ok['fullname'].show()
  244. elif widget == self.entry['hostname']:
  245. hostname = self.entry['hostname'].get_text()
  246. self.validate('hostname', hostname)
  247. elif widget == self.entry['username']:
  248. username = self.entry['username'].get_text()
  249. self.validate('username', username)
  250. elif (widget == self.entry['password'] or
  251. widget == self.entry['verified_password']):
  252. validation.check_password(
  253. self.entry['password'],
  254. self.entry['verified_password'],
  255. self.image_is_ok['password'],
  256. self.error_label['password'],
  257. self.password_strength)
  258. # Check if all fields are filled and ok
  259. all_ok = True
  260. ok_widgets = self.image_is_ok.values()
  261. if not self.settings.get('z_hidden'):
  262. for ok_widget in ok_widgets:
  263. icon_name = ok_widget.get_property('icon-name')
  264. visible = ok_widget.is_visible()
  265. if not visible or icon_name == ICON_WARNING:
  266. all_ok = False
  267. self.forward_button.set_sensitive(all_ok)
  268. if __name__ == '__main__':
  269. from test_screen import _, run
  270. run('UserInfo')