aboutsummaryrefslogblamecommitdiffstats
path: root/mail/e-mail-config-auth-check.c
blob: 21e6633b76c847a71647b054783d39f4f355a752 (plain) (tree)
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























































































































































































































































































































































































































































                                                                                


                                                                                  





                                                                         
/*
 * e-mail-config-auth-check.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#include "e-mail-config-auth-check.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#include <libevolution-utils/e-alert.h>
#include <e-util/e-mktemp.h>
#include <misc/e-auth-combo-box.h>
#include <mail/e-mail-config-service-page.h>

#define E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_CONFIG_AUTH_CHECK, EMailConfigAuthCheckPrivate))

typedef struct _AsyncContext AsyncContext;

struct _EMailConfigAuthCheckPrivate {
    EMailConfigServiceBackend *backend;
    gchar *active_mechanism;

    GtkWidget *combo_box;  /* not referenced */
};

struct _AsyncContext {
    EMailConfigAuthCheck *auth_check;
    CamelSession *temporary_session;
    EActivity *activity;
};

enum {
    PROP_0,
    PROP_ACTIVE_MECHANISM,
    PROP_BACKEND
};

G_DEFINE_TYPE (
    EMailConfigAuthCheck,
    e_mail_config_auth_check,
    GTK_TYPE_BOX)

static void
async_context_free (AsyncContext *async_context)
{
    if (async_context->auth_check != NULL)
        g_object_unref (async_context->auth_check);

    if (async_context->temporary_session != NULL)
        g_object_unref (async_context->temporary_session);

    if (async_context->activity != NULL)
        g_object_unref (async_context->activity);

    g_slice_free (AsyncContext, async_context);
}

static void
mail_config_auth_check_update_done_cb (GObject *source_object,
                                       GAsyncResult *result,
                                       gpointer user_data)
{
    AsyncContext *async_context = user_data;
    EMailConfigAuthCheck *auth_check;
    EAlertSink *alert_sink;
    GList *available_authtypes;
    GError *error = NULL;

    auth_check = async_context->auth_check;
    alert_sink = e_activity_get_alert_sink (async_context->activity);

    available_authtypes = camel_service_query_auth_types_finish (
        CAMEL_SERVICE (source_object), result, &error);

    if (e_activity_handle_cancellation (async_context->activity, error)) {
        g_warn_if_fail (available_authtypes == NULL);
        g_error_free (error);

    } else if (error != NULL) {
        g_warn_if_fail (available_authtypes == NULL);
        e_alert_submit (
            alert_sink,
            "mail:checking-service-error",
            error->message, NULL);
        g_error_free (error);

    } else {
        e_auth_combo_box_update_available (
            E_AUTH_COMBO_BOX (auth_check->priv->combo_box),
            available_authtypes);
        g_list_free (available_authtypes);
    }

    gtk_widget_set_sensitive (GTK_WIDGET (auth_check), TRUE);

    async_context_free (async_context);
}

