aboutsummaryrefslogblamecommitdiffstats
path: root/e-util/e-source-combo-box.c
blob: d8d2273527055bffad1b0abfa8f0153042e8d849 (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
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




























































































































































































































































































































































































































































































































































































































































































































                                                                               
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-source-combo-box.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-source-combo-box.h"
#include "e-cell-renderer-color.h"

#define E_SOURCE_COMBO_BOX_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_SOURCE_COMBO_BOX, ESourceComboBoxPrivate))

struct _ESourceComboBoxPrivate {
    ESourceRegistry *registry;
    gchar *extension_name;

    gulong source_added_handler_id;
    gulong source_removed_handler_id;
    gulong source_enabled_handler_id;
    gulong source_disabled_handler_id;

    gboolean show_colors;
};

enum {
    PROP_0,
    PROP_EXTENSION_NAME,
    PROP_REGISTRY,
    PROP_SHOW_COLORS
};

enum {
    COLUMN_COLOR,       /* GDK_TYPE_COLOR */
    COLUMN_NAME,        /* G_TYPE_STRING */
    COLUMN_SENSITIVE,   /* G_TYPE_BOOLEAN */
    COLUMN_UID,     /* G_TYPE_STRING */
    NUM_COLUMNS
};

G_DEFINE_TYPE (ESourceComboBox, e_source_combo_box, GTK_TYPE_COMBO_BOX)

static gboolean
source_combo_box_traverse (GNode *node,
                           ESourceComboBox *combo_box)
{
    ESource *source;
    ESourceSelectable *extension = NULL;
    GtkTreeModel *model;
    GtkTreeIter iter;
    GString *indented;
    GdkColor color;
    const gchar *ext_name;
    const gchar *display_name;
    const gchar *uid;
    gboolean sensitive = FALSE;
    gboolean use_color = FALSE;
    guint depth;

    /* Skip the root node. */
    if (G_NODE_IS_ROOT (node))
        return FALSE;

    ext_name = e_source_combo_box_get_extension_name (combo_box);

    model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
    gtk_list_store_append (GTK_LIST_STORE (model), &iter);

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

    indented = g_string_new (NULL);

    depth = g_node_depth (node);
    g_warn_if_fail (depth > 1);
    while (--depth > 1)
        g_string_append (indented, "    ");
    g_string_append (indented, display_name);

    if (ext_name != NULL && e_source_has_extension (source, ext_name)) {
        extension = e_source_get_extension (source, ext_name);
        sensitive = TRUE;
    }

    if (E_IS_SOURCE_SELECTABLE (extension)) {
        const gchar *color_spec;

        color_spec = e_source_selectable_get_color (extension);
        if (color_spec != NULL && *color_spec != '\0')
            use_color = gdk_color_parse (color_spec, &color);
    }

    gtk_list_store_set (
        GTK_LIST_STORE (model), &iter,
        COLUMN_COLOR, use_color ? &color : NULL,
        COLUMN_NAME, indented->str,
        COLUMN_SENSITIVE, sensitive,
        COLUMN_UID, uid,
        -1);

    g_string_free (indented, TRUE);

    return FALSE;
}

static void
source_combo_box_build_model (ESourceComboBox *combo_box)
{
    ESourceRegistry *registry;
    GtkComboBox *gtk_combo_box;
    GtkTreeModel *model;
    GNode *root;
    const gchar *active_id;
    const gchar *extension_name;

    registry = e_source_combo_box_get_registry (combo_box);
    extension_name = e_source_combo_box_get_extension_name (combo_box);

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

    /* Constructor properties trigger this function before the
     * list store is configured.  Detect it and return silently. */
    if (model == NULL)
        return;

    /* Remember the active ID so we can try to restore it. */
    active_id = gtk_combo_box_get_active_id (gtk_combo_box);

    gtk_list_store_clear (GTK_LIST_STORE (model));

    /* If we have no registry, leave the combo box empty. */
    if (registry == NULL)
        return;

    /* If we have no extension name, leave the combo box empty. */
    if (extension_name == NULL)
        return;

    root = e_source_registry_build_display_tree (registry, extension_name);

    g_node_traverse (
        root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
        (GNodeTraverseFunc) source_combo_box_traverse,
        combo_box);

    e_source_registry_free_display_tree (root);

    /* Restore the active ID, or else set it to something reasonable. */
    gtk_combo_box_set_active_id (gtk_combo_box, active_id);
    if (gtk_combo_box_get_active_id (gtk_combo_box) == NULL) {
        ESource *source;

        source = e_source_registry_ref_default_for_extension_name (
            registry, extension_name);
        if (source != NULL) {
            e_source_combo_box_set_active (combo_box, source);
            g_object_unref (source);
        }
    }
}

static void
source_combo_box_source_added_cb (ESourceRegistry *registry,
                                  ESource *source,
                                  ESourceComboBox *combo_box)
{
    source_combo_box_build_model (combo_box);
}

static void
source_combo_box_source_removed_cb (ESourceRegistry *registry,
                                    ESource *source,
                                    ESourceComboBox *combo_box)
{
    source_combo_box_build_model (combo_box);
}

static void
source_combo_box_source_enabled_cb (ESourceRegistry *registry,
                                    ESource *source,
                                    ESourceComboBox *combo_box)
{
    source_combo_box_build_model (combo_box);
}

static void
source_combo_box_source_disabled_cb (ESourceRegistry *registry,
                                     ESource *source,
                                     ESourceComboBox *combo_box)
{
    source_combo_box_build_model (combo_box);
}

static void
source_combo_box_set_property (GObject *object,
                               guint property_id,
                               const GValue *value,
                               GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_EXTENSION_NAME:
            e_source_combo_box_set_extension_name (
                E_SOURCE_COMBO_BOX (object),
                g_value_get_string (value));
            return;

        case PROP_REGISTRY:
            e_source_combo_box_set_registry (
                E_SOURCE_COMBO_BOX (object),
                g_value_get_object (value));
            return;

        case PROP_SHOW_COLORS:
            e_source_combo_box_set_show_colors (
                E_SOURCE_COMBO_BOX (object),
                g_value_get_boolean (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_combo_box_get_property (GObject *object,
                               guint property_id,
                               GValue *value,
                               GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_EXTENSION_NAME:
            g_value_set_string (
                value,
                e_source_combo_box_get_extension_name (
                E_SOURCE_COMBO_BOX (object)));
            return;

        case PROP_REGISTRY:
            g_value_set_object (
                value,
                e_source_combo_box_get_registry (
                E_SOURCE_COMBO_BOX (object)));
            return;

        case PROP_SHOW_COLORS:
            g_value_set_boolean (
                value,
                e_source_combo_box_get_show_colors (
                E_SOURCE_COMBO_BOX (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_combo_box_dispose (GObject *object)
{
    ESourceComboBoxPrivate *priv;

    priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (object);

    if (priv->registry != NULL) {
        g_signal_handler_disconnect (
            priv->registry,
            priv->source_added_handler_id);
        g_signal_handler_disconnect (
            priv->registry,
            priv->source_removed_handler_id);
        g_signal_handler_disconnect (
            priv->registry,
            priv->source_enabled_handler_id);
        g_signal_handler_disconnect (
            priv->registry,
            priv->source_disabled_handler_id);
        g_object_unref (priv->registry);
        priv->registry = NULL;
    }

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

static void
source_combo_box_finalize (GObject *object)
{
    ESourceComboBoxPrivate *priv;

    priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (object);

    g_free (priv->extension_name);

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

static void
source_combo_box_constructed (GObject *object)
{
    ESourceComboBox *combo_box;
    GtkCellRenderer *renderer;
    GtkCellLayout *layout;
    GtkListStore *store;

    combo_box = E_SOURCE_COMBO_BOX (object);

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

    store = gtk_list_store_new (
        NUM_COLUMNS,
        GDK_TYPE_COLOR,     /* COLUMN_COLOR */
        G_TYPE_STRING,      /* COLUMN_NAME */
        G_TYPE_BOOLEAN,     /* COLUMN_SENSITIVE */
        G_TYPE_STRING);     /* COLUMN_UID */
    gtk_combo_box_set_model (
        GTK_COMBO_BOX (combo_box),
        GTK_TREE_MODEL (store));
    g_object_unref (store);

    gtk_combo_box_set_id_column (GTK_COMBO_BOX (combo_box), COLUMN_UID);

    layout = GTK_CELL_LAYOUT (combo_box);

    renderer = e_cell_renderer_color_new ();
    gtk_cell_layout_pack_start (layout, renderer, FALSE);
    gtk_cell_layout_set_attributes (
        layout, renderer,
        "color", COLUMN_COLOR,
        "sensitive", COLUMN_SENSITIVE,
        NULL);

    g_object_bind_property (
        combo_box, "show-colors",
        renderer, "visible",
        G_BINDING_SYNC_CREATE);

    renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (layout, renderer, TRUE);
    gtk_cell_layout_set_attributes (
        layout, renderer,
        "text", COLUMN_NAME,
        "sensitive", COLUMN_SENSITIVE,
        NULL);

    source_combo_box_build_model (combo_box);
}

static void
e_source_combo_box_class_init (ESourceComboBoxClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    g_type_class_add_private (class, sizeof (ESourceComboBoxPrivate));

    object_class->set_property = source_combo_box_set_property;
    object_class->get_property = source_combo_box_get_property;
    object_class->dispose = source_combo_box_dispose;
    object_class->finalize = source_combo_box_finalize;
    object_class->constructed = source_combo_box_constructed;

    g_object_class_install_property (
        object_class,
        PROP_EXTENSION_NAME,
        g_param_spec_string (
            "extension-name",
            "Extension Name",
            "ESource extension name to filter",
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT));

    /* XXX Don't use G_PARAM_CONSTRUCT_ONLY here.  We need to allow
     *     for this class to be instantiated by a GtkBuilder with no
     *     special construct parameters, and then subsequently give
     *     it an ESourceRegistry. */
    g_object_class_install_property (
        object_class,
        PROP_REGISTRY,
        g_param_spec_object (
            "registry",
            "Registry",
            "Data source registry",
            E_TYPE_SOURCE_REGISTRY,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_SHOW_COLORS,
        g_param_spec_boolean (
            "show-colors",
            "Show Colors",
            "Whether to show colors next to names",
            TRUE,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT |
            G_PARAM_STATIC_STRINGS));
}

static void
e_source_combo_box_init (ESourceComboBox *combo_box)
{
    combo_box->priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (combo_box);

}

/**
 * e_source_combo_box_new:
 * @registry: an #ESourceRegistry, or %NULL
 * @extension_name: an #ESource extension name
 *
 * Creates a new #ESourceComboBox widget that lets the user pick an #ESource
 * from the provided #ESourceRegistry.  The displayed sources are restricted
 * to those which have an @extension_name extension.
 *
 * Returns: a new #ESourceComboBox
 *
 * Since: 2.22
 **/
GtkWidget *
e_source_combo_box_new (ESourceRegistry *registry,
                        const gchar *extension_name)
{
    if (registry != NULL)
        g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);

    return g_object_new (
        E_TYPE_SOURCE_COMBO_BOX, "registry", registry,
        "extension-name", extension_name, NULL);
}

/**
 * e_source_combo_box_get_registry:
 * @combo_box: an #ESourceComboBox
 *
 * Returns the #ESourceRegistry used to populate @combo_box.
 *
 * Returns: the #ESourceRegistry, or %NULL
 *
 * Since: 3.6
 **/
ESourceRegistry *
e_source_combo_box_get_registry (ESourceComboBox *combo_box)
{
    g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);

    return combo_box->priv->registry;
}

/**
 * e_source_combo_box_set_registry:
 * @combo_box: an #ESourceComboBox
 * @registry: an #ESourceRegistry
 *
 * Sets the #ESourceRegistry used to populate @combo_box.
 *
 * This function is intended for cases where @combo_box is instantiated
 * by a #GtkBuilder and has to be given an #ESourceRegistry after it is
 * fully constructed.
 *
 * Since: 3.6
 **/
void
e_source_combo_box_set_registry (ESourceComboBox *combo_box,
                                 ESourceRegistry *registry)
{
    g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));

    if (combo_box->priv->registry == registry)
        return;

    if (registry != NULL) {
        g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
        g_object_ref (registry);
    }

    if (combo_box->priv->registry != NULL) {
        g_signal_handler_disconnect (
            combo_box->priv->registry,
            combo_box->priv->source_added_handler_id);
        g_signal_handler_disconnect (
            combo_box->priv->registry,
            combo_box->priv->source_removed_handler_id);
        g_signal_handler_disconnect (
            combo_box->priv->registry,
            combo_box->priv->source_enabled_handler_id);
        g_signal_handler_disconnect (
            combo_box->priv->registry,
            combo_box->priv->source_disabled_handler_id);
        g_object_unref (combo_box->priv->registry);
    }

    combo_box->priv->registry = registry;

    combo_box->priv->source_added_handler_id = 0;
    combo_box->priv->source_removed_handler_id = 0;
    combo_box->priv->source_enabled_handler_id = 0;
    combo_box->priv->source_disabled_handler_id = 0;

    if (registry != NULL) {
        gulong handler_id;

        handler_id = g_signal_connect (
            registry, "source-added",
            G_CALLBACK (source_combo_box_source_added_cb),
            combo_box);
        combo_box->priv->source_added_handler_id = handler_id;

        handler_id = g_signal_connect (
            registry, "source-removed",
            G_CALLBACK (source_combo_box_source_removed_cb),
            combo_box);
        combo_box->priv->source_removed_handler_id = handler_id;

        handler_id = g_signal_connect (
            registry, "source-enabled",
            G_CALLBACK (source_combo_box_source_enabled_cb),
            combo_box);
        combo_box->priv->source_enabled_handler_id = handler_id;

        handler_id = g_signal_connect (
            registry, "source-disabled",
            G_CALLBACK (source_combo_box_source_disabled_cb),
            combo_box);
        combo_box->priv->source_disabled_handler_id = handler_id;
    }

    source_combo_box_build_model (combo_box);

    g_object_notify (G_OBJECT (combo_box), "registry");
}

/**
 * e_source_combo_box_get_extension_name:
 * @combo_box: an #ESourceComboBox
 *
 * Returns the extension name used to filter which data sources are
 * shown in @combo_box.
 *
 * Returns: the #ESource extension name
 *
 * Since: 3.6
 **/
const gchar *
e_source_combo_box_get_extension_name (ESourceComboBox *combo_box)
{
    g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);

    return combo_box->priv->extension_name;
}

/**
 * e_source_combo_box_set_extension_name:
 * @combo_box: an #ESourceComboBox
 * @extension_name: an #ESource extension name
 *
 * Sets the extension name used to filter which data sources are shown in
 * @combo_box.
 *
 * Since: 3.6
 **/
void
e_source_combo_box_set_extension_name (ESourceComboBox *combo_box,
                                       const gchar *extension_name)
{
    g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));

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

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

    source_combo_box_build_model (combo_box);

    g_object_notify (G_OBJECT (combo_box), "extension-name");
}

/**
 * e_source_combo_box_get_show_colors:
 * @combo_box: an #ESourceComboBox
 *
 * Returns whether colors are shown next to data sources.
 *
 * Returns: %TRUE if colors are being shown
 *
 * Since: 3.6
 **/
gboolean
e_source_combo_box_get_show_colors (ESourceComboBox *combo_box)
{
    g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), FALSE);

    return combo_box->priv->show_colors;
}

/**
 * e_source_combo_box_set_show_colors:
 * @combo_box: an #ESourceComboBox
 * @show_colors: whether to show colors
 *
 * Sets whether to show colors next to data sources.
 *
 * Since: 3.6
 **/
void
e_source_combo_box_set_show_colors (ESourceComboBox *combo_box,
                                    gboolean show_colors)
{
    g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));

    if ((show_colors ? 1 : 0) == (combo_box->priv->show_colors ? 1 : 0))
        return;

    combo_box->priv->show_colors = show_colors;

    source_combo_box_build_model (combo_box);

    g_object_notify (G_OBJECT (combo_box), "show-colors");
}

