aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-plugin-ui.c
blob: 2528efd0faa1c311ca4d02ee68795d06b1e3d33b (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
/*
 * 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/>  
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */

#include "e-plugin-ui.h"

#include <string.h>

#define E_PLUGIN_UI_HOOK_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_PLUGIN_UI_HOOK, EPluginUIHookPrivate))

#define E_PLUGIN_UI_INIT_FUNC       "e_plugin_ui_init"
#define E_PLUGIN_UI_HOOK_CLASS_ID       "org.gnome.evolution.ui:1.0"

struct _EPluginUIHookPrivate {

    /* Table of GtkUIManager ID's to UI definitions. 
     *
     * For example:
     *
     *     <hook class="org.gnome.evolution.ui:1.0">
     *       <ui-manager id="org.gnome.evolution.sample">
     *               ... UI definition ...
     *       </ui-manager>
     *     </hook>
     *
     * Results in:
     *
     *     g_hash_table_insert (
     *             ui_definitions,
     *             "org.gnome.evolution.sample",
     *             "... UI definition ...");
     *
     * See http://library.gnome.org/devel/gtk/unstable/GtkUIManager.html
     * for more information about UI definitions.  Note: the <ui> tag is
     * optional.
     */
    GHashTable *ui_definitions;

    /* The registry is the heart of EPluginUI.  It tracks GtkUIManager
     * instances, GtkUIManager IDs, and UI merge IDs as a hash table of
     * hash tables:
     *
     *     GtkUIManager instance -> GtkUIManager ID -> UI Merge ID
     *
     * A GtkUIManager instance and ID form a unique key for looking up
     * UI merge IDs.  The reason both are needed is because the same
     * GtkUIManager instance and be registered under multiple IDs.
     *
     * This is done primarily to support shell views, which share a
     * common GtkUIManager instance for a particular shell window.
     * Each shell view registers the same GtkUIManager instance under
     * a unique ID:
     *
     *     "org.gnome.evolution.mail"      }
     *     "org.gnome.evolution.contacts"  }  aliases for a common
     *     "org.gnome.evolution.calendar"  }  GtkUIManager instance
     *     "org.gnome.evolution.memos"     }
     *     "org.gnome.evolution.tasks"     }
     *
     * Note: The shell window also registers the same GtkUIManager
     *       instance as "org.gnome.evolution.shell".
     *
     * This way, plugins that extend a shell view's UI will follow the
     * merging and unmerging of the shell view automatically.
     *
     * The presence or absence of GtkUIManager IDs in the registry is
     * significant.  Presence of a (instance, ID) pair indicates that
     * UI manager is active, absence indicates inactive.  Furthermore,
     * a non-zero merge ID for an active UI manager indicates the
     * plugin is enabled.  Zero indicates disabled.
     *
     * Here's a quick scenario to illustrate:
     *
     * Suppose we have a plugin that extends the mail shell view UI.
     * Its EPlugin definition file has this section:
     *
     *     <hook class="org.gnome.evolution.ui:1.0">
     *       <ui-manager id="org.gnome.evolution.mail">
     *               ... UI definition ...
     *       </ui-manager>
     *     </hook>
     *
     * The plugin is enabled and the active shell view is "mail".
     * Let "ManagerA" denote the common GtkUIManager instance for
     * this shell window.  Here's what happens to the registry as
     * the user performs various actions;
     *
     *     - Initial State                            Merge ID
     *                                                   V
     *       { "ManagerA", { "org.gnome.evolution.mail", 3 } }
     *
     *     - User Disables the Plugin
     *
     *       { "ManagerA", { "org.gnome.evolution.mail", 0 } }
     *
     *     - User Enables the Plugin
     *
     *       { "ManagerA", { "org.gnome.evolution.mail", 4 } }
     *
     *     - User Switches to Calendar View
     *
     *       { "ManagerA", { } }
     *
     *     - User Disables the Plugin
     *
     *       { "ManagerA", { } }
     *
     *     - User Switches to Mail View
     *
     *       { "ManagerA", { "org.gnome.evolution.mail", 0 } }
     *
     *     - User Enables the Plugin
     *
     *       { "ManagerA", { "org.gnome.evolution.mail", 5 } }
     */
    GHashTable *registry;
};

static gpointer parent_class;

static void
plugin_ui_hook_unregister_manager (EPluginUIHook *hook,
                                   GtkUIManager *manager)
{
    GHashTable *registry;

    /* Note: Manager may already be finalized. */
    registry = hook->priv->registry;
    g_hash_table_remove (registry, manager);
}

static void
plugin_ui_hook_register_manager (EPluginUIHook *hook,
                                 GtkUIManager *manager,
                                 gpointer user_data)
{
    EPlugin *plugin;
    EPluginUIInitFunc func;
    GHashTable *registry;
    GHashTable *hash_table;

    plugin = ((EPluginHook *) hook)->plugin;
    func = e_plugin_get_symbol (plugin, E_PLUGIN_UI_INIT_FUNC);

    /* Pass the manager and user_data to the plugin's e_plugin_ui_init()
     * function (if it defined one).  The plugin should install whatever
     * GtkActions and GtkActionGroups are neccessary to implement the
     * action names in its UI definition. */
    if (func != NULL && !func (manager, user_data))
        return;

    g_object_weak_ref (
        G_OBJECT (manager), (GWeakNotify)
        plugin_ui_hook_unregister_manager, hook);

    registry = hook->priv->registry;
    hash_table = g_hash_table_lookup (registry, manager);

    if (hash_table == NULL) {
        hash_table = g_hash_table_new_full (
            g_str_hash, g_str_equal,
            (GDestroyNotify) g_free,
            (GDestroyNotify) NULL);
        g_hash_table_insert (registry, manager, hash_table);
    }
}

static guint
plugin_ui_hook_merge_ui (EPluginUIHook *hook,
                         GtkUIManager *manager,
                         const gchar *id)
{
    GHashTable *hash_table;
    const gchar *ui_definition;
    guint merge_id;
    GError *error = NULL;

    hash_table = hook->priv->ui_definitions;
    ui_definition = g_hash_table_lookup (hash_table, id);
    g_return_val_if_fail (ui_definition != NULL, 0);

    merge_id = gtk_ui_manager_add_ui_from_string (
        manager, ui_definition, -1, &error);

    if (error != NULL) {
        g_warning ("%s", error->message);
        g_error_free (error);
    }

    return merge_id;
}

static void
plugin_ui_enable_manager (EPluginUIHook *hook,
                          GtkUIManager *manager,
                          const gchar *id)
{
    GHashTable *hash_table;
    GHashTable *ui_definitions;
    GList *keys;

    hash_table = hook->priv->registry;
    hash_table = g_hash_table_lookup (hash_table, manager);

    if (hash_table == NULL)
        return;

    if (id != NULL)
        keys = g_list_prepend (NULL, (gpointer) id);
    else
        keys = g_hash_table_get_keys (hash_table);

    ui_definitions = hook->priv->ui_definitions;

    while (keys != NULL) {
        guint merge_id;
        gpointer data;

        id = keys->data;
        keys = g_list_delete_link (keys, keys);

        if (g_hash_table_lookup (ui_definitions, id) == NULL)
            continue;

        data = g_hash_table_lookup (hash_table, id);
        merge_id = GPOINTER_TO_UINT (data);

        if (merge_id > 0)
            continue;

        if (((EPluginHook *) hook)->plugin->enabled)
            merge_id = plugin_ui_hook_merge_ui (hook, manager, id);

        /* Merge ID will be 0 on error, which is what we want. */
        data = GUINT_TO_POINTER (merge_id);
        g_hash_table_insert (hash_table, g_strdup (id), data);
    }
}

static void
plugin_ui_disable_manager (EPluginUIHook *hook,
                           GtkUIManager *manager,
                           const gchar *id,
                           gboolean remove)
{
    GHashTable *hash_table;
    GHashTable *ui_definitions;
    GList *keys;

    hash_table = hook->priv->registry;
    hash_table = g_hash_table_lookup (hash_table, manager);

    if (hash_table == NULL)
        return;

    if (id != NULL)
        keys = g_list_prepend (NULL, (gpointer) id);
    else
        keys = g_hash_table_get_keys (hash_table);

    ui_definitions = hook->priv->ui_definitions;

    while (keys != NULL) {
        guint merge_id;
        gpointer data;

        id = keys->data;
        keys = g_list_delete_link (keys, keys);

        if (g_hash_table_lookup (ui_definitions, id) == NULL)
            continue;

        data = g_hash_table_lookup (hash_table, id);
        merge_id = GPOINTER_TO_UINT (data);

        /* Merge ID could be 0 if the plugin is disabled. */
        if (merge_id > 0)
            gtk_ui_manager_remove_ui (manager, merge_id);

        if (remove)
            g_hash_table_remove (hash_table, id);
        else
            g_hash_table_insert (hash_table, g_strdup (id), NULL);
    }
}

static void
plugin_ui_enable_hook (EPluginUIHook *hook)
{
    GHashTable *hash_table;
    GHashTableIter iter;
    gpointer key;

    /* Enable all GtkUIManagers for this hook. */

    hash_table = hook->priv->registry;
    g_hash_table_iter_init (&iter, hash_table);

    while (g_hash_table_iter_next (&iter, &key, NULL)) {
        GtkUIManager *manager = key;
        plugin_ui_enable_manager (hook, manager, NULL);
    }
}

static void
plugin_ui_disable_hook (EPluginUIHook *hook)
{
    GHashTable *hash_table;
    GHashTableIter iter;
    gpointer key;

    /* Disable all GtkUIManagers for this hook. */

    hash_table = hook->priv->registry;
    g_hash_table_iter_init (&iter, hash_table);

    while (g_hash_table_iter_next (&iter, &key, NULL)) {
        GtkUIManager *manager = key;
        plugin_ui_disable_manager (hook, manager, NULL, FALSE);
    }
}

static void
plugin_ui_hook_finalize (GObject *object)
{
    EPluginUIHookPrivate *priv;
    GHashTableIter iter;
    gpointer manager;

    priv = E_PLUGIN_UI_HOOK_GET_PRIVATE (object);

    /* Remove weak reference callbacks to GtkUIManagers. */
    g_hash_table_iter_init (&iter, priv->registry);
    while (g_hash_table_iter_next (&iter, &manager, NULL))
        g_object_weak_unref (
            G_OBJECT (manager), (GWeakNotify)
            plugin_ui_hook_unregister_manager, object);

    g_hash_table_destroy (priv->ui_definitions);
    g_hash_table_destroy (priv->registry);

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

static gint
plugin_ui_hook_construct (EPluginHook *hook,
                          EPlugin *plugin,
                          xmlNodePtr node)
{
    EPluginUIHookPrivate *priv;

    priv = E_PLUGIN_UI_HOOK_GET_PRIVATE (hook);

    /* XXX The EPlugin should be a property of EPluginHookClass.
     *     Then it could be passed directly to g_object_new() and
     *     we wouldn't have to chain up here. */

    /* Chain up to parent's construct() method. */
    E_PLUGIN_HOOK_CLASS (parent_class)->construct (hook, plugin, node);

    for (node = node->children; node != NULL; node = node->next) {
        xmlNodePtr child;
        xmlBufferPtr buffer;
        const gchar *content;
        gchar *id;

        if (strcmp ((gchar *) node->name, "ui-manager") != 0)
            continue;

        id = e_plugin_xml_prop (node, "id");
        if (id == NULL) {
            g_warning ("<ui-manager> requires 'id' property");
            continue;
        }

        /* Extract the XML content below <ui-manager> */
        buffer = xmlBufferCreate ();
        child = node->children;
        while (child != NULL && xmlNodeIsText (child))
            child = child->next;
        if (child != NULL)
            xmlNodeDump (buffer, node->doc, child, 2, 1);
        content = (const gchar *) xmlBufferContent (buffer);

        g_hash_table_insert (
            priv->ui_definitions,
            id, g_strdup (content));

        xmlBufferFree (buffer);
    }

    return 0;
}

static void
plugin_ui_hook_enable (EPluginHook *hook,
                       gint state)
{
    if (state)
        plugin_ui_enable_hook (E_PLUGIN_UI_HOOK (hook));
    else
        plugin_ui_disable_hook (E_PLUGIN_UI_HOOK (hook));
}

static void
plugin_ui_hook_class_init (EPluginUIHookClass *class)
{
    GObjectClass *object_class;
    EPluginHookClass *plugin_hook_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EPluginUIHookPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = plugin_ui_hook_finalize;

    plugin_hook_class = E_PLUGIN_HOOK_CLASS (class);
    plugin_hook_class->id = E_PLUGIN_UI_HOOK_CLASS_ID;
    plugin_hook_class->construct = plugin_ui_hook_construct;
    plugin_hook_class->enable = plugin_ui_hook_enable;
}

static void
plugin_ui_hook_init (EPluginUIHook *hook)
{
    GHashTable *ui_definitions;
    GHashTable *registry;

    ui_definitions = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) g_free);

    registry = g_hash_table_new (g_direct_hash, g_direct_equal);

    hook->priv = E_PLUGIN_UI_HOOK_GET_PRIVATE (hook);
    hook->priv->ui_definitions = ui_definitions;
    hook->priv->registry = registry;
}

