Merge pull request #647 from konsolebox/master
[geany-mirror.git] / src / plugins.c
blobbcc42e9b9090e42ad6a326315eee0906cd9fed7b
1 /*
2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* Code to manage, load and unload plugins. */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #ifdef HAVE_PLUGINS
30 #include "plugins.h"
32 #include "app.h"
33 #include "dialogs.h"
34 #include "encodings.h"
35 #include "geanyobject.h"
36 #include "geanywraplabel.h"
37 #include "highlighting.h"
38 #include "keybindingsprivate.h"
39 #include "keyfile.h"
40 #include "main.h"
41 #include "msgwindow.h"
42 #include "navqueue.h"
43 #include "plugindata.h"
44 #include "pluginprivate.h"
45 #include "pluginutils.h"
46 #include "prefs.h"
47 #include "sciwrappers.h"
48 #include "stash.h"
49 #include "support.h"
50 #include "symbols.h"
51 #include "templates.h"
52 #include "toolbar.h"
53 #include "ui_utils.h"
54 #include "utils.h"
55 #include "win32.h"
57 #include "gtkcompat.h"
59 #include <string.h>
62 GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
65 static gboolean want_plugins = FALSE;
67 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
68 * opened, afterwards it will be destroyed */
69 static GList *plugin_list = NULL;
70 static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
71 static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
73 static GtkWidget *menu_separator = NULL;
75 static gchar *get_plugin_path(void);
76 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
78 static GeanyData geany_data;
81 static void
82 geany_data_init(void)
84 GeanyData gd = {
85 app,
86 &main_widgets,
87 documents_array,
88 filetypes_array,
89 &prefs,
90 &interface_prefs,
91 &toolbar_prefs,
92 &editor_prefs,
93 &file_prefs,
94 &search_prefs,
95 &tool_prefs,
96 &template_prefs,
97 &build_info,
98 filetypes_by_title
101 geany_data = gd;
105 /* Prevent the same plugin filename being loaded more than once.
106 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
107 static gboolean
108 plugin_loaded(GModule *module)
110 gchar *basename_module, *basename_loaded;
111 GList *item;
113 basename_module = g_path_get_basename(g_module_name(module));
114 for (item = plugin_list; item != NULL; item = g_list_next(item))
116 basename_loaded = g_path_get_basename(
117 g_module_name(((Plugin*)item->data)->module));
119 if (utils_str_equal(basename_module, basename_loaded))
121 g_free(basename_loaded);
122 g_free(basename_module);
123 return TRUE;
125 g_free(basename_loaded);
127 /* Look also through the list of active plugins. This prevents problems when we have the same
128 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
129 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
130 * the plugin manager would list both plugins. Additionally, unloading the active plugin
131 * would cause a crash. */
132 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
134 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
136 if (utils_str_equal(basename_module, basename_loaded))
138 g_free(basename_loaded);
139 g_free(basename_module);
140 return TRUE;
142 g_free(basename_loaded);
144 g_free(basename_module);
145 return FALSE;
149 static Plugin *find_active_plugin_by_name(const gchar *filename)
151 GList *item;
153 g_return_val_if_fail(filename, FALSE);
155 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
157 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
158 return item->data;
161 return NULL;
165 /* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
166 #define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
168 static gboolean
169 plugin_check_version(Plugin *plugin, int plugin_version_code)
171 GModule *module = plugin->module;
172 if (plugin_version_code < 0)
174 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
175 "release of Geany - please recompile it."), g_module_name(module));
176 geany_debug("Plugin \"%s\" is not binary compatible with this "
177 "release of Geany - recompile it.", g_module_name(module));
178 return FALSE;
180 if (plugin_version_code > GEANY_API_VERSION)
182 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
183 g_module_name(module), plugin_version_code);
184 return FALSE;
186 return TRUE;
190 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
192 PluginCallback *cb;
193 guint i, len = 0;
195 while (TRUE)
197 cb = &callbacks[len];
198 if (!cb->signal_name || !cb->callback)
199 break;
200 len++;
202 if (len == 0)
203 return;
205 for (i = 0; i < len; i++)
207 cb = &callbacks[i];
209 /* Pass the callback data as default user_data if none was set by the plugin itself */
210 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
211 cb->callback, cb->user_data ? cb->user_data : plugin->cb_data);
216 static void read_key_group(Plugin *plugin)
218 GeanyKeyGroupInfo *p_key_info;
219 GeanyKeyGroup **p_key_group;
221 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
222 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
223 if (p_key_info && p_key_group)
225 GeanyKeyGroupInfo *key_info = p_key_info;
227 if (*p_key_group)
228 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
229 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
230 plugin->info.name);
231 else
233 if (key_info->count)
235 GeanyKeyGroup *key_group =
236 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
237 if (key_group)
238 *p_key_group = key_group;
240 else
241 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
242 "count field is zero. Maybe use plugin_set_key_group() instead?",
243 plugin->info.name);
246 else if (p_key_info || p_key_group)
247 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
248 "Maybe use plugin_set_key_group() instead?",
249 plugin->info.name);
253 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
255 const Plugin *pa = a;
256 const Plugin *pb = b;
258 return strcmp(pa->info.name, pb->info.name);
262 /** Register a plugin to Geany.
264 * The plugin will show up in the plugin manager. The user can interact with
265 * it based on the functions it provides and installed GUI elements.
267 * You must initialize the info and funcs fields of @ref GeanyPlugin
268 * appropriately prior to calling this, otherwise registration will fail. For
269 * info at least a valid name must be set (possibly localized). For funcs,
270 * at least init() and cleanup() functions must be implemented and set.
272 * The return value must be checked. It may be FALSE if the plugin failed to register which can
273 * mainly happen for two reasons (future Geany versions may add new failure conditions):
274 * - Not all mandatory fields of GeanyPlugin have been set.
275 * - The ABI or API versions reported by the plugin are incompatible with the running Geany.
277 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
278 * handles @a api_version and @a abi_version.
280 * @param plugin The plugin provided by Geany
281 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
282 * @param min_api_version The minimum API version required by the plugin
283 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
285 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
287 * @since 1.26 (API 225)
288 * @see GEANY_PLUGIN_REGISTER()
290 GEANY_API_SYMBOL
291 gboolean geany_plugin_register(GeanyPlugin *plugin, gint api_version, gint min_api_version,
292 gint abi_version)
294 Plugin *p;
295 GeanyPluginFuncs *cbs = plugin->funcs;
297 g_return_val_if_fail(plugin != NULL, FALSE);
299 p = plugin->priv;
300 /* already registered successfully */
301 g_return_val_if_fail(!PLUGIN_LOADED_OK(p), FALSE);
303 /* Prevent registering incompatible plugins. */
304 if (! plugin_check_version(p, PLUGIN_VERSION_CODE(api_version, abi_version)))
305 return FALSE;
307 /* Only init and cleanup callbacks are truly mandatory. */
308 if (! cbs->init || ! cbs->cleanup)
310 geany_debug("Plugin '%s' has no %s function - ignoring plugin!",
311 g_module_name(p->module), cbs->init ? "cleanup" : "init");
313 else
315 /* Yes, name is checked again later on, however we want return FALSE here
316 * to signal the error back to the plugin (but we don't print the message twice) */
317 if (! EMPTY(p->info.name))
318 p->flags = LOADED_OK;
321 /* If it ever becomes necessary we can save the api version in Plugin
322 * and apply compat code on a per-plugin basis, because we learn about
323 * the requested API version here. For now it's not necessary. */
325 return PLUGIN_LOADED_OK(p);
329 /** Register a plugin to Geany, with plugin-defined data.
331 * This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
332 * Refer to that function for more details on registering in general.
334 * @p pdata is the pointer going to be passed to the individual plugin callbacks
335 * of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
336 * @p pdata, which connects the data to the plugin's module life time.
338 * You cannot use geany_plugin_set_data() after registering with this function. Use
339 * geany_plugin_register() if you need to.
341 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
342 * handles @p api_version and @p abi_version.
344 * @param plugin The plugin provided by Geany.
345 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
346 * @param min_api_version The minimum API version required by the plugin.
347 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
348 * @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
349 * @param free_func Function used to deallocate @a pdata, may be @c NULL.
351 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
353 * @since 1.26 (API 225)
354 * @see GEANY_PLUGIN_REGISTER_FULL()
355 * @see geany_plugin_register()
357 GEANY_API_SYMBOL
358 gboolean geany_plugin_register_full(GeanyPlugin *plugin, gint api_version, gint min_api_version,
359 gint abi_version, gpointer pdata, GDestroyNotify free_func)
361 if (geany_plugin_register(plugin, api_version, min_api_version, abi_version))
363 geany_plugin_set_data(plugin, pdata, free_func);
364 /* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
365 * as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
366 * after GeanyPluginFuncs::cleanup() */
367 plugin->priv->flags |= LOAD_DATA;
368 return TRUE;
370 return FALSE;
373 struct LegacyRealFuncs
375 void (*init) (GeanyData *data);
376 GtkWidget* (*configure) (GtkDialog *dialog);
377 void (*help) (void);
378 void (*cleanup) (void);
381 /* Wrappers to support legacy plugins are below */
382 static gboolean legacy_init(GeanyPlugin *plugin, gpointer pdata)
384 struct LegacyRealFuncs *h = pdata;
385 h->init(plugin->geany_data);
386 return TRUE;
389 static void legacy_cleanup(GeanyPlugin *plugin, gpointer pdata)
391 struct LegacyRealFuncs *h = pdata;
392 /* Can be NULL because it's optional for legacy plugins */
393 if (h->cleanup)
394 h->cleanup();
397 static void legacy_help(GeanyPlugin *plugin, gpointer pdata)
399 struct LegacyRealFuncs *h = pdata;
400 h->help();
403 static GtkWidget *legacy_configure(GeanyPlugin *plugin, GtkDialog *parent, gpointer pdata)
405 struct LegacyRealFuncs *h = pdata;
406 return h->configure(parent);
409 static void free_legacy_cbs(gpointer data)
411 g_slice_free(struct LegacyRealFuncs, data);
414 /* This function is the equivalent of geany_plugin_register() for legacy-style
415 * plugins which we continue to load for the time being. */
416 static void register_legacy_plugin(Plugin *plugin, GModule *module)
418 gint (*p_version_check) (gint abi_version);
419 void (*p_set_info) (PluginInfo *info);
420 void (*p_init) (GeanyData *geany_data);
421 GeanyData **p_geany_data;
422 struct LegacyRealFuncs *h;
424 #define CHECK_FUNC(__x) \
425 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
427 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
428 g_module_name(plugin->module)); \
429 return; \
431 CHECK_FUNC(version_check);
432 CHECK_FUNC(set_info);
433 CHECK_FUNC(init);
434 #undef CHECK_FUNC
436 /* We must verify the version first. If the plugin has become incompatible any
437 * further actions should be considered invalid and therefore skipped. */
438 if (! plugin_check_version(plugin, p_version_check(GEANY_ABI_VERSION)))
439 return;
441 h = g_slice_new(struct LegacyRealFuncs);
443 /* Since the version check passed we can proceed with setting basic fields and
444 * calling its set_info() (which might want to call Geany functions already). */
445 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
446 if (p_geany_data)
447 *p_geany_data = &geany_data;
448 /* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
449 p_set_info(&plugin->info);
451 /* If all went well we can set the remaining callbacks and let it go for good. */
452 h->init = p_init;
453 g_module_symbol(module, "plugin_configure", (void *) &h->configure);
454 g_module_symbol(module, "plugin_configure_single", (void *) &plugin->configure_single);
455 g_module_symbol(module, "plugin_help", (void *) &h->help);
456 g_module_symbol(module, "plugin_cleanup", (void *) &h->cleanup);
457 /* pointer to callbacks struct can be stored directly, no wrapper necessary */
458 g_module_symbol(module, "plugin_callbacks", (void *) &plugin->cbs.callbacks);
459 if (app->debug_mode)
461 if (h->configure && plugin->configure_single)
462 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
463 "only plugin_configure() will be used!",
464 plugin->info.name);
465 if (h->cleanup == NULL)
466 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
467 plugin->info.name);
470 plugin->cbs.init = legacy_init;
471 plugin->cbs.cleanup = legacy_cleanup;
472 plugin->cbs.configure = h->configure ? legacy_configure : NULL;
473 plugin->cbs.help = h->help ? legacy_help : NULL;
475 plugin->flags = LOADED_OK | IS_LEGACY;
476 geany_plugin_set_data(&plugin->public, h, free_legacy_cbs);
480 static gboolean
481 plugin_load(Plugin *plugin)
483 gboolean init_ok = TRUE;
484 /* Start the plugin. Legacy plugins require additional cruft. */
485 if (PLUGIN_IS_LEGACY(plugin))
487 GeanyPlugin **p_geany_plugin;
488 PluginInfo **p_info;
489 PluginFields **plugin_fields;
490 /* set these symbols before plugin_init() is called
491 * we don't set geany_data since it is set directly by plugin_new() */
492 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
493 if (p_geany_plugin)
494 *p_geany_plugin = &plugin->public;
495 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
496 if (p_info)
497 *p_info = &plugin->info;
498 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
499 if (plugin_fields)
500 *plugin_fields = &plugin->fields;
501 read_key_group(plugin);
503 /* Legacy plugin_init() cannot fail. */
504 plugin->cbs.init(&plugin->public, plugin->cb_data);
506 /* now read any plugin-owned data that might have been set in plugin_init() */
507 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
509 ui_add_document_sensitive(plugin->fields.menu_item);
512 else
514 init_ok = plugin->cbs.init(&plugin->public, plugin->cb_data);
517 if (! init_ok)
518 return FALSE;
520 /* new-style plugins set their callbacks in geany_load_module() */
521 if (plugin->cbs.callbacks)
522 add_callbacks(plugin, plugin->cbs.callbacks);
524 /* remember which plugins are active.
525 * keep list sorted so tools menu items and plugin preference tabs are
526 * sorted by plugin name */
527 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
529 geany_debug("Loaded: %s (%s)", plugin->filename, plugin->info.name);
530 return TRUE;
534 /* Load and optionally init a plugin.
535 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
536 * called, the plugin will be started, if not the plugin will be read only (for the list of
537 * available plugins in the plugin manager).
538 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
539 static Plugin*
540 plugin_new(const gchar *fname, gboolean load_plugin, gboolean add_to_list)
542 Plugin *plugin;
543 GModule *module;
544 void (*p_geany_load_module)(GeanyPlugin *);
546 g_return_val_if_fail(fname, NULL);
547 g_return_val_if_fail(g_module_supported(), NULL);
549 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
550 * load the module */
551 plugin = find_active_plugin_by_name(fname);
552 if (plugin != NULL)
554 geany_debug("Plugin \"%s\" already loaded.", fname);
555 if (add_to_list)
557 /* do not add to the list twice */
558 if (g_list_find(plugin_list, plugin) != NULL)
559 return NULL;
561 plugin_list = g_list_prepend(plugin_list, plugin);
563 return plugin;
566 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
567 * causing a segfault. Without that flag the module will safely fail to load.
568 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
569 * otherwise not be detected due to the shadowing of Geany's app variable.
570 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
571 * function from a plugin will be shadowed. */
572 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
573 if (! module)
575 geany_debug("Can't load plugin: %s", g_module_error());
576 return NULL;
579 if (plugin_loaded(module))
581 geany_debug("Plugin \"%s\" already loaded.", fname);
583 if (! g_module_close(module))
584 g_warning("%s: %s", fname, g_module_error());
585 return NULL;
588 plugin = g_new0(Plugin, 1);
589 plugin->module = module;
590 plugin->filename = g_strdup(fname);
591 plugin->public.geany_data = &geany_data;
592 plugin->public.priv = plugin;
593 /* Fields of plugin->info/funcs must to be initialized by the plugin */
594 plugin->public.info = &plugin->info;
595 plugin->public.funcs = &plugin->cbs;
597 g_module_symbol(module, "geany_load_module", (void *) &p_geany_load_module);
598 if (p_geany_load_module)
600 /* This is a new style plugin. It should fill in plugin->info and then call
601 * geany_plugin_register() in its geany_load_module() to successfully load.
602 * The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
603 * We check the LOADED_OK flag separately to protect us against buggy plugins
604 * who ignore the result of geany_plugin_register() and register anyway */
605 p_geany_load_module(&plugin->public);
607 else
609 /* This is the legacy / deprecated code path. It does roughly the same as
610 * geany_load_module() and geany_plugin_register() together for the new ones */
611 register_legacy_plugin(plugin, module);
614 if (! PLUGIN_LOADED_OK(plugin))
616 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname);
617 goto err;
620 if (EMPTY(plugin->info.name))
622 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname);
623 goto err;
626 if (load_plugin && !plugin_load(plugin))
628 /* Handle failing init same as failing to load for now. In future we
629 * could present a informational UI or something */
630 geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname);
631 goto err;
634 if (add_to_list)
635 plugin_list = g_list_prepend(plugin_list, plugin);
637 return plugin;
639 err:
640 if (plugin->cb_data_destroy)
641 plugin->cb_data_destroy(plugin->cb_data);
642 if (! g_module_close(module))
643 g_warning("%s: %s", fname, g_module_error());
644 g_free(plugin->filename);
645 g_free(plugin);
646 return NULL;
650 static void on_object_weak_notify(gpointer data, GObject *old_ptr)
652 Plugin *plugin = data;
653 guint i = 0;
655 g_return_if_fail(plugin && plugin->signal_ids);
657 for (i = 0; i < plugin->signal_ids->len; i++)
659 SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
661 if (sc->object == old_ptr)
663 g_array_remove_index_fast(plugin->signal_ids, i);
664 /* we can break the loop right after finding the first match,
665 * because we will get one notification per connected signal */
666 break;
672 /* add an object to watch for destruction, and release pointers to it when destroyed.
673 * this should only be used by plugin_signal_connect() to add a watch on
674 * the object lifetime and nuke out references to it in plugin->signal_ids */
675 void plugin_watch_object(Plugin *plugin, gpointer object)
677 g_object_weak_ref(object, on_object_weak_notify, plugin);
681 static void remove_callbacks(Plugin *plugin)
683 GArray *signal_ids = plugin->signal_ids;
684 SignalConnection *sc;
686 if (signal_ids == NULL)
687 return;
689 foreach_array(SignalConnection, sc, signal_ids)
691 g_signal_handler_disconnect(sc->object, sc->handler_id);
692 g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
695 g_array_free(signal_ids, TRUE);
699 static void remove_sources(Plugin *plugin)
701 GList *item;
703 item = plugin->sources;
704 while (item != NULL)
706 GList *next = item->next; /* cache the next pointer because current item will be freed */
708 g_source_destroy(item->data);
709 item = next;
711 /* don't free the list here, it is allocated inside each source's data */
715 static gboolean is_active_plugin(Plugin *plugin)
717 return (g_list_find(active_plugin_list, plugin) != NULL);
721 /* Clean up anything used by an active plugin */
722 static void
723 plugin_cleanup(Plugin *plugin)
725 GtkWidget *widget;
727 /* With geany_register_plugin cleanup is mandatory */
728 plugin->cbs.cleanup(&plugin->public, plugin->cb_data);
730 remove_callbacks(plugin);
731 remove_sources(plugin);
733 if (plugin->key_group)
734 keybindings_free_group(plugin->key_group);
736 widget = plugin->toolbar_separator.widget;
737 if (widget)
738 gtk_widget_destroy(widget);
740 if (!PLUGIN_HAS_LOAD_DATA(plugin) && plugin->cb_data_destroy)
742 /* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
743 * if it was already set through geany_plugin_register_full() because we couldn't call
744 * its init() anymore (not without completely reloading it anyway). */
745 plugin->cb_data_destroy(plugin->cb_data);
746 plugin->cb_data = NULL;
747 plugin->cb_data_destroy = NULL;
750 geany_debug("Unloaded: %s", plugin->filename);
754 static void
755 plugin_free(Plugin *plugin)
757 g_return_if_fail(plugin);
758 g_return_if_fail(plugin->module);
760 if (is_active_plugin(plugin))
761 plugin_cleanup(plugin);
763 active_plugin_list = g_list_remove(active_plugin_list, plugin);
764 plugin_list = g_list_remove(plugin_list, plugin);
766 /* cb_data_destroy might be plugin code and must be called before unloading the module */
767 if (plugin->cb_data_destroy)
768 plugin->cb_data_destroy(plugin->cb_data);
770 if (! g_module_close(plugin->module))
771 g_warning("%s: %s", plugin->filename, g_module_error());
773 g_free(plugin->filename);
774 g_free(plugin);
775 plugin = NULL;
779 static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
780 const gchar *plugin_path_system)
782 gchar *plugin_path_custom;
784 if (EMPTY(prefs.custom_plugin_path))
785 return NULL;
787 plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
788 utils_tidy_path(plugin_path_custom);
790 /* check whether the custom plugin path is one of the system or user plugin paths
791 * and abort if so */
792 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
793 utils_str_equal(plugin_path_custom, plugin_path_system))
795 g_free(plugin_path_custom);
796 return NULL;
798 return plugin_path_custom;
802 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
803 * so we need to check active plugins are in the right place before loading */
804 static gboolean check_plugin_path(const gchar *fname)
806 gchar *plugin_path_config;
807 gchar *plugin_path_system;
808 gchar *plugin_path_custom;
809 gboolean ret = FALSE;
811 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
812 if (g_str_has_prefix(fname, plugin_path_config))
813 ret = TRUE;
815 plugin_path_system = get_plugin_path();
816 if (g_str_has_prefix(fname, plugin_path_system))
817 ret = TRUE;
819 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
820 if (plugin_path_custom)
822 if (g_str_has_prefix(fname, plugin_path_custom))
823 ret = TRUE;
825 g_free(plugin_path_custom);
827 g_free(plugin_path_config);
828 g_free(plugin_path_system);
829 return ret;
833 /* load active plugins at startup */
834 static void
835 load_active_plugins(void)
837 guint i, len;
839 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
840 return;
842 for (i = 0; i < len; i++)
844 const gchar *fname = active_plugins_pref[i];
846 if (!EMPTY(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
848 if (!check_plugin_path(fname) || plugin_new(fname, TRUE, FALSE) == NULL)
849 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
855 static void
856 load_plugins_from_path(const gchar *path)
858 GSList *list, *item;
859 gchar *fname, *tmp;
860 gint count = 0;
862 list = utils_get_file_list(path, NULL, NULL);
864 for (item = list; item != NULL; item = g_slist_next(item))
866 tmp = strrchr(item->data, '.');
867 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
868 continue;
870 fname = g_build_filename(path, item->data, NULL);
871 if (plugin_new(fname, FALSE, TRUE))
872 count++;
873 g_free(fname);
876 g_slist_foreach(list, (GFunc) g_free, NULL);
877 g_slist_free(list);
879 if (count)
880 geany_debug("Added %d plugin(s) in '%s'.", count, path);
884 static gchar *get_plugin_path(void)
886 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN));
890 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
891 static void load_all_plugins(void)
893 gchar *plugin_path_config;
894 gchar *plugin_path_system;
895 gchar *plugin_path_custom;
897 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
898 plugin_path_system = get_plugin_path();
900 /* first load plugins in ~/.config/geany/plugins/ */
901 load_plugins_from_path(plugin_path_config);
903 /* load plugins from a custom path */
904 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
905 if (plugin_path_custom)
907 load_plugins_from_path(plugin_path_custom);
908 g_free(plugin_path_custom);
911 /* finally load plugins from $prefix/lib/geany */
912 load_plugins_from_path(plugin_path_system);
914 g_free(plugin_path_config);
915 g_free(plugin_path_system);
919 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
921 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
922 guint i = 0;
923 gboolean have_plugin_menu_items = FALSE;
925 for (item = list; item != NULL; item = g_list_next(item))
927 if (item->data == menu_separator)
929 if (i < g_list_length(list) - 1)
931 have_plugin_menu_items = TRUE;
932 break;
935 i++;
937 g_list_free(list);
939 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
943 /* Calling this starts up plugin support */
944 void plugins_load_active(void)
946 GtkWidget *widget;
948 want_plugins = TRUE;
950 geany_data_init();
952 widget = gtk_separator_menu_item_new();
953 gtk_widget_show(widget);
954 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
956 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
957 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
958 gtk_widget_show(widget);
959 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
961 menu_separator = gtk_separator_menu_item_new();
962 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
963 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
965 load_active_plugins();
969 /* Update the global active plugins list so it's up-to-date when configuration
970 * is saved. Called in response to GeanyObject's "save-settings" signal. */
971 static void update_active_plugins_pref(void)
973 gint i = 0;
974 GList *list;
975 gsize count;
977 /* if plugins are disabled, don't clear list of active plugins */
978 if (!want_plugins)
979 return;
981 count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
983 g_strfreev(active_plugins_pref);
985 if (count == 0)
987 active_plugins_pref = NULL;
988 return;
991 active_plugins_pref = g_new0(gchar*, count + 1);
993 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
995 Plugin *plugin = list->data;
997 active_plugins_pref[i] = g_strdup(plugin->filename);
998 i++;
1000 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
1002 const gchar *fname = list->data;
1004 active_plugins_pref[i] = g_strdup(fname);
1005 i++;
1007 active_plugins_pref[i] = NULL;
1011 /* called even if plugin support is disabled */
1012 void plugins_init(void)
1014 StashGroup *group;
1015 gchar *path;
1017 path = get_plugin_path();
1018 geany_debug("System plugin path: %s", path);
1019 g_free(path);
1021 group = stash_group_new("plugins");
1022 configuration_add_pref_group(group, TRUE);
1024 stash_group_add_toggle_button(group, &prefs.load_plugins,
1025 "load_plugins", TRUE, "check_plugins");
1026 stash_group_add_entry(group, &prefs.custom_plugin_path,
1027 "custom_plugin_path", "", "extra_plugin_path_entry");
1029 g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
1030 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1034 /* called even if plugin support is disabled */
1035 void plugins_finalize(void)
1037 if (failed_plugins_list != NULL)
1039 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1040 g_list_free(failed_plugins_list);
1042 if (active_plugin_list != NULL)
1044 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1045 g_list_free(active_plugin_list);
1047 g_strfreev(active_plugins_pref);
1051 /* Check whether there are any plugins loaded which provide a configure symbol */
1052 gboolean plugins_have_preferences(void)
1054 GList *item;
1056 if (active_plugin_list == NULL)
1057 return FALSE;
1059 foreach_list(item, active_plugin_list)
1061 Plugin *plugin = item->data;
1062 if (plugin->configure_single != NULL || plugin->cbs.configure != NULL)
1063 return TRUE;
1066 return FALSE;
1070 /* Plugin Manager */
1072 enum
1074 PLUGIN_COLUMN_CHECK = 0,
1075 PLUGIN_COLUMN_PLUGIN,
1076 PLUGIN_N_COLUMNS,
1077 PM_BUTTON_KEYBINDINGS,
1078 PM_BUTTON_CONFIGURE,
1079 PM_BUTTON_HELP
1082 typedef struct
1084 GtkWidget *dialog;
1085 GtkWidget *tree;
1086 GtkListStore *store;
1087 GtkWidget *filter_entry;
1088 GtkWidget *configure_button;
1089 GtkWidget *keybindings_button;
1090 GtkWidget *help_button;
1091 GtkWidget *popup_menu;
1092 GtkWidget *popup_configure_menu_item;
1093 GtkWidget *popup_keybindings_menu_item;
1094 GtkWidget *popup_help_menu_item;
1096 PluginManagerWidgets;
1098 static PluginManagerWidgets pm_widgets;
1101 static void pm_update_buttons(Plugin *p)
1103 gboolean has_configure = FALSE;
1104 gboolean has_help = FALSE;
1105 gboolean has_keybindings = FALSE;
1107 if (p != NULL && is_active_plugin(p))
1109 has_configure = p->cbs.configure || p->configure_single;
1110 has_help = p->cbs.help != NULL;
1111 has_keybindings = p->key_group && p->key_group->plugin_key_count;
1114 gtk_widget_set_sensitive(pm_widgets.configure_button, has_configure);
1115 gtk_widget_set_sensitive(pm_widgets.help_button, has_help);
1116 gtk_widget_set_sensitive(pm_widgets.keybindings_button, has_keybindings);
1118 gtk_widget_set_sensitive(pm_widgets.popup_configure_menu_item, has_configure);
1119 gtk_widget_set_sensitive(pm_widgets.popup_help_menu_item, has_help);
1120 gtk_widget_set_sensitive(pm_widgets.popup_keybindings_menu_item, has_keybindings);
1124 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1126 GtkTreeIter iter;
1127 GtkTreeModel *model;
1128 Plugin *p;
1130 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1132 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1134 if (p != NULL)
1135 pm_update_buttons(p);
1140 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1142 gboolean old_state, state;
1143 gchar *file_name;
1144 GtkTreeIter iter;
1145 GtkTreeIter store_iter;
1146 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1147 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1148 Plugin *p;
1150 gtk_tree_model_get_iter(model, &iter, path);
1151 gtk_tree_path_free(path);
1153 gtk_tree_model_get(model, &iter,
1154 PLUGIN_COLUMN_CHECK, &old_state,
1155 PLUGIN_COLUMN_PLUGIN, &p, -1);
1157 /* no plugins item */
1158 if (p == NULL)
1159 return;
1161 gtk_tree_model_filter_convert_iter_to_child_iter(
1162 GTK_TREE_MODEL_FILTER(model), &store_iter, &iter);
1164 state = ! old_state; /* toggle the state */
1166 /* save the filename of the plugin */
1167 file_name = g_strdup(p->filename);
1169 /* unload plugin module */
1170 if (!state)
1171 /* save shortcuts (only need this group, but it doesn't take long) */
1172 keybindings_write_to_file();
1174 plugin_free(p);
1176 /* reload plugin module and initialize it if item is checked */
1177 p = plugin_new(file_name, state, TRUE);
1178 if (!p)
1180 /* plugin file may no longer be on disk, or is now incompatible */
1181 gtk_list_store_remove(pm_widgets.store, &store_iter);
1183 else
1185 if (state)
1186 keybindings_load_keyfile(); /* load shortcuts */
1188 /* update model */
1189 gtk_list_store_set(pm_widgets.store, &store_iter,
1190 PLUGIN_COLUMN_CHECK, state,
1191 PLUGIN_COLUMN_PLUGIN, p, -1);
1193 /* set again the sensitiveness of the configure and help buttons */
1194 pm_update_buttons(p);
1196 g_free(file_name);
1200 static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y,
1201 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
1203 GtkTreeModel *model;
1204 GtkTreeIter iter;
1205 GtkTreePath *path;
1206 Plugin *p = NULL;
1208 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget), &x, &y, keyboard_mode,
1209 &model, &path, &iter))
1210 return FALSE;
1212 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1213 if (p != NULL)
1215 gchar *markup;
1216 gchar *details;
1218 details = g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1219 p->info.version, p->info.author, p->filename);
1220 markup = g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
1221 p->info.name, p->info.description, details);
1223 gtk_tooltip_set_markup(tooltip, markup);
1224 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget), tooltip, path);
1226 g_free(details);
1227 g_free(markup);
1229 gtk_tree_path_free(path);
1231 return p != NULL;
1235 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell,
1236 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1238 Plugin *p;
1240 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1242 if (p == NULL)
1243 g_object_set(cell, "text", _("No plugins available."), NULL);
1244 else
1246 gchar *markup = g_markup_printf_escaped("<b>%s</b>\n%s", p->info.name, p->info.description);
1248 g_object_set(cell, "markup", markup, NULL);
1249 g_free(markup);
1254 static gboolean pm_treeview_button_press_cb(GtkWidget *widget, GdkEventButton *event,
1255 G_GNUC_UNUSED gpointer user_data)
1257 if (event->button == 3)
1259 gtk_menu_popup(GTK_MENU(pm_widgets.popup_menu), NULL, NULL, NULL, NULL,
1260 event->button, event->time);
1262 return FALSE;
1266 static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1267 gpointer user_data)
1269 Plugin *pa, *pb;
1271 gtk_tree_model_get(model, a, PLUGIN_COLUMN_PLUGIN, &pa, -1);
1272 gtk_tree_model_get(model, b, PLUGIN_COLUMN_PLUGIN, &pb, -1);
1274 if (pa && pb)
1275 return strcmp(pa->info.name, pb->info.name);
1276 else
1277 return pa - pb;
1281 static gboolean pm_tree_search(const gchar *key, const gchar *haystack)
1283 gchar *normalized_string = NULL;
1284 gchar *normalized_key = NULL;
1285 gchar *case_normalized_string = NULL;
1286 gchar *case_normalized_key = NULL;
1287 gboolean matched = TRUE;
1289 normalized_string = g_utf8_normalize(haystack, -1, G_NORMALIZE_ALL);
1290 normalized_key = g_utf8_normalize(key, -1, G_NORMALIZE_ALL);
1292 if (normalized_string != NULL && normalized_key != NULL)
1294 GString *stripped_key;
1295 gchar **subkey, **subkeys;
1297 case_normalized_string = g_utf8_casefold(normalized_string, -1);
1298 case_normalized_key = g_utf8_casefold(normalized_key, -1);
1299 stripped_key = g_string_new(case_normalized_key);
1300 do {} while (utils_string_replace_all(stripped_key, " ", " "));
1301 subkeys = g_strsplit(stripped_key->str, " ", -1);
1302 g_string_free(stripped_key, TRUE);
1303 foreach_strv(subkey, subkeys)
1305 if (strstr(case_normalized_string, *subkey) == NULL)
1307 matched = FALSE;
1308 break;
1311 g_strfreev(subkeys);
1314 g_free(normalized_key);
1315 g_free(normalized_string);
1316 g_free(case_normalized_key);
1317 g_free(case_normalized_string);
1319 return matched;
1323 static gboolean pm_tree_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
1325 Plugin *plugin;
1326 gboolean matched;
1327 const gchar *key;
1328 gchar *haystack, *filename;
1330 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &plugin, -1);
1331 key = gtk_entry_get_text(GTK_ENTRY(pm_widgets.filter_entry));
1333 filename = g_path_get_basename(plugin->filename);
1334 haystack = g_strjoin(" ", plugin->info.name, plugin->info.description,
1335 plugin->info.author, filename, NULL);
1336 matched = pm_tree_search(key, haystack);
1337 g_free(haystack);
1338 g_free(filename);
1340 return matched;
1344 static void on_pm_tree_filter_entry_changed_cb(GtkEntry *entry, gpointer user_data)
1346 GtkTreeModel *filter_model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1347 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model));
1351 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos,
1352 GdkEvent *event, gpointer user_data)
1354 if (event->button.button == 1 && icon_pos == GTK_ENTRY_ICON_PRIMARY)
1355 on_pm_tree_filter_entry_changed_cb(entry, user_data);
1359 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1361 GtkCellRenderer *text_renderer, *checkbox_renderer;
1362 GtkTreeViewColumn *column;
1363 GtkTreeModel *filter_model;
1364 GtkTreeIter iter;
1365 GList *list;
1366 GtkTreeSelection *sel;
1368 g_signal_connect(tree, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip), NULL);
1369 gtk_widget_set_has_tooltip(tree, TRUE);
1370 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1372 checkbox_renderer = gtk_cell_renderer_toggle_new();
1373 column = gtk_tree_view_column_new_with_attributes(
1374 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1375 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1376 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1378 text_renderer = gtk_cell_renderer_text_new();
1379 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1380 column = gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer, NULL);
1381 gtk_tree_view_column_set_cell_data_func(column, text_renderer,
1382 pm_treeview_text_cell_data_func, NULL, NULL);
1383 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1385 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1386 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1387 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN,
1388 pm_tree_sort_func, NULL, NULL);
1389 gtk_tree_sortable_set_sort_column_id(
1390 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN, GTK_SORT_ASCENDING);
1392 /* selection handling */
1393 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1394 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1395 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1397 g_signal_connect(tree, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb), NULL);
1399 list = g_list_first(plugin_list);
1400 if (list == NULL)
1402 gtk_list_store_append(store, &iter);
1403 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1404 PLUGIN_COLUMN_PLUGIN, NULL, -1);
1406 else
1408 Plugin *p;
1409 for (; list != NULL; list = list->next)
1411 p = list->data;
1413 gtk_list_store_append(store, &iter);
1414 gtk_list_store_set(store, &iter,
1415 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1416 PLUGIN_COLUMN_PLUGIN, p,
1417 -1);
1420 /* filter */
1421 filter_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(store), NULL);
1422 gtk_tree_model_filter_set_visible_func(
1423 GTK_TREE_MODEL_FILTER(filter_model), pm_tree_filter_func, NULL, NULL);
1425 /* set model to tree view */
1426 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), filter_model);
1427 g_object_unref(store);
1428 g_object_unref(filter_model);
1432 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
1434 GtkTreeModel *model;
1435 GtkTreeSelection *selection;
1436 GtkTreeIter iter;
1437 Plugin *p;
1439 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1440 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1442 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1444 if (p != NULL)
1446 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1447 plugin_show_configure(&p->public);
1448 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP)
1449 p->cbs.help(&p->public, p->cb_data);
1450 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_KEYBINDINGS && p->key_group && p->key_group->plugin_key_count > 0)
1451 keybindings_dialog_show_prefs_scroll(p->info.name);
1457 static void
1458 free_non_active_plugin(gpointer data, gpointer user_data)
1460 Plugin *plugin = data;
1462 /* don't do anything when closing the plugin manager and it is an active plugin */
1463 if (is_active_plugin(plugin))
1464 return;
1466 plugin_free(plugin);
1470 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1471 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1472 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1474 switch (response)
1476 case GTK_RESPONSE_CLOSE:
1477 case GTK_RESPONSE_DELETE_EVENT:
1478 if (plugin_list != NULL)
1480 /* remove all non-active plugins from the list */
1481 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1482 g_list_free(plugin_list);
1483 plugin_list = NULL;
1485 gtk_widget_destroy(GTK_WIDGET(dialog));
1487 configuration_save();
1488 break;
1489 case PM_BUTTON_CONFIGURE:
1490 case PM_BUTTON_HELP:
1491 case PM_BUTTON_KEYBINDINGS:
1492 /* forward event to the generic handler */
1493 pm_on_plugin_button_clicked(NULL, GINT_TO_POINTER(response));
1494 break;
1499 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1501 GtkWidget *vbox, *vbox2, *swin, *label, *menu_item, *filter_entry;
1503 /* before showing the dialog, we need to create the list of available plugins */
1504 load_all_plugins();
1506 pm_widgets.dialog = gtk_dialog_new();
1507 gtk_window_set_title(GTK_WINDOW(pm_widgets.dialog), _("Plugins"));
1508 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets.dialog), GTK_WINDOW(main_widgets.window));
1509 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets.dialog), TRUE);
1511 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1512 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1513 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1515 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1517 pm_widgets.help_button = gtk_dialog_add_button(
1518 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_HELP, PM_BUTTON_HELP);
1519 pm_widgets.configure_button = gtk_dialog_add_button(
1520 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_PREFERENCES, PM_BUTTON_CONFIGURE);
1521 pm_widgets.keybindings_button = gtk_dialog_add_button(
1522 GTK_DIALOG(pm_widgets.dialog), _("Keybindings"), PM_BUTTON_KEYBINDINGS);
1523 gtk_dialog_add_button(GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1524 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets.dialog), GTK_RESPONSE_CLOSE);
1526 /* filter */
1527 pm_widgets.filter_entry = filter_entry = gtk_entry_new();
1528 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
1529 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
1530 g_signal_connect(filter_entry, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb), NULL);
1531 g_signal_connect(filter_entry, "icon-release",
1532 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb), NULL);
1534 /* prepare treeview */
1535 pm_widgets.tree = gtk_tree_view_new();
1536 pm_widgets.store = gtk_list_store_new(
1537 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_POINTER);
1538 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1540 swin = gtk_scrolled_window_new(NULL, NULL);
1541 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1542 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1543 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1544 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1546 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1548 /* plugin popup menu */
1549 pm_widgets.popup_menu = gtk_menu_new();
1551 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
1552 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1553 g_signal_connect(menu_item, "activate",
1554 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1555 pm_widgets.popup_configure_menu_item = menu_item;
1557 menu_item = gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1558 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1559 g_signal_connect(menu_item, "activate",
1560 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS));
1561 pm_widgets.popup_keybindings_menu_item = menu_item;
1563 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
1564 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1565 g_signal_connect(menu_item, "activate",
1566 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1567 pm_widgets.popup_help_menu_item = menu_item;
1569 /* put it together */
1570 vbox2 = gtk_vbox_new(FALSE, 6);
1571 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
1572 gtk_box_pack_start(GTK_BOX(vbox2), filter_entry, FALSE, FALSE, 0);
1573 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1575 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1577 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
1578 gtk_widget_show_all(pm_widgets.dialog);
1579 gtk_widget_show_all(pm_widgets.popup_menu);
1581 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
1582 pm_update_buttons(NULL);
1583 gtk_widget_grab_focus(pm_widgets.filter_entry);
1587 #endif