From 4c96f6983df6e48a5ae153b32ee9caab9472b96b Mon Sep 17 00:00:00 2001 From: Manish Talreja Date: Fri, 30 Dec 2011 20:09:40 -0500 Subject: [PATCH 1/2] Added support for page number file upload for Kindle 4 Touch and Non Touch versions --- src/calibre/devices/kindle/driver.py | 57 ++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 78b4368950..ef2c49b1f9 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -284,11 +284,11 @@ class KINDLE(USBMS): class KINDLE2(KINDLE): - name = 'Kindle 2/3 Device Interface' - description = _('Communicate with the Kindle 2/3 eBook reader.') + name = 'Kindle 2/3/4 Device Interface' + description = _('Communicate with the Kindle 2/3/4 eBook reader.') FORMATS = KINDLE.FORMATS + ['pdf', 'azw4', 'pobi'] - DELETE_EXTS = KINDLE.DELETE_EXTS + DELETE_EXTS = KINDLE.DELETE_EXTS + ['.mbp1', '.mbs'] PRODUCT_ID = [0x0002, 0x0004] BCD = [0x0100] @@ -307,13 +307,19 @@ class KINDLE2(KINDLE): 'generator will produce pages that correspond better to a printed book. ' 'However, this method is slower and will slow down sending files ' 'to the Kindle.'), + _('Use the sidecar (.sdr) folder for the page number file (Kindle 4)') + + ':::' + + _('Enable this for the Kindle 4 Touch/Non Touch only. ' + ), ] EXTRA_CUSTOMIZATION_DEFAULT = [ True, False, + False, ] OPT_APNX = 0 OPT_APNX_ACCURATE = 1 + OPT_APNX_SIDECAR = 2 def books(self, oncard=None, end_session=True): bl = USBMS.books(self, oncard=oncard, end_session=end_session) @@ -346,6 +352,43 @@ class KINDLE2(KINDLE): h = hashlib.sha1(path).hexdigest() if h in path_map: book.device_collections = list(sorted(path_map[h])) + + + def delete_books(self, paths, end_session=True): + opts = self.settings() + print('KINDLE2: deleting %d books'%(len(paths))) + for i, path in enumerate(paths): + self.report_progress((i+1) / float(len(paths)), _('Removing books from device...')) + path = self.normalize_path(path) + if os.path.exists(path): + # Delete the ebook + os.unlink(path) + + filepath = os.path.splitext(path)[0] + + if (opts.extra_customization[self.OPT_APNX_SIDECAR]): + filename = os.path.splitext(os.path.basename(path))[0] + sidecarpath = os.path.join(os.path.dirname(filepath), filename + ".sdr") + print 'SDR delete debug: Filepath= {0} \nSDR debug:Filename = {1}\nSDR debug: SDR Folder={2}'.format(filepath, filename, sidecarpath) + + for ext in self.DELETE_EXTS: + print 'SDR delete debug: extension {0}'.format(ext) + if os.path.exists(filepath + ext): + os.unlink(filepath + ext) + if os.path.exists(path + ext): + os.unlink(path + ext) + if os.path.exists(os.path.join(sidecarpath, filename + ext)): + os.unlink(os.path.join(sidecarpath, filename + ext)) + + if self.SUPPORTS_SUB_DIRS: + try: + if (os.path.exists(sidecarpath)): + os.removedirs(sidecarpath) + os.removedirs(os.path.dirname(path)) + except: + pass + self.report_progress(1.0, _('Removing books from device...')) + print('KINDLE2: finished deleting %d books'%(len(paths))) def upload_cover(self, path, filename, metadata, filepath): ''' @@ -357,6 +400,14 @@ class KINDLE2(KINDLE): if os.path.splitext(filepath.lower())[1] not in ('.azw', '.mobi', '.prc'): return + + if (opts.extra_customization[self.OPT_APNX_SIDECAR]): + path = os.path.join(os.path.dirname(filepath), filename+".sdr") + print 'SDR debug: Filepath= {0} \nSDR debug:Filename = {1}\nSDR debug: SDR Folder={2}'.format(filepath, filename, path) + + if not os.path.exists(path): + os.makedirs(path) + apnx_path = '%s.apnx' % os.path.join(path, filename) apnx_builder = APNXBuilder() From e11f46abc352fe563b82082e6f8310e1d1dc37c8 Mon Sep 17 00:00:00 2001 From: Manish Talreja Date: Fri, 30 Dec 2011 23:26:33 -0500 Subject: [PATCH 2/2] Added support for page number file upload for Kindle 4 Touch and Non Touch versions --- src/calibre/devices/kindle/driver.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 5f407cd53f..a7c619540c 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -307,19 +307,13 @@ class KINDLE2(KINDLE): 'generator will produce pages that correspond better to a printed book. ' 'However, this method is slower and will slow down sending files ' 'to the Kindle.'), - _('Use the sidecar (.sdr) folder for the page number file (Kindle 4)') + - ':::' + - _('Enable this for the Kindle 4 Touch/Non Touch only. ' - ), ] EXTRA_CUSTOMIZATION_DEFAULT = [ True, False, - False, ] OPT_APNX = 0 OPT_APNX_ACCURATE = 1 - OPT_APNX_SIDECAR = 2 def books(self, oncard=None, end_session=True): bl = USBMS.books(self, oncard=oncard, end_session=end_session) @@ -353,7 +347,13 @@ class KINDLE2(KINDLE): if h in path_map: book.device_collections = list(sorted(path_map[h])) - + # Detect if the product family needs .apnx files uploaded to sidecar folder + def post_open_callback(self): + product_id = self.device_being_opened[1] + self.sidecar_apnx = ((product_id == 0x4) | (product_id == 0x6)) # 4 for for Kindle 4 and 6 for Kindle Fire + + # Override the deletion of books. Delete files from the sidecar folder. This code is very similar + # to the USBMS delete books def delete_books(self, paths, end_session=True): opts = self.settings() for i, path in enumerate(paths): @@ -365,7 +365,8 @@ class KINDLE2(KINDLE): filepath = os.path.splitext(path)[0] - if (opts.extra_customization[self.OPT_APNX_SIDECAR]): + # If the Kindle needs a sidecar folder, build the path in a variable + if (self.sidecar_apnx): filename = os.path.splitext(os.path.basename(path))[0] sidecarpath = os.path.join(os.path.dirname(filepath), filename + ".sdr") @@ -374,11 +375,13 @@ class KINDLE2(KINDLE): os.unlink(filepath + ext) if os.path.exists(path + ext): os.unlink(path + ext) + # Delete files in the sidecar folder as well if os.path.exists(os.path.join(sidecarpath, filename + ext)): os.unlink(os.path.join(sidecarpath, filename + ext)) if self.SUPPORTS_SUB_DIRS: try: + # Remove the sidecar folder if it exists if (os.path.exists(sidecarpath)): os.removedirs(sidecarpath) os.removedirs(os.path.dirname(path)) @@ -397,12 +400,12 @@ class KINDLE2(KINDLE): if os.path.splitext(filepath.lower())[1] not in ('.azw', '.mobi', '.prc'): return - if (opts.extra_customization[self.OPT_APNX_SIDECAR]): + # Create the sidecar folder if necessary + if (self.sidecar_apnx): path = os.path.join(os.path.dirname(filepath), filename+".sdr") if not os.path.exists(path): os.makedirs(path) - apnx_path = '%s.apnx' % os.path.join(path, filename) apnx_builder = APNXBuilder()