1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Copyright (C) 2001-2004 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "exchange-esource.h"
26 #include <libedataserver/e-source.h>
27 #include <libedataserver/e-source-list.h>
28 #include <libedataserver/e-source-group.h>
33 static gboolean is_offline (void);
36 add_folder_esource (ExchangeAccount *account,
37 FolderType folder_type,
38 const char *folder_name,
39 const char *physical_uri)
41 ESource *source = NULL;
42 ESourceGroup *source_group = NULL;
43 char *relative_uri = NULL;
46 gboolean is_contacts_folder = TRUE, group_new = FALSE, source_new = FALSE;
47 const char *offline = NULL;
48 char *username, *useremail, *authtype = NULL;
50 ESourceList *source_list = NULL;
51 gboolean offline_flag, update_selection = TRUE, foriegn_folder;
53 client = gconf_client_get_default ();
56 foriegn_folder = folder_type & FORIEGN_FOLDER_FLAG;
59 folder_type = folder_type & ~FORIEGN_FOLDER_FLAG;
61 if (folder_type == EXCHANGE_CONTACTS_FOLDER) {
62 source_list = e_source_list_new_for_gconf ( client,
64 /* Modify the URI handling of Contacts to the same way as calendar and tasks */
65 if (!g_str_has_prefix (physical_uri, "gal://")) {
66 relative_uri = g_strdup (physical_uri + strlen (EXCHANGE_URI_PREFIX));
69 else if (folder_type == EXCHANGE_CALENDAR_FOLDER) {
70 source_list = e_source_list_new_for_gconf ( client,
72 relative_uri = g_strdup (physical_uri + strlen (EXCHANGE_URI_PREFIX));
73 is_contacts_folder = FALSE;
75 else if (folder_type == EXCHANGE_TASKS_FOLDER) {
76 source_list = e_source_list_new_for_gconf ( client,
78 relative_uri = g_strdup (physical_uri + strlen (EXCHANGE_URI_PREFIX));
79 is_contacts_folder = FALSE;
82 exchange_account_is_offline_sync_set (account, &mode);
83 username = exchange_account_get_username (account);
84 useremail = exchange_account_get_email_id (account);
85 authtype = exchange_account_get_authtype (account);
87 if ((source_group = e_source_list_peek_group_by_name (source_list,
88 account->account_name)) == NULL) {
89 source_group = e_source_group_new (account->account_name,
91 if (!e_source_list_add_group (source_list, source_group, -1)) {
92 g_object_unref (source_list);
93 g_object_unref (source_group);
94 g_object_unref (client);
95 g_free (relative_uri);
98 if (is_contacts_folder && g_str_has_prefix (physical_uri, "gal://")) {
99 source = e_source_new_with_absolute_uri (folder_name,
101 e_source_set_property (source, "completion", "true");
104 source = e_source_new (folder_name, relative_uri);
107 if (mode == OFFLINE_MODE) {
108 /* If account is marked for offline sync during account
109 * creation, mark all the folders for offline sync
111 e_source_set_property (source, "offline_sync", "1");
114 if (foriegn_folder && (folder_type != EXCHANGE_CONTACTS_FOLDER)) {
115 e_source_set_property (source, "alarm", "never");
116 e_source_set_property (source, "subscriber", useremail);
119 e_source_set_property (source, "username", username);
120 e_source_set_property (source, "auth-domain", "Exchange");
122 e_source_set_property (source, "auth-type", authtype);
123 if (is_contacts_folder)
124 e_source_set_property (source, "auth", "plain/password");
126 e_source_set_property (source, "auth", "1");
127 e_source_group_add_source (source_group, source, -1);
128 e_source_list_sync (source_list, NULL);
129 group_new = source_new = TRUE;
132 /* source group already exists*/
133 if((source = e_source_group_peek_source_by_name (source_group,
134 folder_name)) == NULL) {
135 printf("old group, new source\n");
136 if (is_contacts_folder && g_str_has_prefix (physical_uri, "gal://")) {
137 source = e_source_new_with_absolute_uri (
138 folder_name, physical_uri);
139 e_source_set_property (source, "completion", "true");
142 source = e_source_new (folder_name, relative_uri);
145 if (mode == OFFLINE_MODE)
146 e_source_set_property (source, "offline_sync", "1");
148 e_source_set_property (source, "username", username);
149 e_source_set_property (source, "auth-domain", "Exchange");
151 e_source_set_property (source, "auth-type", authtype);
152 if (is_contacts_folder)
153 e_source_set_property (source, "auth", "plain/password");
155 e_source_set_property (source, "auth", "1");
157 if (foriegn_folder && (folder_type != EXCHANGE_CONTACTS_FOLDER)) {
158 e_source_set_property (source, "alarm", "never");
159 e_source_set_property (source, "subscriber", useremail);
162 e_source_group_add_source (source_group, source, -1);
164 e_source_list_sync (source_list, NULL);
166 update_selection = FALSE;
167 /* source group and source both already exist */
168 offline = e_source_get_property (source, "offline_sync");
170 /* Folder doesn't have any offline property set */
171 if (mode == OFFLINE_MODE) {
172 e_source_set_property (source, "offline_sync", "1");
173 e_source_list_sync (source_list, NULL);
179 offline_flag = is_offline ();
180 if (source && !is_contacts_folder && update_selection) {
182 /* Select the folder created */
183 if (folder_type == EXCHANGE_CALENDAR_FOLDER && !offline_flag) {
184 ids = gconf_client_get_list (client,
185 CONF_KEY_SELECTED_CAL_SOURCES,
186 GCONF_VALUE_STRING, NULL);
187 ids = g_slist_append (ids,
188 g_strdup (e_source_peek_uid (source)));
189 gconf_client_set_list (client,
190 CONF_KEY_SELECTED_CAL_SOURCES,
191 GCONF_VALUE_STRING, ids, NULL);
192 g_slist_foreach (ids, (GFunc) g_free, NULL);
195 else if (folder_type == EXCHANGE_TASKS_FOLDER && !offline_flag) {
196 ids = gconf_client_get_list (client,
197 CONF_KEY_SELECTED_TASKS_SOURCES,
198 GCONF_VALUE_STRING, NULL);
200 ids = g_slist_append (ids,
201 g_strdup (e_source_peek_uid (source)));
202 gconf_client_set_list (client,
203 CONF_KEY_SELECTED_TASKS_SOURCES,
204 GCONF_VALUE_STRING, ids, NULL);
205 g_slist_foreach (ids, (GFunc) g_free, NULL);
210 g_free (relative_uri);
214 g_object_unref (source);
216 g_object_unref (source_group);
217 g_object_unref (source_list);
218 g_object_unref (client);
222 remove_folder_esource (ExchangeAccount *account,
223 FolderType folder_type,
224 const char *physical_uri)
230 gboolean found_group, is_contacts_folder = TRUE;
231 char *read_uri = NULL;
232 const char *source_uid;
233 GSList *ids, *temp_ids, *node_to_be_deleted;
235 ESourceList *source_list = NULL;
237 client = gconf_client_get_default ();
239 /* Remove ESource for a given folder */
240 if (folder_type == EXCHANGE_CONTACTS_FOLDER) {
241 source_list = e_source_list_new_for_gconf ( client,
244 else if (folder_type == EXCHANGE_CALENDAR_FOLDER) {
245 source_list = e_source_list_new_for_gconf ( client,
247 is_contacts_folder = FALSE;
249 else if (folder_type == EXCHANGE_TASKS_FOLDER) {
250 source_list = e_source_list_new_for_gconf ( client,
252 is_contacts_folder = FALSE;
255 groups = e_source_list_peek_groups (source_list);
258 for ( ; groups != NULL && !found_group; groups = g_slist_next (groups)) {
259 group = E_SOURCE_GROUP (groups->data);
261 if (strcmp (e_source_group_peek_name (group), account->account_name) == 0
263 strcmp (e_source_group_peek_base_uri (group), EXCHANGE_URI_PREFIX) == 0) {
265 sources = e_source_group_peek_sources (group);
267 for( ; sources != NULL; sources = g_slist_next (sources)) {
269 source = E_SOURCE (sources->data);
270 read_uri = e_source_get_uri (source);
272 if (strcmp (read_uri, physical_uri) == 0) {
274 source_uid = e_source_peek_uid (source);
275 /* Folder Deleted - Remove only the source */
277 e_source_group_remove_source_by_uid (
281 e_source_group_remove_source (
284 e_source_list_sync (source_list, NULL);
285 if (!is_contacts_folder) {
286 /* Remove from the selected folders */
287 if (folder_type == EXCHANGE_CALENDAR_FOLDER) {
288 ids = gconf_client_get_list (
290 CONF_KEY_SELECTED_CAL_SOURCES,
291 GCONF_VALUE_STRING, NULL);
293 node_to_be_deleted = g_slist_find_custom (ids,
295 (GCompareFunc) strcmp);
296 if (node_to_be_deleted) {
297 g_free (node_to_be_deleted->data);
298 ids = g_slist_delete_link (ids,
303 for (; temp_ids != NULL; temp_ids = g_slist_next (temp_ids))
304 g_free (temp_ids->data);
307 else if (folder_type == EXCHANGE_TASKS_FOLDER) {
308 ids = gconf_client_get_list (client,
309 CONF_KEY_SELECTED_TASKS_SOURCES,
310 GCONF_VALUE_STRING, NULL);
312 node_to_be_deleted = g_slist_find_custom (ids,
314 (GCompareFunc) strcmp);
315 if (node_to_be_deleted) {
316 g_free (node_to_be_deleted->data);
317 ids = g_slist_delete_link (ids,
322 for (; temp_ids != NULL; temp_ids = g_slist_next (temp_ids))
323 g_free (temp_ids->data);
334 g_object_unref (source_list);
335 g_object_unref (client);
343 gboolean offline = FALSE;
345 client = gconf_client_get_default ();
346 value = gconf_client_get (client,
347 "/apps/evolution/shell/start_offline", NULL);
349 offline = gconf_value_get_bool (value);
351 g_object_unref (client);
352 gconf_value_free (value);