diff options
author | Tomasz Wasilczyk <twasilczyk@pidgin.im> | 2014-05-22 14:08:33 +0200 |
---|---|---|
committer | Tomasz Wasilczyk <twasilczyk@pidgin.im> | 2014-05-22 14:08:33 +0200 |
commit | 581dab12b86596d8a4fcc5070adc28ac86917439 (patch) | |
tree | 1fb3ddefcfcd0937c35561c15274a59105da6fe7 /libpurple | |
parent | 49f17cd03a5d42045cf35c425a547d72ea2df9f9 (diff) | |
download | pidgin-581dab12b86596d8a4fcc5070adc28ac86917439.tar.gz |
PurpleMessage: implement IDs and storage-by-id
Diffstat (limited to 'libpurple')
-rw-r--r-- | libpurple/core.c | 3 | ||||
-rw-r--r-- | libpurple/message.c | 59 | ||||
-rw-r--r-- | libpurple/message.h | 12 |
3 files changed, 73 insertions, 1 deletions
diff --git a/libpurple/core.c b/libpurple/core.c index 03f8601776..ef1cc3a446 100644 --- a/libpurple/core.c +++ b/libpurple/core.c @@ -33,6 +33,7 @@ #include "idle.h" #include "image-store.h" #include "keyring.h" +#include "message.h" #include "network.h" #include "notify.h" #include "plugin.h" @@ -191,6 +192,7 @@ purple_core_init(const char *ui) purple_savedstatuses_init(); purple_notify_init(); purple_certificate_init(); + _purple_message_init(); purple_conversations_init(); purple_blist_init(); purple_log_init(); @@ -301,6 +303,7 @@ purple_core_quit(void) purple_cmds_uninit(); purple_log_uninit(); + _purple_message_uninit(); /* Everything after util_uninit cannot try to write things to the * confdir nor use purple_escape_js */ diff --git a/libpurple/message.c b/libpurple/message.c index e6b8ccf377..1e3a7185ce 100644 --- a/libpurple/message.c +++ b/libpurple/message.c @@ -20,6 +20,7 @@ */ #include "internal.h" +#include "dbus-maybe.h" #include "glibcompat.h" #include "debug.h" @@ -29,6 +30,7 @@ (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MESSAGE, PurpleMessagePrivate)) typedef struct { + guint id; gchar *who; gchar *contents; PurpleMessageFlags flags; @@ -37,6 +39,7 @@ typedef struct { enum { PROP_0, + PROP_ID, PROP_WHO, PROP_CONTENTS, PROP_FLAGS, @@ -46,6 +49,8 @@ enum static GObjectClass *parent_class; static GParamSpec *properties[PROP_LAST]; +static GHashTable *messages = NULL; + /****************************************************************************** * API implementation ******************************************************************************/ @@ -64,11 +69,42 @@ purple_message_new(const gchar *who, const gchar *contents, NULL); } +guint +purple_message_get_id(PurpleMessage *msg) +{ + PurpleMessagePrivate *priv = PURPLE_MESSAGE_GET_PRIVATE(msg); + + g_return_val_if_fail(priv != NULL, 0); + + return priv->id; +} + +PurpleMessage * +purple_message_find_by_id(guint id) +{ + g_return_val_if_fail(id > 0, NULL); + + return g_hash_table_lookup(messages, GINT_TO_POINTER(id)); +} + /****************************************************************************** * Object stuff ******************************************************************************/ static void +purple_message_init(GTypeInstance *instance, gpointer klass) +{ + static guint max_id = 0; + + PurpleMessage *msg = PURPLE_MESSAGE(instance); + PurpleMessagePrivate *priv = PURPLE_MESSAGE_GET_PRIVATE(msg); + PURPLE_DBUS_REGISTER_POINTER(msg, PurpleMessage); + + priv->id = ++max_id; + g_hash_table_insert(messages, GINT_TO_POINTER(max_id), msg); +} + +static void purple_message_finalize(GObject *obj) { PurpleMessage *message = PURPLE_MESSAGE(obj); @@ -88,6 +124,9 @@ purple_message_get_property(GObject *object, guint par_id, GValue *value, PurpleMessagePrivate *priv = PURPLE_MESSAGE_GET_PRIVATE(message); switch (par_id) { + case PROP_ID: + g_value_set_uint(value, priv->id); + break; case PROP_WHO: g_value_set_string(value, priv->who); break; @@ -141,6 +180,9 @@ purple_message_class_init(PurpleMessageClass *klass) gobj_class->get_property = purple_message_get_property; gobj_class->set_property = purple_message_set_property; + properties[PROP_ID] = g_param_spec_uint("id", + "ID", "The session-unique message id", + 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); properties[PROP_WHO] = g_param_spec_string("who", "Author", "The nick of the person, who sent the message", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); @@ -165,7 +207,8 @@ purple_message_get_type(void) static const GTypeInfo info = { .class_size = sizeof(PurpleMessageClass), .class_init = (GClassInitFunc)purple_message_class_init, - .instance_size = sizeof(PurpleMessage) + .instance_size = sizeof(PurpleMessage), + .instance_init = purple_message_init, }; type = g_type_register_static(G_TYPE_OBJECT, @@ -174,3 +217,17 @@ purple_message_get_type(void) return type; } + +void +_purple_message_init(void) +{ + messages = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, g_object_unref); +} + +void +_purple_message_uninit(void) +{ + g_hash_table_destroy(messages); + messages = NULL; +} diff --git a/libpurple/message.h b/libpurple/message.h index e04d70557e..c6cf4cbc3a 100644 --- a/libpurple/message.h +++ b/libpurple/message.h @@ -86,6 +86,18 @@ PurpleMessage * purple_message_new(const gchar *who, const gchar *contents, PurpleMessageFlags flags); +guint +purple_message_get_id(PurpleMessage *msg); + +PurpleMessage * +purple_message_find_by_id(guint id); + +void +_purple_message_init(void); + +void +_purple_message_uninit(void); + G_END_DECLS #endif /* _PURPLE_MESSAGE_H_ */ |