static void
mail_config_auth_check_update (EMailConfigAuthCheck *auth_check)
{
    EActivity *activity;
    EMailConfigServicePage *page;
    EMailConfigServiceBackend *backend;
    EMailConfigServicePageClass *page_class;
    EMailConfigServiceBackendClass *backend_class;
    CamelService *service;
    CamelSession *session;
    CamelSettings *settings;
    GCancellable *cancellable;
    AsyncContext *async_context;
    gchar *temp_dir;
    GError *error = NULL;

    backend = e_mail_config_auth_check_get_backend (auth_check);
    page = e_mail_config_service_backend_get_page (backend);
    settings = e_mail_config_service_backend_get_settings (backend);

    page_class = E_MAIL_CONFIG_SERVICE_PAGE_GET_CLASS (page);
    backend_class = E_MAIL_CONFIG_SERVICE_BACKEND_GET_CLASS (backend);

    temp_dir = e_mkdtemp ("evolution-auth-check-XXXXXX");

    /* Create a temporary session for our temporary service.
     * Use the same temporary directory for "user-data-dir" and
     * "user-cache-dir".  For our purposes it shouldn't matter. */
    session = g_object_new (
        CAMEL_TYPE_SESSION,
        "user-data-dir", temp_dir,
        "user-cache-dir", temp_dir,
        NULL);

    /* This returns a BORROWED reference to the CamelService. */
    service = camel_session_add_service (
        session, "fake-uid",
        backend_class->backend_name,
        page_class->provider_type, &error);

    g_free (temp_dir);

    if (error != NULL) {
        g_warn_if_fail (service == NULL);
        e_alert_submit (
            E_ALERT_SINK (page),
            "mail:checking-service-error",
            error->message, NULL);
        g_error_free (error);
        return;
    }

    g_return_if_fail (CAMEL_IS_SERVICE (service));

    camel_service_set_settings (service, settings);

    activity = e_mail_config_service_page_new_activity (page);
    cancellable = e_activity_get_cancellable (activity);

    gtk_widget_set_sensitive (GTK_WIDGET (auth_check), FALSE);

    async_context = g_slice_new (AsyncContext);
    async_context->auth_check = g_object_ref (auth_check);
    async_context->temporary_session = session;  /* takes ownership */
    async_context->activity = activity;          /* takes ownership */

    camel_service_query_auth_types (
        service, G_PRIORITY_DEFAULT, cancellable,
        mail_config_auth_check_update_done_cb, async_context);
}

static void
mail_config_auth_check_clicked_cb (GtkButton *button,
                                   EMailConfigAuthCheck *auth_check)
{
    mail_config_auth_check_update (auth_check);
}

static void
mail_config_auth_check_init_mechanism (EMailConfigAuthCheck *auth_check)
{
    EMailConfigServiceBackend *backend;
    CamelProvider *provider;
    CamelSettings *settings;
    const gchar *auth_mechanism = NULL;

    /* Pick an initial active mechanism name by examining both
     * the corresponding CamelNetworkSettings and CamelProvider. */

    backend = e_mail_config_auth_check_get_backend (auth_check);
    provider = e_mail_config_service_backend_get_provider (backend);
    settings = e_mail_config_service_backend_get_settings (backend);
    g_return_if_fail (CAMEL_IS_NETWORK_SETTINGS (settings));

    auth_mechanism =
        camel_network_settings_get_auth_mechanism (
        CAMEL_NETWORK_SETTINGS (settings));

    /* If CamelNetworkSettings does not have a mechanism name set,
     * choose from the CamelProvider's list of supported mechanisms. */
    if (auth_mechanism == NULL && provider != NULL) {
        if (provider->authtypes != NULL) {
            CamelServiceAuthType *auth_type;
            auth_type = provider->authtypes->data;
            auth_mechanism = auth_type->authproto;
        }
    }

    if (auth_mechanism != NULL)
        e_mail_config_auth_check_set_active_mechanism (
            auth_check, auth_mechanism);
}

static void
mail_config_auth_check_set_backend (EMailConfigAuthCheck *auth_check,
                                    EMailConfigServiceBackend *backend)
{
    g_return_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend));
    g_return_if_fail (auth_check->priv->backend == NULL);

    auth_check->priv->backend = g_object_ref (backend);
}

