From 7b1078ec2826bd0b2954fbeb7ae46403d2da83d7 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Mon, 1 May 2023 13:34:16 +0100 Subject: [PATCH] Add opening the data folder to the Open action. Allow it to have a shortcut. Create the data folder if it doesn't exist. --- src/calibre/gui2/actions/open.py | 11 +++++++++-- src/calibre/gui2/actions/view.py | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/actions/open.py b/src/calibre/gui2/actions/open.py index 31c0b94c00..8998e5f343 100644 --- a/src/calibre/gui2/actions/open.py +++ b/src/calibre/gui2/actions/open.py @@ -5,6 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' +from functools import partial from calibre.gui2.actions import InterfaceAction @@ -12,13 +13,19 @@ from calibre.gui2.actions import InterfaceAction class OpenFolderAction(InterfaceAction): name = 'Open Folder' - action_spec = (_('Open containing folder'), 'document_open.png', + action_spec = (_('Open book folder'), 'document_open.png', _('Open the folder containing the current book\'s files'), _('O')) dont_add_to = frozenset(('context-menu-device',)) action_type = 'current' + action_add_menu = True + action_menu_clone_qaction = _('Open book folder') def genesis(self): - self.qaction.triggered.connect(self.gui.iactions['View'].view_folder) + va = self.gui.iactions['View'].view_folder + self.qaction.triggered.connect(va) + a = self.create_menu_action(self.qaction.menu(), 'show-data-folder', + _('Open book data folder'), icon='document_open.png', shortcut=tuple()) + a.triggered.connect(partial(va, data_folder=True)) def location_selected(self, loc): enabled = loc == 'library' diff --git a/src/calibre/gui2/actions/view.py b/src/calibre/gui2/actions/view.py index db28358fb3..d7cf1d4b16 100644 --- a/src/calibre/gui2/actions/view.py +++ b/src/calibre/gui2/actions/view.py @@ -263,7 +263,7 @@ class ViewAction(InterfaceAction): 'cannot be stopped until complete. Do you wish to continue?' ) % num, show_copy_button=False, skip_dialog_name=skip_dialog_name) - def view_folder(self, *args): + def view_folder(self, *args, **kwargs): rows = self.gui.current_view().selectionModel().selectedRows() if not rows or len(rows) == 0: d = error_dialog(self.gui, _('Cannot open folder'), @@ -272,12 +272,25 @@ class ViewAction(InterfaceAction): return if not self._view_check(len(rows), max_=10, skip_dialog_name='open-folder-many-check'): return + data_folder = kwargs.get('data_folder', False) db = self.gui.current_db for i, row in enumerate(rows): self.gui.extra_files_watcher.watch_book(db.id(row.row())) path = db.abspath(row.row()) if path: - open_local_file(path) + if data_folder: + path = os.path.join(path, DATA_DIR_NAME) + if not os.path.exists(path): + try: + os.mkdir(path) + except Exception as e: + error_dialog(self.gui, _('Failed to create folder'), str(e), show=True) + continue + try: + open_local_file(path) + except Exception as e: + # We shouldn't get here ... + error_dialog(self.gui, _('Cannot open folder'), str(e), show=True) if ismacos and i < len(rows) - 1: time.sleep(0.1) # Finder cannot handle multiple folder opens @@ -289,7 +302,14 @@ class ViewAction(InterfaceAction): def view_data_folder_for_id(self, id_): self.gui.extra_files_watcher.watch_book(id_) path = self.gui.current_db.abspath(id_, index_is_id=True) - open_local_file(os.path.join(path, DATA_DIR_NAME)) + path = os.path.join(path, DATA_DIR_NAME) + if not os.path.exists(path): + try: + os.mkdir(path) + except Exception as e: + error_dialog(self.gui, _('Failed to create folder'), str(e), show=True) + return + open_local_file(path) def view_book(self, triggered): rows = self.gui.current_view().selectionModel().selectedRows()