comparison libpurple/imgstore.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file imgstore.h IM Image Store API
3 * @ingroup core
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27 #include <glib.h>
28 #include "debug.h"
29 #include "imgstore.h"
30
31 static GSList *imgstore = NULL;
32 static int nextid = 0;
33
34 /**
35 * Stored image
36 *
37 * Represents a single IM image awaiting display and/or transmission.
38 * Now that this type is basicly private too, these two structs could
39 * probably be combined.
40 */
41 struct _GaimStoredImage
42 {
43 char *data; /**< The image data. */
44 size_t size; /**< The image data's size. */
45 char *filename; /**< The filename (for the UI) */
46 };
47
48 typedef struct
49 {
50 int id;
51 int refcount;
52 GaimStoredImage *img;
53 } GaimStoredImagePriv;
54
55 /* private functions */
56
57 static GaimStoredImagePriv *gaim_imgstore_get_priv(int id) {
58 GSList *tmp = imgstore;
59 GaimStoredImagePriv *priv = NULL;
60
61 g_return_val_if_fail(id > 0, NULL);
62
63 while (tmp && !priv) {
64 GaimStoredImagePriv *tmp_priv = tmp->data;
65
66 if (tmp_priv->id == id)
67 priv = tmp_priv;
68
69 tmp = tmp->next;
70 }
71
72 if (!priv)
73 gaim_debug(GAIM_DEBUG_ERROR, "imgstore", "failed to find image id %d\n", id);
74
75 return priv;
76 }
77
78 static void gaim_imgstore_free_priv(GaimStoredImagePriv *priv) {
79 GaimStoredImage *img = NULL;
80
81 g_return_if_fail(priv != NULL);
82
83 img = priv->img;
84 if (img) {
85 g_free(img->data);
86 g_free(img->filename);
87 g_free(img);
88 }
89
90 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "freed image id %d\n", priv->id);
91
92 g_free(priv);
93
94 imgstore = g_slist_remove(imgstore, priv);
95 }
96
97 /* public functions */
98
99 int gaim_imgstore_add(const void *data, size_t size, const char *filename) {
100 GaimStoredImagePriv *priv;
101 GaimStoredImage *img;
102
103 g_return_val_if_fail(data != NULL, 0);
104 g_return_val_if_fail(size > 0, 0);
105
106 img = g_new0(GaimStoredImage, 1);
107 img->data = g_memdup(data, size);
108 img->size = size;
109 img->filename = g_strdup(filename);
110
111 priv = g_new0(GaimStoredImagePriv, 1);
112 priv->id = ++nextid;
113 priv->refcount = 1;
114 priv->img = img;
115
116 imgstore = g_slist_append(imgstore, priv);
117 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "added image id %d\n", priv->id);
118
119 return priv->id;
120 }
121
122 GaimStoredImage *gaim_imgstore_get(int id) {
123 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
124
125 g_return_val_if_fail(priv != NULL, NULL);
126
127 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "retrieved image id %d\n", priv->id);
128
129 return priv->img;
130 }
131
132 gpointer gaim_imgstore_get_data(GaimStoredImage *i) {
133 return i->data;
134 }
135
136 size_t gaim_imgstore_get_size(GaimStoredImage *i) {
137 return i->size;
138 }
139
140 const char *gaim_imgstore_get_filename(GaimStoredImage *i) {
141 return i->filename;
142 }
143
144 void gaim_imgstore_ref(int id) {
145 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
146
147 g_return_if_fail(priv != NULL);
148
149 (priv->refcount)++;
150
151 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "referenced image id %d (count now %d)\n", priv->id, priv->refcount);
152 }
153
154 void gaim_imgstore_unref(int id) {
155 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
156
157 g_return_if_fail(priv != NULL);
158 g_return_if_fail(priv->refcount > 0);
159
160 (priv->refcount)--;
161
162 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "unreferenced image id %d (count now %d)\n", priv->id, priv->refcount);
163
164 if (priv->refcount == 0)
165 gaim_imgstore_free_priv(priv);
166 }