View Single Post
Old 07-17-2011, 04:40 PM   #1
siebert
Developer
siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.
 
Posts: 136
Karma: 280
Join Date: Nov 2010
Device: Kindle 3 (Keyboard) 3G / iPad 3 WiFi / Nexus 4 (Android)
Patch: Calibre adds tags to identify ebook formats created by calibre.

Hi,

for ebooks with several formats (in my case mostly epub and mobi) I'd like to know which format contains the original file and which one was created by calibre (normally by converting from the other format).

With the patch below (against 0.8.8) calibre automatically applies a tag named 'Converted <format>' where <format> is replaced by the name of the format (for example 'Converted EPUB'), when a conversion is performed.

If a format is deleted, the corresponding tag will also be removed by calibre.

If a mobi or epub file is added to calibre, the producer metadata field is searched for the string 'calibre'. If the field contains it, the file was created by calibre and the tag is set, too.

Ciao,
Steffen

Code:
diff --git a/src/calibre/ebooks/metadata/epub.py b/src/calibre/ebooks/metadata/epub.py
index d46c160..7165783 100644
--- a/src/calibre/ebooks/metadata/epub.py
+++ b/src/calibre/ebooks/metadata/epub.py
@@ -189,6 +189,13 @@ def get_metadata(stream, extract_cover=True):
     if mi.language is not None and not mi.has_identifier('language'):
         mi.set_identifier('language', mi.language)
     mi.timestamp = None
+    if mi.book_producer and mi.book_producer.find('calibre') != -1:
+        print "mi.book_producer = %s" % mi.book_producer
+        if not mi.tags:
+            mi.tags = ['Converted EPUB']
+        else:
+            if 'Converted EPUB' not in mi.tags:
+                mi.tags.append('Converted EPUB')
     return mi
 
 def get_quick_metadata(stream):
diff --git a/src/calibre/ebooks/mobi/reader.py b/src/calibre/ebooks/mobi/reader.py
index 9c04009..4e2f998 100644
--- a/src/calibre/ebooks/mobi/reader.py
+++ b/src/calibre/ebooks/mobi/reader.py
@@ -103,6 +103,13 @@ class EXTHHeader(object):
             except:
                 pass
         elif id == 108:
+            producer = content.decode(codec, 'ignore')
+            if producer.find("calibre") != -1:
+                if not self.mi.tags:
+                    self.mi.tags = ['Converted MOBI']
+                else:
+                    if 'Converted MOBI' not in self.mi.tags:
+                        self.mi.tags.append('Converted MOBI')
             pass # Producer
         elif id == 113:
             pass # ASIN or UUID
diff --git a/src/calibre/gui2/actions/convert.py b/src/calibre/gui2/actions/convert.py
index 17fa0ad..660c8ff 100644
--- a/src/calibre/gui2/actions/convert.py
+++ b/src/calibre/gui2/actions/convert.py
@@ -174,6 +174,7 @@ class ConvertAction(InterfaceAction):
             with open(temp_files[-1].name, 'rb') as data:
                 self.gui.library_view.model().db.add_format(book_id, \
                     fmt, data, index_is_id=True)
+                self.gui.library_view.model().db.set_tags(book_id, ['Converted %s' % fmt], append=True)
             self.gui.status_bar.show_message(job.description + \
                     (' completed'), 2000)
         finally:
diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py
index 227a225..c571f8b 100644
--- a/src/calibre/gui2/metadata/basic_widgets.py
+++ b/src/calibre/gui2/metadata/basic_widgets.py
@@ -1065,9 +1065,10 @@ class TagsEdit(MultiCompleteLineEdit): # {{{
 
 
     def commit(self, db, id_):
-        self.books_to_refresh |= db.set_tags(
-                id_, self.current_val, notify=False, commit=False,
-                allow_case_change=True)
+        if self.changed:
+            self.books_to_refresh |= db.set_tags(
+                    id_, self.current_val, notify=False, commit=False,
+                    allow_case_change=True)
         return True
 
 # }}}
diff --git a/src/calibre/library/database.py b/src/calibre/library/database.py
index 2138b2f..a9e8a74 100644
--- a/src/calibre/library/database.py
+++ b/src/calibre/library/database.py
@@ -1121,6 +1121,7 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
         id = index if index_is_id else self.id(index)
         self.conn.execute('DELETE FROM data WHERE book=? AND format=?', (id, ext.lower()))
         self.conn.commit()
+        self.unapply_tags(index, ['Converted %s' % ext])
 
     def set(self, row, column, val):
         '''
diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py
index 9d8a27d..cd19ef9 100644
--- a/src/calibre/library/database2.py
+++ b/src/calibre/library/database2.py
@@ -1353,6 +1353,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
             self.refresh_ids([id])
             if notify:
                 self.notify('metadata', [id])
+            self.unapply_tags(index, ['Converted %s' % format])
 
     def clean(self):
         '''

Last edited by siebert; 07-17-2011 at 04:44 PM.
siebert is offline   Reply With Quote