core: process incoming changes for roaming contacts
[siplcs.git] / src / core / sipe-group.c
blob0589dc9e01ba9a1d246c6e7e8a2963ecd5bfbe90
1 /**
2 * @file sipe-group.c
4 * pidgin-sipe
6 * Copyright (C) 2011-12 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <glib.h>
29 #include "sipmsg.h"
30 #include "sip-soap.h"
31 #include "sip-transport.h"
32 #include "sipe-backend.h"
33 #include "sipe-buddy.h"
34 #include "sipe-core.h"
35 #include "sipe-core-private.h"
36 #include "sipe-group.h"
37 #include "sipe-nls.h"
38 #include "sipe-utils.h"
39 #include "sipe-xml.h"
41 struct group_user_context {
42 gchar *group_name;
43 gchar *user_name;
46 static void
47 sipe_group_context_destroy(gpointer data)
49 struct group_user_context *ctx = data;
50 g_free(ctx->group_name);
51 g_free(ctx->user_name);
52 g_free(ctx);
55 static gboolean
56 process_add_group_response(struct sipe_core_private *sipe_private,
57 struct sipmsg *msg,
58 struct transaction *trans)
60 if (msg->response == 200) {
61 struct sipe_group *group;
62 struct group_user_context *ctx = trans->payload->data;
63 sipe_xml *xml;
64 const sipe_xml *node;
65 char *group_id;
66 struct sipe_buddy *buddy;
68 xml = sipe_xml_parse(msg->body, msg->bodylen);
69 if (!xml) {
70 return FALSE;
73 node = sipe_xml_child(xml, "Body/addGroup/groupID");
74 if (!node) {
75 sipe_xml_free(xml);
76 return FALSE;
79 group_id = sipe_xml_data(node);
80 if (!group_id) {
81 sipe_xml_free(xml);
82 return FALSE;
85 group = g_new0(struct sipe_group, 1);
86 group->id = (int)g_ascii_strtod(group_id, NULL);
87 g_free(group_id);
88 group->name = g_strdup(ctx->group_name);
90 sipe_group_add(sipe_private, group);
92 if (ctx->user_name) {
93 buddy = g_hash_table_lookup(sipe_private->buddies, ctx->user_name);
94 if (buddy) {
95 buddy->groups = slist_insert_unique_sorted(buddy->groups, group, (GCompareFunc)sipe_group_compare);
98 sipe_core_group_set_user(SIPE_CORE_PUBLIC, ctx->user_name);
101 sipe_xml_free(xml);
102 return TRUE;
104 return FALSE;
108 sipe_group_compare(struct sipe_group *group1, struct sipe_group *group2) {
109 return group1->id - group2->id;
112 struct sipe_group*
113 sipe_group_find_by_id(struct sipe_core_private *sipe_private,
114 int id)
116 struct sipe_group *group;
117 GSList *entry;
119 if (!sipe_private)
120 return NULL;
122 entry = sipe_private->groups;
123 while (entry) {
124 group = entry->data;
125 if (group->id == id) {
126 return group;
128 entry = entry->next;
130 return NULL;
133 struct sipe_group*
134 sipe_group_find_by_name(struct sipe_core_private *sipe_private,
135 const gchar * name)
137 struct sipe_group *group;
138 GSList *entry;
140 if (!sipe_private || !name)
141 return NULL;
143 entry = sipe_private->groups;
144 while (entry) {
145 group = entry->data;
146 if (sipe_strequal(group->name, name)) {
147 return group;
149 entry = entry->next;
151 return NULL;
154 void
155 sipe_group_create(struct sipe_core_private *sipe_private,
156 const gchar *name,
157 const gchar *who)
159 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
160 struct group_user_context *ctx = g_new0(struct group_user_context, 1);
161 const gchar *soap_name = sipe_strequal(name, _("Other Contacts")) ? "~" : name;
162 gchar *request;
163 ctx->group_name = g_strdup(name);
164 ctx->user_name = g_strdup(who);
165 payload->destroy = sipe_group_context_destroy;
166 payload->data = ctx;
168 /* soap_name can contain restricted characters */
169 request = g_markup_printf_escaped("<m:name>%s</m:name>"
170 "<m:externalURI />",
171 soap_name);
172 sip_soap_request_cb(sipe_private,
173 "addGroup",
174 request,
175 process_add_group_response,
176 payload);
177 g_free(request);
180 gboolean sipe_group_rename(struct sipe_core_private *sipe_private,
181 struct sipe_group *group,
182 const gchar *name)
184 gboolean renamed = sipe_backend_buddy_group_rename(SIPE_CORE_PUBLIC,
185 group->name,
186 name);
187 if (renamed) {
188 g_free(group->name);
189 group->name = g_strdup(name);
191 return(renamed);
194 void
195 sipe_group_add(struct sipe_core_private *sipe_private,
196 struct sipe_group * group)
198 if (sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC,group->name))
200 SIPE_DEBUG_INFO("added group %s (id %d)", group->name, group->id);
201 sipe_private->groups = g_slist_append(sipe_private->groups,
202 group);
204 else
206 SIPE_DEBUG_INFO("did not add group %s", group->name ? group->name : "");
210 void
211 sipe_core_group_rename(struct sipe_core_public *sipe_public,
212 const gchar *old_name,
213 const gchar *new_name)
215 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
216 struct sipe_group *s_group = sipe_group_find_by_name(sipe_private, old_name);
218 if (s_group) {
219 gchar *request;
220 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name, new_name);
221 /* new_name can contain restricted characters */
222 request = g_markup_printf_escaped("<m:groupID>%d</m:groupID>"
223 "<m:name>%s</m:name>"
224 "<m:externalURI />",
225 s_group->id, new_name);
226 sip_soap_request(sipe_private,
227 "modifyGroup",
228 request);
229 g_free(request);
231 g_free(s_group->name);
232 s_group->name = g_strdup(new_name);
233 } else {
234 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name);
238 void
239 sipe_core_group_remove(struct sipe_core_public *sipe_public,
240 const gchar *name)
242 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
243 struct sipe_group *s_group = sipe_group_find_by_name(sipe_private, name);
245 if (s_group) {
246 gchar *request;
247 SIPE_DEBUG_INFO("Deleting group %s", name);
248 request = g_strdup_printf("<m:groupID>%d</m:groupID>",
249 s_group->id);
250 sip_soap_request(sipe_private,
251 "deleteGroup",
252 request);
253 g_free(request);
255 sipe_private->groups = g_slist_remove(sipe_private->groups,
256 s_group);
257 g_free(s_group->name);
258 g_free(s_group);
259 } else {
260 SIPE_DEBUG_INFO("Cannot find group %s to delete", name);
265 * Returns string like "2 4 7 8" - group ids buddy belong to.
267 static gchar *sipe_get_buddy_groups_string(struct sipe_buddy *buddy)
269 int i = 0;
270 gchar *res;
271 //creating array from GList, converting int to gchar*
272 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
273 GSList *entry = buddy->groups;
275 if (!ids_arr) return NULL;
277 while (entry) {
278 struct sipe_group * group = entry->data;
279 ids_arr[i] = g_strdup_printf("%d", group->id);
280 entry = entry->next;
281 i++;
283 ids_arr[i] = NULL;
284 res = g_strjoinv(" ", ids_arr);
285 g_strfreev(ids_arr);
286 return res;
290 * Sends buddy update to server
292 void sipe_core_group_set_user(struct sipe_core_public *sipe_public,
293 const gchar *who)
295 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
296 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies, who);
297 sipe_backend_buddy backend_buddy = sipe_backend_buddy_find(sipe_public, who, NULL);
299 if (buddy && backend_buddy) {
300 gchar *groups = sipe_get_buddy_groups_string(buddy);
302 if (groups) {
303 gchar *alias = sipe_backend_buddy_get_alias(sipe_public, backend_buddy);
304 gchar *request;
305 SIPE_DEBUG_INFO("Saving buddy %s with alias %s and groups %s", who, alias, groups);
307 /* alias can contain restricted characters */
308 request = g_markup_printf_escaped("<m:displayName>%s</m:displayName>"
309 "<m:groups>%s</m:groups>"
310 "<m:subscribed>true</m:subscribed>"
311 "<m:URI>%s</m:URI>"
312 "<m:externalURI />",
313 alias, groups, buddy->name);
314 g_free(alias);
315 g_free(groups);
317 sip_soap_request(sipe_private,
318 "setContact",
319 request);
320 g_free(request);
326 Local Variables:
327 mode: c
328 c-file-style: "bsd"
329 indent-tabs-mode: t
330 tab-width: 8
331 End: