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
|
|
|
|
|
2017-01-08 10:06:32 +01:00
|
|
|
import requests
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.utils import platDesc, versionWithBuild
|
|
|
|
from aqt.qt import *
|
2020-11-17 08:42:43 +01:00
|
|
|
from aqt.utils import TR, 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)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def __init__(self, main):
|
|
|
|
QThread.__init__(self)
|
|
|
|
self.main = main
|
|
|
|
self.config = main.pm.meta
|
|
|
|
|
|
|
|
def _data(self):
|
2020-05-11 17:09:22 +02:00
|
|
|
return {
|
2019-12-23 01:34:10 +01:00
|
|
|
"ver": versionWithBuild(),
|
|
|
|
"os": platDesc(),
|
|
|
|
"id": self.config["id"],
|
|
|
|
"lm": self.config["lastMsg"],
|
|
|
|
"crt": self.config["created"],
|
|
|
|
}
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def run(self):
|
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:
|
2017-01-08 10:06:32 +01:00
|
|
|
r = requests.post(aqt.appUpdate, data=d)
|
|
|
|
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
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def askAndUpdate(mw, ver):
|
2020-11-17 12:47:47 +01:00
|
|
|
baseStr = tr(TR.QT_MISC_ANKI_UPDATEDANKI_HAS_BEEN_RELEASED, val=ver)
|
2012-12-21 08:51:59 +01:00
|
|
|
msg = QMessageBox(mw)
|
2020-07-26 00:22:05 +02:00
|
|
|
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
msg.setIcon(QMessageBox.Information)
|
2020-11-17 08:42:43 +01:00
|
|
|
msg.setText(baseStr + tr(TR.QT_MISC_WOULD_YOU_LIKE_TO_DOWNLOAD_IT))
|
|
|
|
button = QPushButton(tr(TR.QT_MISC_IGNORE_THIS_UPDATE))
|
2012-12-21 08:51:59 +01:00
|
|
|
msg.addButton(button, QMessageBox.RejectRole)
|
|
|
|
msg.setDefaultButton(QMessageBox.Yes)
|
|
|
|
ret = msg.exec_()
|
|
|
|
if msg.clickedButton() == button:
|
|
|
|
# ignore this update
|
2019-12-23 01:34:10 +01:00
|
|
|
mw.pm.meta["suppressUpdate"] = ver
|
2012-12-21 08:51:59 +01:00
|
|
|
elif ret == QMessageBox.Yes:
|
|
|
|
openLink(aqt.appWebsite)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def showMessages(mw, data):
|
2019-12-23 01:34:10 +01:00
|
|
|
showText(data["msg"], parent=mw, type="html")
|
|
|
|
mw.pm.meta["lastMsg"] = data["msgId"]
|