From 4f773e1c1b8085a1ec88c80de2183e7240470232 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Sun, 2 Nov 2025 11:13:52 +1100 Subject: [PATCH] Detect fresh install and guide the user to set an encryption passphrase so they know why they're prompted --- bouquin/key_prompt.py | 4 ++-- bouquin/main_window.py | 19 ++++++++++++++++--- bouquin/settings_dialog.py | 6 +++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bouquin/key_prompt.py b/bouquin/key_prompt.py index 1fe8dee..095093c 100644 --- a/bouquin/key_prompt.py +++ b/bouquin/key_prompt.py @@ -14,8 +14,8 @@ class KeyPrompt(QDialog): def __init__( self, parent=None, - title: str = "Unlock database", - message: str = "Enter SQLCipher key", + title: str = "Enter key", + message: str = "Enter key", ): super().__init__(parent) self.setWindowTitle(title) diff --git a/bouquin/main_window.py b/bouquin/main_window.py index 0e1364f..8a03852 100644 --- a/bouquin/main_window.py +++ b/bouquin/main_window.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import sys from PySide6.QtCore import QDate, QTimer, Qt, QSettings @@ -37,8 +38,14 @@ class MainWindow(QMainWindow): self.setMinimumSize(1000, 650) self.cfg = load_db_config() + if not os.path.exists(self.cfg.path): + # Fresh database/first time use, so guide the user re: setting a key + first_time = True + else: + first_time = False + # Always prompt for the key (we never store it) - if not self._prompt_for_key_until_valid(): + if not self._prompt_for_key_until_valid(first_time): sys.exit(1) # ---- UI: Left fixed panel (calendar) + right editor ----------------- @@ -161,12 +168,18 @@ class MainWindow(QMainWindow): return False return ok - def _prompt_for_key_until_valid(self) -> bool: + def _prompt_for_key_until_valid(self, first_time: bool) -> bool: """ Prompt for the SQLCipher key. """ + if first_time: + title = "Set an encryption key" + message = "Bouquin encrypts your data.\n\nPlease create a strong passphrase to encrypt the notebook.\n\nYou can always change it later!" + else: + title = "Unlock encrypted notebook" + message = "Enter your key to unlock the notebook" while True: - dlg = KeyPrompt(self, message="Enter a key to unlock the notebook") + dlg = KeyPrompt(self, title, message) if dlg.exec() != QDialog.Accepted: return False self.cfg.key = dlg.key() diff --git a/bouquin/settings_dialog.py b/bouquin/settings_dialog.py index a59e1c6..d739630 100644 --- a/bouquin/settings_dialog.py +++ b/bouquin/settings_dialog.py @@ -76,11 +76,11 @@ class SettingsDialog(QDialog): self.accept() def _change_key(self): - p1 = KeyPrompt(self, title="Change key", message="Enter new key") + p1 = KeyPrompt(self, title="Change key", message="Enter a new encryption key") if p1.exec() != QDialog.Accepted: return new_key = p1.key() - p2 = KeyPrompt(self, title="Change key", message="Re-enter new key") + p2 = KeyPrompt(self, title="Change key", message="Re-enter the new key") if p2.exec() != QDialog.Accepted: return if new_key != p2.key(): @@ -92,7 +92,7 @@ class SettingsDialog(QDialog): try: self._db.rekey(new_key) QMessageBox.information( - self, "Key changed", "The database key was updated." + self, "Key changed", "The notebook was re-encrypted with the new key!" ) except Exception as e: QMessageBox.critical(self, "Error", f"Could not change key:\n{e}")