6 * Copyright (C) 2011 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
25 #include "sipe-core.h"
26 #include "sipe-core-private.h"
27 #include "sipe-backend.h"
28 #include "sip-transport.h"
30 #include "sipe-buddy.h"
32 #include "sipe-group.h"
37 sipe_group_context_destroy(gpointer data
)
39 struct group_user_context
*ctx
= data
;
40 g_free(ctx
->group_name
);
41 g_free(ctx
->user_name
);
46 process_add_group_response(struct sipe_core_private
*sipe_private
,
48 struct transaction
*trans
)
50 if (msg
->response
== 200) {
51 struct sipe_group
*group
;
52 struct group_user_context
*ctx
= trans
->payload
->data
;
56 struct sipe_buddy
*buddy
;
58 xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
63 node
= sipe_xml_child(xml
, "Body/addGroup/groupID");
69 group_id
= sipe_xml_data(node
);
75 group
= g_new0(struct sipe_group
, 1);
76 group
->id
= (int)g_ascii_strtod(group_id
, NULL
);
78 group
->name
= g_strdup(ctx
->group_name
);
80 sipe_group_add(sipe_private
, group
);
83 buddy
= g_hash_table_lookup(sipe_private
->buddies
, ctx
->user_name
);
85 buddy
->groups
= slist_insert_unique_sorted(buddy
->groups
, group
, (GCompareFunc
)sipe_group_compare
);
88 sipe_core_group_set_user(SIPE_CORE_PUBLIC
, ctx
->user_name
);
98 sipe_group_compare(struct sipe_group
*group1
, struct sipe_group
*group2
) {
99 return group1
->id
- group2
->id
;
103 sipe_group_find_by_id(struct sipe_core_private
*sipe_private
,
106 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
107 struct sipe_group
*group
;
116 if (group
->id
== id
) {
125 sipe_group_find_by_name(struct sipe_core_private
*sipe_private
,
128 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
129 struct sipe_group
*group
;
138 if (sipe_strequal(group
->name
, name
)) {
147 sipe_group_create(struct sipe_core_private
*sipe_private
,
151 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
152 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
153 struct group_user_context
*ctx
= g_new0(struct group_user_context
, 1);
154 const gchar
*soap_name
= sipe_strequal(name
, _("Other Contacts")) ? "~" : name
;
156 ctx
->group_name
= g_strdup(name
);
157 ctx
->user_name
= g_strdup(who
);
158 payload
->destroy
= sipe_group_context_destroy
;
161 body
= g_markup_printf_escaped(SIPE_SOAP_ADD_GROUP
, soap_name
, sip
->contacts_delta
++);
162 send_soap_request_with_cb(sipe_private
, NULL
, body
, process_add_group_response
, payload
);
167 sipe_group_add(struct sipe_core_private
*sipe_private
,
168 struct sipe_group
* group
)
170 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
171 if (sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC
,group
->name
))
173 SIPE_DEBUG_INFO("added group %s (id %d)", group
->name
, group
->id
);
174 sip
->groups
= g_slist_append(sip
->groups
, group
);
178 SIPE_DEBUG_INFO("did not add group %s", group
->name
? group
->name
: "");
183 sipe_core_group_rename(struct sipe_core_public
*sipe_public
,
184 const gchar
*old_name
,
185 const gchar
*new_name
)
187 struct sipe_group
*s_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_name
);
190 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA
;
192 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name
, new_name
);
193 body
= g_markup_printf_escaped(SIPE_SOAP_MOD_GROUP
, s_group
->id
, new_name
, sip
->contacts_delta
++);
194 send_soap_request(SIPE_CORE_PRIVATE
, body
);
196 g_free(s_group
->name
);
197 s_group
->name
= g_strdup(new_name
);
199 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name
);
204 sipe_core_group_remove(struct sipe_core_public
*sipe_public
,
207 struct sipe_group
* s_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, name
);
210 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA
;
212 SIPE_DEBUG_INFO("Deleting group %s", name
);
213 body
= g_strdup_printf(SIPE_SOAP_DEL_GROUP
, s_group
->id
, sip
->contacts_delta
++);
214 send_soap_request(SIPE_CORE_PRIVATE
, body
);
217 sip
->groups
= g_slist_remove(sip
->groups
, s_group
);
218 g_free(s_group
->name
);
221 SIPE_DEBUG_INFO("Cannot find group %s to delete", name
);