From 6bad1bccf829457ebc4de9c49caacc3f664eaeae Mon Sep 17 00:00:00 2001 From: Manu Date: Sun, 31 May 2020 20:01:50 +0800 Subject: [PATCH] New setting to store previously selected profile. Fixes #241 --- src/vorta/models.py | 35 ++++++++++++++++++++++++---------- src/vorta/views/main_window.py | 17 +++++++++++++---- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/vorta/models.py b/src/vorta/models.py index cbd1fc55..1aa4e550 100644 --- a/src/vorta/models.py +++ b/src/vorta/models.py @@ -15,7 +15,7 @@ from playhouse.migrate import SqliteMigrator, migrate from vorta.i18n import trans_late from vorta.utils import is_system_tray_available, slugify -SCHEMA_VERSION = 13 +SCHEMA_VERSION = 14 db = pw.Proxy() @@ -165,7 +165,8 @@ class SchemaVersion(pw.Model): class SettingsModel(pw.Model): """App settings unrelated to a single profile or repo""" key = pw.CharField(unique=True) - value = pw.BooleanField() + value = pw.BooleanField(default=False) + str_value = pw.CharField(default='') label = pw.CharField() type = pw.CharField() @@ -210,6 +211,10 @@ def get_misc_settings(): 'label': trans_late('settings', 'Open main window on startup') }, + { + 'key': 'previous_profile_id', 'str_value': '1', 'type': 'internal', + 'label': 'Previously selected profile' + }, ] if sys.platform == 'darwin': settings += [ @@ -239,14 +244,6 @@ def init_db(con=None): default_profile = BackupProfileModel(name='Default') default_profile.save() - # Create missing settings and update labels. Leave setting values untouched. - for setting in get_misc_settings(): - s, created = SettingsModel.get_or_create(key=setting['key'], defaults=setting) - if created and setting['key'] == "enable_notifications_success": - s.value = not bool(is_system_tray_available()) - s.label = setting['label'] - s.save() - # Delete old log entries after 3 months. three_months_ago = datetime.now() - timedelta(days=180) EventLogModel.delete().where(EventLogModel.start_time < three_months_ago) @@ -339,3 +336,21 @@ def init_db(con=None): ArchiveModel.insert_many(data[i:i + size], fields=fields).execute() _apply_schema_update(current_schema, 13) + + if current_schema.version < 14: + _apply_schema_update( + current_schema, 14, + migrator.add_column(SettingsModel._meta.table_name, + 'str_value', pw.CharField(default=''))) + + # Create missing settings and update labels. Leave setting values untouched. + for setting in get_misc_settings(): + s, created = SettingsModel.get_or_create(key=setting['key'], defaults=setting) + if created and setting['key'] == "enable_notifications_success": + s.value = not bool(is_system_tray_available()) + s.label = setting['label'] + s.save() + + # Delete old log entries after 3 months. + three_months_ago = datetime.now() - timedelta(days=180) + EventLogModel.delete().where(EventLogModel.start_time < three_months_ago) diff --git a/src/vorta/views/main_window.py b/src/vorta/views/main_window.py index be921657..ffdefbf4 100644 --- a/src/vorta/views/main_window.py +++ b/src/vorta/views/main_window.py @@ -6,7 +6,7 @@ from PyQt5.QtGui import QKeySequence from vorta.borg.borg_thread import BorgThread from vorta.i18n import trans_late -from vorta.models import BackupProfileModel +from vorta.models import BackupProfileModel, SettingsModel from vorta.utils import borg_compat, get_asset, is_system_tray_available from vorta.views.utils import get_colored_icon @@ -28,9 +28,14 @@ class MainWindow(MainWindowBase, MainWindowUI): self.setWindowTitle('Vorta for Borg Backup') self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) self.app = parent - self.current_profile = BackupProfileModel.select().order_by('id').first() self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint) + # Select previously used profile, if available + prev_profile_id = SettingsModel.get(key='previous_profile_id') + self.current_profile = BackupProfileModel.get_or_none(id=prev_profile_id.str_value) + if self.current_profile is None: + self.current_profile = BackupProfileModel.select().order_by('name').first() + # Load tab models self.repoTab = RepoTab(self.repoTabSlot) self.sourceTab = SourceTab(self.sourceTabSlot) @@ -56,9 +61,10 @@ class MainWindow(MainWindowBase, MainWindowUI): self.app.backup_cancelled_event.connect(self.backup_cancelled_event) # Init profile list - for profile in BackupProfileModel.select(): + for profile in BackupProfileModel.select().order_by(BackupProfileModel.name): self.profileSelector.addItem(profile.name, profile.id) - self.profileSelector.setCurrentIndex(0) + current_profile_index = self.profileSelector.findData(self.current_profile.id) + self.profileSelector.setCurrentIndex(current_profile_index) self.profileSelector.currentIndexChanged.connect(self.profile_select_action) self.profileRenameButton.clicked.connect(self.profile_rename_action) self.profileDeleteButton.clicked.connect(self.profile_delete_action) @@ -107,6 +113,9 @@ class MainWindow(MainWindowBase, MainWindowUI): self.repoTab.populate_from_profile() self.sourceTab.populate_from_profile() self.scheduleTab.populate_from_profile() + SettingsModel.update({SettingsModel.str_value: self.current_profile.id})\ + .where(SettingsModel.key == 'previous_profile_id')\ + .execute() def profile_rename_action(self): window = EditProfileWindow(rename_existing_id=self.profileSelector.currentData())