changeset 6886:b5fb1d5282e5

[gaim-migrate @ 7432] Buddy icon caching can now be enabled/disabled in the core, and the cache dir can be changed. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Thu, 18 Sep 2003 07:11:55 +0000
parents 66dd420d3d23
children 5d48764e711c
files src/blist.c src/buddyicon.c src/buddyicon.h
diffstat 3 files changed, 135 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/src/blist.c	Thu Sep 18 05:39:44 2003 +0000
+++ b/src/blist.c	Thu Sep 18 07:11:55 2003 +0000
@@ -496,58 +496,6 @@
 	return b;
 }
 
-static void
-write_buddy_icon(GaimBuddy *buddy, GaimBuddyIcon *icon)
-{
-	const void *data;
-	size_t len;
-	char *random;
-	char *filename;
-	char *dirname;
-	char *old_icon;
-	FILE *file = NULL;
-
-	data = gaim_buddy_icon_get_data(icon, &len);
-
-	random   = g_strdup_printf("%x", g_random_int());
-	filename = g_build_filename(gaim_user_dir(), "icons", random, NULL);
-	dirname  = g_build_filename(gaim_user_dir(), "icons", NULL);
-	old_icon = gaim_buddy_get_setting(buddy, "buddy_icon");
-
-	g_free(random);
-
-	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR))
-	{
-		gaim_debug_info("buddy icons", "Creating icon cache directory.\n");
-
-		if (mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
-		{
-			gaim_debug_error("buddy icons",
-							 "Unable to create directory %s: %s\n",
-							 dirname, strerror(errno));
-		}
-	}
-
-	g_free(dirname);
-
-	if ((file = fopen(filename, "wb")) != NULL)
-	{
-		fwrite(data, 1, len, file);
-		fclose(file);
-	}
-
-	if (old_icon != NULL)
-	{
-		unlink(old_icon);
-		g_free(old_icon);
-	}
-
-	gaim_buddy_set_setting(buddy, "buddy_icon", filename);
-	gaim_blist_save();
-
-	g_free(filename);
-}
-
 void
 gaim_buddy_set_icon(GaimBuddy *buddy, GaimBuddyIcon *icon)
 {
@@ -561,7 +509,7 @@
 
 	buddy->icon = (icon == NULL ? NULL : gaim_buddy_icon_ref(icon));
 
-	write_buddy_icon(buddy, icon);
+	gaim_buddy_icon_cache(icon, buddy);
 
 	gaim_blist_update_buddy_icon(buddy);
 }
--- a/src/buddyicon.c	Thu Sep 18 05:39:44 2003 +0000
+++ b/src/buddyicon.c	Thu Sep 18 07:11:55 2003 +0000
@@ -23,8 +23,12 @@
 #include "internal.h"
 #include "buddyicon.h"
 #include "conversation.h"
+#include "debug.h"
+#include "util.h"
 
 static GHashTable *account_cache = NULL;
+static char       *cache_dir     = NULL;
+static gboolean    icon_caching  = TRUE;
 
 GaimBuddyIcon *
 gaim_buddy_icon_new(GaimAccount *account, const char *username,
@@ -154,6 +158,62 @@
 }
 
 void
