Commits

Yuya Nishihara committed 732c546

qscilib: fix file editing dialog to report I/O error

QFile will never raise exception.

Comments (0)

Files changed (1)

tortoisehg/hgqt/qscilib.py

 
 def readFile(editor, filename):
     f = QFile(filename)
-    f.open(QIODevice.ReadOnly)
-    editor.read(f)
+    if not f.open(QIODevice.ReadOnly):
+        qtlib.WarningMsgBox(_('Unable to read file'),
+                            _('Could not open the specified file for reading.'),
+                            f.errorString(), parent=editor)
+        return False
+    if not editor.read(f):
+        qtlib.WarningMsgBox(_('Unable to read file'),
+                            _('An error occurred while reading the file.'),
+                            f.errorString(), parent=editor)
+        return False
     editor.setModified(False)
+    return True
 
 def writeFile(editor, filename):
     f = QFile(filename)
-    f.open(QIODevice.WriteOnly)
-    editor.write(f)
+    if not f.open(QIODevice.WriteOnly):
+        qtlib.WarningMsgBox(_('Unable to write file'),
+                            _('Could not open the specified file for writing.'),
+                            f.errorString(), parent=editor)
+        return False
+    if not editor.write(f):
+        qtlib.WarningMsgBox(_('Unable to write file'),
+                            _('An error occurred while writing the file.'),
+                            f.errorString(), parent=editor)
+        return False
+    return True
 
 def fileEditor(filename, **opts):
     'Open a simple modal file editing dialog'
     dialog.resize(desktopgeom.size() * 0.5)
     dialog.restoreGeometry(s.value(geomname).toByteArray())
 
-    ret = QDialog.Rejected
-    try:
-        readFile(editor, filename)
-        ret = dialog.exec_()
-        if ret == QDialog.Accepted:
-            writeFile(editor, filename)
-        s.setValue(geomname, dialog.saveGeometry())
-    except EnvironmentError, e:
-        qtlib.WarningMsgBox(_('Unable to read/write config file'),
-                            hglib.tounicode(str(e)), parent=dialog)
+    if not readFile(editor, filename):
+        return QDialog.Rejected
+    ret = dialog.exec_()
+    if ret != QDialog.Accepted:
+        return ret
+    if not writeFile(editor, filename):
+        return QDialog.Rejected
+    s.setValue(geomname, dialog.saveGeometry())
     return ret
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.