From 30b4e5511a843360499472f31b5b31ba494d181b Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:43:56 +0100 Subject: [PATCH] Add a common function to open file paths externally --- novelwriter/common.py | 15 ++++++++++++++- novelwriter/core/project.py | 4 ++-- novelwriter/gui/mainmenu.py | 9 +++------ novelwriter/tools/dictionaries.py | 15 ++++----------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/novelwriter/common.py b/novelwriter/common.py index 2627a1b0..0f61bd89 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -33,8 +33,11 @@ from typing import Any, Literal from pathlib import Path from datetime import datetime from configparser import ConfigParser +from urllib.parse import urljoin +from urllib.request import pathname2url -from PyQt5.QtCore import QCoreApplication +from PyQt5.QtGui import QDesktopServices +from PyQt5.QtCore import QCoreApplication, QUrl from PyQt5.QtWidgets import QWidget, qApp from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout @@ -495,6 +498,16 @@ def getFileSize(path: Path) -> int: return -1 +def openExternalPath(path: Path) -> bool: + """Open a path by passing it to the desktop environment.""" + if Path(path).exists(): + QDesktopServices.openUrl( + QUrl(urljoin("file:", pathname2url(str(path)))) + ) + return True + return False + + # =============================================================================================== # # Other Functions # =============================================================================================== # diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 4cba0c82..f66f4549 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -45,7 +45,7 @@ from novelwriter.core.sessions import NWSessionLog from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter, XMLReadState from novelwriter.core.projectdata import NWProjectData from novelwriter.common import ( - checkStringNone, formatInt, formatTimeStamp, hexToInt, makeFileNameSafe, minmax + checkStringNone, formatInt, formatTimeStamp, getFileSize, hexToInt, makeFileNameSafe, minmax ) if TYPE_CHECKING: # pragma: no cover @@ -420,7 +420,7 @@ class NWProject: timeStamp = formatTimeStamp(time(), fileSafe=True) archName = baseDir / f"{cleanName} {timeStamp}.zip" if self._storage.zipIt(archName, compression=2): - size = formatInt(archName.stat().st_size) + size = formatInt(getFileSize(archName)) if doNotify: SHARED.info( self.tr("Created a backup of your project of size {0}B.").format(size), diff --git a/novelwriter/gui/mainmenu.py b/novelwriter/gui/mainmenu.py index 8c74f270..9238d3f1 100644 --- a/novelwriter/gui/mainmenu.py +++ b/novelwriter/gui/mainmenu.py @@ -27,15 +27,14 @@ import logging from typing import TYPE_CHECKING from pathlib import Path -from urllib.parse import urljoin -from urllib.request import pathname2url -from PyQt5.QtCore import QUrl, pyqtSignal, pyqtSlot from PyQt5.QtGui import QDesktopServices +from PyQt5.QtCore import QUrl, pyqtSignal, pyqtSlot from PyQt5.QtWidgets import QMenuBar, QAction from novelwriter import CONFIG, SHARED from novelwriter.enum import nwDocAction, nwDocInsert, nwWidget +from novelwriter.common import openExternalPath from novelwriter.constants import nwConst, trConst, nwKeyWords, nwLabels, nwUnicode if TYPE_CHECKING: # pragma: no cover @@ -111,9 +110,7 @@ class GuiMainMenu(QMenuBar): def _openUserManualFile(self) -> None: """Open the documentation in PDF format.""" if isinstance(CONFIG.pdfDocs, Path): - QDesktopServices.openUrl( - QUrl(urljoin("file:", pathname2url(str(CONFIG.pdfDocs)))) - ) + openExternalPath(CONFIG.pdfDocs) return @pyqtSlot(str) diff --git a/novelwriter/tools/dictionaries.py b/novelwriter/tools/dictionaries.py index 847925a0..c14bd163 100644 --- a/novelwriter/tools/dictionaries.py +++ b/novelwriter/tools/dictionaries.py @@ -27,19 +27,17 @@ import logging from pathlib import Path from zipfile import ZipFile -from urllib.parse import urljoin -from urllib.request import pathname2url -from PyQt5.QtGui import QCloseEvent, QDesktopServices, QTextCursor -from PyQt5.QtCore import QUrl, pyqtSlot +from PyQt5.QtGui import QCloseEvent, QTextCursor +from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import ( QDialog, QDialogButtonBox, QFileDialog, QFrame, QHBoxLayout, QLabel, QLineEdit, QPlainTextEdit, QPushButton, QVBoxLayout, QWidget, qApp ) from novelwriter import CONFIG, SHARED -from novelwriter.common import formatInt, getFileSize from novelwriter.error import formatException +from novelwriter.common import openExternalPath, formatInt, getFileSize logger = logging.getLogger(__name__) @@ -217,12 +215,7 @@ class GuiDictionaries(QDialog): @pyqtSlot() def _doOpenInstallLocation(self) -> None: """Open the dictionary folder.""" - path = self.inPath.text() - if Path(path).is_dir(): - QDesktopServices.openUrl( - QUrl(urljoin("file:", pathname2url(path))) - ) - else: + if not openExternalPath(Path(self.inPath.text())): SHARED.error("Path not found.") return