mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
import media in custom location during upgrade
This commit is contained in:
parent
a1a7e7341c
commit
11f95139f4
1 changed files with 45 additions and 3 deletions
|
@ -2,11 +2,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import os, cPickle
|
import os, cPickle, ctypes
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from anki.utils import isMac, isWin
|
from anki.utils import isMac, isWin
|
||||||
from anki import Collection
|
from anki import Collection
|
||||||
from anki.importing import Anki1Importer
|
from anki.importing import Anki1Importer
|
||||||
|
from anki.db import DB
|
||||||
|
|
||||||
class Upgrader(object):
|
class Upgrader(object):
|
||||||
|
|
||||||
|
@ -148,7 +149,7 @@ carefully, as a lot has changed since the previous Anki version."""))
|
||||||
v.addWidget(l2)
|
v.addWidget(l2)
|
||||||
self.setLayout(v)
|
self.setLayout(v)
|
||||||
# run the upgrade in a different thread
|
# run the upgrade in a different thread
|
||||||
self.thread = UpgradeThread(decks, colpath)
|
self.thread = UpgradeThread(decks, colpath, upgrader.conf)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
# and periodically update the GUI
|
# and periodically update the GUI
|
||||||
self.timer = QTimer(self)
|
self.timer = QTimer(self)
|
||||||
|
@ -191,15 +192,18 @@ Below is a log of the update:
|
||||||
return FinishedPage()
|
return FinishedPage()
|
||||||
|
|
||||||
class UpgradeThread(QThread):
|
class UpgradeThread(QThread):
|
||||||
def __init__(self, paths, colpath):
|
|
||||||
|
def __init__(self, paths, colpath, oldprefs):
|
||||||
QThread.__init__(self)
|
QThread.__init__(self)
|
||||||
self.paths = paths
|
self.paths = paths
|
||||||
self.max = len(paths)
|
self.max = len(paths)
|
||||||
self.current = 1
|
self.current = 1
|
||||||
self.finished = False
|
self.finished = False
|
||||||
self.colpath = colpath
|
self.colpath = colpath
|
||||||
|
self.oldprefs = oldprefs
|
||||||
self.name = ""
|
self.name = ""
|
||||||
self.log = []
|
self.log = []
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# open profile deck
|
# open profile deck
|
||||||
self.col = Collection(self.colpath)
|
self.col = Collection(self.colpath)
|
||||||
|
@ -214,18 +218,27 @@ class UpgradeThread(QThread):
|
||||||
self.current += 1
|
self.current += 1
|
||||||
self.col.close()
|
self.col.close()
|
||||||
self.finished = True
|
self.finished = True
|
||||||
|
|
||||||
def progress(self):
|
def progress(self):
|
||||||
if self.finished:
|
if self.finished:
|
||||||
return
|
return
|
||||||
return _("Upgrading deck %(a)s of %(b)s...\n%(c)s") % \
|
return _("Upgrading deck %(a)s of %(b)s...\n%(c)s") % \
|
||||||
dict(a=self.current, b=self.max, c=self.name)
|
dict(a=self.current, b=self.max, c=self.name)
|
||||||
|
|
||||||
def upgrade(self, path):
|
def upgrade(self, path):
|
||||||
log = self._upgrade(path)
|
log = self._upgrade(path)
|
||||||
self.log.append((self.name, log))
|
self.log.append((self.name, log))
|
||||||
|
|
||||||
def _upgrade(self, path):
|
def _upgrade(self, path):
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
return [_("File was missing.")]
|
return [_("File was missing.")]
|
||||||
imp = Anki1Importer(self.col, path)
|
imp = Anki1Importer(self.col, path)
|
||||||
|
# try to copy over dropbox media first
|
||||||
|
try:
|
||||||
|
self.maybeCopyFromCustomFolder(path)
|
||||||
|
except Exception, e:
|
||||||
|
imp.log.append(unicode(e))
|
||||||
|
# then run the import
|
||||||
try:
|
try:
|
||||||
imp.run()
|
imp.run()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
@ -234,3 +247,32 @@ class UpgradeThread(QThread):
|
||||||
pass
|
pass
|
||||||
self.col.save()
|
self.col.save()
|
||||||
return imp.log
|
return imp.log
|
||||||
|
|
||||||
|
def maybeCopyFromCustomFolder(self, path):
|
||||||
|
folder = os.path.basename(path).replace(".anki", ".media")
|
||||||
|
loc = self.oldprefs.get("mediaLocation")
|
||||||
|
if not loc:
|
||||||
|
# no prefix; user had media next to deck
|
||||||
|
return
|
||||||
|
elif loc == "dropbox":
|
||||||
|
# dropbox no longer exports the folder location; try default
|
||||||
|
if isWin:
|
||||||
|
dll = ctypes.windll.shell32
|
||||||
|
buf = ctypes.create_string_buffer(300)
|
||||||
|
dll.SHGetSpecialFolderPathA(None, buf, 0x0005, False)
|
||||||
|
loc = os.path.join(buf.value, 'Dropbox')
|
||||||
|
else:
|
||||||
|
loc = os.path.expanduser("~/Dropbox")
|
||||||
|
loc = os.path.join(loc, "Public", "Anki")
|
||||||
|
# no media folder in custom location?
|
||||||
|
mfolder = os.path.join(loc, folder)
|
||||||
|
if not os.path.exists(mfolder):
|
||||||
|
return
|
||||||
|
# folder exists; copy data next to the deck. leave a copy in the
|
||||||
|
# custom location so users can revert easily.
|
||||||
|
mdir = self.col.media.dir()
|
||||||
|
for f in os.listdir(mfolder):
|
||||||
|
src = os.path.join(mfolder, f)
|
||||||
|
dst = os.path.join(mdir, f)
|
||||||
|
if not os.path.exists(dst):
|
||||||
|
shutil.copy2(src, dst)
|
||||||
|
|
Loading…
Reference in a new issue