Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Login interface and experience #371

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions blackboard_sync/qt/LoginWebView.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

import webbrowser
from functools import partial
from threading import Timer
from requests.cookies import RequestsCookieJar

from PyQt6.QtCore import QCoreApplication
from PyQt6.QtCore import pyqtSlot, pyqtSignal, QObject, QUrl
from PyQt6.QtWidgets import QWidget
from PyQt6.QtWidgets import QWidget, QPushButton, QLabel
from PyQt6.QtNetwork import QNetworkCookie
from PyQt6.QtWebEngineCore import QWebEngineCookieStore, QWebEngineProfile
from PyQt6.QtWebEngineWidgets import QWebEngineView

from .assets import load_ui
from .assets import load_ui, get_theme_icon, AppIcon


tr = partial(QCoreApplication.translate, 'LoginWebView')
WATCHDOG_DELAY = 30


class LoginWebView(QWidget):
Expand All @@ -32,28 +40,44 @@ class LoginWebView(QWidget):
class Signals(QObject):
login_complete = pyqtSignal()

def __init__(self) -> None:
def __init__(self, help_url: str) -> None:
super().__init__()

# Typing information
self.web_view: QWebEngineView
self.home_button: QPushButton
self.back_button: QPushButton
self.help_button: QPushButton
self.status: QLabel

self.watchdog: Timer | None

self.start_url: str | None = None
self.target_url: str | None = None

self._cookie_jar = RequestsCookieJar()
self.help_url = help_url

self.signals = self.Signals()

self._init_ui()

def _init_ui(self) -> None:
load_ui(self)

self.home_button.setIcon(get_theme_icon(AppIcon.HOME))
self.back_button.setIcon(get_theme_icon(AppIcon.BACK))
self.help_button.setIcon(get_theme_icon(AppIcon.HELP))
self.help_button.setVisible(False)

self.init_signals()

def init_signals(self) -> None:
profile, cookie_store = self.get_profile_and_cookie_store()
self.web_view.loadFinished.connect(self.slot_load_finished)
self.home_button.clicked.connect(self.home)
self.back_button.clicked.connect(self.web_view.back)
self.help_button.clicked.connect(self.slot_help)

if profile is not None:
profile.clearHttpCacheCompleted.connect(self.slot_cache_cleared)
Expand All @@ -64,12 +88,21 @@ def load(self, start_url: str | None, target_url: str | None) -> None:
self.start_url = start_url
self.target_url = target_url

# start watchdog
self.watchdog = Timer(WATCHDOG_DELAY, self.show_help)
self.watchdog.start()

self.home()

def home(self) -> None:
self.web_view.load(QUrl.fromUserInput(self.start_url))

@pyqtSlot()
def slot_load_finished(self) -> None:
"""Check if we have reached the target url."""
if self.target_url and self.url.startswith(self.target_url):
if self.watchdog:
self.watchdog.cancel()
self.signals.login_complete.emit()

@pyqtSlot(QNetworkCookie)
Expand All @@ -85,7 +118,17 @@ def slot_cookie_added(self, cookie: QNetworkCookie) -> None:

@pyqtSlot()
def slot_cache_cleared(self) -> None:
self.load(self.start_url, self.target_url)
self.home()

@pyqtSlot()
def slot_help(self) -> None:
webbrowser.open(self.help_url)

def show_help(self) -> None:
self.status.setText(tr(
"Trouble logging in? Press the help button to let us know."
))
self.help_button.setVisible(True)

def restore(self) -> None:
"""Restore web view to original state."""
Expand Down Expand Up @@ -116,6 +159,10 @@ def get_profile_and_cookie_store(

return (profile, cookie_store)

def cancel_watchdog(self) -> None:
if self.watchdog:
self.watchdog.cancel()

@property
def url(self) -> str:
"""URL of current website."""
Expand Down
80 changes: 80 additions & 0 deletions blackboard_sync/qt/LoginWebView.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,86 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="status">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string>This window will close once you log in.</string>
</property>
<property name="margin">
<number>4</number>
</property>
<property name="indent">
<number>5</number>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="toolbar">
<item>
<widget class="QToolButton" name="back_button">
<property name="text">
<string>...</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="home_button">
<property name="text">
<string>...</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="spacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="help_button">
<property name="text">
<string>...</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
Expand Down
3 changes: 3 additions & 0 deletions blackboard_sync/qt/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class AppIcon(Enum):
EXIT = QIcon.ThemeIcon.ApplicationExit
OPEN = QIcon.ThemeIcon.FolderOpen
SYNC = QIcon.ThemeIcon.ViewRefresh
HELP = QIcon.ThemeIcon.HelpAbout
HOME = QIcon.ThemeIcon.GoHome
BACK = QIcon.ThemeIcon.EditUndo


def get_theme_icon(icon: AppIcon) -> QIcon:
Expand Down
3 changes: 2 additions & 1 deletion blackboard_sync/qt/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _init_ui(self, universities: list[str],
autodetected: int | None) -> None:
self.setup_window = SetupWizard(self.help_uri,
universities, autodetected)
self.login_window = LoginWebView()
self.login_window = LoginWebView(self.help_uri)
self.config_window = SettingsWindow()
self.tray = SyncTrayIcon()
self.dialogs = Dialogs()
Expand Down Expand Up @@ -176,6 +176,7 @@ def slot_config(self) -> None:

@pyqtSlot()
def slot_quit(self) -> None:
self.login_window.cancel_watchdog()
self.app.quit()

def open_settings(self, download_location: Path,
Expand Down