webticket: implement caching
[siplcs.git] / src / core / sipe-group.c
blob32f0af10299b6f1a8d0e906ae51e70915abbfe0c
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);
96 sipe_group_update_buddy(sipe_private, buddy);
100 sipe_xml_free(xml);
101 return TRUE;
103 return FALSE;
107 sipe_group_compare(struct sipe_group *group1, struct sipe_group *group2) {
108 return group1->id - group2->id;
111 struct sipe_group*
112 sipe_group_find_by_id(struct sipe_core_private *sipe_private,
113 int id)
115 struct sipe_group *group;
116 GSList *entry;
118 if (!sipe_private)
119 return NULL;
121 entry = sipe_private->groups;
122 while (entry) {
123 group = entry->data;
124 if (group->id == id) {
125 return group;
127 entry = entry->next;
129 return NULL;
132 struct sipe_group*
133 sipe_group_find_by_name(struct sipe_core_private *sipe_private,
134 const gchar * name)
136 struct sipe_group *group;
137 GSList *entry;
139 if (!sipe_private || !name)
140 return NULL;
142 entry = sipe_private->groups;
143 while (entry) {
144 group = entry->data;
145 if (sipe_strequal(group->name, name)) {
146 return group;
148 entry = entry->next;
150 return NULL;
153 void
154 sipe_group_create(struct sipe_core_private *sipe_private,
155 const gchar *name,
156 const gchar *who)
158 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
159 struct group_user_context *ctx = g_new0(struct group_user_context, 1);
160 const gchar *soap_name = sipe_strequal(name, _("Other Contacts")) ? "~" : name;
161 gchar *request;
162 ctx->group_name = g_strdup(name);
163 ctx->user_name = g_strdup(who);
164 payload->destroy = sipe_group_context_destroy;
165 payload->data = ctx;
167 /* soap_name can contain restricted characters */
168 request = g_markup_printf_escaped("<m:name>%s</m:name>"
169 "<m:externalURI />",
170 soap_name);
171 sip_soap_request_cb(sipe_private,
172 "addGroup",
173 request,
174 process_add_group_response,
175 payload);
176 g_free(request);
179 gboolean sipe_group_rename(struct sipe_core_private *sipe_private,
180 struct sipe_group *group,
181 const gchar *name)
183 gboolean renamed = sipe_backend_buddy_group_rename(SIPE_CORE_PUBLIC,
184 group->name,
185 name);
186 if (renamed) {
187 g_free(group->name);
188 group->name = g_strdup(name);
190 return(renamed);
193 void
194 sipe_group_add(struct sipe_core_private *sipe_private,
195 struct sipe_group * group)
197 if (sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC,group->name))
199 SIPE_DEBUG_INFO("added group %s (id %d)", group->name, group->id);
200 sipe_private->groups = g_slist_append(sipe_private->groups,
201 group);
203 else
205 SIPE_DEBUG_INFO("did not add group %s", group->name ? group->name : "");
209 void sipe_group_free(struct sipe_core_private *sipe_private,
210 struct sipe_group *group)
212 sipe_private->groups = g_slist_remove(sipe_private->groups,
213 group);
214 g_free(group->name);
215 g_free(group);
218 void sipe_group_remove(struct sipe_core_private *sipe_private,
219 struct sipe_group *group)
221 if (group) {
222 SIPE_DEBUG_INFO("removing group %s (id %d)", group->name, group->id);
223 sipe_backend_buddy_group_remove(SIPE_CORE_PUBLIC, group->name);
224 sipe_group_free(sipe_private, group);
228 void
229 sipe_core_group_rename(struct sipe_core_public *sipe_public,
230 const gchar *old_name,
231 const gchar *new_name)
233 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
234 struct sipe_group *s_group = sipe_group_find_by_name(sipe_private, old_name);
236 if (s_group) {
237 gchar *request;
238 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name, new_name);
239 /* new_name can contain restricted characters */
240 request = g_markup_printf_escaped("<m:groupID>%d</m:groupID>"
241 "<m:name>%s</m:name>"
242 "<m:externalURI />",
243 s_group->id, new_name);
244 sip_soap_request(sipe_private,
245 "modifyGroup",
246 request);
247 g_free(request);
249 g_free(s_group->name);
250 s_group->name = g_strdup(new_name);
251 } else {
252 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name);
256 void
257 sipe_core_group_remove(struct sipe_core_public *sipe_public,
258 const gchar *name)
260 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
261 struct sipe_group *s_group = sipe_group_find_by_name(sipe_private, name);
263 if (s_group) {
264 gchar *request;
265 SIPE_DEBUG_INFO("Deleting group %s", name);
266 request = g_strdup_printf("<m:groupID>%d</m:groupID>",
267 s_group->id);
268 sip_soap_request(sipe_private,
269 "deleteGroup",
270 request);
271 g_free(request);
273 sipe_group_free(sipe_private, s_group);
274 } else {
275 SIPE_DEBUG_INFO("Cannot find group %s to delete", name);
280 * Returns string like "2 4 7 8" - group ids buddy belong to.
282 static gchar *sipe_get_buddy_groups_string(struct sipe_buddy *buddy)
284 int i = 0;
285 gchar *res;
286 //creating array from GList, converting int to gchar*
287 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
288 GSList *entry = buddy->groups;
290 if (!ids_arr) return NULL;
292 while (entry) {
293 struct sipe_group * group = entry->data;
294 ids_arr[i] = g_strdup_printf("%d", group->id);
295 entry = entry->next;
296 i++;
298 ids_arr[i] = NULL;
299 res = g_strjoinv(" ", ids_arr);
300 g_strfreev(ids_arr);
301 return res;
305 * Sends buddy update to server
307 static void send_buddy_update(struct sipe_core_private *sipe_private,
308 struct sipe_buddy *buddy,
309 const gchar *alias)
311 gchar *groups = sipe_get_buddy_groups_string(buddy);
313 if (groups) {
314 gchar *request;
315 SIPE_DEBUG_INFO("Saving buddy %s with alias '%s' and groups '%s'",
316 buddy->name, alias, groups);
318 /* alias can contain restricted characters */
319 request = g_markup_printf_escaped("<m:displayName>%s</m:displayName>"
320 "<m:groups>%s</m:groups>"
321 "<m:subscribed>true</m:subscribed>"
322 "<m:URI>%s</m:URI>"
323 "<m:externalURI />",
324 alias, groups, buddy->name);
325 g_free(groups);
327 sip_soap_request(sipe_private,
328 "setContact",
329 request);
330 g_free(request);
334 /* indicates that buddy information on the server needs updating */
335 void sipe_group_update_buddy(struct sipe_core_private *sipe_private,
336 struct sipe_buddy *buddy)
338 if (buddy) {
339 sipe_backend_buddy backend_buddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
340 buddy->name,
341 NULL);
342 if (backend_buddy) {
343 gchar *alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
344 backend_buddy);
345 send_buddy_update(sipe_private, buddy, alias);
346 g_free(alias);
351 void sipe_core_group_set_alias(struct sipe_core_public *sipe_public,
352 const gchar *who,
353 const gchar *alias)
355 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
356 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies,
357 who);
359 if (buddy)
360 send_buddy_update(sipe_private, buddy, alias);
364 Local Variables:
365 mode: c
366 c-file-style: "bsd"
367 indent-tabs-mode: t
368 tab-width: 8
369 End: