aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-mail-signature-combo-box.c
blob: fcd3dd07888bc96a06580a329ef323df7e3e35c2 (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
/*
 * e-mail-signature-combo-box.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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "e-mail-signature-combo-box.h"

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

#define E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_SIGNATURE_COMBO_BOX, EMailSignatureComboBoxPrivate))

#define SOURCE_IS_MAIL_SIGNATURE(source) \
    (e_source_has_extension ((source), E_SOURCE_EXTENSION_MAIL_SIGNATURE))

struct _EMailSignatureComboBoxPrivate {
    ESourceRegistry *registry;
    guint refresh_idle_id;
    gchar *identity_uid;
};

enum {
    PROP_0,
    PROP_IDENTITY_UID,
    PROP_REGISTRY
};

enum {
    COLUMN_DISPLAY_NAME,
    COLUMN_UID
};

G_DEFINE_TYPE (
    EMailSignatureComboBox,
    e_mail_signature_combo_box,
    GTK_TYPE_COMBO_BOX)

static gboolean
mail_signature_combo_box_refresh_idle_cb (EMailSignatureComboBox *combo_box)
{
    /* The refresh function will clear the idle ID. */
    e_mail_signature_combo_box_refresh (combo_box);

    return FALSE;
}

static void
mail_signature_combo_box_registry_changed (ESourceRegistry *registry,
                                           ESource *source,
                                           EMailSignatureComboBox *combo_box)
{
    /* If the ESource in question has a "Mail Signature" extension,
     * schedule a refresh of the tree model.  Otherwise ignore it.
     * We use an idle callback to limit how frequently we refresh
     * the tree model, in case the registry is emitting lots of
     * signals at once. */

    if (!SOURCE_IS_MAIL_SIGNATURE (source))
        return;

    if (combo_box->priv->refresh_idle_id > 0)
        return;

    combo_box->priv->refresh_idle_id = g_idle_add (
        (GSourceFunc) mail_signature_combo_box_refresh_idle_cb,
        combo_box);
}

static gboolean
mail_signature_combo_box_identity_to_signature (GBinding *binding,
                                                const GValue *source_value,
                                                GValue *target_value,
                                                gpointer user_data)
{
    EMailSignatureComboBox *combo_box;
    ESourceRegistry *registry;
    GObject *source_object;
    ESource *source;
    ESourceMailIdentity *extension;
    const gchar *identity_uid;
    const gchar *signature_uid = "none";
    const gchar *extension_name;

    /* Source and target are the same object. */
    source_object = g_binding_get_source (binding);
    combo_box = E_MAIL_SIGNATURE_COMBO_BOX (source_object);
    registry = e_mail_signature_combo_box_get_registry (combo_box);

    identity_uid = g_value_get_string (source_value);
    if (identity_uid == NULL)
        return FALSE;

    source = e_source_registry_ref_source (registry, identity_uid);
    if (source == NULL)
        return FALSE;

    extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
    if (!e_source_has_extension (source, extension_name)) {
        g_object_unref (source);
        return FALSE;
    }

    extension = e_source_get_extension (source, extension_name);
    signature_uid = e_source_mail_identity_get_signature_uid (extension);
    g_value_set_string (target_value, signature_uid);

    g_object_unref (source);

    return TRUE;
}

static void
mail_signature_combo_box_set_registry (EMailSignatureComboBox *combo_box,
                                       ESourceRegistry *registry)
{
    g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
    g_return_if_fail (combo_box->priv->registry == NULL);

    combo_box->priv->registry = g_object_ref (registry);

    g_signal_connect (
        registry, "source-added",
        G_CALLBACK (mail_signature_combo_box_registry_changed),
        combo_box);

    g_signal_connect (
        registry, "source-changed",
        G_CALLBACK (mail_signature_combo_box_registry_changed),
        combo_box);

    g_signal_connect (
        registry, "source-removed",
        G_CALLBACK (mail_signature_combo_box_registry_changed),
        combo_box);
}