GType
e_plugin_ui_hook_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EPluginUIHookClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) plugin_ui_hook_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EPluginUIHook),
            0,     /* n_preallocs */
            (GInstanceInitFunc) plugin_ui_hook_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            E_TYPE_PLUGIN_HOOK, "EPluginUIHook", &type_info, 0);
    }

    return type;
}

void
e_plugin_ui_register_manager (GtkUIManager *manager,
                              const gchar *id,
                              gpointer user_data)
{
    GSList *plugin_list;

    g_return_if_fail (GTK_IS_UI_MANAGER (manager));
    g_return_if_fail (id != NULL);

    /* Loop over all installed plugins. */
    plugin_list = e_plugin_list_plugins ();
    while (plugin_list != NULL) {
        EPlugin *plugin = plugin_list->data;
        GSList *iter;

        plugin_list = g_slist_next (plugin_list);

        /* Look for hooks of type EPluginUIHook. */
        for (iter = plugin->hooks; iter != NULL; iter = iter->next) {
            EPluginUIHook *hook = iter->data;
            GHashTable *hash_table;

            if (!E_IS_PLUGIN_UI_HOOK (hook))
                continue;

            hash_table = hook->priv->ui_definitions;

            /* Check if the hook has a UI definition
             * for the GtkUIManager being registered. */
            if (g_hash_table_lookup (hash_table, id) == NULL)
                continue;

            /* Register the manager with the hook. */
            plugin_ui_hook_register_manager (
                hook, manager, user_data);
        }
    }
}

