2020-02-04 00:07:15 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-02-04 02:41:20 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-02-04 00:07:15 +01:00
|
|
|
import time
|
|
|
|
from concurrent.futures import Future
|
|
|
|
from dataclasses import dataclass
|
2020-05-29 11:59:50 +02:00
|
|
|
from typing import List, Optional, Union
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
import aqt
|
2020-03-23 10:54:01 +01:00
|
|
|
from anki.rsbackend import (
|
|
|
|
TR,
|
|
|
|
Interrupted,
|
|
|
|
MediaSyncProgress,
|
|
|
|
NetworkError,
|
|
|
|
ProgressKind,
|
|
|
|
)
|
2020-02-04 00:07:15 +01:00
|
|
|
from anki.types import assert_impossible
|
|
|
|
from anki.utils import intTime
|
|
|
|
from aqt import gui_hooks
|
2020-05-29 11:59:50 +02:00
|
|
|
from aqt.qt import QDialog, QDialogButtonBox, QPushButton, QTimer, qconnect
|
2020-02-17 01:18:20 +01:00
|
|
|
from aqt.utils import showWarning, tr
|
2020-02-04 00:07:15 +01:00
|
|
|
|
2020-02-06 09:16:39 +01:00
|
|
|
LogEntry = Union[MediaSyncProgress, str]
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class LogEntryWithTime:
|
|
|
|
time: int
|
|
|
|
entry: LogEntry
|
|
|
|
|
|
|
|
|
|
|
|
class MediaSyncer:
|
2020-02-04 03:26:10 +01:00
|
|
|
def __init__(self, mw: aqt.main.AnkiQt):
|
|
|
|
self.mw = mw
|
2020-02-06 09:16:39 +01:00
|
|
|
self._syncing: bool = False
|
2020-02-04 00:07:15 +01:00
|
|
|
self._log: List[LogEntryWithTime] = []
|
2020-05-29 11:59:50 +02:00
|
|
|
self._progress_timer: Optional[QTimer] = None
|
2020-02-05 02:55:14 +01:00
|
|
|
gui_hooks.media_sync_did_start_or_stop.append(self._on_start_stop)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
2020-05-29 11:59:50 +02:00
|
|
|
def _on_progress(self):
|
|
|
|
progress = self.mw.col.latest_progress()
|
2020-02-10 08:58:54 +01:00
|
|
|
if progress.kind != ProgressKind.MediaSync:
|
2020-05-29 11:59:50 +02:00
|
|
|
return
|
|
|
|
|
2020-02-10 08:58:54 +01:00
|
|
|
assert isinstance(progress.val, MediaSyncProgress)
|
2020-02-06 09:16:39 +01:00
|
|
|
self._log_and_notify(progress.val)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
2020-02-04 03:26:10 +01:00
|
|
|
def start(self) -> None:
|
2020-02-04 00:07:15 +01:00
|
|
|
"Start media syncing in the background, if it's not already running."
|
2020-02-06 09:16:39 +01:00
|
|
|
if self._syncing:
|
2020-02-04 00:07:15 +01:00
|
|
|
return
|
|
|
|
|
2020-02-04 03:26:10 +01:00
|
|
|
if not self.mw.pm.media_syncing_enabled():
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_DISABLED))
|
2020-02-04 03:26:10 +01:00
|
|
|
return
|
|
|
|
|
2020-05-30 04:28:22 +02:00
|
|
|
auth = self.mw.pm.sync_auth()
|
|
|
|
if auth is None:
|
|
|
|
return
|
|
|
|
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_STARTING))
|
2020-02-06 09:16:39 +01:00
|
|
|
self._syncing = True
|
2020-05-29 11:59:50 +02:00
|
|
|
self._progress_timer = self.mw.progress.timer(1000, self._on_progress, True)
|
2020-02-05 02:55:14 +01:00
|
|
|
gui_hooks.media_sync_did_start_or_stop(True)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
def run() -> None:
|
2020-05-30 04:28:22 +02:00
|
|
|
self.mw.col.backend.sync_media(auth)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
2020-02-04 03:26:10 +01:00
|
|
|
self.mw.taskman.run_in_background(run, self._on_finished)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
def _log_and_notify(self, entry: LogEntry) -> None:
|
|
|
|
entry_with_time = LogEntryWithTime(time=intTime(), entry=entry)
|
|
|
|
self._log.append(entry_with_time)
|
2020-02-04 03:26:10 +01:00
|
|
|
self.mw.taskman.run_on_main(
|
2020-02-04 00:07:15 +01:00
|
|
|
lambda: gui_hooks.media_sync_did_progress(entry_with_time)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _on_finished(self, future: Future) -> None:
|
2020-02-06 09:16:39 +01:00
|
|
|
self._syncing = False
|
2020-05-29 11:59:50 +02:00
|
|
|
if self._progress_timer:
|
|
|
|
self._progress_timer.stop()
|
|
|
|
self._progress_timer = None
|
2020-02-05 02:55:14 +01:00
|
|
|
gui_hooks.media_sync_did_start_or_stop(False)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
exc = future.exception()
|
|
|
|
if exc is not None:
|
2020-02-04 03:26:10 +01:00
|
|
|
self._handle_sync_error(exc)
|
|
|
|
else:
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_COMPLETE))
|
2020-02-04 03:26:10 +01:00
|
|
|
|
|
|
|
def _handle_sync_error(self, exc: BaseException):
|
2020-02-04 06:16:11 +01:00
|
|
|
if isinstance(exc, Interrupted):
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_ABORTED))
|
2020-02-04 06:16:11 +01:00
|
|
|
return
|
2020-03-23 10:06:13 +01:00
|
|
|
elif isinstance(exc, NetworkError):
|
|
|
|
# avoid popups for network errors
|
|
|
|
self._log_and_notify(str(exc))
|
|
|
|
return
|
2020-02-04 06:16:11 +01:00
|
|
|
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_FAILED))
|
2020-02-27 01:46:24 +01:00
|
|
|
showWarning(str(exc))
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
def entries(self) -> List[LogEntryWithTime]:
|
|
|
|
return self._log
|
|
|
|
|
|
|
|
def abort(self) -> None:
|
2020-02-04 03:26:10 +01:00
|
|
|
if not self.is_syncing():
|
|
|
|
return
|
2020-02-27 03:25:19 +01:00
|
|
|
self._log_and_notify(tr(TR.SYNC_MEDIA_ABORTING))
|
2020-05-29 11:59:50 +02:00
|
|
|
# fixme: latter should do the former for us in the future
|
|
|
|
self.mw.col.backend.set_wants_abort()
|
2020-05-29 08:45:27 +02:00
|
|
|
self.mw.col.backend.abort_sync()
|
2020-02-04 00:07:15 +01:00
|
|
|
|
2020-02-04 02:41:20 +01:00
|
|
|
def is_syncing(self) -> bool:
|
2020-02-06 09:16:39 +01:00
|
|
|
return self._syncing
|
2020-02-04 02:41:20 +01:00
|
|
|
|
2020-02-27 04:27:58 +01:00
|
|
|
def _on_start_stop(self, running: bool) -> None:
|
|
|
|
self.mw.toolbar.set_sync_active(running)
|
2020-02-04 03:26:10 +01:00
|
|
|
|
|
|
|
def show_sync_log(self):
|
|
|
|
aqt.dialogs.open("sync_log", self.mw, self)
|
|
|
|
|
2020-02-05 03:23:15 +01:00
|
|
|
def show_diag_until_finished(self):
|
|
|
|
# nothing to do if not syncing
|
|
|
|
if not self.is_syncing():
|
|
|
|
return
|
|
|
|
|
|
|
|
diag: MediaSyncDialog = aqt.dialogs.open("sync_log", self.mw, self, True)
|
|
|
|
diag.exec_()
|
|
|
|
|
2020-02-05 03:38:36 +01:00
|
|
|
def seconds_since_last_sync(self) -> int:
|
|
|
|
if self.is_syncing():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if self._log:
|
|
|
|
last = self._log[-1].time
|
|
|
|
else:
|
|
|
|
last = 0
|
|
|
|
return intTime() - last
|
|
|
|
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
class MediaSyncDialog(QDialog):
|
2020-02-04 02:41:20 +01:00
|
|
|
silentlyClose = True
|
|
|
|
|
2020-02-05 03:23:15 +01:00
|
|
|
def __init__(
|
|
|
|
self, mw: aqt.main.AnkiQt, syncer: MediaSyncer, close_when_done: bool = False
|
|
|
|
) -> None:
|
2020-02-04 02:41:20 +01:00
|
|
|
super().__init__(mw)
|
|
|
|
self.mw = mw
|
2020-02-04 00:07:15 +01:00
|
|
|
self._syncer = syncer
|
2020-02-05 03:23:15 +01:00
|
|
|
self._close_when_done = close_when_done
|
2020-02-04 00:07:15 +01:00
|
|
|
self.form = aqt.forms.synclog.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2020-02-27 03:25:19 +01:00
|
|
|
self.setWindowTitle(tr(TR.SYNC_MEDIA_LOG_TITLE))
|
|
|
|
self.abort_button = QPushButton(tr(TR.SYNC_ABORT_BUTTON))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.abort_button.clicked, self._on_abort)
|
2020-02-04 03:26:10 +01:00
|
|
|
self.abort_button.setAutoDefault(False)
|
2020-02-04 00:07:15 +01:00
|
|
|
self.form.buttonBox.addButton(self.abort_button, QDialogButtonBox.ActionRole)
|
2020-02-27 03:22:24 +01:00
|
|
|
self.abort_button.setHidden(not self._syncer.is_syncing())
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
gui_hooks.media_sync_did_progress.append(self._on_log_entry)
|
2020-02-05 03:23:15 +01:00
|
|
|
gui_hooks.media_sync_did_start_or_stop.append(self._on_start_stop)
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
self.form.plainTextEdit.setPlainText(
|
|
|
|
"\n".join(self._entry_to_text(x) for x in syncer.entries())
|
|
|
|
)
|
2020-02-04 02:41:20 +01:00
|
|
|
self.show()
|
|
|
|
|
2020-02-05 03:23:15 +01:00
|
|
|
def reject(self) -> None:
|
|
|
|
if self._close_when_done and self._syncer.is_syncing():
|
|
|
|
# closing while syncing on close starts an abort
|
|
|
|
self._on_abort()
|
|
|
|
return
|
2020-02-04 02:41:20 +01:00
|
|
|
|
|
|
|
aqt.dialogs.markClosed("sync_log")
|
2020-02-05 03:23:15 +01:00
|
|
|
QDialog.reject(self)
|
2020-02-04 02:41:20 +01:00
|
|
|
|
2020-02-05 03:23:15 +01:00
|
|
|
def reopen(self, mw, syncer, close_when_done: bool = False) -> None:
|
|
|
|
self._close_when_done = close_when_done
|
2020-02-04 02:41:20 +01:00
|
|
|
self.show()
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
def _on_abort(self, *args) -> None:
|
|
|
|
self._syncer.abort()
|
|
|
|
self.abort_button.setHidden(True)
|
|
|
|
|
|
|
|
def _time_and_text(self, stamp: int, text: str) -> str:
|
|
|
|
asctime = time.asctime(time.localtime(stamp))
|
|
|
|
return f"{asctime}: {text}"
|
|
|
|
|
|
|
|
def _entry_to_text(self, entry: LogEntryWithTime):
|
2020-02-04 03:26:10 +01:00
|
|
|
if isinstance(entry.entry, str):
|
|
|
|
txt = entry.entry
|
2020-02-06 09:16:39 +01:00
|
|
|
elif isinstance(entry.entry, MediaSyncProgress):
|
2020-02-04 00:07:15 +01:00
|
|
|
txt = self._logentry_to_text(entry.entry)
|
|
|
|
else:
|
|
|
|
assert_impossible(entry.entry)
|
|
|
|
return self._time_and_text(entry.time, txt)
|
|
|
|
|
2020-02-06 09:16:39 +01:00
|
|
|
def _logentry_to_text(self, e: MediaSyncProgress) -> str:
|
2020-02-16 09:46:51 +01:00
|
|
|
return f"{e.added}, {e.removed}, {e.checked}"
|
2020-02-04 00:07:15 +01:00
|
|
|
|
|
|
|
def _on_log_entry(self, entry: LogEntryWithTime):
|
|
|
|
self.form.plainTextEdit.appendPlainText(self._entry_to_text(entry))
|
2020-02-04 03:26:10 +01:00
|
|
|
if not self._syncer.is_syncing():
|
|
|
|
self.abort_button.setHidden(True)
|
2020-02-05 03:23:15 +01:00
|
|
|
|
|
|
|
def _on_start_stop(self, running: bool) -> None:
|
|
|
|
if not running and self._close_when_done:
|
|
|
|
aqt.dialogs.markClosed("sync_log")
|
|
|
|
self._close_when_done = False
|
|
|
|
self.close()
|