static void
mail_signature_combo_box_set_property (GObject *object,
                                       guint property_id,
                                       const GValue *value,
                                       GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_IDENTITY_UID:
            e_mail_signature_combo_box_set_identity_uid (
                E_MAIL_SIGNATURE_COMBO_BOX (object),
                g_value_get_string (value));
            return;

        case PROP_REGISTRY:
            mail_signature_combo_box_set_registry (
                E_MAIL_SIGNATURE_COMBO_BOX (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_signature_combo_box_get_property (GObject *object,
                                       guint property_id,
                                       GValue *value,
                                       GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_IDENTITY_UID:
            g_value_set_string (
                value,
                e_mail_signature_combo_box_get_identity_uid (
                E_MAIL_SIGNATURE_COMBO_BOX (object)));
            return;

        case PROP_REGISTRY:
            g_value_set_object (
                value,
                e_mail_signature_combo_box_get_registry (
                E_MAIL_SIGNATURE_COMBO_BOX (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_signature_combo_box_dispose (GObject *object)
{
    EMailSignatureComboBoxPrivate *priv;

    priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (object);

    if (priv->registry != NULL) {
        g_signal_handlers_disconnect_matched (
            priv->registry, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, object);
        g_object_unref (priv->registry);
        priv->registry = NULL;
    }

    if (priv->refresh_idle_id > 0) {
        g_source_remove (priv->refresh_idle_id);
        priv->refresh_idle_id = 0;
    }

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

static void
mail_signature_combo_box_finalize (GObject *object)
{
    EMailSignatureComboBoxPrivate *priv;

    priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (object);

    g_free (priv->identity_uid);

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

static void
mail_signature_combo_box_constructed (GObject *object)
{
    GtkListStore *list_store;
    GtkComboBox *combo_box;
    GtkCellLayout *cell_layout;
    GtkCellRenderer *cell_renderer;

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

    combo_box = GTK_COMBO_BOX (object);
    cell_layout = GTK_CELL_LAYOUT (object);

    list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
    gtk_combo_box_set_model (combo_box, GTK_TREE_MODEL (list_store));
    gtk_combo_box_set_id_column (combo_box, COLUMN_UID);
    g_object_unref (list_store);

    cell_renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (cell_layout, cell_renderer, TRUE);
    gtk_cell_layout_add_attribute (
        cell_layout, cell_renderer, "text", COLUMN_DISPLAY_NAME);

    g_object_bind_property_full (
        combo_box, "identity-uid",
        combo_box, "active-id",
        G_BINDING_DEFAULT,
        mail_signature_combo_box_identity_to_signature,
        NULL,
        NULL, (GDestroyNotify) NULL);

    e_mail_signature_combo_box_refresh (
        E_MAIL_SIGNATURE_COMBO_BOX (object));
}

static void
e_mail_signature_combo_box_class_init (EMailSignatureComboBoxClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (
        class, sizeof (EMailSignatureComboBoxPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_signature_combo_box_set_property;
    object_class->get_property = mail_signature_combo_box_get_property;
    object_class->dispose = mail_signature_combo_box_dispose;
    object_class->finalize = mail_signature_combo_box_finalize;
    object_class->constructed = mail_signature_combo_box_constructed;

    g_object_class_install_property (
        object_class,
        PROP_IDENTITY_UID,
        g_param_spec_string (
            "identity-uid",
            "Identity UID",
            NULL,
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_REGISTRY,
        g_param_spec_object (
            "registry",
            "Registry",
            NULL,
            E_TYPE_SOURCE_REGISTRY,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));
}

static void
e_mail_signature_combo_box_init (EMailSignatureComboBox *combo_box)
{
    combo_box->priv = E_MAIL_SIGNATURE_COMBO_BOX_GET_PRIVATE (combo_box);
}

GtkWidget *
e_mail_signature_combo_box_new (ESourceRegistry *registry)
{
    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);

    return g_object_new (
        E_TYPE_MAIL_SIGNATURE_COMBO_BOX,
        "registry", registry, NULL);
}

void
e_mail_signature_combo_box_refresh (EMailSignatureComboBox *combo_box)
{
    ESourceRegistry *registry;
    GtkComboBox *gtk_combo_box;
    GtkTreeModel *tree_model;
    GtkTreeIter iter;
    ESource *source;
    GList *list, *link;
    const gchar *extension_name;
    const gchar *saved_uid;

    g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));

    if (combo_box->priv->refresh_idle_id > 0) {
        g_source_remove (combo_box->priv->refresh_idle_id);
        combo_box->priv->refresh_idle_id = 0;
    }

    gtk_combo_box = GTK_COMBO_BOX (combo_box);
    tree_model = gtk_combo_box_get_model (gtk_combo_box);

    /* This is an interned string, which means it's safe
     * to use even after clearing the combo box model. */
    saved_uid = gtk_combo_box_get_active_id (gtk_combo_box);

    gtk_list_store_clear (GTK_LIST_STORE (tree_model));

    extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
    registry = e_mail_signature_combo_box_get_registry (combo_box);
    list = e_source_registry_list_sources (registry, extension_name);

    /* The "None" option always comes first. */

    gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);

    gtk_list_store_set (
        GTK_LIST_STORE (tree_model), &iter,
        COLUMN_DISPLAY_NAME, _("None"),
        COLUMN_UID, "none", -1);

    /* The "autogenerated" UID has special meaning. */

    gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);

    gtk_list_store_set (
        GTK_LIST_STORE (tree_model), &iter,
        COLUMN_DISPLAY_NAME, _("Autogenerated"),
        COLUMN_UID, E_MAIL_SIGNATURE_AUTOGENERATED_UID, -1);

    /* Followed by the other mail signatures, alphabetized. */

    for (link = list; link != NULL; link = g_list_next (link)) {
        GtkTreeIter iter;
        const gchar *display_name;
        const gchar *uid;

        source = E_SOURCE (link->data);
        display_name = e_source_get_display_name (source);
        uid = e_source_get_uid (source);

        gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);

        gtk_list_store_set (
            GTK_LIST_STORE (tree_model), &iter,
            COLUMN_DISPLAY_NAME, display_name,
            COLUMN_UID, uid, -1);
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);

    /* Try and restore the previous selected source, or else "None". */

    if (saved_uid != NULL)
        gtk_combo_box_set_active_id (gtk_combo_box, saved_uid);

    if (gtk_combo_box_get_active_id (gtk_combo_box) == NULL)
        gtk_combo_box_set_active (gtk_combo_box, 0);
}

ESourceRegistry *
e_mail_signature_combo_box_get_registry (EMailSignatureComboBox *combo_box)
{
    g_return_val_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box), NULL);

    return combo_box->priv->registry;
}

const gchar *
e_mail_signature_combo_box_get_identity_uid (EMailSignatureComboBox *combo_box)
{
    g_return_val_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box), NULL);

    return combo_box->priv->identity_uid;
}

void
e_mail_signature_combo_box_set_identity_uid (EMailSignatureComboBox *combo_box,
                                             const gchar *identity_uid)
{
    const gchar *active_id;

    g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));

    if (g_strcmp0 (combo_box->priv->identity_uid, identity_uid) == 0)
        return;

    g_free (combo_box->priv->identity_uid);
    combo_box->priv->identity_uid = g_strdup (identity_uid);

    g_object_notify (G_OBJECT (combo_box), "identity-uid");

    /* If "Autogenerated" is selected, emit a "changed" signal as
     * a hint to whomever is listening to reload the signature. */
    active_id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (combo_box));
    if (g_strcmp0 (active_id, E_MAIL_SIGNATURE_AUTOGENERATED_UID) == 0)
        g_signal_emit_by_name (combo_box, "changed");
}

