Statistics
| Revision:

root / src / plugin.c @ 2164

History | View | Annotate | Download (15.7 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2009 Hiroyuki Yamamoto
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 */
19

    
20
#include <glib.h>
21
#include <gmodule.h>
22
#include <gtk/gtk.h>
23

    
24
#include "plugin.h"
25
#include "utils.h"
26
#include "folder.h"
27

    
28
G_DEFINE_TYPE(SylPlugin, syl_plugin, G_TYPE_OBJECT);
29

    
30
enum {
31
        PLUGIN_LOAD,
32
        PLUGIN_UNLOAD,
33
        FOLDERVIEW_MENU_POPUP,
34
        LAST_SIGNAL
35
};
36

    
37
#define syl_plugin_lookup_symbol(name)        g_hash_table_lookup(sym_table, name)
38

    
39
#define SAFE_CALL(func_ptr)                { if (func_ptr) func_ptr(); }
40
#define SAFE_CALL_RET(func_ptr)                (func_ptr ? func_ptr() : NULL)
41
#define SAFE_CALL_ARG1(func_ptr, arg1)        { if (func_ptr) func_ptr(arg1); }
42
#define SAFE_CALL_ARG1_RET(func_ptr, arg1) \
43
                                (func_ptr ? func_ptr(arg1) : NULL)
44
#define SAFE_CALL_ARG2(func_ptr, arg1, arg2) \
45
                                { if (func_ptr) func_ptr(arg1, arg2); }
46
#define SAFE_CALL_ARG2_RET(func_ptr, arg1, arg2) \
47
                                (func_ptr ? func_ptr(arg1, arg2) : NULL)
48
#define SAFE_CALL_ARG2_RET_VAL(func_ptr, arg1, arg2, retval) \
49
                                (func_ptr ? func_ptr(arg1, arg2) : retval)
50
#define SAFE_CALL_ARG3(func_ptr, arg1, arg2, arg3) \
51
                                { if (func_ptr) func_ptr(arg1, arg2, arg3); }
52
#define SAFE_CALL_ARG3_RET(func_ptr, arg1, arg2, arg3) \
53
                                (func_ptr ? func_ptr(arg1, arg2, arg3) : NULL)
54
#define SAFE_CALL_ARG4(func_ptr, arg1, arg2, arg3, arg4) \
55
                                { if (func_ptr) func_ptr(arg1, arg2, arg3); }
56
#define SAFE_CALL_ARG4_RET(func_ptr, arg1, arg2, arg3, arg4) \
57
                                (func_ptr ? func_ptr(arg1, arg2, arg3, arg4) : NULL)
58

    
59
static guint plugin_signals[LAST_SIGNAL] = { 0 };
60

    
61
static GHashTable *sym_table = NULL;
62
static GSList *module_list = NULL;
63
static GObject *plugin_obj = NULL;
64

    
65
static void syl_plugin_init(SylPlugin *self)
66
{
67
}
68

    
69
static void syl_plugin_class_init(SylPluginClass *klass)
70
{
71
        GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
72

    
73
        plugin_signals[PLUGIN_LOAD] =
74
                g_signal_new("plugin-load",
75
                             G_TYPE_FROM_CLASS(gobject_class),
76
                             G_SIGNAL_RUN_FIRST,
77
                             G_STRUCT_OFFSET(SylPluginClass, plugin_load),
78
                             NULL, NULL,
79
                             g_cclosure_marshal_VOID__POINTER,
80
                             G_TYPE_NONE,
81
                             1,
82
                             G_TYPE_POINTER);
83
        plugin_signals[PLUGIN_UNLOAD] =
84
                g_signal_new("plugin-unload",
85
                             G_TYPE_FROM_CLASS(gobject_class),
86
                             G_SIGNAL_RUN_FIRST,
87
                             G_STRUCT_OFFSET(SylPluginClass, plugin_unload),
88
                             NULL, NULL,
89
                             g_cclosure_marshal_VOID__POINTER,
90
                             G_TYPE_NONE,
91
                             1,
92
                             G_TYPE_POINTER);
93
        plugin_signals[FOLDERVIEW_MENU_POPUP] =
94
                g_signal_new("folderview-menu-popup",
95
                             G_TYPE_FROM_CLASS(gobject_class),
96
                             G_SIGNAL_RUN_FIRST,
97
                             G_STRUCT_OFFSET(SylPluginClass,
98
                                             folderview_menu_popup),
99
                             NULL, NULL,
100
                             g_cclosure_marshal_VOID__POINTER,
101
                             G_TYPE_NONE,
102
                             1,
103
                             G_TYPE_POINTER);
104
}
105

    
106
void syl_plugin_signal_connect(const gchar *name, GCallback callback,
107
                               gpointer data)
108
{
109
        g_signal_connect(plugin_obj, name, callback, data);
110
}
111

    
112
void syl_plugin_signal_disconnect(gpointer func, gpointer data)
113
{
114
        g_signal_handlers_disconnect_by_func(plugin_obj, func, data);
115
}
116

    
117
void syl_plugin_signal_emit(const gchar *name, ...)
118
{
119
        guint signal_id;
120

    
121
        if (g_signal_parse_name(name, G_TYPE_FROM_INSTANCE(plugin_obj), &signal_id, NULL, FALSE)) {
122
                va_list var_args;
123
                va_start(var_args, name);
124
                g_signal_emit_valist(plugin_obj, signal_id, 0, var_args);
125
                va_end(var_args);
126
        } else
127
                g_warning("%s: signal '%s' not found", G_STRLOC, name);
128
}
129

    
130
gint syl_plugin_init_lib(void)
131
{
132
        if (!g_module_supported()) {
133
                g_warning("Plug-in is not supported.");
134
                return -1;
135
        }
136

    
137
        if (!sym_table) {
138
                sym_table = g_hash_table_new(g_str_hash, g_str_equal);
139
        }
140

    
141
        if (!plugin_obj) {
142
                plugin_obj = g_object_new(SYL_TYPE_PLUGIN, NULL);
143
        }
144

    
145
        return 0;
146
}
147

    
148
gint syl_plugin_load(const gchar *name)
149
{
150
        GModule *module;
151
        SylPluginLoadFunc load_func = NULL;
152
        gchar *file;
153

    
154
        g_return_val_if_fail(name != NULL, -1);
155

    
156
        debug_print("syl_plugin_load: loading %s\n", name);
157

    
158
        if (!g_path_is_absolute(name))
159
                file = g_strconcat(PLUGINDIR, G_DIR_SEPARATOR_S, name, NULL);
160
        else
161
                file = g_strdup(name);
162

    
163
        module = g_module_open(file, G_MODULE_BIND_LAZY);
164
        if (!module) {
165
                g_warning("Cannot open module: %s: %s", name, g_module_error());
166
                g_free(file);
167
                return -1;
168
        }
169
        if (g_slist_find(module_list, module)) {
170
                g_warning("Module %s is already loaded", name);
171
                g_free(file);
172
                return -1;
173
        }
174

    
175
        if (g_module_symbol(module, "plugin_load", (gpointer *)&load_func)) {
176
                if (!syl_plugin_check_version(module)) {
177
#if 0
178
                        g_warning("Version check failed. Skipping: %s", name);
179
                        g_module_close(module);
180
                        g_free(file);
181
                        return -1;
182
#endif
183
                }
184

    
185
                debug_print("calling plugin_load() in %s\n",
186
                            g_module_name(module));
187
                load_func();
188
                module_list = g_slist_prepend(module_list, module);
189
                g_signal_emit(plugin_obj, plugin_signals[PLUGIN_LOAD], 0, module);
190
        } else {
191
                g_warning("Cannot get symbol: %s: %s", name, g_module_error());
192
                g_module_close(module);
193
                g_free(file);
194
                return -1;
195
        }
196

    
197
        g_free(file);
198

    
199
        return 0;
200
}
201

    
202
gint syl_plugin_load_all(const gchar *dir)
203
{
204
        GDir *d;
205
        const gchar *dir_name;
206
        gchar *path;
207
        gint count = 0;
208

    
209
        if ((d = g_dir_open(dir, 0, NULL)) == NULL) {
210
                g_warning("failed to open directory: %s", dir);
211
                return -1;
212
        }
213

    
214
        while ((dir_name = g_dir_read_name(d)) != NULL) {
215
                if (!g_str_has_suffix(dir_name, "." G_MODULE_SUFFIX))
216
                        continue;
217
                path = g_strconcat(dir, G_DIR_SEPARATOR_S, dir_name, NULL);
218
                if (syl_plugin_load(path) == 0)
219
                        count++;
220
                g_free(path);
221
        }
222

    
223
        g_dir_close(d);
224

    
225
        return count;
226
}
227

    
228
void syl_plugin_unload_all(void)
229
{
230
        GSList *cur;
231

    
232
        for (cur = module_list; cur != NULL; cur = cur->next) {
233
                GModule *module = (GModule *)cur->data;
234
                SylPluginUnloadFunc unload_func = NULL;
235

    
236
                if (g_module_symbol(module, "plugin_unload",
237
                                    (gpointer *)&unload_func)) {
238
                        g_signal_emit(plugin_obj, plugin_signals[PLUGIN_UNLOAD],
239
                                      0, module);
240
                        debug_print("calling plugin_unload() in %s\n",
241
                                    g_module_name(module));
242
                        unload_func();
243
                } else {
244
                        g_warning("Cannot get symbol: %s", g_module_error());
245
                }
246
                if (!g_module_close(module)) {
247
                        g_warning("Module unload failed: %s", g_module_error());
248
                }
249
        }
250

    
251
        g_slist_free(module_list);
252
        module_list = NULL;
253
}
254

    
255
GSList *syl_plugin_get_module_list(void)
256
{
257
        return module_list;
258
}
259

    
260
SylPluginInfo *syl_plugin_get_info(GModule *module)
261
{
262
        SylPluginInfo * (*plugin_info_func)(void);
263

    
264
        g_return_val_if_fail(module != NULL, NULL);
265

    
266
        debug_print("getting plugin information of %s\n",
267
                    g_module_name(module));
268

    
269
        if (g_module_symbol(module, "plugin_info",
270
                            (gpointer *)&plugin_info_func)) {
271
                debug_print("calling plugin_info() in %s\n",
272
                            g_module_name(module));
273
                return plugin_info_func();
274
        } else {
275
                g_warning("Cannot get symbol: %s: %s", g_module_name(module),
276
                          g_module_error());
277
                return NULL;
278
        }
279
}
280

    
281
gboolean syl_plugin_check_version(GModule *module)
282
{
283
        gint (*version_func)(void);
284
        gint ver;
285

    
286
        g_return_val_if_fail(module != NULL, FALSE);
287

    
288
        if (g_module_symbol(module, "plugin_interface_version",
289
                            (gpointer *)&version_func)) {
290
                debug_print("calling plugin_interface_version() in %s\n",
291
                            g_module_name(module));
292
                ver = version_func();
293
        } else {
294
                g_warning("Cannot get symbol: %s: %s", g_module_name(module),
295
                          g_module_error());
296
                return FALSE;
297
        }
298

    
299
        if (ver == SYL_PLUGIN_INTERFACE_VERSION) {
300
                debug_print("Version OK: plugin: %d, app: %d\n",
301
                            ver, SYL_PLUGIN_INTERFACE_VERSION);
302
                return TRUE;
303
        } else {
304
                g_warning("Plugin interface version mismatch: plugin: %d, app: %d", ver, SYL_PLUGIN_INTERFACE_VERSION);
305
                return FALSE;
306
        }
307
}
308

    
309
gint syl_plugin_add_symbol(const gchar *name, gpointer sym)
310
{
311
        g_hash_table_insert(sym_table, (gpointer)name, sym);
312
        return 0;
313
}
314

    
315
const gchar *syl_plugin_get_prog_version(void)
316
{
317
        gpointer sym;
318

    
319
        sym = syl_plugin_lookup_symbol("prog_version");
320
        return (gchar *)sym;
321
}
322

    
323
gpointer syl_plugin_main_window_get(void)
324
{
325
        gpointer (*func)(void);
326

    
327
        func = syl_plugin_lookup_symbol("main_window_get");
328
        return SAFE_CALL_RET(func);
329
}
330

    
331
void syl_plugin_main_window_popup(gpointer mainwin)
332
{
333
        void (*func)(gpointer);
334

    
335
        func = syl_plugin_lookup_symbol("main_window_popup");
336
        SAFE_CALL_ARG1(func, mainwin);
337
}
338

    
339
void syl_plugin_app_will_exit(gboolean force)
340
{
341
        void (*func)(gboolean);
342

    
343
        func = syl_plugin_lookup_symbol("app_will_exit");
344
        SAFE_CALL_ARG1(func, force);
345
}
346

    
347
static GtkItemFactory *get_item_factory(const gchar *path)
348
{
349
        GtkItemFactory *ifactory;
350

    
351
        if (!path)
352
                return NULL;
353

    
354
        if (strncmp(path, "<Main>", 6) == 0)
355
                ifactory = syl_plugin_lookup_symbol("main_window_menu_factory");
356
        else if (strncmp(path, "<MailFolder>", 12) == 0)
357
                ifactory = syl_plugin_lookup_symbol("folderview_mail_popup_factory");
358
        else
359
                ifactory = syl_plugin_lookup_symbol("main_window_menu_factory");
360

    
361
        return ifactory;
362
}
363

    
364
gint syl_plugin_add_menuitem(const gchar *parent, const gchar *label,
365
                             SylPluginCallbackFunc func, gpointer data)
366
{
367
        GtkItemFactory *ifactory;
368
        GtkWidget *menu;
369
        GtkWidget *menuitem;
370

    
371
        if (!parent)
372
                return -1;
373

    
374
        ifactory = get_item_factory(parent);
375
        if (!ifactory)
376
                return -1;
377

    
378
        menu = gtk_item_factory_get_widget(ifactory, parent);
379
        if (!menu)
380
                return -1;
381

    
382
        if (label)
383
                menuitem = gtk_menu_item_new_with_label(label);
384
        else {
385
                menuitem = gtk_menu_item_new();
386
                gtk_widget_set_sensitive(menuitem, FALSE);
387
        }
388
        gtk_widget_show(menuitem);
389
        gtk_menu_append(GTK_MENU(menu), menuitem);
390
        if (func)
391
                g_signal_connect(G_OBJECT(menuitem), "activate",
392
                                 G_CALLBACK(func), data);
393

    
394
        return 0;
395
}
396

    
397
gint syl_plugin_add_factory_item(const gchar *parent, const gchar *label,
398
                                 SylPluginCallbackFunc func, gpointer data)
399
{
400
        GtkItemFactory *ifactory;
401
        GtkItemFactoryEntry entry = {NULL, NULL, NULL, 0, NULL};
402

    
403
        if (!parent)
404
                return -1;
405

    
406
        ifactory = get_item_factory(parent);
407
        if (!ifactory)
408
                return -1;
409

    
410
        if (label) {
411
                entry.path = (gchar *)label;
412
                if (g_str_has_suffix(label, "/---"))
413
                        entry.item_type = "<Separator>";
414
                else
415
                        entry.item_type = NULL;
416
        } else {
417
                entry.path = "/---";
418
                entry.item_type = "<Separator>";
419
        }
420
        entry.callback = func;
421
        g_print("entry.path = %s\n", entry.path);
422

    
423
        gtk_item_factory_create_item(ifactory, &entry, data, 2);
424

    
425
        return 0;
426
}
427

    
428
void syl_plugin_menu_set_sensitive(const gchar *path, gboolean sensitive)
429
{
430
        GtkItemFactory *ifactory;
431
        GtkWidget *widget;
432

    
433
        g_return_if_fail(path != NULL);
434

    
435
        ifactory = get_item_factory(path);
436
        if (!ifactory)
437
                return;
438

    
439
        widget = gtk_item_factory_get_item(ifactory, path);
440
        gtk_widget_set_sensitive(widget, sensitive);
441
}
442

    
443
void syl_plugin_menu_set_sensitive_all(GtkMenuShell *menu_shell,
444
                                       gboolean sensitive)
445
{
446
        GList *cur;
447

    
448
        for (cur = menu_shell->children; cur != NULL; cur = cur->next)
449
                gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
450
}
451

    
452
void syl_plugin_menu_set_active(const gchar *path, gboolean is_active)
453
{
454
        GtkItemFactory *ifactory;
455
        GtkWidget *widget;
456

    
457
        g_return_if_fail(path != NULL);
458

    
459
        ifactory = get_item_factory(path);
460
        if (!ifactory)
461
                return;
462

    
463
        widget = gtk_item_factory_get_item(ifactory, path);
464
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
465
}
466

    
467
gpointer syl_plugin_folderview_get(void)
468
{
469
        gpointer sym;
470

    
471
        sym = syl_plugin_lookup_symbol("folderview");
472
        return sym;
473
}
474

    
475
FolderItem *syl_plugin_folderview_get_selected_item(void)
476
{
477
        FolderItem * (*func)(gpointer);
478
        gpointer folderview;
479

    
480
        folderview = syl_plugin_folderview_get();
481
        if (folderview) {
482
                func = syl_plugin_lookup_symbol("folderview_get_selected_item");
483
                return SAFE_CALL_ARG1_RET(func, folderview);
484
        }
485

    
486
        return NULL;
487
}
488

    
489
gpointer syl_plugin_summary_view_get(void)
490
{
491
        gpointer sym;
492

    
493
        sym = syl_plugin_lookup_symbol("summaryview");
494
        return sym;
495
}
496

    
497
void syl_plugin_sumary_select_by_msgnum(guint msgnum)
498
{
499
        void (*func)(gpointer, guint);
500
        gpointer summary;
501

    
502
        summary = syl_plugin_summary_view_get();
503
        if (summary) {
504
                func = syl_plugin_lookup_symbol("summary_select_by_msgnum");
505
                SAFE_CALL_ARG2(func, summary, msgnum);
506
        }
507
}
508

    
509
gboolean syl_plugin_summary_select_by_msginfo(MsgInfo *msginfo)
510
{
511
        gboolean (*func)(gpointer, MsgInfo *);
512
        gpointer summary;
513

    
514
        summary = syl_plugin_summary_view_get();
515
        if (summary) {
516
                func = syl_plugin_lookup_symbol("summary_select_by_msginfo");
517
                return SAFE_CALL_ARG2_RET_VAL(func, summary, msginfo, FALSE);
518
        }
519

    
520
        return FALSE;
521
}
522

    
523
void syl_plugin_open_message(const gchar *folder_id, guint msgnum)
524
{
525
        FolderItem *item;
526
        MsgInfo *msginfo;
527

    
528
        item = folder_find_item_from_identifier(folder_id);
529
        msginfo = folder_item_get_msginfo(item, msgnum);
530

    
531
        if (msginfo) {
532
                if (!syl_plugin_summary_select_by_msginfo(msginfo)) {
533
                        syl_plugin_open_message_by_new_window(msginfo);
534
                }
535
                procmsg_msginfo_free(msginfo);
536
        }
537
}
538

    
539
gpointer syl_plugin_messageview_create_with_new_window(void)
540
{
541
        gpointer (*func)(void);
542

    
543
        func = syl_plugin_lookup_symbol("messageview_create_with_new_window");
544
        return SAFE_CALL_RET(func);
545
}
546

    
547
void syl_plugin_open_message_by_new_window(MsgInfo *msginfo)
548
{
549
        gpointer msgview;
550
        gpointer (*func)(gpointer, MsgInfo *, gboolean);
551

    
552
        msgview = syl_plugin_messageview_create_with_new_window();
553
        if (msgview) {
554
                func = syl_plugin_lookup_symbol("messageview_show");
555
                SAFE_CALL_ARG3(func, msgview, msginfo, FALSE);
556
        }
557
}
558

    
559
FolderItem *syl_plugin_folder_sel(Folder *cur_folder, gint sel_type,
560
                                  const gchar *default_folder)
561
{
562
        FolderItem * (*func)(Folder *, gint, const gchar *);
563

    
564
        func = syl_plugin_lookup_symbol("foldersel_folder_sel");
565
        return SAFE_CALL_ARG3_RET(func, cur_folder, sel_type, default_folder);
566
}
567

    
568
FolderItem *syl_plugin_folder_sel_full(Folder *cur_folder, gint sel_type,
569
                                       const gchar *default_folder,
570
                                       const gchar *message)
571
{
572
        FolderItem * (*func)(Folder *, gint, const gchar *, const gchar *);
573

    
574
        func = syl_plugin_lookup_symbol("foldersel_folder_sel_full");
575
        return SAFE_CALL_ARG4_RET(func, cur_folder, sel_type, default_folder,
576
                                  message);
577
}
578

    
579
gchar *syl_plugin_input_dialog(const gchar *title, const gchar *message,
580
                               const gchar *default_string)
581
{
582
        gchar * (*func)(const gchar *, const gchar *, const gchar *);
583

    
584
        func = syl_plugin_lookup_symbol("input_dialog");
585
        return SAFE_CALL_ARG3_RET(func, title, message, default_string);
586
}
587

    
588
gchar *syl_plugin_input_dialog_with_invisible(const gchar *title,
589
                                              const gchar *message,
590
                                              const gchar *default_string)
591
{
592
        gchar * (*func)(const gchar *, const gchar *, const gchar *);
593

    
594
        func = syl_plugin_lookup_symbol("input_dialog_with_invisible");
595
        return SAFE_CALL_ARG3_RET(func, title, message, default_string);
596
}
597

    
598
void syl_plugin_manage_window_set_transient(GtkWindow *window)
599
{
600
        void (*func)(GtkWindow *);
601

    
602
        func = syl_plugin_lookup_symbol("manage_window_set_transient");
603
        SAFE_CALL_ARG1(func, window);
604
}
605

    
606
void syl_plugin_manage_window_signals_connect(GtkWindow *window)
607
{
608
        void (*func)(GtkWindow *);
609

    
610
        func = syl_plugin_lookup_symbol("manage_window_signals_connect");
611
        SAFE_CALL_ARG1(func, window);
612
}
613

    
614
GtkWidget *syl_plugin_manage_window_get_focus_window(void)
615
{
616
        GtkWidget * (*func)(void);
617

    
618
        func = syl_plugin_lookup_symbol("manage_window_get_focus_window");
619
        return SAFE_CALL_RET(func);
620
}
621

    
622
void syl_plugin_inc_mail(void)
623
{
624
        void (*func)(gpointer);
625

    
626
        func = syl_plugin_lookup_symbol("inc_mail");
627
        SAFE_CALL_ARG1(func, syl_plugin_main_window_get());
628
}
629

    
630
void syl_plugin_inc_lock(void)
631
{
632
        void (*func)(void);
633

    
634
        func = syl_plugin_lookup_symbol("inc_lock");
635
        SAFE_CALL(func);
636
}
637

    
638
void syl_plugin_inc_unlock(void)
639
{
640
        void (*func)(void);
641

    
642
        func = syl_plugin_lookup_symbol("inc_unlock");
643
        SAFE_CALL(func);
644
}