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 }