[meld] ui.filechooser: Default to autodetecting encoding (#350)



commit 708ac12871b5b636f10db245c32e5364dd93664d
Author: Me <me example com>
Date:   Sat Jul 27 08:52:22 2019 +1000

    ui.filechooser: Default to autodetecting encoding (#350)
    
    When we added the custom file chooser that allowed encoding selection,
    the default encoding behaviour changed. Previously, there would be no
    custom encoding, so the default behaviour would be used: try the user-
    configured encodings, then try the GtkSourceView list for the current
    locale.
    
    With the custom filechooser, the default encoding selection was whatever
    the current locale was, and so instead of using the user-configured
    encoding list, we would always try the current locale encoding. This is
    fine in most scenarios because users' locale encoding is typically
    UTF-8... but if a user had something more exciting as their locale
    things would go badly.
    
    More importantly, because the user had chosen a locale, we didn't try
    the user-configured locales. While this behaviour is arguably correct
    for when the user actually chooses an encoding, it's definitely not
    right when the user just doesn't select one and gets the locale-default
    encoding.
    
    This change fixes all of this by simply adding an "Autodetect" option to
    the custom filechooser, and using that by default.

 meld/ui/filechooser.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/meld/ui/filechooser.py b/meld/ui/filechooser.py
index 9ef2d1ad..639a0a2d 100644
--- a/meld/ui/filechooser.py
+++ b/meld/ui/filechooser.py
@@ -20,6 +20,8 @@ import sys
 from gi.repository import Gtk
 from gi.repository import GtkSource
 
+from meld.conf import _
+
 
 FILE_ACTIONS = {
     Gtk.FileChooserAction.OPEN,
@@ -65,7 +67,12 @@ class MeldFileChooserDialog(Gtk.FileChooserDialog):
             current = GtkSource.encoding_get_current()
 
         codecs = [
-            (current.to_string(), current.get_charset()),
+            (_('Autodetect Encoding'), None),
+            (None, None),
+            (
+                _('Current Locale ({})').format(current.get_charset()),
+                current.get_charset()
+            ),
             (None, None),
         ]
         for encoding in GtkSource.encoding_get_all():
@@ -81,7 +88,7 @@ class MeldFileChooserDialog(Gtk.FileChooserDialog):
         combo.pack_start(cell, True)
         combo.add_attribute(cell, 'text', 0)
         combo.set_row_separator_func(
-            lambda model, it, data: not model.get_value(it, 1), None)
+            lambda model, it, data: not model.get_value(it, 0), None)
         combo.props.active = 0
         return combo
 
@@ -91,6 +98,8 @@ class MeldFileChooserDialog(Gtk.FileChooserDialog):
         if not combo:
             return None
         charset = self.encoding_store.get_value(combo.get_active_iter(), 1)
+        if not charset:
+            return None
         return GtkSource.Encoding.get_from_charset(charset)
 
     def action_changed_cb(self, *args):


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