nemiver r839 - in trunk: . src/persp/dbgperspective src/persp/dbgperspective/schemas



Author: dodji
Date: Sun Jun  8 20:57:53 2008
New Revision: 839
URL: http://svn.gnome.org/viewvc/nemiver?rev=839&view=rev

Log:
fix #537281

	* src/persp/dbgperspective/nmv-dbg-perspective.cc:
	  When opening a file, it has to be converted to the UTF-8 encoding.
	  This has been so for ages. What I just added is a way to specify the
	  possible encodings of the file we want to open. We need to know the
	  list of possible encodings of the files we need to open so that
	  Nemiver can know how to convert the encoding to UTF8. I have then
	  added a gconf key that lists the possible encodings. Whenever a file
	  cannot be opened because we don't know its encoding, the file
	  encoding just has to be added to the value of the gconf key.
	  This entry should fix bug #537281.


Modified:
   trunk/ChangeLog
   trunk/src/persp/dbgperspective/nmv-dbg-perspective.cc
   trunk/src/persp/dbgperspective/schemas/nemiver-dbgperspective.schemas

Modified: trunk/src/persp/dbgperspective/nmv-dbg-perspective.cc
==============================================================================
--- trunk/src/persp/dbgperspective/nmv-dbg-perspective.cc	(original)
+++ trunk/src/persp/dbgperspective/nmv-dbg-perspective.cc	Sun Jun  8 20:57:53 2008
@@ -133,7 +133,9 @@
                 "/apps/nemiver/dbgperspective/show-source-line-numbers" ;
 static const char* CONF_KEY_HIGHLIGHT_SOURCE_CODE =
                 "/apps/nemiver/dbgperspective/highlight-source-code" ;
-static const UString CONF_KEY_USE_SYSTEM_FONT =
+static const char* CONF_KEY_SOURCE_FILE_ENCODING_LIST =
+                "/apps/nemiver/dbgperspective/source-file-encoding-list" ;
+static const char* CONF_KEY_USE_SYSTEM_FONT =
                 "/apps/nemiver/dbgperspective/use-system-font" ;
 static const char* CONF_KEY_CUSTOM_FONT_NAME=
                 "/apps/nemiver/dbgperspective/custom-font-name" ;
@@ -156,6 +158,17 @@
 const Gtk::StockID STOCK_STEP_OVER (STEP_OVER) ;
 const Gtk::StockID STOCK_STEP_OUT (STEP_OUT) ;
 
