aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/exchange-operations/exchange-account-setup.c
blob: 24aa1935be07b38bacfebf9d34aa5e89bf177b80 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Sushma Rai <rsushma@novell.com>
 *  Copyright (C) 2004 Novell, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "libedataserver/e-account.h"
#include "e-util/e-error.h"

#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glib/gi18n.h>
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <gtk/gtkdialog.h>
#include <gconf/gconf-client.h>
#include <camel/camel-provider.h>
#include <camel/camel-url.h>
#include <camel/camel-service.h>
#include <camel/camel-folder.h>
#include <libedataserver/e-xml-hash-utils.h>
#include <e-util/e-dialog-utils.h>
#include <e2k-validate.h>
#include <exchange-oof.h>
#include <exchange-account.h>
#include "exchange-config-listener.h"
#include "exchange-operations.h"
#include "exchange-folder-size-display.h"
#include "mail/em-account-editor.h"
#include "mail/em-config.h"
#include "exchange-delegates.h"
#include "exchange-change-password.h"
#include "exchange-folder-size-display.h"

GtkWidget* org_gnome_exchange_settings(EPlugin *epl, EConfigHookItemFactoryData *data);
GtkWidget *org_gnome_exchange_owa_url(EPlugin *epl, EConfigHookItemFactoryData *data);
gboolean org_gnome_exchange_check_options(EPlugin *epl, EConfigHookPageCheckData *data);
GtkWidget *org_gnome_exchange_auth_section (EPlugin *epl, EConfigHookItemFactoryData *data);
void org_gnome_exchange_commit (EPlugin *epl, EConfigHookItemFactoryData *data);
GtkWidget* org_gnome_exchange_show_folder_size_factory (EPlugin *epl, EConfigHookItemFactoryData *data); 

CamelServiceAuthType camel_exchange_ntlm_authtype = {
        /* i18n: "Secure Password Authentication" is an Outlookism */
        N_("Secure Password"),

        /* i18n: "NTLM" probably doesn't translate */
        N_("This option will connect to the Exchange server using "
           "secure password (NTLM) authentication."),

        "NTLM",
        TRUE
};

CamelServiceAuthType camel_exchange_password_authtype = {
        N_("Plaintext Password"),

        N_("This option will connect to the Exchange server using "
           "standard plaintext password authentication."),

        "Basic",
        TRUE
};


typedef struct {
    gboolean state;
    char *message;
    GtkWidget *text_view;
}OOFData;

static OOFData *oof_data;

static void
update_state (GtkTextBuffer *buffer, gpointer data)
{
    if (gtk_text_buffer_get_modified (buffer)) {
        GtkTextIter start, end;
        if (oof_data->message)
            g_free (oof_data->message);
        gtk_text_buffer_get_bounds (buffer, &start, &end);
        oof_data->message =  gtk_text_buffer_get_text (buffer, &start,
                                   &end, FALSE);
        gtk_text_buffer_set_modified (buffer, FALSE);
    }
}

static void 
toggled_state (GtkToggleButton *button, gpointer data)
{
    gboolean current_oof_state = gtk_toggle_button_get_active (button);

    if (current_oof_state == oof_data->state)
        return; 
    oof_data->state = current_oof_state;
    gtk_widget_set_sensitive (oof_data->text_view, current_oof_state);
}

#ifdef HAVE_KRB5
static void
btn_chpass_clicked (GtkButton *button, gpointer data)
{
    ExchangeAccount *account;
    char *old_password, *new_password;
    ExchangeAccountResult result;

    account = exchange_operations_get_exchange_account ();
                    
    old_password = exchange_account_get_password (account);
    if (!old_password) {
        g_print ("Could not fetch old password\n");
        return;
    }
    new_password = exchange_get_new_password (old_password, TRUE);
    if (!new_password) {
        /* "Cacel" button was hit */
        return;
    }
    //g_print ("Current password is \"%s\"\n", old_password);
    result = exchange_account_set_password (account, old_password, new_password);
    if (result != EXCHANGE_ACCOUNT_CONNECT_SUCCESS)
        exchange_operations_report_error (account, result);
    
    g_free (old_password);
    g_free (new_password);  
}
#endif

static void
btn_dass_clicked (GtkButton *button, gpointer data)
{
    ExchangeAccount *account;
    account = exchange_operations_get_exchange_account ();
    exchange_delegates (account, gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_WINDOW));
}

static void
btn_fsize_clicked (GtkButton *button, gpointer data)
{
    ExchangeAccount *account;
    GtkListStore *model;

    account = exchange_operations_get_exchange_account ();

    model = exchange_account_folder_size_get_model (account);
    if (model)
        exchange_folder_size_display (model, GTK_WIDGET (button));
}

