Merge pull request #464 from eht16/undeprecate_plugins
[geany-mirror.git] / src / plugins.c
blob3b724ac00b411733aca36840f919db55ae24612c
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 static gboolean
166 plugin_check_version(GModule *module)
168 gint (*version_check)(gint) = NULL;
170 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
172 if (G_UNLIKELY(! version_check))
174 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
175 g_module_name(module));
176 return FALSE;
178 else
180 gint result = version_check(GEANY_ABI_VERSION);
182 if (result < 0)
184 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
185 "release of Geany - please recompile it."), g_module_name(module));
186 geany_debug("Plugin \"%s\" is not binary compatible with this "
187 "release of Geany - recompile it.", g_module_name(module));
188 return FALSE;
190 if (result > GEANY_API_VERSION)
192 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
193 g_module_name(module), result);
194 return FALSE;
197 return TRUE;
201 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
203 PluginCallback *cb;
204 guint i, len = 0;
206 while (TRUE)
208 cb = &callbacks[len];
209 if (!cb->signal_name || !cb->callback)
210 break;
211 len++;
213 if (len == 0)
214 return;
216 for (i = 0; i < len; i++)
218 cb = &callbacks[i];
220 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
221 cb->callback, cb->user_data);
226 static void read_key_group(Plugin *plugin)
228 GeanyKeyGroupInfo *p_key_info;
229 GeanyKeyGroup **p_key_group;
231 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
232 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
233 if (p_key_info && p_key_group)
235 GeanyKeyGroupInfo *key_info = p_key_info;
237 if (*p_key_group)
238 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
239 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
240 plugin->info.name);
241 else
243 if (key_info->count)
245 GeanyKeyGroup *key_group =
246 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
247 if (key_group)
248 *p_key_group = key_group;
250 else
251 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
252 "count field is zero. Maybe use plugin_set_key_group() instead?",
253 plugin->info.name);
256 else if (p_key_info || p_key_group)
257 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
258 "Maybe use plugin_set_key_group() instead?",
259 plugin->info.name);
263 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
265 const Plugin *pa = a;
266 const Plugin *pb = b;
268 return strcmp(pa->info.name, pb->info.name);
272 static void
273 plugin_load(Plugin *plugin)
275 GeanyPlugin **p_geany_plugin;
276 PluginCallback *callbacks;
277 PluginInfo **p_info;
278 PluginFields **plugin_fields;
280 /* set these symbols before plugin_init() is called
281 * we don't set geany_data since it is set directly by plugin_new() */
282 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
283 if (p_geany_plugin)
284 *p_geany_plugin = &plugin->public;
285 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
286 if (p_info)
287 *p_info = &plugin->info;
288 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
289 if (plugin_fields)
290 *plugin_fields = &plugin->fields;
291 read_key_group(plugin);
293 /* start the plugin */
294 g_return_if_fail(plugin->init);
295 plugin->init(&geany_data);
297 /* store some function pointers for later use */
298 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
299 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
300 if (app->debug_mode && plugin->configure && plugin->configure_single)
301 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
302 "only plugin_configure() will be used!",
303 plugin->info.name);
305 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
306 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
307 if (plugin->cleanup == NULL)
309 if (app->debug_mode)
310 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
311 plugin->info.name);
314 /* now read any plugin-owned data that might have been set in plugin_init() */
316 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
318 ui_add_document_sensitive(plugin->fields.menu_item);
321 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
322 if (callbacks)
323 add_callbacks(plugin, callbacks);
325 /* remember which plugins are active.
326 * keep list sorted so tools menu items and plugin preference tabs are
327 * sorted by plugin name */
328 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
330 geany_debug("Loaded: %s (%s)", plugin->filename,
331 FALLBACK(plugin->info.name, "<Unknown>"));
335 /* Load and optionally init a plugin.
336 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
337 * called, the plugin will be started, if not the plugin will be read only (for the list of
338 * available plugins in the plugin manager).
339 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
340 static Plugin*
341 plugin_new(const gchar *fname, gboolean load_plugin, gboolean add_to_list)
343 Plugin *plugin;
344 GModule *module;
345 GeanyData **p_geany_data;
346 void (*plugin_set_info)(PluginInfo*);
348 g_return_val_if_fail(fname, NULL);
349 g_return_val_if_fail(g_module_supported(), NULL);
351 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
352 * load the module */
353 plugin = find_active_plugin_by_name(fname);
354 if (plugin != NULL)
356 geany_debug("Plugin \"%s\" already loaded.", fname);
357 if (add_to_list)
359 /* do not add to the list twice */
360 if (g_list_find(plugin_list, plugin) != NULL)
361 return NULL;
363 plugin_list = g_list_prepend(plugin_list, plugin);
365 return plugin;
368 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
369 * causing a segfault. Without that flag the module will safely fail to load.
370 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
371 * otherwise not be detected due to the shadowing of Geany's app variable.
372 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
373 * function from a plugin will be shadowed. */
374 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
375 if (! module)
377 geany_debug("Can't load plugin: %s", g_module_error());
378 return NULL;
381 if (plugin_loaded(module))
383 geany_debug("Plugin \"%s\" already loaded.", fname);
385 if (! g_module_close(module))
386 g_warning("%s: %s", fname, g_module_error());
387 return NULL;
390 if (! plugin_check_version(module))
392 if (! g_module_close(module))
393 g_warning("%s: %s", fname, g_module_error());
394 return NULL;
397 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
398 if (plugin_set_info == NULL)
400 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
402 if (! g_module_close(module))
403 g_warning("%s: %s", fname, g_module_error());
404 return NULL;
407 plugin = g_new0(Plugin, 1);
409 /* set basic fields here to allow plugins to call Geany functions in set_info() */
410 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
411 if (p_geany_data)
412 *p_geany_data = &geany_data;
414 /* read plugin name, etc. */
415 plugin_set_info(&plugin->info);
416 if (G_UNLIKELY(EMPTY(plugin->info.name)))
418 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
419 fname);
421 if (! g_module_close(module))
422 g_warning("%s: %s", fname, g_module_error());
423 g_free(plugin);
424 return NULL;
427 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
428 if (plugin->init == NULL)
430 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
431 plugin->info.name);
433 if (! g_module_close(module))
434 g_warning("%s: %s", fname, g_module_error());
435 g_free(plugin);
436 return NULL;
438 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
440 plugin->filename = g_strdup(fname);
441 plugin->module = module;
442 plugin->public.info = &plugin->info;
443 plugin->public.priv = plugin;
445 if (load_plugin)
446 plugin_load(plugin);
448 if (add_to_list)
449 plugin_list = g_list_prepend(plugin_list, plugin);
451 return plugin;
455 static void on_object_weak_notify(gpointer data, GObject *old_ptr)
457 Plugin *plugin = data;
458 guint i = 0;
460 g_return_if_fail(plugin && plugin->signal_ids);
462 for (i = 0; i < plugin->signal_ids->len; i++)
464 SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
466 if (sc->object == old_ptr)
468 g_array_remove_index_fast(plugin->signal_ids, i);
469 /* we can break the loop right after finding the first match,
470 * because we will get one notification per connected signal */
471 break;
477 /* add an object to watch for destruction, and release pointers to it when destroyed.
478 * this should only be used by plugin_signal_connect() to add a watch on
479 * the object lifetime and nuke out references to it in plugin->signal_ids */
480 void plugin_watch_object(Plugin *plugin, gpointer object)
482 g_object_weak_ref(object, on_object_weak_notify, plugin);
486 static void remove_callbacks(Plugin *plugin)
488 GArray *signal_ids = plugin->signal_ids;
489 SignalConnection *sc;
491 if (signal_ids == NULL)
492 return;
494 foreach_array(SignalConnection, sc, signal_ids)
496 g_signal_handler_disconnect(sc->object, sc->handler_id);
497 g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
500 g_array_free(signal_ids, TRUE);
504 static void remove_sources(Plugin *plugin)
506 GList *item;
508 item = plugin->sources;
509 while (item != NULL)
511 GList *next = item->next; /* cache the next pointer because current item will be freed */
513 g_source_destroy(item->data);
514 item = next;
516 /* don't free the list here, it is allocated inside each source's data */
520 static gboolean is_active_plugin(Plugin *plugin)
522 return (g_list_find(active_plugin_list, plugin) != NULL);
526 /* Clean up anything used by an active plugin */
527 static void
528 plugin_cleanup(Plugin *plugin)
530 GtkWidget *widget;
532 if (plugin->cleanup)
533 plugin->cleanup();
535 remove_callbacks(plugin);
536 remove_sources(plugin);
538 if (plugin->key_group)
539 keybindings_free_group(plugin->key_group);
541 widget = plugin->toolbar_separator.widget;
542 if (widget)
543 gtk_widget_destroy(widget);
545 geany_debug("Unloaded: %s", plugin->filename);
549 static void
550 plugin_free(Plugin *plugin)
552 g_return_if_fail(plugin);
553 g_return_if_fail(plugin->module);
555 if (is_active_plugin(plugin))
556 plugin_cleanup(plugin);
558 active_plugin_list = g_list_remove(active_plugin_list, plugin);
560 if (! g_module_close(plugin->module))
561 g_warning("%s: %s", plugin->filename, g_module_error());
563 plugin_list = g_list_remove(plugin_list, plugin);
565 g_free(plugin->filename);
566 g_free(plugin);
567 plugin = NULL;
571 static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
572 const gchar *plugin_path_system)
574 gchar *plugin_path_custom;
576 if (EMPTY(prefs.custom_plugin_path))
577 return NULL;
579 plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
580 utils_tidy_path(plugin_path_custom);
582 /* check whether the custom plugin path is one of the system or user plugin paths
583 * and abort if so */
584 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
585 utils_str_equal(plugin_path_custom, plugin_path_system))
587 g_free(plugin_path_custom);
588 return NULL;
590 return plugin_path_custom;
594 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
595 * so we need to check active plugins are in the right place before loading */
596 static gboolean check_plugin_path(const gchar *fname)
598 gchar *plugin_path_config;
599 gchar *plugin_path_system;
600 gchar *plugin_path_custom;
601 gboolean ret = FALSE;
603 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
604 if (g_str_has_prefix(fname, plugin_path_config))
605 ret = TRUE;
607 plugin_path_system = get_plugin_path();
608 if (g_str_has_prefix(fname, plugin_path_system))
609 ret = TRUE;
611 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
612 if (plugin_path_custom)
614 if (g_str_has_prefix(fname, plugin_path_custom))
615 ret = TRUE;
617 g_free(plugin_path_custom);
619 g_free(plugin_path_config);
620 g_free(plugin_path_system);
621 return ret;
625 /* load active plugins at startup */
626 static void
627 load_active_plugins(void)
629 guint i, len;
631 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
632 return;
634 for (i = 0; i < len; i++)
636 const gchar *fname = active_plugins_pref[i];
638 if (!EMPTY(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
640 if (!check_plugin_path(fname) || plugin_new(fname, TRUE, FALSE) == NULL)
641 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
647 static void
648 load_plugins_from_path(const gchar *path)
650 GSList *list, *item;
651 gchar *fname, *tmp;
652 gint count = 0;
654 list = utils_get_file_list(path, NULL, NULL);
656 for (item = list; item != NULL; item = g_slist_next(item))
658 tmp = strrchr(item->data, '.');
659 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
660 continue;
662 fname = g_build_filename(path, item->data, NULL);
663 if (plugin_new(fname, FALSE, TRUE))
664 count++;
665 g_free(fname);
668 g_slist_foreach(list, (GFunc) g_free, NULL);
669 g_slist_free(list);
671 if (count)
672 geany_debug("Added %d plugin(s) in '%s'.", count, path);
676 static gchar *get_plugin_path(void)
678 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN));
682 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
683 static void load_all_plugins(void)
685 gchar *plugin_path_config;
686 gchar *plugin_path_system;
687 gchar *plugin_path_custom;
689 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
690 plugin_path_system = get_plugin_path();
692 /* first load plugins in ~/.config/geany/plugins/ */
693 load_plugins_from_path(plugin_path_config);
695 /* load plugins from a custom path */
696 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
697 if (plugin_path_custom)
699 load_plugins_from_path(plugin_path_custom);
700 g_free(plugin_path_custom);
703 /* finally load plugins from $prefix/lib/geany */
704 load_plugins_from_path(plugin_path_system);
706 g_free(plugin_path_config);
707 g_free(plugin_path_system);
711 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
713 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
714 guint i = 0;
715 gboolean have_plugin_menu_items = FALSE;
717 for (item = list; item != NULL; item = g_list_next(item))
719 if (item->data == menu_separator)
721 if (i < g_list_length(list) - 1)
723 have_plugin_menu_items = TRUE;
724 break;
727 i++;
729 g_list_free(list);
731 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
735 /* Calling this starts up plugin support */
736 void plugins_load_active(void)
738 GtkWidget *widget;
740 want_plugins = TRUE;
742 geany_data_init();
744 widget = gtk_separator_menu_item_new();
745 gtk_widget_show(widget);
746 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
748 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
749 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
750 gtk_widget_show(widget);
751 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
753 menu_separator = gtk_separator_menu_item_new();
754 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
755 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
757 load_active_plugins();
761 /* Update the global active plugins list so it's up-to-date when configuration
762 * is saved. Called in response to GeanyObject's "save-settings" signal. */
763 static void update_active_plugins_pref(void)
765 gint i = 0;
766 GList *list;
767 gsize count;
769 /* if plugins are disabled, don't clear list of active plugins */
770 if (!want_plugins)
771 return;
773 count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
775 g_strfreev(active_plugins_pref);
777 if (count == 0)
779 active_plugins_pref = NULL;
780 return;
783 active_plugins_pref = g_new0(gchar*, count + 1);
785 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
787 Plugin *plugin = list->data;
789 active_plugins_pref[i] = g_strdup(plugin->filename);
790 i++;
792 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
794 const gchar *fname = list->data;
796 active_plugins_pref[i] = g_strdup(fname);
797 i++;
799 active_plugins_pref[i] = NULL;
803 /* called even if plugin support is disabled */
804 void plugins_init(void)
806 StashGroup *group;
807 gchar *path;
809 path = get_plugin_path();
810 geany_debug("System plugin path: %s", path);
811 g_free(path);
813 group = stash_group_new("plugins");
814 configuration_add_pref_group(group, TRUE);
816 stash_group_add_toggle_button(group, &prefs.load_plugins,
817 "load_plugins", TRUE, "check_plugins");
818 stash_group_add_entry(group, &prefs.custom_plugin_path,
819 "custom_plugin_path", "", "extra_plugin_path_entry");
821 g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
822 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
826 /* called even if plugin support is disabled */
827 void plugins_finalize(void)
829 if (failed_plugins_list != NULL)
831 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
832 g_list_free(failed_plugins_list);
834 if (active_plugin_list != NULL)
836 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
837 g_list_free(active_plugin_list);
839 g_strfreev(active_plugins_pref);
843 /* Check whether there are any plugins loaded which provide a configure symbol */
844 gboolean plugins_have_preferences(void)
846 GList *item;
848 if (active_plugin_list == NULL)
849 return FALSE;
851 foreach_list(item, active_plugin_list)
853 Plugin *plugin = item->data;
854 if (plugin->configure != NULL || plugin->configure_single != NULL)
855 return TRUE;
858 return FALSE;
862 /* Plugin Manager */
864 enum
866 PLUGIN_COLUMN_CHECK = 0,
867 PLUGIN_COLUMN_PLUGIN,
868 PLUGIN_N_COLUMNS,
869 PM_BUTTON_KEYBINDINGS,
870 PM_BUTTON_CONFIGURE,
871 PM_BUTTON_HELP
874 typedef struct
876 GtkWidget *dialog;
877 GtkWidget *tree;
878 GtkListStore *store;
879 GtkWidget *configure_button;
880 GtkWidget *keybindings_button;
881 GtkWidget *help_button;
883 PluginManagerWidgets;
885 static PluginManagerWidgets pm_widgets;
888 static void pm_update_buttons(Plugin *p)
890 gboolean is_active;
892 is_active = is_active_plugin(p);
893 gtk_widget_set_sensitive(pm_widgets.configure_button,
894 (p->configure || p->configure_single) && is_active);
895 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
896 gtk_widget_set_sensitive(pm_widgets.keybindings_button,
897 p->key_group && p->key_group->plugin_key_count > 0 && is_active);
901 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
903 GtkTreeIter iter;
904 GtkTreeModel *model;
905 Plugin *p;
907 if (gtk_tree_selection_get_selected(selection, &model, &iter))
909 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
911 if (p != NULL)
912 pm_update_buttons(p);
917 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
919 gboolean old_state, state;
920 gchar *file_name;
921 GtkTreeIter iter;
922 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
923 Plugin *p;
925 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
926 gtk_tree_path_free(path);
928 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
929 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
931 /* no plugins item */
932 if (p == NULL)
933 return;
935 state = ! old_state; /* toggle the state */
937 /* save the filename of the plugin */
938 file_name = g_strdup(p->filename);
940 /* unload plugin module */
941 if (!state)
942 /* save shortcuts (only need this group, but it doesn't take long) */
943 keybindings_write_to_file();
945 plugin_free(p);
947 /* reload plugin module and initialize it if item is checked */
948 p = plugin_new(file_name, state, TRUE);
949 if (!p)
951 /* plugin file may no longer be on disk, or is now incompatible */
952 gtk_list_store_remove(pm_widgets.store, &iter);
954 else
956 if (state)
957 keybindings_load_keyfile(); /* load shortcuts */
959 /* update model */
960 gtk_list_store_set(pm_widgets.store, &iter,
961 PLUGIN_COLUMN_CHECK, state,
962 PLUGIN_COLUMN_PLUGIN, p, -1);
964 /* set again the sensitiveness of the configure and help buttons */
965 pm_update_buttons(p);
967 g_free(file_name);
971 static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y,
972 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
974 GtkTreeModel *model;
975 GtkTreeIter iter;
976 GtkTreePath *path;
977 Plugin *p = NULL;
979 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget), &x, &y, keyboard_mode,
980 &model, &path, &iter))
981 return FALSE;
983 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
984 if (p != NULL)
986 gchar *markup;
987 gchar *details;
989 details = g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
990 p->info.version, p->info.author, p->filename);
991 markup = g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
992 p->info.name, p->info.description, details);
994 gtk_tooltip_set_markup(tooltip, markup);
995 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget), tooltip, path);
997 g_free(details);
998 g_free(markup);
1000 gtk_tree_path_free(path);
1002 return p != NULL;
1006 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell,
1007 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1009 Plugin *p;
1011 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1013 if (p == NULL)
1014 g_object_set(cell, "text", _("No plugins available."), NULL);
1015 else
1017 gchar *markup = g_markup_printf_escaped("<b>%s</b>\n%s", p->info.name, p->info.description);
1019 g_object_set(cell, "markup", markup, NULL);
1020 g_free(markup);
1025 static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1026 gpointer user_data)
1028 Plugin *pa, *pb;
1030 gtk_tree_model_get(model, a, PLUGIN_COLUMN_PLUGIN, &pa, -1);
1031 gtk_tree_model_get(model, b, PLUGIN_COLUMN_PLUGIN, &pb, -1);
1033 if (pa && pb)
1034 return strcmp(pa->info.name, pb->info.name);
1035 else
1036 return pa - pb;
1040 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1042 GtkCellRenderer *text_renderer, *checkbox_renderer;
1043 GtkTreeViewColumn *column;
1044 GtkTreeIter iter;
1045 GList *list;
1046 GtkTreeSelection *sel;
1048 g_signal_connect(tree, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip), NULL);
1049 gtk_widget_set_has_tooltip(tree, TRUE);
1050 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1052 checkbox_renderer = gtk_cell_renderer_toggle_new();
1053 column = gtk_tree_view_column_new_with_attributes(
1054 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1055 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1056 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1058 text_renderer = gtk_cell_renderer_text_new();
1059 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1060 column = gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer, NULL);
1061 gtk_tree_view_column_set_cell_data_func(column, text_renderer,
1062 pm_treeview_text_cell_data_func, NULL, NULL);
1063 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1065 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1066 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1067 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN,
1068 pm_tree_sort_func, NULL, NULL);
1069 gtk_tree_sortable_set_sort_column_id(
1070 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN, GTK_SORT_ASCENDING);
1072 /* selection handling */
1073 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1074 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1075 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1077 list = g_list_first(plugin_list);
1078 if (list == NULL)
1080 gtk_list_store_append(store, &iter);
1081 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1082 PLUGIN_COLUMN_PLUGIN, NULL, -1);
1084 else
1086 Plugin *p;
1087 for (; list != NULL; list = list->next)
1089 p = list->data;
1091 gtk_list_store_append(store, &iter);
1092 gtk_list_store_set(store, &iter,
1093 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1094 PLUGIN_COLUMN_PLUGIN, p,
1095 -1);
1098 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1099 g_object_unref(store);
1103 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1105 GtkTreeModel *model;
1106 GtkTreeSelection *selection;
1107 GtkTreeIter iter;
1108 Plugin *p;
1110 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1111 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1113 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1115 if (p != NULL)
1117 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1118 plugin_show_configure(&p->public);
1119 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1120 p->help();
1121 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_KEYBINDINGS && p->key_group && p->key_group->plugin_key_count > 0)
1122 keybindings_dialog_show_prefs_scroll(p->info.name);
1128 static void
1129 free_non_active_plugin(gpointer data, gpointer user_data)
1131 Plugin *plugin = data;
1133 /* don't do anything when closing the plugin manager and it is an active plugin */
1134 if (is_active_plugin(plugin))
1135 return;
1137 plugin_free(plugin);
1141 /* Callback when plugin manager dialog closes, only ever has response of
1142 * GTK_RESPONSE_OK or GTK_RESPONSE_DELETE_EVENT and both are treated the same. */
1143 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1145 if (plugin_list != NULL)
1147 /* remove all non-active plugins from the list */
1148 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1149 g_list_free(plugin_list);
1150 plugin_list = NULL;
1152 gtk_widget_destroy(GTK_WIDGET(dialog));
1154 configuration_save();
1158 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1160 GtkWidget *vbox, *vbox2, *hbox, *swin, *label;
1162 /* before showing the dialog, we need to create the list of available plugins */
1163 load_all_plugins();
1165 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1166 GTK_DIALOG_DESTROY_WITH_PARENT,
1167 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1168 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1169 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1170 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1172 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1174 pm_widgets.tree = gtk_tree_view_new();
1175 pm_widgets.store = gtk_list_store_new(
1176 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_POINTER);
1177 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1179 swin = gtk_scrolled_window_new(NULL, NULL);
1180 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1181 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1182 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1183 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1185 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1187 pm_widgets.keybindings_button = gtk_button_new_with_label(_("Keybindings"));
1188 gtk_widget_set_sensitive(pm_widgets.keybindings_button, FALSE);
1189 g_signal_connect(pm_widgets.keybindings_button, "clicked",
1190 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS));
1192 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1193 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1194 g_signal_connect(pm_widgets.configure_button, "clicked",
1195 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1197 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1198 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1199 g_signal_connect(pm_widgets.help_button, "clicked",
1200 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1202 hbox = gtk_hbox_new(FALSE, 0);
1203 gtk_box_set_spacing(GTK_BOX(hbox), 6);
1204 gtk_box_pack_end(GTK_BOX(hbox), pm_widgets.keybindings_button, FALSE, FALSE, 0);
1205 gtk_box_pack_end(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1206 gtk_box_pack_end(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 0);
1208 vbox2 = gtk_vbox_new(FALSE, 3);
1209 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1210 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1211 gtk_box_pack_start(GTK_BOX(vbox2), hbox, FALSE, TRUE, 0);
1213 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1215 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
1216 gtk_widget_show_all(pm_widgets.dialog);
1220 #endif