/**************** e_mail_signature_combo_box_load_selected() *****************/

typedef struct _LoadContext LoadContext;

struct _LoadContext {
    gchar *contents;
    gsize length;
    gboolean is_html;
};

static void
load_context_free (LoadContext *context)
{
    g_free (context->contents);
    g_slice_free (LoadContext, context);
}

static void
mail_signature_combo_box_autogenerate (EMailSignatureComboBox *combo_box,
                                       LoadContext *context)
{
    ESourceMailIdentity *extension;
    ESourceRegistry *registry;
    ESource *source;
    GString *buffer;
    const gchar *extension_name;
    const gchar *identity_uid;
    const gchar *text;
    gchar *escaped;

    identity_uid = e_mail_signature_combo_box_get_identity_uid (combo_box);

    /* If we have no mail identity UID, handle it as though
     * "None" were selected.  No need to report an error. */
    if (identity_uid == NULL)
        return;

    registry = e_mail_signature_combo_box_get_registry (combo_box);
    source = e_source_registry_ref_source (registry, identity_uid);

    /* If the mail identity lookup fails, handle it as though
     * "None" were selected.  No need to report an error. */
    if (source == NULL)
        return;

    /* If the source is not actually a mail identity, handle it as
     * though "None" were selected.  No need to report an error. */
    extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
    if (!e_source_has_extension (source, extension_name)) {
        g_object_unref (source);
        return;
    }

    extension = e_source_get_extension (source, extension_name);

    /* The autogenerated signature format is:
     *
     *   <NAME> <ADDRESS>
     *   <ORGANIZATION>
     *
     * The <ADDRESS> is a mailto link and
     * the <ORGANIZATION> line is optional.
     */

    buffer = g_string_sized_new (512);

    text = e_source_mail_identity_get_name (extension);
    escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
    if (escaped != NULL && *escaped != '\0')
        g_string_append (buffer, escaped);
    g_free (escaped);

    text = e_source_mail_identity_get_address (extension);
    escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
    if (escaped != NULL && *escaped != '\0')
        g_string_append_printf (
            buffer, " &lt;<a href=\"mailto:%s\">%s</a>&gt;",
            escaped, escaped);
    g_free (escaped);

    text = e_source_mail_identity_get_organization (extension);
    escaped = (text != NULL) ? g_markup_escape_text (text, -1) : NULL;
    if (escaped != NULL && *escaped != '\0')
        g_string_append_printf (buffer, "<br>%s", escaped);
    g_free (escaped);

    context->length = buffer->len;
    context->contents = g_string_free (buffer, FALSE);
    context->is_html = TRUE;

    g_object_unref (source);
}

