Support res directly in sokoke_find_data_filename

One allocation less each time, less fragmentation.
This commit is contained in:
Christian Dywan 2011-10-29 01:16:08 +02:00
parent b8a8272398
commit a25c34e280
6 changed files with 20 additions and 34 deletions

View file

@ -32,27 +32,22 @@ formhistory_toggle_state_cb (GtkAction* action,
static gboolean static gboolean
formhistory_prepare_js () formhistory_prepare_js ()
{ {
gchar* data_path;
gchar* autosuggest; gchar* autosuggest;
gchar* style; gchar* style;
guint i; guint i;
gchar* file; gchar* file;
data_path = g_build_filename (PACKAGE_NAME, "res", "autosuggestcontrol.js", NULL); file = sokoke_find_data_filename ("autosuggestcontrol.js", TRUE);
file = sokoke_find_data_filename (data_path);
if (!g_file_get_contents (file, &autosuggest, NULL, NULL)) if (!g_file_get_contents (file, &autosuggest, NULL, NULL))
{ {
g_free (data_path);
g_free (file); g_free (file);
return FALSE; return FALSE;
} }
g_strchomp (autosuggest); g_strchomp (autosuggest);
katze_assign (data_path, g_build_filename (PACKAGE_NAME, "res", "autosuggestcontrol.css", NULL)); katze_assign (file, sokoke_find_data_filename ("autosuggestcontrol.css", TRUE));
katze_assign (file, sokoke_find_data_filename (data_path));
if (!g_file_get_contents (file, &style, NULL, NULL)) if (!g_file_get_contents (file, &style, NULL, NULL))
{ {
g_free (data_path);
g_free (file); g_free (file);
return FALSE; return FALSE;
} }
@ -83,7 +78,6 @@ formhistory_prepare_js ()
autosuggest, autosuggest,
style); style);
g_strstrip (jsforms); g_strstrip (jsforms);
g_free (data_path);
g_free (file); g_free (file);
g_free (style); g_free (style);
g_free (autosuggest); g_free (autosuggest);

View file

@ -1348,7 +1348,7 @@ midori_app_setup (gchar** argument_vector)
else else
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
{ {
gchar* path = sokoke_find_data_filename ("locale"); gchar* path = sokoke_find_data_filename ("locale", FALSE);
bindtextdomain (GETTEXT_PACKAGE, path); bindtextdomain (GETTEXT_PACKAGE, path);
g_free (path); g_free (path);
} }

View file

@ -4853,7 +4853,7 @@ _action_help_link_activate (GtkAction* action,
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
{ {
#ifdef DOCDIR #ifdef DOCDIR
gchar* path = sokoke_find_data_filename ("doc/midori/faq.html"); gchar* path = sokoke_find_data_filename ("doc/midori/faq.html", FALSE);
uri = free_uri = g_filename_to_uri (path, NULL, NULL); uri = free_uri = g_filename_to_uri (path, NULL, NULL);
if (g_access (path, F_OK) != 0) if (g_access (path, F_OK) != 0)
{ {

View file

@ -1130,12 +1130,8 @@ midori_view_web_view_resource_request_cb (WebKitWebView* web_view,
if (g_str_has_prefix (uri, "res://")) if (g_str_has_prefix (uri, "res://"))
{ {
gchar* filename = g_build_filename ("midori", "res", &uri[6], NULL); gchar* filepath = sokoke_find_data_filename (&uri[6], TRUE);
gchar* filepath = sokoke_find_data_filename (filename); gchar* file_uri = g_filename_to_uri (filepath, NULL, NULL);
gchar* file_uri;
g_free (filename);
file_uri = g_filename_to_uri (filepath, NULL, NULL);
g_free (filepath); g_free (filepath);
webkit_network_request_set_uri (request, file_uri); webkit_network_request_set_uri (request, file_uri);
g_free (file_uri); g_free (file_uri);
@ -1426,11 +1422,9 @@ midori_view_display_error (MidoriView* view,
const gchar* try_again, const gchar* try_again,
WebKitWebFrame* web_frame) WebKitWebFrame* web_frame)
{ {
gchar* template_file = g_build_filename ("midori", "res", "error.html", NULL); gchar* path = sokoke_find_data_filename ("error.html", TRUE);
gchar* path = sokoke_find_data_filename (template_file);
gchar* template; gchar* template;
g_free (template_file);
if (g_file_get_contents (path, &template, NULL, NULL)) if (g_file_get_contents (path, &template, NULL, NULL))
{ {
gchar* title_escaped; gchar* title_escaped;
@ -3696,19 +3690,13 @@ prepare_speed_dial_html (MidoriView* view)
guint slot_count = 1, i, grid_index = 3, slot_size; guint slot_count = 1, i, grid_index = 3, slot_size;
gchar* speed_dial_head; gchar* speed_dial_head;
gchar* file_path; gchar* file_path;
gchar* file_name;
gchar** groups; gchar** groups;
g_object_get (browser, "speed-dial", &key_file, NULL); g_object_get (browser, "speed-dial", &key_file, NULL);
if (!key_file) file_path = sokoke_find_data_filename ("speeddial-head-" MIDORI_VERSION ".html", TRUE);
return g_strdup ("");
file_name = g_build_filename ("midori", "res", if (key_file != NULL
"speeddial-head-" MIDORI_VERSION ".html", NULL); && g_access (file_path, F_OK) == 0
file_path = sokoke_find_data_filename (file_name);
g_free (file_name);
if (g_access (file_path, F_OK) == 0
&& g_file_get_contents (file_path, &speed_dial_head, NULL, NULL)) && g_file_get_contents (file_path, &speed_dial_head, NULL, NULL))
{ {
gchar* header = sokoke_replace_variables (speed_dial_head, gchar* header = sokoke_replace_variables (speed_dial_head,

View file

@ -1342,8 +1342,11 @@ gchar* sokoke_find_lib_path (const gchar* folder)
* Return value: a newly allocated full path * Return value: a newly allocated full path
**/ **/
gchar* gchar*
sokoke_find_data_filename (const gchar* filename) sokoke_find_data_filename (const gchar* filename,
gboolean res)
{ {
const gchar* res1 = res ? PACKAGE_NAME : "";
const gchar* res2 = res ? "res" : "";
const gchar* const* data_dirs = g_get_system_data_dirs (); const gchar* const* data_dirs = g_get_system_data_dirs ();
guint i = 0; guint i = 0;
const gchar* data_dir; const gchar* data_dir;
@ -1351,7 +1354,7 @@ sokoke_find_data_filename (const gchar* filename)
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
gchar* install_path = g_win32_get_package_installation_directory_of_module (NULL); gchar* install_path = g_win32_get_package_installation_directory_of_module (NULL);
path = g_build_filename (install_path, "share", filename, NULL); path = g_build_filename (install_path, "share", res1, res2, filename, NULL);
g_free (install_path); g_free (install_path);
if (g_access (path, F_OK) == 0) if (g_access (path, F_OK) == 0)
return path; return path;
@ -1359,19 +1362,19 @@ sokoke_find_data_filename (const gchar* filename)
g_free (path); g_free (path);
#endif #endif
path = g_build_filename (g_get_user_data_dir (), filename, NULL); path = g_build_filename (g_get_user_data_dir (), res1, res2, filename, NULL);
if (g_access (path, F_OK) == 0) if (g_access (path, F_OK) == 0)
return path; return path;
g_free (path); g_free (path);
while ((data_dir = data_dirs[i++])) while ((data_dir = data_dirs[i++]))
{ {
path = g_build_filename (data_dir, filename, NULL); path = g_build_filename (data_dir, res1, res2, filename, NULL);
if (g_access (path, F_OK) == 0) if (g_access (path, F_OK) == 0)
return path; return path;
g_free (path); g_free (path);
} }
return g_build_filename (MDATADIR, filename, NULL); return g_build_filename (MDATADIR, res1, res2, filename, NULL);
} }
/** /**

View file

@ -148,7 +148,8 @@ gchar*
sokoke_find_lib_path (const gchar* folder); sokoke_find_lib_path (const gchar* folder);
gchar* gchar*
sokoke_find_data_filename (const gchar* filename); sokoke_find_data_filename (const gchar* filename,
gboolean res);
gchar** gchar**
sokoke_get_argv (gchar** argument_vector); sokoke_get_argv (gchar** argument_vector);