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"
39 #include "sipe-utils.h"
46 struct group_user_context
{
52 sipe_group_context_destroy(gpointer data
)
54 struct group_user_context
*ctx
= data
;
55 g_free(ctx
->group_name
);
56 g_free(ctx
->user_name
);
61 process_add_group_response(struct sipe_core_private
*sipe_private
,
63 struct transaction
*trans
)
65 if (msg
->response
== 200) {
66 struct sipe_group
*group
;
67 struct group_user_context
*ctx
= trans
->payload
->data
;
72 xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
77 node
= sipe_xml_child(xml
, "Body/addGroup/groupID");
83 group_id
= sipe_xml_data(node
);
89 group
= sipe_group_add(sipe_private
,
93 g_ascii_strtoull(group_id
, NULL
, 10));
96 if (group
&& ctx
->user_name
) {
97 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
100 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
102 (GCompareFunc
)sipe_group_compare
,
104 sipe_group_update_buddy(sipe_private
, buddy
);
115 sipe_group_compare(struct sipe_group
*group1
, struct sipe_group
*group2
) {
116 return group1
->id
- group2
->id
;
120 sipe_group_find_by_id(struct sipe_core_private
*sipe_private
,
123 struct sipe_group
*group
;
129 entry
= sipe_private
->groups
->list
;
132 if (group
->id
== id
) {
141 sipe_group_find_by_name(struct sipe_core_private
*sipe_private
,
144 struct sipe_group
*group
;
147 if (!sipe_private
|| !name
)
150 entry
= sipe_private
->groups
->list
;
153 if (sipe_strequal(group
->name
, name
)) {
162 sipe_group_create(struct sipe_core_private
*sipe_private
,
166 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
167 struct group_user_context
*ctx
= g_new0(struct group_user_context
, 1);
168 const gchar
*soap_name
= sipe_strequal(name
, _("Other Contacts")) ? "~" : name
;
170 ctx
->group_name
= g_strdup(name
);
171 ctx
->user_name
= g_strdup(who
);
172 payload
->destroy
= sipe_group_context_destroy
;
175 /* soap_name can contain restricted characters */
176 request
= g_markup_printf_escaped("<m:name>%s</m:name>"
179 sip_soap_request_cb(sipe_private
,
182 process_add_group_response
,
187 gboolean
sipe_group_rename(struct sipe_core_private
*sipe_private
,
188 struct sipe_group
*group
,
191 gboolean renamed
= sipe_backend_buddy_group_rename(SIPE_CORE_PUBLIC
,
196 group
->name
= g_strdup(name
);
201 struct sipe_group
*sipe_group_add(struct sipe_core_private
*sipe_private
,
203 const gchar
*exchange_key
,
204 const gchar
*change_key
,
207 struct sipe_group
*group
= NULL
;
209 if (!is_empty(name
) &&
210 sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC
, name
)) {
212 group
= g_new0(struct sipe_group
, 1);
213 group
->name
= g_strdup(name
);
217 group
->exchange_key
= g_strdup(exchange_key
);
219 group
->change_key
= g_strdup(change_key
);
221 sipe_private
->groups
->list
= g_slist_append(sipe_private
->groups
->list
,
224 SIPE_DEBUG_INFO("sipe_group_add: created backend group '%s' with id %d",
225 group
->name
, group
->id
);
227 SIPE_DEBUG_INFO("sipe_group_add: backend group '%s' already exists",
233 static void group_free(struct sipe_core_private
*sipe_private
,
234 struct sipe_group
*group
)
236 sipe_private
->groups
->list
= g_slist_remove(sipe_private
->groups
->list
,
239 g_free(group
->exchange_key
);
240 g_free(group
->change_key
);
244 void sipe_group_remove(struct sipe_core_private
*sipe_private
,
245 struct sipe_group
*group
)
248 SIPE_DEBUG_INFO("removing group %s (id %d)", group
->name
, group
->id
);
249 sipe_backend_buddy_group_remove(SIPE_CORE_PUBLIC
, group
->name
);
250 group_free(sipe_private
, group
);
255 sipe_core_group_rename(struct sipe_core_public
*sipe_public
,
256 const gchar
*old_name
,
257 const gchar
*new_name
)
259 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
260 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, old_name
);
263 SIPE_DEBUG_INFO("sipe_core_group_rename: from '%s' to '%s'", old_name
, new_name
);
265 if (sipe_ucs_is_migrated(sipe_private
)) {
266 sipe_ucs_group_rename(sipe_private
,
270 /* new_name can contain restricted characters */
271 gchar
*request
= g_markup_printf_escaped("<m:groupID>%d</m:groupID>"
272 "<m:name>%s</m:name>"
276 sip_soap_request(sipe_private
,
282 g_free(s_group
->name
);
283 s_group
->name
= g_strdup(new_name
);
285 SIPE_DEBUG_INFO("sipe_core_group_rename: cannot find group '%s'", old_name
);
290 sipe_core_group_remove(struct sipe_core_public
*sipe_public
,
293 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
294 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, name
);
297 SIPE_DEBUG_INFO("sipe_core_group_remove: delete '%s'", name
);
299 if (sipe_ucs_is_migrated(sipe_private
)) {
300 sipe_ucs_group_remove(sipe_private
,
303 gchar
*request
= g_strdup_printf("<m:groupID>%d</m:groupID>",
305 sip_soap_request(sipe_private
,
311 group_free(sipe_private
, s_group
);
313 SIPE_DEBUG_INFO("sipe_core_group_remove: cannot find group '%s'", name
);
318 * Returns string like "2 4 7 8" - group ids buddy belong to.
320 static gchar
*sipe_get_buddy_groups_string(struct sipe_buddy
*buddy
)
324 //creating array from GList, converting int to gchar*
325 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
326 GSList
*entry
= buddy
->groups
;
328 if (!ids_arr
) return NULL
;
331 struct sipe_group
* group
= entry
->data
;
332 ids_arr
[i
] = g_strdup_printf("%d", group
->id
);
337 res
= g_strjoinv(" ", ids_arr
);
343 * Sends buddy update to server
345 static void send_buddy_update(struct sipe_core_private
*sipe_private
,
346 struct sipe_buddy
*buddy
,
349 gchar
*groups
= sipe_get_buddy_groups_string(buddy
);
353 SIPE_DEBUG_INFO("Saving buddy %s with alias '%s' and groups '%s'",
354 buddy
->name
, alias
, groups
);
356 /* alias can contain restricted characters */
357 request
= g_markup_printf_escaped("<m:displayName>%s</m:displayName>"
358 "<m:groups>%s</m:groups>"
359 "<m:subscribed>true</m:subscribed>"
362 alias
, groups
, buddy
->name
);
365 sip_soap_request(sipe_private
,
372 /* indicates that buddy information on the server needs updating */
373 void sipe_group_update_buddy(struct sipe_core_private
*sipe_private
,
374 struct sipe_buddy
*buddy
)
377 sipe_backend_buddy backend_buddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
381 gchar
*alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
383 send_buddy_update(sipe_private
, buddy
, alias
);
389 void sipe_core_group_set_alias(struct sipe_core_public
*sipe_public
,
393 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
394 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
398 send_buddy_update(sipe_private
, buddy
, alias
);
401 struct sipe_group
*sipe_group_first(struct sipe_core_private
*sipe_private
)
403 return(sipe_private
->groups
->list
? sipe_private
->groups
->list
->data
: NULL
);
406 guint
sipe_group_count(struct sipe_core_private
*sipe_private
)
408 return(g_slist_length(sipe_private
->groups
->list
));
411 void sipe_group_init(struct sipe_core_private
*sipe_private
)
413 sipe_private
->groups
= g_new0(struct sipe_groups
, 1);
416 void sipe_group_free(struct sipe_core_private
*sipe_private
)
420 while ((entry
= sipe_private
->groups
->list
) != NULL
)
421 group_free(sipe_private
, entry
->data
);
423 g_free(sipe_private
->groups
);
424 sipe_private
->groups
= NULL
;