6982
|
1 /**
|
|
2 * @file imgstore.h IM Image Store API
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Copyright (C) 2003 Robert McQueen <robot101@debian.org>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 *
|
|
23 */
|
|
24
|
|
25 #include <glib.h>
|
|
26 #include <debug.h>
|
|
27 #include <imgstore.h>
|
|
28
|
|
29 static GSList *imgstore = NULL;
|
|
30 static int nextid = 0;
|
|
31
|
|
32 typedef struct
|
|
33 {
|
|
34 int id;
|
|
35 int refcount;
|
|
36 GaimStoredImage *img;
|
|
37 } GaimStoredImagePriv;
|
|
38
|
|
39 /* private functions */
|
|
40
|
|
41 static GaimStoredImagePriv *gaim_imgstore_get_priv(int id) {
|
|
42 GSList *tmp = imgstore;
|
|
43 GaimStoredImagePriv *priv = NULL;
|
|
44
|
|
45 g_return_val_if_fail(id > 0, NULL);
|
|
46
|
|
47 while (tmp && !priv) {
|
|
48 GaimStoredImagePriv *tmp_priv = tmp->data;
|
|
49
|
|
50 if (tmp_priv->id == id)
|
|
51 priv = tmp_priv;
|
|
52
|
|
53 tmp = tmp->next;
|
|
54 }
|
|
55
|
|
56 if (!priv)
|
|
57 gaim_debug(GAIM_DEBUG_ERROR, "imgstore", "failed to find image id %d\n", id);
|
|
58
|
|
59 return priv;
|
|
60 }
|
|
61
|
|
62 static void gaim_imgstore_free_priv(GaimStoredImagePriv *priv) {
|
|
63 GaimStoredImage *img = NULL;
|
|
64
|
|
65 g_return_if_fail(priv != NULL);
|
|
66
|
|
67 img = priv->img;
|
|
68 if (img) {
|
|
69 if (img->data)
|
|
70 g_free(img->data);
|
|
71 if (img->filename)
|
|
72 g_free(img->filename);
|
|
73 g_free(img);
|
|
74 }
|
|
75
|
|
76 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "freed image id %d\n", priv->id);
|
|
77
|
|
78 g_free(priv);
|
|
79
|
|
80 imgstore = g_slist_remove(imgstore, priv);
|
|
81 }
|
|
82
|
|
83 /* public functions */
|
|
84
|
|
85 int gaim_imgstore_add(const void *data, size_t size, const char *filename) {
|
|
86 GaimStoredImagePriv *priv;
|
|
87 GaimStoredImage *img;
|
|
88
|
|
89 g_return_val_if_fail(data != NULL, 0);
|
|
90 g_return_val_if_fail(size > 0, 0);
|
|
91
|
|
92 img = g_new0(GaimStoredImage, 1);
|
|
93 img->data = g_memdup(data, size);
|
|
94 img->size = size;
|
|
95 img->filename = g_strdup(filename);
|
|
96
|
|
97 priv = g_new0(GaimStoredImagePriv, 1);
|
|
98 priv->id = ++nextid;
|
|
99 priv->refcount = 1;
|
|
100 priv->img = img;
|
|
101
|
|
102 imgstore = g_slist_append(imgstore, priv);
|
|
103 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "added image id %d\n", priv->id);
|
|
104
|
|
105 return priv->id;
|
|
106 }
|
|
107
|
|
108 GaimStoredImage *gaim_imgstore_get(int id) {
|
|
109 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
110
|
|
111 g_return_val_if_fail(priv != NULL, NULL);
|
|
112
|
|
113 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "retrieved image id %d\n", priv->id);
|
|
114
|
|
115 return priv->img;
|
|
116 }
|
|
117
|
|
118 void gaim_imgstore_ref(int id) {
|
|
119 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
120
|
|
121 g_return_if_fail(priv != NULL);
|
|
122
|
|
123 (priv->refcount)++;
|
|
124
|
|
125 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "referenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
126 }
|
|
127
|
|
128 void gaim_imgstore_unref(int id) {
|
|
129 GaimStoredImagePriv *priv = gaim_imgstore_get_priv(id);
|
|
130
|
|
131 g_return_if_fail(priv != NULL);
|
|
132 g_return_if_fail(priv->refcount > 0);
|
|
133
|
|
134 (priv->refcount)--;
|
|
135
|
|
136 gaim_debug(GAIM_DEBUG_INFO, "imgstore", "unreferenced image id %d (count now %d)\n", priv->id, priv->refcount);
|
|
137
|
|
138 if (priv->refcount == 0)
|
|
139 gaim_imgstore_free_priv(priv);
|
|
140 }
|