From 88a9ce16df8947c364ef1cf7118bb39f5dc719e3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Jun 2015 14:41:44 +0530 Subject: [PATCH] Edit Book: Allow drag and drop of files onto dock icon on OS X --- src/calibre/gui2/tweak_book/boss.py | 4 ++++ src/calibre/gui2/tweak_book/main.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 4760619fd9..5bbd9f38c2 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -242,6 +242,10 @@ class Boss(QObject): :param edit_file: The name of a file inside the newly opened book to start editing. Can also be a list of names. ''' + if isinstance(path, (list, tuple)) and path: + # Can happen from an file_event_hook on OS X when drag and dropping + # onto the icon in the dock or using open -a + path = path[-1] if not self._check_before_open(): return if not hasattr(path, 'rpartition'): diff --git a/src/calibre/gui2/tweak_book/main.py b/src/calibre/gui2/tweak_book/main.py index 7b269f7eca..044b805bbb 100644 --- a/src/calibre/gui2/tweak_book/main.py +++ b/src/calibre/gui2/tweak_book/main.py @@ -25,6 +25,14 @@ files inside the book which will be opened for editing automatically. setup_gui_option_parser(parser) return parser +class EventAccumulator(object): + + def __init__(self): + self.events = [] + + def __call__(self, ev): + self.events.append(ev) + def gui_main(path=None, notify=None): _run(['ebook-edit', path], notify=notify) @@ -55,6 +63,7 @@ def _run(args, notify=None): decouple('edit-book-') override = 'calibre-edit-book' if islinux else None app = Application(args, override_program_name=override, color_prefs=tprefs) + app.file_event_hook = EventAccumulator() app.load_builtin_fonts() app.setWindowIcon(QIcon(I('tweak.png'))) Application.setOrganizationName(ORG_NAME) @@ -64,6 +73,11 @@ def _run(args, notify=None): main.show() if len(args) > 1: main.boss.open_book(args[1], edit_file=args[2:], clear_notify_data=False) + else: + for path in reversed(app.file_event_hook.events): + main.boss.open_book(path) + break + app.file_event_hook = main.boss.open_book app.exec_() # Ensure that the parse worker has quit so that temp files can be deleted # on windows