Skip to content

Commit a4cbeed

Browse files
committed
Added "Generate Password" btn to the "More options" dialog
1 parent 1277457 commit a4cbeed

2 files changed

Lines changed: 68 additions & 58 deletions

File tree

src/gui/more_options_dialog.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,18 @@ def __init__(self, parent):
7575
self.periodic_row.add_row(self.dirRow)
7676

7777
# Adw.ActionRow for entering a password for the archive encryption
78-
self.get_password_from_file()
79-
8078
self.cpwdRow = Adw.PasswordEntryRow.new()
8179
self.cpwdRow.set_title(_("Password for encryption"))
82-
try:
83-
self.cpwdRow.set_text(self.password)
84-
except:
85-
self.cpwdRow.set_text("")
8680
self.periodic_row.add_row(self.cpwdRow)
81+
self._get_password_from_file()
82+
83+
# Button for generating strong password
84+
self.pswdgenButton = Gtk.Button.new_from_icon_name("dialog-password-symbolic")
85+
self.pswdgenButton.set_tooltip_text(_("Generate Password"))
86+
self.pswdgenButton.add_css_class("flat")
87+
self.pswdgenButton.set_valign(Gtk.Align.CENTER)
88+
self.pswdgenButton.connect("clicked", self._get_generated_password)
89+
self.cpwdRow.add_suffix(self.pswdgenButton)
8790

8891
# Manual saving section
8992
self.manRow = Adw.ExpanderRow.new()
@@ -131,12 +134,16 @@ def _expand_periodic_row(self):
131134
if os.path.exists(f"{CACHE}/expand_pb_row"):
132135
self.periodic_row.set_expanded(True)
133136

134-
def get_password_from_file(self):
137+
def _get_password_from_file(self):
135138
if os.path.exists(f"{DATA}/password"):
136139
p = PasswordStore()
137-
self.password = p.password
140+
self.cpwdRow.set_text(p.password)
138141
else:
139-
self.password = ""
142+
self.cpwdRow.set_text("")
143+
144+
def _get_generated_password(self, w):
145+
self.password = self.parent._password_generator()
146+
self.cpwdRow.set_text(self.password)
140147

141148
def open_file_dialog(self, w):
142149
self.parent.select_pb_folder(w="")

src/gui/window.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -446,42 +446,6 @@ def import_selected(source, res, data):
446446

447447
# Dialog for creating password for the config archive
448448
def create_password_dialog(self):
449-
# Action after closing pswdDialog
450-
def pswdDialog_closed(w, response):
451-
if response == 'ok':
452-
with open(f"{CACHE}/temp_file", "w") as tmp:
453-
tmp.write(self.pswdEntry.get_text())
454-
self.save_config()
455-
456-
# Check the password to see if it meets the criteria
457-
def check_password(pswdEntry):
458-
password = self.pswdEntry.get_text()
459-
criteria = [
460-
(len(password) < 12, "The password is too short. It should has at least 12 characters"),
461-
(not re.search(r'[A-Z]', password), "The password should has at least one capital letter"),
462-
(not re.search(r'[a-z]', password), "The password should has at least one lowercase letter"),
463-
(not re.search(r'[-_@.:,+=]', password), "The password should has at least one special character"),
464-
(" " in password, "The password must not contain spaces")
465-
]
466-
467-
for condition, message in criteria:
468-
if condition:
469-
self.pswdDialog.set_response_enabled("ok", False)
470-
print(message)
471-
return
472-
473-
self.pswdDialog.set_response_enabled("ok", True)
474-
475-
# Generate Password
476-
def pswd_generator(w):
477-
safe = "-_@.:,+="
478-
allc = safe + string.ascii_letters + string.digits
479-
password = [random.choice(safe), random.choice(string.ascii_letters), random.choice(string.digits)] + \
480-
[random.choice(allc) for _ in range(21)]
481-
random.shuffle(password)
482-
password = ''.join(password)
483-
self.pswdEntry.set_text(password)
484-
485449
# Dialog itself
486450
self.pswdDialog = Adw.AlertDialog.new()
487451
self.pswdDialog.set_heading(_("Create new password"))
@@ -491,34 +455,64 @@ def pswd_generator(w):
491455
self.pswdDialog.add_response("ok", _("Apply"))
492456
self.pswdDialog.set_response_enabled("ok", False)
493457
self.pswdDialog.set_response_appearance('ok', Adw.ResponseAppearance.SUGGESTED)
494-
self.pswdDialog.connect('response', pswdDialog_closed)
458+
self.pswdDialog.connect('response', self._pswdDialog_closed)
495459
self.pswdDialog.present()
496460

