]> spindle.queued.net Git - midori/commitdiff
Load data files from system data dirs
authorChristian Dywan <christian@twotoasts.de>
Mon, 20 Jul 2009 06:43:23 +0000 (08:43 +0200)
committerChristian Dywan <christian@twotoasts.de>
Mon, 20 Jul 2009 07:10:53 +0000 (09:10 +0200)
A new helper function in sokoke is added to keep the code simple.

midori/midori-browser.c
midori/midori-view.c
midori/sokoke.c
midori/sokoke.h

index 810891ca958431eb9f7c199ad85c730471b4d1ac..f03a525f7f79f5d9a6936ee356c281260449dba6 100644 (file)
@@ -903,13 +903,16 @@ midori_browser_speed_dial_get_next_free_slot (void)
 
     if (!g_file_test (body_fname, G_FILE_TEST_EXISTS))
     {
-        if (g_file_get_contents (MDATADIR "/midori/res/speeddial.json",
-                                 &speed_dial_body, NULL, NULL))
+        gchar* filename = g_build_filename ("midori", "res", "speeddial.json", NULL);
+        gchar* filepath = sokoke_find_data_filename (filename);
+        g_free (filename);
+        if (g_file_get_contents (filepath, &speed_dial_body, NULL, NULL))
         {
             g_file_set_contents (body_fname, speed_dial_body, -1, NULL);
 
             g_free (speed_dial_body);
         }
+        g_free (filepath);
         g_free (body_fname);
         return g_strdup ("s1");
     }
index 845c2537a861915b86792464d7278b9134ca2652..a8886d43242c8e9533454bdc2be5ca1e23d691e0 100644 (file)
@@ -732,10 +732,12 @@ webkit_web_view_load_error_cb (WebKitWebView*  web_view,
                                GError*         error,
                                MidoriView*     view)
 {
-    const gchar* template_file = MDATADIR "/midori/res/error.html";
+    gchar* template_file = g_build_filename ("midori", "res", "error.html", NULL);
+    gchar* path = sokoke_find_data_filename (template_file);
     gchar* template;
 
-    if (g_file_get_contents (template_file, &template, NULL, NULL))
+    g_free (template_file);
+    if (g_file_get_contents (path, &template, NULL, NULL))
     {
         SoupServer* res_server;
         guint port;
@@ -766,9 +768,11 @@ webkit_web_view_load_error_cb (WebKitWebView*  web_view,
         g_free (res_root);
         g_free (stock_root);
         g_free (result);
+        g_free (path);
 
         return TRUE;
     }
+    g_free (path);
 
     return FALSE;
 }
index a663f3ec4b5d4b1154e4e3678f9ebe8e5943f6ed..194657f4dac6e66498c96b801e022e6e17bb2938 100644 (file)
@@ -967,6 +967,32 @@ sokoke_remove_path (const gchar* path,
     return TRUE;
 }
 
+/**
+ * sokoke_find_data_filename:
+ * @filename: a filename or relative path
+ *
+ * Looks for the specified filename in the system data
+ * directories, depending on the platform.
+ *
+ * Return value: a full path
+ **/
+gchar*
+sokoke_find_data_filename (const gchar* filename)
+{
+    const gchar* const* data_dirs = g_get_system_data_dirs ();
+    guint i = 0;
+    const gchar* data_dir;
+
+    while ((data_dir = data_dirs[i++]))
+    {
+        gchar* path = g_build_filename (data_dir, filename, NULL);
+        if (g_file_test (path, G_FILE_TEST_EXISTS))
+            return path;
+        g_free (path);
+    }
+    return g_build_filename (MDATADIR, filename, NULL);
+}
+
 static void
 res_server_handler_cb (SoupServer*        res_server,
                        SoupMessage*       msg,
@@ -977,13 +1003,15 @@ res_server_handler_cb (SoupServer*        res_server,
 {
     if (g_str_has_prefix (path, "/res"))
     {
-        gchar* filename = g_strconcat (MDATADIR "/midori", path, NULL);
+        gchar* filename = g_build_filename ("midori", path, NULL);
+        gchar* filepath = sokoke_find_data_filename (filename);
         gchar* contents;
         gsize length;
 
-        if (g_file_get_contents (filename, &contents, &length, NULL))
+        g_free (filename);
+        if (g_file_get_contents (filepath, &contents, &length, NULL))
         {
-            gchar* content_type = g_content_type_guess (filename, (guchar*)contents,
+            gchar* content_type = g_content_type_guess (filepath, (guchar*)contents,
                                                         length, NULL);
             gchar* mime_type = g_content_type_get_mime_type (content_type);
             g_free (content_type);
@@ -994,7 +1022,7 @@ res_server_handler_cb (SoupServer*        res_server,
         }
         else
             soup_message_set_status (msg, 404);
-        g_free (filename);
+        g_free (filepath);
     }
     else if (g_str_has_prefix (path, "/stock/"))
     {
index d656410e56b829b477f633c92b478023cf06cfc3..a39b3e6f7f3c67da417ed0a7a537437b9cca33c8 100644 (file)
@@ -151,6 +151,9 @@ gboolean
 sokoke_remove_path                      (const gchar*    path,
                                          gboolean        ignore_errors);
 
+gchar*
+sokoke_find_data_filename               (const gchar*    filename);
+
 SoupServer*
 sokoke_get_res_server                   (void);