6982
|
1 /**
|
|
2 * @file imgstore.h IM Image Store API
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
8046
|
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.
|
6982
|
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
|
8962
|
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
|
6982
|
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 if (img->data)
|
|
86 g_free(img->data);
|
|
87 if (img->filename)
|
|
88 g_free(img->filename);
|
|
89 g_free(img);
|
|
90 }
|
|
91
|
|
92 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "freed image id %d\n", priv->id);
|
|
93
|
|
94 g_free(priv);
|
|
95
|
|
96 imgstore = g_slist_remove(imgstore, priv);
|
|
97 }
|
|
98
|
|
99 /* public functions */
|
|
100
|
|
101 int gaim_imgstore_add(const void *data, size_t size, const char *filename) {
|
|
102 GaimStoredImagePriv *priv;
|
|
103 GaimStoredImage *img;
|
|
104
|
|
105 g_return_val_if_fail(data != NULL, 0);
|
|
106 g_return_val_if_fail(size > 0, 0);
|
|
107
|
|
108 img = g_new0(GaimStoredImage, 1);
|
|
109 img->data = g_memdup(data, size);
|
|
110 img->size = size;
|
|
111 img->filename = g_strdup(filename);
|
|
112
|
|
113 priv = g_new0(GaimStoredImagePriv, 1);
|
|
114 priv->id = ++nextid;
|
|
115 priv->refcount = 1;
|
|
116 priv->img = img;
|
|
117
|
|
118 imgstore = g_slist_append(imgstore, priv);
|
|
119 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "added image id %d\n", priv->id);
|
|
120
|
|
121 return priv->id;
|
|
122 }
|
|
123
|
|
124 GaimStoredImage *gaim_imgstore_get(int id) {
|
|
125 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
126
|
|
127 g_return_val_if_fail(priv != NULL, NULL);
|
|
128
|
|
129 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "retrieved image id %d\n", priv->id);
|
|
130
|
|
131 return priv->img;
|
|
132 }
|
|
133
|
8962
|
134 gpointer gaim_imgstore_get_data(GaimStoredImage *i) {
|
|
135 return i->data;
|
|
136 }
|
|
137
|
|
138 size_t gaim_imgstore_get_size(GaimStoredImage *i) {
|
|
139 return i->size;
|
|
140 }
|
|
141
|
|
142 const char *gaim_imgstore_get_filename(GaimStoredImage *i) {
|
|
143 return i->filename;
|
|
144 }
|
|
145
|
6982
|
146 void gaim_imgstore_ref(int id) {
|
|
147 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
148
|
|
149 g_return_if_fail(priv != NULL);
|
|
150
|
|
151 (priv->refcount)++;
|
|
152
|
|
153 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "referenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
154 }
|
|
155
|
|
156 void gaim_imgstore_unref(int id) {
|
|
157 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
158
|
|
159 g_return_if_fail(priv != NULL);
|
|
160 g_return_if_fail(priv->refcount > 0);
|
|
161
|
|
162 (priv->refcount)--;
|
|
163
|
|
164 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "unreferenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
165
|
|
166 if (priv->refcount == 0)
|
|
167 gaim_imgstore_free_priv(priv);
|
|
168 }
|