/**
 * e_source_combo_box_ref_active:
 * @combo_box: an #ESourceComboBox
 *
 * Returns the #ESource corresponding to the currently active item,
 * or %NULL if there is no active item.
 *
 * The returned #ESource is referenced for thread-safety and must be
 * unreferenced with g_object_unref() when finished with it.
 *
 * Returns: an #ESource or %NULL
 *
 * Since: 3.6
 **/
ESource *
e_source_combo_box_ref_active (ESourceComboBox *combo_box)
{
    ESourceRegistry *registry;
    GtkComboBox *gtk_combo_box;
    const gchar *active_id;

    g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);

    registry = e_source_combo_box_get_registry (combo_box);

    gtk_combo_box = GTK_COMBO_BOX (combo_box);
    active_id = gtk_combo_box_get_active_id (gtk_combo_box);

    if (active_id == NULL)
        return NULL;

    return e_source_registry_ref_source (registry, active_id);
}

/**
 * e_source_combo_box_set_active:
 * @combo_box: an #ESourceComboBox
 * @source: an #ESource
 *
 * Sets the active item to the one corresponding to @source.
 *
 * Since: 2.22
 **/
void
e_source_combo_box_set_active (ESourceComboBox *combo_box,
                               ESource *source)
{
    GtkComboBox *gtk_combo_box;
    const gchar *uid;

    g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));
    g_return_if_fail (E_IS_SOURCE (source));

    uid = e_source_get_uid (source);

    gtk_combo_box = GTK_COMBO_BOX (combo_box);
    gtk_combo_box_set_active_id (gtk_combo_box, uid);
}