From 68edd087de3fdc009b5de19a4a29fa8d8aac4bdd Mon Sep 17 00:00:00 2001 From: drew2a Date: Mon, 24 Oct 2022 10:21:33 +0200 Subject: [PATCH] Refactor the New Version Dialog logic --- src/tribler/gui/i18n/pt_BR.ts | 2 +- src/tribler/gui/i18n/ru_RU.ts | 2 +- src/tribler/gui/i18n/zh_CN.ts | 2 +- src/tribler/gui/tribler_window.py | 35 +---------------------- src/tribler/gui/upgrade_manager.py | 45 ++++++++++++++++++++++++++++-- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/tribler/gui/i18n/pt_BR.ts b/src/tribler/gui/i18n/pt_BR.ts index 862a82f43b7..714215aacf7 100644 --- a/src/tribler/gui/i18n/pt_BR.ts +++ b/src/tribler/gui/i18n/pt_BR.ts @@ -644,7 +644,7 @@ If unsure, press 'No'. You will be able to remove those directories fr - Version %s of Tribler is available.Do you want to visit the website to download the newest version? + Version %s of Tribler is available. Do you want to visit the website to download the newest version? Versão %s disponível. Quer visitar o site para baixar a versão mais recente? diff --git a/src/tribler/gui/i18n/ru_RU.ts b/src/tribler/gui/i18n/ru_RU.ts index d8b717d1c78..2ac7e9baee4 100644 --- a/src/tribler/gui/i18n/ru_RU.ts +++ b/src/tribler/gui/i18n/ru_RU.ts @@ -632,7 +632,7 @@ If unsure, press 'No'. You will be able to remove those directories fr - Version %s of Tribler is available.Do you want to visit the website to download the newest version? + Version %s of Tribler is available. Do you want to visit the website to download the newest version? Доступна новая версия Tribler: %s. Перейти к веб-сайту разработчиков чтобы загрузить её? diff --git a/src/tribler/gui/i18n/zh_CN.ts b/src/tribler/gui/i18n/zh_CN.ts index c16589af9b6..09f6636accb 100644 --- a/src/tribler/gui/i18n/zh_CN.ts +++ b/src/tribler/gui/i18n/zh_CN.ts @@ -638,7 +638,7 @@ If unsure, press 'No'. You will be able to remove those directories fr - Version %s of Tribler is available.Do you want to visit the website to download the newest version? + Version %s of Tribler is available. Do you want to visit the website to download the newest version? Tribler 版本 %s 可用。你想要访问网站下载最新版本吗? diff --git a/src/tribler/gui/tribler_window.py b/src/tribler/gui/tribler_window.py index 502e825a767..373325da95b 100644 --- a/src/tribler/gui/tribler_window.py +++ b/src/tribler/gui/tribler_window.py @@ -206,7 +206,6 @@ def __init__( self.dialog = None self.create_dialog = None self.chosen_dir = None - self.new_version_dialog = None self.new_version_dialog_postponed = False self.start_download_dialog_active = False self.selected_torrent_files = [] @@ -731,39 +730,7 @@ def on_add_button_pressed(channel_id): self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent") def on_new_version_available(self, version): - if version == str(self.gui_settings.value('last_reported_version')): - return - if self.new_version_dialog_postponed: - return - - # To prevent multiple dialogs on top of each other, - # close any existing dialog first. - if self.new_version_dialog: - self.new_version_dialog.close_dialog() - self.new_version_dialog = None - - self.new_version_dialog = ConfirmationDialog( - self, - tr("New version available"), - tr("Version %s of Tribler is available.Do you want to visit the " "website to download the newest version?") - % version, - [(tr("IGNORE"), BUTTON_TYPE_NORMAL), (tr("LATER"), BUTTON_TYPE_NORMAL), (tr("OK"), BUTTON_TYPE_NORMAL)], - ) - connect(self.new_version_dialog.button_clicked, lambda action: self.on_new_version_dialog_done(version, action)) - self.new_version_dialog.show() - - def on_new_version_dialog_done(self, version, action): - if action == 0: # ignore - self.gui_settings.setValue("last_reported_version", version) - elif action == 1: # postpone - self.new_version_dialog_postponed = True - elif action == 2: # ok - import webbrowser - - webbrowser.open("https://tribler.org") - if self.new_version_dialog: - self.new_version_dialog.close_dialog() - self.new_version_dialog = None + self.upgrade_manager.on_new_version_available(tribler_window=self, new_version=version) def on_search_text_change(self, text): # We do not want to bother the database on petty 1-character queries diff --git a/src/tribler/gui/upgrade_manager.py b/src/tribler/gui/upgrade_manager.py index f2b47606305..af824d9aa4a 100644 --- a/src/tribler/gui/upgrade_manager.py +++ b/src/tribler/gui/upgrade_manager.py @@ -1,14 +1,22 @@ +from __future__ import annotations + import logging -from typing import List +import webbrowser +from typing import List, Optional, TYPE_CHECKING from PyQt5.QtCore import QObject, QThread, pyqtSignal from PyQt5.QtWidgets import QMessageBox from tribler.core.upgrade.version_manager import TriblerVersion, VersionHistory +from tribler.gui.defs import BUTTON_TYPE_NORMAL +from tribler.gui.dialogs.confirmationdialog import ConfirmationDialog from tribler.gui.exceptions import UpgradeError from tribler.gui.utilities import connect, format_size, tr from tribler.run_tribler_upgrader import upgrade_state_dir +if TYPE_CHECKING: + from tribler.gui.tribler_window import TriblerWindow + class StateDirUpgradeWorker(QObject): finished = pyqtSignal(object) @@ -55,12 +63,45 @@ class UpgradeManager(QObject): def __init__(self, version_history: VersionHistory): QObject.__init__(self, None) - self.version_history = version_history self._logger = logging.getLogger(self.__class__.__name__) + self.version_history = version_history + self.new_version_dialog_postponed: bool = False + self.dialog: Optional[ConfirmationDialog] = None + self._upgrade_worker = None self._upgrade_thread = None + def on_new_version_available(self, tribler_window: TriblerWindow, new_version: str): + last_reported_version = str(tribler_window.gui_settings.value('last_reported_version')) + if new_version == last_reported_version: + return + + if self.new_version_dialog_postponed or self.dialog: + return + + self.dialog = ConfirmationDialog( + tribler_window, + tr("New version available"), + tr("Version %s of Tribler is available. Do you want to visit the website to download the newest version?") + % new_version, + [(tr("IGNORE"), BUTTON_TYPE_NORMAL), (tr("LATER"), BUTTON_TYPE_NORMAL), (tr("OK"), BUTTON_TYPE_NORMAL)], + ) + + def on_button_clicked(click_result: int): + self.dialog.close_dialog() + self.dialog = None + + if click_result == 0: # ignore + tribler_window.gui_settings.setValue("last_reported_version", new_version) + elif click_result == 1: # later + self.new_version_dialog_postponed = True + elif click_result == 2: # ok + webbrowser.open("https://tribler.org") + + connect(self.dialog.button_clicked, on_button_clicked) + self.dialog.show() + def _show_question_box(self, title, body, additional_text, default_button=None): message_box = QMessageBox() message_box.setIcon(QMessageBox.Question)