Update size when sending book to Kobo device.

Thiis is to prevent the book from being deleted by the device when it processes new books after the disconnect.
This commit is contained in:
davidfor 2013-01-17 16:19:48 +11:00
parent 39aa2072cb
commit 3b466e69e6

View File

@ -1706,18 +1706,19 @@ class KOBOTOUCH(KOBO):
def upload_books(self, files, names, on_card=None, end_session=True, def upload_books(self, files, names, on_card=None, end_session=True,
metadata=None): metadata=None):
debug_print('KoboTouch:upload_books - %d books'%(len(files))) debug_print('KoboTouch:upload_books - %d books'%(len(files)))
debug_print('KoboTouch:upload_books - files=', files)
result = super(KOBOTOUCH, self).upload_books(files, names, on_card, end_session, metadata) result = super(KOBOTOUCH, self).upload_books(files, names, on_card, end_session, metadata)
# debug_print('KoboTouch:upload_books - result=', result) # debug_print('KoboTouch:upload_books - result=', result)
if self.dbversion >= 53: if self.dbversion >= 53 and prefs['manage_device_metadata'] == 'on_connect':
import sqlite3 as sqlite import sqlite3 as sqlite
try: try:
with closing(sqlite.connect(self.normalize_path(self._main_prefix + with closing(sqlite.connect(self.normalize_path(self._main_prefix +
'.kobo/KoboReader.sqlite'))) as connection: '.kobo/KoboReader.sqlite'))) as connection:
connection.text_factory = lambda x: unicode(x, "utf-8", "ignore") connection.text_factory = lambda x: unicode(x, "utf-8", "ignore")
cursor = connection.cursor() cursor = connection.cursor()
query = "DELETE FROM content WHERE ContentID = ? AND Accessibility = 1 AND IsDownloaded = 'false'" cleanup_query = "DELETE FROM content WHERE ContentID = ? AND Accessibility = 1 AND IsDownloaded = 'false'"
for fname, cycle in result: for fname, cycle in result:
show_debug = self.is_debugging_title(fname) show_debug = self.is_debugging_title(fname)
@ -1726,9 +1727,11 @@ class KOBOTOUCH(KOBO):
debug_print('KoboTouch:upload_books: fname=', fname) debug_print('KoboTouch:upload_books: fname=', fname)
debug_print('KoboTouch:upload_books: contentID=', contentID) debug_print('KoboTouch:upload_books: contentID=', contentID)
t = (contentID,) cleanup_values = (contentID,)
# debug_print('KoboTouch:upload_books: Delete record left if deleted on Touch') # debug_print('KoboTouch:upload_books: Delete record left if deleted on Touch')
cursor.execute(query, t) cursor.execute(cleanup_query, cleanup_values)
self.set_filesize_in_device_database(connection, contentID, fname)
connection.commit() connection.commit()
@ -2183,6 +2186,43 @@ class KOBOTOUCH(KOBO):
connection.commit() connection.commit()
cursor.close() cursor.close()
def set_filesize_in_device_database(self, connection, contentID, fpath):
show_debug = self.is_debugging_title(fpath)
if show_debug:
debug_print('KoboTouch:set_filesize_in_device_database contentID="%s"'%contentID)
test_query = 'SELECT ___FileSize ' \
'FROM content ' \
'WHERE ContentID = ? ' \
' AND ContentType = 6'
test_values = (contentID, )
updatequery = 'UPDATE content ' \
'SET ___FileSize = ? ' \
'WHERE ContentId = ? ' \
'AND ContentType = 6'
cursor = connection.cursor()
cursor.execute(test_query, test_values)
result = cursor.fetchone()
if result is None:
if show_debug:
debug_print(' Did not find a record - new book on device')
elif os.path.exists(fpath):
file_size = os.stat(self.normalize_path(fpath)).st_size
if show_debug:
debug_print(' Found a record - will update - ___FileSize=', result[0], ' file_size=', file_size)
if file_size != int(result[0]):
update_values = (file_size, contentID, )
cursor.execute(updatequery, update_values)
if show_debug:
debug_print(' Size updated.')
connection.commit()
cursor.close()
# debug_print("KoboTouch:set_filesize_in_device_database - end")
def delete_empty_bookshelves(self, connection): def delete_empty_bookshelves(self, connection):
debug_print("KoboTouch:delete_empty_bookshelves - start") debug_print("KoboTouch:delete_empty_bookshelves - start")