/soc/2012/tomkiewicz/gg: ceebe795b644: Gadu-Gadu: roster - code ...
Tomasz Wasilczyk
tomkiewicz at cpw.pidgin.im
Thu Jul 12 10:27:31 EDT 2012
Changeset: ceebe795b644b98ed82d26e40602f6ec200971f0
Author: Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date: 2012-07-12 16:27 +0200
Branch: soc.2012.gg
URL: http://hg.pidgin.im/soc/2012/tomkiewicz/gg/rev/ceebe795b644
Description:
Gadu-Gadu: roster - code cleanup
diffstat:
libpurple/protocols/gg/purplew.c | 10 +
libpurple/protocols/gg/purplew.h | 3 +
libpurple/protocols/gg/roster.c | 313 ++++++++++++++++++++------------------
3 files changed, 177 insertions(+), 149 deletions(-)
diffs (truncated from 451 to 300 lines):
diff --git a/libpurple/protocols/gg/purplew.c b/libpurple/protocols/gg/purplew.c
--- a/libpurple/protocols/gg/purplew.c
+++ b/libpurple/protocols/gg/purplew.c
@@ -53,3 +53,13 @@
purple_request_close(PURPLE_REQUEST_ACTION, handle->request_handle);
g_free(handle);
}
+
+PurpleGroup * ggp_purplew_buddy_get_group_only(PurpleBuddy *buddy)
+{
+ PurpleGroup *group = purple_buddy_get_group(buddy);
+ if (!group)
+ return NULL;
+ if (0 == strcmp(_("Buddies"), purple_group_get_name(group)))
+ return NULL;
+ return group;
+}
diff --git a/libpurple/protocols/gg/purplew.h b/libpurple/protocols/gg/purplew.h
--- a/libpurple/protocols/gg/purplew.h
+++ b/libpurple/protocols/gg/purplew.h
@@ -36,4 +36,7 @@
void ggp_purplew_request_processing_done(
ggp_purplew_request_processing_handle *handle);
+// ignores default group
+PurpleGroup * ggp_purplew_buddy_get_group_only(PurpleBuddy *buddy);
+
#endif /* _GGP_PURPLEW_H */
diff --git a/libpurple/protocols/gg/roster.c b/libpurple/protocols/gg/roster.c
--- a/libpurple/protocols/gg/roster.c
+++ b/libpurple/protocols/gg/roster.c
@@ -3,13 +3,13 @@
#include "gg.h"
#include "xml.h"
#include "utils.h"
+#include "purplew.h"
#include <debug.h>
#define GGP_ROSTER_SYNC_SETT "gg-synchronized"
#define GGP_ROSTER_ID_SETT "gg-id"
#define GGP_ROSTER_DEBUG 1
-#define GGP_ROSTER_GROUP_DEFAULT _("Buddies")
#define GGP_ROSTER_GROUPID_DEFAULT "00000000-0000-0000-0000-000000000000"
/*
@@ -263,6 +263,156 @@
rdata->version = version;
}
+static gboolean ggp_roster_reply_list_read_group(xmlnode *node, ggp_roster_content *content, GHashTable *groups)
+{
+ char *name, *id;
+ gboolean removable;
+ gboolean succ = TRUE;
+ PurpleGroup *local_group;
+
+ succ &= ggp_xml_get_string(node, "Id", &id);
+ succ &= ggp_xml_get_string(node, "Name", &name);
+ succ &= ggp_xml_get_bool(node, "IsRemovable", &removable);
+
+ if (!succ)
+ {
+ g_free(id);
+ g_free(name);
+ g_return_val_if_reached(FALSE);
+ }
+
+ if (!removable)
+ {
+ g_free(id);
+ g_free(name);
+ return TRUE;
+ }
+
+ //TODO: group rename - first find by id and maybe rename local; if not found, do the following
+ local_group = purple_find_group(name);
+ if (local_group)
+ purple_blist_node_set_string(PURPLE_BLIST_NODE(local_group), GGP_ROSTER_ID_SETT, id);
+
+ g_hash_table_insert(content->group_nodes, g_strdup(id), node);
+ g_hash_table_insert(groups, id, name);
+
+ return TRUE;
+}
+
+static gboolean ggp_roster_reply_list_read_buddy(PurpleConnection *gc, xmlnode *node, ggp_roster_content *content, GHashTable *groups, GHashTable *remove_buddies)
+{
+ gchar *alias, *group_name;
+ uin_t uin;
+ gboolean isbot;
+ gboolean succ = TRUE;
+ xmlnode *group_list, *group_elem;
+ PurpleBuddy *buddy = NULL;
+ PurpleGroup *group = NULL;
+ PurpleGroup *currentGroup;
+ gboolean alias_changed;
+ PurpleAccount *account = purple_connection_get_account(gc);
+
+ succ &= ggp_xml_get_string(node, "ShowName", &alias);
+ succ &= ggp_xml_get_uint(node, "GGNumber", &uin);
+
+ group_list = xmlnode_get_child(node, "Groups");
+ succ &= (group_list != NULL);
+
+ if (!succ)
+ {
+ g_free(alias);
+ g_return_val_if_reached(FALSE);
+ }
+
+ g_hash_table_insert(content->contact_nodes, GINT_TO_POINTER(uin), node);
+
+ // we don't want to import bots;
+ // they are inserted to roster by default
+ if (isbot)
+ {
+ g_free(alias);
+ return TRUE;
+ }
+
+ // check, if alias is set
+ if (strlen(alias) == 0 ||
+ strcmp(alias, ggp_uin_to_str(uin)) == 0)
+ {
+ g_free(alias);
+ alias = NULL;
+ }
+
+ // getting (eventually creating) group
+ group_elem = xmlnode_get_child(group_list, "GroupId");
+ while (group_elem != NULL)
+ {
+ gchar *id;
+ if (!ggp_xml_get_string(group_elem, NULL, &id))
+ continue;
+ group_name = g_hash_table_lookup(groups, id);
+ isbot = (strcmp(id, "0b345af6-0001-0000-0000-000000000004") == 0 ||
+ g_strcmp0(group_name, "Pomocnicy") == 0);
+ g_free(id);
+
+ if (isbot)
+ break;
+
+ if (group_name != NULL)
+ break;
+
+ group_elem = xmlnode_get_next_twin(group_elem);
+ }
+ if (group_name)
+ {
+ group = purple_find_group(group_name);
+ if (!group)
+ {
+ group = purple_group_new(group_name);
+ purple_blist_add_group(group, NULL);
+ }
+ }
+
+ // add buddy, if doesn't exists
+ buddy = purple_find_buddy(account, ggp_uin_to_str(uin));
+ g_hash_table_remove(remove_buddies, GINT_TO_POINTER(uin));
+ if (!buddy)
+ {
+ purple_debug_info("gg", "ggp_roster_reply_list: adding %u to buddy list\n", uin);
+ buddy = purple_buddy_new(account, ggp_uin_to_str(uin), alias);
+ purple_blist_add_buddy(buddy, NULL, group, NULL);
+ ggp_roster_set_synchronized(gc, buddy, TRUE);
+
+ g_free(alias);
+ return TRUE;
+ }
+
+ // buddy exists, but is not synchronized - local list has priority
+ if (!ggp_roster_is_synchronized(buddy))
+ {
+ purple_debug_info("gg", "ggp_roster_reply_list: ignoring not synchronized %s\n", purple_buddy_get_name(buddy));
+ g_free(alias);
+ return TRUE;
+ }
+
+ currentGroup = ggp_purplew_buddy_get_group_only(buddy);
+ alias_changed = (0 != g_strcmp0(alias, purple_buddy_get_alias_only(buddy)));
+
+ if (currentGroup == group && !alias_changed)
+ {
+ g_free(alias);
+ return TRUE;
+ }
+
+ purple_debug_misc("gg", "ggp_roster_reply_list: updating %s [currentAlias=%s, alias=%s, currentGroup=%p, group=%p]\n", purple_buddy_get_name(buddy), purple_buddy_get_alias(buddy), alias, currentGroup, group);
+ if (alias_changed)
+ purple_blist_alias_buddy(buddy, alias);
+ if (currentGroup != group)
+ purple_blist_add_buddy(buddy, NULL, group, NULL);
+
+ g_free(alias);
+ return TRUE;
+}
+
static void ggp_roster_reply_list(PurpleConnection *gc, uint32_t version, const char *data)
{
ggp_roster_session_data *rdata = ggp_roster_get_rdata(gc);
@@ -299,53 +449,23 @@
#endif
// reading groups
-
content->groups_node = xmlnode_get_child(xml, "Groups");
if (content->groups_node == NULL)
{
ggp_roster_content_free(content);
g_return_if_reached();
}
-
groups = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
curr_elem = xmlnode_get_child(content->groups_node, "Group");
while (curr_elem != NULL)
{
- char *name, *id;
- gboolean removable;
- gboolean succ = TRUE;
-
- succ &= ggp_xml_get_string(curr_elem, "Id", &id);
- succ &= ggp_xml_get_string(curr_elem, "Name", &name);
- succ &= ggp_xml_get_bool(curr_elem, "IsRemovable", &removable);
-
- if (!succ)
+ if (!ggp_roster_reply_list_read_group(curr_elem, content, groups))
{
- g_free(id);
- g_free(name);
g_hash_table_destroy(groups);
ggp_roster_content_free(content);
g_return_if_reached();
}
- if (removable)
- {
- PurpleGroup *local_group;
-
- //TODO: group rename - first find by id and maybe rename local; if not found, do the following
- local_group = purple_find_group(name);
- if (local_group)
- purple_blist_node_set_string(PURPLE_BLIST_NODE(local_group), GGP_ROSTER_ID_SETT, id);
-
- g_hash_table_insert(content->group_nodes, g_strdup(id), curr_elem);
- g_hash_table_insert(groups, id, name);
- }
- else
- {
- g_free(id);
- g_free(name);
- }
-
curr_elem = xmlnode_get_next_twin(curr_elem);
}
@@ -353,7 +473,6 @@
// we will:
// - remove synchronized ones, if not found in list at server
// - upload not synchronized ones
-
buddies = purple_find_buddies(account, NULL);
remove_buddies = g_hash_table_new(g_direct_hash, g_direct_equal);
while (buddies)
@@ -373,7 +492,6 @@
}
// reading buddies
-
content->contacts_node = xmlnode_get_child(xml, "Contacts");
if (content->contacts_node == NULL)
{
@@ -383,126 +501,18 @@
ggp_roster_content_free(content);
g_return_if_reached();
}
-
curr_elem = xmlnode_get_child(content->contacts_node, "Contact");
while (curr_elem != NULL)
{
- gchar *alias, *group_name;
- uin_t uin;
- gboolean isbot;
- gboolean succ = TRUE;
- xmlnode *group_list, *group_elem;
- PurpleBuddy *buddy = NULL;
- PurpleGroup *group = NULL;
-
- succ &= ggp_xml_get_string(curr_elem, "ShowName", &alias);
- succ &= ggp_xml_get_uint(curr_elem, "GGNumber", &uin);
-
- group_list = xmlnode_get_child(curr_elem, "Groups");
- succ &= (group_list != NULL);
-
- if (!succ)
+ if (!ggp_roster_reply_list_read_buddy(gc, curr_elem, content, groups, remove_buddies))
{
- g_free(alias);
More information about the Commits
mailing list