diff src/blist.c @ 6846:8ab95f4c9800

[gaim-migrate @ 7391] Added new buddy icon caching code. Each GaimBuddy has its own icon, and the complete list of all icons is now stored in a set of hashtables for quick retrieval. Buddy icons now live much happier in the core, with the magma and tooth fairies (that's where they really live). committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Mon, 15 Sep 2003 07:35:49 +0000
parents 549a0bbbf73d
children 82607477da69
line wrap: on
line diff
--- a/src/blist.c	Mon Sep 15 02:23:58 2003 +0000
+++ b/src/blist.c	Mon Sep 15 07:35:49 2003 +0000
@@ -37,6 +37,7 @@
 struct gaim_buddy_list *gaimbuddylist = NULL;
 static struct gaim_blist_ui_ops *blist_ui_ops = NULL;
 
+
 /*****************************************************************************
  * Private Utility functions                                                 *
  *****************************************************************************/
@@ -494,6 +495,84 @@
 	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)
+{
+	g_return_if_fail(buddy != NULL);
+
+	if (buddy->icon == icon)
+		return;
+
+	if (buddy->icon != NULL)
+		gaim_buddy_icon_unref(buddy->icon);
+
+	buddy->icon = (icon == NULL ? NULL : gaim_buddy_icon_ref(icon));
+
+	write_buddy_icon(buddy, icon);
+
+	gaim_blist_update_buddy_icon(buddy);
+}
+
+GaimBuddyIcon *
+gaim_buddy_get_icon(const GaimBuddy *buddy)
+{
+	g_return_val_if_fail(buddy != NULL, NULL);
+
+	return buddy->icon;
+}
+
 void gaim_blist_add_chat(GaimBlistChat *chat, GaimGroup *group, GaimBlistNode *node)
 {
 	GaimBlistNode *n = node, *cnode = (GaimBlistNode*)chat;
@@ -977,6 +1056,9 @@
 	if(buddy->timer > 0)
 		g_source_remove(buddy->timer);
 
+	if (buddy->icon != NULL)
+		gaim_buddy_icon_unref(buddy->icon);
+
 	ops->remove(gaimbuddylist, node);
 	g_hash_table_destroy(buddy->settings);
 	g_free(buddy->name);