diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 65b16e5f7..76d1889f3 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2721,6 +2721,64 @@ class _StyleDidInitFilter: style_did_init = _StyleDidInitFilter() +class _SyncDidFinishHook: + """Executes after the sync of the collection concluded. + + Note that the media sync did not necessarily finish at this point.""" + + _hooks: List[Callable[[], None]] = [] + + def append(self, cb: Callable[[], None]) -> None: + """()""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self) -> None: + for hook in self._hooks: + try: + hook() + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +sync_did_finish = _SyncDidFinishHook() + + +class _SyncWillStartHook: + _hooks: List[Callable[[], None]] = [] + + def append(self, cb: Callable[[], None]) -> None: + """()""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self) -> None: + for hook in self._hooks: + try: + hook() + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +sync_will_start = _SyncWillStartHook() + + class _TagEditorDidProcessKeyHook: _hooks: List[Callable[[TagEdit, QEvent], None]] = [] diff --git a/qt/aqt/main.py b/qt/aqt/main.py index f9b80ef3a..990398c53 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -908,9 +908,12 @@ title="%s" %s>%s""" % ( def on_collection_sync_finished(): self.col.clearUndo() self.col.models._clear_cache() + gui_hooks.sync_did_finish() self.reset() + after_sync() + gui_hooks.sync_will_start() sync_collection(self, on_done=on_collection_sync_finished) def maybe_auto_sync_on_open_close(self, after_sync: Callable[[], None]) -> None: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 1d7b3336b..dfecb14ff 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -538,6 +538,14 @@ hooks = [ args=["diag: aqt.emptycards.EmptyCardsDialog"], doc="""Allows changing the list of cards to delete.""", ), + Hook(name="sync_will_start", args=[]), + Hook( + name="sync_did_finish", + args=[], + doc="""Executes after the sync of the collection concluded. + + Note that the media sync did not necessarily finish at this point.""", + ), # Adding cards ################### Hook(