aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/exchange-account-setup/exchange-ask-password.c
blob: eadab0438c019a8c1f630960234120d532fe79ae (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Sushma Rai <rsushma@novell.com>
 *  Copyright (C) 2004 Novell, Inc.
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use, copy,
 *  modify, merge, publish, distribute, sublicense, and/or sell copies
 *  of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <libgnome/gnome-i18n.h>
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <gtk/gtkdialog.h>
#include <camel/camel-provider.h>
#include <camel/camel-url.h>
#include "mail/em-account-editor.h"
#include "mail/em-config.h"
#include "e-util/e-account.h"
#include "e-util/e-passwords.h"
#include "e-util/e-config.h"

int e_plugin_lib_enable (EPluginLib *ep, int enable);

GtkWidget *org_gnome_exchange_read_url(EPlugin *epl, EConfigHookItemFactoryData *data);
GtkWidget *org_gnome_exchange_set_url(EPlugin *epl, EConfigHookItemFactoryData *data);

char *owa_entry_text = NULL; 

typedef gboolean (CamelProviderValidateUserFunc) (CamelURL *camel_url, char *url, CamelException *ex);

typedef struct {
        CamelProviderValidateUserFunc *validate_user;
}CamelProviderValidate;


static gboolean
validate_exchange_user (void *data)
{
    EMConfigTargetAccount *target_account = data;
    CamelProviderValidate *validate;
    CamelURL *url=NULL;
    CamelProvider *provider = NULL;
    gboolean valid = TRUE;
    char *account_url, *url_string;
    static int count = 0;

    if (count) 
        return valid;

    account_url = g_strdup (target_account->account->source->url);
    url = camel_url_new_with_base (NULL, account_url);
    provider = camel_provider_get (account_url, NULL);
    g_free (account_url);
    if (!provider) {
        return FALSE;   /* This should never happen */
    }

    validate = provider->priv; 
    if (validate)
        validate->validate_user (url, owa_entry_text, NULL); 
    else
        valid = FALSE; 
    /* FIXME: need to check for return value */
    if (valid) {
        count ++;
        url_string = camel_url_to_string (url, 0);
        target_account->account->source->url = url_string;
    }
    return valid;
}

static void
ok_button_clicked (GtkWidget *button, void *data)
{
    validate_exchange_user (data); // FIXME: return value
}

static void
owa_entry_changed (GtkWidget *entry, void *data) 
{
    GtkWidget *button = data;

    /* FIXME: return owa_entry_text instead of making it global */
    owa_entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
    if (owa_entry_text)
        gtk_widget_set_sensitive (button, TRUE);
}

static GtkWidget *
add_owa_entry (GtkWidget *parent, EConfig *config, EMConfigTargetAccount *target_account)
{
    GtkWidget *section, *owa_entry;
    GtkWidget *hbox, *button, *label;

    section =  gtk_vbox_new (FALSE, 0);
    gtk_widget_show (section);
    gtk_box_pack_start (GTK_BOX (parent), section, FALSE, FALSE, 0);

    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show(hbox);
    gtk_box_pack_start (GTK_BOX (section), hbox, FALSE, FALSE, 0);
    label = gtk_label_new_with_mnemonic(_("_Url:"));
    gtk_widget_show (label);
    owa_entry = gtk_entry_new ();
    gtk_widget_show (owa_entry);
    button = gtk_button_new_from_stock (GTK_STOCK_OK);
    gtk_widget_set_sensitive (button, FALSE);
    gtk_widget_show (button);

    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), owa_entry, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

    g_signal_connect (owa_entry, "changed", G_CALLBACK(owa_entry_changed), button);
    g_signal_connect (button, "clicked", G_CALLBACK(ok_button_clicked), target_account);
    
    return section; /* FIXME: return entry */
}

static GtkWidget *
add_owa_entry_to_editor (GtkWidget *parent, EConfig *config, EMConfigTargetAccount *target_account)
{
    GtkWidget *section, *owa_entry;
    GtkWidget *hbox, *label;

    section =  gtk_vbox_new (FALSE, 0);
    gtk_widget_show (section);
    gtk_box_pack_start (GTK_BOX (parent), section, FALSE, FALSE, 0);

    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show(hbox);
    gtk_box_pack_start (GTK_BOX (section), hbox, FALSE, FALSE, 0);
    label = gtk_label_new_with_mnemonic(_("_Url:"));
    gtk_widget_show (label);
    owa_entry = gtk_entry_new ();
    if (owa_entry_text)
        gtk_entry_set_text (owa_entry, owa_entry_text);
    gtk_widget_show (owa_entry);

    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), owa_entry, TRUE, TRUE, 0);

    g_signal_connect (owa_entry, "changed", G_CALLBACK(owa_entry_changed), NULL); /* FIXME - gconf handling*/
    return section; /* FIXME: return entry */
    /* FIXME: Proper placing of the widget */
}

int
e_plugin_lib_enable (EPluginLib *ep, int enable)
{
    if (enable) {
    }
    return 0;
}

GtkWidget *
org_gnome_exchange_read_url(EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    EConfig *config;
    char *account_url = NULL, *exchange_url = NULL, *temp_url;
    GtkWidget *owa_entry, *parent;
    CamelURL *url=NULL;

    target_account = (EMConfigTargetAccount *)data->config->target;
    account_url = g_strdup (target_account->account->source->url);
    exchange_url = g_strrstr (account_url, "exchange");
    config = data->config;

    if (exchange_url) {
        printf ("org_gnome_exchange_read_url \n");
        if (data->old) {
            return data->old;
        }
    
        /* hack for making page check work when host is not there */
        url = camel_url_new_with_base (NULL, account_url);
        if (url->host == NULL) {
            url->host = g_strdup("localhost"); 
            /* FIXME: extract host name from url and use it*/
            temp_url = camel_url_to_string (url, 0);
            target_account->account->source->url = g_strdup(temp_url);
            g_free (temp_url);
        }

        parent = data->parent;
        owa_entry = add_owa_entry(parent, config, target_account);
        g_free (account_url);
        return owa_entry;
    }
    else {
        g_free (account_url);
        return NULL;
    }
}

GtkWidget *
org_gnome_exchange_set_url(EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    EConfig *config;
    char *account_url = NULL, *exchange_url = NULL;
    GtkWidget *owa_entry = NULL, *parent;

    target_account = (EMConfigTargetAccount *)data->config->target;
    account_url = g_strdup (target_account->account->source->url);
    exchange_url = g_strrstr (account_url, "exchange");
    config = data->config;

    if (exchange_url) {
        if (data->old)
            return data->old;

        parent = data->parent;
        owa_entry = add_owa_entry_to_editor(parent, config, target_account);
    }
    g_free (account_url);
    return owa_entry;
}