Skip to content

Commit

Permalink
Add a tool to download spell check dictionaries (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored Nov 20, 2023
2 parents c585c87 + d741a75 commit afc0451
Show file tree
Hide file tree
Showing 12 changed files with 501 additions and 24 deletions.
8 changes: 8 additions & 0 deletions novelwriter/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ def makeFileNameSafe(text: str) -> str:
return "".join(c for c in text if c.isalnum() or c in allowed)


def getFileSize(path: Path) -> int:
"""Return the size of a file."""
try:
return path.stat().st_size
except Exception:
return -1


# =============================================================================================== #
# Other Functions
# =============================================================================================== #
Expand Down
2 changes: 2 additions & 0 deletions novelwriter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def __init__(self) -> None:
# Other System Info
self.hostName = QSysInfo.machineHostName()
self.kernelVer = QSysInfo.kernelVersion()
self.isDebug = False

# Packages
self.hasEnchant = False # The pyenchant package
Expand Down Expand Up @@ -484,6 +485,7 @@ def initConfig(self, confPath: str | Path | None = None,

self._recentObj.loadCache()
self._checkOptionalPackages()
self.isDebug = logger.getEffectiveLevel() == logging.DEBUG

logger.debug("Config instance initialised")

Expand Down
3 changes: 3 additions & 0 deletions novelwriter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class nwConst:
URL_HELP = "https://github.com/vkbo/novelWriter/discussions"
URL_RELEASE = "https://github.com/vkbo/novelWriter/releases/latest"

# Requests
USER_AGENT = "Mozilla/5.0 (compatible; novelWriter (Python))"

# Gui Settings
STATUS_MSG_TIMEOUT = 15000 # milliseconds

Expand Down
39 changes: 25 additions & 14 deletions novelwriter/dialogs/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
from datetime import datetime
from urllib.request import Request, urlopen

from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCloseEvent, QCursor
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtWidgets import (
qApp, QDialog, QHBoxLayout, QVBoxLayout, QDialogButtonBox, QLabel
QWidget, qApp, QDialog, QHBoxLayout, QVBoxLayout, QDialogButtonBox, QLabel
)

from novelwriter import CONFIG, SHARED, __version__, __date__
Expand All @@ -44,8 +44,8 @@

class GuiUpdates(QDialog):

def __init__(self, mainGui):
super().__init__(parent=mainGui)
def __init__(self, parent: QWidget) -> None:
super().__init__(parent=parent)

logger.debug("Create: GuiUpdates")
self.setObjectName("GuiUpdates")
Expand Down Expand Up @@ -94,8 +94,8 @@ def __init__(self, mainGui):
self.latestLabel.setFont(hFont)

# Buttons
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self._doClose)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Close)
self.buttonBox.rejected.connect(self._doClose)

# Assemble
self.innerBox = QHBoxLayout()
Expand All @@ -114,17 +114,16 @@ def __init__(self, mainGui):

return

def __del__(self): # pragma: no cover
def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiUpdates")
return

def checkLatest(self):
"""Check for latest release.
"""
def checkLatest(self) -> None:
"""Check for latest release."""
qApp.setOverrideCursor(QCursor(Qt.WaitCursor))

urlReq = Request("https://api.github.com/repos/vkbo/novelwriter/releases/latest")
urlReq.add_header("User-Agent", "Mozilla/5.0 (compatible; novelWriter (Python))")
urlReq.add_header("User-Agent", nwConst.USER_AGENT)
urlReq.add_header("Accept", "application/vnd.github.v3+json")

rawData = {}
Expand Down Expand Up @@ -161,10 +160,22 @@ def checkLatest(self):
return

##
# Internal Functions
# Events
##

def _doClose(self):
def closeEvent(self, event: QCloseEvent) -> None:
"""Capture the user closing the window."""
event.accept()
self.deleteLater()
return

##
# Private Slots
##

@pyqtSlot()
def _doClose(self) -> None:
"""Close the dialog."""
self.close()
return

Expand Down
2 changes: 1 addition & 1 deletion novelwriter/gui/docviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def _dumpHistory(self) -> None:
"""Debug function to dump history to the logger. Since it is a
for loop, it is skipped entirely if log level isn't DEBUG.
"""
if logger.getEffectiveLevel() == logging.DEBUG: # pragma: no cover
if CONFIG.isDebug: # pragma: no cover
for i, (h, p) in enumerate(zip(self._navHistory, self._posHistory)):
logger.debug(
"History %02d: %s %13s [x:%d]" % (
Expand Down
5 changes: 5 additions & 0 deletions novelwriter/gui/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,11 @@ def _buildToolsMenu(self) -> None:
self.aEditWordList = self.toolsMenu.addAction(self.tr("Project Word List"))
self.aEditWordList.triggered.connect(lambda: self.mainGui.showProjectWordListDialog())

# Tools > Add Dictionaries
if CONFIG.osWindows or CONFIG.isDebug:
self.aAddDicts = self.toolsMenu.addAction(self.tr("Add Dictionaries"))
self.aAddDicts.triggered.connect(self.mainGui.showDictionariesDialog)

# Tools > Separator
self.toolsMenu.addSeparator()

Expand Down
17 changes: 16 additions & 1 deletion novelwriter/guimain.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from novelwriter.tools.lipsum import GuiLipsum
from novelwriter.tools.manuscript import GuiManuscript
from novelwriter.tools.projwizard import GuiProjectWizard
from novelwriter.tools.dictionaries import GuiDictionaries
from novelwriter.tools.writingstats import GuiWritingStats
from novelwriter.core.coretools import ProjectBuilder

Expand Down Expand Up @@ -340,7 +341,7 @@ def __init__(self) -> None:

logger.debug("Ready: GUI")

if __hexversion__[-2] == "a" and logger.getEffectiveLevel() > logging.DEBUG:
if __hexversion__[-2] == "a" and not CONFIG.isDebug:
SHARED.warn(self.tr(
"You are running an untested development version of novelWriter. "
"Please be careful when working on a live project "
Expand Down Expand Up @@ -1065,6 +1066,20 @@ def showUpdatesDialog(self) -> None:

return

@pyqtSlot()
def showDictionariesDialog(self) -> None:
"""Show the download dictionaries dialog."""
dlgDicts = GuiDictionaries(self)
dlgDicts.setModal(True)
dlgDicts.show()
dlgDicts.raise_()
qApp.processEvents()
if not dlgDicts.initDialog():
dlgDicts.close()
SHARED.error(self.tr("Could not initialise the dialog."))

return

def reportConfErr(self) -> bool:
"""Checks if the Config module has any errors to report, and let
the user know if this is the case. The Config module caches
Expand Down
Loading

0 comments on commit afc0451

Please sign in to comment.