Skip to content

Commit

Permalink
Ask for update check permission (TODO: actual check)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Nov 5, 2017
1 parent 7ac8cfc commit 109f33e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
60 changes: 57 additions & 3 deletions pext/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class SortMode(IntEnum):


class ConfigRetriever():
"""Retrieve configuration entries."""
"""Retrieve global configuration entries."""

def __init__(self) -> None:
"""Initialize the configuration."""
"""Initialize the configuration location."""
# Initialze defaults
try:
config_home = os.environ['XDG_CONFIG_HOME']
Expand All @@ -117,6 +117,28 @@ def get_setting(self, variable: str) -> str:
"""Get a specific configuration setting."""
return self.config[variable]

def get_updatecheck_permission_asked(self) -> bool:
"""Return info on if allowing updates was asked."""
try:
with open(os.path.join(self.get_setting('config_path'), 'update_check_enabled'), 'r') as update_check_file:
return True
except (FileNotFoundError):
return False

def get_updatecheck_permission(self) -> bool:
"""Return info on if update checking is allowed."""
try:
with open(os.path.join(self.get_setting('config_path'), 'update_check_enabled'), 'r') as update_check_file:
result = update_check_file.readline()
return True if result == str(1) else False
except (FileNotFoundError):
return False

def save_updatecheck_permission(self, granted: bool) -> None:
"""Save the current updatecheck permission status."""
with open(os.path.join(self.get_setting('config_path'), 'update_check_enabled'), 'w') as update_check_file:
update_check_file.write(str(int(granted)))


class RunConseq():
"""A simple helper to run several functions consecutively."""
Expand Down Expand Up @@ -1853,6 +1875,8 @@ def __init__(self, settings: Dict, config_retriever: ConfigRetriever, parent=Non
QObject, "menuMinimizeToTrayManually")
menu_show_tray_icon_shortcut = self.window.findChild(
QObject, "menuShowTrayIcon")
self.menu_enable_update_check_shortcut = self.window.findChild(
QObject, "menuEnableUpdateCheck")

menu_quit_shortcut = self.window.findChild(QObject, "menuQuit")
menu_quit_without_saving_shortcut = self.window.findChild(
Expand Down Expand Up @@ -1892,6 +1916,7 @@ def __init__(self, settings: Dict, config_retriever: ConfigRetriever, parent=Non
menu_minimize_normally_manually_shortcut.toggled.connect(self._menu_minimize_normally_manually)
menu_minimize_to_tray_manually_shortcut.toggled.connect(self._menu_minimize_to_tray_manually)
menu_show_tray_icon_shortcut.toggled.connect(self._menu_toggle_tray_icon)
self.menu_enable_update_check_shortcut.toggled.connect(self._menu_toggle_update_check)

menu_quit_shortcut.triggered.connect(self.quit)
menu_quit_without_saving_shortcut.triggered.connect(
Expand Down Expand Up @@ -1926,6 +1951,9 @@ def __init__(self, settings: Dict, config_retriever: ConfigRetriever, parent=Non
QQmlProperty.write(menu_show_tray_icon_shortcut,
"checked",
self.settings['tray'])
QQmlProperty.write(self.menu_enable_update_check_shortcut,
"checked",
self.settings['update_check'])

# Get reference to tabs list
self.tabs = self.window.findChild(QObject, "tabs")
Expand All @@ -1937,6 +1965,18 @@ def __init__(self, settings: Dict, config_retriever: ConfigRetriever, parent=Non
if not settings['background']:
self.show()

if self.settings['update_check'] == None:
# Ask if the user wants to enable automatic update checking
permissionDialogEngine = QQmlApplicationEngine(self)
permissionDialogEngine.load(QUrl.fromLocalFile(os.path.join(AppFile.get_path(), 'qml', 'UpdatePermissionDialog.qml')))

updatePermissionDialog = permissionDialogEngine.rootObjects()[0]

# Save user choice
updatePermissionDialog.yes.connect(lambda: self._menu_toggle_update_check(True))
updatePermissionDialog.no.connect(lambda: self._menu_toggle_update_check(False))
self.window.windowStateChanged.connect(self._process_window_state)

def _bind_context(self) -> None:
"""Bind the context for the module."""
current_tab = QQmlProperty.read(self.tabs, "currentIndex")
Expand Down Expand Up @@ -2195,6 +2235,12 @@ def _menu_toggle_tray_icon(self, enabled: bool) -> None:
except AttributeError:
pass

def _menu_toggle_update_check(self, enabled: bool) -> None:
self.settings['update_check'] = enabled
QQmlProperty.write(self.menu_enable_update_check_shortcut,
"checked",
self.settings['update_check'])

def _search(self) -> None:
try:
self._get_current_element()['vm'].search(manual=True)
Expand Down Expand Up @@ -2612,7 +2658,8 @@ def _load_settings(argv: List[str], config_retriever: ConfigRetriever) -> Dict:
'profile': 'default',
'save_settings': True,
'sort_mode': SortMode.Module,
'tray': True}
'tray': True,
'update_check': None} # None = not asked, True/False = permission

# getopt requires all possible options to be listed, but we do not know
# more about module-specific options in advance than that they start with
Expand Down Expand Up @@ -2792,6 +2839,10 @@ def _load_settings(argv: List[str], config_retriever: ConfigRetriever) -> Dict:
elif opt == "--no-tray":
settings['tray'] = False

# See if automatic update checks are allowed
if config_retriever.get_updatecheck_permission_asked():
settings['update_check'] = config_retriever.get_updatecheck_permission()

return settings


Expand All @@ -2806,6 +2857,9 @@ def _shut_down(pidfile: str, profile: str, window: Window, config_retriever: Con
ProfileManager(config_retriever).save_theme(profile, window.settings['theme'])
ProfileManager(config_retriever).save_settings(profile, window.settings)

if window.settings['update_check'] != None:
config_retriever.save_updatecheck_permission(window.settings['update_check'])


def usage() -> None:
"""Print usage information."""
Expand Down
32 changes: 32 additions & 0 deletions pext/qml/UpdatePermissionDialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright (c) 2017 Sylvia van Os <sylvia@hackerchick.me>
This file is part of Pext
Pext is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 2.3
import QtQuick.Dialogs 1.2

MessageDialog {
title: "Pext"
icon: StandardIcon.Question
standardButtons: StandardButton.Yes | StandardButton.No

text: qsTr("May Pext automatically check for updates? You can change this at any time from the settings menu.")

Component.onCompleted: visible = true;
}

8 changes: 8 additions & 0 deletions pext/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ ApplicationWindow {
text: qsTr("Show tray icon")
checkable: true
}

MenuSeparator { }

MenuItem {
objectName: "menuEnableUpdateCheck"
text: qsTr("Automatically check for updates")
checkable: true
}
}

Menu {
Expand Down

0 comments on commit 109f33e

Please sign in to comment.