Skip to content

Commit

Permalink
Add Offline Indicator to StatusBar (#164)
Browse files Browse the repository at this point in the history
* Add Offline Indicator to StatusBar

* Use GitHub as host and reduce timeout

* Check online status in separate thread

Co-authored-by: DavidoTek <[email protected]>
  • Loading branch information
sonic2kk and DavidoTek authored Dec 21, 2022
1 parent e089455 commit ecc1dcf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pupgui2/pupgui2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import pkgutil
import requests
import subprocess
import threading

from PySide6.QtCore import Qt, QCoreApplication, QObject, QThread, QWaitCondition, QMutex, QDataStream
from PySide6.QtCore import QByteArray, QEvent, Slot, QTranslator, QLocale, QLibraryInfo
from PySide6.QtCore import QByteArray, QEvent, Signal, Slot, QTranslator, QLocale, QLibraryInfo
from PySide6.QtGui import QIcon, QKeyEvent
from PySide6.QtWidgets import QApplication, QDialog, QMessageBox, QLabel, QPushButton, QCheckBox, QProgressBar, QVBoxLayout
from PySide6.QtUiTools import QUiLoader
Expand All @@ -25,7 +26,7 @@
from pupgui2.steamutil import get_steam_acruntime_list, get_steam_game_list
from pupgui2.util import apply_dark_theme, create_compatibilitytools_folder, get_installed_ctools, remove_ctool
from pupgui2.util import install_directory, available_install_directories, get_install_location_from_directory_name
from pupgui2.util import print_system_information, single_instance, download_awacy_gamelist
from pupgui2.util import print_system_information, single_instance, download_awacy_gamelist, is_online


class InstallWineThread(QThread):
Expand Down Expand Up @@ -73,6 +74,8 @@ def stop(self):

class MainWindow(QObject):

update_statusbar_message = Signal(str)

def __init__(self):
super(MainWindow, self).__init__()

Expand All @@ -99,6 +102,7 @@ def __init__(self):

self.load_ui()
self.setup_ui()
self.update_statusbar_message.connect(self.ui.statusBar().showMessage)
self.update_ui()

self.ui.show()
Expand Down Expand Up @@ -134,7 +138,7 @@ def setup_ui(self):
self.ui.btnRemoveSelected.setEnabled(False)
self.ui.btnShowCtInfo.setEnabled(False)

self.ui.statusBar().showMessage(f'{APP_NAME} {APP_VERSION}')
self.set_default_statusbar()

self.giw = GamepadInputWorker()
if os.getenv('PUPGUI2_DISABLE_GAMEPAD', '0') == '0':
Expand All @@ -146,6 +150,16 @@ def setup_ui(self):
self.install_thread.start()
QApplication.instance().aboutToQuit.connect(self.install_thread.stop)

def set_default_statusbar(self):
""" Show the default text in the status bar - non-blocking using update_statusbar_message Signal """
def _set_default_statusbar_thread(update_statusbar_message: Signal):
if not is_online():
update_statusbar_message.emit(f'{APP_NAME} {APP_VERSION} (Offline)')
else:
update_statusbar_message.emit(f'{APP_NAME} {APP_VERSION}')
t = threading.Thread(target=_set_default_statusbar_thread, args=[self.update_statusbar_message])
t.start()

def update_combo_install_location(self):
self.updating_combo_install_location = True

Expand Down Expand Up @@ -194,7 +208,7 @@ def update_ui(self):

self.ui.txtActiveDownloads.setText(str(len(self.pending_downloads)))
if len(self.pending_downloads) == 0:
self.ui.statusBar().showMessage(f'{APP_NAME} {APP_VERSION}')
self.set_default_statusbar()
self.progressBarDownload.setVisible(False)
self.ui.comboInstallLocation.setEnabled(True)

Expand Down Expand Up @@ -229,7 +243,7 @@ def set_fetching_releases(self, value):
if value:
self.ui.statusBar().showMessage(self.tr('Fetching releases...'))
else:
self.ui.statusBar().showMessage(f'{APP_NAME} {APP_VERSION}')
self.set_default_statusbar()

def set_download_progress_percent(self, value):
""" set download progress bar value and update status bar text """
Expand Down
13 changes: 13 additions & 0 deletions pupgui2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,16 @@ def ghapi_rlcheck(json: dict):
if 'API rate limit exceeded' in json.get('message', ''):
print('Warning: GitHub API rate limit exceeded. See https://github.com/DavidoTek/ProtonUp-Qt/issues/161#issuecomment-1358200080 for details.')
return json

def is_online(host='https://api.github.com/repos/', timeout=3) -> bool:
"""
Attempts to ping a given host using `requests`.
Returns False if `requests` raises a `ConnectionError` or `Timeout` exception, otherwise returns True
Return Type: bool
"""
try:
requests.get(host, timeout=timeout)
return True
except (requests.ConnectionError, requests.Timeout):
return False

0 comments on commit ecc1dcf

Please sign in to comment.