+gaim_buddy_icon_cache(GaimBuddyIcon *icon, GaimBuddy *buddy)
+{
+	const void *data;
+	const char *dirname;
+	char *random;
+	char *filename;
+	char *old_icon;
+	size_t len;
+	FILE *file = NULL;
+
+	g_return_if_fail(icon  != NULL);
+	g_return_if_fail(buddy != NULL);
+
+	if (!gaim_buddy_icons_is_caching())
+		return;
+
+	data = gaim_buddy_icon_get_data(icon, &len);
+
+	random   = g_strdup_printf("%x", g_random_int());
+	dirname  = gaim_buddy_icons_get_cache_dir();
+	filename = g_build_filename(dirname, random, NULL);
+	old_icon = gaim_buddy_get_setting(buddy, "buddy_icon");
+
+	g_free(random);
+
+	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR))
+	{
+		gaim_debug_info("buddy icons", "Creating icon cache directory.\n");
+
+		if (mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
+		{
+			gaim_debug_error("buddy icons",
+							 "Unable to create directory %s: %s\n",
+							 dirname, strerror(errno));
+		}
+	}
+
+	if ((file = fopen(filename, "wb")) != NULL)
+	{
+		fwrite(data, 1, len, file);
+		fclose(file);
+	}
+
+	if (old_icon != NULL)
+	{
+		unlink(old_icon);
+		g_free(old_icon);
+	}
+
+	gaim_buddy_set_setting(buddy, "buddy_icon", filename);
+	gaim_blist_save();
+
+	g_free(filename);
+}
+
+void
 gaim_buddy_icon_set_account(GaimBuddyIcon *icon, GaimAccount *account)
 {
 	g_return_if_fail(icon    != NULL);
@@ -249,6 +309,35 @@
 	return g_hash_table_lookup(icon_cache, username);
 }
 
+void
+gaim_buddy_icons_set_caching(gboolean caching)
+{
+	icon_caching = caching;
+}
+
+gboolean
+gaim_buddy_icons_is_caching(void)
+{
+	return icon_caching;
+}
+
+void
+gaim_buddy_icons_set_cache_dir(const char *dir)
+{
+	g_return_if_fail(cache_dir != NULL);
+
+	if (cache_dir != NULL)
+		g_free(cache_dir);
+
+	cache_dir = g_strdup(dir);
+}
+
+const char *
+gaim_buddy_icons_get_cache_dir(void)
+{
+	return cache_dir;
+}
+
 void *
 gaim_buddy_icons_get_handle()
 {
@@ -263,6 +352,8 @@
 	account_cache = g_hash_table_new_full(
 		g_direct_hash, g_direct_equal,
 		NULL, (GFreeFunc)g_hash_table_destroy);
+
+	cache_dir = g_build_filename(gaim_user_dir(), "icons", NULL);
 }
 
 void
--- a/src/buddyicon.h	Thu Sep 18 05:39:44 2003 +0000
+++ b/src/buddyicon.h	Thu Sep 18 07:11:55 2003 +0000
@@ -94,6 +94,14 @@
 void gaim_buddy_icon_update(GaimBuddyIcon *icon);
 
 /**
+ * Caches a buddy icon associated with a specific buddy to disk.
+ *
+ * @param icon  The buddy icon.
+ * @param buddy The buddy that this icon belongs to.
+ */
+void gaim_buddy_icon_cache(GaimBuddyIcon *icon, GaimBuddy *buddy);
+
+/**
  * Sets the buddy icon's account.
  *
  * @param icon    The buddy icon.
@@ -176,6 +184,41 @@
 									 const char *username);
 
 /**
+ * Sets whether or not buddy icon caching is enabled.
+ *
+ * @param caching TRUE of buddy icon caching should be enabled, or
+ *                FALSE otherwise.
+ */
+void gaim_buddy_icons_set_caching(gboolean caching);
+
+/**
+ * Returns whether or not buddy icon caching should be enabled.
+ *
+ * The default is TRUE, unless otherwise specified by
+ * gaim_buddy_icons_set_caching().
+ *
+ * @return TRUE if buddy icon caching is enabled, or FALSE otherwise.
+ */
+gboolean gaim_buddy_icons_is_caching(void);
+
+/**
+ * Sets the directory used to store buddy icon cache files.
+ *
+ * @param dir The directory to store buddy icon cache files to.
+ */
+void gaim_buddy_icons_set_cache_dir(const char *cache_dir);
+
+/**
+ * Returns the directory used to store buddy icon cache files.
+ *
+ * The default directory is GAIMDIR/icons, unless otherwise specified
+ * by gaim_buddy_icons_set_cache_dir().
+ *
+ * @return The directory to store buddy icon cache files to.
+ */
+const char *gaim_buddy_icons_get_cache_dir(void);
+
+/**
  * Returns the buddy icon subsystem handle.
  *
  * @return The subsystem handle.