diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index 8069c637ad..2d6e66afc3 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -543,7 +543,72 @@ class KOBO(USBMS): paths[source_id] = os.path.join(prefix, *(path.split('/'))) return paths + def reset_readstatus(self, connection, oncard): + cursor = connection.cursor() + + # Reset Im_Reading list in the database + if oncard == 'carda': + query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ContentID like \'file:///mnt/sd/%\'' + elif oncard != 'carda' and oncard != 'cardb': + query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ContentID not like \'file:///mnt/sd/%\'' + + try: + cursor.execute (query) + except: + debug_print(' Database Exception: Unable to reset ReadStatus list') + raise + else: + connection.commit() + debug_print(' Commit: Reset ReadStatus list') + + cursor.close() + + def set_readstatus(self, connection, ContentID, ReadStatus): + cursor = connection.cursor() + t = (ContentID,) + cursor.execute('select DateLastRead from Content where BookID is Null and ContentID = ?', t) + result = cursor.fetchone() + if result is None: + datelastread = '1970-01-01T00:00:00' + else: + datelastread = result[0] if result[0] is not None else '1970-01-01T00:00:00' + + t = (ReadStatus,datelastread,ContentID,) + + try: + cursor.execute('update content set ReadStatus=?,FirstTimeReading=\'false\',DateLastRead=? where BookID is Null and ContentID = ?', t) + except: + debug_print(' Database Exception: Unable update ReadStatus') + raise + else: + connection.commit() + debug_print(' Commit: Setting ReadStatus List') + cursor.close() + + def reset_favouritesindex(self, connection, oncard): + # Reset FavouritesIndex list in the database + if oncard == 'carda': + query= 'update content set FavouritesIndex=-1 where BookID is Null and ContentID like \'file:///mnt/sd/%\'' + elif oncard != 'carda' and oncard != 'cardb': + query= 'update content set FavouritesIndex=-1 where BookID is Null and ContentID not like \'file:///mnt/sd/%\'' + + cursor = connection.cursor() + try: + cursor.execute (query) + except: + debug_print('Database Exception: Unable to reset Shortlist list') + raise + else: + connection.commit() + debug_print(' Commit: Reset FavouritesIndex list') + def update_device_database_collections(self, booklists, collections_attributes, oncard): + # Define lists for the ReadStatus + readstatuslist = { + "Im_Reading":1, + "Read":2, + "Closed":3, + } # debug_print('Starting update_device_database_collections', collections_attributes) # Force collections_attributes to be 'tags' as no other is currently supported @@ -562,149 +627,35 @@ class KOBO(USBMS): # return bytestrings if the content cannot the decoded as unicode connection.text_factory = lambda x: unicode(x, "utf-8", "ignore") - cursor = connection.cursor() - - if collections: + + # Need to reset the collections outside the particular loops + # otherwise the last item will not be removed + self.reset_readstatus(connection, oncard) + self.reset_favouritesindex(connection, oncard) + # Process any collections that exist for category, books in collections.items(): - # debug_print (category) - if category == 'Im_Reading': - # Reset Im_Reading list in the database - if oncard == 'carda': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 1 and ContentID like \'file:///mnt/sd/%\'' - elif oncard != 'carda' and oncard != 'cardb': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 1 and ContentID not like \'file:///mnt/sd/%\'' - - try: - cursor.execute (query) - except: - debug_print('Database Exception: Unable to reset Im_Reading list') - raise - else: -# debug_print('Commit: Reset Im_Reading list') - connection.commit() + # This is used to reset the Im_Reading, Read and Closed list + # in the ReadStatus column of the Content table + if category in readstatuslist.keys(): + debug_print("Category: ", category, " id = ", readstatuslist.get(category)) for book in books: -# debug_print('Title:', book.title, 'lpath:', book.path) - if 'Im_Reading' not in book.device_collections: - book.device_collections.append('Im_Reading') + debug_print(' Title:', book.title, 'category: ', category) + if category not in book.device_collections: + book.device_collections.append(category) extension = os.path.splitext(book.path)[1] ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(book.path) ContentID = self.contentid_from_path(book.path, ContentType) - - t = (ContentID,) - cursor.execute('select DateLastRead from Content where BookID is Null and ContentID = ?', t) - result = cursor.fetchone() - if result is None: - datelastread = '1970-01-01T00:00:00' - else: - datelastread = result[0] if result[0] is not None else '1970-01-01T00:00:00' - - t = (datelastread,ContentID,) - - try: - cursor.execute('update content set ReadStatus=1,FirstTimeReading=\'false\',DateLastRead=? where BookID is Null and ContentID = ?', t) - except: - debug_print('Database Exception: Unable create Im_Reading list') - raise - else: - connection.commit() - # debug_print('Database: Commit create Im_Reading list') - if category == 'Read': - # Reset Im_Reading list in the database - if oncard == 'carda': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 2 and ContentID like \'file:///mnt/sd/%\'' - elif oncard != 'carda' and oncard != 'cardb': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 2 and ContentID not like \'file:///mnt/sd/%\'' - - try: - cursor.execute (query) - except: - debug_print('Database Exception: Unable to reset Im_Reading list') - raise - else: -# debug_print('Commit: Reset Im_Reading list') - connection.commit() - - for book in books: -# debug_print('Title:', book.title, 'lpath:', book.path) - if 'Read' not in book.device_collections: - book.device_collections.append('Read') - - extension = os.path.splitext(book.path)[1] - ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(book.path) - - ContentID = self.contentid_from_path(book.path, ContentType) -# datelastread = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()) - - t = (ContentID,) - - try: - cursor.execute('update content set ReadStatus=2,FirstTimeReading=\'true\' where BookID is Null and ContentID = ?', t) - except: - debug_print('Database Exception: Unable set book as Finished') - raise - else: - connection.commit() -# debug_print('Database: Commit set ReadStatus as Finished') - if category == 'Closed': - # Reset Im_Reading list in the database - if oncard == 'carda': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 3 and ContentID like \'file:///mnt/sd/%\'' - elif oncard != 'carda' and oncard != 'cardb': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ReadStatus = 3 and ContentID not like \'file:///mnt/sd/%\'' - - try: - cursor.execute (query) - except: - debug_print('Database Exception: Unable to reset Closed list') - raise - else: -# debug_print('Commit: Reset Closed list') - connection.commit() - - for book in books: -# debug_print('Title:', book.title, 'lpath:', book.path) - if 'Closed' not in book.device_collections: - book.device_collections.append('Closed') - - extension = os.path.splitext(book.path)[1] - ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(book.path) - - ContentID = self.contentid_from_path(book.path, ContentType) -# datelastread = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()) - - t = (ContentID,) - - try: - cursor.execute('update content set ReadStatus=3,FirstTimeReading=\'true\' where BookID is Null and ContentID = ?', t) - except: - debug_print('Database Exception: Unable set book as Closed') - raise - else: - connection.commit() -# debug_print('Database: Commit set ReadStatus as Closed') + self.set_readstatus(connection, ContentID, readstatuslist.get(category)) if category == 'Shortlist': - # Reset FavouritesIndex list in the database - if oncard == 'carda': - query= 'update content set FavouritesIndex=-1 where BookID is Null and ContentID like \'file:///mnt/sd/%\'' - elif oncard != 'carda' and oncard != 'cardb': - query= 'update content set FavouritesIndex=-1 where BookID is Null and ContentID not like \'file:///mnt/sd/%\'' - - try: - cursor.execute (query) - except: - debug_print('Database Exception: Unable to reset Shortlist list') - raise - else: -# debug_print('Commit: Reset Shortlist list') - connection.commit() - + debug_print("Category: ", category) + cursor = connection.cursor() for book in books: -# debug_print('Title:', book.title, 'lpath:', book.path) + debug_print(' Title:', book.title, 'category: ', category) if 'Shortlist' not in book.device_collections: book.device_collections.append('Shortlist') # debug_print ("Shortlist found for: ", book.title) @@ -727,23 +678,11 @@ class KOBO(USBMS): else: # No collections # Since no collections exist the ReadStatus needs to be reset to 0 (Unread) - print "Reseting ReadStatus to 0" - # Reset Im_Reading list in the database - if oncard == 'carda': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ContentID like \'file:///mnt/sd/%\'' - elif oncard != 'carda' and oncard != 'cardb': - query= 'update content set ReadStatus=0, FirstTimeReading = \'true\' where BookID is Null and ContentID not like \'file:///mnt/sd/%\'' + debug_print("No Collections - reseting ReadStatus to 0") + self.reset_readstatus(connection, oncard) + debug_print("No Collections - reseting FavouritesIndex") + self.reset_favouritesindex(connection, oncard) - try: - cursor.execute (query) - except: - debug_print('Database Exception: Unable to reset Im_Reading list') - raise - else: -# debug_print('Commit: Reset Im_Reading list') - connection.commit() - - cursor.close() connection.close() # debug_print('Finished update_device_database_collections', collections_attributes)