svnno****@sourc*****
svnno****@sourc*****
Fri Jan 23 14:12:46 JST 2009
Revision: 3544 http://svn.sourceforge.jp/view?root=kazehakase&view=rev&rev=3544 Author: ikezoe Date: 2009-01-23 14:12:46 +0900 (Fri, 23 Jan 2009) Log Message: ----------- * src/kz-favicon.c: Use GKeyFile instead of KzProfile. Modified Paths: -------------- kazehakase/trunk/ChangeLog kazehakase/trunk/src/kz-favicon.c Modified: kazehakase/trunk/ChangeLog =================================================================== --- kazehakase/trunk/ChangeLog 2009-01-23 03:08:36 UTC (rev 3543) +++ kazehakase/trunk/ChangeLog 2009-01-23 05:12:46 UTC (rev 3544) @@ -7,6 +7,7 @@ * src/bookmarks/kz-bookmark.c: Use g_object_set_qdata_full instead of g_object_set since g_object_set is slow. * src/bookmarks/kz-bookmark-editor.c: Use gtk_action_set_sensitive. + * src/kz-favicon.c: Use GKeyFile instead of KzProfile. 2009-01-21 Hiroyuki Ikezoe <poinc****@ikezo*****> Modified: kazehakase/trunk/src/kz-favicon.c =================================================================== --- kazehakase/trunk/src/kz-favicon.c 2009-01-23 03:08:36 UTC (rev 3543) +++ kazehakase/trunk/src/kz-favicon.c 2009-01-23 05:12:46 UTC (rev 3544) @@ -26,7 +26,6 @@ #include "kazehakase.h" #include "kz-favicon.h" -#include "kz-profile.h" #include "kz-downloader.h" #include "utils.h" #include "glib-utils.h" @@ -37,9 +36,7 @@ { GHashTable *uri_hash; GHashTable *pixbuf_hash; - KzProfile *uri_list; - - guint n_favicon; + GKeyFile *favicon_file; }; #define KZ_FAVICON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), KZ_TYPE_FAVICON, KzFaviconPrivate)) @@ -59,9 +56,7 @@ static void dispose (GObject *object); static void kz_favicon_load_uri_list (KzFavicon *kzfav); -static void kz_favicon_uri_hash_to_uri_list (gpointer key, - gpointer value, - KzFavicon *kzfav); +static void kz_favicon_store_uri_list (KzFavicon *kzfav); static void cb_downloader_load_complete (KzDownloader *dl, downloader_info_t *info); @@ -101,20 +96,31 @@ g_type_class_add_private (object_class, sizeof(KzFaviconPrivate)); } +static gchar * +get_favicon_filename (void) +{ + gchar *filename; + filename = g_build_filename(g_get_home_dir(), + "."PACKAGE, + "faviconrc", + NULL); + return filename; +} static void kz_favicon_init (KzFavicon *kzfav) { - gchar *favicon_file; - KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE (kzfav); + gchar *filename; + KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE(kzfav); - favicon_file = g_build_filename(g_get_home_dir(), - "."PACKAGE, - "faviconrc", - NULL); - priv->uri_list = kz_profile_open(favicon_file, NULL); - g_free(favicon_file); - kz_profile_set_save_each_time(priv->uri_list, FALSE); + filename = get_favicon_filename(); + priv->favicon_file = g_key_file_new(); + if (g_file_test(filename, G_FILE_TEST_EXISTS)) + { + g_key_file_load_from_file(priv->favicon_file, filename, + G_KEY_FILE_NONE, NULL); + } + g_free(filename); priv->uri_hash = g_hash_table_new_full (g_str_hash, g_str_equal, @@ -125,40 +131,22 @@ NULL, (GDestroyNotify)g_object_unref); kz_favicon_load_uri_list(kzfav); - kz_profile_close(priv->uri_list); } static void dispose (GObject *object) { - KzFavicon *kzfav = KZ_FAVICON(object); - gchar *favicon_file; - KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE (kzfav); + KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE(object); - favicon_file = g_build_filename(g_get_home_dir(), - "."PACKAGE, - "faviconrc", - NULL); - - priv->uri_list = kz_profile_open(favicon_file, NULL); - g_free(favicon_file); - kz_profile_set_save_each_time(priv->uri_list, FALSE); - - kz_profile_delete_section(priv->uri_list, "URI"); - priv->n_favicon = 0; - - /* convert hash table to KzProfileList */ - g_hash_table_foreach(priv->uri_hash, - (GHFunc)kz_favicon_uri_hash_to_uri_list, - kzfav); - + kz_favicon_store_uri_list(KZ_FAVICON(object)); g_hash_table_destroy(priv->pixbuf_hash); - - kz_profile_close(priv->uri_list); g_hash_table_destroy(priv->uri_hash); - priv->uri_list = NULL; + if (priv->favicon_file) + g_key_file_free(priv->favicon_file); + priv->favicon_file = NULL; + priv->uri_hash = NULL; priv->pixbuf_hash = NULL; @@ -191,36 +179,63 @@ static void kz_favicon_load_uri_list (KzFavicon *kzfav) { - KzProfileList *p; KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE (kzfav); - KzProfile *uri_list = priv->uri_list; - - for (p = uri_list->list; p; p = p->next) + gchar **uris; + gsize i, n_uris; + + uris = g_key_file_get_keys(priv->favicon_file, "URI", &n_uris, NULL); + if (!uris) + return; + + for (i = 0; i < n_uris; i++) { - if (p->type == KZ_PROFILE_DATA_TYPE_KEY - && g_strcmp(p->section, "URI") == 0) - { - g_hash_table_insert(priv->uri_hash, - g_strdup(p->key), - g_strdup(p->value)); - } + gchar *value; + value = g_key_file_get_value(priv->favicon_file, + "URI", + uris[i], NULL); + if (!value) + continue; + g_hash_table_insert(priv->uri_hash, + uris[i], value); } + g_free(uris); } static void -kz_favicon_uri_hash_to_uri_list (gpointer key, gpointer value, KzFavicon *kzfav) +kz_favicon_store_uri_list (KzFavicon *kzfav) { KzFaviconPrivate *priv = KZ_FAVICON_GET_PRIVATE (kzfav); - if (priv->n_favicon > MAX_FAVICONS) + gchar **uris; + gsize n_uris, length; + gchar *contents; + + uris = g_key_file_get_keys(priv->favicon_file, "URI", &n_uris, NULL); + if (!uris) return; + + if (n_uris > MAX_FAVICONS) + { + gsize i; + for (i = MAX_FAVICONS; i < n_uris; i++) + { + g_key_file_remove_key(priv->favicon_file, + "URI", uris[i], NULL); + } + } - kz_profile_set_value(priv->uri_list, "URI", - (const gchar*)key, (const gchar *)value, - strlen((gchar *)value) + 1, - KZ_PROFILE_VALUE_TYPE_STRING); + contents = g_key_file_to_data(priv->favicon_file, &length, NULL); + if (contents) + { + gchar *filename; - priv->n_favicon++; + filename = get_favicon_filename(); + g_file_set_contents(filename, contents, length, NULL); + g_free(filename); + g_free(contents); + } + + g_strfreev(uris); } @@ -268,9 +283,14 @@ } else { + gchar *key; + key = create_profile_key_from_uri(uri); + g_hash_table_insert(priv->uri_hash, - create_profile_key_from_uri(uri), - info->favicon_file_location); + key, info->favicon_file_location); + g_key_file_set_string(priv->favicon_file, + "URI", key, + info->favicon_file_location); } g_free(info);