void
e_plugin_ui_enable_manager (GtkUIManager *manager,
                            const gchar *id)
{
    GSList *plugin_list;

    g_return_if_fail (GTK_IS_UI_MANAGER (manager));
    g_return_if_fail (id != NULL);

    /* Loop over all installed plugins. */
    plugin_list = e_plugin_list_plugins ();
    while (plugin_list != NULL) {
        EPlugin *plugin = plugin_list->data;
        GSList *iter;

        plugin_list = g_slist_next (plugin_list);

        /* Look for hooks of type EPluginUIHook. */
        for (iter = plugin->hooks; iter != NULL; iter = iter->next) {
            EPluginUIHook *hook = iter->data;

            if (!E_IS_PLUGIN_UI_HOOK (hook))
                continue;

            plugin_ui_enable_manager (hook, manager, id);
        }
    }
}

void
e_plugin_ui_disable_manager (GtkUIManager *manager,
                             const gchar *id)
{
    GSList *plugin_list;

    g_return_if_fail (GTK_IS_UI_MANAGER (manager));
    g_return_if_fail (id != NULL);

    /* Loop over all installed plugins. */
    plugin_list = e_plugin_list_plugins ();
    while (plugin_list != NULL) {
        EPlugin *plugin = plugin_list->data;
        GSList *iter;

        plugin_list = g_slist_next (plugin_list);

        /* Look for hooks of type EPluginUIHook. */
        for (iter = plugin->hooks; iter != NULL; iter = iter->next) {
            EPluginUIHook *hook = iter->data;

            if (!E_IS_PLUGIN_UI_HOOK (hook))
                continue;

            plugin_ui_disable_manager (hook, manager, id, TRUE);
        }
    }
}