Skip to content

Commit

Permalink
Refactor the New Version Dialog logic
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Oct 24, 2022
1 parent 01704cf commit 68edd08
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/tribler/gui/i18n/pt_BR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ If unsure, press 'No'. You will be able to remove those directories fr
</message>
<message>
<location filename="../tribler_window.py" line="595"/>
<source>Version %s of Tribler is available.Do you want to visit the website to download the newest version?</source>
<source>Version %s of Tribler is available. Do you want to visit the website to download the newest version?</source>
<translation>Versão %s disponível. Quer visitar o site para baixar a versão mais recente?</translation>
</message>
<message>
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/gui/i18n/ru_RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ If unsure, press &apos;No&apos;. You will be able to remove those directories fr
</message>
<message>
<location filename="../tribler_window.py" line="649"/>
<source>Version %s of Tribler is available.Do you want to visit the website to download the newest version?</source>
<source>Version %s of Tribler is available. Do you want to visit the website to download the newest version?</source>
<translation>Доступна новая версия Tribler: %s. Перейти к веб-сайту разработчиков чтобы загрузить её?</translation>
</message>
<message>
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/gui/i18n/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ If unsure, press &apos;No&apos;. You will be able to remove those directories fr
</message>
<message>
<location filename="../tribler_window.py" line="595"/>
<source>Version %s of Tribler is available.Do you want to visit the website to download the newest version?</source>
<source>Version %s of Tribler is available. Do you want to visit the website to download the newest version?</source>
<translation>Tribler 版本 %s 可用。你想要访问网站下载最新版本吗?</translation>
</message>
<message>
Expand Down
35 changes: 1 addition & 34 deletions src/tribler/gui/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
45 changes: 43 additions & 2 deletions src/tribler/gui/upgrade_manager.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 68edd08

Please sign in to comment.