Add collections to the smartdevice driver

This commit is contained in:
Charles Haley 2012-08-06 18:52:00 +02:00
parent d46891a82b
commit 1f39af8010

View File

@ -17,7 +17,7 @@ from calibre.constants import numeric_version, DEBUG
from calibre.devices.errors import (OpenFailed, ControlError, TimeoutError, from calibre.devices.errors import (OpenFailed, ControlError, TimeoutError,
InitialConnectionError) InitialConnectionError)
from calibre.devices.interface import DevicePlugin from calibre.devices.interface import DevicePlugin
from calibre.devices.usbms.books import Book, BookList from calibre.devices.usbms.books import Book, CollectionsBookList
from calibre.devices.usbms.deviceconfig import DeviceConfig from calibre.devices.usbms.deviceconfig import DeviceConfig
from calibre.devices.usbms.driver import USBMS from calibre.devices.usbms.driver import USBMS
from calibre.ebooks import BOOK_EXTENSIONS from calibre.ebooks import BOOK_EXTENSIONS
@ -107,8 +107,18 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
} }
reverse_opcodes = dict([(v, k) for k,v in opcodes.iteritems()]) reverse_opcodes = dict([(v, k) for k,v in opcodes.iteritems()])
ALL_BY_TITLE = _('All by title')
ALL_BY_AUTHOR = _('All by author')
EXTRA_CUSTOMIZATION_MESSAGE = [ EXTRA_CUSTOMIZATION_MESSAGE = [
_('Comma separated list of metadata fields '
'to turn into collections on the device. Possibilities include: ')+\
'series, tags, authors' +\
_('. Two special collections are available: %(abt)s:%(abtv)s and %(aba)s:%(abav)s. Add '
'these values to the list to enable them. The collections will be '
'given the name provided after the ":" character.')%dict(
abt='abt', abtv=ALL_BY_TITLE, aba='aba', abav=ALL_BY_AUTHOR),
'',
_('Enable connections at startup') + ':::<p>' + _('Enable connections at startup') + ':::<p>' +
_('Check this box to allow connections when calibre starts') + '</p>', _('Check this box to allow connections when calibre starts') + '</p>',
'', '',
@ -124,6 +134,8 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
_('Check this box if requested when reporting problems') + '</p>', _('Check this box if requested when reporting problems') + '</p>',
] ]
EXTRA_CUSTOMIZATION_DEFAULT = [ EXTRA_CUSTOMIZATION_DEFAULT = [
'tags, series',
'',
False, False,
'', '',
'', '',
@ -131,11 +143,12 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
False, '9090', False, '9090',
False, False,
] ]
OPT_AUTOSTART = 0 OPT_COLLECTIONS = 0
OPT_PASSWORD = 2 OPT_AUTOSTART = 2
OPT_USE_PORT = 4 OPT_PASSWORD = 4
OPT_PORT_NUMBER = 5 OPT_USE_PORT = 6
OPT_EXTRA_DEBUG = 6 OPT_PORT_NUMBER = 7
OPT_EXTRA_DEBUG = 8
def __init__(self, path): def __init__(self, path):
self.sync_lock = threading.RLock() self.sync_lock = threading.RLock()
@ -659,9 +672,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
def books(self, oncard=None, end_session=True): def books(self, oncard=None, end_session=True):
self._debug(oncard) self._debug(oncard)
if oncard is not None: if oncard is not None:
return BookList(None, None, None) return CollectionsBookList(None, None, None)
opcode, result = self._call_client('GET_BOOK_COUNT', {}) opcode, result = self._call_client('GET_BOOK_COUNT', {})
bl = BookList(None, self.PREFIX, self.settings) bl = CollectionsBookList(None, self.PREFIX, self.settings)
if opcode == 'OK': if opcode == 'OK':
count = result['count'] count = result['count']
for i in range(0, count): for i in range(0, count):
@ -681,10 +694,21 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
@synchronous('sync_lock') @synchronous('sync_lock')
def sync_booklists(self, booklists, end_session=True): def sync_booklists(self, booklists, end_session=True):
self._debug() self._debug()
collections = [x.strip() for x in
self.settings().extra_customization[self.OPT_COLLECTIONS].split(',')]
collections = booklists[0].get_collections(collections)
coldict = {}
for k,v in collections.iteritems():
lpaths = []
for book in v:
lpaths.append(book.lpath)
coldict[k] = lpaths
self._debug(coldict)
# If we ever do device_db plugboards, this is where it will go. We will # If we ever do device_db plugboards, this is where it will go. We will
# probably need to send two booklists, one with calibre's data that is # probably need to send two booklists, one with calibre's data that is
# given back by "books", and one that has been plugboarded. # given back by "books", and one that has been plugboarded.
self._call_client('SEND_BOOKLISTS', { 'count': len(booklists[0]) } ) self._call_client('SEND_BOOKLISTS', { 'count': len(booklists[0]),
'collections': coldict} )
for i,book in enumerate(booklists[0]): for i,book in enumerate(booklists[0]):
if not self._metadata_already_on_device(book): if not self._metadata_already_on_device(book):
self._set_known_metadata(book) self._set_known_metadata(book)