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
|
|
34 typedef struct
|
|
35 {
|
|
36 int id;
|
|
37 int refcount;
|
|
38 GaimStoredImage *img;
|
|
39 } GaimStoredImagePriv;
|
|
40
|
|
41 /* private functions */
|
|
42
|
|
43 static GaimStoredImagePriv *gaim_imgstore_get_priv(int id) {
|
|
44 GSList *tmp = imgstore;
|
|
45 GaimStoredImagePriv *priv = NULL;
|
|
46
|
|
47 g_return_val_if_fail(id > 0, NULL);
|
|
48
|
|
49 while (tmp && !priv) {
|
|
50 GaimStoredImagePriv *tmp_priv = tmp->data;
|
|
51
|
|
52 if (tmp_priv->id == id)
|
|
53 priv = tmp_priv;
|
|
54
|
|
55 tmp = tmp->next;
|
|
56 }
|
|
57
|
|
58 if (!priv)
|
|
59 gaim_debug(GAIM_DEBUG_ERROR, "imgstore", "failed to find image id %d\n", id);
|
|
60
|
|
61 return priv;
|
|
62 }
|
|
63
|
|
64 static void gaim_imgstore_free_priv(GaimStoredImagePriv *priv) {
|
|
65 GaimStoredImage *img = NULL;
|
|
66
|
|
67 g_return_if_fail(priv != NULL);
|
|
68
|
|
69 img = priv->img;
|
|
70 if (img) {
|
|
71 if (img->data)
|
|
72 g_free(img->data);
|
|
73 if (img->filename)
|
|
74 g_free(img->filename);
|
|
75 g_free(img);
|
|
76 }
|
|
77
|
|
78 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "freed image id %d\n", priv->id);
|
|
79
|
|
80 g_free(priv);
|
|
81
|
|
82 imgstore = g_slist_remove(imgstore, priv);
|
|
83 }
|
|
84
|
|
85 /* public functions */
|
|
86
|
|
87 int gaim_imgstore_add(const void *data, size_t size, const char *filename) {
|
|
88 GaimStoredImagePriv *priv;
|
|
89 GaimStoredImage *img;
|
|
90
|
|
91 g_return_val_if_fail(data != NULL, 0);
|
|
92 g_return_val_if_fail(size > 0, 0);
|
|
93
|
|
94 img = g_new0(GaimStoredImage, 1);
|
|
95 img->data = g_memdup(data, size);
|
|
96 img->size = size;
|
|
97 img->filename = g_strdup(filename);
|
|
98
|
|
99 priv = g_new0(GaimStoredImagePriv, 1);
|
|
100 priv->id = ++nextid;
|
|
101 priv->refcount = 1;
|
|
102 priv->img = img;
|
|
103
|
|
104 imgstore = g_slist_append(imgstore, priv);
|
|
105 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "added image id %d\n", priv->id);
|
|
106
|
|
107 return priv->id;
|
|
108 }
|
|
109
|
|
110 GaimStoredImage *gaim_imgstore_get(int id) {
|
|
111 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
112
|
|
113 g_return_val_if_fail(priv != NULL, NULL);
|
|
114
|
|
115 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "retrieved image id %d\n", priv->id);
|
|
116
|
|
117 return priv->img;
|
|
118 }
|
|
119
|
|
120 void gaim_imgstore_ref(int id) {
|
|
121 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
122
|
|
123 g_return_if_fail(priv != NULL);
|
|
124
|
|
125 (priv->refcount)++;
|
|
126
|
|
127 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "referenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
128 }
|
|
129
|
|
130 void gaim_imgstore_unref(int id) {
|
|
131 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
132
|
|
133 g_return_if_fail(priv != NULL);
|
|
134 g_return_if_fail(priv->refcount > 0);
|
|
135
|
|
136 (priv->refcount)--;
|
|
137
|
|
138 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "unreferenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
139
|
|
140 if (priv->refcount == 0)
|
|
141 gaim_imgstore_free_priv(priv);
|
|
142 }
|