static void
mail_signature_combo_box_load_cb (ESource *source,
                                  GAsyncResult *result,
                                  GSimpleAsyncResult *simple)
{
    ESourceMailSignature *extension;
    LoadContext *context;
    const gchar *extension_name;
    const gchar *mime_type;
    GError *error = NULL;

    context = g_simple_async_result_get_op_res_gpointer (simple);

    e_source_mail_signature_load_finish (
        source, result, &context->contents, &context->length, &error);

    if (error != NULL) {
        g_simple_async_result_set_from_error (simple, error);
        g_simple_async_result_complete (simple);
        g_object_unref (simple);
        g_error_free (error);
        return;
    }

    extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
    extension = e_source_get_extension (source, extension_name);
    mime_type = e_source_mail_signature_get_mime_type (extension);
    context->is_html = (g_strcmp0 (mime_type, "text/html") == 0);

    g_simple_async_result_complete (simple);

    g_object_unref (simple);
}

void
e_mail_signature_combo_box_load_selected (EMailSignatureComboBox *combo_box,
                                          gint io_priority,
                                          GCancellable *cancellable,
                                          GAsyncReadyCallback callback,
                                          gpointer user_data)
{
    GSimpleAsyncResult *simple;
    ESourceRegistry *registry;
    LoadContext *context;
    ESource *source;
    const gchar *active_id;

    g_return_if_fail (E_IS_MAIL_SIGNATURE_COMBO_BOX (combo_box));

    context = g_slice_new0 (LoadContext);

    simple = g_simple_async_result_new (
        G_OBJECT (combo_box), callback, user_data,
        e_mail_signature_combo_box_load_selected);

    g_simple_async_result_set_op_res_gpointer (
        simple, context, (GDestroyNotify) load_context_free);

    active_id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (combo_box));

    if (active_id == NULL) {
        g_simple_async_result_complete_in_idle (simple);
        g_object_unref (simple);
        return;
    }

    if (g_strcmp0 (active_id, E_MAIL_SIGNATURE_AUTOGENERATED_UID) == 0) {
        mail_signature_combo_box_autogenerate (combo_box, context);
        g_simple_async_result_complete_in_idle (simple);
        g_object_unref (simple);
        return;
    }

    registry = e_mail_signature_combo_box_get_registry (combo_box);
    source = e_source_registry_ref_source (registry, active_id);

    /* If for some reason the ESource lookup fails, handle it as
     * though "None" were selected.  No need to report an error. */
    if (source == NULL) {
        g_simple_async_result_complete_in_idle (simple);
        g_object_unref (simple);
        return;
    }

    e_source_mail_signature_load (
        source, io_priority, cancellable, (GAsyncReadyCallback)
        mail_signature_combo_box_load_cb, simple);

    g_object_unref (source);
}

gboolean
e_mail_signature_combo_box_load_selected_finish (EMailSignatureComboBox *combo_box,
                                                 GAsyncResult *result,
                                                 gchar **contents,
                                                 gsize *length,
                                                 gboolean *is_html,
                                                 GError **error)
{
    GSimpleAsyncResult *simple;
    LoadContext *context;

    g_return_val_if_fail (
        g_simple_async_result_is_valid (
        result, G_OBJECT (combo_box),
        e_mail_signature_combo_box_load_selected), FALSE);

    simple = G_SIMPLE_ASYNC_RESULT (result);
    context = g_simple_async_result_get_op_res_gpointer (simple);

    if (g_simple_async_result_propagate_error (simple, error))
        return FALSE;

    if (contents != NULL) {
        *contents = context->contents;
        context->contents = NULL;
    }

    if (length != NULL)
        *length = context->length;

    if (is_html != NULL)
        *is_html = context->is_html;

    return TRUE;
}