New setting to store previously selected profile. Fixes #241

This commit is contained in:
Manu 2020-05-31 20:01:50 +08:00
parent eedd1184a5
commit 6bad1bccf8
2 changed files with 38 additions and 14 deletions

View File

@ -15,7 +15,7 @@ from playhouse.migrate import SqliteMigrator, migrate
from vorta.i18n import trans_late from vorta.i18n import trans_late
from vorta.utils import is_system_tray_available, slugify from vorta.utils import is_system_tray_available, slugify
SCHEMA_VERSION = 13 SCHEMA_VERSION = 14
db = pw.Proxy() db = pw.Proxy()
@ -165,7 +165,8 @@ class SchemaVersion(pw.Model):
class SettingsModel(pw.Model): class SettingsModel(pw.Model):
"""App settings unrelated to a single profile or repo""" """App settings unrelated to a single profile or repo"""
key = pw.CharField(unique=True) key = pw.CharField(unique=True)
value = pw.BooleanField() value = pw.BooleanField(default=False)
str_value = pw.CharField(default='')
label = pw.CharField() label = pw.CharField()
type = pw.CharField() type = pw.CharField()
@ -210,6 +211,10 @@ def get_misc_settings():
'label': trans_late('settings', 'label': trans_late('settings',
'Open main window on startup') 'Open main window on startup')
}, },
{
'key': 'previous_profile_id', 'str_value': '1', 'type': 'internal',
'label': 'Previously selected profile'
},
] ]
if sys.platform == 'darwin': if sys.platform == 'darwin':
settings += [ settings += [
@ -239,14 +244,6 @@ def init_db(con=None):
default_profile = BackupProfileModel(name='Default') default_profile = BackupProfileModel(name='Default')
default_profile.save() 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. # Delete old log entries after 3 months.
three_months_ago = datetime.now() - timedelta(days=180) three_months_ago = datetime.now() - timedelta(days=180)
EventLogModel.delete().where(EventLogModel.start_time < three_months_ago) 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() ArchiveModel.insert_many(data[i:i + size], fields=fields).execute()
_apply_schema_update(current_schema, 13) _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)

View File

@ -6,7 +6,7 @@ from PyQt5.QtGui import QKeySequence
from vorta.borg.borg_thread import BorgThread from vorta.borg.borg_thread import BorgThread
from vorta.i18n import trans_late 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.utils import borg_compat, get_asset, is_system_tray_available
from vorta.views.utils import get_colored_icon from vorta.views.utils import get_colored_icon
@ -28,9 +28,14 @@ class MainWindow(MainWindowBase, MainWindowUI):
self.setWindowTitle('Vorta for Borg Backup') self.setWindowTitle('Vorta for Borg Backup')
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.app = parent self.app = parent
self.current_profile = BackupProfileModel.select().order_by('id').first()
self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint) 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 # Load tab models
self.repoTab = RepoTab(self.repoTabSlot) self.repoTab = RepoTab(self.repoTabSlot)
self.sourceTab = SourceTab(self.sourceTabSlot) self.sourceTab = SourceTab(self.sourceTabSlot)
@ -56,9 +61,10 @@ class MainWindow(MainWindowBase, MainWindowUI):
self.app.backup_cancelled_event.connect(self.backup_cancelled_event) self.app.backup_cancelled_event.connect(self.backup_cancelled_event)
# Init profile list # 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.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.profileSelector.currentIndexChanged.connect(self.profile_select_action)
self.profileRenameButton.clicked.connect(self.profile_rename_action) self.profileRenameButton.clicked.connect(self.profile_rename_action)
self.profileDeleteButton.clicked.connect(self.profile_delete_action) self.profileDeleteButton.clicked.connect(self.profile_delete_action)
@ -107,6 +113,9 @@ class MainWindow(MainWindowBase, MainWindowUI):
self.repoTab.populate_from_profile() self.repoTab.populate_from_profile()
self.sourceTab.populate_from_profile() self.sourceTab.populate_from_profile()
self.scheduleTab.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): def profile_rename_action(self):
window = EditProfileWindow(rename_existing_id=self.profileSelector.currentData()) window = EditProfileWindow(rename_existing_id=self.profileSelector.currentData())