2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2013-10-20 03:26:11 +02:00
|
|
|
import time
|
2021-10-03 10:59:42 +02:00
|
|
|
from typing import Any
|
2013-10-20 03:26:11 +02:00
|
|
|
|
2017-01-08 10:06:32 +01:00
|
|
|
import requests
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2021-10-25 06:50:13 +02:00
|
|
|
from anki.utils import plat_desc, version_with_build
|
2021-02-01 13:08:56 +01:00
|
|
|
from aqt.main import AnkiQt
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-03-26 05:21:04 +01:00
|
|
|
from aqt.utils import openLink, showText, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class LatestVersionFinder(QThread):
|
2016-05-31 10:51:40 +02:00
|
|
|
newVerAvail = pyqtSignal(str)
|
|
|
|
newMsg = pyqtSignal(dict)
|
|
|
|
clockIsOff = pyqtSignal(float)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def __init__(self, main: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QThread.__init__(self)
|
|
|
|
self.main = main
|
|
|
|
self.config = main.pm.meta
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _data(self) -> dict[str, Any]:
|
2020-05-11 17:09:22 +02:00
|
|
|
return {
|
2021-10-25 06:50:13 +02:00
|
|
|
"ver": version_with_build(),
|
|
|
|
"os": plat_desc(),
|
2019-12-23 01:34:10 +01:00
|
|
|
"id": self.config["id"],
|
|
|
|
"lm": self.config["lastMsg"],
|
|
|
|
"crt": self.config["created"],
|
|
|
|
}
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def run(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
if not self.config["updates"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
d = self._data()
|
2019-12-23 01:34:10 +01:00
|
|
|
d["proto"] = 1
|
2017-01-08 10:06:32 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2022-09-24 01:46:43 +02:00
|
|
|
r = requests.post(aqt.appUpdate, data=d, timeout=60)
|
2017-01-08 10:06:32 +01:00
|
|
|
r.raise_for_status()
|
|
|
|
resp = r.json()
|
2012-12-21 08:51:59 +01:00
|
|
|
except:
|
|
|
|
# behind proxy, corrupt message, etc
|
2016-05-31 10:51:40 +02:00
|
|
|
print("update check failed")
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
if resp["msg"]:
|
2020-07-26 00:22:05 +02:00
|
|
|
self.newMsg.emit(resp) # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
if resp["ver"]:
|
2020-07-26 00:22:05 +02:00
|
|
|
self.newVerAvail.emit(resp["ver"]) # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
diff = resp["time"] - time.time()
|
2012-12-21 08:51:59 +01:00
|
|
|
if abs(diff) > 300:
|
2020-07-26 00:22:05 +02:00
|
|
|
self.clockIsOff.emit(diff) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def askAndUpdate(mw: aqt.AnkiQt, ver: str) -> None:
|
2021-03-26 05:21:04 +01:00
|
|
|
baseStr = tr.qt_misc_anki_updatedanki_has_been_released(val=ver)
|
2012-12-21 08:51:59 +01:00
|
|
|
msg = QMessageBox(mw)
|
2021-10-05 05:53:01 +02:00
|
|
|
msg.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) # type: ignore
|
|
|
|
msg.setIcon(QMessageBox.Icon.Information)
|
2021-03-26 04:48:26 +01:00
|
|
|
msg.setText(baseStr + tr.qt_misc_would_you_like_to_download_it())
|
|
|
|
button = QPushButton(tr.qt_misc_ignore_this_update())
|
2021-10-05 05:53:01 +02:00
|
|
|
msg.addButton(button, QMessageBox.ButtonRole.RejectRole)
|
|
|
|
msg.setDefaultButton(QMessageBox.StandardButton.Yes)
|
2021-10-05 02:01:45 +02:00
|
|
|
ret = msg.exec()
|
2012-12-21 08:51:59 +01:00
|
|
|
if msg.clickedButton() == button:
|
|
|
|
# ignore this update
|
2019-12-23 01:34:10 +01:00
|
|
|
mw.pm.meta["suppressUpdate"] = ver
|
2021-10-05 05:53:01 +02:00
|
|
|
elif ret == QMessageBox.StandardButton.Yes:
|
2022-08-31 10:35:53 +02:00
|
|
|
openLink(aqt.appWebsiteDownloadSection)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def showMessages(mw: aqt.AnkiQt, data: dict) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
showText(data["msg"], parent=mw, type="html")
|
|
|
|
mw.pm.meta["lastMsg"] = data["msgId"]
|