Wireless driver: Always replace file when resending a previously sent book to the device, even if the title/author have changed.

This commit is contained in:
Kovid Goyal 2013-01-15 15:31:30 +05:30
commit 2f3dde257e

View File

@ -300,19 +300,21 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
'particular IP address. The driver will listen only on the ' 'particular IP address. The driver will listen only on the '
'entered address, and this address will be the one advertized ' 'entered address, and this address will be the one advertized '
'over mDNS (bonjour).') + '</p>', 'over mDNS (bonjour).') + '</p>',
_('Replace books with the same calibre identifier') + ':::<p>' +
_('Use this option to overwrite a book on the device if that book '
'has the same calibre identifier as the book being sent. The file name of the '
'book will not change even if the save template produces a '
'different result. Using this option in most cases prevents '
'having multiple copies of a book on the device.') + '</p>',
] ]
EXTRA_CUSTOMIZATION_DEFAULT = [ EXTRA_CUSTOMIZATION_DEFAULT = [
False, False, '',
'', '', '',
'',
'',
False, '9090', False, '9090',
False, False, '',
'', '', '',
'', True, '',
'', True
True,
''
] ]
OPT_AUTOSTART = 0 OPT_AUTOSTART = 0
OPT_PASSWORD = 2 OPT_PASSWORD = 2
@ -322,6 +324,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
OPT_COLLECTIONS = 8 OPT_COLLECTIONS = 8
OPT_AUTODISCONNECT = 10 OPT_AUTODISCONNECT = 10
OPT_FORCE_IP_ADDRESS = 11 OPT_FORCE_IP_ADDRESS = 11
OPT_OVERWRITE_BOOKS_UUID = 12
def __init__(self, path): def __init__(self, path):
@ -386,8 +389,14 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
ext = os.path.splitext(fname)[1] ext = os.path.splitext(fname)[1]
try: try:
# If we have already seen this book's UUID, use the existing path
if self.settings().extra_customization[self.OPT_OVERWRITE_BOOKS_UUID]:
existing_book = self._uuid_already_on_device(mdata.uuid)
if existing_book and existing_book.lpath:
return existing_book.lpath
# If the device asked for it, try to use the UUID as the file name. # If the device asked for it, try to use the UUID as the file name.
# Fall back to the template if the UUID doesn't exist. # Fall back to the ch if the UUID doesn't exist.
if self.client_wants_uuid_file_names and mdata.uuid: if self.client_wants_uuid_file_names and mdata.uuid:
return (mdata.uuid + ext) return (mdata.uuid + ext)
except: except:
@ -679,12 +688,20 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
return not v_thumb or v_thumb[1] == b_thumb[1] return not v_thumb or v_thumb[1] == b_thumb[1]
return False return False
def _uuid_already_on_device(self, uuid):
return self.known_uuids.get(uuid, None)
def _set_known_metadata(self, book, remove=False): def _set_known_metadata(self, book, remove=False):
lpath = book.lpath lpath = book.lpath
uuid = book.get('uuid', None)
if remove: if remove:
self.known_metadata.pop(lpath, None) self.known_metadata.pop(lpath, None)
if uuid:
self.known_uuids.pop(uuid, None)
else: else:
self.known_metadata[lpath] = book.deepcopy() new_book = self.known_metadata[lpath] = book.deepcopy()
if uuid:
self.known_uuids[uuid] = new_book
def _close_device_socket(self): def _close_device_socket(self):
if self.device_socket is not None: if self.device_socket is not None:
@ -1097,6 +1114,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
for i, infile in enumerate(files): for i, infile in enumerate(files):
mdata, fname = metadata.next(), names.next() mdata, fname = metadata.next(), names.next()
lpath = self._create_upload_path(mdata, fname, create_dirs=False) lpath = self._create_upload_path(mdata, fname, create_dirs=False)
self._debug('lpath', lpath)
if not hasattr(infile, 'read'): if not hasattr(infile, 'read'):
infile = USBMS.normalize_path(infile) infile = USBMS.normalize_path(infile)
book = SDBook(self.PREFIX, lpath, other=mdata) book = SDBook(self.PREFIX, lpath, other=mdata)
@ -1258,6 +1276,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self.device_socket = None self.device_socket = None
self.json_codec = JsonCodec() self.json_codec = JsonCodec()
self.known_metadata = {} self.known_metadata = {}
self.known_uuids = {}
self.debug_time = time.time() self.debug_time = time.time()
self.debug_start_time = time.time() self.debug_start_time = time.time()
self.max_book_packet_len = 0 self.max_book_packet_len = 0