plugins: refactor GtkListStore population code into separate function
[geany-mirror.git] / src / plugins.c
blob6640e15a74a1171da190407d5bdfe204d7635f47
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 typedef struct {
79 gchar extension[8];
80 Plugin *plugin; /* &builtin_so_proxy_plugin for native plugins */
81 } PluginProxy;
84 static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, const gchar *filename, gpointer pdata);
85 static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata);
87 static Plugin builtin_so_proxy_plugin = {
88 .proxy_cbs = {
89 .load = plugin_load_gmodule,
90 .unload = plugin_unload_gmodule,
92 /* rest of Plugin can be NULL/0 */
95 static PluginProxy builtin_so_proxy = {
96 .extension = G_MODULE_SUFFIX,
97 .plugin = &builtin_so_proxy_plugin,
100 static GPtrArray *active_proxies = NULL;
102 static GeanyData geany_data;
104 static void
105 geany_data_init(void)
107 GeanyData gd = {
108 app,
109 &main_widgets,
110 documents_array,
111 filetypes_array,
112 &prefs,
113 &interface_prefs,
114 &toolbar_prefs,
115 &editor_prefs,
116 &file_prefs,
117 &search_prefs,
118 &tool_prefs,
119 &template_prefs,
120 &build_info,
121 filetypes_by_title
124 geany_data = gd;
128 /* Prevent the same plugin filename being loaded more than once.
129 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
130 static gboolean
131 plugin_loaded(Plugin *plugin)
133 gchar *basename_module, *basename_loaded;
134 GList *item;
136 basename_module = g_path_get_basename(plugin->filename);
137 for (item = plugin_list; item != NULL; item = g_list_next(item))
139 basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
141 if (utils_str_equal(basename_module, basename_loaded))
143 g_free(basename_loaded);
144 g_free(basename_module);
145 return TRUE;
147 g_free(basename_loaded);
149 /* Look also through the list of active plugins. This prevents problems when we have the same
150 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
151 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
152 * the plugin manager would list both plugins. Additionally, unloading the active plugin
153 * would cause a crash. */
154 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
156 basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
158 if (utils_str_equal(basename_module, basename_loaded))
160 g_free(basename_loaded);
161 g_free(basename_module);
162 return TRUE;
164 g_free(basename_loaded);
166 g_free(basename_module);
167 return FALSE;
171 static Plugin *find_active_plugin_by_name(const gchar *filename)
173 GList *item;
175 g_return_val_if_fail(filename, FALSE);
177 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
179 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
180 return item->data;
183 return NULL;
187 /* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
188 #define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
190 static gboolean
191 plugin_check_version(Plugin *plugin, int plugin_version_code)
193 gboolean ret = TRUE;
194 if (plugin_version_code < 0)
196 gchar *name = g_path_get_basename(plugin->filename);
197 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
198 "release of Geany - please recompile it."), name);
199 geany_debug("Plugin \"%s\" is not binary compatible with this "
200 "release of Geany - recompile it.", name);
201 ret = FALSE;
202 g_free(name);
204 else if (plugin_version_code > GEANY_API_VERSION)
206 gchar *name = g_path_get_basename(plugin->filename);
207 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
208 name, plugin_version_code);
209 ret = FALSE;
210 g_free(name);
213 return ret;
217 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
219 PluginCallback *cb;
220 guint i, len = 0;
222 while (TRUE)
224 cb = &callbacks[len];
225 if (!cb->signal_name || !cb->callback)
226 break;
227 len++;
229 if (len == 0)
230 return;
232 for (i = 0; i < len; i++)
234 cb = &callbacks[i];
236 /* Pass the callback data as default user_data if none was set by the plugin itself */
237 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
238 cb->callback, cb->user_data ? cb->user_data : plugin->cb_data);
243 static void read_key_group(Plugin *plugin)
245 GeanyKeyGroupInfo *p_key_info;
246 GeanyKeyGroup **p_key_group;
247 GModule *module = plugin->proxy_data;
249 g_module_symbol(module, "plugin_key_group_info", (void *) &p_key_info);
250 g_module_symbol(module, "plugin_key_group", (void *) &p_key_group);
251 if (p_key_info && p_key_group)
253 GeanyKeyGroupInfo *key_info = p_key_info;
255 if (*p_key_group)
256 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
257 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
258 plugin->info.name);
259 else
261 if (key_info->count)
263 GeanyKeyGroup *key_group =
264 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
265 if (key_group)
266 *p_key_group = key_group;
268 else
269 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
270 "count field is zero. Maybe use plugin_set_key_group() instead?",
271 plugin->info.name);
274 else if (p_key_info || p_key_group)
275 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
276 "Maybe use plugin_set_key_group() instead?",
277 plugin->info.name);
281 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
283 const Plugin *pa = a;
284 const Plugin *pb = b;
286 return strcmp(pa->info.name, pb->info.name);
290 /** Register a plugin to Geany.
292 * The plugin will show up in the plugin manager. The user can interact with
293 * it based on the functions it provides and installed GUI elements.
295 * You must initialize the info and funcs fields of @ref GeanyPlugin
296 * appropriately prior to calling this, otherwise registration will fail. For
297 * info at least a valid name must be set (possibly localized). For funcs,
298 * at least init() and cleanup() functions must be implemented and set.
300 * The return value must be checked. It may be FALSE if the plugin failed to register which can
301 * mainly happen for two reasons (future Geany versions may add new failure conditions):
302 * - Not all mandatory fields of GeanyPlugin have been set.
303 * - The ABI or API versions reported by the plugin are incompatible with the running Geany.
305 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
306 * handles @a api_version and @a abi_version.
308 * @param plugin The plugin provided by Geany
309 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
310 * @param min_api_version The minimum API version required by the plugin
311 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
313 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
315 * @since 1.26 (API 225)
316 * @see GEANY_PLUGIN_REGISTER()
318 GEANY_API_SYMBOL
319 gboolean geany_plugin_register(GeanyPlugin *plugin, gint api_version, gint min_api_version,
320 gint abi_version)
322 Plugin *p;
323 GeanyPluginFuncs *cbs = plugin->funcs;
325 g_return_val_if_fail(plugin != NULL, FALSE);
327 p = plugin->priv;
328 /* already registered successfully */
329 g_return_val_if_fail(!PLUGIN_LOADED_OK(p), FALSE);
331 /* Prevent registering incompatible plugins. */
332 if (! plugin_check_version(p, PLUGIN_VERSION_CODE(api_version, abi_version)))
333 return FALSE;
335 /* Only init and cleanup callbacks are truly mandatory. */
336 if (! cbs->init || ! cbs->cleanup)
338 gchar *name = g_path_get_basename(p->filename);
339 geany_debug("Plugin '%s' has no %s function - ignoring plugin!", name,
340 cbs->init ? "cleanup" : "init");
341 g_free(name);
343 else
345 /* Yes, name is checked again later on, however we want return FALSE here
346 * to signal the error back to the plugin (but we don't print the message twice) */
347 if (! EMPTY(p->info.name))
348 p->flags = LOADED_OK;
351 /* If it ever becomes necessary we can save the api version in Plugin
352 * and apply compat code on a per-plugin basis, because we learn about
353 * the requested API version here. For now it's not necessary. */
355 return PLUGIN_LOADED_OK(p);
359 /** Register a plugin to Geany, with plugin-defined data.
361 * This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
362 * Refer to that function for more details on registering in general.
364 * @p pdata is the pointer going to be passed to the individual plugin callbacks
365 * of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
366 * @p pdata, which connects the data to the plugin's module life time.
368 * You cannot use geany_plugin_set_data() after registering with this function. Use
369 * geany_plugin_register() if you need to.
371 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
372 * handles @p api_version and @p abi_version.
374 * @param plugin The plugin provided by Geany.
375 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
376 * @param min_api_version The minimum API version required by the plugin.
377 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
378 * @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
379 * @param free_func Function used to deallocate @a pdata, may be @c NULL.
381 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
383 * @since 1.26 (API 225)
384 * @see GEANY_PLUGIN_REGISTER_FULL()
385 * @see geany_plugin_register()
387 GEANY_API_SYMBOL
388 gboolean geany_plugin_register_full(GeanyPlugin *plugin, gint api_version, gint min_api_version,
389 gint abi_version, gpointer pdata, GDestroyNotify free_func)
391 if (geany_plugin_register(plugin, api_version, min_api_version, abi_version))
393 geany_plugin_set_data(plugin, pdata, free_func);
394 /* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
395 * as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
396 * after GeanyPluginFuncs::cleanup() */
397 plugin->priv->flags |= LOAD_DATA;
398 return TRUE;
400 return FALSE;
403 struct LegacyRealFuncs
405 void (*init) (GeanyData *data);
406 GtkWidget* (*configure) (GtkDialog *dialog);
407 void (*help) (void);
408 void (*cleanup) (void);
411 /* Wrappers to support legacy plugins are below */
412 static gboolean legacy_init(GeanyPlugin *plugin, gpointer pdata)
414 struct LegacyRealFuncs *h = pdata;
415 h->init(plugin->geany_data);
416 return TRUE;
419 static void legacy_cleanup(GeanyPlugin *plugin, gpointer pdata)
421 struct LegacyRealFuncs *h = pdata;
422 /* Can be NULL because it's optional for legacy plugins */
423 if (h->cleanup)
424 h->cleanup();
427 static void legacy_help(GeanyPlugin *plugin, gpointer pdata)
429 struct LegacyRealFuncs *h = pdata;
430 h->help();
433 static GtkWidget *legacy_configure(GeanyPlugin *plugin, GtkDialog *parent, gpointer pdata)
435 struct LegacyRealFuncs *h = pdata;
436 return h->configure(parent);
439 static void free_legacy_cbs(gpointer data)
441 g_slice_free(struct LegacyRealFuncs, data);
444 /* This function is the equivalent of geany_plugin_register() for legacy-style
445 * plugins which we continue to load for the time being. */
446 static void register_legacy_plugin(Plugin *plugin, GModule *module)
448 gint (*p_version_check) (gint abi_version);
449 void (*p_set_info) (PluginInfo *info);
450 void (*p_init) (GeanyData *geany_data);
451 GeanyData **p_geany_data;
452 struct LegacyRealFuncs *h;
454 #define CHECK_FUNC(__x) \
455 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
457 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
458 g_module_name(module)); \
459 return; \
461 CHECK_FUNC(version_check);
462 CHECK_FUNC(set_info);
463 CHECK_FUNC(init);
464 #undef CHECK_FUNC
466 /* We must verify the version first. If the plugin has become incompatible any
467 * further actions should be considered invalid and therefore skipped. */
468 if (! plugin_check_version(plugin, p_version_check(GEANY_ABI_VERSION)))
469 return;
471 h = g_slice_new(struct LegacyRealFuncs);
473 /* Since the version check passed we can proceed with setting basic fields and
474 * calling its set_info() (which might want to call Geany functions already). */
475 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
476 if (p_geany_data)
477 *p_geany_data = &geany_data;
478 /* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
479 p_set_info(&plugin->info);
481 /* If all went well we can set the remaining callbacks and let it go for good. */
482 h->init = p_init;
483 g_module_symbol(module, "plugin_configure", (void *) &h->configure);
484 g_module_symbol(module, "plugin_configure_single", (void *) &plugin->configure_single);
485 g_module_symbol(module, "plugin_help", (void *) &h->help);
486 g_module_symbol(module, "plugin_cleanup", (void *) &h->cleanup);
487 /* pointer to callbacks struct can be stored directly, no wrapper necessary */
488 g_module_symbol(module, "plugin_callbacks", (void *) &plugin->cbs.callbacks);
489 if (app->debug_mode)
491 if (h->configure && plugin->configure_single)
492 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
493 "only plugin_configure() will be used!",
494 plugin->info.name);
495 if (h->cleanup == NULL)
496 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
497 plugin->info.name);
500 plugin->cbs.init = legacy_init;
501 plugin->cbs.cleanup = legacy_cleanup;
502 plugin->cbs.configure = h->configure ? legacy_configure : NULL;
503 plugin->cbs.help = h->help ? legacy_help : NULL;
505 plugin->flags = LOADED_OK | IS_LEGACY;
506 geany_plugin_set_data(&plugin->public, h, free_legacy_cbs);
510 static gboolean
511 plugin_load(Plugin *plugin)
513 gboolean init_ok = TRUE;
515 /* Start the plugin. Legacy plugins require additional cruft. */
516 if (PLUGIN_IS_LEGACY(plugin) && plugin->proxy == &builtin_so_proxy_plugin)
518 GeanyPlugin **p_geany_plugin;
519 PluginInfo **p_info;
520 PluginFields **plugin_fields;
521 GModule *module = plugin->proxy_data;
522 /* set these symbols before plugin_init() is called
523 * we don't set geany_data since it is set directly by plugin_new() */
524 g_module_symbol(module, "geany_plugin", (void *) &p_geany_plugin);
525 if (p_geany_plugin)
526 *p_geany_plugin = &plugin->public;
527 g_module_symbol(module, "plugin_info", (void *) &p_info);
528 if (p_info)
529 *p_info = &plugin->info;
530 g_module_symbol(module, "plugin_fields", (void *) &plugin_fields);
531 if (plugin_fields)
532 *plugin_fields = &plugin->fields;
533 read_key_group(plugin);
535 /* Legacy plugin_init() cannot fail. */
536 plugin->cbs.init(&plugin->public, plugin->cb_data);
538 /* now read any plugin-owned data that might have been set in plugin_init() */
539 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
541 ui_add_document_sensitive(plugin->fields.menu_item);
544 else
546 init_ok = plugin->cbs.init(&plugin->public, plugin->cb_data);
549 if (! init_ok)
550 return FALSE;
552 /* new-style plugins set their callbacks in geany_load_module() */
553 if (plugin->cbs.callbacks)
554 add_callbacks(plugin, plugin->cbs.callbacks);
556 /* remember which plugins are active.
557 * keep list sorted so tools menu items and plugin preference tabs are
558 * sorted by plugin name */
559 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
561 geany_debug("Loaded: %s (%s)", plugin->filename, plugin->info.name);
562 return TRUE;
566 static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, const gchar *fname, gpointer pdata)
568 GModule *module;
569 void (*p_geany_load_module)(GeanyPlugin *);
571 g_return_val_if_fail(g_module_supported(), NULL);
572 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
573 * causing a segfault. Without that flag the module will safely fail to load.
574 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
575 * otherwise not be detected due to the shadowing of Geany's app variable.
576 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
577 * function from a plugin will be shadowed. */
578 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
579 if (!module)
581 geany_debug("Can't load plugin: %s", g_module_error());
582 return NULL;
585 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
586 g_module_symbol(module, "geany_load_module", (void *) &p_geany_load_module);
587 if (p_geany_load_module)
589 /* This is a new style plugin. It should fill in plugin->info and then call
590 * geany_plugin_register() in its geany_load_module() to successfully load.
591 * The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
592 * We check the LOADED_OK flag separately to protect us against buggy plugins
593 * who ignore the result of geany_plugin_register() and register anyway */
594 p_geany_load_module(subplugin);
596 else
598 /* This is the legacy / deprecated code path. It does roughly the same as
599 * geany_load_module() and geany_plugin_register() together for the new ones */
600 register_legacy_plugin(subplugin->priv, module);
602 /* We actually check the LOADED_OK flag later */
603 return module;
607 static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, gpointer load_data, gpointer pdata)
609 GModule *module = (GModule *) load_data;
611 g_return_if_fail(module != NULL);
613 if (! g_module_close(module))
614 g_warning("%s: %s", subplugin->priv->filename, g_module_error());
618 /* Load and optionally init a plugin.
619 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
620 * called, the plugin will be started, if not the plugin will be read only (for the list of
621 * available plugins in the plugin manager).
622 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
623 static Plugin*
624 plugin_new(Plugin *proxy, const gchar *fname, gboolean load_plugin, gboolean add_to_list)
626 Plugin *plugin;
628 g_return_val_if_fail(fname, NULL);
629 g_return_val_if_fail(proxy, NULL);
631 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
632 * load the module */
633 plugin = find_active_plugin_by_name(fname);
634 if (plugin != NULL)
636 geany_debug("Plugin \"%s\" already loaded.", fname);
637 if (add_to_list)
639 /* do not add to the list twice */
640 if (g_list_find(plugin_list, plugin) != NULL)
641 return NULL;
643 plugin_list = g_list_prepend(plugin_list, plugin);
645 return plugin;
648 plugin = g_new0(Plugin, 1);
649 plugin->filename = g_strdup(fname);
650 plugin->proxy = proxy;
651 plugin->public.geany_data = &geany_data;
652 plugin->public.priv = plugin;
653 /* Fields of plugin->info/funcs must to be initialized by the plugin */
654 plugin->public.info = &plugin->info;
655 plugin->public.funcs = &plugin->cbs;
657 if (plugin_loaded(plugin))
659 geany_debug("Plugin \"%s\" already loaded.", fname);
660 goto err;
663 /* Load plugin, this should read its name etc. It must also call
664 * geany_plugin_register() for the following PLUGIN_LOADED_OK condition */
665 plugin->proxy_data = proxy->proxy_cbs.load(&proxy->public, &plugin->public, fname, proxy->cb_data);
667 if (! PLUGIN_LOADED_OK(plugin))
669 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname);
670 goto err;
673 /* The proxy assumes success, therefore we have to call unload from here
674 * on in case of errors */
675 if (EMPTY(plugin->info.name))
677 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname);
678 goto err_unload;
681 /* cb_data_destroy() frees plugin->cb_data. If that pointer also passed to unload() afterwards
682 * then that would become a use-after-free. Disallow this combination. If a proxy
683 * needs the same pointer it must not use a destroy func but free manually in its unload(). */
684 if (plugin->proxy_data == proxy->cb_data && plugin->cb_data_destroy)
686 geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname);
687 plugin->proxy_data = NULL;
688 goto err_unload;
691 if (load_plugin && !plugin_load(plugin))
693 /* Handle failing init same as failing to load for now. In future we
694 * could present a informational UI or something */
695 geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname);
696 goto err_unload;
699 if (add_to_list)
700 plugin_list = g_list_prepend(plugin_list, plugin);
702 return plugin;
704 err_unload:
705 if (plugin->cb_data_destroy)
706 plugin->cb_data_destroy(plugin->cb_data);
707 proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
708 err:
709 g_free(plugin->filename);
710 g_free(plugin);
711 return NULL;
715 static void on_object_weak_notify(gpointer data, GObject *old_ptr)
717 Plugin *plugin = data;
718 guint i = 0;
720 g_return_if_fail(plugin && plugin->signal_ids);
722 for (i = 0; i < plugin->signal_ids->len; i++)
724 SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
726 if (sc->object == old_ptr)
728 g_array_remove_index_fast(plugin->signal_ids, i);
729 /* we can break the loop right after finding the first match,
730 * because we will get one notification per connected signal */
731 break;
737 /* add an object to watch for destruction, and release pointers to it when destroyed.
738 * this should only be used by plugin_signal_connect() to add a watch on
739 * the object lifetime and nuke out references to it in plugin->signal_ids */
740 void plugin_watch_object(Plugin *plugin, gpointer object)
742 g_object_weak_ref(object, on_object_weak_notify, plugin);
746 static void remove_callbacks(Plugin *plugin)
748 GArray *signal_ids = plugin->signal_ids;
749 SignalConnection *sc;
751 if (signal_ids == NULL)
752 return;
754 foreach_array(SignalConnection, sc, signal_ids)
756 g_signal_handler_disconnect(sc->object, sc->handler_id);
757 g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
760 g_array_free(signal_ids, TRUE);
764 static void remove_sources(Plugin *plugin)
766 GList *item;
768 item = plugin->sources;
769 while (item != NULL)
771 GList *next = item->next; /* cache the next pointer because current item will be freed */
773 g_source_destroy(item->data);
774 item = next;
776 /* don't free the list here, it is allocated inside each source's data */
780 /* Make the GModule backing plugin resident (if it's GModule-backed at all) */
781 void plugin_make_resident(Plugin *plugin)
783 if (plugin->proxy == &builtin_so_proxy_plugin)
785 g_return_if_fail(plugin->proxy_data != NULL);
786 g_module_make_resident(plugin->proxy_data);
788 else
789 g_warning("Skipping g_module_make_resident() for non-native plugin");
793 /* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
794 gpointer plugin_get_module_symbol(Plugin *plugin, const gchar *sym)
796 gpointer symbol;
798 if (plugin->proxy == &builtin_so_proxy_plugin)
800 g_return_val_if_fail(plugin->proxy_data != NULL, NULL);
801 if (g_module_symbol(plugin->proxy_data, sym, &symbol))
802 return symbol;
803 else
804 g_warning("Failed to locate signal handler for '%s': %s",
805 sym, g_module_error());
807 else /* TODO: Could possibly support this via a new proxy hook */
808 g_warning("Failed to locate signal handler for '%s': Not supported for non-native plugins",
809 sym);
810 return NULL;
814 static gboolean is_active_plugin(Plugin *plugin)
816 return (g_list_find(active_plugin_list, plugin) != NULL);
820 /* Clean up anything used by an active plugin */
821 static void
822 plugin_cleanup(Plugin *plugin)
824 GtkWidget *widget;
826 /* With geany_register_plugin cleanup is mandatory */
827 plugin->cbs.cleanup(&plugin->public, plugin->cb_data);
829 remove_callbacks(plugin);
830 remove_sources(plugin);
832 if (plugin->key_group)
833 keybindings_free_group(plugin->key_group);
835 widget = plugin->toolbar_separator.widget;
836 if (widget)
837 gtk_widget_destroy(widget);
839 if (!PLUGIN_HAS_LOAD_DATA(plugin) && plugin->cb_data_destroy)
841 /* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
842 * if it was already set through geany_plugin_register_full() because we couldn't call
843 * its init() anymore (not without completely reloading it anyway). */
844 plugin->cb_data_destroy(plugin->cb_data);
845 plugin->cb_data = NULL;
846 plugin->cb_data_destroy = NULL;
849 geany_debug("Unloaded: %s", plugin->filename);
853 static void
854 plugin_free(Plugin *plugin)
856 Plugin *proxy;
858 g_return_if_fail(plugin);
859 g_return_if_fail(plugin->proxy);
861 proxy = plugin->proxy;
862 if (is_active_plugin(plugin))
863 plugin_cleanup(plugin);
865 active_plugin_list = g_list_remove(active_plugin_list, plugin);
866 plugin_list = g_list_remove(plugin_list, plugin);
868 /* cb_data_destroy might be plugin code and must be called before unloading the module. */
869 if (plugin->cb_data_destroy)
870 plugin->cb_data_destroy(plugin->cb_data);
871 proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
873 g_free(plugin->filename);
874 g_free(plugin);
875 plugin = NULL;
879 static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
880 const gchar *plugin_path_system)
882 gchar *plugin_path_custom;
884 if (EMPTY(prefs.custom_plugin_path))
885 return NULL;
887 plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
888 utils_tidy_path(plugin_path_custom);
890 /* check whether the custom plugin path is one of the system or user plugin paths
891 * and abort if so */
892 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
893 utils_str_equal(plugin_path_custom, plugin_path_system))
895 g_free(plugin_path_custom);
896 return NULL;
898 return plugin_path_custom;
902 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
903 * so we need to check active plugins are in the right place before loading */
904 static gboolean check_plugin_path(const gchar *fname)
906 gchar *plugin_path_config;
907 gchar *plugin_path_system;
908 gchar *plugin_path_custom;
909 gboolean ret = FALSE;
911 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
912 if (g_str_has_prefix(fname, plugin_path_config))
913 ret = TRUE;
915 plugin_path_system = get_plugin_path();
916 if (g_str_has_prefix(fname, plugin_path_system))
917 ret = TRUE;
919 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
920 if (plugin_path_custom)
922 if (g_str_has_prefix(fname, plugin_path_custom))
923 ret = TRUE;
925 g_free(plugin_path_custom);
927 g_free(plugin_path_config);
928 g_free(plugin_path_system);
929 return ret;
933 /* Retuns NULL if this ain't a plugin,
934 * otherwise it returns the appropriate PluginProxy instance to load it */
935 static PluginProxy* is_plugin(const gchar *file)
937 PluginProxy *proxy;
938 const gchar *ext;
939 guint i;
941 /* extract file extension to avoid g_str_has_suffix() in the loop */
942 ext = (const gchar *)strrchr(file, '.');
943 if (ext == NULL)
944 return FALSE;
945 /* ensure the dot is really part of the filename */
946 else if (strchr(ext, G_DIR_SEPARATOR) != NULL)
947 return FALSE;
949 ext += 1;
950 /* O(n*m), (m being extensions per proxy) doesn't scale very well in theory
951 * but not a problem in practice yet */
952 foreach_ptr_array(proxy, i, active_proxies)
954 if (utils_str_casecmp(ext, proxy->extension) == 0)
956 return proxy;
959 return NULL;
963 /* load active plugins at startup */
964 static void
965 load_active_plugins(void)
967 guint i, len;
969 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
970 return;
972 for (i = 0; i < len; i++)
974 const gchar *fname = active_plugins_pref[i];
976 #ifdef G_OS_WIN32
977 /* ensure we have canonical paths */
978 gchar *p = fname;
979 while ((p = strchr(p, '/')) != NULL)
980 *p = G_DIR_SEPARATOR;
981 #endif
983 if (!EMPTY(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
985 PluginProxy *proxy = NULL;
986 if (check_plugin_path(fname))
987 proxy = is_plugin(fname);
988 if (proxy == NULL || plugin_new(proxy->plugin, fname, TRUE, FALSE) == NULL)
989 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
995 static void
996 load_plugins_from_path(const gchar *path)
998 GSList *list, *item;
999 gint count = 0;
1001 list = utils_get_file_list(path, NULL, NULL);
1003 for (item = list; item != NULL; item = g_slist_next(item))
1005 gchar *fname = g_build_filename(path, item->data, NULL);
1006 PluginProxy *proxy = is_plugin(fname);
1008 if (proxy != NULL && plugin_new(proxy->plugin, fname, FALSE, TRUE))
1009 count++;
1011 g_free(fname);
1014 g_slist_foreach(list, (GFunc) g_free, NULL);
1015 g_slist_free(list);
1017 if (count)
1018 geany_debug("Added %d plugin(s) in '%s'.", count, path);
1022 static gchar *get_plugin_path(void)
1024 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN));
1028 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1029 static void load_all_plugins(void)
1031 gchar *plugin_path_config;
1032 gchar *plugin_path_system;
1033 gchar *plugin_path_custom;
1035 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
1036 plugin_path_system = get_plugin_path();
1038 /* first load plugins in ~/.config/geany/plugins/ */
1039 load_plugins_from_path(plugin_path_config);
1041 /* load plugins from a custom path */
1042 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
1043 if (plugin_path_custom)
1045 load_plugins_from_path(plugin_path_custom);
1046 g_free(plugin_path_custom);
1049 /* finally load plugins from $prefix/lib/geany */
1050 load_plugins_from_path(plugin_path_system);
1052 g_free(plugin_path_config);
1053 g_free(plugin_path_system);
1057 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
1059 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
1060 guint i = 0;
1061 gboolean have_plugin_menu_items = FALSE;
1063 for (item = list; item != NULL; item = g_list_next(item))
1065 if (item->data == menu_separator)
1067 if (i < g_list_length(list) - 1)
1069 have_plugin_menu_items = TRUE;
1070 break;
1073 i++;
1075 g_list_free(list);
1077 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
1081 /* Calling this starts up plugin support */
1082 void plugins_load_active(void)
1084 GtkWidget *widget;
1086 want_plugins = TRUE;
1088 geany_data_init();
1090 widget = gtk_separator_menu_item_new();
1091 gtk_widget_show(widget);
1092 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1094 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1095 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1096 gtk_widget_show(widget);
1097 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
1099 menu_separator = gtk_separator_menu_item_new();
1100 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
1101 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
1103 load_active_plugins();
1107 /* Update the global active plugins list so it's up-to-date when configuration
1108 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1109 static void update_active_plugins_pref(void)
1111 gint i = 0;
1112 GList *list;
1113 gsize count;
1115 /* if plugins are disabled, don't clear list of active plugins */
1116 if (!want_plugins)
1117 return;
1119 count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
1121 g_strfreev(active_plugins_pref);
1123 if (count == 0)
1125 active_plugins_pref = NULL;
1126 return;
1129 active_plugins_pref = g_new0(gchar*, count + 1);
1131 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
1133 Plugin *plugin = list->data;
1135 active_plugins_pref[i] = g_strdup(plugin->filename);
1136 i++;
1138 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
1140 const gchar *fname = list->data;
1142 active_plugins_pref[i] = g_strdup(fname);
1143 i++;
1145 active_plugins_pref[i] = NULL;
1149 /* called even if plugin support is disabled */
1150 void plugins_init(void)
1152 StashGroup *group;
1153 gchar *path;
1155 path = get_plugin_path();
1156 geany_debug("System plugin path: %s", path);
1157 g_free(path);
1159 group = stash_group_new("plugins");
1160 configuration_add_pref_group(group, TRUE);
1162 stash_group_add_toggle_button(group, &prefs.load_plugins,
1163 "load_plugins", TRUE, "check_plugins");
1164 stash_group_add_entry(group, &prefs.custom_plugin_path,
1165 "custom_plugin_path", "", "extra_plugin_path_entry");
1167 g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
1168 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1170 active_proxies = g_ptr_array_sized_new(1);
1171 g_ptr_array_add(active_proxies, &builtin_so_proxy);
1175 /* called even if plugin support is disabled */
1176 void plugins_finalize(void)
1178 if (failed_plugins_list != NULL)
1180 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1181 g_list_free(failed_plugins_list);
1183 if (active_plugin_list != NULL)
1185 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1186 g_list_free(active_plugin_list);
1188 g_strfreev(active_plugins_pref);
1192 /* Check whether there are any plugins loaded which provide a configure symbol */
1193 gboolean plugins_have_preferences(void)
1195 GList *item;
1197 if (active_plugin_list == NULL)
1198 return FALSE;
1200 foreach_list(item, active_plugin_list)
1202 Plugin *plugin = item->data;
1203 if (plugin->configure_single != NULL || plugin->cbs.configure != NULL)
1204 return TRUE;
1207 return FALSE;
1211 /* Plugin Manager */
1213 enum
1215 PLUGIN_COLUMN_CHECK = 0,
1216 PLUGIN_COLUMN_PLUGIN,
1217 PLUGIN_N_COLUMNS,
1218 PM_BUTTON_KEYBINDINGS,
1219 PM_BUTTON_CONFIGURE,
1220 PM_BUTTON_HELP
1223 typedef struct
1225 GtkWidget *dialog;
1226 GtkWidget *tree;
1227 GtkListStore *store;
1228 GtkWidget *filter_entry;
1229 GtkWidget *configure_button;
1230 GtkWidget *keybindings_button;
1231 GtkWidget *help_button;
1232 GtkWidget *popup_menu;
1233 GtkWidget *popup_configure_menu_item;
1234 GtkWidget *popup_keybindings_menu_item;
1235 GtkWidget *popup_help_menu_item;
1237 PluginManagerWidgets;
1239 static PluginManagerWidgets pm_widgets;
1242 static void pm_update_buttons(Plugin *p)
1244 gboolean has_configure = FALSE;
1245 gboolean has_help = FALSE;
1246 gboolean has_keybindings = FALSE;
1248 if (p != NULL && is_active_plugin(p))
1250 has_configure = p->cbs.configure || p->configure_single;
1251 has_help = p->cbs.help != NULL;
1252 has_keybindings = p->key_group && p->key_group->plugin_key_count;
1255 gtk_widget_set_sensitive(pm_widgets.configure_button, has_configure);
1256 gtk_widget_set_sensitive(pm_widgets.help_button, has_help);
1257 gtk_widget_set_sensitive(pm_widgets.keybindings_button, has_keybindings);
1259 gtk_widget_set_sensitive(pm_widgets.popup_configure_menu_item, has_configure);
1260 gtk_widget_set_sensitive(pm_widgets.popup_help_menu_item, has_help);
1261 gtk_widget_set_sensitive(pm_widgets.popup_keybindings_menu_item, has_keybindings);
1265 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1267 GtkTreeIter iter;
1268 GtkTreeModel *model;
1269 Plugin *p;
1271 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1273 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1275 if (p != NULL)
1276 pm_update_buttons(p);
1281 static void pm_populate(GtkListStore *store);
1284 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1286 gboolean old_state, state;
1287 gchar *file_name;
1288 GtkTreeIter iter;
1289 GtkTreeIter store_iter;
1290 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1291 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1292 Plugin *p;
1293 Plugin *proxy;
1295 gtk_tree_model_get_iter(model, &iter, path);
1296 gtk_tree_path_free(path);
1298 gtk_tree_model_get(model, &iter,
1299 PLUGIN_COLUMN_CHECK, &old_state,
1300 PLUGIN_COLUMN_PLUGIN, &p, -1);
1302 /* no plugins item */
1303 if (p == NULL)
1304 return;
1306 gtk_tree_model_filter_convert_iter_to_child_iter(
1307 GTK_TREE_MODEL_FILTER(model), &store_iter, &iter);
1309 state = ! old_state; /* toggle the state */
1311 /* save the filename and proxy of the plugin */
1312 file_name = g_strdup(p->filename);
1313 proxy = p->proxy;
1315 /* unload plugin module */
1316 if (!state)
1317 /* save shortcuts (only need this group, but it doesn't take long) */
1318 keybindings_write_to_file();
1320 plugin_free(p);
1322 /* reload plugin module and initialize it if item is checked */
1323 p = plugin_new(proxy, file_name, state, TRUE);
1324 if (!p)
1326 /* plugin file may no longer be on disk, or is now incompatible */
1327 gtk_list_store_remove(pm_widgets.store, &store_iter);
1329 else
1331 if (state)
1332 keybindings_load_keyfile(); /* load shortcuts */
1334 /* update model */
1335 gtk_list_store_set(pm_widgets.store, &store_iter,
1336 PLUGIN_COLUMN_CHECK, state,
1337 PLUGIN_COLUMN_PLUGIN, p, -1);
1339 /* set again the sensitiveness of the configure and help buttons */
1340 pm_update_buttons(p);
1342 g_free(file_name);
1345 static void pm_populate(GtkListStore *store)
1347 GtkTreeIter iter;
1348 GList *list;
1350 gtk_list_store_clear(store);
1351 list = g_list_first(plugin_list);
1352 if (list == NULL)
1354 gtk_list_store_append(store, &iter);
1355 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1356 PLUGIN_COLUMN_PLUGIN, NULL, -1);
1358 else
1360 for (; list != NULL; list = list->next)
1362 Plugin *p = list->data;
1364 gtk_list_store_append(store, &iter);
1365 gtk_list_store_set(store, &iter,
1366 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1367 PLUGIN_COLUMN_PLUGIN, p,
1368 -1);
1373 static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y,
1374 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
1376 GtkTreeModel *model;
1377 GtkTreeIter iter;
1378 GtkTreePath *path;
1379 Plugin *p = NULL;
1381 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget), &x, &y, keyboard_mode,
1382 &model, &path, &iter))
1383 return FALSE;
1385 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1386 if (p != NULL)
1388 gchar *markup;
1389 gchar *details;
1391 details = g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1392 p->info.version, p->info.author, p->filename);
1393 markup = g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
1394 p->info.name, p->info.description, details);
1396 gtk_tooltip_set_markup(tooltip, markup);
1397 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget), tooltip, path);
1399 g_free(details);
1400 g_free(markup);
1402 gtk_tree_path_free(path);
1404 return p != NULL;
1408 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell,
1409 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1411 Plugin *p;
1413 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1415 if (p == NULL)
1416 g_object_set(cell, "text", _("No plugins available."), NULL);
1417 else
1419 gchar *markup = g_markup_printf_escaped("<b>%s</b>\n%s", p->info.name, p->info.description);
1421 g_object_set(cell, "markup", markup, NULL);
1422 g_free(markup);
1427 static gboolean pm_treeview_button_press_cb(GtkWidget *widget, GdkEventButton *event,
1428 G_GNUC_UNUSED gpointer user_data)
1430 if (event->button == 3)
1432 gtk_menu_popup(GTK_MENU(pm_widgets.popup_menu), NULL, NULL, NULL, NULL,
1433 event->button, event->time);
1435 return FALSE;
1439 static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1440 gpointer user_data)
1442 Plugin *pa, *pb;
1444 gtk_tree_model_get(model, a, PLUGIN_COLUMN_PLUGIN, &pa, -1);
1445 gtk_tree_model_get(model, b, PLUGIN_COLUMN_PLUGIN, &pb, -1);
1447 if (pa && pb)
1448 return strcmp(pa->info.name, pb->info.name);
1449 else
1450 return pa - pb;
1454 static gboolean pm_tree_search(const gchar *key, const gchar *haystack)
1456 gchar *normalized_string = NULL;
1457 gchar *normalized_key = NULL;
1458 gchar *case_normalized_string = NULL;
1459 gchar *case_normalized_key = NULL;
1460 gboolean matched = TRUE;
1462 normalized_string = g_utf8_normalize(haystack, -1, G_NORMALIZE_ALL);
1463 normalized_key = g_utf8_normalize(key, -1, G_NORMALIZE_ALL);
1465 if (normalized_string != NULL && normalized_key != NULL)
1467 GString *stripped_key;
1468 gchar **subkey, **subkeys;
1470 case_normalized_string = g_utf8_casefold(normalized_string, -1);
1471 case_normalized_key = g_utf8_casefold(normalized_key, -1);
1472 stripped_key = g_string_new(case_normalized_key);
1473 do {} while (utils_string_replace_all(stripped_key, " ", " "));
1474 subkeys = g_strsplit(stripped_key->str, " ", -1);
1475 g_string_free(stripped_key, TRUE);
1476 foreach_strv(subkey, subkeys)
1478 if (strstr(case_normalized_string, *subkey) == NULL)
1480 matched = FALSE;
1481 break;
1484 g_strfreev(subkeys);
1487 g_free(normalized_key);
1488 g_free(normalized_string);
1489 g_free(case_normalized_key);
1490 g_free(case_normalized_string);
1492 return matched;
1496 static gboolean pm_tree_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
1498 Plugin *plugin;
1499 gboolean matched;
1500 const gchar *key;
1501 gchar *haystack, *filename;
1503 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &plugin, -1);
1504 key = gtk_entry_get_text(GTK_ENTRY(pm_widgets.filter_entry));
1506 filename = g_path_get_basename(plugin->filename);
1507 haystack = g_strjoin(" ", plugin->info.name, plugin->info.description,
1508 plugin->info.author, filename, NULL);
1509 matched = pm_tree_search(key, haystack);
1510 g_free(haystack);
1511 g_free(filename);
1513 return matched;
1517 static void on_pm_tree_filter_entry_changed_cb(GtkEntry *entry, gpointer user_data)
1519 GtkTreeModel *filter_model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1520 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model));
1524 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos,
1525 GdkEvent *event, gpointer user_data)
1527 if (event->button.button == 1 && icon_pos == GTK_ENTRY_ICON_PRIMARY)
1528 on_pm_tree_filter_entry_changed_cb(entry, user_data);
1532 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1534 GtkCellRenderer *text_renderer, *checkbox_renderer;
1535 GtkTreeViewColumn *column;
1536 GtkTreeModel *filter_model;
1537 GtkTreeSelection *sel;
1539 g_signal_connect(tree, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip), NULL);
1540 gtk_widget_set_has_tooltip(tree, TRUE);
1541 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1543 checkbox_renderer = gtk_cell_renderer_toggle_new();
1544 column = gtk_tree_view_column_new_with_attributes(
1545 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1546 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1547 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1549 text_renderer = gtk_cell_renderer_text_new();
1550 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1551 column = gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer, NULL);
1552 gtk_tree_view_column_set_cell_data_func(column, text_renderer,
1553 pm_treeview_text_cell_data_func, NULL, NULL);
1554 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1556 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1557 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1558 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN,
1559 pm_tree_sort_func, NULL, NULL);
1560 gtk_tree_sortable_set_sort_column_id(
1561 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN, GTK_SORT_ASCENDING);
1563 /* selection handling */
1564 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1565 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1566 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1568 g_signal_connect(tree, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb), NULL);
1570 /* filter */
1571 filter_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(store), NULL);
1572 gtk_tree_model_filter_set_visible_func(
1573 GTK_TREE_MODEL_FILTER(filter_model), pm_tree_filter_func, NULL, NULL);
1575 /* set model to tree view */
1576 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), filter_model);
1577 g_object_unref(filter_model);
1579 pm_populate(store);
1583 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
1585 GtkTreeModel *model;
1586 GtkTreeSelection *selection;
1587 GtkTreeIter iter;
1588 Plugin *p;
1590 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1591 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1593 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1595 if (p != NULL)
1597 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1598 plugin_show_configure(&p->public);
1599 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP)
1600 p->cbs.help(&p->public, p->cb_data);
1601 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_KEYBINDINGS && p->key_group && p->key_group->plugin_key_count > 0)
1602 keybindings_dialog_show_prefs_scroll(p->info.name);
1608 static void
1609 free_non_active_plugin(gpointer data, gpointer user_data)
1611 Plugin *plugin = data;
1613 /* don't do anything when closing the plugin manager and it is an active plugin */
1614 if (is_active_plugin(plugin))
1615 return;
1617 plugin_free(plugin);
1621 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1622 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1623 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1625 switch (response)
1627 case GTK_RESPONSE_CLOSE:
1628 case GTK_RESPONSE_DELETE_EVENT:
1629 if (plugin_list != NULL)
1631 /* remove all non-active plugins from the list */
1632 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1633 g_list_free(plugin_list);
1634 plugin_list = NULL;
1636 gtk_widget_destroy(GTK_WIDGET(dialog));
1638 configuration_save();
1639 break;
1640 case PM_BUTTON_CONFIGURE:
1641 case PM_BUTTON_HELP:
1642 case PM_BUTTON_KEYBINDINGS:
1643 /* forward event to the generic handler */
1644 pm_on_plugin_button_clicked(NULL, GINT_TO_POINTER(response));
1645 break;
1650 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1652 GtkWidget *vbox, *vbox2, *swin, *label, *menu_item, *filter_entry;
1654 /* before showing the dialog, we need to create the list of available plugins */
1655 load_all_plugins();
1657 pm_widgets.dialog = gtk_dialog_new();
1658 gtk_window_set_title(GTK_WINDOW(pm_widgets.dialog), _("Plugins"));
1659 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets.dialog), GTK_WINDOW(main_widgets.window));
1660 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets.dialog), TRUE);
1662 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1663 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1664 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1666 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1668 pm_widgets.help_button = gtk_dialog_add_button(
1669 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_HELP, PM_BUTTON_HELP);
1670 pm_widgets.configure_button = gtk_dialog_add_button(
1671 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_PREFERENCES, PM_BUTTON_CONFIGURE);
1672 pm_widgets.keybindings_button = gtk_dialog_add_button(
1673 GTK_DIALOG(pm_widgets.dialog), _("Keybindings"), PM_BUTTON_KEYBINDINGS);
1674 gtk_dialog_add_button(GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1675 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets.dialog), GTK_RESPONSE_CLOSE);
1677 /* filter */
1678 pm_widgets.filter_entry = filter_entry = gtk_entry_new();
1679 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
1680 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
1681 g_signal_connect(filter_entry, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb), NULL);
1682 g_signal_connect(filter_entry, "icon-release",
1683 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb), NULL);
1685 /* prepare treeview */
1686 pm_widgets.tree = gtk_tree_view_new();
1687 pm_widgets.store = gtk_list_store_new(
1688 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_POINTER);
1689 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1690 g_object_unref(pm_widgets.store);
1692 swin = gtk_scrolled_window_new(NULL, NULL);
1693 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1694 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1695 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1696 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1698 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1700 /* plugin popup menu */
1701 pm_widgets.popup_menu = gtk_menu_new();
1703 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
1704 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1705 g_signal_connect(menu_item, "activate",
1706 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1707 pm_widgets.popup_configure_menu_item = menu_item;
1709 menu_item = gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1710 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1711 g_signal_connect(menu_item, "activate",
1712 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS));
1713 pm_widgets.popup_keybindings_menu_item = menu_item;
1715 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
1716 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1717 g_signal_connect(menu_item, "activate",
1718 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1719 pm_widgets.popup_help_menu_item = menu_item;
1721 /* put it together */
1722 vbox2 = gtk_vbox_new(FALSE, 6);
1723 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
1724 gtk_box_pack_start(GTK_BOX(vbox2), filter_entry, FALSE, FALSE, 0);
1725 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1727 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1729 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
1730 gtk_widget_show_all(pm_widgets.dialog);
1731 gtk_widget_show_all(pm_widgets.popup_menu);
1733 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
1734 pm_update_buttons(NULL);
1735 gtk_widget_grab_focus(pm_widgets.filter_entry);
1739 #endif