/pidgin/main: 49018c6bf755: Imgstore: implement with PurpleImage...
Tomasz Wasilczyk
twasilczyk at pidgin.im
Wed Apr 9 19:23:59 EDT 2014
Changeset: 49018c6bf75512b67d079f9e300dd041f6dcb309
Author: Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date: 2014-04-10 01:23 +0200
Branch: default
URL: https://hg.pidgin.im/pidgin/main/rev/49018c6bf755
Description:
Imgstore: implement with PurpleImage and purple_image_store
diffstat:
libpurple/core.c | 3 -
libpurple/image-store.c | 8 +
libpurple/image.c | 4 +-
libpurple/image.h | 2 +-
libpurple/imgstore.c | 180 +++--------------------------------
libpurple/imgstore.h | 242 +----------------------------------------------
pidgin/gtkaccount.c | 3 +
7 files changed, 40 insertions(+), 402 deletions(-)
diffs (truncated from 653 to 300 lines):
diff --git a/libpurple/core.c b/libpurple/core.c
--- a/libpurple/core.c
+++ b/libpurple/core.c
@@ -32,7 +32,6 @@
#include "http.h"
#include "idle.h"
#include "image-store.h"
-#include "imgstore.h"
#include "keyring.h"
#include "network.h"
#include "notify.h"
@@ -179,7 +178,6 @@ purple_core_init(const char *ui)
purple_theme_manager_init();
/* The buddy icon code uses the imgstore, so init it early. */
- purple_imgstore_init();
_purple_image_store_init();
/* Accounts use status, buddy icons and connection signals, so
@@ -280,7 +278,6 @@ purple_core_quit(void)
purple_xfers_uninit();
purple_proxy_uninit();
purple_dnsquery_uninit();
- purple_imgstore_uninit();
_purple_image_store_uninit();
purple_network_uninit();
diff --git a/libpurple/image-store.c b/libpurple/image-store.c
--- a/libpurple/image-store.c
+++ b/libpurple/image-store.c
@@ -120,6 +120,12 @@ remove_temporary(gpointer _image)
return G_SOURCE_REMOVE;
}
+static void
+cancel_temporary(gpointer key, gpointer value, gpointer _unused)
+{
+ purple_timeout_remove(GPOINTER_TO_INT(key));
+}
+
guint
purple_image_store_add_temporary(PurpleImage *image)
{
@@ -129,6 +135,7 @@ purple_image_store_add_temporary(PurpleI
g_return_val_if_fail(PURPLE_IS_IMAGE(image), 0);
id = image_get_id(image);
+ /* XXX: add_temporary doesn't extend previous temporary call, sorry */
if (id > 0)
return id;
@@ -164,6 +171,7 @@ void
g_slist_free_full(perm_images, g_object_unref);
perm_images = NULL;
+ g_hash_table_foreach(temp_images, cancel_temporary, NULL);
g_hash_table_destroy(temp_images);
temp_images = NULL;
diff --git a/libpurple/image.c b/libpurple/image.c
--- a/libpurple/image.c
+++ b/libpurple/image.c
@@ -225,7 +225,7 @@ purple_image_has_failed(PurpleImage *ima
}
gsize
-purple_image_get_data_size(PurpleImage *image)
+purple_image_get_size(PurpleImage *image)
{
PurpleImagePrivate *priv;
priv = PURPLE_IMAGE_GET_PRIVATE(image);
@@ -264,7 +264,7 @@ purple_image_get_extenstion(PurpleImage
if (priv->extension)
return priv->extension;
- if (purple_image_get_data_size(image) < 4)
+ if (purple_image_get_size(image) < 4)
return NULL;
data = purple_image_get_data(image);
diff --git a/libpurple/image.h b/libpurple/image.h
--- a/libpurple/image.h
+++ b/libpurple/image.h
@@ -96,7 +96,7 @@ gboolean
purple_image_has_failed(PurpleImage *image);
gsize
-purple_image_get_data_size(PurpleImage *image);
+purple_image_get_size(PurpleImage *image);
gpointer
purple_image_get_data(PurpleImage *image);
diff --git a/libpurple/imgstore.c b/libpurple/imgstore.c
--- a/libpurple/imgstore.c
+++ b/libpurple/imgstore.c
@@ -27,186 +27,87 @@
#include "imgstore.h"
#include "util.h"
-static GHashTable *imgstore;
-static unsigned int nextid = 0;
-
-/*
- * NOTE: purple_imgstore_new() creates these without zeroing the memory, so
- * NOTE: make sure to update that function when adding members.
- */
-struct _PurpleStoredImage
-{
- int id;
- guint8 refcount;
- size_t size; /* The image data's size. */
- char *filename; /* The filename (for the UI) */
- gpointer data; /* The image data. */
-};
-
PurpleStoredImage *
purple_imgstore_new(gpointer data, size_t size, const char *filename)
{
- PurpleStoredImage *img;
-
- g_return_val_if_fail(data != NULL, NULL);
- g_return_val_if_fail(size > 0, NULL);
-
- img = g_new(PurpleStoredImage, 1);
- PURPLE_DBUS_REGISTER_POINTER(img, PurpleStoredImage);
- img->data = data;
- img->size = size;
- img->filename = g_strdup(filename);
- img->refcount = 1;
- img->id = 0;
-
- return img;
+ return purple_image_new_from_data(data, size);
}
PurpleStoredImage *
purple_imgstore_new_from_file(const char *path)
{
- gchar *data = NULL;
- size_t len;
- GError *err = NULL;
-
- g_return_val_if_fail(path != NULL && *path != '\0', NULL);
-
- if (!g_file_get_contents(path, &data, &len, &err)) {
- purple_debug_error("imgstore", "Error reading %s: %s\n",
- path, err->message);
- g_error_free(err);
- return NULL;
- }
- return purple_imgstore_new(data, len, path);
+ return purple_image_new_from_file(path, TRUE);
}
int
purple_imgstore_new_with_id(gpointer data, size_t size, const char *filename)
{
- PurpleStoredImage *image;
- int id;
+ PurpleImage *image = purple_image_new_from_data(data, size);
- image = purple_imgstore_new(data, size, filename);
- id = purple_imgstore_add_with_id(image);
- purple_imgstore_unref(image);
-
- return id;
+ return purple_image_store_add(image);
}
int
purple_imgstore_add_with_id(PurpleStoredImage *image)
{
- g_return_val_if_fail(image != NULL, 0);
-
- if (image->id != 0)
- return image->id;
-
- /*
- * Use the next unused id number. We do it in a loop on the
- * off chance that nextid wraps back around to 0 and the hash
- * table still contains entries from the first time around.
- */
- do {
- image->id = ++nextid;
- } while (image->id == 0 || g_hash_table_lookup(imgstore, &(image->id)) != NULL);
-
- purple_imgstore_ref(image);
- g_hash_table_insert(imgstore, &(image->id), image);
-
- return image->id;
+ return purple_image_store_add(image);
}
PurpleStoredImage *purple_imgstore_find_by_id(int id)
{
- PurpleStoredImage *img = g_hash_table_lookup(imgstore, &id);
-
- if (img != NULL)
- purple_debug_misc("imgstore", "retrieved image id %d\n", img->id);
-
- return img;
+ return purple_image_store_get(id);
}
gconstpointer purple_imgstore_get_data(PurpleStoredImage *img)
{
- g_return_val_if_fail(img != NULL, NULL);
-
- return img->data;
+ return purple_image_get_data(img);
}
size_t purple_imgstore_get_size(PurpleStoredImage *img)
{
- g_return_val_if_fail(img != NULL, 0);
-
- return img->size;
+ return purple_image_get_size(img);
}
const char *purple_imgstore_get_filename(const PurpleStoredImage *img)
{
- g_return_val_if_fail(img != NULL, NULL);
-
- return img->filename;
+ return purple_image_get_path((PurpleImage *)img);
}
const char *purple_imgstore_get_extension(PurpleStoredImage *img)
{
- g_return_val_if_fail(img != NULL, NULL);
-
- return purple_util_get_image_extension(img->data, img->size);
+ return purple_image_get_extenstion(img);
}
void purple_imgstore_ref_by_id(int id)
{
- PurpleStoredImage *img = purple_imgstore_find_by_id(id);
+ PurpleImage *img = purple_image_store_get(id);
g_return_if_fail(img != NULL);
- purple_imgstore_ref(img);
+ g_object_ref(img);
}
void purple_imgstore_unref_by_id(int id)
{
- PurpleStoredImage *img = purple_imgstore_find_by_id(id);
+ PurpleImage *img = purple_image_store_get(id);
g_return_if_fail(img != NULL);
- purple_imgstore_unref(img);
+ g_object_unref(img);
}
PurpleStoredImage *
purple_imgstore_ref(PurpleStoredImage *img)
{
- g_return_val_if_fail(img != NULL, NULL);
-
- img->refcount++;
+ g_object_ref(img);
return img;
}
-PurpleStoredImage *
+void
purple_imgstore_unref(PurpleStoredImage *img)
{
- if (img == NULL)
- return NULL;
-
- g_return_val_if_fail(img->refcount > 0, NULL);
-
- img->refcount--;
-
- if (img->refcount == 0)
- {
- purple_signal_emit(purple_imgstore_get_handle(),
- "image-deleting", img);
- if (img->id)
- g_hash_table_remove(imgstore, &img->id);
-
- g_free(img->data);
- g_free(img->filename);
- PURPLE_DBUS_UNREGISTER_POINTER(img);
- g_free(img);
- img = NULL;
- }
-
- return img;
+ g_object_unref(img);
}
void *
@@ -216,50 +117,3 @@ purple_imgstore_get_handle()
More information about the Commits
mailing list