6 * Copyright (C) 2011-2013 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
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"
38 #include "sipe-utils.h"
45 struct group_user_context
{
51 sipe_group_context_destroy(gpointer data
)
53 struct group_user_context
*ctx
= data
;
54 g_free(ctx
->group_name
);
55 g_free(ctx
->user_name
);
60 process_add_group_response(struct sipe_core_private
*sipe_private
,
62 struct transaction
*trans
)
64 if (msg
->response
== 200) {
65 struct sipe_group
*group
;
66 struct group_user_context
*ctx
= trans
->payload
->data
;
71 xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
76 node
= sipe_xml_child(xml
, "Body/addGroup/groupID");
82 group_id
= sipe_xml_data(node
);
88 group
= sipe_group_add(sipe_private
,
90 g_ascii_strtoull(group_id
, NULL
, 10));
93 if (group
&& ctx
->user_name
) {
94 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
97 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
99 (GCompareFunc
)sipe_group_compare
,
101 sipe_group_update_buddy(sipe_private
, buddy
);
112 sipe_group_compare(struct sipe_group
*group1
, struct sipe_group
*group2
) {
113 return group1
->id
- group2
->id
;
117 sipe_group_find_by_id(struct sipe_core_private
*sipe_private
,
120 struct sipe_group
*group
;
126 entry
= sipe_private
->groups
->list
;
129 if (group
->id
== id
) {
138 sipe_group_find_by_name(struct sipe_core_private
*sipe_private
,
141 struct sipe_group
*group
;
144 if (!sipe_private
|| !name
)
147 entry
= sipe_private
->groups
->list
;
150 if (sipe_strequal(group
->name
, name
)) {
159 sipe_group_create(struct sipe_core_private
*sipe_private
,
163 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
164 struct group_user_context
*ctx
= g_new0(struct group_user_context
, 1);
165 const gchar
*soap_name
= sipe_strequal(name
, _("Other Contacts")) ? "~" : name
;
167 ctx
->group_name
= g_strdup(name
);
168 ctx
->user_name
= g_strdup(who
);
169 payload
->destroy
= sipe_group_context_destroy
;
172 /* soap_name can contain restricted characters */
173 request
= g_markup_printf_escaped("<m:name>%s</m:name>"
176 sip_soap_request_cb(sipe_private
,
179 process_add_group_response
,
184 gboolean
sipe_group_rename(struct sipe_core_private
*sipe_private
,
185 struct sipe_group
*group
,
188 gboolean renamed
= sipe_backend_buddy_group_rename(SIPE_CORE_PUBLIC
,
193 group
->name
= g_strdup(name
);
198 struct sipe_group
*sipe_group_add(struct sipe_core_private
*sipe_private
,
202 struct sipe_group
*group
= NULL
;
204 if (!is_empty(name
) &&
205 sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC
, name
)) {
207 group
= g_new0(struct sipe_group
, 1);
208 group
->name
= g_strdup(name
);
211 sipe_private
->groups
->list
= g_slist_append(sipe_private
->groups
->list
,
214 SIPE_DEBUG_INFO("sipe_group_add: created backend group '%s' with id %d",
215 group
->name
, group
->id
);
217 SIPE_DEBUG_INFO("sipe_group_add: backend group '%s' already exists",
223 static void group_free(struct sipe_core_private
*sipe_private
,
224 struct sipe_group
*group
)
226 sipe_private
->groups
->list
= g_slist_remove(sipe_private
->groups
->list
,
232 void sipe_group_remove(struct sipe_core_private
*sipe_private
,
233 struct sipe_group
*group
)
236 SIPE_DEBUG_INFO("removing group %s (id %d)", group
->name
, group
->id
);
237 sipe_backend_buddy_group_remove(SIPE_CORE_PUBLIC
, group
->name
);
238 group_free(sipe_private
, group
);
243 sipe_core_group_rename(struct sipe_core_public
*sipe_public
,
244 const gchar
*old_name
,
245 const gchar
*new_name
)
247 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
248 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, old_name
);
252 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name
, new_name
);
253 /* new_name can contain restricted characters */
254 request
= g_markup_printf_escaped("<m:groupID>%d</m:groupID>"
255 "<m:name>%s</m:name>"
257 s_group
->id
, new_name
);
258 sip_soap_request(sipe_private
,
263 g_free(s_group
->name
);
264 s_group
->name
= g_strdup(new_name
);
266 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name
);
271 sipe_core_group_remove(struct sipe_core_public
*sipe_public
,
274 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
275 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, name
);
279 SIPE_DEBUG_INFO("Deleting group %s", name
);
280 request
= g_strdup_printf("<m:groupID>%d</m:groupID>",
282 sip_soap_request(sipe_private
,
287 group_free(sipe_private
, s_group
);
289 SIPE_DEBUG_INFO("Cannot find group %s to delete", name
);
294 * Returns string like "2 4 7 8" - group ids buddy belong to.
296 static gchar
*sipe_get_buddy_groups_string(struct sipe_buddy
*buddy
)
300 //creating array from GList, converting int to gchar*
301 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
302 GSList
*entry
= buddy
->groups
;
304 if (!ids_arr
) return NULL
;
307 struct sipe_group
* group
= entry
->data
;
308 ids_arr
[i
] = g_strdup_printf("%d", group
->id
);
313 res
= g_strjoinv(" ", ids_arr
);
319 * Sends buddy update to server
321 static void send_buddy_update(struct sipe_core_private
*sipe_private
,
322 struct sipe_buddy
*buddy
,
325 gchar
*groups
= sipe_get_buddy_groups_string(buddy
);
329 SIPE_DEBUG_INFO("Saving buddy %s with alias '%s' and groups '%s'",
330 buddy
->name
, alias
, groups
);
332 /* alias can contain restricted characters */
333 request
= g_markup_printf_escaped("<m:displayName>%s</m:displayName>"
334 "<m:groups>%s</m:groups>"
335 "<m:subscribed>true</m:subscribed>"
338 alias
, groups
, buddy
->name
);
341 sip_soap_request(sipe_private
,
348 /* indicates that buddy information on the server needs updating */
349 void sipe_group_update_buddy(struct sipe_core_private
*sipe_private
,
350 struct sipe_buddy
*buddy
)
353 sipe_backend_buddy backend_buddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
357 gchar
*alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
359 send_buddy_update(sipe_private
, buddy
, alias
);
365 void sipe_core_group_set_alias(struct sipe_core_public
*sipe_public
,
369 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
370 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
374 send_buddy_update(sipe_private
, buddy
, alias
);
377 struct sipe_group
*sipe_group_first(struct sipe_core_private
*sipe_private
)
379 return(sipe_private
->groups
->list
? sipe_private
->groups
->list
->data
: NULL
);
382 guint
sipe_group_count(struct sipe_core_private
*sipe_private
)
384 return(g_slist_length(sipe_private
->groups
->list
));
387 void sipe_group_init(struct sipe_core_private
*sipe_private
)
389 sipe_private
->groups
= g_new0(struct sipe_groups
, 1);
392 void sipe_group_free(struct sipe_core_private
*sipe_private
)
396 while ((entry
= sipe_private
->groups
->list
) != NULL
)
397 group_free(sipe_private
, entry
->data
);
399 g_free(sipe_private
->groups
);
400 sipe_private
->groups
= NULL
;