Skip to content

Commit 190bc75

Browse files
committed
[Enhancement] Added setting to run vorta in background
- Added setting to run vorta in background - Added option in the mics tab to enable/disable the background question popup.
1 parent a00ed62 commit 190bc75

6 files changed

Lines changed: 71 additions & 34 deletions

File tree

src/vorta/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def open_main_window_action(self):
128128

129129
def toggle_main_window_visibility(self):
130130
if self.main_window.isVisible():
131-
self.main_window.close()
131+
self.main_window.hide()
132132
else:
133133
self.open_main_window_action()
134134

src/vorta/store/connection.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
from datetime import datetime, timedelta
33
from peewee import Tuple, fn
4-
from playhouse import signals
5-
from vorta.autostart import open_app_at_startup
64
from .migrations import run_migrations
75
from .models import (
86
DB,
@@ -21,12 +19,6 @@
2119
SCHEMA_VERSION = 20
2220

2321

24-
@signals.post_save(sender=SettingsModel)
25-
def setup_autostart(model_class, instance, created):
26-
if instance.key == 'autostart':
27-
open_app_at_startup(instance.value)
28-
29-
3022
def cleanup_db():
3123
# Clean up database
3224
DB.execute_sql("VACUUM")

src/vorta/store/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def get_misc_settings() -> List[Dict[str, str]]:
1717
startup = trans_late('settings', 'Startup')
1818
information = trans_late('settings', 'Information')
1919
security = trans_late('settings', 'Security')
20+
background = trans_late('settings', 'Background')
2021

2122
# Default settings for all platforms.
2223
settings = [
@@ -122,9 +123,10 @@ def get_misc_settings() -> List[Dict[str, str]]:
122123
'key': 'enable_background_question',
123124
'value': True,
124125
'type': 'checkbox',
126+
'group': background,
125127
'label': trans_late(
126128
'settings',
127-
"If the system tray isn't available, " "ask whether to continue in the background " "on exit",
129+
"When closing the window, ask whether to continue in the background",
128130
),
129131
},
130132
{

src/vorta/views/main_window.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from PyQt5.QtWidgets import QApplication, QCheckBox, QFileDialog, QMenu, QMessageBox, QShortcut, QToolTip
77
from vorta.profile_export import ImportFailedException, ProfileExport
88
from vorta.store.models import BackupProfileModel, SettingsModel
9-
from vorta.utils import borg_compat, get_asset, get_network_status_monitor, is_system_tray_available
9+
from vorta.utils import borg_compat, get_asset, get_network_status_monitor
1010
from vorta.views.partials.loading_button import LoadingButton
1111
from vorta.views.utils import get_colored_icon
1212
from .archive_tab import ArchiveTab
@@ -277,27 +277,26 @@ def closeEvent(self, event):
277277
SettingsModel.key == 'previous_window_height'
278278
).execute()
279279

280-
if not is_system_tray_available():
281-
if SettingsModel.get(key="enable_background_question").value:
282-
msg = QMessageBox()
283-
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
284-
msg.setParent(self, QtCore.Qt.Sheet)
285-
msg.setText(self.tr("Should Vorta continue to run in the background?"))
286-
msg.button(QMessageBox.Yes).clicked.connect(
287-
lambda: self.miscTab.save_setting("disable_background_state", True)
288-
)
289-
msg.button(QMessageBox.No).clicked.connect(
290-
lambda: (
291-
self.miscTab.save_setting("disable_background_state", False),
292-
self.app.quit(),
293-
)
280+
if SettingsModel.get(key="enable_background_question").value:
281+
msg = QMessageBox()
282+
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
283+
msg.setParent(self, QtCore.Qt.Sheet)
284+
msg.setText(self.tr("Should Vorta continue to run in the background?"))
285+
msg.button(QMessageBox.Yes).clicked.connect(
286+
lambda: self.miscTab.save_setting("disable_background_state", True)
287+
)
288+
msg.button(QMessageBox.No).clicked.connect(
289+
lambda: (
290+
self.miscTab.save_setting("disable_background_state", False),
291+
self.app.quit(),
294292
)
295-
msg.setWindowTitle(self.tr("Quit"))
296-
dont_show_box = QCheckBox(self.tr("Don't show this again"))
297-
dont_show_box.clicked.connect(lambda x: self.miscTab.save_setting("enable_background_question", not x))
298-
dont_show_box.setTristate(False)
299-
msg.setCheckBox(dont_show_box)
300-
msg.exec()
301-
elif not SettingsModel.get(key="disable_background_state").value:
302-
self.app.quit()
293+
)
294+
msg.setWindowTitle(self.tr("Quit"))
295+
dont_show_box = QCheckBox(self.tr("Don't show this again"))
296+
dont_show_box.clicked.connect(lambda x: self.miscTab.save_setting("enable_background_question", not x))
297+
dont_show_box.setTristate(False)
298+
msg.setCheckBox(dont_show_box)
299+
msg.exec()
300+
elif not SettingsModel.get(key="disable_background_state").value:
301+
self.app.quit()
303302
event.accept()

src/vorta/views/misc_tab.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
from typing import Dict
3+
from playhouse import signals
24
from PyQt5 import uic
35
from PyQt5.QtCore import Qt
46
from PyQt5.QtWidgets import QApplication, QCheckBox, QFormLayout, QHBoxLayout, QLabel, QSizePolicy, QSpacerItem
57
from vorta._version import __version__
8+
from vorta.autostart import open_app_at_startup
69
from vorta.config import LOG_DIR
710
from vorta.i18n import translate
811
from vorta.store.models import BackupProfileMixin, SettingsModel
@@ -22,6 +25,8 @@ def __init__(self, parent=None):
2225
"""Init."""
2326
super().__init__(parent)
2427
self.setupUi(parent)
28+
self.settings_checkboxes: Dict[str, QCheckBox] = {}
29+
2530
self.versionLabel.setText(__version__)
2631
self.logLink.setText(
2732
f'<a href="file://{LOG_DIR}"><span style="text-decoration:' 'underline; color:#0984e3;">Log</span></a>'
@@ -34,6 +39,7 @@ def __init__(self, parent=None):
3439
self.checkboxLayout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.FieldsStayAtSizeHint)
3540
self.checkboxLayout.setFormAlignment(Qt.AlignmentFlag.AlignHCenter)
3641
self.tooltip_buttons = []
42+
signals.post_save.connect(self.on_setting_update, sender=SettingsModel)
3743

3844
self.populate()
3945

@@ -86,8 +92,8 @@ def populate(self):
8692

8793
# create widget
8894
cb = QCheckBox(translate('settings', setting.label))
95+
cb.setChecked(setting.value)
8996
cb.setToolTip(setting.tooltip)
90-
cb.setCheckState(setting.value)
9197
cb.setTristate(False)
9298
cb.stateChanged.connect(lambda v, key=setting.key: self.save_setting(key, v))
9399

@@ -101,6 +107,7 @@ def populate(self):
101107
cbl.addItem(QSpacerItem(0, 0, hPolicy=QSizePolicy.Policy.Expanding))
102108

103109
# add widget
110+
self.settings_checkboxes[setting.key] = cb
104111
self.checkboxLayout.setLayout(i, QFormLayout.ItemRole.FieldRole, cbl)
105112
self.tooltip_buttons.append(tb)
106113

@@ -109,6 +116,41 @@ def populate(self):
109116

110117
self.set_icons()
111118

119+
def on_setting_update(self, sender, instance: SettingsModel, created=False):
120+
"""
121+
Handle a update of the settings db.
122+
Non-PyQt slot for peewee's `playhouse.signals` api.
123+
It calls `update_checkbox`.
124+
125+
Parameters
126+
----------
127+
sender : Type[SettingsModel]
128+
table sending model
129+
instance : SettingsModel
130+
The model instance (row) saved.
131+
created : bool, optional
132+
Whether it was newly created, by default False
133+
"""
134+
if not created and instance.type == 'checkbox':
135+
self.update_checkbox(instance.key, instance.value)
136+
if instance.key == 'autostart':
137+
open_app_at_startup(instance.value)
138+
139+
def update_checkbox(self, key, value):
140+
"""
141+
Update the checkbox for a setting with a given key.
142+
143+
Parameters
144+
----------
145+
key : str
146+
The key of the setting to update.
147+
value : bool
148+
The value to set the checkbox to.
149+
"""
150+
checkbox = self.settings_checkboxes.get(key)
151+
if checkbox:
152+
checkbox.setChecked(value)
153+
112154
def set_icons(self):
113155
"""Set or update the icons in this view."""
114156
for button in self.tooltip_buttons:

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import MagicMock
55
import pytest
66
from peewee import SqliteDatabase
7+
from playhouse import signals
78
import vorta
89
import vorta.application
910
import vorta.borg.jobs_manager
@@ -91,6 +92,7 @@ def init_db(qapp, qtbot, tmpdir_factory):
9192
source_dir.save()
9293

9394
qapp.main_window.deleteLater()
95+
signals.post_save.disconnect(receiver=qapp.main_window.miscTab.on_setting_update, sender=SettingsModel)
9496
del qapp.main_window
9597
qapp.main_window = MainWindow(qapp) # Re-open main window to apply mock data in UI
9698

0 commit comments

Comments
 (0)