+const char* SUPPORTED_ENCODINGS[] =
+{
+    "UTF-8",
+    "ISO-8859",
+    "ISO-8859-1",
+    "ISO-8859-15",
+};
+
+#define SIZE_OF_SUPPORTED_ENCODINGS \
+sizeof (SUPPORTED_ENCODINGS)/sizeof (SUPPORTED_ENCODINGS[0])
+
 class DBGPerspective : public IDBGPerspective, public sigc::trackable {
     //non copyable
     DBGPerspective (const IPerspective&) ;
@@ -865,10 +878,12 @@
     //Try to convert it from user locale to utf8, and make sure the result
     //is valid. if not, throw an exception due to the fact the file is
     //encoded in an unknown encoding.
-    bool ensure_buffer_is_in_utf8 (const std::string &a_input,
+    bool ensure_buffer_is_in_utf8 (const UString &a_path,
+                                   const std::string &a_input,
                                    UString &a_output,
                                    std::string &a_current_charset)
     {
+        LOG_FUNCTION_SCOPE_NORMAL_DD ;
         NEMIVER_TRY
 
         UString buf_content;
@@ -876,18 +891,68 @@
             a_output = a_input;
             return true;
         }
-        if (Glib::get_charset (a_current_charset)) {
-            //User's charset is utf8 but a_buffer is not encoded in utf8.
-            //So there is no way to convert the content of
-            //a_bufer into utf8 as we don't know the encoding of a_buffer
-            //to begin with.
-            return false;
+        UString utf8_content;
+        bool converted=false;
+        //get the list of candidate encodings that could be the encoding of
+        //the a_input. This list is in gconf. So let's kindly ask gconf for
+        //it. If for a reason we cannot reach gconf or the list is empty,
+        //then we will fall back to a hardcoded list of encodings.
+        THROW_IF_FAIL (workbench);
+        IConfMgrSafePtr conf_mgr = workbench->get_configuration_manager ();
+        THROW_IF_FAIL (conf_mgr);
+        std::list<UString> supported_encodings;
+        if (conf_mgr->get_key_value (CONF_KEY_SOURCE_FILE_ENCODING_LIST,
+                                     supported_encodings)
+            && !supported_encodings.empty ()) {
+            LOG_DD ("trying encodings coming from gconf");
+            std::list<UString>::const_iterator it;
+            for (it = supported_encodings.begin ();
+                 it != supported_encodings.end ();
+                 ++it) {
+                try {
+                    LOG_DD ("trying to convert buffer from encoding "
+                             << it->c_str ()
+                             << " to UTF-8");
+                    utf8_content = Glib::convert (a_input,
+                                                  "UTF-8",
+                                                  it->c_str ());
+                } catch (Glib::Exception &e) {
+                    LOG_DD ("tentative encoding conversion failed!");
+                    continue;
+                } catch (...) {
+                    THROW ("unknown exception raised by Glib::convert()");
+                }
+                LOG_DD ("tentative encoding conversion succeeded!");
+                converted = true;
+                break;
+            }
+        } else {
+            //fall back to trying the hardcoded list of supported encodings
+            LOG_DD ("trying hardcoded encodings");
+            for (unsigned int i=0; i < SIZE_OF_SUPPORTED_ENCODINGS; i++) {
+                try {
+                    utf8_content = Glib::convert (a_input,
+                                                  "UTF-8",
+                                                  SUPPORTED_ENCODINGS[i]);
+                } catch (Glib::Exception &e) {
+                    continue;
+                } catch (...) {
+                    THROW ("unknown exception raised by Glib::convert()");
+                }
+                converted = true;
+            }
+        }
+        if (!converted) {
+            THROW_IF_FAIL (!a_path.empty ());
+            UString message;
+            message.printf (_("File %s has an encoding that is not listed in "
+                            "the value of gconf key %s. Please add the encoding "
+                            "of that file to the values of the gconf key, and "
+                            "resume debugging"),
+                             a_path.c_str (),
+                             CONF_KEY_SOURCE_FILE_ENCODING_LIST);
+            THROW (message);
         }
-        LOG_DD ("user's charset: " << a_current_charset);
-        LOG_DD ("going to convert a_buffer from "
-                << a_current_charset
-                << " to utf8 ...");
-        UString utf8_content = Glib::locale_to_utf8 (a_input);
         const char *end=0;
         if (utf8_content.empty ()
             || !g_utf8_validate (utf8_content.raw ().c_str (),
@@ -4070,7 +4135,8 @@
 #endif // WITH_GIO
     UString utf8_content;
     std::string cur_charset;
-    if (!m_priv->ensure_buffer_is_in_utf8 (content,
+    if (!m_priv->ensure_buffer_is_in_utf8 (a_path,
+                                           content,
                                            utf8_content,
                                            cur_charset)) {
         UString msg;

Modified: trunk/src/persp/dbgperspective/schemas/nemiver-dbgperspective.schemas
==============================================================================
--- trunk/src/persp/dbgperspective/schemas/nemiver-dbgperspective.schemas	(original)
+++ trunk/src/persp/dbgperspective/schemas/nemiver-dbgperspective.schemas	Sun Jun  8 20:57:53 2008
@@ -133,6 +133,18 @@
 	<long>Do not automatically display call stack wider than this value</long>
       </locale>
     </schema>
+    <schema>
+      <key>/schemas/apps/nemiver/dbgperspective/source-file-encoding-list</key>
+      <applyto>/apps/nemiver/dbgperspective/source-file-encoding-list</applyto>
+      <owner>nemiver</owner>
+      <type>list</type>
+      <list_type>string</list_type>
+      <default>[UTF-8,ISO-8859,ISO-8859-1,ISO-8859-15]</default>
+      <locale name="C">
+	<short>The list of of file encodings supported by nemiver</short>
+	<long>When nemiver opens a file, it will assume the encoding of the file is one of the encodings listed here. Please bear in mind that Nemiver works internally with files in the UTF-8 encoding. So when it loads a file, it must convert the file encoding into UTF-8. Therefore, Nemiver needs to know the possible set of encodings of the source files it has to open</long>
+      </locale>
+    </schema>
   </schemalist>
 </gconfschemafile>
 



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