/* only used in editor */
GtkWidget *
org_gnome_exchange_settings(EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    ExchangeAccount *account = NULL;
    CamelURL *url;
    const char *source_url;
    char *message = NULL, *txt = NULL, *oof_message;
    gboolean oof_state = FALSE;

    GtkVBox *vbox_settings;

    /* OOF fields */
    GtkFrame *frm_oof;
    GtkVBox *vbox_oof;
    GtkLabel *lbl_oof_desc;
    GtkTable *tbl_oof_status;
    GtkLabel *lbl_status;
    GtkRadioButton *radio_iof, *radio_oof;
    GtkScrolledWindow *scrwnd_oof;
    GtkTextView *txtview_oof;

    /* Authentication settings */
    GtkFrame *frm_auth;
    GtkVBox *vbox_auth;
    GtkTable *tbl_auth;
#ifdef HAVE_KRB5
    GtkLabel *lbl_chpass;
    GtkButton *btn_chpass;
#endif
    GtkLabel *lbl_dass;
    GtkButton *btn_dass;
    
    /* Miscelleneous setting */
    GtkFrame *frm_misc;
    GtkVBox *vbox_misc;
    GtkTable *tbl_misc;
    GtkLabel *lbl_fsize;
    GtkButton *btn_fsize;

    GtkTextBuffer *buffer;
    GtkTextIter start, end;

    target_account = (EMConfigTargetAccount *)data->config->target;
    source_url = e_account_get_string (target_account->account,  E_ACCOUNT_SOURCE_URL);
    url = camel_url_new(source_url, NULL);
    if (url == NULL
        || strcmp(url->protocol, "exchange") != 0) {
        if (url)
            camel_url_free(url);
        return NULL;
    }

    if (data->old) {
        camel_url_free(url);
        return data->old;
    }
    if (url)
        camel_url_free (url);

    account = exchange_operations_get_exchange_account ();

    oof_data = g_new0 (OOFData, 1);

    oof_data->state = FALSE;
    oof_data->message = NULL;
    oof_data->text_view = NULL;

    /* See if oof info found already */
    
    if (account && !exchange_oof_get (account, &oof_state, &message)) {

        e_error_run (NULL, ERROR_DOMAIN ":state-read-error", NULL);

                return NULL;
        }
    
    if (message && *message)
        oof_data->message = g_strdup (message);
    else
        oof_data->message = NULL;
    oof_data->state = oof_state;

    /* construct page */

    vbox_settings = (GtkVBox*) gtk_object_new (GTK_TYPE_VBOX, "homogeneous", FALSE, "spacing", 6, NULL);
    gtk_container_set_border_width (GTK_CONTAINER (vbox_settings), 12);

    frm_oof = (GtkFrame*) gtk_object_new (GTK_TYPE_FRAME, "label", _("Out Of Office"), NULL);
    gtk_box_pack_start (GTK_BOX (vbox_settings), GTK_WIDGET (frm_oof), FALSE, FALSE, 0);
    
    vbox_oof = (GtkVBox*) gtk_object_new (GTK_TYPE_VBOX, NULL, "homogeneous", FALSE, "spacing", 12, NULL);
    gtk_container_set_border_width (GTK_CONTAINER (vbox_oof), 6);
    gtk_container_add (GTK_CONTAINER (frm_oof), GTK_WIDGET (vbox_oof));
    
    lbl_oof_desc = (GtkLabel*) gtk_object_new (GTK_TYPE_LABEL, "label", _("The message specified below will be automatically sent to \neach person who sends mail to you while you are out of the office."), "justify", GTK_JUSTIFY_LEFT, NULL); 
    gtk_misc_set_alignment (GTK_MISC (lbl_oof_desc), 0, 0.5);
    gtk_box_pack_start (GTK_BOX (vbox_oof), GTK_WIDGET (lbl_oof_desc), FALSE, FALSE, 0);
    
    tbl_oof_status = (GtkTable*) gtk_object_new (GTK_TYPE_TABLE, "n-rows", 2, "n-columns", 2, "homogeneous", FALSE, "row-spacing", 6, "column-spacing", 6, NULL);
    txt = g_strdup_printf ("<b>%s</b>", _("Status:"));
    lbl_status = (GtkLabel*) gtk_object_new (GTK_TYPE_LABEL, "label", txt, "use-markup", TRUE, NULL);
    g_free (txt);
    gtk_misc_set_alignment (GTK_MISC (lbl_status), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (lbl_status), 0, 0);
            
    if (oof_data->state) {
        radio_oof = (GtkRadioButton*) gtk_object_new (GTK_TYPE_RADIO_BUTTON, "label", _("I am out of the office"), NULL);
        radio_iof = (GtkRadioButton*) gtk_object_new (GTK_TYPE_RADIO_BUTTON, "label", _("I am in the office"), "group", radio_oof, NULL);
    }
    else {
        radio_iof = (GtkRadioButton*) gtk_object_new (GTK_TYPE_RADIO_BUTTON, "label", _("I am in the office"), NULL);
        radio_oof = (GtkRadioButton*) gtk_object_new (GTK_TYPE_RADIO_BUTTON, "label", _("I am out of the office"), "group", radio_iof, NULL);
    }
    g_signal_connect (radio_oof, "toggled", G_CALLBACK (toggled_state), NULL);
    

    gtk_table_attach (tbl_oof_status, GTK_WIDGET (lbl_status), 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
    gtk_table_attach (tbl_oof_status, GTK_WIDGET (radio_iof), 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
    gtk_table_attach (tbl_oof_status, GTK_WIDGET (radio_oof), 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);

    gtk_box_pack_start (GTK_BOX (vbox_oof), GTK_WIDGET (tbl_oof_status), FALSE, FALSE, 0);
    
    
    scrwnd_oof = (GtkScrolledWindow*) gtk_object_new (GTK_TYPE_SCROLLED_WINDOW, "hscrollbar-policy", GTK_POLICY_AUTOMATIC, "vscrollbar-policy", GTK_POLICY_AUTOMATIC, "shadow-type", GTK_SHADOW_IN, NULL);
    gtk_box_pack_start (GTK_BOX (vbox_oof), GTK_WIDGET (scrwnd_oof), FALSE, FALSE, 0);
    txtview_oof = (GtkTextView*) gtk_object_new (GTK_TYPE_TEXT_VIEW, "justification", GTK_JUSTIFY_LEFT, "wrap-mode", GTK_WRAP_WORD, "editable", TRUE, NULL);
    buffer = gtk_text_view_get_buffer (txtview_oof);
    gtk_text_buffer_get_bounds (buffer, &start, &end);
    oof_message = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
    if (oof_message && *oof_message) {
        /* Will this ever happen? */
        oof_data->message = oof_message;
    }
    if (oof_data->message) {
        /* previuosly set message */
        gtk_text_buffer_set_text (buffer, oof_data->message, -1);
        gtk_text_view_set_buffer (txtview_oof, buffer);
        
    }
    gtk_text_buffer_set_modified (buffer, FALSE);
    if (!oof_data->state)
        gtk_widget_set_sensitive (GTK_WIDGET (txtview_oof), FALSE);
    oof_data->text_view = GTK_WIDGET (txtview_oof);
    g_signal_connect (buffer, "changed", G_CALLBACK (update_state), NULL);
    gtk_container_add (GTK_CONTAINER (scrwnd_oof), GTK_WIDGET (txtview_oof));

    /* Security settings */
    frm_auth = (GtkFrame*) gtk_object_new (GTK_TYPE_FRAME, "label", _("Security"), NULL);
    gtk_box_pack_start (GTK_BOX (vbox_settings), GTK_WIDGET (frm_auth), FALSE, FALSE, 0);
    
    vbox_auth = (GtkVBox*) gtk_object_new (GTK_TYPE_VBOX, "homogeneous", FALSE, "spacing", 6, NULL);
    gtk_container_set_border_width (GTK_CONTAINER (vbox_auth), 6);
    gtk_container_add (GTK_CONTAINER (frm_auth), GTK_WIDGET (vbox_auth));

    tbl_auth = (GtkTable*) gtk_object_new (GTK_TYPE_TABLE, "n-rows", 2, "n-columns", 2, "homogeneous", FALSE, "row-spacing", 6, "column-spacing", 6, NULL);

#ifdef HAVE_KRB5
    /* Change Password */
    lbl_chpass = (GtkLabel*) gtk_object_new (GTK_TYPE_LABEL, "label", _("Change the password for Exchange account"), NULL);
    gtk_misc_set_alignment (GTK_MISC (lbl_chpass), 0, 0.5);
    btn_chpass = (GtkButton*) gtk_object_new (GTK_TYPE_BUTTON, "label", _("Change Password"), NULL);
    gtk_signal_connect (GTK_OBJECT (btn_chpass), "clicked", G_CALLBACK (btn_chpass_clicked), NULL);
#endif

    /* Delegation Assistant */
    lbl_dass = (GtkLabel*) gtk_object_new (GTK_TYPE_LABEL, "label", _("Manage the delegate settings for Exchange account"), NULL);
    gtk_misc_set_alignment (GTK_MISC (lbl_dass), 0, 0.5);
    btn_dass = (GtkButton*) gtk_object_new (GTK_TYPE_BUTTON, "label", _("Delegation Assistant"), NULL);
    g_signal_connect (btn_dass, "clicked", G_CALLBACK (btn_dass_clicked), NULL);
    /* Add items to the table */
#ifdef HAVE_KRB5
    gtk_table_attach_defaults (tbl_auth, GTK_WIDGET (lbl_chpass), 0, 1, 0, 1);
    gtk_table_attach (tbl_auth, GTK_WIDGET (btn_chpass), 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
#endif
    gtk_table_attach_defaults (tbl_auth, GTK_WIDGET (lbl_dass), 0, 1, 1, 2);
    gtk_table_attach (tbl_auth, GTK_WIDGET (btn_dass), 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
    gtk_box_pack_start (GTK_BOX (vbox_auth), GTK_WIDGET (tbl_auth), FALSE, FALSE, 0);

    /* Miscelleneous settings */
    frm_misc = (GtkFrame*) gtk_object_new (GTK_TYPE_FRAME, "label", _("Miscelleneous"), NULL);
    gtk_box_pack_start (GTK_BOX (vbox_settings), GTK_WIDGET (frm_misc), FALSE, FALSE, 0);
    
    vbox_misc = (GtkVBox*) gtk_object_new (GTK_TYPE_VBOX, "homogeneous", FALSE, "spacing", 6, NULL);
    gtk_container_set_border_width (GTK_CONTAINER (vbox_misc), 6);
    gtk_container_add (GTK_CONTAINER (frm_misc), GTK_WIDGET (vbox_misc));

    tbl_misc = (GtkTable*) gtk_object_new (GTK_TYPE_TABLE, "n-rows", 1, "n-columns", 1, "homogeneous", FALSE, "row-spacing", 6, "column-spacing", 6, NULL);

    /* Folder Size */
    lbl_fsize = (GtkLabel*) gtk_object_new (GTK_TYPE_LABEL, "label", _("View the size of all Exchange folders"), NULL);
    gtk_misc_set_alignment (GTK_MISC (lbl_fsize), 0, 0.5);
    btn_fsize = (GtkButton*) gtk_object_new (GTK_TYPE_BUTTON, "label", _("Folders Size"), NULL);
    g_signal_connect (btn_fsize, "clicked", G_CALLBACK (btn_fsize_clicked), NULL);
    gtk_table_attach_defaults (tbl_misc, GTK_WIDGET (lbl_fsize), 0, 1, 0, 1);
    gtk_table_attach (tbl_misc, GTK_WIDGET (btn_fsize), 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
    gtk_box_pack_start (GTK_BOX (vbox_misc), GTK_WIDGET (tbl_misc), FALSE, FALSE, 0);

    gtk_widget_show_all (GTK_WIDGET (vbox_settings));
    gtk_notebook_insert_page (GTK_NOTEBOOK (data->parent), GTK_WIDGET (vbox_settings), gtk_label_new(_("Exchange Settings")), 4);
    return GTK_WIDGET (vbox_settings);

}

static void
print_error (const char *owa_url, E2kAutoconfigResult result)
{
    switch (result) {

        case E2K_AUTOCONFIG_CANT_CONNECT:
            e_error_run (NULL, ERROR_DOMAIN ":account-connect-error", "", NULL);
            break;

        case E2K_AUTOCONFIG_CANT_RESOLVE:
            e_error_run (NULL, ERROR_DOMAIN ":account-resolve-error", "", NULL);
            break;

        case E2K_AUTOCONFIG_AUTH_ERROR:
        case E2K_AUTOCONFIG_AUTH_ERROR_TRY_NTLM:
        case E2K_AUTOCONFIG_AUTH_ERROR_TRY_BASIC:
            e_error_run (NULL, ERROR_DOMAIN ":password-incorrect", NULL);
            break;

        case E2K_AUTOCONFIG_AUTH_ERROR_TRY_DOMAIN:
            e_error_run (NULL, ERROR_DOMAIN ":account-domain-error", NULL);
            break;

        case E2K_AUTOCONFIG_NO_OWA:
        case E2K_AUTOCONFIG_NOT_EXCHANGE:
            e_error_run (NULL, ERROR_DOMAIN ":account-wss-error", NULL);
            break;

        case E2K_AUTOCONFIG_CANT_BPROPFIND:
            e_error_run (NULL, ERROR_DOMAIN ":connect-exchange-error", 
                     "http://support.novell.com/cgi-bin/search/searchtid.cgi?/ximian/ximian328.html",
                     NULL);
            break;

        case E2K_AUTOCONFIG_EXCHANGE_5_5:
            e_error_run (NULL, ERROR_DOMAIN ":account-version-error", NULL);
            break;

        default:
            e_error_run (NULL, ERROR_DOMAIN ":configure-error", NULL);
            break;

    }
}

static void
owa_authenticate_user(GtkWidget *button, EConfig *config)
{
    EMConfigTargetAccount *target_account = (EMConfigTargetAccount *)config->target;
    E2kAutoconfigResult result;
    CamelURL *url=NULL;
    gboolean remember_password;
    char *url_string, *key; 
    const char *source_url, *id_name, *owa_url;
    char *at, *user;
    gboolean valid = FALSE;
    ExchangeParams *exchange_params;

    exchange_params = g_new0 (ExchangeParams, 1);
    exchange_params->host = NULL;
    exchange_params->ad_server = NULL;
    exchange_params->mailbox = NULL;
    exchange_params->owa_path = NULL;
    exchange_params->is_ntlm = TRUE;

    source_url = e_account_get_string (target_account->account, E_ACCOUNT_SOURCE_URL);

    url = camel_url_new(source_url, NULL);
    if (url->user == NULL) {
        id_name = e_account_get_string (target_account->account, E_ACCOUNT_ID_ADDRESS);
        if (id_name) {
            at = strchr(id_name, '@');
            user = g_alloca(at-id_name+1);
            memcpy(user, id_name, at-id_name);
            user[at-id_name] = 0; 
            camel_url_set_user (url, user);
        }
    }

    /* validate_user() CALLS GTK!!!

       THIS IS TOTALLY UNNACCEPTABLE!!!!!!!!

       It must use camel_session_ask_password, and it should return an exception for any problem,
       which should then be shown using e-error */

    owa_url = camel_url_get_param (url, "owa_url");
    if (camel_url_get_param (url, "authmech"))
        exchange_params->is_ntlm = TRUE;
    else
        exchange_params->is_ntlm = FALSE;
    camel_url_set_authmech (url, exchange_params->is_ntlm ? "NTLM" : "Basic");

    key = camel_url_to_string (url, CAMEL_URL_HIDE_PASSWORD | CAMEL_URL_HIDE_PARAMS);
    /* Supress the trailing slash */
    key [strlen(key) -1] = 0;
    
    valid =  e2k_validate_user (owa_url, key, &url->user, exchange_params, 
                        &remember_password, &result,
                        GTK_WINDOW (gtk_widget_get_toplevel (button)));
    g_free (key);
    
    if (!valid && result != E2K_AUTOCONFIG_CANCELLED)
        print_error (owa_url, result);

    camel_url_set_host (url, valid ? exchange_params->host : "");
    

    if (valid)
        camel_url_set_param (url, "save-passwd", remember_password? "true" : "false");
    
    camel_url_set_param (url, "ad_server", valid ? exchange_params->ad_server: NULL);
    camel_url_set_param (url, "mailbox", valid ? exchange_params->mailbox : NULL);
    camel_url_set_param (url, "owa_path", valid ? exchange_params->owa_path : NULL);

    g_free (exchange_params->owa_path);
    g_free (exchange_params->mailbox);
    g_free (exchange_params->host);
    g_free (exchange_params->ad_server);
    g_free (exchange_params);

    if (valid) {    
        url_string = camel_url_to_string (url, 0);
        e_account_set_string (target_account->account, E_ACCOUNT_SOURCE_URL, url_string);
        e_account_set_string (target_account->account, E_ACCOUNT_TRANSPORT_URL, url_string);
        e_account_set_bool (target_account->account, E_ACCOUNT_SOURCE_SAVE_PASSWD, remember_password);
        g_free (url_string);
    }
    camel_url_free (url);
}

static void
owa_editor_entry_changed(GtkWidget *entry, EConfig *config)
{
    const char *uri, *ssl = NULL;
    CamelURL *url, *owaurl = NULL;
    char *url_string;
    EMConfigTargetAccount *target = (EMConfigTargetAccount *)config->target;
    GtkWidget *button = g_object_get_data((GObject *)entry, "authenticate-button");
    int active = FALSE;

    /* NB: we set the button active only if we have a parsable uri entered */

    url = camel_url_new(e_account_get_string(target->account, E_ACCOUNT_SOURCE_URL), NULL);
    uri = gtk_entry_get_text((GtkEntry *)entry);
    if (uri && uri[0]) {
        camel_url_set_param(url, "owa_url", uri);
        owaurl = camel_url_new(uri, NULL);
        if (owaurl) {
            active = TRUE;

            /* Reading the owa url and setting use_ssl paramemter */
            if (!strcmp(owaurl->protocol, "https"))
                ssl = "always";
            camel_url_free(owaurl);
        }
    } else {
        camel_url_set_param(url, "owa_url", NULL);
    }

    camel_url_set_param(url, "use_ssl", ssl);
    gtk_widget_set_sensitive(button, active);

    url_string = camel_url_to_string(url, 0);
    e_account_set_string(target->account, E_ACCOUNT_SOURCE_URL, url_string);
    g_free(url_string);
    camel_url_free (url);
}

static char *
construct_owa_url (CamelURL *url) 
{
    const char *owa_path, *use_ssl = NULL;
    const char *protocol = "http", *mailbox_name;
    char *owa_url;

    use_ssl = camel_url_get_param (url, "use_ssl");
    if (use_ssl) {
        if (!strcmp (use_ssl, "always"))
            protocol = "https";
    }

    owa_path = camel_url_get_param (url, "owa_path");
    if (!owa_path)
        owa_path = "/exchange";
    mailbox_name = camel_url_get_param (url, "mailbox");

    if (mailbox_name)
        owa_url = g_strdup_printf("%s://%s%s/%s", protocol, url->host, owa_path, mailbox_name); 
    else
        owa_url = g_strdup_printf("%s://%s%s", protocol, url->host, owa_path ); 

    return owa_url;
}

/* used by editor and druid - same code */
GtkWidget *
org_gnome_exchange_owa_url(EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    const char *source_url;
    char *owa_url = NULL;
    GtkWidget *owa_entry;
    CamelURL *url;
    int row;
    GtkWidget *hbox, *label, *button;

    target_account = (EMConfigTargetAccount *)data->config->target;
    source_url = e_account_get_string (target_account->account,  E_ACCOUNT_SOURCE_URL);
    url = camel_url_new(source_url, NULL);
    if (url == NULL
        || strcmp(url->protocol, "exchange") != 0) {
        if (url)
            camel_url_free(url);

        if (data->old
            && (label = g_object_get_data((GObject *)data->old, "authenticate-label")))
            gtk_widget_destroy(label);

        /* TODO: we could remove 'owa-url' from the url,
           but that will lose it if we come back.  Maybe a commit callback could do it */

        return NULL;
    }

    if (data->old) {
        camel_url_free(url);
        return data->old;
    }

    owa_url = g_strdup (camel_url_get_param(url, "owa_url"));

    /* if the host is null, then user+other info is dropped silently, force it to be kept */
    if (url->host == NULL) {
        char *uri;

        camel_url_set_host(url, "");
        uri = camel_url_to_string(url, 0);
        e_account_set_string(target_account->account,  E_ACCOUNT_SOURCE_URL, uri);
        g_free(uri);
    }

    row = ((GtkTable *)data->parent)->nrows;

    hbox = gtk_hbox_new (FALSE, 6);
    label = gtk_label_new_with_mnemonic(_("_OWA URL:"));
    gtk_widget_show(label);

    owa_entry = gtk_entry_new();

    if (!owa_url) {
        if (url->host[0] != 0) {
            char *uri;

            /* url has hostname but not owa_url. 
             * Account has been created using x-c-s or evo is upgraded to 2.2
             * When invoked from druid, hostname will get set after validation,
             * so this condition will never be true during account creation.
             */
            owa_url = construct_owa_url (url);
            camel_url_set_param (url, "owa_url", owa_url);
            uri = camel_url_to_string(url, 0);
            e_account_set_string(target_account->account,  E_ACCOUNT_SOURCE_URL, uri);
            g_free(uri);
        }
    }
    camel_url_free (url);   
    if (owa_url)
        gtk_entry_set_text(GTK_ENTRY (owa_entry), owa_url); 
    gtk_label_set_mnemonic_widget((GtkLabel *)label, owa_entry);

    button = gtk_button_new_with_mnemonic (_("A_uthenticate"));
    gtk_widget_set_sensitive (button, owa_url && owa_url[0]);

    gtk_box_pack_start (GTK_BOX (hbox), owa_entry, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
    gtk_widget_show_all(hbox);

    gtk_table_attach (GTK_TABLE (data->parent), label, 0, 1, row, row+1, 0, 0, 0, 0); 
    gtk_table_attach (GTK_TABLE (data->parent), hbox, 1, 2, row, row+1, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0); 

    g_signal_connect (owa_entry, "changed", G_CALLBACK(owa_editor_entry_changed), data->config);
    g_object_set_data((GObject *)owa_entry, "authenticate-button", button);
    g_signal_connect (button, "clicked", G_CALLBACK(owa_authenticate_user), data->config);

    /* Track the authenticate label, so we can destroy it if e-config is to destroy the hbox */
    g_object_set_data((GObject *)hbox, "authenticate-label", label);

    g_free (owa_url);
    return hbox;
}

gboolean
org_gnome_exchange_check_options(EPlugin *epl, EConfigHookPageCheckData *data)
{
    EMConfigTargetAccount *target = (EMConfigTargetAccount *)data->config->target;
    int status = TRUE;

    /* We assume that if the host is set, then the setting is valid.
       The host gets set when the provider validate() call is made */
    /* We do this check for receive page also, so that user can
     * proceed with the account set up only after user is validated,
     * and host name is reset by validate() call
     */
    if (data->pageid == NULL ||
        strcmp (data->pageid, "10.receive") == 0 ||
        strcmp (data->pageid, "20.receive_options") == 0) {
        CamelURL *url;

        url = camel_url_new(e_account_get_string(target->account,  E_ACCOUNT_SOURCE_URL), NULL);
        /* Note: we only care about exchange url's, we WILL get called on all other url's too. */
        if (url != NULL
            && strcmp(url->protocol, "exchange") == 0
            && (url->host == NULL || url->host[0] == 0))
            status = FALSE;

        if (url)
            camel_url_free(url);
    }

    return status;
}

static void 
set_oof_info (void)
{
    ExchangeAccount *account;

    account = exchange_operations_get_exchange_account ();

    if (account && !exchange_oof_set (account, oof_data->state, oof_data->message)) {

        e_error_run (NULL, ERROR_DOMAIN ":state-update-error", NULL);
    }
}

static void
destroy_oof_data (void)
{
    if (oof_data->message)
        g_free (oof_data->message);
    g_free (oof_data);  
}

void
org_gnome_exchange_commit (EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    const char *source_url;
    CamelURL *url;
    
    target_account = (EMConfigTargetAccount *)data->config->target;
    source_url = e_account_get_string (target_account->account,  E_ACCOUNT_SOURCE_URL);
    url = camel_url_new (source_url, NULL);
    if (url == NULL
        || strcmp (url->protocol, "exchange") != 0) {
        if (url)
            camel_url_free (url);

        return;
    }
    if (data->old) {
        camel_url_free(url);
        return;
    }

    camel_url_free (url);
    /* Set oof data in exchange account */
    set_oof_info ();
    destroy_oof_data ();
    return;
}

static void
exchange_check_authtype (GtkWidget *w, EConfig *config)
{
    return;
}

static void 
exchange_authtype_changed (GtkComboBox *dropdown, EConfig *config)
{
    EMConfigTargetAccount *target = (EMConfigTargetAccount *)config->target;
    int id = gtk_combo_box_get_active(dropdown);
    GtkTreeModel *model;
    GtkTreeIter iter;
    CamelServiceAuthType *authtype;
    CamelURL *url_source, *url_transport;
    const char *source_url, *transport_url;
    char *source_url_string, *transport_url_string;

    source_url = e_account_get_string (target->account,
                       E_ACCOUNT_SOURCE_URL);
    if (id == -1)
        return;

    url_source = camel_url_new (source_url, NULL);

    transport_url = e_account_get_string (target->account,
                          E_ACCOUNT_TRANSPORT_URL);
    url_transport = camel_url_new (transport_url, NULL);
    
    model = gtk_combo_box_get_model(dropdown);
    if (gtk_tree_model_iter_nth_child(model, &iter, NULL, id)) {
        gtk_tree_model_get(model, &iter, 1, &authtype, -1);
        if (authtype) {
            camel_url_set_authmech(url_source, authtype->authproto);
            camel_url_set_authmech(url_transport, authtype->authproto);
        }
        else {
            camel_url_set_authmech(url_source, NULL);
            camel_url_set_authmech(url_transport, NULL);
        }
    
        source_url_string = camel_url_to_string(url_source, 0);
        transport_url_string = camel_url_to_string(url_transport, 0);
        e_account_set_string(target->account, E_ACCOUNT_SOURCE_URL, source_url_string);
        e_account_set_string(target->account, E_ACCOUNT_TRANSPORT_URL, transport_url_string);
        g_free(source_url_string);
        g_free(transport_url_string);
    }
    camel_url_free(url_source);
    camel_url_free(url_transport);
}


GtkWidget *
org_gnome_exchange_auth_section (EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    const char *source_url; 
    char *label_text, *exchange_account_authtype = NULL;
    CamelURL *url;
    GtkWidget *hbox, *button, *auth_label, *vbox, *label_hide;
    GtkComboBox *dropdown;
    GtkTreeIter iter;
    GtkListStore *store;
    int i, active=0, auth_changed_id = 0;
    GList *authtypes, *l, *ll;
    ExchangeAccount *account;

    target_account = (EMConfigTargetAccount *)data->config->target;
    source_url = e_account_get_string (target_account->account, 
                       E_ACCOUNT_SOURCE_URL);
    url = camel_url_new (source_url, NULL);
    if (url == NULL
        || strcmp (url->protocol, "exchange") != 0) {
        if (url)
            camel_url_free (url);

        return NULL;
    }

    if (data->old) {
        camel_url_free(url);
        return data->old;
    }

    account = exchange_operations_get_exchange_account ();
    if (account)
        exchange_account_authtype = exchange_account_get_authtype (account);

    vbox = gtk_vbox_new (FALSE, 6);

    label_text = g_strdup_printf("<b>%s</b>", _("Authentication Type"));
    auth_label = gtk_label_new (label_text);
    g_free (label_text);
    gtk_label_set_justify (GTK_LABEL (auth_label), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (auth_label), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (auth_label), 0, 0); 
    gtk_label_set_use_markup (GTK_LABEL (auth_label), TRUE);

    label_hide = gtk_label_new("\n");

    hbox = gtk_hbox_new (FALSE, 6);

    dropdown = (GtkComboBox * )gtk_combo_box_new ();

    button = gtk_button_new_with_mnemonic (_("Ch_eck for Supported Types"));

    authtypes = g_list_prepend (g_list_prepend (NULL, &camel_exchange_password_authtype),
                    &camel_exchange_ntlm_authtype);
    store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);

    for (i=0, l=authtypes; l; l=l->next, i++) {
        CamelServiceAuthType *authtype = l->data;
        int avail = TRUE;

        if (authtypes) {
            for (ll = authtypes; ll; ll = g_list_next(ll))
                if (!strcmp(authtype->authproto, 
                    ((CamelServiceAuthType *)ll->data)->authproto))
                    break;
            avail = ll != NULL;
        }
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, authtype->name, 1, 
                    authtype, 2, !avail, -1);

        if (url && url->authmech && !strcmp(url->authmech, authtype->authproto)) {
            active = i;
        }
        else if (url && exchange_account_authtype && 
             !strcmp (exchange_account_authtype, authtype->authproto)) {
            /* if the url doesn't contain authmech, read the value from 
             * exchange account and set the tab selection and 
             * also set the authmech back to url 
             */
            camel_url_set_authmech (url, exchange_account_authtype);
            active = i;
        }
    }

    gtk_combo_box_set_model (dropdown, (GtkTreeModel *)store);
    gtk_combo_box_set_active (dropdown, -1);

    if (auth_changed_id == 0) {
        GtkCellRenderer *cell = gtk_cell_renderer_text_new();

        gtk_cell_layout_pack_start ((GtkCellLayout *)dropdown, cell, TRUE);
        gtk_cell_layout_set_attributes ((GtkCellLayout *)dropdown, cell, 
                        "text", 0, "strikethrough", 2, NULL);

        auth_changed_id = g_signal_connect (dropdown, 
                            "changed", 
                            G_CALLBACK (exchange_authtype_changed), 
                            data->config);
        g_signal_connect (button, 
                  "clicked", 
                  G_CALLBACK(exchange_check_authtype), 
                  data->config);
    }

    gtk_combo_box_set_active(dropdown, active);

    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (dropdown), FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (vbox), auth_label, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (vbox), label_hide, TRUE, TRUE, 0);
    gtk_widget_show_all (vbox);

    gtk_box_pack_start (GTK_BOX (data->parent), vbox, TRUE, TRUE, 0);   

    if (url)
        camel_url_free(url);
    g_list_free (authtypes);
    g_free (exchange_account_authtype);

    return vbox;
}

GtkWidget *
org_gnome_exchange_show_folder_size_factory (EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetFolder *target=  (EMConfigTargetFolder *)data->config->target;
    CamelFolder *cml_folder = target->folder;
    CamelService *service;
    CamelProvider *provider;
    ExchangeAccount *account;
    GtkWidget *lbl_size, *lbl_size_val;
    GtkListStore *model;
    GtkVBox *vbx;
    GtkHBox *hbx_size;
    char *folder_name, *folder_size;

    service = CAMEL_SERVICE (camel_folder_get_parent_store (cml_folder));
    if (!service)
        return NULL;

    provider = camel_service_get_provider (service);
    if (!provider)
        return NULL;

    if (g_ascii_strcasecmp (provider->protocol, "exchange"))
        return NULL;
    
    folder_name = (char*) camel_folder_get_name (cml_folder);
    if (!folder_name)
        folder_name = g_strdup ("name");
    account = exchange_operations_get_exchange_account ();
    model = exchange_account_folder_size_get_model (account);
    if (model)
        folder_size = g_strdup_printf (_("%s KB"), exchange_folder_size_get_val (model, folder_name));
    else
        folder_size = g_strdup (_("0 KB"));

    hbx_size = (GtkHBox*) gtk_hbox_new (FALSE, 0);
    vbx = (GtkVBox *)gtk_notebook_get_nth_page (GTK_NOTEBOOK (data->parent), 0);

    lbl_size = gtk_label_new_with_mnemonic (_("Size:"));
    lbl_size_val = gtk_label_new_with_mnemonic (_(folder_size));
    gtk_widget_show (lbl_size);
    gtk_widget_show (lbl_size_val);
    gtk_misc_set_alignment (GTK_MISC (lbl_size), 0.0, 0.5);
    gtk_misc_set_alignment (GTK_MISC (lbl_size_val), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (hbx_size), lbl_size, FALSE, TRUE, 12);
    gtk_box_pack_start (GTK_BOX (hbx_size), lbl_size_val, FALSE, TRUE, 10);
    gtk_widget_show_all (GTK_WIDGET (hbx_size));

    gtk_box_pack_start (GTK_BOX (vbx), GTK_WIDGET (hbx_size), FALSE, FALSE, 0);
    g_free (folder_size);

    return GTK_WIDGET (hbx_size);
}