497461
# Button for generating strong password
498462
self.pswdgenButton = Gtk.Button.new_from_icon_name("dialog-password-symbolic")
499463
self.pswdgenButton.set_tooltip_text(_("Generate Password"))
500464
self.pswdgenButton.add_css_class("flat")
501465
self.pswdgenButton.set_valign(Gtk.Align.CENTER)
502-
self.pswdgenButton.connect("clicked", pswd_generator)
466+
self.pswdgenButton.connect("clicked", self._get_generated_password)
503467

504468
# entry for entering password
505469
self.pswdEntry = Adw.PasswordEntryRow.new()
506470
self.pswdEntry.set_title(_("Password"))
507-
self.pswdEntry.connect('changed', check_password)
471+
self.pswdEntry.connect('changed', self._check_password)
508472
self.pswdEntry.add_suffix(self.pswdgenButton)
509473
self.pswdDialog.set_extra_child(self.pswdEntry)
510474

511-
# dialog for entering password of the archive
512-
def check_password_dialog(self):
513-
# action after closing dialog for checking password
514-
def checkDialog_closed(w, response):
515-
if response == 'ok':
516-
self.checkDialog.set_response_enabled("ok", False)
517-
with open(f"{CACHE}/temp_file", "w") as tmp:
518-
tmp.write(self.checkEntry.get_text())
475+
# Action after closing pswdDialog
476+
def _pswdDialog_closed(self, w, response):
477+
if response == 'ok':
478+
with open(f"{CACHE}/temp_file", "w") as tmp:
479+
tmp.write(self.pswdEntry.get_text())
480+
self.save_config()
481+
482+
# Check the password to see if it meets the criteria
483+
def _check_password(self, pswdEntry):
484+
password = self.pswdEntry.get_text()
485+
criteria = [
486+
(len(password) < 12, "The password is too short. It should has at least 12 characters"),
487+
(not re.search(r'[A-Z]', password), "The password should has at least one capital letter"),
488+
(not re.search(r'[a-z]', password), "The password should has at least one lowercase letter"),
489+
(not re.search(r'[-_@.:,+=]', password), "The password should has at least one special character"),
490+
(" " in password, "The password must not contain spaces")
491+
]
492+
493+
for condition, message in criteria:
494+
if condition:
495+
self.pswdDialog.set_response_enabled("ok", False)
496+
print(message)
497+
return
519498

520-
self.import_config()
499+
self.pswdDialog.set_response_enabled("ok", True)
521500

501+
# Generate Password
502+
def _get_generated_password(self, w):
503+
self.password = self._password_generator()
504+
self.pswdEntry.set_text(self.password)
505+
506+
def _password_generator(self):
507+
safe = "-_@.:,+="
508+
allc = safe + string.ascii_letters + string.digits
509+
password = [random.choice(safe), random.choice(string.ascii_letters), random.choice(string.digits)] + \
510+
[random.choice(allc) for _ in range(21)]
511+
random.shuffle(password)
512+
return ''.join(password)
513+
514+
# dialog for entering password of the archive
515+
def check_password_dialog(self):
522516
# Dialog itself
523517
self.checkDialog = Adw.AlertDialog.new()
524518
self.checkDialog.set_heading(_("Unlock the archive with a password"))
@@ -527,13 +521,22 @@ def checkDialog_closed(w, response):
527521
self.checkDialog.add_response("cancel", _("Cancel"))
528522
self.checkDialog.add_response("ok", _("Apply"))
529523
self.checkDialog.set_response_appearance('ok', Adw.ResponseAppearance.SUGGESTED)
530-
self.checkDialog.connect('response', checkDialog_closed)
524+
self.checkDialog.connect('response', self._checkDialog_closed)
531525
self.checkDialog.present()
532526

533527
self.checkEntry = Adw.PasswordEntryRow.new()
534528
self.checkEntry.set_title(_("Password"))
535529
self.checkDialog.set_extra_child(self.checkEntry)
536530

531+
# action after closing dialog for checking password
532+
def _checkDialog_closed(self, w, response):
533+
if response == 'ok':
534+
self.checkDialog.set_response_enabled("ok", False)
535+
with open(f"{CACHE}/temp_file", "w") as tmp:
536+
tmp.write(self.checkEntry.get_text())
537+
538+
self.import_config()
539+
537540
# Save configuration
538541
def save_config(self):
539542
self.archive_mode = "--create"

0 commit comments

Comments
 (0)