Skip to content

Commit

Permalink
Framework for fetching annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Mar 2, 2010
1 parent c2be1c1 commit 5238aab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/calibre/devices/kindle/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class KINDLE(USBMS):
EBOOK_DIR_CARD_A = 'documents'
DELETE_EXTS = ['.mbp']
SUPPORTS_SUB_DIRS = True
SUPPORTS_ANNOTATIONS = True

WIRELESS_FILE_NAME_PATTERN = re.compile(
r'(?P<title>[^-]+)-asin_(?P<asin>[a-zA-Z\d]{10,})-type_(?P<type>\w{4})-v_(?P<index>\d+).*')
Expand All @@ -60,6 +61,9 @@ def metadata_from_path(cls, path):
'replace')
return mi

def get_annotations(self, path_map):
return {}


class KINDLE2(KINDLE):

Expand Down
28 changes: 25 additions & 3 deletions src/calibre/gui2/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from binascii import unhexlify

from PyQt4.Qt import QMenu, QAction, QActionGroup, QIcon, SIGNAL, QPixmap, \
Qt
Qt, pyqtSignal

from calibre.customize.ui import available_input_formats, available_output_formats, \
device_plugins
Expand Down Expand Up @@ -218,6 +218,16 @@ def books(self, done):
'''Return callable that returns the list of books on device as two booklists'''
return self.create_job(self._books, done, description=_('Get list of books on device'))

def _annotations(self, path_map):
return self.device.get_annotations(path_map)

def annotations(self, path_map, done):
'''Return mapping of ids to annotations. Each annotation is of the
form (type, location_info, content). path_map is a mapping of
ids to paths on the device.'''
return self.create_job(self._annotations, done, args=[path_map],
description=_('Get annotations from device'))

def _sync_booklists(self, booklists):
'''Sync metadata to device'''
self.device.sync_booklists(booklists, end_session=False)
Expand Down Expand Up @@ -298,6 +308,8 @@ def __repr__(self):

class DeviceMenu(QMenu):

fetch_annotations = pyqtSignal()

def __init__(self, parent=None):
QMenu.__init__(self, parent)
self.group = QActionGroup(self)
Expand Down Expand Up @@ -389,10 +401,16 @@ def __init__(self, parent=None):

self.connect(self.group, SIGNAL('triggered(QAction*)'),
self.change_default_action)
self.enable_device_actions(False)
if opts.accounts:
self.addSeparator()
self.addMenu(self.email_to_menu)
self.addSeparator()
annot = self.addAction(_('Fetch annotations (experimental)'))
annot.setEnabled(False)
annot.triggered.connect(lambda x :
self.fetch_annotations.emit())
self.annotation_action = annot
self.enable_device_actions(False)

def change_default_action(self, action):
config['default_send_to_device_action'] = repr(action)
Expand All @@ -409,7 +427,8 @@ def trigger_default(self, *args):
self.action_triggered(action)
break

def enable_device_actions(self, enable, card_prefix=(None, None)):
def enable_device_actions(self, enable, card_prefix=(None, None),
device=None):
for action in self.actions:
if action.dest in ('main:', 'carda:0', 'cardb:0'):
if not enable:
Expand All @@ -428,6 +447,9 @@ def enable_device_actions(self, enable, card_prefix=(None, None)):
else:
action.setEnabled(False)

annot_enable = enable and getattr(device, 'SUPPORTS_ANNOTATIONS', False)
self.annotation_action.setEnabled(annot_enable)


class Emailer(Thread):

Expand Down
24 changes: 23 additions & 1 deletion src/calibre/gui2/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ def create_device_menu(self):
self.dispatch_sync_event)
self.connect(self.action_sync, SIGNAL('triggered(bool)'),
self._sync_menu.trigger_default)
self._sync_menu.fetch_annotations.connect(self.fetch_annotations)

def add_spare_server(self, *args):
self.spare_servers.append(Server(limit=int(config['worker_limit']/2.0)))
Expand Down Expand Up @@ -855,7 +856,9 @@ def device_detected(self, connected):
self.device_manager.device.__class__.get_gui_name()+\
_(' detected.'), 3000)
self.device_connected = True
self._sync_menu.enable_device_actions(True, self.device_manager.device.card_prefix())
self._sync_menu.enable_device_actions(True,
self.device_manager.device.card_prefix(),
self.device_manager.device)
self.location_view.model().device_connected(self.device_manager.device)
else:
self.save_device_view_settings()
Expand Down Expand Up @@ -918,7 +921,26 @@ def metadata_downloaded(self, job):
self.sync_catalogs()
############################################################################

######################### Fetch annotations ################################

def fetch_annotations(self, *args):
#current_device = self.device_manager.device
path_map = {}
# code to calculate path_map
self.device_manager.annotations(Dispatcher(self.annotations_fetched),
path_map)

def annotations_fetched(self, annotation_map):
if not annotation_map: return
from calibre.gui2.dialogs.progress import ProgressDialog
pd = ProgressDialog(_('Adding annotations'),
_('Annotations will be saved in the comments field'),
min=0, max=0, parent=self)
# code to add annotations to database should run in a separate
# thread as it could potentially take a long time
pd.exec_()

############################################################################

################################# Add books ################################

Expand Down

0 comments on commit 5238aab

Please sign in to comment.