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"
41 struct group_user_context
{
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
);
56 process_add_group_response(struct sipe_core_private
*sipe_private
,
58 struct transaction
*trans
)
60 if (msg
->response
== 200) {
61 struct sipe_group
*group
;
62 struct group_user_context
*ctx
= trans
->payload
->data
;
66 struct sipe_buddy
*buddy
;
68 xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
73 node
= sipe_xml_child(xml
, "Body/addGroup/groupID");
79 group_id
= sipe_xml_data(node
);
85 group
= g_new0(struct sipe_group
, 1);
86 group
->id
= (int)g_ascii_strtod(group_id
, NULL
);
88 group
->name
= g_strdup(ctx
->group_name
);
90 sipe_group_add(sipe_private
, group
);
93 buddy
= g_hash_table_lookup(sipe_private
->buddies
, ctx
->user_name
);
95 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
97 (GCompareFunc
)sipe_group_compare
,
99 sipe_group_update_buddy(sipe_private
, buddy
);
110 sipe_group_compare(struct sipe_group
*group1
, struct sipe_group
*group2
) {
111 return group1
->id
- group2
->id
;
115 sipe_group_find_by_id(struct sipe_core_private
*sipe_private
,
118 struct sipe_group
*group
;
124 entry
= sipe_private
->groups
;
127 if (group
->id
== id
) {
136 sipe_group_find_by_name(struct sipe_core_private
*sipe_private
,
139 struct sipe_group
*group
;
142 if (!sipe_private
|| !name
)
145 entry
= sipe_private
->groups
;
148 if (sipe_strequal(group
->name
, name
)) {
157 sipe_group_create(struct sipe_core_private
*sipe_private
,
161 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
162 struct group_user_context
*ctx
= g_new0(struct group_user_context
, 1);
163 const gchar
*soap_name
= sipe_strequal(name
, _("Other Contacts")) ? "~" : name
;
165 ctx
->group_name
= g_strdup(name
);
166 ctx
->user_name
= g_strdup(who
);
167 payload
->destroy
= sipe_group_context_destroy
;
170 /* soap_name can contain restricted characters */
171 request
= g_markup_printf_escaped("<m:name>%s</m:name>"
174 sip_soap_request_cb(sipe_private
,
177 process_add_group_response
,
182 gboolean
sipe_group_rename(struct sipe_core_private
*sipe_private
,
183 struct sipe_group
*group
,
186 gboolean renamed
= sipe_backend_buddy_group_rename(SIPE_CORE_PUBLIC
,
191 group
->name
= g_strdup(name
);
197 sipe_group_add(struct sipe_core_private
*sipe_private
,
198 struct sipe_group
* group
)
200 if (sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC
,group
->name
))
202 SIPE_DEBUG_INFO("added group %s (id %d)", group
->name
, group
->id
);
203 sipe_private
->groups
= g_slist_append(sipe_private
->groups
,
208 SIPE_DEBUG_INFO("did not add group %s", group
->name
? group
->name
: "");
212 void sipe_group_free(struct sipe_core_private
*sipe_private
,
213 struct sipe_group
*group
)
215 sipe_private
->groups
= g_slist_remove(sipe_private
->groups
,
221 void sipe_group_remove(struct sipe_core_private
*sipe_private
,
222 struct sipe_group
*group
)
225 SIPE_DEBUG_INFO("removing group %s (id %d)", group
->name
, group
->id
);
226 sipe_backend_buddy_group_remove(SIPE_CORE_PUBLIC
, group
->name
);
227 sipe_group_free(sipe_private
, group
);
232 sipe_core_group_rename(struct sipe_core_public
*sipe_public
,
233 const gchar
*old_name
,
234 const gchar
*new_name
)
236 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
237 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, old_name
);
241 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name
, new_name
);
242 /* new_name can contain restricted characters */
243 request
= g_markup_printf_escaped("<m:groupID>%d</m:groupID>"
244 "<m:name>%s</m:name>"
246 s_group
->id
, new_name
);
247 sip_soap_request(sipe_private
,
252 g_free(s_group
->name
);
253 s_group
->name
= g_strdup(new_name
);
255 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name
);
260 sipe_core_group_remove(struct sipe_core_public
*sipe_public
,
263 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
264 struct sipe_group
*s_group
= sipe_group_find_by_name(sipe_private
, name
);
268 SIPE_DEBUG_INFO("Deleting group %s", name
);
269 request
= g_strdup_printf("<m:groupID>%d</m:groupID>",
271 sip_soap_request(sipe_private
,
276 sipe_group_free(sipe_private
, s_group
);
278 SIPE_DEBUG_INFO("Cannot find group %s to delete", name
);
283 * Returns string like "2 4 7 8" - group ids buddy belong to.
285 static gchar
*sipe_get_buddy_groups_string(struct sipe_buddy
*buddy
)
289 //creating array from GList, converting int to gchar*
290 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
291 GSList
*entry
= buddy
->groups
;
293 if (!ids_arr
) return NULL
;
296 struct sipe_group
* group
= entry
->data
;
297 ids_arr
[i
] = g_strdup_printf("%d", group
->id
);
302 res
= g_strjoinv(" ", ids_arr
);
308 * Sends buddy update to server
310 static void send_buddy_update(struct sipe_core_private
*sipe_private
,
311 struct sipe_buddy
*buddy
,
314 gchar
*groups
= sipe_get_buddy_groups_string(buddy
);
318 SIPE_DEBUG_INFO("Saving buddy %s with alias '%s' and groups '%s'",
319 buddy
->name
, alias
, groups
);
321 /* alias can contain restricted characters */
322 request
= g_markup_printf_escaped("<m:displayName>%s</m:displayName>"
323 "<m:groups>%s</m:groups>"
324 "<m:subscribed>true</m:subscribed>"
327 alias
, groups
, buddy
->name
);
330 sip_soap_request(sipe_private
,
337 /* indicates that buddy information on the server needs updating */
338 void sipe_group_update_buddy(struct sipe_core_private
*sipe_private
,
339 struct sipe_buddy
*buddy
)
342 sipe_backend_buddy backend_buddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
346 gchar
*alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
348 send_buddy_update(sipe_private
, buddy
, alias
);
354 void sipe_core_group_set_alias(struct sipe_core_public
*sipe_public
,
358 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
359 struct sipe_buddy
*buddy
= g_hash_table_lookup(sipe_private
->buddies
,
363 send_buddy_update(sipe_private
, buddy
, alias
);