From 7fb8f74c1629dded7cfa8c19c669900af58b47a5 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 8 Oct 2010 17:34:57 +0100 Subject: [PATCH] UUID fixes, plus some debugging information --- src/calibre/gui2/actions/copy_to_library.py | 9 ++++++--- src/calibre/gui2/device.py | 17 +++++++++-------- src/calibre/gui2/library/models.py | 12 ++++++++++-- src/calibre/library/database2.py | 14 +++++++++++++- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/calibre/gui2/actions/copy_to_library.py b/src/calibre/gui2/actions/copy_to_library.py index 513026f757..47f7904841 100644 --- a/src/calibre/gui2/actions/copy_to_library.py +++ b/src/calibre/gui2/actions/copy_to_library.py @@ -18,7 +18,7 @@ from calibre.utils.config import prefs, tweaks class Worker(Thread): - def __init__(self, ids, db, loc, progress, done): + def __init__(self, ids, db, loc, progress, done, delete_after): Thread.__init__(self) self.ids = ids self.processed = set([]) @@ -27,6 +27,7 @@ class Worker(Thread): self.error = None self.progress = progress self.done = done + self.delete_after = delete_after def run(self): try: @@ -68,7 +69,8 @@ class Worker(Thread): self.add_formats(identical_book, paths, newdb, replace=False) if not added: newdb.import_book(mi, paths, notify=False, import_hooks=False, - apply_import_tags=tweaks['add_new_book_tags_when_importing_books']) + apply_import_tags=tweaks['add_new_book_tags_when_importing_books'], + preserve_uuid=self.delete_after) co = self.db.conversion_options(x, 'PIPE') if co is not None: newdb.set_conversion_options(x, 'PIPE', co) @@ -134,7 +136,8 @@ class CopyToLibraryAction(InterfaceAction): self.pd.set_msg(_('Copying') + ' ' + title) self.pd.set_value(idx) - self.worker = Worker(ids, db, loc, Dispatcher(progress), Dispatcher(self.pd.accept)) + self.worker = Worker(ids, db, loc, Dispatcher(progress), + Dispatcher(self.pd.accept), delete_after) self.worker.start() self.pd.exec_() diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 0344a8f21d..bc9f5cf671 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -1413,15 +1413,16 @@ class DeviceMixin(object): # {{{ # Force a reset if the caches are not initialized if reset or not hasattr(self, 'db_book_title_cache'): + # Build a cache (map) of the library, so the search isn't On**2 + self.db_book_title_cache = {} + self.db_book_uuid_cache = {} # It might be possible to get here without having initialized the # library view. In this case, simply give up try: db = self.library_view.model().db except: return False - # Build a cache (map) of the library, so the search isn't On**2 - self.db_book_title_cache = {} - self.db_book_uuid_cache = {} + for id in db.data.iterallids(): mi = db.get_metadata(id, index_is_id=True) title = clean_string(mi.title) @@ -1455,7 +1456,7 @@ class DeviceMixin(object): # {{{ if update_metadata: book.smart_update(self.db_book_uuid_cache[book.uuid], replace_metadata=True) - book.in_library = True + book.in_library = 'UUID' # ensure that the correct application_id is set book.application_id = \ self.db_book_uuid_cache[book.uuid].application_id @@ -1472,7 +1473,7 @@ class DeviceMixin(object): # {{{ if update_metadata: book.smart_update(d['db_ids'][book.application_id], replace_metadata=True) - book.in_library = True + book.in_library = 'APP_ID' continue # Sonys know their db_id independent of the application_id # in the metadata cache. Check that as well. @@ -1480,7 +1481,7 @@ class DeviceMixin(object): # {{{ if update_metadata: book.smart_update(d['db_ids'][book.db_id], replace_metadata=True) - book.in_library = True + book.in_library = 'DB_ID' book.application_id = \ d['db_ids'][book.db_id].application_id continue @@ -1497,14 +1498,14 @@ class DeviceMixin(object): # {{{ if update_metadata: book.smart_update(d['authors'][book_authors], replace_metadata=True) - book.in_library = True + book.in_library = 'AUTHOR' book.application_id = \ d['authors'][book_authors].application_id elif book_authors in d['author_sort']: if update_metadata: book.smart_update(d['author_sort'][book_authors], replace_metadata=True) - book.in_library = True + book.in_library = 'AUTH_SORT' book.application_id = \ d['author_sort'][book_authors].application_id else: diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 53e0982211..2946985342 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -24,7 +24,7 @@ from calibre.library.caches import _match, CONTAINS_MATCH, EQUALS_MATCH, \ REGEXP_MATCH, CoverCache, MetadataBackup from calibre.library.cli import parse_series_string from calibre import strftime, isbytestring, prepare_string_for_xml -from calibre.constants import filesystem_encoding +from calibre.constants import filesystem_encoding, DEBUG from calibre.gui2.library import DEFAULT_SORT def human_readable(size, precision=1): @@ -699,6 +699,10 @@ class BooksModel(QAbstractTableModel): # {{{ if role == Qt.DisplayRole: return QVariant(self.headers[self.column_map[section]]) return NONE + if DEBUG and role == Qt.ToolTipRole and orientation == Qt.Vertical: + col = self.db.field_metadata['uuid']['rec_index'] + return QVariant(_('This book\'s UUID is "{0}"').format(self.db.data[section][col])) + if role == Qt.DisplayRole: # orientation is vertical return QVariant(section+1) return NONE @@ -1206,6 +1210,8 @@ class DeviceBooksModel(BooksModel): # {{{ if tags: tags.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) return QVariant(', '.join(tags)) + elif DEBUG and cname == 'inlibrary': + return QVariant(self.db[self.map[row]].in_library) elif role == Qt.ToolTipRole and index.isValid(): if self.map[row] in self.indices_to_be_deleted(): return QVariant(_('Marked for deletion')) @@ -1227,8 +1233,10 @@ class DeviceBooksModel(BooksModel): # {{{ return NONE def headerData(self, section, orientation, role): - if role == Qt.ToolTipRole: + if role == Qt.ToolTipRole and orientation == Qt.Horizontal: return QVariant(_('The lookup/search name is "{0}"').format(self.column_map[section])) + if DEBUG and role == Qt.ToolTipRole and orientation == Qt.Vertical: + return QVariant(_('This book\'s UUID is "{0}"').format(self.db[self.map[section]].uuid)) if role != Qt.DisplayRole: return NONE if orientation == Qt.Horizontal: diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index f1087b3898..f123dbac79 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1466,6 +1466,16 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if notify: self.notify('metadata', [id]) + def set_uuid(self, id, uuid, notify=True, commit=True): + if uuid: + self.conn.execute('UPDATE books SET uuid=? WHERE id=?', (uuid, id)) + self.data.set(id, self.FIELD_MAP['uuid'], uuid, row_is_id=True) + self.dirtied([id], commit=False) + if commit: + self.conn.commit() + if notify: + self.notify('metadata', [id]) + # Convenience methods for tags_list_editor # Note: we generally do not need to refresh_ids because library_view will # refresh everything. @@ -2129,7 +2139,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): return None, len(ids) def import_book(self, mi, formats, notify=True, import_hooks=True, - apply_import_tags=True): + apply_import_tags=True, preserve_uuid=False): series_index = 1.0 if mi.series_index is None else mi.series_index if apply_import_tags: self._add_newbook_tag(mi) @@ -2152,6 +2162,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if mi.pubdate is None: mi.pubdate = utcnow() self.set_metadata(id, mi, ignore_errors=True) + if preserve_uuid and mi.uuid: + self.set_uuid(id, mi.uuid, commit=False) for path in formats: ext = os.path.splitext(path)[1][1:].lower() if ext == 'opf':