1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Copyright (C) 2001-2004 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* ExchangeConfigListener: a class that listens to the config database
* and handles creating the ExchangeAccount object and making sure that
* default folders are updated as needed.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "exchange-config-listener.h"
#include "exchange-operations.h"
#include <exchange-account.h>
#include <exchange-constants.h>
#include <exchange-hierarchy.h>
#include <e-folder-exchange.h>
#include <e2k-marshal.h>
#include <e2k-uri.h>
#include <camel/camel-url.h>
#include <e-util/e-error.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-source-list.h>
#include <libedataserver/e-source-group.h>
#include <libedataserverui/e-passwords.h>
#include <stdlib.h>
#include <string.h>
struct _ExchangeConfigListenerPrivate {
GConfClient *gconf;
guint idle_id;
char *configured_uri, *configured_name;
EAccount *configured_account;
ExchangeAccount *exchange_account;
};
typedef struct {
const char *name;
const char *uri;
int type;
}FolderInfo;
enum {
EXCHANGE_ACCOUNT_CREATED,
EXCHANGE_ACCOUNT_REMOVED,
LAST_SIGNAL
};
static guint signals [LAST_SIGNAL] = { 0 };
#define PARENT_TYPE E_TYPE_ACCOUNT_LIST
#define CONF_KEY_SELECTED_CAL_SOURCES "/apps/evolution/calendar/display/selected_calendars"
#define CONF_KEY_SELECTED_TASKS_SOURCES "/apps/evolution/calendar/tasks/selected_tasks"
static EAccountListClass *parent_class = NULL;
static void dispose (GObject *object);
static void finalize (GObject *object);
static void account_added (EAccountList *account_listener,
EAccount *account);
static void account_changed (EAccountList *account_listener,
EAccount *account);
static void account_removed (EAccountList *account_listener,
EAccount *account);
static void exchange_add_autocompletion_folders (GConfClient *gc_client,
ExchangeAccount *account);
static gboolean exchange_camel_urls_is_equal (const gchar *url1,
const gchar *url2);
static void remove_selected_non_offline_esources (ExchangeAccount *account,
const char *gconf_key);
static void
class_init (GObjectClass *object_class)
{
EAccountListClass *e_account_list_class =
E_ACCOUNT_LIST_CLASS (object_class);
parent_class = g_type_class_ref (PARENT_TYPE);
/* virtual method override */
object_class->dispose = dispose;
object_class->finalize = finalize;
e_account_list_class->account_added = account_added;
e_account_list_class->account_changed = account_changed;
e_account_list_class->account_removed = account_removed;
/* signals */
signals[EXCHANGE_ACCOUNT_CREATED] =
g_signal_new ("exchange_account_created",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ExchangeConfigListenerClass, exchange_account_created),
NULL, NULL,
e2k_marshal_NONE__POINTER,
G_TYPE_NONE, 1,
G_TYPE_POINTER);
signals[EXCHANGE_ACCOUNT_REMOVED] =
g_signal_new ("exchange_account_removed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ExchangeConfigListenerClass, exchange_account_removed),
NULL, NULL,
e2k_marshal_NONE__POINTER,
G_TYPE_NONE, 1,
G_TYPE_POINTER);
}
static void
init (GObject *object)
{
ExchangeConfigListener *config_listener =
EXCHANGE_CONFIG_LISTENER (object);
config_listener->priv = g_new0 (ExchangeConfigListenerPrivate, 1);
}
static void
dispose (GObject *object)
{
ExchangeConfigListener *config_listener =
EXCHANGE_CONFIG_LISTENER (object);
if (config_listener->priv->idle_id) {
g_source_remove (config_listener->priv->idle_id);
config_listener->priv->idle_id = 0;
}
if (config_listener->priv->gconf) {
g_object_unref (config_listener->priv->gconf);
config_listener->priv->gconf = NULL;
}
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
ExchangeConfigListener *config_listener =
EXCHANGE_CONFIG_LISTENER (object);
g_free (config_listener->priv->configured_name);
g_free (config_listener->priv->configured_uri);
g_free (config_listener->priv);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
E2K_MAKE_TYPE (exchange_config_listener, ExchangeConfigListener, class_init, init, PARENT_TYPE)
#define EVOLUTION_URI_PREFIX "evolution:/"
#define EVOLUTION_URI_PREFIX_LEN (sizeof (EVOLUTION_URI_PREFIX) - 1)
static EFolder *
standard_folder (ExchangeAccount *account, const char *folder_type)
{
const char *uri;
uri = exchange_account_get_standard_uri (account, folder_type);
if (!uri)
return NULL;
return exchange_account_get_folder (account, uri);
}
static void
set_special_mail_folder (ExchangeAccount *account, const char *folder_type,
char **physical_uri)
{
EFolder *folder;
folder = standard_folder (account, folder_type);
if (!folder)
return;
g_free (*physical_uri);
*physical_uri = g_strdup (e_folder_get_physical_uri (folder));
}
static void
add_defaults_for_account (ExchangeConfigListener *config_listener,
E2kContext *ctx,
ExchangeAccount *account)
{
EAccount *eaccount;
exchange_add_autocompletion_folders (config_listener->priv->gconf, account);
eaccount = config_listener->priv->configured_account;
set_special_mail_folder (account, "drafts",
&eaccount->drafts_folder_uri);
set_special_mail_folder (account, "sentitems",
&eaccount->sent_folder_uri);
e_account_list_change (E_ACCOUNT_LIST (config_listener), eaccount);
e_account_list_save (E_ACCOUNT_LIST (config_listener));
}
static gboolean
is_active_exchange_account (EAccount *account)
{
if (!account->enabled)
return FALSE;
if (!account->source || !account->source->url)
return FALSE;
return (strncmp (account->source->url, EXCHANGE_URI_PREFIX, 11) == 0);
}
#if 0 /* Not using the following code anywhere for the moment */
static void
add_account_esources (ExchangeAccount *account,
GSList *folders)
{
ESource *source = NULL;
ESourceGroup *cal_source_group = NULL;
ESourceGroup *tasks_source_group = NULL;
ESourceGroup *contacts_source_group = NULL;
char *relative_uri = NULL, *username = NULL;
#if 0
GSList *ids;
#endif
GConfClient *client;
int mode;
ESourceList *cal_source_list, *tasks_source_list, *contacts_source_list;
FolderInfo *folder=NULL;
gboolean offline_mode = FALSE;
client = gconf_client_get_default ();
cal_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_CAL);
tasks_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_TASKS);
contacts_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_CONTACTS);
exchange_account_is_offline_sync_set (account, &mode);
if (mode == OFFLINE_MODE) {
/* If account is marked for offline sync during account
* creation, mark all the folders for offline sync
*/
offline_mode = TRUE;
}
username = exchange_account_get_username (account);
/* For each component create a source group */
cal_source_group = e_source_group_new (account->account_name,
EXCHANGE_URI_PREFIX);
tasks_source_group = e_source_group_new (account->account_name,
EXCHANGE_URI_PREFIX);
contacts_source_group = e_source_group_new (account->account_name,
EXCHANGE_URI_PREFIX);
if (!e_source_list_add_group (contacts_source_list, contacts_source_group, -1) ||
!e_source_list_add_group (cal_source_list, cal_source_group, -1) ||
!e_source_list_add_group (tasks_source_list, tasks_source_group, -1)) {
goto done;
}
for ( ; folders != NULL ; folders = g_slist_next (folders)) {
/* Create source for each folder and add to the group */
folder = folders->data;
if (folder->type == EXCHANGE_CONTACTS_FOLDER) {
source = e_source_new_with_absolute_uri (folder->name,
folder->uri);
if (offline_mode)
e_source_set_property (source, "offline_sync", "1");
if (username)
e_source_set_property (source, "username", username);
e_source_set_property (source, "auth", "1");
e_source_set_property (source, "auth-domain", "Exchange");
e_source_group_add_source (contacts_source_group,
source, -1);
g_object_unref (source);
}
else if (folder->type == EXCHANGE_CALENDAR_FOLDER){
relative_uri = g_strdup (folder->uri +
strlen (EXCHANGE_URI_PREFIX));
source = e_source_new (folder->name, relative_uri);
if (offline_mode)
e_source_set_property (source, "offline_sync", "1");
if (username)
e_source_set_property (source, "username", username);
e_source_set_property (source, "auth", "1");
e_source_set_property (source, "auth-domain", "Exchange");
e_source_group_add_source (cal_source_group,
source, -1);
#if 0
ids = gconf_client_get_list (client,
CONF_KEY_SELECTED_CAL_SOURCES,
GCONF_VALUE_STRING, NULL);
ids = g_slist_append (ids,
g_strdup (e_source_peek_uid (source)));
gconf_client_set_list (client,
CONF_KEY_SELECTED_CAL_SOURCES,
GCONF_VALUE_STRING, ids, NULL);
g_slist_foreach (ids, (GFunc) g_free, NULL);
g_slist_free (ids);
#endif
g_object_unref (source);
g_free (relative_uri);
}
else if (folder->type == EXCHANGE_TASKS_FOLDER){
relative_uri = g_strdup (folder->uri +
strlen (EXCHANGE_URI_PREFIX));
source = e_source_new (folder->name, relative_uri);
if (offline_mode == ONLINE_MODE)
e_source_set_property (source, "offline_sync", "1");
if (username)
e_source_set_property (source, "username", username);
e_source_set_property (source, "auth", "1");
e_source_set_property (source, "auth-domain", "Exchange");
e_source_group_add_source (tasks_source_group,
source, -1);
#if 0
ids = gconf_client_get_list (client,
CONF_KEY_SELECTED_TASKS_SOURCES,
GCONF_VALUE_STRING, NULL);
ids = g_slist_append (ids,
g_strdup (e_source_peek_uid (source)));
gconf_client_set_list (client,
CONF_KEY_SELECTED_TASKS_SOURCES,
GCONF_VALUE_STRING, ids, NULL);
g_slist_foreach (ids, (GFunc) g_free, NULL);
g_slist_free (ids);
#endif
g_object_unref (source);
g_free (relative_uri);
}
}
e_source_list_sync (cal_source_list, NULL);
e_source_list_sync (tasks_source_list, NULL);
e_source_list_sync (contacts_source_list, NULL);
done:
g_object_unref (cal_source_group);
g_object_unref (tasks_source_group);
g_object_unref (contacts_source_group);
g_object_unref (cal_source_list);
g_object_unref (tasks_source_list);
g_object_unref (contacts_source_list);
g_object_unref (client);
}
static void
add_new_sources (ExchangeAccount *account)
{
GPtrArray *exchange_folders;
exchange_folders = exchange_account_get_folders (account);
if (exchange_folders && exchange_folders->len > 0) {
int i;
const char *folder_type;
const char *folder_name;
const char *folder_uri;
int type;
EFolder *folder;
ExchangeHierarchy *hier;
gboolean create_esource = FALSE;
for (i = 0; i < exchange_folders->len; i++) {
folder = exchange_folders->pdata[i];
hier = e_folder_exchange_get_hierarchy (folder);
if (hier->type != EXCHANGE_HIERARCHY_PUBLIC) {
folder_name = e_folder_get_name (folder);
folder_uri = e_folder_get_physical_uri (folder);
folder_type = e_folder_get_type_string (folder);
if (!(strcmp (folder_type, "calendar")) ||
!(strcmp (folder_type, "calendar/public"))) {
type = EXCHANGE_CALENDAR_FOLDER;
create_esource = TRUE;
}
else if (!(strcmp (folder_type, "tasks")) ||
!(strcmp (folder_type, "tasks/public"))) {
type = EXCHANGE_TASKS_FOLDER;
create_esource = TRUE;
}
else if (!(strcmp (folder_type, "contacts")) ||
!(strcmp (folder_type, "contacts/public")) ||
!(strcmp (folder_type, "contacts/ldap"))) {
type = EXCHANGE_CONTACTS_FOLDER;
create_esource = TRUE;
}
else {
create_esource = FALSE;
}
if (create_esource)
add_folder_esource (account, type,
folder_name, folder_uri);
} /* End hierarchy type check */
} /* End for loop */
} /* End check for a list of folders */
}
static void
add_sources (ExchangeAccount *account)
{
GPtrArray *exchange_folders;
exchange_folders = exchange_account_get_folders (account);
if (exchange_folders && exchange_folders->len > 0) {
int i;
const char *folder_type;
EFolder *folder;
GSList *folders = NULL;
for (i = 0; i < exchange_folders->len; i++) {
FolderInfo *folder_info = g_new0 (FolderInfo, 1);
folder = exchange_folders->pdata[i];
folder_type = e_folder_get_type_string (folder);
if (!(strcmp (folder_type, "calendar")) ||
!(strcmp (folder_type, "calendar/public"))) {
folder_info->name = e_folder_get_name (folder);
folder_info->uri = e_folder_get_physical_uri (folder);
folder_info->type = EXCHANGE_CALENDAR_FOLDER;
folders = g_slist_append (folders, folder_info);
}
else if (!(strcmp (folder_type, "tasks")) ||
!(strcmp (folder_type, "tasks/public"))) {
folder_info->name = e_folder_get_name (folder);
folder_info->uri = e_folder_get_physical_uri (folder);
folder_info->type = EXCHANGE_TASKS_FOLDER;
folders = g_slist_append (folders, folder_info);
}
else if (!(strcmp (folder_type, "contacts")) ||
!(strcmp (folder_type, "contacts/public")) ||
!(strcmp (folder_type, "contacts/ldap"))) {
folder_info->name = e_folder_get_name (folder);
folder_info->uri = e_folder_get_physical_uri (folder);
folder_info->type = EXCHANGE_CONTACTS_FOLDER;
folders = g_slist_append (folders, folder_info);
}
else
g_free (folder_info);
}
/* Add e-sources for all the folders */
add_account_esources (account, folders);
g_slist_foreach (folders, (GFunc) g_free, NULL);
g_slist_free (folders);
}
}
#endif
static void
remove_account_esource (ExchangeAccount *account,
FolderType folder_type)
{
ESourceGroup *group;
ESource *source = NULL;
GSList *groups;
GSList *sources;
GSList *ids, *node_to_be_deleted;
gboolean found_group;
const char *source_uid;
GConfClient *client;
ESourceList *source_list = NULL;
/* Remove the ESource group, to remove all the folders in a component */
client = gconf_client_get_default ();
if (folder_type == EXCHANGE_CONTACTS_FOLDER)
source_list = e_source_list_new_for_gconf ( client,
CONF_KEY_CONTACTS);
else if (folder_type == EXCHANGE_CALENDAR_FOLDER)
source_list = e_source_list_new_for_gconf ( client,
CONF_KEY_CAL);
else if (folder_type == EXCHANGE_TASKS_FOLDER)
source_list = e_source_list_new_for_gconf ( client,
CONF_KEY_TASKS);
groups = e_source_list_peek_groups (source_list);
found_group = FALSE;
for ( ; groups != NULL && !found_group; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (strcmp (e_source_group_peek_name (group), account->account_name) == 0
&&
strcmp (e_source_group_peek_base_uri (group), EXCHANGE_URI_PREFIX) == 0) {
sources = e_source_group_peek_sources (group);
for( ; sources != NULL; sources = g_slist_next (sources)) {
source = E_SOURCE (sources->data);
source_uid = e_source_peek_uid (source);
/* Remove from the selected folders */
if (folder_type == EXCHANGE_CALENDAR_FOLDER) {
ids = gconf_client_get_list (
client,
CONF_KEY_SELECTED_CAL_SOURCES ,
GCONF_VALUE_STRING, NULL);
if (ids) {
node_to_be_deleted = g_slist_find_custom (
ids,
source_uid,
(GCompareFunc) strcmp);
if (node_to_be_deleted) {
g_free (node_to_be_deleted->data);
ids = g_slist_delete_link (ids,
node_to_be_deleted);
gconf_client_set_list (client,
CONF_KEY_SELECTED_CAL_SOURCES,
GCONF_VALUE_STRING, ids, NULL);
}
g_slist_foreach (ids, (GFunc) g_free, NULL);
g_slist_free (ids);
}
}
else if (folder_type == EXCHANGE_TASKS_FOLDER) {
ids = gconf_client_get_list (client,
CONF_KEY_SELECTED_TASKS_SOURCES ,
GCONF_VALUE_STRING, NULL);
if (ids) {
node_to_be_deleted = g_slist_find_custom (
ids,
source_uid,
(GCompareFunc) strcmp);
if (node_to_be_deleted) {
g_free (node_to_be_deleted->data);
ids = g_slist_delete_link (ids,
node_to_be_deleted);
gconf_client_set_list (client,
CONF_KEY_SELECTED_TASKS_SOURCES,
GCONF_VALUE_STRING, ids, NULL);
}
g_slist_foreach (ids, (GFunc) g_free, NULL);
g_slist_free (ids);
}
}
}
e_source_list_remove_group (source_list, group);
e_source_list_sync (source_list, NULL);
found_group = TRUE;
}
}
g_object_unref (source_list);
g_object_unref (client);
}
static void
remove_account_esources (ExchangeAccount *account)
{
/* Remove ESources for all the folders in all the components */
remove_account_esource (account, EXCHANGE_CALENDAR_FOLDER);
remove_account_esource (account, EXCHANGE_TASKS_FOLDER);
remove_account_esource (account, EXCHANGE_CONTACTS_FOLDER);
}
ExchangeAccountResult
exchange_config_listener_authenticate (ExchangeConfigListener *ex_conf_listener, ExchangeAccount *account)
{
ExchangeConfigListenerPrivate *priv;
ExchangeAccountResult result;
char *key, *password, *title;
gboolean oldremember, remember = FALSE;
CamelURL *camel_url;
const char *remember_password;
g_return_val_if_fail (EXCHANGE_IS_CONFIG_LISTENER (ex_conf_listener), EXCHANGE_ACCOUNT_CONFIG_ERROR);
priv = ex_conf_listener->priv;
camel_url = camel_url_new (priv->configured_uri, NULL);
key = camel_url_to_string (camel_url, CAMEL_URL_HIDE_PASSWORD | CAMEL_URL_HIDE_PARAMS);
remember_password = camel_url_get_param (camel_url, "save-passwd");
password = e_passwords_get_password ("Exchange", key);
if (!password) {
oldremember = remember = exchange_account_is_save_password (account);
title = g_strdup_printf (_("Enter Password for %s"), account->account_name);
password = e_passwords_ask_password (title, "Exchange", key, title,
E_PASSWORDS_REMEMBER_FOREVER|E_PASSWORDS_SECRET,
&remember, NULL);
if (remember != oldremember) {
exchange_account_set_save_password (account, remember);
}
g_free (title);
}
else if (remember_password && !g_strcasecmp (remember_password, "fasle")) {
/* get_password returns the password cached but user has not
* selected remember password option, forget this password
* whis is stored temporarily by e2k_validate_user(), to avoid
* asking for password again, at the end of account creation.
*/
e_passwords_forget_password ("Exchange", key);
}
exchange_account_connect (account, password, &result);
g_free (key);
camel_url_free (camel_url);
return result;
}
static void
account_added (EAccountList *account_list, EAccount *account)
{
ExchangeConfigListener *config_listener;
ExchangeAccount *exchange_account;
gint is_offline;
if (!is_active_exchange_account (account))
return;
config_listener = EXCHANGE_CONFIG_LISTENER (account_list);
if (config_listener->priv->configured_account) {
/* Multiple accounts configured. */
e_error_run (NULL, "org-gnome-exchange-operations:single-account-error", NULL);
return;
}
/* New account! Yippee! */
exchange_account = exchange_account_new (account_list, account);
if (!exchange_account) {
g_warning ("Could not parse exchange uri '%s'",
account->source->url);
return;
}
config_listener->priv->exchange_account = exchange_account;
config_listener->priv->configured_account = account;
g_free (config_listener->priv->configured_uri);
config_listener->priv->configured_uri = g_strdup (account->source->url);
g_free (config_listener->priv->configured_name);
config_listener->priv->configured_name = g_strdup (account->name);
if (account == e_account_list_get_default (account_list)) {
g_signal_connect_swapped (config_listener->priv->exchange_account,
"connected",
G_CALLBACK (add_defaults_for_account),
config_listener);
}
g_signal_emit (config_listener, signals[EXCHANGE_ACCOUNT_CREATED], 0,
exchange_account);
/* add_sources (exchange_account); */
exchange_config_listener_get_offline_status (config_listener, &is_offline);
if (is_offline == OFFLINE_MODE) {
remove_selected_non_offline_esources (exchange_account, CONF_KEY_CAL);
remove_selected_non_offline_esources (exchange_account, CONF_KEY_TASKS);
return;
}
exchange_config_listener_authenticate (config_listener, exchange_account);
exchange_account_set_online (exchange_account);
}
struct account_update_data {
EAccountList *account_list;
EAccount *account;
};
static void
configured_account_destroyed (gpointer user_data, GObject *where_account_was)
{
struct account_update_data *aud = user_data;
if (!EXCHANGE_CONFIG_LISTENER (aud->account_list)->priv->configured_account)
account_added (aud->account_list, aud->account);
g_object_unref (aud->account_list);
g_object_unref (aud->account);
g_free (aud);
}
static gboolean
requires_relogin (char *current_url, char *new_url)
{
E2kUri *current_uri, *new_uri;
const char *current_param_val, *new_param_val;
const char *params [] = { "owa_url", "ad_server", "use_ssl" };
const int n_params = G_N_ELEMENTS (params);
int i;
gboolean relogin = FALSE;
current_uri = e2k_uri_new (current_url);
new_uri = e2k_uri_new (new_url);
if (strcmp (current_uri->user, new_uri->user) ||
strcmp (current_uri->host, new_uri->host)) {
relogin = TRUE;
goto end;
}
if (current_uri->authmech || new_uri->authmech) {
if (current_uri->authmech && new_uri->authmech) {
if (strcmp (current_uri->authmech, new_uri->authmech)) {
/* Auth mechanism has changed */
relogin = TRUE;
goto end;
}
}
else {
/* Auth mechanism is set for the first time */
relogin = TRUE;
goto end;
}
}
for (i=0; i<n_params; i++) {
current_param_val = e2k_uri_get_param (current_uri, params[i]);
new_param_val = e2k_uri_get_param (new_uri, params[i]);
if (current_param_val && new_param_val) {
/* both the urls have params to be compared */
if (strcmp (current_param_val, new_param_val)) {
relogin = TRUE;
break;
}
}
else if (current_param_val || new_param_val){
/* check for added or deleted parameter */
relogin = TRUE;
break;
}
}
end:
e2k_uri_free (new_uri);
e2k_uri_free (current_uri);
return relogin;
}
static void
account_changed (EAccountList *account_list, EAccount *account)
{
ExchangeConfigListener *config_listener =
EXCHANGE_CONFIG_LISTENER (account_list);
ExchangeConfigListenerPrivate *priv = config_listener->priv;
if (account != config_listener->priv->configured_account) {
if (!is_active_exchange_account (account))
return;
/* The user has converted an existing non-Exchange
* account to an Exchange account, so treat it like an
* add.
*/
account_added (account_list, account);
return;
} else if (!is_active_exchange_account (account)) {
/* The user has disabled the Exchange account or
* converted it to non-Exchange, so treat it like a
* remove.
*/
account_removed (account_list, account);
return;
}
/* FIXME: The order of the parameters in the Camel URL string is not in
* order for the two given strings. So, we will not be able to use
* plain string comparison. Instead compare the parameters one by one.
*/
if (exchange_camel_urls_is_equal (config_listener->priv->configured_uri,
account->source->url) &&
!strcmp (config_listener->priv->configured_name, account->name)) {
/* The user changed something we don't care about. */
return;
}
/* OK, so he modified the active account in a way we care
* about. If the user hasn't connected yet, we're still ok.
*/
if (!exchange_account_get_context (config_listener->priv->exchange_account)) {
/* Good. Remove the current account, and wait for it
* to actually go away (which may not happen immediately
* since there may be a function higher up on the stack
* still holding a ref on it). Then create the new one.
* (We have to wait for it to go away because the new
* storage probably still has the same name as the old
* one, so trying to create it before the old one is
* removed would fail.)
*/
struct account_update_data *aud;
aud = g_new (struct account_update_data, 1);
aud->account = g_object_ref (account);
aud->account_list = g_object_ref (account_list);
g_object_weak_ref (G_OBJECT (config_listener->priv->exchange_account), configured_account_destroyed, aud);
account_removed (account_list, account);
return;
}
/* If account name has changed, or the url value has changed, which
* could be due to change in hostname or some parameter value,
* remove old e-sources
*/
if (requires_relogin (config_listener->priv->configured_uri,
account->source->url)) {
remove_account_esources (priv->exchange_account);
exchange_account_forget_password (priv->exchange_account);
} else if (strcmp (config_listener->priv->configured_name, account->name)) {
/* remove_account_esources (priv->exchange_account); */
exchange_config_listener_modify_esource_group_name (config_listener,
config_listener->priv->configured_name,
account->name);
g_free (config_listener->priv->configured_name);
config_listener->priv->configured_name = g_strdup (account->name);
return;
} else {
/* FIXME: Do ESources need to be modified? */
return;
}
/* Nope. Let the user know we're ignoring him. */
e_error_run (NULL, "org-gnome-exchange-operations:apply-restart", NULL);
/* But note the new URI so if he changes something else, we
* only warn again if he changes again.
*/
g_free (config_listener->priv->configured_uri);
config_listener->priv->configured_uri = g_strdup (account->source->url);
}
static void
account_removed (EAccountList *account_list, EAccount *account)
{
ExchangeConfigListener *config_listener =
EXCHANGE_CONFIG_LISTENER (account_list);
ExchangeConfigListenerPrivate *priv = config_listener->priv;
if (account != priv->configured_account)
return;
/* Remove all ESources */
remove_account_esources (priv->exchange_account);
exchange_account_forget_password (priv->exchange_account);
if (!exchange_account_get_context (priv->exchange_account)) {
/* The account isn't connected yet, so we can destroy
* it without problems.
*/
g_signal_emit (config_listener,
signals[EXCHANGE_ACCOUNT_REMOVED], 0,
priv->exchange_account);
g_object_unref (priv->exchange_account);
priv->exchange_account = NULL;
priv->configured_account = NULL;
g_free (priv->configured_uri);
priv->configured_uri = NULL;
g_free (priv->configured_name);
priv->configured_name = NULL;
}
}
static gboolean
idle_construct (gpointer data)
{
ExchangeConfigListener *config_listener = data;
config_listener->priv->idle_id = 0;
e_account_list_construct (E_ACCOUNT_LIST (config_listener),
config_listener->priv->gconf);
return FALSE;
}
ExchangeConfigListenerStatus
exchange_config_listener_get_offline_status (ExchangeConfigListener *excl,
gint *mode)
{
ExchangeConfigListenerPrivate *priv;
GConfValue *value;
ExchangeConfigListenerStatus status = CONFIG_LISTENER_STATUS_OK;
gboolean offline = FALSE;
g_return_val_if_fail (excl != NULL, CONFIG_LISTENER_STATUS_NOT_FOUND);
priv = excl->priv;
value = gconf_client_get (priv->gconf,
"/apps/evolution/shell/start_offline", NULL);
if (value)
offline = gconf_value_get_bool (value);
if (offline)
*mode = OFFLINE_MODE;
else
*mode = ONLINE_MODE;
return status;
}
/**
* exchange_config_listener_new:
*
* This creates and returns a new #ExchangeConfigListener, which
* monitors GConf and creates and (theoretically) destroys accounts
* accordingly. It will emit an %account_created signal when a new
* account is created (or shortly after the listener itself is created
* if an account already exists).
*
* Due to various constraints, the user is currently limited to a
* single account, and it is not possible to destroy an existing
* account. Thus, the %account_created signal will never be emitted
* more than once currently.
*
* Return value: the new config listener.
**/
ExchangeConfigListener *
exchange_config_listener_new (void)
{
ExchangeConfigListener *config_listener;
config_listener = g_object_new (EXCHANGE_TYPE_CONFIG_LISTENER, NULL);
config_listener->priv->gconf = gconf_client_get_default ();
config_listener->priv->idle_id =
g_idle_add (idle_construct, config_listener);
return config_listener;
}
GSList *
exchange_config_listener_get_accounts (ExchangeConfigListener *config_listener)
{
g_return_val_if_fail (EXCHANGE_IS_CONFIG_LISTENER (config_listener), NULL);
if (config_listener->priv->exchange_account)
return g_slist_append (NULL, config_listener->priv->exchange_account);
else
return NULL;
}
/**
* exchange_config_listener_modify_esource_group_name
*
* @excl: Handle for Exchange Config Listener
* @old_name: Old name of the ESourceGroup
* @new_name: New name of the ESourceGroup
*
* This function modifies the old source group name to the specified new
* source group name
**/
void
exchange_config_listener_modify_esource_group_name (ExchangeConfigListener *excl,
const char *old_name,
const char *new_name)
{
GConfClient *client;
ESourceGroup *group;
GSList *groups;
ESourceList *c_source_list = NULL, *t_source_list = NULL,
*a_source_list = NULL;
client = excl->priv->gconf;
c_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_CAL);
t_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_TASKS);
a_source_list = e_source_list_new_for_gconf ( client, CONF_KEY_CONTACTS);
groups = e_source_list_peek_groups (c_source_list);
for ( ; groups != NULL; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (!strcmp (e_source_group_peek_name (group), old_name)) {
e_source_group_set_name (group, new_name);
break;
}
}
groups = e_source_list_peek_groups (t_source_list);
for ( ; groups != NULL; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (!strcmp (e_source_group_peek_name (group), old_name)) {
e_source_group_set_name (group, new_name);
break;
}
}
groups = e_source_list_peek_groups (a_source_list);
for ( ; groups != NULL; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (!strcmp (e_source_group_peek_name (group), old_name)) {
e_source_group_set_name (group, new_name);
break;
}
}
e_source_list_sync (c_source_list, NULL);
e_source_list_sync (t_source_list, NULL);
e_source_list_sync (a_source_list, NULL);
g_object_unref (c_source_list);
g_object_unref (t_source_list);
g_object_unref (a_source_list);
}
/**
* exchange_add_autocompletion_folders:
*
* @gc_client: GConfClient handle
* @account: ExchangeAccount handle
*
* This function adds the GAL of the Exchange account to the autocompletion list
* while configuring a new Exchange account
*
**/
static void
exchange_add_autocompletion_folders (GConfClient *gc_client, ExchangeAccount *account)
{
ESourceList *sl=NULL;
ESourceGroup *group;
ESource *source;
GSList *groups, *sources;
gboolean found_group=FALSE;
sl = e_source_list_new_for_gconf (gc_client, CONF_KEY_CONTACTS);
groups = e_source_list_peek_groups (sl);
for ( ; groups != NULL && !found_group; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (strcmp (e_source_group_peek_name (group), account->account_name) == 0
&&
strcmp (e_source_group_peek_base_uri (group), EXCHANGE_URI_PREFIX) == 0) {
sources = e_source_group_peek_sources (group);
for( ; sources != NULL; sources = g_slist_next (sources)) {
source = E_SOURCE (sources->data);
if (g_str_has_prefix (e_source_peek_absolute_uri (source),
"gal://")) {
/* Set autocompletion on GAL alone by default */
e_source_set_property (source, "completion", "true");
break;
}
}
found_group = TRUE;
}
}
g_object_unref (sl);
}
/**
* exchange_camel_urls_is_equal
*
* @url1: CAMEL URL string 1
* @url2: CAMEL URL string 2
*
* This function checks if the parameters present in two given CAMEL URLS are
* identical and returns the result.
*
* Return Value: Boolean result of the comparision.
*
**/
static gboolean
exchange_camel_urls_is_equal (const gchar *url1, const gchar *url2)
{
CamelURL *curl1, *curl2;
gchar *param1, *param2;
const char *params[] = {
"auth",
"owa_url",
"owa_path",
"mailbox",
"ad_server",
};
const int n_params = 5;
int i;
curl1 = camel_url_new (url1, NULL);
curl2 = camel_url_new (url2, NULL);
for (i = 0; i < n_params; ++i) {
param1 = (gchar*) camel_url_get_param (curl1, params[i]);
param2 = (gchar*) camel_url_get_param (curl2, params[i]);
if ((param1 && !param2) || (!param1 && param2) || /* Missing */
(param1 && param2 && strcmp (param1, param2))) { /* Differing */
g_free (param1);
g_free (param2);
g_free (curl1);
g_free (curl2);
return FALSE;
}
g_free (param1);
g_free (param2);
}
g_free (curl1);
g_free (curl2);
return TRUE;
}
/**
* remove_selected_non_offline_esources
*
* @account: Handle for Exchange Account
* @gconf_key: GConf key of the calendar or tasks
*
* This function removes the non-offline calendars and taks list from the
* selection list
**/
static void
remove_selected_non_offline_esources (ExchangeAccount *account, const char *gconf_key)
{
ESourceGroup *group;
ESource *source = NULL;
GSList *groups;
GSList *sources;
GSList *ids, *node_to_be_deleted;
gboolean found_group;
const char *source_uid;
GConfClient *client;
ESourceList *source_list = NULL;
const char *offline_mode=NULL;
char *selected_gconf_key;
if (gconf_key && !strcmp (gconf_key, CONF_KEY_CAL)) {
selected_gconf_key = g_strdup (CONF_KEY_SELECTED_CAL_SOURCES);
} else if (gconf_key && !strcmp (gconf_key, CONF_KEY_TASKS)) {
selected_gconf_key = g_strdup (CONF_KEY_SELECTED_TASKS_SOURCES);
}
else {
return;
}
client = gconf_client_get_default ();
source_list = e_source_list_new_for_gconf ( client, gconf_key);
groups = e_source_list_peek_groups (source_list);
found_group = FALSE;
for ( ; groups != NULL && !found_group; groups = g_slist_next (groups)) {
group = E_SOURCE_GROUP (groups->data);
if (strcmp (e_source_group_peek_name (group), account->account_name) == 0
&&
strcmp (e_source_group_peek_base_uri (group), EXCHANGE_URI_PREFIX) == 0) {
sources = e_source_group_peek_sources (group);
for( ; sources != NULL; sources = g_slist_next (sources)) {
source = E_SOURCE (sources->data);
source_uid = e_source_peek_uid (source);
/* Remove from the selected folders */
ids = gconf_client_get_list (client,
selected_gconf_key,
GCONF_VALUE_STRING, NULL);
if (ids) {
offline_mode = e_source_get_property (source, "offline_sync");
if (!offline_mode ||
(offline_mode && strcmp (offline_mode, "1"))) {
while ((node_to_be_deleted =
g_slist_find_custom (ids,
source_uid,
(GCompareFunc) strcmp))) {
g_free (node_to_be_deleted->data);
ids = g_slist_delete_link (ids,
node_to_be_deleted);
gconf_client_set_list (client,
selected_gconf_key,
GCONF_VALUE_STRING, ids, NULL);
}
}
g_slist_foreach (ids, (GFunc) g_free, NULL);
g_slist_free (ids);
}
}
found_group = TRUE;
e_source_list_sync (source_list, NULL);
}
}
g_free (selected_gconf_key);
g_object_unref (source_list);
g_object_unref (client);
}
|