diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index cf38e902..712f08c8 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -14,7 +14,7 @@ import logging import nw from os import path -from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QFileDialog, QStackedWidget, QShortcut +from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QFileDialog, QStackedWidget, QShortcut, QMessageBox from PyQt5.QtGui import QIcon from PyQt5.QtCore import Qt @@ -97,6 +97,34 @@ class GuiMain(QMainWindow): return + def makeAlert(self, theMessage, theLevel): + """Alert both the user and the logger at the same time. Message can be either a string or an + array of strings. Severity level is 0 = info, 1 = warning, and 2 = error. + """ + + if isinstance(theMessage, list): + popMsg = "
".join(theMessage) + logMsg = theMessage + else: + popMsg = theMessage + logMsg = [theMessage] + + msgBox = QMessageBox() + if theLevel == 0: + for msgLine in logMsg: + logger.info(msgLine) + msgBox.information(self, "Information", popMsg) + elif theLevel == 1: + for msgLine in logMsg: + logger.warning(msgLine) + msgBox.warning(self, "Warning", popMsg) + elif theLevel == 2: + for msgLine in logMsg: + logger.error(msgLine) + msgBox.critical(self, "Error", popMsg) + + return + ## # Project Actions ## diff --git a/nw/project/document.py b/nw/project/document.py index 483a3377..04854275 100644 --- a/nw/project/document.py +++ b/nw/project/document.py @@ -45,8 +45,12 @@ class NWDoc(): docPath = path.join(dataDir, docFile) if path.isfile(docPath): - with open(docPath,mode="r") as inFile: - theDoc = inFile.read() + try: + with open(docPath,mode="r") as inFile: + theDoc = inFile.read() + except Exception as e: + self.theParent.makeAlert(["Failed to open document file.",str(e)],2) + return "" else: logger.debug("The requested document does not exist.") return "" @@ -75,8 +79,12 @@ class NWDoc(): if path.isfile(docBack): rename(docBack,docTemp) if path.isfile(docPath): rename(docPath,docBack) - with open(docPath,mode="w") as outFile: - outFile.write(docText) + try: + with open(docPath,mode="w") as outFile: + outFile.write(docText) + except Exception as e: + self.theParent.makeAlert(["Could not save document.",str(e)],2) + return False if path.isfile(docTemp): unlink(docTemp) diff --git a/nw/project/project.py b/nw/project/project.py index 91ddd01e..992c969b 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -128,7 +128,7 @@ class NWProject(): if not path.isfile(fileName): fileName = path.join(fileName, "nwProject.nwx") if not path.isfile(fileName): - logger.error("File not found: %s" % fileName) + self.theParent.makeAlert("File not found: %s" % fileName,2) return False self.clearProject() @@ -146,7 +146,7 @@ class NWProject(): logger.verbose("File version is %s" % fileVersion) if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0": - logger.error("Project file does not appear to be a novelWriterXML file version 1.0") + self.theParent.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0",2) return False for xChild in xRoot: @@ -192,15 +192,15 @@ class NWProject(): def saveProject(self): if self.projPath is None: - logger.error("Project path not set, cannot save.") + self.theParent.makeAlert("Project path not set, cannot save.",2) return False if not path.isdir(self.projPath): try: mkdir(self.projPath) logger.info("Created folder %s" % self.projPath) - except: - logger.error("Could not create folder %s" % self.projPath) + except Exception as e: + self.theParent.makeAlert(["Could not create folder.",str(e)],2) return False logger.debug("Saving project: %s" % self.projPath) @@ -239,8 +239,7 @@ class NWProject(): xml_declaration = True )) except Exception as e: - logger.error("Failed to save project to %s" % saveFile) - logger.error(str(e)) + self.theParent.makeAlert(["Failed to save project.",str(e)],2) return False self.mainConf.setRecent(self.projPath)