Remove useless comparison
[pidgin-git.git] / libpurple / imgstore.c
blob06c75b0e47ee542af13c6b43650ffeb892e33216
1 /**
2 * @file imgstore.c IM Image Store API
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
28 #include "internal.h"
30 #include "dbus-maybe.h"
31 #include "debug.h"
32 #include "imgstore.h"
33 #include "util.h"
35 static GHashTable *imgstore;
36 static unsigned int nextid = 0;
39 * NOTE: purple_imgstore_add() creates these without zeroing the memory, so
40 * NOTE: make sure to update that function when adding members.
42 struct _PurpleStoredImage
44 int id;
45 guint8 refcount;
46 size_t size; /**< The image data's size. */
47 char *filename; /**< The filename (for the UI) */
48 gpointer data; /**< The image data. */
51 PurpleStoredImage *
52 purple_imgstore_add(gpointer data, size_t size, const char *filename)
54 PurpleStoredImage *img;
56 g_return_val_if_fail(data != NULL, NULL);
57 g_return_val_if_fail(size > 0, NULL);
59 img = g_new(PurpleStoredImage, 1);
60 PURPLE_DBUS_REGISTER_POINTER(img, PurpleStoredImage);
61 img->data = data;
62 img->size = size;
63 img->filename = g_strdup(filename);
64 img->refcount = 1;
65 img->id = 0;
67 return img;
70 PurpleStoredImage *
71 purple_imgstore_new_from_file(const char *path)
73 gchar *data = NULL;
74 size_t len;
75 GError *err = NULL;
77 g_return_val_if_fail(path != NULL && *path != '\0', NULL);
79 if (!g_file_get_contents(path, &data, &len, &err)) {
80 purple_debug_error("imgstore", "Error reading %s: %s\n",
81 path, err->message);
82 g_error_free(err);
83 return NULL;
85 return purple_imgstore_add(data, len, path);
88 int
89 purple_imgstore_add_with_id(gpointer data, size_t size, const char *filename)
91 PurpleStoredImage *img = purple_imgstore_add(data, size, filename);
92 if (!img) {
93 return 0;
97 * Use the next unused id number. We do it in a loop on the
98 * off chance that nextid wraps back around to 0 and the hash
99 * table still contains entries from the first time around.
101 do {
102 img->id = ++nextid;
103 } while (img->id == 0 || g_hash_table_lookup(imgstore, &(img->id)) != NULL);
105 g_hash_table_insert(imgstore, &(img->id), img);
107 return img->id;
110 PurpleStoredImage *purple_imgstore_find_by_id(int id)
112 PurpleStoredImage *img = g_hash_table_lookup(imgstore, &id);
114 if (img != NULL)
115 purple_debug_misc("imgstore", "retrieved image id %d\n", img->id);
117 return img;
120 gconstpointer purple_imgstore_get_data(PurpleStoredImage *img)
122 g_return_val_if_fail(img != NULL, NULL);
124 return img->data;
127 size_t purple_imgstore_get_size(PurpleStoredImage *img)
129 g_return_val_if_fail(img != NULL, 0);
131 return img->size;
134 const char *purple_imgstore_get_filename(const PurpleStoredImage *img)
136 g_return_val_if_fail(img != NULL, NULL);
138 return img->filename;
141 const char *purple_imgstore_get_extension(PurpleStoredImage *img)
143 g_return_val_if_fail(img != NULL, NULL);
145 return purple_util_get_image_extension(img->data, img->size);
148 void purple_imgstore_ref_by_id(int id)
150 PurpleStoredImage *img = purple_imgstore_find_by_id(id);
152 g_return_if_fail(img != NULL);
154 purple_imgstore_ref(img);
157 void purple_imgstore_unref_by_id(int id)
159 PurpleStoredImage *img = purple_imgstore_find_by_id(id);
161 g_return_if_fail(img != NULL);
163 purple_imgstore_unref(img);
166 PurpleStoredImage *
167 purple_imgstore_ref(PurpleStoredImage *img)
169 g_return_val_if_fail(img != NULL, NULL);
171 img->refcount++;
173 return img;
176 PurpleStoredImage *
177 purple_imgstore_unref(PurpleStoredImage *img)
179 if (img == NULL)
180 return NULL;
182 g_return_val_if_fail(img->refcount > 0, NULL);
184 img->refcount--;
186 if (img->refcount == 0)
188 purple_signal_emit(purple_imgstore_get_handle(),
189 "image-deleting", img);
190 if (img->id)
191 g_hash_table_remove(imgstore, &img->id);
193 g_free(img->data);
194 g_free(img->filename);
195 PURPLE_DBUS_UNREGISTER_POINTER(img);
196 g_free(img);
197 img = NULL;
200 return img;
203 void *
204 purple_imgstore_get_handle()
206 static int handle;
208 return &handle;
211 void
212 purple_imgstore_init()
214 void *handle = purple_imgstore_get_handle();
216 purple_signal_register(handle, "image-deleting",
217 purple_marshal_VOID__POINTER, NULL,
219 purple_value_new(PURPLE_TYPE_SUBTYPE,
220 PURPLE_SUBTYPE_STORED_IMAGE));
222 imgstore = g_hash_table_new(g_int_hash, g_int_equal);
225 void
226 purple_imgstore_uninit()
228 g_hash_table_destroy(imgstore);
230 purple_signals_unregister_by_instance(purple_imgstore_get_handle());