changeset 27271:a4416cd6bb4f

Append name of actual encoding used if "SYSTEM" is selected. * gui-preferences.h: Add name of actual encoding used for "SYSTEM". * resource-manager.cc (do_get_codecs, do_combo_encoding): Add name of actual encoding used for "SYSTEM". * file-editor-tab.cc (file_editor_tab): Add name of actual encoding used for "SYSTEM". (check_valid_codec): Check if _encoding starts with "SYSTEM". * main-window.cc (gui_preference_adjust): Add name of actual encoding used for "SYSTEM". (update_default_encoding): Strip actual encoding from "SYSTEM" before passing it to F__mfile_encoding__.
author Markus Mützel <markus.muetzel@gmx.de>
date Sun, 21 Jul 2019 12:16:09 +0200
parents 47f42ad90b0b
children 9de4741a896e
files libgui/src/gui-preferences.h libgui/src/m-editor/file-editor-tab.cc libgui/src/main-window.cc libgui/src/resource-manager.cc
diffstat 4 files changed, 31 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/gui-preferences.h	Sat Jul 20 17:57:23 2019 +0200
+++ b/libgui/src/gui-preferences.h	Sun Jul 21 12:16:09 2019 +0200
@@ -30,6 +30,7 @@
 #include <QStringList>
 #include <QStyle>
 #include <QTabWidget>
+#include <QTextCodec>
 #include <QVariant>
 
 // Structure for the definition of pairs: key and default value
@@ -111,7 +112,10 @@
 // File handling
 const gui_pref ed_show_dbg_file ("editor/show_dbg_file", QVariant (true));
 #if defined (Q_OS_WIN32)
-const gui_pref ed_default_enc ("editor/default_encoding", QVariant ("SYSTEM"));
+const gui_pref ed_default_enc
+  ("editor/default_encoding",
+   QVariant (QTextCodec::codecForLocale ()->name ().toUpper ().prepend
+               ("SYSTEM (").append (")")));
 #else
 const gui_pref ed_default_enc ("editor/default_encoding", QVariant ("UTF-8"));
 #endif
--- a/libgui/src/m-editor/file-editor-tab.cc	Sat Jul 20 17:57:23 2019 +0200
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sun Jul 21 12:16:09 2019 +0200
@@ -232,7 +232,9 @@
 
     // encoding, not updated with the settings
 #if defined (Q_OS_WIN32)
-    _encoding = settings->value ("editor/default_encoding", "SYSTEM")
+    QString locale_enc_name =
+      QTextCodec::codecForLocale ()->name ().toUpper ().prepend ("SYSTEM (").append (")");
+    _encoding = settings->value ("editor/default_encoding", locale_enc_name)
                 .toString ();
 #else
     _encoding = settings->value ("editor/default_encoding", "UTF-8")
@@ -2412,9 +2414,8 @@
   {
     QTextCodec *codec = QTextCodec::codecForName (_encoding.toLatin1 ());
 
-    // "SYSTEM" is used as alias for locale on windows systems,
-    // which might not support "SYSTEM" codec
-    if ((! codec) && (_encoding == "SYSTEM"))
+    // "SYSTEM" is used as alias for the locale encoding on Windows systems.
+    if ((! codec) && _encoding.startsWith("SYSTEM"))
       codec = QTextCodec::codecForLocale ();
 
     if (! codec)
--- a/libgui/src/main-window.cc	Sat Jul 20 17:57:23 2019 +0200
+++ b/libgui/src/main-window.cc	Sun Jul 21 12:16:09 2019 +0200
@@ -2708,7 +2708,10 @@
         resource_manager::get_codecs (&codecs);
 
         QRegExp re ("^CP(\\d+)$");
-        if (re.indexIn (adjusted_value) > -1)
+        if (adjusted_value == "SYSTEM")
+          adjusted_value =
+            QTextCodec::codecForLocale ()->name ().toUpper ().prepend ("SYSTEM (").append (")");
+        else if (re.indexIn (adjusted_value) > -1)
           {
             if (codecs.contains ("IBM" + re.cap (1)))
               adjusted_value = "IBM" + re.cap (1);
@@ -2855,12 +2858,15 @@
   void main_window::update_default_encoding (const QString& default_encoding)
   {
     m_default_encoding = default_encoding;
+    std::string mfile_encoding = m_default_encoding.toStdString ();
+    if (m_default_encoding.startsWith ("SYSTEM", Qt::CaseInsensitive))
+      mfile_encoding = "SYSTEM";
 
     event_manager& evmgr
       = __get_event_manager__ ("main_window::notice_settings");
 
     evmgr.post_event
-      ([this] (void)
+      ([this, mfile_encoding] (void)
        {
          // INTERPRETER THREAD
 
@@ -2868,7 +2874,7 @@
            = __get_interpreter__ ("main_window::notice_settings");
 
          F__mfile_encoding__ (interp,
-                              ovl (m_default_encoding.toStdString ()));
+                              ovl (mfile_encoding));
        });
   }
 
--- a/libgui/src/resource-manager.cc	Sat Jul 20 17:57:23 2019 +0200
+++ b/libgui/src/resource-manager.cc	Sun Jul 21 12:16:09 2019 +0200
@@ -421,9 +421,10 @@
         codecs->append (c->name ().toUpper ());
       }
 
-    // If on windows append SYSTEM even if not supported
-    if (ed_default_enc.def.toString () == "SYSTEM")
-      codecs->append (ed_default_enc.def.toString ());
+    // If on Windows, append "SYSTEM"
+    if (ed_default_enc.def.toString ().startsWith("SYSTEM"))
+      codecs->append (QTextCodec::codecForLocale ()->name ().toUpper ().prepend
+                        ("SYSTEM (").append (")"));
 
     // Clean up and sort list of codecs
     codecs->removeDuplicates ();
@@ -439,13 +440,16 @@
     // get the value from the settings file if no current encoding is given
     QString enc = current;
 
-    // Check for valid codec for the default. Allow "SYSTEM" even no valid
-    // codec exists, since codecForLocale will be chosen in this case
+    // Check for valid codec for the default. If this fails, "SYSTEM" (i.e.
+    // codecForLocale) will be chosen.
     bool default_exists = false;
     if (QTextCodec::codecForName (ed_default_enc.def.toString ().toLatin1 ())
-        || (ed_default_enc.def.toString () == "SYSTEM"))
+        || (ed_default_enc.def.toString ().startsWith ("SYSTEM")))
       default_exists = true;
 
+    QString default_enc =
+      QTextCodec::codecForLocale ()->name ().toUpper ().prepend
+        ("SYSTEM (").append (")");
     if (enc.isEmpty ())
       {
         enc = m_settings->value (ed_default_enc.key, ed_default_enc.def).toString ();
@@ -455,7 +459,7 @@
             if (default_exists)
               enc = ed_default_enc.def.toString ();
             else
-              enc = QTextCodec::codecForLocale ()->name ().toUpper ();
+              enc = default_enc;
           }
       }
 
@@ -468,7 +472,7 @@
     if (default_exists)
       combo->insertItem (0, ed_default_enc.def.toString ());
     else
-      combo->insertItem (0, QTextCodec::codecForLocale ()->name ().toUpper ());
+      combo->insertItem (0, default_enc);
 
     // select the default or the current one
     int idx = combo->findText (enc, Qt::MatchExactly);