pidgin/pidgin

45e719e8d3bc
Parents 4caeb8c0b1ca
Children b5e0f65a5d9c
Replace lists of PurpleAttr with GHashTable
--- a/libpurple/account.c Sat Nov 16 13:37:56 2019 +0300
+++ b/libpurple/account.c Tue Nov 26 23:59:35 2019 +0300
@@ -1792,19 +1792,19 @@
purple_account_set_status(PurpleAccount *account, const char *status_id,
gboolean active, ...)
{
- GList *attrs = NULL;
+ GHashTable *attrs;
va_list args;
va_start(args, active);
- attrs = purple_attr_list_from_vargs(args);
+ attrs = purple_attrs_from_vargs(args);
purple_account_set_status_list(account, status_id, active, attrs);
- g_list_free_full(attrs, g_free);
+ g_hash_table_destroy(attrs);
va_end(args);
}
void
purple_account_set_status_list(PurpleAccount *account, const char *status_id,
- gboolean active, GList *attrs)
+ gboolean active, GHashTable *attrs)
{
PurpleStatus *status;
--- a/libpurple/account.h Sat Nov 16 13:37:56 2019 +0300
+++ b/libpurple/account.h Tue Nov 26 23:59:35 2019 +0300
@@ -440,8 +440,9 @@
* @status_id: The ID of the status.
* @active: Whether @a status_id is to be activated (%TRUE) or
* deactivated (%FALSE).
- * @attrs: (element-type utf8): A list of <type>const char *</type> attribute names followed by
- * <type>const char *</type> attribute values for the status.
+ * @attrs: (element-type utf8 gpointer): A hash table of
+ * <type>const char *</type> attribute names followed by
+ * <type>gpointer</type> attribute values for the status.
* (For example, one pair might be <literal>"message"</literal>
* followed by <literal>"hello, talk to me!"</literal>.)
*
@@ -453,7 +454,7 @@
* FALSE, this function does nothing.
*/
void purple_account_set_status_list(PurpleAccount *account,
- const char *status_id, gboolean active, GList *attrs);
+ const char *status_id, gboolean active, GHashTable *attrs);
/**
* purple_account_set_public_alias:
--- a/libpurple/accounts.c Sat Nov 16 13:37:56 2019 +0300
+++ b/libpurple/accounts.c Tue Nov 26 23:59:35 2019 +0300
@@ -200,17 +200,16 @@
migrate_xmpp_encryption(account);
}
-static GList *
+static GHashTable *
parse_status_attrs(PurpleXmlNode *node, PurpleStatus *status)
{
- GList *list = NULL;
+ GHashTable *attrs = g_hash_table_new(g_str_hash, g_str_equal);
PurpleXmlNode *child;
GValue *attr_value;
for (child = purple_xmlnode_get_child(node, "attribute"); child != NULL;
child = purple_xmlnode_get_next_twin(child))
{
- PurpleAttr *attr = NULL;
const char *id = purple_xmlnode_get_attrib(child, "id");
const char *value = purple_xmlnode_get_attrib(child, "value");
@@ -224,26 +223,23 @@
switch (G_VALUE_TYPE(attr_value))
{
case G_TYPE_STRING:
- attr = purple_attr_new(id, (char *)value);
+ g_hash_table_insert(attrs, id, (char *)value);
break;
case G_TYPE_INT:
case G_TYPE_BOOLEAN:
{
int v;
- if (sscanf(value, "%d", &v) == 1)
- attr = purple_attr_new(id, GINT_TO_POINTER(v));
+ if (sscanf(value, "%d", &v) == 1) {
+ g_hash_table_insert(attrs, id, GINT_TO_POINTER(v));
+ }
break;
}
default:
break;
}
-
- if (attr != NULL) {
- list = g_list_append(list, attr);
- }
}
- return list;
+ return attrs;
}
static void
@@ -253,7 +249,7 @@
const char *data;
const char *type;
PurpleXmlNode *child;
- GList *attrs = NULL;
+ GHashTable *attrs = NULL;
/* Get the active/inactive state */
data = purple_xmlnode_get_attrib(node, "active");
@@ -281,7 +277,7 @@
purple_account_set_status_list(account, type, active, attrs);
- g_list_free_full(attrs, g_free);
+ g_hash_table_destroy(attrs);
}
static void
--- a/libpurple/status.c Sat Nov 16 13:37:56 2019 +0300
+++ b/libpurple/status.c Tue Nov 26 23:59:35 2019 +0300
@@ -714,23 +714,27 @@
void
purple_status_set_active_with_attrs(PurpleStatus *status, gboolean active, va_list args)
{
- GList *attrs = purple_attr_list_from_vargs(args);
+ GHashTable *attrs = purple_attrs_from_vargs(args);
purple_status_set_active_with_attrs_list(status, active, attrs);
- g_list_free_full(attrs, g_free);
+ g_hash_table_destroy(attrs);
}
void
purple_status_set_active_with_attrs_list(PurpleStatus *status, gboolean active,
- GList *attrs)
+ GHashTable *attrs)
{
PurpleStatusPrivate *priv = NULL;
gboolean changed = FALSE;
+ GHashTableIter iter;
+ gchar *id;
+ gpointer data;
GList *l;
GList *specified_attr_ids = NULL;
PurpleStatusType *status_type;
g_return_if_fail(PURPLE_IS_STATUS(status));
+ g_return_if_fail(attrs != NULL);
priv = purple_status_get_instance_private(status);
@@ -750,44 +754,43 @@
priv->active = active;
/* Set any attributes */
- for (l = attrs; l != NULL; l = l->next)
- {
- PurpleAttr *attr = l->data;
+ g_hash_table_iter_init(&iter, attrs);
+ while (g_hash_table_iter_next(&iter, (gpointer *)&id, (gpointer *)&data)) {
GValue *value;
- value = purple_status_get_attr_value(status, attr->id);
+ value = purple_status_get_attr_value(status, id);
if (value == NULL)
{
purple_debug_warning("status", "The attribute \"%s\" on the status \"%s\" is "
- "not supported.\n", attr->id, priv->status_type->name);
+ "not supported.\n", id, priv->status_type->name);
/* Skip over the data and move on to the next attribute */
continue;
}
- specified_attr_ids = g_list_prepend(specified_attr_ids, (gpointer)attr->id);
+ specified_attr_ids = g_list_prepend(specified_attr_ids, (gpointer)id);
if (G_VALUE_TYPE(value) == G_TYPE_STRING)
{
- const gchar *string_data = attr->data;
+ const gchar *string_data = data;
if (purple_strequal(string_data, g_value_get_string(value)))
continue;
- status_set_attr_string(status, attr->id, string_data);
+ status_set_attr_string(status, id, string_data);
changed = TRUE;
}
else if (G_VALUE_TYPE(value) == G_TYPE_INT)
{
- int int_data = GPOINTER_TO_INT(attr->data);
+ int int_data = GPOINTER_TO_INT(data);
if (int_data == g_value_get_int(value))
continue;
- status_set_attr_int(status, attr->id, int_data);
+ status_set_attr_int(status, id, int_data);
changed = TRUE;
}
else if (G_VALUE_TYPE(value) == G_TYPE_BOOLEAN)
{
- gboolean boolean_data = GPOINTER_TO_INT(attr->data);
+ gboolean boolean_data = GPOINTER_TO_INT(data);
if (boolean_data == g_value_get_boolean(value))
continue;
- status_set_attr_boolean(status, attr->id, boolean_data);
+ status_set_attr_boolean(status, id, boolean_data);
changed = TRUE;
}
}
@@ -1351,32 +1354,19 @@
}
/**************************************************************************/
-/* PurpleAttr helpers */
+/* Helpers */
/**************************************************************************/
-PurpleAttr *
-purple_attr_new(const char *id, gpointer data)
+GHashTable *
+purple_attrs_from_vargs(va_list args)
{
- PurpleAttr *attr;
-
- attr = g_new0(PurpleAttr, 1);
- attr->id = id;
- attr->data = data;
+ GHashTable *attrs = g_hash_table_new(g_str_hash, g_str_equal);
+ gchar *id;
- return attr;
-}
-
-GList *
-purple_attr_list_from_vargs(va_list args)
-{
- GList *attrs = NULL;
- const gchar *id;
-
- while ((id = va_arg(args, const char *)) != NULL)
+ while ((id = va_arg(args, gchar *)) != NULL)
{
gpointer data = va_arg(args, gpointer);
- PurpleAttr *attr = purple_attr_new(id, data);
- attrs = g_list_append(attrs, attr);
+ g_hash_table_insert(attrs, id, data);
}
return attrs;
--- a/libpurple/status.h Sat Nov 16 13:37:56 2019 +0300
+++ b/libpurple/status.h Tue Nov 26 23:59:35 2019 +0300
@@ -96,21 +96,6 @@
PURPLE_STATUS_NUM_PRIMITIVES, /*< skip >*/
} PurpleStatusPrimitive;
-/**
- * PurpleAttr:
- * @id: The attribute id
- * @data: The attribute data
- *
- * A name-value pair.
- *
- * Similar to PurpleKeyValuePair except it doesn't allocate memory for @id and @data.
- */
-typedef struct _PurpleAttr
-{
- const gchar *id;
- gpointer data;
-} PurpleAttr;
-
#include "presence.h"
#define PURPLE_TUNE_ARTIST "tune_artist"
@@ -510,17 +495,16 @@
* purple_status_set_active_with_attrs_list:
* @status: The status.
* @active: The active state.
- * @attrs: (element-type PurpleAttr): A list of attributes to set on the status. This list is
- * composed of key/value pairs, where each key is a valid
- * attribute name for this PurpleStatusType. The list is
- * not modified or freed by this function.
+ * @attrs: (element-type utf8 gpointer): A hash table of attributes to set on the status.
+ * This hash table's keys are valid attribute names for this PurpleStatusType.
+ * The hash table is not modified or freed by this function.
*
* Sets whether or not a status is active.
*
* This should only be called by the account, conversation, and buddy APIs.
*/
void purple_status_set_active_with_attrs_list(PurpleStatus *status, gboolean active,
- GList *attrs);
+ GHashTable *attrs);
/**
* purple_status_get_status_type:
@@ -716,36 +700,23 @@
void purple_statuses_uninit(void);
/**************************************************************************/
-/* PurpleAttr helpers */
+/* Helpers */
/**************************************************************************/
/**
- * purple_attr_new:
- * @id: The name part of PurpleAttr
- * @data: The value part of PurpleAttr
- *
- * Creates a new PurpleAttr.
- *
- * Returns: The created PurpleAttr
- *
- * Since: 3.0.0
- */
-PurpleAttr *purple_attr_new(const char *id, gpointer data);
-
-/**
- * purple_attr_list_from_vargs:
+ * purple_attrs_from_vargs:
* @args: A list of attributes to parse. This list is
* composed of key/value pairs, where each key is a valid
* attribute name for this PurpleStatusType. The list should
* be NULL terminated.
*
- * Returns a list of attributes constructed from args.
+ * Returns a hash table of attributes constructed from args.
*
- * Returns: (element-type PurpleAttr) (transfer full): The list of attributes.
+ * Returns: (element-type utf8 gpointer) (transfer container): The hash table of attributes.
*
* Since: 3.0.0
*/
-GList *purple_attr_list_from_vargs(va_list args);
+GHashTable *purple_attrs_from_vargs(va_list args);
G_END_DECLS