static void
mail_config_auth_check_set_property (GObject *object,
                                     guint property_id,
                                     const GValue *value,
                                     GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ACTIVE_MECHANISM:
            e_mail_config_auth_check_set_active_mechanism (
                E_MAIL_CONFIG_AUTH_CHECK (object),
                g_value_get_string (value));
            return;

        case PROP_BACKEND:
            mail_config_auth_check_set_backend (
                E_MAIL_CONFIG_AUTH_CHECK (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_config_auth_check_get_property (GObject *object,
                                     guint property_id,
                                     GValue *value,
                                     GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ACTIVE_MECHANISM:
            g_value_set_string (
                value,
                e_mail_config_auth_check_get_active_mechanism (
                E_MAIL_CONFIG_AUTH_CHECK (object)));
            return;

        case PROP_BACKEND:
            g_value_set_object (
                value,
                e_mail_config_auth_check_get_backend (
                E_MAIL_CONFIG_AUTH_CHECK (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_config_auth_check_dispose (GObject *object)
{
    EMailConfigAuthCheckPrivate *priv;

    priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (object);

    if (priv->backend != NULL) {
        g_object_unref (priv->backend);
        priv->backend = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
        dispose (object);
}

static void
mail_config_auth_check_finalize (GObject *object)
{
    EMailConfigAuthCheckPrivate *priv;

    priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (object);

    g_free (priv->active_mechanism);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
        finalize (object);
}

static void
mail_config_auth_check_constructed (GObject *object)
{
    EMailConfigAuthCheck *auth_check;
    EMailConfigServiceBackend *backend;
    CamelProvider *provider;
    GtkWidget *widget;
    const gchar *text;

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
        constructed (object);

    auth_check = E_MAIL_CONFIG_AUTH_CHECK (object);
    backend = e_mail_config_auth_check_get_backend (auth_check);
    provider = e_mail_config_service_backend_get_provider (backend);

    widget = e_auth_combo_box_new ();
    e_auth_combo_box_set_provider (E_AUTH_COMBO_BOX (widget), provider);
    gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
    auth_check->priv->combo_box = widget;  /* do not reference */
    gtk_widget_show (widget);

    g_object_bind_property (
        widget, "active-id",
        auth_check, "active-mechanism",
        G_BINDING_BIDIRECTIONAL |
        G_BINDING_SYNC_CREATE);

    text = _("Check for Supported Types");
    widget = gtk_button_new_with_label (text);
    gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    g_signal_connect (
        widget, "clicked",
        G_CALLBACK (mail_config_auth_check_clicked_cb),
        auth_check);

    mail_config_auth_check_init_mechanism (auth_check);
}

static void
e_mail_config_auth_check_class_init (EMailConfigAuthCheckClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EMailConfigAuthCheckPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_config_auth_check_set_property;
    object_class->get_property = mail_config_auth_check_get_property;
    object_class->dispose = mail_config_auth_check_dispose;
    object_class->finalize = mail_config_auth_check_finalize;
    object_class->constructed = mail_config_auth_check_constructed;

    g_object_class_install_property (
        object_class,
        PROP_ACTIVE_MECHANISM,
        g_param_spec_string (
            "active-mechanism",
            "Active Mechanism",
            "Active authentication mechanism",
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_BACKEND,
        g_param_spec_object (
            "backend",
            "Backend",
            "Mail configuration backend",
            E_TYPE_MAIL_CONFIG_SERVICE_BACKEND,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));
}

static void
e_mail_config_auth_check_init (EMailConfigAuthCheck *auth_check)
{
    auth_check->priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (auth_check);

    gtk_orientable_set_orientation (
        GTK_ORIENTABLE (auth_check),
        GTK_ORIENTATION_HORIZONTAL);

    gtk_box_set_spacing (GTK_BOX (auth_check), 6);
}

GtkWidget *
e_mail_config_auth_check_new (EMailConfigServiceBackend *backend)
{
    g_return_val_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend), NULL);

    return g_object_new (
        E_TYPE_MAIL_CONFIG_AUTH_CHECK,
        "backend", backend, NULL);
}

EMailConfigServiceBackend *
e_mail_config_auth_check_get_backend (EMailConfigAuthCheck *auth_check)
{
    g_return_val_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check), NULL);

    return auth_check->priv->backend;
}

const gchar *
e_mail_config_auth_check_get_active_mechanism (EMailConfigAuthCheck *auth_check)
{
    g_return_val_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check), NULL);

    return auth_check->priv->active_mechanism;
}

void
e_mail_config_auth_check_set_active_mechanism (EMailConfigAuthCheck *auth_check,
                                               const gchar *active_mechanism)
{
    g_return_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check));

    if (g_strcmp0 (auth_check->priv->active_mechanism, active_mechanism) == 0)
        return;

    g_free (auth_check->priv->active_mechanism);
    auth_check->priv->active_mechanism = g_strdup (active_mechanism);

    g_object_notify (G_OBJECT (auth_check), "active-mechanism");
}