diff options
author | Olli Salli <olli.salli@collabora.co.uk> | 2007-02-04 22:30:35 +0000 |
---|---|---|
committer | Olli Salli <olli.salli@collabora.co.uk> | 2007-02-04 22:30:35 +0000 |
commit | 24c105919db0e9865a7aa8b304f789df1ce9d168 (patch) | |
tree | 4bfddfb8972f7edc573584b78c991d844fe3729d /src/idle-handles.c | |
parent | 559e01e2c8e978fce73ef98556fd0b5fdff09975 (diff) | |
download | telepathy-idle-24c105919db0e9865a7aa8b304f789df1ce9d168.tar.gz |
Migrate to the handle repos and sets in telepathy-glib
20070204223035-9db4d-eb01a394b0a783b0647d11c1a33d4dc8d15e1c7a.gz
Diffstat (limited to 'src/idle-handles.c')
-rw-r--r-- | src/idle-handles.c | 425 |
1 files changed, 54 insertions, 371 deletions
diff --git a/src/idle-handles.c b/src/idle-handles.c index ee8789a..1143d3e 100644 --- a/src/idle-handles.c +++ b/src/idle-handles.c @@ -24,12 +24,9 @@ #include <telepathy-glib/heap.h> #include "idle-handles.h" -#include "idle-handles-private.h" #include "idle-connection.h" -#define idle_handle_priv_new() (g_slice_new0(IdleHandlePriv)) - gboolean idle_nickname_is_valid(const gchar *nickname) { gsize len; @@ -155,301 +152,45 @@ gboolean idle_channelname_is_valid(const gchar *channel) return TRUE; } -static void handle_priv_free(IdleHandlePriv *priv) +static GQuark +idle_handle_real_quark() { - g_assert(priv != NULL); - g_free(priv->string); - - if (priv->cp != NULL) - { - idle_contact_presence_free(priv->cp); - } - - g_slice_free(IdleHandlePriv, priv); + static GQuark quark = 0; + + if (!quark) + { + quark = g_quark_from_static_string("idle_handle_real"); + } + + return quark; } -static IdleHandle idle_handle_alloc(IdleHandleStorage *storage, TpHandleType type) +static GQuark +idle_handle_presence_quark() { - IdleHandle ret; - - g_assert(storage != NULL); - - switch (type) - { - case TP_HANDLE_TYPE_CONTACT: - { - ret = GPOINTER_TO_INT(tp_heap_extract_first(storage->contact_unused)); - - if (ret == 0) - { - ret = storage->contact_serial++; - } - } - break; - case TP_HANDLE_TYPE_ROOM: - { - ret = GPOINTER_TO_INT(tp_heap_extract_first(storage->room_unused)); - - if (ret == 0) - { - ret = storage->room_serial++; - } - } - break; - case TP_HANDLE_TYPE_LIST: - default: - { - g_debug("%s: unsupported handle type %u", G_STRFUNC, type); - ret = 0; - } - break; - } - -/* g_debug("%s: returning %u (type %u)", G_STRFUNC, ret, type);*/ - - return ret; + static GQuark quark = 0; + + if (!quark) + { + quark = g_quark_from_static_string("idle_handle_presence"); + } + + return quark; } -IdleHandlePriv *handle_priv_lookup(IdleHandleStorage *storage, TpHandleType type, IdleHandle handle) +const gchar * +idle_handle_inspect(TpHandleRepoIface *storage, TpHandle handle) { - IdleHandlePriv *priv; + g_assert(storage != NULL); + g_assert(tp_handle_is_valid(storage, handle, NULL)); - g_assert(storage != NULL); - - if (handle == 0) - { - return NULL; - } - - switch (type) - { - case TP_HANDLE_TYPE_CONTACT: - { - priv = g_hash_table_lookup(storage->contact_handles, GINT_TO_POINTER(handle)); - } - break; - case TP_HANDLE_TYPE_ROOM: - { - priv = g_hash_table_lookup(storage->room_handles, GINT_TO_POINTER(handle)); - } - break; - case TP_HANDLE_TYPE_LIST: - default: - { - g_critical("%s: Only TP_HANDLE_TYPE_CONTACT and TP_HANDLE_TYPE_ROOM supported!", G_STRFUNC); - return NULL; - } - break; - } - - return priv; + return tp_handle_get_qdata(storage, handle, idle_handle_real_quark()); } -void handle_priv_remove(IdleHandleStorage *storage, TpHandleType type, IdleHandlePriv *priv, IdleHandle handle) +TpHandle idle_handle_for_contact(TpHandleRepoIface *storage, const char *nickname) { - g_assert(storage != NULL); - - switch (type) - { - case TP_HANDLE_TYPE_CONTACT: - { - g_hash_table_remove(storage->contact_strings, priv->string); - g_hash_table_remove(storage->contact_handles, GINT_TO_POINTER(handle)); - - if (handle == storage->contact_serial-1) - { - /* take advantage of good luck ;) */ - storage->contact_serial--; - } - else - { - tp_heap_add(storage->contact_unused, GINT_TO_POINTER(handle)); - } - } - break; - case TP_HANDLE_TYPE_ROOM: - { - g_hash_table_remove(storage->room_strings, priv->string); - g_hash_table_remove(storage->room_handles, GINT_TO_POINTER(handle)); - - if (handle == storage->room_serial-1) - { - storage->room_serial--; - } - else - { - tp_heap_add(storage->room_unused, GINT_TO_POINTER(handle)); - } - } - break; - case TP_HANDLE_TYPE_LIST: - default: - { - g_critical("%s: Only TP_HANDLE_TYPE_CONTACT and TP_HANDLE_TYPE_ROOM supported!", G_STRFUNC); - return; - } - break; - } -} - -gboolean idle_handle_type_is_valid(TpHandleType type) -{ - switch (type) - { - case TP_HANDLE_TYPE_CONTACT: - case TP_HANDLE_TYPE_ROOM: - { - return TRUE; - } - break; - case TP_HANDLE_TYPE_LIST: - default: - { - return FALSE; - } - break; - } -} - -static guint g_strncase_hash(gconstpointer key) -{ - guint ret; - gchar *tmp; - - tmp = g_utf8_strdown(key, 32); - ret = g_str_hash(tmp); - - g_free(tmp); - - return ret; -} - -static gboolean g_strncase_equal(gconstpointer a, gconstpointer b) -{ - gchar *s1, *s2; - gboolean ret; - - s1 = g_utf8_casefold(a, -1); - s2 = g_utf8_casefold(b, -1); - - ret = (strcmp(s1, s2) == 0); - - g_free(s1); - g_free(s2); - - return ret; -} - -static gint idle_handle_compare(gconstpointer a, gconstpointer b) -{ - return (a < b) ? -1 : (a == b) ? 0 : 1; -} - -IdleHandleStorage *idle_handle_storage_new() -{ - IdleHandleStorage *ret; - - ret = g_slice_new0(IdleHandleStorage); - - ret->contact_handles = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)(handle_priv_free)); - ret->room_handles = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)(handle_priv_free)); - - ret->contact_strings = g_hash_table_new_full(g_strncase_hash, g_strncase_equal, NULL, NULL); - ret->room_strings = g_hash_table_new_full(g_strncase_hash, g_strncase_equal, NULL, NULL); - - ret->contact_unused = tp_heap_new(idle_handle_compare); - ret->room_unused = tp_heap_new(idle_handle_compare); - - ret->contact_serial = 1; - ret->room_serial = 1; - - return ret; -} - -void idle_handle_storage_destroy(IdleHandleStorage *storage) -{ - g_assert(storage != NULL); - g_assert(storage->contact_handles != NULL); - g_assert(storage->room_handles != NULL); - - g_hash_table_destroy(storage->contact_handles); - g_hash_table_destroy(storage->room_handles); - - g_hash_table_destroy(storage->contact_strings); - g_hash_table_destroy(storage->room_strings); - - tp_heap_destroy(storage->contact_unused); - tp_heap_destroy(storage->room_unused); - - g_slice_free(IdleHandleStorage, storage); -} - -gboolean idle_handle_is_valid(IdleHandleStorage *storage, TpHandleType type, IdleHandle handle) -{ - return (handle_priv_lookup(storage, type, handle) != NULL); -} - -gboolean idle_handle_ref(IdleHandleStorage *storage, TpHandleType type, IdleHandle handle) -{ - IdleHandlePriv *priv; - - priv = handle_priv_lookup(storage, type, handle); - - if (priv == NULL) - { - return FALSE; - } - else - { - priv->refcount++; - return TRUE; - } -} - -gboolean idle_handle_unref(IdleHandleStorage *storage, TpHandleType type, IdleHandle handle) -{ - IdleHandlePriv *priv; - - priv = handle_priv_lookup(storage, type, handle); - - if (priv == NULL) - { - return FALSE; - } - else - { - g_assert(priv->refcount > 0); - - priv->refcount--; - - if (priv->refcount == 0) - { - handle_priv_remove(storage, type, priv, handle); - } - - return TRUE; - } -} - -const char *idle_handle_inspect(IdleHandleStorage *storage, TpHandleType type, IdleHandle handle) -{ - IdleHandlePriv *priv; - - priv = handle_priv_lookup(storage, type, handle); - - if (priv == NULL) - { - return NULL; - } - else - { - return priv->string; - } -} - -IdleHandle idle_handle_for_contact(IdleHandleStorage *storage, const char *nickname) -{ - IdleHandle handle; + TpHandle handle; + gchar *nickname_down; g_assert(storage != NULL); @@ -464,53 +205,26 @@ IdleHandle idle_handle_for_contact(IdleHandleStorage *storage, const char *nickn g_debug("%s: nickname (%s) isn't valid!", G_STRFUNC, nickname); return 0; } - - handle = GPOINTER_TO_INT(g_hash_table_lookup(storage->contact_strings, nickname)); - - if (handle == 0) - { - handle = idle_handle_alloc(storage, TP_HANDLE_TYPE_CONTACT); - } - - if (handle_priv_lookup(storage, TP_HANDLE_TYPE_CONTACT, handle) == NULL) - { - IdleHandlePriv *priv; - priv = idle_handle_priv_new(); - priv->string = g_strdup(nickname); - g_hash_table_insert(storage->contact_handles, GINT_TO_POINTER(handle), priv); - g_hash_table_insert(storage->contact_strings, priv->string, GINT_TO_POINTER(handle)); - } - - return handle; -} + nickname_down = g_utf8_strdown(nickname, -1); -gboolean idle_handle_for_room_exists(IdleHandleStorage *storage, const char *channel_up) -{ - IdleHandle handle; - gchar *channel; + handle = tp_handle_request(storage, nickname_down, FALSE); - g_assert(storage != NULL); + if (!handle) + { + handle = tp_handle_request(storage, nickname_down, TRUE); + g_assert(tp_handle_set_qdata(storage, handle, idle_handle_real_quark(), g_strdup(nickname), (GDestroyNotify)(g_free))); + } - channel = g_ascii_strdown(channel_up, -1); + g_free(nickname_down); - if ((channel == NULL) || (channel[0] == '\0')) - { - return FALSE; - } - else - { - handle = GPOINTER_TO_INT(g_hash_table_lookup(storage->room_strings, channel)); - } - - g_free(channel); - - return handle_priv_lookup(storage, TP_HANDLE_TYPE_ROOM, handle) != NULL; + return handle; } -IdleHandle idle_handle_for_room(IdleHandleStorage *storage, const char *channel) +TpHandle idle_handle_for_room(TpHandleRepoIface *storage, const char *channel) { - IdleHandle handle; + TpHandle handle; + gchar *channel_down; g_assert(storage != NULL); @@ -525,64 +239,33 @@ IdleHandle idle_handle_for_room(IdleHandleStorage *storage, const char *channel) g_debug("%s: channel name (%s) not valid!", G_STRFUNC, channel); return 0; } - - handle = GPOINTER_TO_INT(g_hash_table_lookup(storage->room_strings, channel)); - if (handle == 0) - { - handle = idle_handle_alloc(storage, TP_HANDLE_TYPE_ROOM); - } + channel_down = g_utf8_strdown(channel, -1); + + handle = tp_handle_request(storage, channel_down, FALSE); - if (handle_priv_lookup(storage, TP_HANDLE_TYPE_ROOM, handle) == NULL) - { - IdleHandlePriv *priv; - priv = idle_handle_priv_new(); - priv->string = g_strdup(channel); + if (!handle) + { + handle = tp_handle_request(storage, channel_down, TRUE); + g_assert(tp_handle_set_qdata(storage, handle, idle_handle_real_quark(), g_strdup(channel), (GDestroyNotify)(g_free))); + } - g_hash_table_insert(storage->room_handles, GINT_TO_POINTER(handle), priv); - g_hash_table_insert(storage->room_strings, priv->string, GINT_TO_POINTER(handle)); - } + g_free(channel_down); return handle; } -gboolean idle_handle_set_presence(IdleHandleStorage *storage, IdleHandle handle, IdleContactPresence *cp) +gboolean idle_handle_set_presence(TpHandleRepoIface *storage, TpHandle handle, IdleContactPresence *cp) { - IdleHandlePriv *priv; - - g_assert(storage != NULL); - g_assert(handle != 0); - - priv = handle_priv_lookup(storage, TP_HANDLE_TYPE_CONTACT, handle); + g_assert(storage != NULL); - if (priv == NULL) - { - return FALSE; - } - else - { - priv->cp = cp; - } - - return TRUE; + return tp_handle_set_qdata(storage, handle, idle_handle_presence_quark(), cp, (GDestroyNotify)(idle_contact_presence_free)); } -IdleContactPresence *idle_handle_get_presence(IdleHandleStorage *storage, IdleHandle handle) +IdleContactPresence *idle_handle_get_presence(TpHandleRepoIface *storage, TpHandle handle) { - IdleHandlePriv *priv; - g_assert(storage != NULL); - g_assert(handle != 0); - - priv = handle_priv_lookup(storage, TP_HANDLE_TYPE_CONTACT, handle); - if (priv == NULL) - { - return NULL; - } - else - { - return priv->cp; - } + return tp_handle_get_qdata(storage, handle, idle_handle_presence_quark()); } |