Skip to content

Commit

Permalink
Add automatic check for updates when app is first launched
Browse files Browse the repository at this point in the history
Automatic checks can be disabled by setting `auto_check_for_updates = false` in the global config file.
  • Loading branch information
jpgill86 committed Jan 17, 2021
1 parent b789711 commit b31541d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions neurotic/__init__.py
Expand Up @@ -78,6 +78,7 @@ def format(self, record):


global_config = {
'auto_check_for_updates': True,
'defaults': {
# defaults used by the command line interface
'file': False,
Expand Down
3 changes: 3 additions & 0 deletions neurotic/global_config_template.txt
Expand Up @@ -2,6 +2,9 @@
# Use this file to configure the way neurotic behaves. This file uses the TOML
# format.

# When the app is launched, neurotic automatically checks for updates unless
# this parameter is set to false.
# auto_check_for_updates = true

[defaults]
# When the app is launched, the following customizable defaults are used unless
Expand Down
48 changes: 35 additions & 13 deletions neurotic/gui/standalone.py
Expand Up @@ -64,7 +64,7 @@ class MainWindow(QT.QMainWindow):
"""

request_download = QT.pyqtSignal()
request_check_for_updates = QT.pyqtSignal()
request_check_for_updates = QT.pyqtSignal(bool)
request_load_dataset = QT.pyqtSignal()

def __init__(self, file=None, initial_selection=None, lazy=True, theme='light', ui_scale='medium', support_increased_line_width=False, show_datetime=False):
Expand Down Expand Up @@ -167,6 +167,15 @@ def __init__(self, file=None, initial_selection=None, lazy=True, theme='light',
self.statusBar().showMessage('ERROR: Bad dataset key, will '
'ignore', msecs=5000)

def showEvent(self, event):
"""
Executed when the window is shown.
"""
QT.QMainWindow.showEvent(self, event)

if global_config['auto_check_for_updates']:
self.check_for_updates(show_new_only=True)

def create_menus(self):
"""
Construct the menus of the app.
Expand Down Expand Up @@ -561,15 +570,15 @@ def deauthorize_gdrive(self):
self.statusBar().showMessage('Purged Google Drive authorization token',
msecs=5000)

def check_for_updates(self):
def check_for_updates(self, *args, show_new_only=False):
"""
Check for new releases in a separate thread.
"""

self.network_thread.start()
self.request_check_for_updates.emit()
self.request_check_for_updates.emit(show_new_only)

def on_version_check_finished(self, latest_release):
def on_version_check_finished(self, latest_release, show_new_only):
"""
Cleanup network thread and display a dialog window showing the state of
available updates.
Expand All @@ -592,8 +601,14 @@ def on_version_check_finished(self, latest_release):
</table></p>
<p><a href='{urls['updating']}'>How do I update <i>neurotic</i>?</a></p>
<p>Automatically check for updates at launch: {'Yes' if global_config['auto_check_for_updates'] else 'No'}<br/>
<a href='{urls['globalconfig']}'>Learn how to change this</a></p>
"""
else:
title = 'Check for updates'
return QT.QMessageBox.about(self, title, text)

elif not show_new_only:
# display up to date
text = f"""
<h2>neurotic is up to date</h2>
Expand All @@ -602,8 +617,14 @@ def on_version_check_finished(self, latest_release):
<tr><td>Installed version:</td> <td>{__version__}</td></tr>
<tr><td>Latest version:</td> <td>{latest_release}</td></tr>
</table></p>
<p>Automatically check for updates at launch: {'Yes' if global_config['auto_check_for_updates'] else 'No'}<br/>
<a href='{urls['globalconfig']}'>Learn how to change this</a></p>
"""
else:
title = 'Check for updates'
return QT.QMessageBox.about(self, title, text)

elif not show_new_only:
# display failure message
text = f"""
<h2>Could not detect latest version</h2>
Expand All @@ -616,11 +637,12 @@ def on_version_check_finished(self, latest_release):
<p><a href='{urls['releases']}'>Check for latest version manually</a></p>
<p><a href='{urls['updating']}'>How do I update <i>neurotic</i>?</a></p>
"""
title = 'Check for updates'

QT.QMessageBox.about(self, title, text)
<p>Automatically check for updates at launch: {'Yes' if global_config['auto_check_for_updates'] else 'No'}<br/>
<a href='{urls['globalconfig']}'>Learn how to change this</a></p>
"""
title = 'Check for updates'
return QT.QMessageBox.about(self, title, text)

def show_about(self):
"""
Expand Down Expand Up @@ -804,7 +826,7 @@ class _NetworkWorker(QT.QObject):
"""

download_finished = QT.pyqtSignal(bool)
version_check_finished = QT.pyqtSignal(str)
version_check_finished = QT.pyqtSignal(str, bool)

def __init__(self, mainwindow):
"""
Expand All @@ -829,7 +851,7 @@ def download(self):
finally:
self.download_finished.emit(success)

def get_latest_release_number(self):
def get_latest_release_number(self, show_new_only):
"""
Query GitHub for the version number of the latest release and emit a
signal when complete.
Expand All @@ -847,7 +869,7 @@ def get_latest_release_number(self):
# something went wrong with the query
logger.error(f'Query for latest release version failed: {e}')
finally:
self.version_check_finished.emit(latest_release)
self.version_check_finished.emit(latest_release, show_new_only)


class _LoadDatasetWorker(QT.QObject):
Expand Down

0 comments on commit b31541d

Please sign in to comment.