Load data files from system data dirs

A new helper function in sokoke is added to keep the code simple.
This commit is contained in:
Christian Dywan 2009-07-20 08:43:23 +02:00
parent ff8489da20
commit 7a3535afac
4 changed files with 46 additions and 8 deletions

View 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_test (body_fname, G_FILE_TEST_EXISTS))
{ {
if (g_file_get_contents (MDATADIR "/midori/res/speeddial.json", gchar* filename = g_build_filename ("midori", "res", "speeddial.json", NULL);
&speed_dial_body, NULL, 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_file_set_contents (body_fname, speed_dial_body, -1, NULL);
g_free (speed_dial_body); g_free (speed_dial_body);
} }
g_free (filepath);
g_free (body_fname); g_free (body_fname);
return g_strdup ("s1"); return g_strdup ("s1");
} }

View file

@ -732,10 +732,12 @@ webkit_web_view_load_error_cb (WebKitWebView* web_view,
GError* error, GError* error,
MidoriView* view) 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; 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; SoupServer* res_server;
guint port; guint port;
@ -766,9 +768,11 @@ webkit_web_view_load_error_cb (WebKitWebView* web_view,
g_free (res_root); g_free (res_root);
g_free (stock_root); g_free (stock_root);
g_free (result); g_free (result);
g_free (path);
return TRUE; return TRUE;
} }
g_free (path);
return FALSE; return FALSE;
} }

View file

@ -967,6 +967,32 @@ sokoke_remove_path (const gchar* path,
return TRUE; 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 static void
res_server_handler_cb (SoupServer* res_server, res_server_handler_cb (SoupServer* res_server,
SoupMessage* msg, SoupMessage* msg,
@ -977,13 +1003,15 @@ res_server_handler_cb (SoupServer* res_server,
{ {
if (g_str_has_prefix (path, "/res")) 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; gchar* contents;
gsize length; 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); length, NULL);
gchar* mime_type = g_content_type_get_mime_type (content_type); gchar* mime_type = g_content_type_get_mime_type (content_type);
g_free (content_type); g_free (content_type);
@ -994,7 +1022,7 @@ res_server_handler_cb (SoupServer* res_server,
} }
else else
soup_message_set_status (msg, 404); soup_message_set_status (msg, 404);
g_free (filename); g_free (filepath);
} }
else if (g_str_has_prefix (path, "/stock/")) else if (g_str_has_prefix (path, "/stock/"))
{ {

View file

@ -151,6 +151,9 @@ gboolean
sokoke_remove_path (const gchar* path, sokoke_remove_path (const gchar* path,
gboolean ignore_errors); gboolean ignore_errors);
gchar*
sokoke_find_data_filename (const gchar* filename);
SoupServer* SoupServer*
sokoke_get_res_server (void); sokoke_get_res_server (void);