Changing the database path should use an already existing database at the new location if possible.

This commit is contained in:
Kovid Goyal 2007-12-15 18:53:29 +00:00
parent 2a2f2ff2b7
commit 3d6919d73a

View File

@ -15,6 +15,7 @@
from libprs500.gui2.update import CheckForUpdates from libprs500.gui2.update import CheckForUpdates
from libprs500 import iswindows from libprs500 import iswindows
from libprs500 import isosx from libprs500 import isosx
from libprs500.library.database import LibraryDatabase
import os, sys, textwrap, cStringIO, collections, traceback, shutil import os, sys, textwrap, cStringIO, collections, traceback, shutil
@ -653,15 +654,25 @@ class Main(MainWindow, Ui_MainWindow):
if os.path.dirname(self.database_path) != d.database_location: if os.path.dirname(self.database_path) != d.database_location:
try: try:
newloc = os.path.join(d.database_location, os.path.basename(self.database_path)) newloc = os.path.join(d.database_location, os.path.basename(self.database_path))
dest = open(newloc, 'wb') if not os.path.exists(newloc):
self.status_bar.showMessage('Copying database to '+newloc) dest = open(newloc, 'wb')
self.setCursor(Qt.BusyCursor) self.status_bar.showMessage('Copying database to '+newloc)
self.library_view.setEnabled(False) self.setCursor(Qt.BusyCursor)
self.library_view.close() self.library_view.setEnabled(False)
src = open(self.database_path, 'rb') self.library_view.close()
shutil.copyfileobj(src, dest) src = open(self.database_path, 'rb')
src.close() shutil.copyfileobj(src, dest)
dest.close() src.close()
dest.close()
os.unlink(self.database_path)
else:
try:
db = LibraryDatabase(newloc)
db.close()
except Exception, err:
d = error_dialog(self, _('Invalid database'),
_('<p>An invalid database already exists at %s, delete it before trying to move the existing database.<br>Error: %s')%(newloc, str(err)))
newloc = self.database_path
self.database_path = newloc self.database_path = newloc
settings = QSettings() settings = QSettings()
settings.setValue("database path", QVariant(self.database_path)) settings.setValue("database path", QVariant(self.database_path))