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-10 04:48:38 +02:00
|
|
|
import gc
|
2019-12-20 10:19:03 +01:00
|
|
|
import time
|
2013-10-10 04:48:38 +02:00
|
|
|
|
2020-01-15 04:49:26 +01:00
|
|
|
from anki import hooks
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.storage import Collection
|
2020-02-04 02:48:51 +01:00
|
|
|
from anki.sync import FullSyncer, RemoteServer, Syncer
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
|
|
|
from aqt.utils import askUserDialog, showInfo, showText, showWarning, tooltip
|
2013-10-10 04:48:38 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Sync manager
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class SyncManager(QObject):
|
2012-12-21 08:51:59 +01:00
|
|
|
def __init__(self, mw, pm):
|
|
|
|
QObject.__init__(self, mw)
|
|
|
|
self.mw = mw
|
|
|
|
self.pm = pm
|
|
|
|
|
|
|
|
def sync(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
if not self.pm.profile["syncKey"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
auth = self._getUserPass()
|
|
|
|
if not auth:
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
self.pm.profile["syncUser"] = auth[0]
|
2012-12-21 08:51:59 +01:00
|
|
|
self._sync(auth)
|
|
|
|
else:
|
|
|
|
self._sync()
|
|
|
|
|
|
|
|
def _sync(self, auth=None):
|
|
|
|
# to avoid gui widgets being garbage collected in the worker thread,
|
|
|
|
# run gc in advance
|
2013-02-27 06:07:11 +01:00
|
|
|
self._didFullUp = False
|
2013-02-28 17:36:14 +01:00
|
|
|
self._didError = False
|
2012-12-21 08:51:59 +01:00
|
|
|
gc.collect()
|
|
|
|
# create the thread, setup signals and start running
|
|
|
|
t = self.thread = SyncThread(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.pm.collectionPath(),
|
|
|
|
self.pm.profile["syncKey"],
|
|
|
|
auth=auth,
|
2018-07-28 10:08:16 +02:00
|
|
|
hostNum=self.pm.profile.get("hostNum"),
|
|
|
|
)
|
2019-12-15 23:51:38 +01:00
|
|
|
t._event.connect(self.onEvent)
|
2020-01-18 08:49:59 +01:00
|
|
|
t.progress_event.connect(self.on_progress)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.label = _("Connecting...")
|
2017-12-28 09:31:05 +01:00
|
|
|
prog = self.mw.progress.start(immediate=True, label=self.label)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.sentBytes = self.recvBytes = 0
|
|
|
|
self._updateLabel()
|
|
|
|
self.thread.start()
|
|
|
|
while not self.thread.isFinished():
|
2017-12-28 09:31:05 +01:00
|
|
|
if prog.wantCancel:
|
2017-01-17 08:15:50 +01:00
|
|
|
self.thread.flagAbort()
|
|
|
|
# make sure we don't display 'upload success' msg
|
|
|
|
self._didFullUp = False
|
|
|
|
# abort may take a while
|
|
|
|
self.mw.progress.update(_("Stopping..."))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.app.processEvents()
|
|
|
|
self.thread.wait(100)
|
|
|
|
self.mw.progress.finish()
|
2013-10-03 23:09:28 +02:00
|
|
|
if self.thread.syncMsg:
|
|
|
|
showText(self.thread.syncMsg)
|
|
|
|
if self.thread.uname:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.pm.profile["syncUser"] = self.thread.uname
|
|
|
|
self.pm.profile["hostNum"] = self.thread.hostNum
|
|
|
|
|
2013-02-28 17:36:14 +01:00
|
|
|
def delayedInfo():
|
|
|
|
if self._didFullUp and not self._didError:
|
2019-12-23 01:34:10 +01:00
|
|
|
showInfo(
|
|
|
|
_(
|
|
|
|
"""\
|
2013-02-27 06:07:11 +01:00
|
|
|
Your collection was successfully uploaded to AnkiWeb.
|
|
|
|
|
|
|
|
If you use any other devices, please sync them now, and choose \
|
|
|
|
to download the collection you have just uploaded from this computer. \
|
|
|
|
After doing so, future reviews and added cards will be merged \
|
2019-12-23 01:34:10 +01:00
|
|
|
automatically."""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-04-21 10:56:30 +02:00
|
|
|
self.mw.progress.timer(1000, delayedInfo, False, requiresCollection=False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _updateLabel(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw.progress.update(
|
|
|
|
label="%s\n%s"
|
|
|
|
% (
|
|
|
|
self.label,
|
|
|
|
_("%(a)0.1fkB up, %(b)0.1fkB down")
|
|
|
|
% dict(a=self.sentBytes / 1024, b=self.recvBytes / 1024),
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-18 08:49:59 +01:00
|
|
|
def on_progress(self, upload: int, download: int) -> None:
|
|
|
|
# posted events not guaranteed to arrive in order; don't go backwards
|
|
|
|
self.sentBytes = max(self.sentBytes, upload)
|
|
|
|
self.recvBytes = max(self.recvBytes, download)
|
|
|
|
self._updateLabel()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onEvent(self, evt, *args):
|
|
|
|
pu = self.mw.progress.update
|
|
|
|
if evt == "badAuth":
|
|
|
|
tooltip(
|
|
|
|
_("AnkiWeb ID or password was incorrect; please try again."),
|
2019-12-23 01:34:10 +01:00
|
|
|
parent=self.mw,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# blank the key so we prompt user again
|
2019-12-23 01:34:10 +01:00
|
|
|
self.pm.profile["syncKey"] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pm.save()
|
|
|
|
elif evt == "corrupt":
|
|
|
|
pass
|
|
|
|
elif evt == "newKey":
|
2019-12-23 01:34:10 +01:00
|
|
|
self.pm.profile["syncKey"] = args[0]
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pm.save()
|
|
|
|
elif evt == "offline":
|
|
|
|
tooltip(_("Syncing failed; internet offline."))
|
2013-05-13 11:48:37 +02:00
|
|
|
elif evt == "upbad":
|
|
|
|
self._didFullUp = False
|
2013-05-14 08:41:10 +02:00
|
|
|
self._checkFailed()
|
2012-12-21 08:51:59 +01:00
|
|
|
elif evt == "sync":
|
2019-12-23 01:34:10 +01:00
|
|
|
m = None
|
|
|
|
t = args[0]
|
2012-12-21 08:51:59 +01:00
|
|
|
if t == "login":
|
|
|
|
m = _("Syncing...")
|
|
|
|
elif t == "upload":
|
2013-02-27 06:07:11 +01:00
|
|
|
self._didFullUp = True
|
2012-12-21 08:51:59 +01:00
|
|
|
m = _("Uploading to AnkiWeb...")
|
|
|
|
elif t == "download":
|
|
|
|
m = _("Downloading from AnkiWeb...")
|
|
|
|
elif t == "sanity":
|
|
|
|
m = _("Checking...")
|
|
|
|
elif t == "upgradeRequired":
|
2019-12-23 01:34:10 +01:00
|
|
|
showText(
|
|
|
|
_(
|
|
|
|
"""\
|
|
|
|
Please visit AnkiWeb, upgrade your deck, then try again."""
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if m:
|
|
|
|
self.label = m
|
|
|
|
self._updateLabel()
|
2014-07-28 10:00:26 +02:00
|
|
|
elif evt == "syncMsg":
|
|
|
|
self.label = args[0]
|
|
|
|
self._updateLabel()
|
2012-12-21 08:51:59 +01:00
|
|
|
elif evt == "error":
|
2013-02-28 17:36:14 +01:00
|
|
|
self._didError = True
|
2019-12-23 01:34:10 +01:00
|
|
|
showText(_("Syncing failed:\n%s") % self._rewriteError(args[0]))
|
2012-12-21 08:51:59 +01:00
|
|
|
elif evt == "clockOff":
|
|
|
|
self._clockOff()
|
2013-05-14 08:41:10 +02:00
|
|
|
elif evt == "checkFailed":
|
|
|
|
self._checkFailed()
|
2012-12-21 08:51:59 +01:00
|
|
|
elif evt == "noChanges":
|
|
|
|
pass
|
|
|
|
elif evt == "fullSync":
|
|
|
|
self._confirmFullSync()
|
2018-09-05 11:53:41 +02:00
|
|
|
elif evt == "downloadClobber":
|
2019-12-23 01:34:10 +01:00
|
|
|
showInfo(
|
|
|
|
_(
|
|
|
|
"Your AnkiWeb collection does not contain any cards. Please sync again and choose 'Upload' instead."
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _rewriteError(self, err):
|
|
|
|
if "Errno 61" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
Couldn't connect to AnkiWeb. Please check your network connection \
|
2019-12-23 01:34:10 +01:00
|
|
|
and try again."""
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif "timed out" in err or "10060" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
The connection to AnkiWeb timed out. Please check your network \
|
2019-12-23 01:34:10 +01:00
|
|
|
connection and try again."""
|
|
|
|
)
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 500" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
AnkiWeb encountered an error. Please try again in a few minutes, and if \
|
2019-12-23 01:34:10 +01:00
|
|
|
the problem persists, please file a bug report."""
|
|
|
|
)
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 501" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"""\
|
|
|
|
Please upgrade to the latest version of Anki."""
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# 502 is technically due to the server restarting, but we reuse the
|
|
|
|
# error message
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 502" in err:
|
2013-05-17 06:58:38 +02:00
|
|
|
return _("AnkiWeb is under maintenance. Please try again in a few minutes.")
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 503" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"""\
|
|
|
|
AnkiWeb is too busy at the moment. Please try again in a few minutes."""
|
|
|
|
)
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 504" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"504 gateway timeout error received. Please try temporarily disabling your antivirus."
|
|
|
|
)
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 409" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"Only one client can access AnkiWeb at a time. If a previous sync failed, please try again in a few minutes."
|
|
|
|
)
|
2013-11-27 15:02:03 +01:00
|
|
|
elif "10061" in err or "10013" in err or "10053" in err:
|
2012-12-21 08:51:59 +01:00
|
|
|
return _(
|
2019-12-23 01:34:10 +01:00
|
|
|
"Antivirus or firewall software is preventing Anki from connecting to the internet."
|
|
|
|
)
|
2014-01-23 19:10:04 +01:00
|
|
|
elif "10054" in err or "Broken pipe" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return _(
|
|
|
|
"Connection timed out. Either your internet connection is experiencing problems, or you have a very large file in your media folder."
|
|
|
|
)
|
2018-05-31 05:24:49 +02:00
|
|
|
elif "Unable to find the server" in err or "socket.gaierror" in err:
|
2013-02-11 18:06:03 +01:00
|
|
|
return _(
|
|
|
|
"Server not found. Either your connection is down, or antivirus/firewall "
|
2019-12-23 01:34:10 +01:00
|
|
|
"software is blocking Anki from connecting to the internet."
|
|
|
|
)
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 407" in err:
|
2012-12-21 08:51:59 +01:00
|
|
|
return _("Proxy authentication required.")
|
2013-10-14 04:18:52 +02:00
|
|
|
elif "code: 413" in err:
|
|
|
|
return _("Your collection or a media file is too large to sync.")
|
2014-02-05 22:50:41 +01:00
|
|
|
elif "EOF occurred in violation of protocol" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return (
|
|
|
|
_(
|
|
|
|
"Error establishing a secure connection. This is usually caused by antivirus, firewall or VPN software, or problems with your ISP."
|
|
|
|
)
|
|
|
|
+ " (eof)"
|
|
|
|
)
|
2014-09-01 14:06:41 +02:00
|
|
|
elif "certificate verify failed" in err:
|
2019-12-23 01:34:10 +01:00
|
|
|
return (
|
|
|
|
_(
|
|
|
|
"Error establishing a secure connection. This is usually caused by antivirus, firewall or VPN software, or problems with your ISP."
|
|
|
|
)
|
|
|
|
+ " (invalid cert)"
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
return err
|
|
|
|
|
|
|
|
def _getUserPass(self):
|
|
|
|
d = QDialog(self.mw)
|
|
|
|
d.setWindowTitle("Anki")
|
2013-01-08 02:36:04 +01:00
|
|
|
d.setWindowModality(Qt.WindowModal)
|
2012-12-21 08:51:59 +01:00
|
|
|
vbox = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
l = QLabel(
|
|
|
|
_(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
<h1>Account Required</h1>
|
|
|
|
A free account is required to keep your collection synchronized. Please \
|
|
|
|
<a href="%s">sign up</a> for an account, then \
|
2019-12-23 01:34:10 +01:00
|
|
|
enter your details below."""
|
|
|
|
)
|
|
|
|
% "https://ankiweb.net/account/login"
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.setOpenExternalLinks(True)
|
|
|
|
l.setWordWrap(True)
|
|
|
|
vbox.addWidget(l)
|
|
|
|
vbox.addSpacing(20)
|
|
|
|
g = QGridLayout()
|
|
|
|
l1 = QLabel(_("AnkiWeb ID:"))
|
|
|
|
g.addWidget(l1, 0, 0)
|
|
|
|
user = QLineEdit()
|
|
|
|
g.addWidget(user, 0, 1)
|
|
|
|
l2 = QLabel(_("Password:"))
|
|
|
|
g.addWidget(l2, 1, 0)
|
|
|
|
passwd = QLineEdit()
|
|
|
|
passwd.setEchoMode(QLineEdit.Password)
|
|
|
|
g.addWidget(passwd, 1, 1)
|
|
|
|
vbox.addLayout(g)
|
2019-12-23 01:34:10 +01:00
|
|
|
bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
2012-12-21 08:51:59 +01:00
|
|
|
bb.button(QDialogButtonBox.Ok).setAutoDefault(True)
|
2016-05-31 10:51:40 +02:00
|
|
|
bb.accepted.connect(d.accept)
|
|
|
|
bb.rejected.connect(d.reject)
|
2012-12-21 08:51:59 +01:00
|
|
|
vbox.addWidget(bb)
|
|
|
|
d.setLayout(vbox)
|
|
|
|
d.show()
|
2014-06-24 22:04:23 +02:00
|
|
|
accepted = d.exec_()
|
2012-12-21 08:51:59 +01:00
|
|
|
u = user.text()
|
|
|
|
p = passwd.text()
|
2014-06-24 22:04:23 +02:00
|
|
|
if not accepted or not u or not p:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
return (u, p)
|
|
|
|
|
|
|
|
def _confirmFullSync(self):
|
2018-05-01 06:34:03 +02:00
|
|
|
self.mw.progress.finish()
|
2017-01-08 13:21:58 +01:00
|
|
|
if self.thread.localIsEmpty:
|
|
|
|
diag = askUserDialog(
|
|
|
|
_("Local collection has no cards. Download from AnkiWeb?"),
|
2019-12-23 01:34:10 +01:00
|
|
|
[_("Download from AnkiWeb"), _("Cancel")],
|
|
|
|
)
|
2017-01-08 13:21:58 +01:00
|
|
|
diag.setDefault(1)
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
diag = askUserDialog(
|
|
|
|
_(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
Your decks here and on AnkiWeb differ in such a way that they can't \
|
|
|
|
be merged together, so it's necessary to overwrite the decks on one \
|
|
|
|
side with the decks from the other.
|
|
|
|
|
2013-02-27 06:07:11 +01:00
|
|
|
If you choose download, Anki will download the collection from AnkiWeb, \
|
|
|
|
and any changes you have made on your computer since the last sync will \
|
|
|
|
be lost.
|
|
|
|
|
|
|
|
If you choose upload, Anki will upload your collection to AnkiWeb, and \
|
|
|
|
any changes you have made on AnkiWeb or your other devices since the \
|
|
|
|
last sync to this device will be lost.
|
|
|
|
|
2013-02-27 11:36:15 +01:00
|
|
|
After all devices are in sync, future reviews and added cards can be merged \
|
2019-12-23 01:34:10 +01:00
|
|
|
automatically."""
|
|
|
|
),
|
|
|
|
[_("Upload to AnkiWeb"), _("Download from AnkiWeb"), _("Cancel")],
|
|
|
|
)
|
2017-01-08 13:21:58 +01:00
|
|
|
diag.setDefault(2)
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = diag.run()
|
|
|
|
if ret == _("Upload to AnkiWeb"):
|
|
|
|
self.thread.fullSyncChoice = "upload"
|
|
|
|
elif ret == _("Download from AnkiWeb"):
|
|
|
|
self.thread.fullSyncChoice = "download"
|
|
|
|
else:
|
|
|
|
self.thread.fullSyncChoice = "cancel"
|
2018-05-01 06:34:03 +02:00
|
|
|
self.mw.progress.start(immediate=True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _clockOff(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
|
|
|
_(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
Syncing requires the clock on your computer to be set correctly. Please \
|
2019-12-23 01:34:10 +01:00
|
|
|
fix the clock and try again."""
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2013-05-14 08:41:10 +02:00
|
|
|
def _checkFailed(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
|
|
|
_(
|
|
|
|
"""\
|
2013-05-14 08:10:58 +02:00
|
|
|
Your collection is in an inconsistent state. Please run Tools>\
|
2019-12-23 01:34:10 +01:00
|
|
|
Check Database, then sync again."""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-05-14 08:10:58 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Sync thread
|
|
|
|
######################################################################
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class SyncThread(QThread):
|
|
|
|
|
2019-12-15 23:51:38 +01:00
|
|
|
_event = pyqtSignal(str, str)
|
2020-01-18 08:49:59 +01:00
|
|
|
progress_event = pyqtSignal(int, int)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
2020-02-04 02:41:20 +01:00
|
|
|
def __init__(self, path, hkey, auth=None, hostNum=None):
|
2012-12-21 08:51:59 +01:00
|
|
|
QThread.__init__(self)
|
|
|
|
self.path = path
|
|
|
|
self.hkey = hkey
|
|
|
|
self.auth = auth
|
2018-07-28 10:08:16 +02:00
|
|
|
self.hostNum = hostNum
|
2019-12-23 01:34:10 +01:00
|
|
|
self._abort = 0 # 1=flagged, 2=aborting
|
2017-01-17 08:15:50 +01:00
|
|
|
|
|
|
|
def flagAbort(self):
|
|
|
|
self._abort = 1
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def run(self):
|
2013-10-22 08:30:46 +02:00
|
|
|
# init this first so an early crash doesn't cause an error
|
|
|
|
# in the main thread
|
|
|
|
self.syncMsg = ""
|
|
|
|
self.uname = ""
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2020-03-05 01:20:16 +01:00
|
|
|
self.col = Collection(self.path)
|
2012-12-21 08:51:59 +01:00
|
|
|
except:
|
|
|
|
self.fireEvent("corrupt")
|
|
|
|
return
|
2018-07-28 10:08:16 +02:00
|
|
|
self.server = RemoteServer(self.hkey, hostNum=self.hostNum)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.client = Syncer(self.col, self.server)
|
|
|
|
self.sentTotal = 0
|
|
|
|
self.recvTotal = 0
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def syncEvent(type):
|
|
|
|
self.fireEvent("sync", type)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2014-07-28 10:00:26 +02:00
|
|
|
def syncMsg(msg):
|
|
|
|
self.fireEvent("syncMsg", msg)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-18 08:49:59 +01:00
|
|
|
def http_progress(upload: int, download: int) -> None:
|
2017-01-17 08:15:50 +01:00
|
|
|
if not self._abort:
|
2020-01-18 08:49:59 +01:00
|
|
|
self.sentTotal += upload
|
|
|
|
self.recvTotal += download
|
|
|
|
self.progress_event.emit(self.sentTotal, self.recvTotal) # type: ignore
|
2017-01-17 08:15:50 +01:00
|
|
|
elif self._abort == 1:
|
|
|
|
self._abort = 2
|
|
|
|
raise Exception("sync cancelled")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-18 08:49:59 +01:00
|
|
|
self.server.client.progress_hook = http_progress
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-15 07:53:24 +01:00
|
|
|
hooks.sync_stage_did_change.append(syncEvent)
|
|
|
|
hooks.sync_progress_did_change.append(syncMsg)
|
2012-12-21 08:51:59 +01:00
|
|
|
# run sync and catch any errors
|
|
|
|
try:
|
|
|
|
self._sync()
|
|
|
|
except:
|
|
|
|
err = traceback.format_exc()
|
|
|
|
self.fireEvent("error", err)
|
|
|
|
finally:
|
|
|
|
# don't bump mod time unless we explicitly save
|
2020-04-04 09:21:45 +02:00
|
|
|
self.col.close(save=False, downgrade=False)
|
2020-01-15 07:53:24 +01:00
|
|
|
hooks.sync_stage_did_change.remove(syncEvent)
|
|
|
|
hooks.sync_progress_did_change.remove(syncMsg)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-01-17 08:15:50 +01:00
|
|
|
def _abortingSync(self):
|
|
|
|
try:
|
|
|
|
return self.client.sync()
|
|
|
|
except Exception as e:
|
|
|
|
if "sync cancelled" in str(e):
|
|
|
|
self.server.abort()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def _sync(self):
|
|
|
|
if self.auth:
|
|
|
|
# need to authenticate and obtain host key
|
|
|
|
self.hkey = self.server.hostKey(*self.auth)
|
|
|
|
if not self.hkey:
|
|
|
|
# provided details were invalid
|
|
|
|
return self.fireEvent("badAuth")
|
|
|
|
else:
|
|
|
|
# write new details and tell calling thread to save
|
|
|
|
self.fireEvent("newKey", self.hkey)
|
|
|
|
# run sync and check state
|
|
|
|
try:
|
2017-01-17 08:15:50 +01:00
|
|
|
ret = self._abortingSync()
|
2016-05-12 06:45:35 +02:00
|
|
|
except Exception as e:
|
2012-12-21 08:51:59 +01:00
|
|
|
log = traceback.format_exc()
|
2013-11-14 03:14:21 +01:00
|
|
|
err = repr(str(e))
|
2019-12-23 01:34:10 +01:00
|
|
|
if (
|
|
|
|
"Unable to find the server" in err
|
|
|
|
or "Errno 2" in err
|
|
|
|
or "getaddrinfo" in err
|
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fireEvent("offline")
|
2017-01-17 08:15:50 +01:00
|
|
|
elif "sync cancelled" in err:
|
|
|
|
pass
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2018-01-20 02:24:01 +01:00
|
|
|
self.fireEvent("error", log)
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
if ret == "badAuth":
|
|
|
|
return self.fireEvent("badAuth")
|
|
|
|
elif ret == "clockOff":
|
|
|
|
return self.fireEvent("clockOff")
|
2013-05-14 08:41:10 +02:00
|
|
|
elif ret == "basicCheckFailed" or ret == "sanityCheckFailed":
|
|
|
|
return self.fireEvent("checkFailed")
|
2012-12-21 08:51:59 +01:00
|
|
|
# full sync?
|
|
|
|
if ret == "fullSync":
|
|
|
|
return self._fullSync()
|
|
|
|
# save and note success state
|
|
|
|
if ret == "noChanges":
|
|
|
|
self.fireEvent("noChanges")
|
2013-10-03 23:09:28 +02:00
|
|
|
elif ret == "success":
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fireEvent("success")
|
2013-10-03 23:09:28 +02:00
|
|
|
elif ret == "serverAbort":
|
2019-04-29 10:27:00 +02:00
|
|
|
self.syncMsg = self.client.syncMsg
|
|
|
|
return
|
2013-10-03 23:09:28 +02:00
|
|
|
else:
|
|
|
|
self.fireEvent("error", "Unknown sync return code.")
|
|
|
|
self.syncMsg = self.client.syncMsg
|
|
|
|
self.uname = self.client.uname
|
2018-07-28 10:08:16 +02:00
|
|
|
self.hostNum = self.client.hostNum
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _fullSync(self):
|
2017-01-08 13:21:58 +01:00
|
|
|
# tell the calling thread we need a decision on sync direction, and
|
|
|
|
# wait for a reply
|
|
|
|
self.fullSyncChoice = False
|
|
|
|
self.localIsEmpty = self.col.isEmpty()
|
|
|
|
self.fireEvent("fullSync")
|
|
|
|
while not self.fullSyncChoice:
|
|
|
|
time.sleep(0.1)
|
|
|
|
f = self.fullSyncChoice
|
2012-12-21 08:51:59 +01:00
|
|
|
if f == "cancel":
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
self.client = FullSyncer(
|
|
|
|
self.col, self.hkey, self.server.client, hostNum=self.hostNum
|
|
|
|
)
|
2017-01-17 08:15:50 +01:00
|
|
|
try:
|
|
|
|
if f == "upload":
|
|
|
|
if not self.client.upload():
|
|
|
|
self.fireEvent("upbad")
|
|
|
|
else:
|
2018-09-05 11:53:41 +02:00
|
|
|
ret = self.client.download()
|
|
|
|
if ret == "downloadClobber":
|
|
|
|
self.fireEvent(ret)
|
|
|
|
return
|
2017-01-17 08:15:50 +01:00
|
|
|
except Exception as e:
|
|
|
|
if "sync cancelled" in str(e):
|
|
|
|
return
|
|
|
|
raise
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
def fireEvent(self, cmd, arg=""):
|
2019-12-15 23:51:38 +01:00
|
|
|
self._event.emit(cmd, arg)
|