autosync media on close

This commit is contained in:
Damien Elmes 2020-02-05 12:23:15 +10:00
parent 59a7993011
commit 12d009e503
2 changed files with 38 additions and 13 deletions

View file

@ -375,6 +375,9 @@ close the profile or restart Anki."""
self._unloadProfile() self._unloadProfile()
onsuccess() onsuccess()
# start media sync if not already running
self.maybe_auto_sync_media()
gui_hooks.profile_will_close() gui_hooks.profile_will_close()
self.unloadCollection(callback) self.unloadCollection(callback)
@ -389,7 +392,7 @@ close the profile or restart Anki."""
# at this point there should be no windows left # at this point there should be no windows left
self._checkForUnclosedWidgets() self._checkForUnclosedWidgets()
self.maybeAutoSync() self.maybeAutoSync(True)
def _checkForUnclosedWidgets(self) -> None: def _checkForUnclosedWidgets(self) -> None:
for w in self.app.topLevelWidgets(): for w in self.app.topLevelWidgets():
@ -846,7 +849,7 @@ title="%s" %s>%s</button>""" % (
self.media_syncer.start() self.media_syncer.start()
# expects a current profile, but no collection loaded # expects a current profile, but no collection loaded
def maybeAutoSync(self) -> None: def maybeAutoSync(self, closing=False) -> None:
if ( if (
not self.pm.profile["syncKey"] not self.pm.profile["syncKey"]
or not self.pm.profile["autoSync"] or not self.pm.profile["autoSync"]
@ -858,6 +861,10 @@ title="%s" %s>%s</button>""" % (
# ok to sync # ok to sync
self._sync() self._sync()
# if media still syncing at this point, pop up progress diag
if closing:
self.media_syncer.show_diag_until_finished()
def maybe_auto_sync_media(self) -> None: def maybe_auto_sync_media(self) -> None:
if not self.pm.profile["autoSync"] or self.safeMode or self.restoringBackup: if not self.pm.profile["autoSync"] or self.safeMode or self.restoringBackup:
return return

View file

@ -7,7 +7,7 @@ import time
from concurrent.futures import Future from concurrent.futures import Future
from copy import copy from copy import copy
from dataclasses import dataclass from dataclasses import dataclass
from typing import List, Optional, Union from typing import Callable, List, Optional, Union
import aqt import aqt
from anki import hooks from anki import hooks
@ -46,10 +46,8 @@ class MediaSyncState:
# fixme: sync.rs fixmes # fixme: sync.rs fixmes
# fixme: maximum size when uploading # fixme: maximum size when uploading
# fixme: abort when closing collection/app
# fixme: concurrent modifications during upload step # fixme: concurrent modifications during upload step
# fixme: mediaSanity # fixme: mediaSanity
# fixme: autosync
# elif evt == "mediaSanity": # elif evt == "mediaSanity":
# showWarning( # showWarning(
# _( # _(
@ -202,19 +200,30 @@ class MediaSyncer:
return self._sync_state is not None return self._sync_state is not None
def _on_start_stop(self, running: bool): def _on_start_stop(self, running: bool):
self.mw.toolbar.set_sync_active(running) self.mw.toolbar.set_sync_active(running) # type: ignore
def show_sync_log(self): def show_sync_log(self):
aqt.dialogs.open("sync_log", self.mw, self) aqt.dialogs.open("sync_log", self.mw, self)
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_()
class MediaSyncDialog(QDialog): class MediaSyncDialog(QDialog):
silentlyClose = True silentlyClose = True
def __init__(self, mw: aqt.main.AnkiQt, syncer: MediaSyncer) -> None: def __init__(
self, mw: aqt.main.AnkiQt, syncer: MediaSyncer, close_when_done: bool = False
) -> None:
super().__init__(mw) super().__init__(mw)
self.mw = mw self.mw = mw
self._syncer = syncer self._syncer = syncer
self._close_when_done = close_when_done
self.form = aqt.forms.synclog.Ui_Dialog() self.form = aqt.forms.synclog.Ui_Dialog()
self.form.setupUi(self) self.form.setupUi(self)
self.abort_button = QPushButton(_("Abort")) self.abort_button = QPushButton(_("Abort"))
@ -223,21 +232,24 @@ class MediaSyncDialog(QDialog):
self.form.buttonBox.addButton(self.abort_button, QDialogButtonBox.ActionRole) self.form.buttonBox.addButton(self.abort_button, QDialogButtonBox.ActionRole)
gui_hooks.media_sync_did_progress.append(self._on_log_entry) gui_hooks.media_sync_did_progress.append(self._on_log_entry)
gui_hooks.media_sync_did_start_or_stop.append(self._on_start_stop)
self.form.plainTextEdit.setPlainText( self.form.plainTextEdit.setPlainText(
"\n".join(self._entry_to_text(x) for x in syncer.entries()) "\n".join(self._entry_to_text(x) for x in syncer.entries())
) )
self.show() self.show()
def reject(self): 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
aqt.dialogs.markClosed("sync_log") aqt.dialogs.markClosed("sync_log")
QDialog.reject(self) QDialog.reject(self)
def accept(self): def reopen(self, mw, syncer, close_when_done: bool = False) -> None:
aqt.dialogs.markClosed("sync_log") self._close_when_done = close_when_done
QDialog.accept(self)
def reopen(self, *args):
self.show() self.show()
def _on_abort(self, *args) -> None: def _on_abort(self, *args) -> None:
@ -272,3 +284,9 @@ class MediaSyncDialog(QDialog):
self.form.plainTextEdit.appendPlainText(self._entry_to_text(entry)) self.form.plainTextEdit.appendPlainText(self._entry_to_text(entry))
if not self._syncer.is_syncing(): if not self._syncer.is_syncing():
self.abort_button.setHidden(True) self.abort_button.setHidden(True)
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()