From eb205aee6f8eb67fbd26ffef7fb660fe6b4189b2 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Thu, 3 Jun 2010 19:25:23 +0100 Subject: [PATCH] A few changes to improve robustness. Also some cosmetic changes. --- src/calibre/gui2/dialogs/tag_list_editor.py | 9 ++++++--- src/calibre/gui2/tag_view.py | 18 ++++++++++++++---- src/calibre/library/custom_columns.py | 6 +++--- src/calibre/library/database2.py | 8 ++++++-- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/calibre/gui2/dialogs/tag_list_editor.py b/src/calibre/gui2/dialogs/tag_list_editor.py index fa05518992..1ec80f4b4a 100644 --- a/src/calibre/gui2/dialogs/tag_list_editor.py +++ b/src/calibre/gui2/dialogs/tag_list_editor.py @@ -31,9 +31,12 @@ class TagListEditor(QDialog, Ui_TagListEditor): result = db.get_publishers_with_ids() compare = (lambda x,y:cmp(x.lower(), y.lower())) else: # should be a custom field - self.cc_label = db.field_metadata[category]['label'] - print 'here', self.cc_label - result = self.db.get_custom_items_with_ids(label=self.cc_label) + self.cc_label = None + if category in db.field_metadata: + self.cc_label = db.field_metadata[category]['label'] + result = self.db.get_custom_items_with_ids(label=self.cc_label) + else: + result = [] compare = (lambda x,y:cmp(x.lower(), y.lower())) for k,v in result: diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index e9e5228dfc..49ed03945f 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -135,8 +135,12 @@ class TagsView(QTreeView): # {{{ if item.type == TagTreeItem.CATEGORY: category = unicode(item.name.toString()) key = item.category_key + # Verify that we are working with a field that we know something about + if key not in self.db.field_metadata: + return True + self.context_menu = QMenu(self) - # If the user right-clicked on a tag/series/publisher, then offer + # If the user right-clicked on an editable item, then offer # the possibility of renaming that item if tag_name and \ (key in ['authors', 'tags', 'series', 'publisher', 'search'] or \ @@ -378,7 +382,7 @@ class TagsModel(QAbstractItemModel): # {{{ if category in data: # They should always be there, but ... # make a map of sets of names per category for duplicate # checking when editing - self.category_items[category] = [tag.name for tag in data[category]] + self.category_items[category] = set([tag.name for tag in data[category]]) self.row_map.append(category) self.categories.append(tb_categories[category]['name']) @@ -425,13 +429,16 @@ class TagsModel(QAbstractItemModel): # {{{ error_dialog(self.tags_view, _('Item is blank'), _('An item cannot be set to nothing. Delete it instead.')).exec_() return False - item = index.internalPointer() key = item.parent.category_key + # make certain we know about the category + if key not in self.db.field_metadata: + return if val in self.category_items[key]: error_dialog(self.tags_view, 'Duplicate item', _('The name %s is already used.')%val).exec_() return False + oldval = item.tag.name if key == 'search': saved_searches.rename(unicode(item.data(role).toString()), val) self.tags_view.search_item_renamed.emit() @@ -449,7 +456,10 @@ class TagsModel(QAbstractItemModel): # {{{ label=self.db.field_metadata[key]['label']) self.tags_view.tag_item_renamed.emit() item.tag.name = val - self.refresh() + self.dataChanged.emit(index, index) + # replace the old value in the duplicate detection map with the new one + self.category_items[key].discard(oldval) + self.category_items[key].add(val) return True def headerData(self, *args): diff --git a/src/calibre/library/custom_columns.py b/src/calibre/library/custom_columns.py index 64a261d935..4d2c8970b6 100644 --- a/src/calibre/library/custom_columns.py +++ b/src/calibre/library/custom_columns.py @@ -177,7 +177,7 @@ class CustomColumns(object): data = self.custom_column_label_map[label] if num is not None: data = self.custom_column_num_map[num] - table, lt = self.custom_table_names(data['num']) + table,lt = self.custom_table_names(data['num']) if not data['normalized']: return [] ans = self.conn.get('SELECT id, value FROM %s'%table) @@ -189,7 +189,7 @@ class CustomColumns(object): data = self.custom_column_label_map[label] if num is not None: data = self.custom_column_num_map[num] - table, lt = self.custom_table_names(data['num']) + table,lt = self.custom_table_names(data['num']) self.conn.execute('UPDATE %s SET value=? WHERE id=?'%table, (new_name, id)) self.conn.commit() @@ -199,7 +199,7 @@ class CustomColumns(object): data = self.custom_column_label_map[label] if num is not None: data = self.custom_column_num_map[num] - table, lt = self.custom_table_names(data['num']) + table,lt = self.custom_table_names(data['num']) self.conn.execute('DELETE FROM %s WHERE value=?'%lt, (id,)) self.conn.execute('DELETE FROM %s WHERE id=?'%table, (id,)) self.conn.commit() diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 860859061c..e7bb5c2060 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -986,6 +986,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): 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. def get_tags_with_ids(self): result = self.conn.get('SELECT id,name FROM tags') if not result: @@ -1017,11 +1019,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): def delete_series_using_id(self, id): if id: books = self.conn.get('SELECT book from books_series_link WHERE series=?', (id,)) - for (book_id,) in books: - self.conn.execute('UPDATE books SET series_index=1.0 WHERE id=?', (book_id,)) self.conn.execute('DELETE FROM books_series_link WHERE series=?', (id,)) self.conn.execute('DELETE FROM series WHERE id=?', (id,)) self.conn.commit() + for (book_id,) in books: + self.conn.execute('UPDATE books SET series_index=1.0 WHERE id=?', (book_id,)) def get_publishers_with_ids(self): result = self.conn.get('SELECT id,name FROM publishers') @@ -1040,6 +1042,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.conn.execute('DELETE FROM publishers WHERE id=?', (id,)) self.conn.commit() + # There is no editor for author, so we do not need get_authors_with_ids or + # delete_author_using_id. def rename_author(self, id, new_name): if id: # Make sure that any commas in new_name are changed to '|'!