3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "image-store.h"
24 #include "eventloop.h"
25 #include "glibcompat.h"
28 #define TEMP_IMAGE_TIMEOUT 5
30 static GHashTable
*id_to_image
= NULL
;
31 static guint last_id
= 0;
33 /* keys: timeout handle */
34 static GHashTable
*temp_images
= NULL
;
37 static GSList
*perm_images
= NULL
;
40 image_reset_id(gpointer _id
)
42 g_return_if_fail(id_to_image
!= NULL
);
44 g_hash_table_remove(id_to_image
, _id
);
48 image_set_id(PurpleImage
*image
)
50 /* Use the next unused id number. We do it in a loop on the off chance
51 * that next id wraps back around to 0 and the hash table still contains
52 * entries from the first time around.
57 if (G_UNLIKELY(last_id
== 0))
60 if (purple_image_store_get(last_id
) == NULL
)
64 g_object_set_data_full(G_OBJECT(image
), "purple-image-store-id",
65 GINT_TO_POINTER(last_id
), image_reset_id
);
66 g_hash_table_insert(id_to_image
, GINT_TO_POINTER(last_id
), image
);
71 image_get_id(PurpleImage
*image
)
73 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(image
),
74 "purple-image-store-id"));
78 purple_image_store_add(PurpleImage
*image
)
82 g_return_val_if_fail(PURPLE_IS_IMAGE(image
), 0);
84 id
= image_get_id(image
);
88 id
= image_set_id(image
);
91 perm_images
= g_slist_prepend(perm_images
, image
);
97 purple_image_store_add_weak(PurpleImage
*image
)
101 g_return_val_if_fail(PURPLE_IS_IMAGE(image
), 0);
103 id
= image_get_id(image
);
107 return image_set_id(image
);
111 remove_temporary(gpointer _image
)
113 PurpleImage
*image
= _image
;
116 handle
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(image
),
117 "purple-image-store-handle"));
119 g_hash_table_remove(temp_images
, GINT_TO_POINTER(handle
));
121 return G_SOURCE_REMOVE
;
125 cancel_temporary(gpointer key
, gpointer value
, gpointer _unused
)
127 g_source_remove(GPOINTER_TO_INT(key
));
131 purple_image_store_add_temporary(PurpleImage
*image
)
136 g_return_val_if_fail(PURPLE_IS_IMAGE(image
), 0);
138 id
= image_get_id(image
);
139 /* XXX: add_temporary doesn't extend previous temporary call, sorry */
143 id
= image_set_id(image
);
146 handle
= g_timeout_add_seconds(TEMP_IMAGE_TIMEOUT
,
147 remove_temporary
, image
);
148 g_object_set_data(G_OBJECT(image
), "purple-image-store-handle",
149 GINT_TO_POINTER(handle
));
150 g_hash_table_insert(temp_images
, GINT_TO_POINTER(handle
), image
);
156 purple_image_store_get(guint id
)
158 return g_hash_table_lookup(id_to_image
, GINT_TO_POINTER(id
));
161 /* TODO: handle PURPLE_IMAGE_STORE_STOCK_PROTOCOL */
163 purple_image_store_get_from_uri(const gchar
*uri
)
170 g_return_val_if_fail(uri
!= NULL
, NULL
);
172 if (!purple_str_has_prefix(uri
, PURPLE_IMAGE_STORE_PROTOCOL
))
175 uri
+= sizeof(PURPLE_IMAGE_STORE_PROTOCOL
) - 1;
179 longid
= g_ascii_strtoull(uri
, &endptr
, 10);
181 if (endchar
!= '\0' && endchar
!= '"' &&
182 endchar
!= '\'' && endchar
!= ' ')
191 return purple_image_store_get(id
);
195 purple_image_store_get_uri(PurpleImage
*image
)
200 g_return_val_if_fail(PURPLE_IS_IMAGE(image
), NULL
);
202 path
= purple_image_get_path(image
);
205 return g_filename_to_uri(path
, NULL
, NULL
);
207 img_id
= purple_image_store_add_weak(image
);
208 return g_strdup_printf(PURPLE_IMAGE_STORE_PROTOCOL
"%u", img_id
);
212 _purple_image_store_init(void)
214 id_to_image
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
215 temp_images
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
216 NULL
, g_object_unref
);
220 _purple_image_store_uninit(void)
222 g_slist_free_full(perm_images
, g_object_unref
);
225 g_hash_table_foreach(temp_images
, cancel_temporary
, NULL
);
226 g_hash_table_destroy(temp_images
);
229 g_hash_table_destroy(id_to_image
);