Files
Launcher/pages/account.py
T
wei c3662d4cc6 CoreLib
Update to v0.1.4

Add `is_function_in_file` and if the key is "<ORIGINAL>", the `TranslationManager.translate()` will return it directly. (For some strings that don't need to translate in specific language)

Launcher
Update to 0.1.0a0

Add plugin system support and add some useful apis for plugin.
Add "Refresh avatar cache" to account page.
2026-10-30 00:16:04 +08:00

528 lines
20 KiB
Python

from __future__ import annotations
import webbrowser
from pathlib import Path
from PySide6.QtCore import QSize, Qt, Signal, QTimer
from PySide6.QtGui import QPixmap, QFontDatabase, QIcon
from PySide6.QtWidgets import (
QHBoxLayout,
QLabel,
QListWidget,
QListWidgetItem,
QMenu,
QMessageBox,
QPushButton,
QVBoxLayout,
QWidget, QDialog, QLineEdit, QApplication, QToolButton, QProgressBar,
)
from constant import MICROSOFT_VERIFY_ENDPOINT, CLIENT_ID
from core_lib.qt.hack import move_window_to_center
from manager.account import AccountLoginStatus, AccountSummary, AccountManager
from manager.settings_models import AccountSettings
class AccountWidget(QWidget):
def __init__(self, app, account_id: str, account_name: str, account_head: QPixmap, login_status: str, parent=None,
/, delete_account_callback=None, check_account_callback=None, refresh_token_callback=None,
switch_account_callback=None,):
super().__init__(parent)
self.app = app
self.account_id = account_id
# Layout
self.layout = QHBoxLayout(self)
# Widgets
self.head_icon_label = QLabel()
self.head_icon_label.setObjectName("HeadIcon")
self.head_icon_label.setFixedSize(account_head.size())
self.head_icon_label.setPixmap(account_head)
self.account_name = QLabel(account_name)
self.status_label = QLabel(login_status)
# Bind widgets
self.layout.addWidget(self.head_icon_label)
self.layout.addWidget(self.account_name)
self.layout.addStretch()
self.layout.addWidget(self.status_label)
# Callback
self.delete_account_callback = delete_account_callback
self.check_account_callback = check_account_callback
self.refresh_token_callback = refresh_token_callback
self.switch_account_callback = switch_account_callback
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_right_click_menu)
def show_right_click_menu(self, position):
menu = QMenu(self)
switch_action = menu.addAction(self.app.tr_text("Switch to this account"))
check_action = menu.addAction(self.app.tr_text("Check login status"))
refresh_action = menu.addAction(self.app.tr_text("Refresh token"))
menu.addSeparator()
delete_action = menu.addAction(self.app.tr_text("Delete account"))
selected = menu.exec(self.mapToGlobal(position))
if selected == delete_action and callable(self.delete_account_callback):
self.delete_account_callback()
elif selected == check_action and callable(self.check_account_callback):
self.check_account_callback()
elif selected == refresh_action and callable(self.refresh_token_callback):
self.refresh_token_callback()
elif selected == switch_action and callable(self.switch_account_callback):
self.switch_account_callback()
class LoginDialog(QDialog):
def __init__(self, app, parent=None, code="", error_message=""):
super().__init__(parent)
self.app = app
self.setWindowTitle(self.app.tr_text("Microsoft Login"))
self.setMinimumWidth(440)
self.setMinimumHeight(380)
self.resize(440, 420)
move_window_to_center(self)
# Layout
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(24, 22, 24, 22)
self.layout.setSpacing(14)
# Top widgets
self.title_label = QLabel(self.app.tr_text("Login Minecraft Account"))
self.title_label.setObjectName("dialogTitle")
self.help_label = QLabel(self.app.tr_text("Copy the code below, then continue in your browser."))
self.help_label.setObjectName("helpText")
self.help_label.setWordWrap(True)
# Center
self.code_edit = QLineEdit()
self.code_edit.setObjectName("codeEdit")
self.code_edit.setReadOnly(True)
self.code_edit.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.code_edit.setFont(QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont))
self.code_edit.setText(code)
self.code_label = self.code_edit
self.error_label = QLabel()
self.error_label.setObjectName("errorMessage")
self.error_label.setWordWrap(True)
self.error_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
# Progress label
self.progress_label = QLabel(
self.app.tr_text("Preparing Microsoft sign-in…")
)
self.progress_label.setObjectName("progressText")
self.progress_label.setWordWrap(True)
# Button
self.copy_code_button = QPushButton(self.app.tr_text("Copy code"))
self.copy_code_button.setObjectName("copyCodeButton")
self.open_browser_button = QPushButton(self.app.tr_text("Open browser"))
self.open_browser_button.setObjectName("openBrowserButton")
self.center_layout = QVBoxLayout()
self.center_layout.setSpacing(10)
self.center_layout.addWidget(self.progress_label)
self.center_layout.addWidget(self.code_edit)
self.center_layout.addWidget(self.error_label)
self.button_layout = QHBoxLayout()
self.button_layout.setSpacing(8)
self.button_layout.addWidget(self.copy_code_button)
self.button_layout.addWidget(self.open_browser_button)
self.layout.addWidget(self.title_label)
self.layout.addWidget(self.help_label)
self.layout.addStretch()
self.layout.addLayout(self.center_layout)
self.layout.addStretch()
self.layout.addLayout(self.button_layout)
self.set_error_message(error_message)
self.copy_code_button.clicked.connect(self.copy_code)
self.app.apply_qss(Path("login_dialog.qss"), self.setStyleSheet)
@property
def code(self):
return self.code_edit.text()
def set_code(self, code):
self.code_edit.setText(str(code or ""))
def set_progress(self, value, message, busy=False):
self.progress_label.setText(f"{message} {f"{f"({value}%)" if not busy else ""}"}")
def copy_code(self):
QApplication.clipboard().setText(self.code)
self.copy_code_button.setText(self.app.tr_text("Copied!"))
self.copy_code_button.setEnabled(False)
QTimer.singleShot(1200, self._reset_copy_button)
def _reset_copy_button(self):
self.copy_code_button.setText(self.app.tr_text("Copy code"))
self.copy_code_button.setEnabled(True)
def set_error_message(self, message):
message = str(message or "").strip()
self.error_label.setText(message)
self.error_label.setVisible(bool(message))
class AccountPage(QWidget):
account_authenticated = Signal(dict)
def __init__(self, app, parent=None):
super().__init__(parent)
self.app = app
self.widget_manager = app.widget_manager
self.account_manager: AccountManager = app.account_manager
self.login_dialog = None
# Layout
main_layout = QVBoxLayout()
right_layout = QVBoxLayout()
layout = QHBoxLayout(self)
# Widgets
self.account_list = QListWidget(self)
self.add_account_button = QPushButton(self.app.tr_text("Add Account"))
self.refresh_button = QPushButton(self.app.tr_text("Refresh all"))
self.reload_button = QPushButton(self.app.tr_text("Reload accounts"))
self.delete_all_button = QPushButton(self.app.tr_text("Delete All"))
self.refresh_avatar_cache_button = QPushButton(self.app.tr_text("Refresh avatar cache"))
# Button click handle
self.add_account_button.clicked.connect(self.start_login_page)
self.refresh_button.clicked.connect(self.refresh_accounts)
self.reload_button.clicked.connect(self.reload_accounts)
self.delete_all_button.clicked.connect(self.account_manager.delete_all_accounts)
self.refresh_avatar_cache_button.clicked.connect(self.refresh_accounts_head)
# Size
self.avatar_size = QSize(32, 32)
# Bind widget
main_layout.addWidget(self.account_list)
right_layout.addWidget(self.add_account_button)
right_layout.addWidget(self.refresh_button)
right_layout.addWidget(self.reload_button)
right_layout.addWidget(self.delete_all_button)
right_layout.addWidget(self.refresh_avatar_cache_button)
right_layout.addStretch()
layout.addLayout(main_layout, 1)
layout.addLayout(right_layout)
self.related_button : QToolButton = None
self.account_manager.accounts_changed.connect(self._render_accounts)
self.account_manager.current_account_changed.connect(self._current_account_changed)
self.account_manager.login_signal.device_code_ready.connect(self._show_device_code)
self.account_manager.login_signal.login_succeeded.connect(self._login_succeeded)
self.account_manager.login_signal.login_failed.connect(self._login_failed)
self.account_manager.login_signal.finished.connect(self._login_finished)
self.account_manager.login_signal.progress_changed.connect(self._login_progress_changed)
self.account_manager.head_signal.updated.connect(self._account_head_updated)
self.account_manager.login_status_checked.connect(self._login_status_checked)
self.account_manager.login_status_check_started.connect(self._log_status_check_started)
self.account_manager.token_refresh_started.connect(self._token_refresh_started)
self.account_manager.token_refreshed.connect(self._token_refreshed)
self.account_manager.token_refresh_failed.connect(self._token_refresh_failed)
self.account_manager.token_refresh_queue_finished.connect(self._token_refresh_queue_finished)
self._render_accounts()
def set_related_button(self, button: QToolButton):
self.related_button = button
self._update_related_button(None)
def _login_progress_changed(self, value: int, message: str, busy: bool):
if self.login_dialog is not None:
self.login_dialog.set_progress(value, message, busy)
def _find_account_widget(self, account_id: str):
for row in range(self.account_list.count()):
item = self.account_list.item(row)
widget = self.account_list.itemWidget(item)
if isinstance(widget, AccountWidget) and widget.account_id == account_id:
return widget
return None
def _update_related_button(self, account_id: str | None):
"""
Update the related button's icon to the current (active) account avatar
:param account_id:
:return:
"""
if self.related_button is None:
return
if account_id is None:
account_id = self.account_manager.get_current_account_id()
if not self.account_manager.is_current_account(account_id):
return
account = self.account_manager.get_account(account_id)
if account is None:
self.related_button.setIcon(
self.app.get_icon(Path(self.app.icon_dir, "head.png"), True)
)
return
pixmap = self.account_manager.get_head_pixmap(
account.avatar,
QSize(32, 32),
)
# Apply avatar to open account page button
self.related_button.setIcon(QIcon(pixmap))
def _render_accounts(self):
self.account_list.clear()
current_id = self.account_manager.get_current_account_id()
accounts = self.account_manager.list_accounts()
# Add suggestion to account list if no account available
if not accounts:
empty_item = QListWidgetItem(self.app.tr_text("No accounts yet. Use Add Account to sign in."))
empty_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
empty_item.setFlags(Qt.ItemFlag.NoItemFlags)
self.account_list.addItem(empty_item)
for account in accounts:
self._add_account_item(account, account.id == current_id)
def _add_account_item(self, account: AccountSummary, is_current: bool):
list_item = QListWidgetItem()
list_item.setData(Qt.ItemDataRole.UserRole, account.id)
widget = AccountWidget(
self.app,
account.id,
account.display_name,
self.account_manager.get_head_pixmap(account.avatar, self.avatar_size),
self.app.tr_text("Current" if is_current else "Available"),
self,
delete_account_callback=lambda: self._delete_account(account.id),
check_account_callback=lambda: self.account_manager.check_login_status_requested.emit(account.id),
refresh_token_callback=lambda: self.account_manager.refresh_account_token(account.id),
switch_account_callback=lambda: self._switch_account(account.id),
)
list_item.setSizeHint(widget.sizeHint())
self.account_list.addItem(list_item)
self.account_list.setItemWidget(list_item, widget)
if not self.account_manager.has_saved_head(account.avatar):
self._update_account_head(account.id, account.minecraft_name or account.username)
def refresh_accounts_head(self):
for row in range(self.account_list.count()):
item = self.account_list.item(row)
widget = self.account_list.itemWidget(item)
if isinstance(widget, AccountWidget):
related_account = self.account_manager.get_account(widget.account_id)
if not related_account:
continue
self._update_account_head(
related_account.id,
related_account.minecraft_name or related_account.username,
force=True,
)
def _update_account_head(self, account_id: str, username: str, *, force: bool = False):
self.account_manager.request_account_head(
account_id,
username,
max(self.avatar_size.width(), self.avatar_size.height()),
force=force,
)
def _account_head_updated(self, account_id: str):
account = self.account_manager.get_account(account_id)
if account is None:
return
for row in range(self.account_list.count()):
item = self.account_list.item(row)
if item.data(Qt.ItemDataRole.UserRole) != account_id:
continue
widget = self.account_list.itemWidget(item)
if isinstance(widget, AccountWidget):
pixmap = self.account_manager.get_head_pixmap(account.avatar, QSize(32, 32))
widget.head_icon_label.setPixmap(
pixmap,
)
break
self._update_related_button(account_id)
def _delete_account(self, account_id: str):
if self.account_manager.remove_account(account_id):
self.account_manager.save()
def _switch_account(self, account_id: str):
self.account_manager.set_current_account(account_id)
self.account_manager.save()
def _current_account_changed(self, _account_id):
self._render_accounts()
def refresh_accounts(self):
self.account_manager.refresh_all_tokens()
def reload_accounts(self):
self.account_manager.reload()
def _token_refresh_started(self, account_id: str):
self.refresh_button.setEnabled(False)
widget = self._find_account_widget(account_id)
if widget is not None:
widget.status_label.setText(self.app.tr_text("Refreshing…"))
def _token_refreshed(self, account_id: str):
widget = self._find_account_widget(account_id)
if widget is not None:
widget.status_label.setText(self.app.tr_text("Signed in"))
def _token_refresh_failed(self, account_id: str, _message: str):
widget = self._find_account_widget(account_id)
if widget is not None:
widget.status_label.setText(self.app.tr_text("Refresh failed"))
def _token_refresh_queue_finished(self):
self.refresh_button.setEnabled(True)
# Microsoft sign-in
def start_login_page(self):
# Active existing login dialog
if self.login_dialog is not None:
self.app.logger.info("Destroying existing login dialog...")
self.login_dialog.close()
self.login_dialog.deleteLater()
# Create login dialog
self.add_account_button.setEnabled(False)
self.login_dialog = LoginDialog(parent=self, app=self.app)
self.login_dialog.help_label.setText(self.app.tr_text("Requesting a Microsoft sign-in code…"))
self.login_dialog.copy_code_button.setEnabled(False)
self.login_dialog.open_browser_button.setEnabled(False)
self.login_dialog.open_browser_button.clicked.connect(self.open_sign_in_page)
self.login_dialog.rejected.connect(self.account_manager.cancel_login)
self.login_dialog.show()
self.account_manager.start_login()
def _show_device_code(self, device: dict):
self._verification_uri = device.get("verification_uri", MICROSOFT_VERIFY_ENDPOINT)
if self.login_dialog is not None:
self.login_dialog.help_label.setText(
device.get("message") or self.app.tr_text("Copy the code below, then continue in your browser.")
)
self.login_dialog.set_code(device.get("user_code", ""))
self.login_dialog.copy_code_button.setEnabled(True)
self.login_dialog.open_browser_button.setEnabled(True)
self.open_sign_in_page()
def open_sign_in_page(self):
webbrowser.open(self._verification_uri)
def _login_succeeded(self, account: dict):
created_id = self.account_manager.login_succeeded(account)
# Cleanup
if self.login_dialog is not None:
dialog = self.login_dialog
self.login_dialog = None
dialog.accept()
dialog.deleteLater()
if created_id is not None:
self.account_authenticated.emit({"account_id": created_id})
account = self.account_manager.get_account(created_id)
if not account:
return
# Update head image
self._update_account_head(created_id, account.username)
def _login_failed(self, message: str):
if self.login_dialog is not None:
self.login_dialog.set_error_message(message)
self.login_dialog.help_label.setText(self.app.tr_text("Microsoft sign-in could not be completed."))
self.login_dialog.copy_code_button.setEnabled(False)
self.login_dialog.open_browser_button.setEnabled(False)
else:
QMessageBox.critical(self, self.app.tr_text("Microsoft sign-in failed"), message)
def _login_finished(self):
self.add_account_button.setEnabled(True)
def _log_status_check_started(self, account_id: str):
widget = self._find_account_widget(account_id)
if widget is not None:
widget.status_label.setText(
self.app.tr_text("Checking…")
)
def _login_status_checked(self, account_id: str, status: AccountLoginStatus):
account = self.account_manager.get_account(
account_id
)
if account is None:
return
widget = self._find_account_widget(account_id)
if widget is not None:
if not status.success:
widget.status_label.setText(
self.app.tr_text("Check failed")
)
elif status.access_token_is_valid:
widget.status_label.setText(
self.app.tr_text("Signed in")
)
else:
widget.status_label.setText(
self.app.tr_text("Expired")
)
if not status.success:
message = self.app.tr_text(
"Unable to check login status: {error}",
error=status.error or "Unknown error",
)
elif status.access_token_is_valid:
message = self.app.tr_text(
"{account} is currently signed in.",
account=account.display_name,
)
else:
message = self.app.tr_text(
"{account}'s session has expired.",
account=account.display_name,
)
QMessageBox.information(
self,
self.app.tr_text("Account status"),
message,
)