[pitivi] Show an error dialog when encountering Unicode decoding errors/broken locales



commit c6b12b7df202f7b3467f6967ef52f37b76af84d5
Author: Lubosz Sarnecki <lubosz gmail com>
Date:   Tue Sep 30 17:36:55 2014 +0200

    Show an error dialog when encountering Unicode decoding errors/broken locales
    
    Fixes bug #729523

 pitivi/project.py    |    4 +++-
 pitivi/settings.py   |    6 ++++++
 pitivi/utils/misc.py |   41 ++++++++++++++++++++++++++++++-----------
 3 files changed, 39 insertions(+), 12 deletions(-)
---
diff --git a/pitivi/project.py b/pitivi/project.py
index e3a8f5b..990c3dd 100644
--- a/pitivi/project.py
+++ b/pitivi/project.py
@@ -40,7 +40,7 @@ from pwd import getpwuid
 from pitivi.undo.undo import UndoableAction
 from pitivi.configure import get_ui_dir
 
-from pitivi.utils.misc import quote_uri, path_from_uri, isWritable
+from pitivi.utils.misc import quote_uri, path_from_uri, isWritable, unicode_error_dialog
 from pitivi.utils.pipeline import PipelineError, Seeker
 from pitivi.utils.loggable import Loggable
 from pitivi.utils.pipeline import Pipeline
@@ -192,6 +192,8 @@ class ProjectManager(GObject.Object, Loggable):
             self.debug('Backup file is %d secs newer: %s', time_diff, backup_path)
         except OSError:
             self.debug('Backup file does not exist: %s', backup_path)
+        except UnicodeEncodeError:
+            unicode_error_dialog()
         else:
             if time_diff > 0:
                 use_backup = self._restoreFromBackupDialog(time_diff)
diff --git a/pitivi/settings.py b/pitivi/settings.py
index 90739a3..e3a5af4 100644
--- a/pitivi/settings.py
+++ b/pitivi/settings.py
@@ -25,6 +25,8 @@ from configparser import SafeConfigParser, ParsingError
 from gi.repository import GLib
 from gi.repository import GObject
 
+from pitivi.utils.misc import unicode_error_dialog
+
 
 def get_bool_env(var):
     value = os.getenv(var)
@@ -146,9 +148,13 @@ class GlobalSettings(GObject.Object):
         """
         Read the configuration from the user configuration file.
         """
+
         try:
             conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf")
             self._config.read(conf_file_path)
+        except UnicodeDecodeError:
+            unicode_error_dialog()
+            return
         except ParsingError:
             return
 
diff --git a/pitivi/utils/misc.py b/pitivi/utils/misc.py
index 539656f..704c65c 100644
--- a/pitivi/utils/misc.py
+++ b/pitivi/utils/misc.py
@@ -70,17 +70,20 @@ def isWritable(path):
     """
     Return whether the file/path is writable.
     """
-    if os.path.isdir(path):
-        # The given path is an existing directory.
-        # To properly check if it is writable, you need to use os.access.
-        return os.access(path, os.W_OK)
-    else:
-        # The given path is supposed to be a file.
-        # Avoid using open(path, "w"), as it might corrupt existing files.
-        # And yet, even if the parent directory is actually writable,
-        # open(path, "rw") will IOError if the file doesn't already exist.
-        # Therefore, simply check the directory permissions instead:
-        return os.access(os.path.dirname(path), os.W_OK)
+    try:
+        if os.path.isdir(path):
+            # The given path is an existing directory.
+            # To properly check if it is writable, you need to use os.access.
+            return os.access(path, os.W_OK)
+        else:
+            # The given path is supposed to be a file.
+            # Avoid using open(path, "w"), as it might corrupt existing files.
+            # And yet, even if the parent directory is actually writable,
+            # open(path, "rw") will IOError if the file doesn't already exist.
+            # Therefore, simply check the directory permissions instead:
+            return os.access(os.path.dirname(path), os.W_OK)
+    except UnicodeDecodeError:
+        unicode_error_dialog()
 
 
 def uri_is_valid(uri):
@@ -234,3 +237,19 @@ def show_user_manual(page=None):
             continue
     log.warning("utils", "Failed loading URIs")
     # TODO: Show an error message to the user.
+
+
+def unicode_error_dialog():
+    message = _("The system's locale that you are using is not UTF-8 capable. "
+                "Unicode support is required for Python3 software like Pitivi. "
+                "Please correct your system settings; if you try to use Pitivi "
+                "with a broken locale, weird bugs will happen.")
+    dialog = Gtk.MessageDialog(transient_for=None,
+                               modal=True,
+                               message_type=Gtk.MessageType.ERROR,
+                               buttons=Gtk.ButtonsType.OK,
+                               text=message)
+    dialog.set_icon_name("pitivi")
+    dialog.set_title(_("Error while decoding a string"))
+    dialog.run()
+    dialog.destroy()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]