2020-02-10 08:58:54 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-02-11 08:30:10 +01:00
|
|
|
import itertools
|
2020-02-10 08:58:54 +01:00
|
|
|
import time
|
|
|
|
from concurrent.futures import Future
|
2020-02-11 08:30:10 +01:00
|
|
|
from typing import Iterable, List, Optional, TypeVar
|
2020-02-10 08:58:54 +01:00
|
|
|
|
|
|
|
import aqt
|
|
|
|
from anki import hooks
|
2020-02-27 03:25:19 +01:00
|
|
|
from anki.rsbackend import TR, Interrupted, MediaCheckOutput, Progress, ProgressKind
|
2020-02-10 08:58:54 +01:00
|
|
|
from aqt.qt import *
|
2020-02-17 01:18:20 +01:00
|
|
|
from aqt.utils import askUser, restoreGeom, saveGeom, showText, tooltip, tr
|
2020-02-10 08:58:54 +01:00
|
|
|
|
2020-02-11 08:30:10 +01:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
def chunked_list(l: Iterable[T], n: int) -> Iterable[List[T]]:
|
|
|
|
l = iter(l)
|
|
|
|
while True:
|
|
|
|
res = list(itertools.islice(l, n))
|
|
|
|
if not res:
|
|
|
|
return
|
|
|
|
yield res
|
|
|
|
|
2020-02-10 08:58:54 +01:00
|
|
|
|
|
|
|
def check_media_db(mw: aqt.AnkiQt) -> None:
|
|
|
|
c = MediaChecker(mw)
|
|
|
|
c.check()
|
|
|
|
|
|
|
|
|
|
|
|
class MediaChecker:
|
|
|
|
progress_dialog: Optional[aqt.progress.ProgressDialog]
|
|
|
|
|
|
|
|
def __init__(self, mw: aqt.AnkiQt) -> None:
|
|
|
|
self.mw = mw
|
|
|
|
|
|
|
|
def check(self) -> None:
|
|
|
|
self.progress_dialog = self.mw.progress.start()
|
|
|
|
hooks.bg_thread_progress_callback.append(self._on_progress)
|
|
|
|
self.mw.taskman.run_in_background(self._check, self._on_finished)
|
|
|
|
|
|
|
|
def _on_progress(self, proceed: bool, progress: Progress) -> bool:
|
|
|
|
if progress.kind != ProgressKind.MediaCheck:
|
|
|
|
return proceed
|
|
|
|
|
|
|
|
if self.progress_dialog.wantCancel:
|
|
|
|
return False
|
|
|
|
|
2020-02-16 09:46:51 +01:00
|
|
|
self.mw.taskman.run_on_main(lambda: self.mw.progress.update(progress.val))
|
2020-02-10 08:58:54 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _check(self) -> MediaCheckOutput:
|
|
|
|
"Run the check on a background thread."
|
|
|
|
return self.mw.col.media.check()
|
|
|
|
|
2020-02-14 07:15:18 +01:00
|
|
|
def _on_finished(self, future: Future) -> None:
|
2020-02-10 08:58:54 +01:00
|
|
|
hooks.bg_thread_progress_callback.remove(self._on_progress)
|
|
|
|
self.mw.progress.finish()
|
|
|
|
self.progress_dialog = None
|
|
|
|
|
|
|
|
exc = future.exception()
|
|
|
|
if isinstance(exc, Interrupted):
|
|
|
|
return
|
|
|
|
|
2020-02-14 07:15:18 +01:00
|
|
|
output: MediaCheckOutput = future.result()
|
|
|
|
report = output.report
|
2020-02-10 08:58:54 +01:00
|
|
|
|
|
|
|
# show report and offer to delete
|
|
|
|
diag = QDialog(self.mw)
|
2020-03-12 08:56:36 +01:00
|
|
|
diag.setWindowTitle(tr(TR.MEDIA_CHECK_WINDOW_TITLE))
|
2020-02-10 08:58:54 +01:00
|
|
|
layout = QVBoxLayout(diag)
|
|
|
|
diag.setLayout(layout)
|
|
|
|
text = QTextEdit()
|
|
|
|
text.setReadOnly(True)
|
|
|
|
text.setPlainText(report)
|
|
|
|
layout.addWidget(text)
|
|
|
|
box = QDialogButtonBox(QDialogButtonBox.Close)
|
|
|
|
layout.addWidget(box)
|
2020-02-11 08:30:10 +01:00
|
|
|
|
2020-02-10 08:58:54 +01:00
|
|
|
if output.unused:
|
2020-02-27 03:25:19 +01:00
|
|
|
b = QPushButton(tr(TR.MEDIA_CHECK_DELETE_UNUSED))
|
2020-02-10 08:58:54 +01:00
|
|
|
b.setAutoDefault(False)
|
2020-02-11 08:30:10 +01:00
|
|
|
box.addButton(b, QDialogButtonBox.RejectRole)
|
|
|
|
b.clicked.connect(lambda c: self._on_trash_files(output.unused)) # type: ignore
|
2020-02-10 08:58:54 +01:00
|
|
|
|
2020-02-11 06:09:33 +01:00
|
|
|
if output.missing:
|
|
|
|
if any(map(lambda x: x.startswith("latex-"), output.missing)):
|
2020-02-27 03:25:19 +01:00
|
|
|
b = QPushButton(tr(TR.MEDIA_CHECK_RENDER_LATEX))
|
2020-02-11 06:09:33 +01:00
|
|
|
b.setAutoDefault(False)
|
|
|
|
box.addButton(b, QDialogButtonBox.RejectRole)
|
|
|
|
b.clicked.connect(self._on_render_latex) # type: ignore
|
|
|
|
|
2020-03-10 03:49:40 +01:00
|
|
|
if output.have_trash:
|
|
|
|
b = QPushButton(tr(TR.MEDIA_CHECK_EMPTY_TRASH))
|
|
|
|
b.setAutoDefault(False)
|
|
|
|
box.addButton(b, QDialogButtonBox.RejectRole)
|
|
|
|
b.clicked.connect(lambda c: self._on_empty_trash()) # type: ignore
|
2020-03-10 04:35:09 +01:00
|
|
|
|
|
|
|
b = QPushButton(tr(TR.MEDIA_CHECK_RESTORE_TRASH))
|
|
|
|
b.setAutoDefault(False)
|
|
|
|
box.addButton(b, QDialogButtonBox.RejectRole)
|
|
|
|
b.clicked.connect(lambda c: self._on_restore_trash()) # type: ignore
|
|
|
|
|
2020-02-10 08:58:54 +01:00
|
|
|
box.rejected.connect(diag.reject) # type: ignore
|
|
|
|
diag.setMinimumHeight(400)
|
|
|
|
diag.setMinimumWidth(500)
|
|
|
|
restoreGeom(diag, "checkmediadb")
|
|
|
|
diag.exec_()
|
|
|
|
saveGeom(diag, "checkmediadb")
|
|
|
|
|
2020-02-11 06:09:33 +01:00
|
|
|
def _on_render_latex(self):
|
|
|
|
self.progress_dialog = self.mw.progress.start()
|
|
|
|
try:
|
2020-02-11 06:36:05 +01:00
|
|
|
out = self.mw.col.media.render_all_latex(self._on_render_latex_progress)
|
2020-02-11 07:46:57 +01:00
|
|
|
if self.progress_dialog.wantCancel:
|
|
|
|
return
|
2020-02-11 06:09:33 +01:00
|
|
|
finally:
|
|
|
|
self.mw.progress.finish()
|
2020-02-11 07:46:57 +01:00
|
|
|
self.progress_dialog = None
|
2020-02-11 06:36:05 +01:00
|
|
|
|
|
|
|
if out is not None:
|
|
|
|
nid, err = out
|
|
|
|
browser = aqt.dialogs.open("Browser", self.mw)
|
|
|
|
browser.form.searchEdit.lineEdit().setText("nid:%d" % nid)
|
|
|
|
browser.onSearchActivated()
|
|
|
|
showText(err, type="html")
|
|
|
|
else:
|
2020-02-27 03:25:19 +01:00
|
|
|
tooltip(tr(TR.MEDIA_CHECK_ALL_LATEX_RENDERED))
|
2020-02-11 06:09:33 +01:00
|
|
|
|
|
|
|
def _on_render_latex_progress(self, count: int) -> bool:
|
|
|
|
if self.progress_dialog.wantCancel:
|
|
|
|
return False
|
|
|
|
|
2020-02-27 03:25:19 +01:00
|
|
|
self.mw.progress.update(tr(TR.MEDIA_CHECK_CHECKED, count=count))
|
2020-02-11 06:09:33 +01:00
|
|
|
return True
|
|
|
|
|
2020-02-11 08:30:10 +01:00
|
|
|
def _on_trash_files(self, fnames: List[str]):
|
2020-02-27 03:25:19 +01:00
|
|
|
if not askUser(tr(TR.MEDIA_CHECK_DELETE_UNUSED_CONFIRM)):
|
2020-02-11 08:30:10 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
self.progress_dialog = self.mw.progress.start()
|
|
|
|
|
|
|
|
last_progress = time.time()
|
|
|
|
remaining = len(fnames)
|
2020-02-17 01:18:20 +01:00
|
|
|
total = len(fnames)
|
2020-02-11 08:30:10 +01:00
|
|
|
try:
|
|
|
|
for chunk in chunked_list(fnames, 25):
|
|
|
|
self.mw.col.media.trash_files(chunk)
|
|
|
|
remaining -= len(chunk)
|
|
|
|
if time.time() - last_progress >= 0.3:
|
2020-02-17 01:18:20 +01:00
|
|
|
self.mw.progress.update(
|
2020-02-27 03:25:19 +01:00
|
|
|
tr(TR.MEDIA_CHECK_FILES_REMAINING, count=remaining)
|
2020-02-11 08:30:10 +01:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
self.mw.progress.finish()
|
|
|
|
self.progress_dialog = None
|
|
|
|
|
2020-02-27 03:25:19 +01:00
|
|
|
tooltip(tr(TR.MEDIA_CHECK_DELETE_UNUSED_COMPLETE, count=total))
|
2020-03-10 03:49:40 +01:00
|
|
|
|
|
|
|
def _on_empty_trash(self):
|
|
|
|
self.progress_dialog = self.mw.progress.start()
|
|
|
|
hooks.bg_thread_progress_callback.append(self._on_progress)
|
|
|
|
|
|
|
|
def empty_trash():
|
|
|
|
self.mw.col.backend.empty_trash()
|
|
|
|
|
|
|
|
def on_done(fut: Future):
|
|
|
|
self.mw.progress.finish()
|
|
|
|
hooks.bg_thread_progress_callback.remove(self._on_progress)
|
|
|
|
# check for errors
|
|
|
|
fut.result()
|
|
|
|
|
|
|
|
tooltip(tr(TR.MEDIA_CHECK_TRASH_EMPTIED))
|
|
|
|
|
|
|
|
self.mw.taskman.run_in_background(empty_trash, on_done)
|
2020-03-10 04:35:09 +01:00
|
|
|
|
|
|
|
def _on_restore_trash(self):
|
|
|
|
self.progress_dialog = self.mw.progress.start()
|
|
|
|
hooks.bg_thread_progress_callback.append(self._on_progress)
|
|
|
|
|
|
|
|
def restore_trash():
|
|
|
|
self.mw.col.backend.restore_trash()
|
|
|
|
|
|
|
|
def on_done(fut: Future):
|
|
|
|
self.mw.progress.finish()
|
|
|
|
hooks.bg_thread_progress_callback.remove(self._on_progress)
|
|
|
|
# check for errors
|
|
|
|
fut.result()
|
|
|
|
|
|
|
|
tooltip(tr(TR.MEDIA_CHECK_TRASH_RESTORED))
|
|
|
|
|
|
|
|
self.mw.taskman.run_in_background(restore_trash, on_done)
|