Adapt libedata-cal to the new ESource API.
[platform/upstream/evolution-data-server.git] / calendar / libedata-cal / e-cal-backend-util.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* Evolution calendar - generic backend class
3  *
4  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5  *
6  * Authors: Rodrigo Moya <rodrigo@ximian.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2 of the GNU Lesser General Public
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <glib/gi18n-lib.h>
28 #include "e-cal-backend-util.h"
29 #include <libedataserver/e-source-mail-account.h>
30 #include <libedataserver/e-source-mail-identity.h>
31
32 /**
33  * e_cal_backend_mail_account_get_default:
34  * @registry: an #ESourceRegistry
35  * @address: placeholder for default address
36  * @name: placeholder for name
37  *
38  * Retrieve the default mail account as stored in Evolution configuration.
39  *
40  * Returns: TRUE if there is a default account, FALSE otherwise.
41  */
42 gboolean
43 e_cal_backend_mail_account_get_default (ESourceRegistry *registry,
44                                         gchar **address,
45                                         gchar **name)
46 {
47         ESource *source;
48         ESourceMailIdentity *extension;
49         const gchar *extension_name;
50
51         g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
52
53         source = e_source_registry_ref_default_mail_identity (registry);
54
55         if (source == NULL)
56                 return FALSE;
57
58         extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
59         extension = e_source_get_extension (source, extension_name);
60
61         if (address != NULL)
62                 *address = e_source_mail_identity_dup_address (extension);
63
64         if (name != NULL)
65                 *name = e_source_mail_identity_dup_name (extension);
66
67         g_object_unref (source);
68
69         return TRUE;
70 }
71
72 /**
73  * e_cal_backend_mail_account_is_valid:
74  * @registry: an #ESourceRegistry
75  * @user: user name for the account to check
76  * @name: placeholder for the account name
77  *
78  * Checks that a mail account is valid, and returns its name.
79  *
80  * Returns: TRUE if the account is valid, FALSE if not.
81  */
82 gboolean
83 e_cal_backend_mail_account_is_valid (ESourceRegistry *registry,
84                                      gchar *user,
85                                      gchar **name)
86 {
87         GList *list, *iter;
88         const gchar *extension_name;
89         gboolean valid = FALSE;
90
91         g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
92         g_return_val_if_fail (user != NULL, FALSE);
93
94         extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
95
96         list = e_source_registry_list_sources (registry, extension_name);
97
98         for (iter = list; iter != NULL; iter = g_list_next (iter)) {
99                 ESource *source = E_SOURCE (iter->data);
100                 ESourceMailAccount *mail_account;
101                 ESourceMailIdentity *mail_identity;
102                 const gchar *uid;
103                 gboolean match = FALSE;
104                 gchar *address;
105
106                 if (!e_source_get_enabled (source))
107                         continue;
108
109                 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
110                 mail_account = e_source_get_extension (source, extension_name);
111                 uid = e_source_mail_account_get_identity_uid (mail_account);
112
113                 if (uid == NULL)
114                         continue;
115
116                 source = e_source_registry_ref_source (registry, uid);
117
118                 if (source == NULL)
119                         continue;
120
121                 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
122
123                 if (!e_source_has_extension (source, extension_name)) {
124                         g_object_unref (source);
125                         continue;
126                 }
127
128                 mail_identity = e_source_get_extension (source, extension_name);
129                 address = e_source_mail_identity_dup_address (mail_identity);
130
131                 if (address != NULL) {
132                         match = (g_ascii_strcasecmp (address, user) == 0);
133                         g_free (address);
134                 }
135
136                 if (match && name != NULL)
137                         *name = e_source_dup_display_name (source);
138
139                 g_object_unref (source);
140
141                 if (match) {
142                         valid = TRUE;
143                         break;
144                 }
145         }
146
147         g_list_free_full (list, (GDestroyNotify) g_object_unref);
148
149         return valid;
150 }
151
152 /**
153  * is_attendee_declined:
154  * @icalcomp: Component where to check the attendee list.
155  * @email: Attendee's email to look for.
156  *
157  * Returns: Whether the required attendee declined or not.
158  *          It's not necessary to have this attendee in the list.
159  **/
160 static gboolean
161 is_attendee_declined (icalcomponent *icalcomp,
162                       const gchar *email)
163 {
164         icalproperty *prop;
165         icalparameter *param;
166
167         g_return_val_if_fail (icalcomp != NULL, FALSE);
168         g_return_val_if_fail (email != NULL, FALSE);
169
170         for (prop = icalcomponent_get_first_property (icalcomp, ICAL_ATTENDEE_PROPERTY);
171              prop != NULL;
172              prop = icalcomponent_get_next_property (icalcomp, ICAL_ATTENDEE_PROPERTY)) {
173                 gchar *attendee;
174                 gchar *text = NULL;
175
176                 attendee = icalproperty_get_value_as_string_r (prop);
177                 if (!attendee)
178                         continue;
179
180                 if (!g_ascii_strncasecmp (attendee, "mailto:", 7))
181                         text = g_strdup (attendee + 7);
182                 text = g_strstrip (text);
183
184                 if (!g_ascii_strcasecmp (email, text)) {
185                         g_free (text);
186                         g_free (attendee);
187                         break;
188                 }
189                 g_free (text);
190                 g_free (attendee);
191         }
192
193         if (!prop)
194                 return FALSE;
195
196         param = icalproperty_get_first_parameter (prop, ICAL_PARTSTAT_PARAMETER);
197
198         return param && icalparameter_get_partstat (param) == ICAL_PARTSTAT_DECLINED;
199 }
200
201 /**
202  * e_cal_backend_user_declined:
203  * @registry: an #ESourceRegistry
204  * @icalcomp: component where to check
205  *
206  * Returns: Whether icalcomp contains attendee with a mail same as any of
207  *          configured enabled mail account and whether this user declined.
208  *
209  * Since: 2.26
210  **/
211 gboolean
212 e_cal_backend_user_declined (ESourceRegistry *registry,
213                              icalcomponent *icalcomp)
214 {
215         GList *list, *iter;
216         const gchar *extension_name;
217         gboolean declined = FALSE;
218
219         g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
220         g_return_val_if_fail (icalcomp != NULL, FALSE);
221
222         extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
223
224         list = e_source_registry_list_sources (registry, extension_name);
225
226         for (iter = list; iter != NULL; iter = g_list_next (iter)) {
227                 ESource *source = E_SOURCE (iter->data);
228                 ESourceMailIdentity *extension;
229                 const gchar *address;
230
231                 if (!e_source_get_enabled (source))
232                         continue;
233
234                 extension = e_source_get_extension (source, extension_name);
235                 address = e_source_mail_identity_get_address (extension);
236
237                 if (address == NULL)
238                         continue;
239
240                 if (is_attendee_declined (icalcomp, address)) {
241                         declined = TRUE;
242                         break;
243                 }
244         }
245
246         g_list_free_full (list, (GDestroyNotify) g_object_unref);
247
248         return declined;
249 }
250