r5051
[geany-mirror.git] / src / plugins.c
blobfe03cc905a8e13acf2dc6e812f3da7c904bb13ff
1 /*
2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 * $Id$
25 /* Code to manage, load and unload plugins. */
27 #include "geany.h"
29 #ifdef HAVE_PLUGINS
31 #include <string.h>
33 #include "Scintilla.h"
34 #include "ScintillaWidget.h"
36 #include "prefix.h"
37 #include "plugins.h"
38 #include "plugindata.h"
39 #include "support.h"
40 #include "utils.h"
41 #include "document.h"
42 #include "filetypes.h"
43 #include "templates.h"
44 #include "sciwrappers.h"
45 #include "ui_utils.h"
46 #include "editor.h"
47 #include "dialogs.h"
48 #include "msgwindow.h"
49 #include "prefs.h"
50 #include "geanywraplabel.h"
51 #include "build.h"
52 #include "encodings.h"
53 #include "search.h"
54 #include "highlighting.h"
55 #include "keybindings.h"
56 #include "navqueue.h"
57 #include "main.h"
58 #include "toolbar.h"
59 #include "stash.h"
60 #include "symbols.h"
61 #include "keyfile.h"
62 #include "win32.h"
63 #include "pluginutils.h"
64 #include "pluginprivate.h"
67 GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
70 static gboolean want_plugins = FALSE;
72 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
73 * opened, afterwards it will be destroyed */
74 static GList *plugin_list = NULL;
75 static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
76 static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
78 static GtkWidget *menu_separator = NULL;
80 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
83 static PluginFuncs plugin_funcs = {
84 &plugin_add_toolbar_item,
85 &plugin_module_make_resident,
86 &plugin_signal_connect,
87 &plugin_set_key_group,
88 &plugin_show_configure
91 static DocumentFuncs doc_funcs = {
92 &document_new_file,
93 &document_get_current,
94 &document_get_from_page,
95 &document_find_by_filename,
96 &document_find_by_real_path,
97 &document_save_file,
98 &document_open_file,
99 &document_open_files,
100 &document_remove_page,
101 &document_reload_file,
102 &document_set_encoding,
103 &document_set_text_changed,
104 &document_set_filetype,
105 &document_close,
106 &document_index,
107 &document_save_file_as,
108 &document_rename_file,
109 &document_get_status_color,
110 &document_get_basename_for_display,
111 &document_get_notebook_page
114 static EditorFuncs editor_funcs = {
115 &editor_get_indent_prefs,
116 &editor_create_widget,
117 &editor_indicator_set_on_range,
118 &editor_indicator_set_on_line,
119 &editor_indicator_clear,
120 &editor_set_indent_type,
121 &editor_get_word_at_pos,
122 &editor_get_eol_char_name,
123 &editor_get_eol_char_len,
124 &editor_get_eol_char,
125 &editor_insert_text_block
128 static ScintillaFuncs scintilla_funcs = {
129 &scintilla_send_message,
130 &scintilla_new
133 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
134 #define _scintilla_send_message_macro scintilla_send_message
136 static SciFuncs sci_funcs = {
137 &_scintilla_send_message_macro,
138 &sci_send_command,
139 &sci_start_undo_action,
140 &sci_end_undo_action,
141 &sci_set_text,
142 &sci_insert_text,
143 &sci_get_text,
144 &sci_get_length,
145 &sci_get_current_position,
146 &sci_set_current_position,
147 &sci_get_col_from_position,
148 &sci_get_line_from_position,
149 &sci_get_position_from_line,
150 &sci_replace_sel,
151 &sci_get_selected_text,
152 &sci_get_selected_text_length,
153 &sci_get_selection_start,
154 &sci_get_selection_end,
155 &sci_get_selection_mode,
156 &sci_set_selection_mode,
157 &sci_set_selection_start,
158 &sci_set_selection_end,
159 &sci_get_text_range,
160 &sci_get_line,
161 &sci_get_line_length,
162 &sci_get_line_count,
163 &sci_get_line_is_visible,
164 &sci_ensure_line_is_visible,
165 &sci_scroll_caret,
166 &sci_find_matching_brace,
167 &sci_get_style_at,
168 &sci_get_char_at,
169 &sci_get_current_line,
170 &sci_has_selection,
171 &sci_get_tab_width,
172 &sci_indicator_clear,
173 &sci_indicator_set,
174 &sci_get_contents,
175 &sci_get_contents_range,
176 &sci_get_selection_contents,
177 &sci_set_font,
178 &sci_get_line_end_position,
179 &sci_set_target_start,
180 &sci_set_target_end,
181 &sci_replace_target,
182 &sci_set_marker_at_line,
183 &sci_delete_marker_at_line,
184 &sci_is_marker_set_at_line,
185 &sci_goto_line,
186 &sci_find_text,
187 &sci_set_line_indentation,
188 &sci_get_line_indentation
191 static TemplateFuncs template_funcs = {
192 &templates_get_template_fileheader
195 static UtilsFuncs utils_funcs = {
196 &utils_str_equal,
197 &utils_string_replace_all,
198 &utils_get_file_list,
199 &utils_write_file,
200 &utils_get_locale_from_utf8,
201 &utils_get_utf8_from_locale,
202 &utils_remove_ext_from_filename,
203 &utils_mkdir,
204 &utils_get_setting_boolean,
205 &utils_get_setting_integer,
206 &utils_get_setting_string,
207 &utils_spawn_sync,
208 &utils_spawn_async,
209 &utils_str_casecmp,
210 &utils_get_date_time,
211 &utils_open_browser,
212 &utils_string_replace_first,
213 &utils_str_middle_truncate,
214 &utils_str_remove_chars,
215 &utils_get_file_list_full,
216 &utils_copy_environment
219 static UIUtilsFuncs uiutils_funcs = {
220 &ui_dialog_vbox_new,
221 &ui_frame_new_with_alignment,
222 &ui_set_statusbar,
223 &ui_table_add_row,
224 &ui_path_box_new,
225 &ui_button_new_with_image,
226 &ui_add_document_sensitive,
227 &ui_widget_set_tooltip_text,
228 &ui_image_menu_item_new,
229 &ui_lookup_widget,
230 &ui_progress_bar_start,
231 &ui_progress_bar_stop,
232 &ui_entry_add_clear_icon,
233 &ui_menu_add_document_items,
234 &ui_widget_modify_font_from_string,
235 &ui_is_keyval_enter_or_return,
236 &ui_get_gtk_settings_integer
239 static DialogFuncs dialog_funcs = {
240 &dialogs_show_question,
241 &dialogs_show_msgbox,
242 &dialogs_show_save_as,
243 &dialogs_show_input_numeric
246 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
247 #define _lookup_widget_macro ui_lookup_widget
249 /* deprecated */
250 static SupportFuncs support_funcs = {
251 &_lookup_widget_macro
254 static MsgWinFuncs msgwin_funcs = {
255 &msgwin_status_add,
256 &msgwin_compiler_add,
257 &msgwin_msg_add,
258 &msgwin_clear_tab,
259 &msgwin_switch_tab
262 static EncodingFuncs encoding_funcs = {
263 &encodings_convert_to_utf8,
264 &encodings_convert_to_utf8_from_charset,
265 &encodings_get_charset_from_index
268 static KeybindingFuncs keybindings_funcs = {
269 &keybindings_send_command,
270 &keybindings_set_item,
271 &keybindings_get_item
274 static TagManagerFuncs tagmanager_funcs = {
275 &tm_get_real_path,
276 &tm_source_file_new,
277 &tm_workspace_add_object,
278 &tm_source_file_update,
279 &tm_work_object_free,
280 &tm_workspace_remove_object
283 static SearchFuncs search_funcs = {
284 &search_show_find_in_files_dialog
287 static HighlightingFuncs highlighting_funcs = {
288 &highlighting_get_style,
289 &highlighting_set_styles
292 static FiletypeFuncs filetype_funcs = {
293 &filetypes_detect_from_file,
294 &filetypes_lookup_by_name,
295 &filetypes_index
298 static NavQueueFuncs navqueue_funcs = {
299 &navqueue_goto_line
302 static MainFuncs main_funcs = {
303 &main_reload_configuration,
304 &main_locale_init,
305 &main_is_realized
308 static StashFuncs stash_funcs = {
309 &stash_group_new,
310 &stash_group_add_boolean,
311 &stash_group_add_integer,
312 &stash_group_add_string,
313 &stash_group_add_string_vector,
314 &stash_group_load_from_key_file,
315 &stash_group_save_to_key_file,
316 &stash_group_free,
317 &stash_group_load_from_file,
318 &stash_group_save_to_file,
319 &stash_group_add_toggle_button,
320 &stash_group_add_radio_buttons,
321 &stash_group_add_spin_button_integer,
322 &stash_group_add_combo_box,
323 &stash_group_add_combo_box_entry,
324 &stash_group_add_entry,
325 &stash_group_add_widget_property,
326 &stash_group_display,
327 &stash_group_update
330 static SymbolsFuncs symbols_funcs = {
331 &symbols_get_context_separator
334 static GeanyFunctions geany_functions = {
335 &doc_funcs,
336 &sci_funcs,
337 &template_funcs,
338 &utils_funcs,
339 &uiutils_funcs,
340 &support_funcs,
341 &dialog_funcs,
342 &msgwin_funcs,
343 &encoding_funcs,
344 &keybindings_funcs,
345 &tagmanager_funcs,
346 &search_funcs,
347 &highlighting_funcs,
348 &filetype_funcs,
349 &navqueue_funcs,
350 &editor_funcs,
351 &main_funcs,
352 &plugin_funcs,
353 &scintilla_funcs,
354 &msgwin_funcs,
355 &stash_funcs,
356 &symbols_funcs
359 static GeanyData geany_data;
362 static void
363 geany_data_init(void)
365 GeanyData gd = {
366 app,
367 &main_widgets,
368 documents_array,
369 filetypes_array,
370 &prefs,
371 &interface_prefs,
372 &toolbar_prefs,
373 &editor_prefs,
374 &file_prefs,
375 &search_prefs,
376 &tool_prefs,
377 &template_prefs,
378 &build_info,
379 filetypes_by_title
381 memcpy(&geany_data, &gd, sizeof(GeanyData));
385 /* Prevent the same plugin filename being loaded more than once.
386 * Note: g_module_name always returns the .so name, even when Plugin::filename is an .la file. */
387 static gboolean
388 plugin_loaded(GModule *module)
390 gchar *basename_module, *basename_loaded;
391 GList *item;
393 basename_module = g_path_get_basename(g_module_name(module));
394 for (item = plugin_list; item != NULL; item = g_list_next(item))
396 basename_loaded = g_path_get_basename(
397 g_module_name(((Plugin*)item->data)->module));
399 if (utils_str_equal(basename_module, basename_loaded))
401 g_free(basename_loaded);
402 g_free(basename_module);
403 return TRUE;
405 g_free(basename_loaded);
407 /* Look also through the list of active plugins. This prevents problems when we have the same
408 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
409 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
410 * the plugin manager would list both plugins. Additionally, unloading the active plugin
411 * would cause a crash. */
412 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
414 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
416 if (utils_str_equal(basename_module, basename_loaded))
418 g_free(basename_loaded);
419 g_free(basename_module);
420 return TRUE;
422 g_free(basename_loaded);
424 g_free(basename_module);
425 return FALSE;
429 static Plugin *find_active_plugin_by_name(const gchar *filename)
431 GList *item;
433 g_return_val_if_fail(filename, FALSE);
435 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
437 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
438 return item->data;
441 return NULL;
445 static gboolean
446 plugin_check_version(GModule *module)
448 gint (*version_check)(gint) = NULL;
450 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
452 if (G_UNLIKELY(! version_check))
454 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
455 g_module_name(module));
456 return FALSE;
458 else
460 gint result = version_check(GEANY_ABI_VERSION);
462 if (result < 0)
464 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
465 "release of Geany - please recompile it."), g_module_name(module));
466 geany_debug("Plugin \"%s\" is not binary compatible with this "
467 "release of Geany - recompile it.", g_module_name(module));
468 return FALSE;
470 if (result > GEANY_API_VERSION)
472 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
473 g_module_name(module), result);
474 return FALSE;
477 return TRUE;
481 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
483 PluginCallback *cb;
484 guint i, len = 0;
486 while (TRUE)
488 cb = &callbacks[len];
489 if (!cb->signal_name || !cb->callback)
490 break;
491 len++;
493 if (len == 0)
494 return;
496 for (i = 0; i < len; i++)
498 cb = &callbacks[i];
500 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
501 cb->callback, cb->user_data);
506 static void read_key_group(Plugin *plugin)
508 GeanyKeyGroupInfo *p_key_info;
509 GeanyKeyGroup **p_key_group;
511 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
512 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
513 if (p_key_info && p_key_group)
515 GeanyKeyGroupInfo *key_info = p_key_info;
517 if (*p_key_group)
518 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
519 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
520 plugin->info.name);
521 else
523 if (key_info->count)
525 GeanyKeyGroup *key_group =
526 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
527 if (key_group)
528 *p_key_group = key_group;
530 else
531 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
532 "count field is zero. Maybe use plugin_set_key_group() instead?",
533 plugin->info.name);
536 else if (p_key_info || p_key_group)
537 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
538 "Maybe use plugin_set_key_group() instead?",
539 plugin->info.name);
543 static void
544 plugin_init(Plugin *plugin)
546 GeanyPlugin **p_geany_plugin;
547 PluginCallback *callbacks;
548 PluginInfo **p_info;
549 PluginFields **plugin_fields;
551 /* set these symbols before plugin_init() is called
552 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
553 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
554 if (p_geany_plugin)
555 *p_geany_plugin = &plugin->public;
556 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
557 if (p_info)
558 *p_info = &plugin->info;
559 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
560 if (plugin_fields)
561 *plugin_fields = &plugin->fields;
562 read_key_group(plugin);
564 /* start the plugin */
565 g_return_if_fail(plugin->init);
566 plugin->init(&geany_data);
568 /* store some function pointers for later use */
569 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
570 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
571 if (app->debug_mode && plugin->configure && plugin->configure_single)
572 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
573 "only plugin_configure() will be used!",
574 plugin->info.name);
576 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
577 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
578 if (plugin->cleanup == NULL)
580 if (app->debug_mode)
581 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
582 plugin->info.name);
585 /* now read any plugin-owned data that might have been set in plugin_init() */
587 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
589 ui_add_document_sensitive(plugin->fields.menu_item);
592 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
593 if (callbacks)
594 add_callbacks(plugin, callbacks);
596 /* remember which plugins are active */
597 active_plugin_list = g_list_append(active_plugin_list, plugin);
599 geany_debug("Loaded: %s (%s)", plugin->filename,
600 NVL(plugin->info.name, "<Unknown>"));
604 /* Load and optionally init a plugin.
605 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
606 * called, the plugin will be started, if not the plugin will be read only (for the list of
607 * available plugins in the plugin manager).
608 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
609 static Plugin*
610 plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
612 Plugin *plugin;
613 GModule *module;
614 GeanyData **p_geany_data;
615 GeanyFunctions **p_geany_functions;
616 void (*plugin_set_info)(PluginInfo*);
618 g_return_val_if_fail(fname, NULL);
619 g_return_val_if_fail(g_module_supported(), NULL);
621 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
622 * load the module */
623 plugin = find_active_plugin_by_name(fname);
624 if (plugin != NULL)
626 geany_debug("Plugin \"%s\" already loaded.", fname);
627 if (add_to_list)
628 plugin_list = g_list_append(plugin_list, plugin);
629 return plugin;
632 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
633 * causing a segfault. Without that flag the module will safely fail to load.
634 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
635 * otherwise not be detected due to the shadowing of Geany's app variable.
636 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
637 * function from a plugin will be shadowed. */
638 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
639 if (! module)
641 geany_debug("Can't load plugin: %s", g_module_error());
642 return NULL;
645 if (plugin_loaded(module))
647 geany_debug("Plugin \"%s\" already loaded.", fname);
649 if (! g_module_close(module))
650 g_warning("%s: %s", fname, g_module_error());
651 return NULL;
654 if (! plugin_check_version(module))
656 if (! g_module_close(module))
657 g_warning("%s: %s", fname, g_module_error());
658 return NULL;
661 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
662 if (plugin_set_info == NULL)
664 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
666 if (! g_module_close(module))
667 g_warning("%s: %s", fname, g_module_error());
668 return NULL;
671 plugin = g_new0(Plugin, 1);
673 /* set basic fields here to allow plugins to call Geany functions in set_info() */
674 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
675 if (p_geany_data)
676 *p_geany_data = &geany_data;
677 g_module_symbol(module, "geany_functions", (void *) &p_geany_functions);
678 if (p_geany_functions)
679 *p_geany_functions = &geany_functions;
681 /* read plugin name, etc. */
682 plugin_set_info(&plugin->info);
683 if (!NZV(plugin->info.name))
685 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
686 fname);
688 if (! g_module_close(module))
689 g_warning("%s: %s", fname, g_module_error());
690 g_free(plugin);
691 return NULL;
694 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
695 if (plugin->init == NULL)
697 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
698 plugin->info.name);
700 if (! g_module_close(module))
701 g_warning("%s: %s", fname, g_module_error());
702 g_free(plugin);
703 return NULL;
705 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
707 plugin->filename = g_strdup(fname);
708 plugin->module = module;
709 plugin->public.info = &plugin->info;
710 plugin->public.priv = plugin;
712 if (init_plugin)
713 plugin_init(plugin);
715 if (add_to_list)
716 plugin_list = g_list_append(plugin_list, plugin);
718 return plugin;
722 static void remove_callbacks(Plugin *plugin)
724 GArray *signal_ids = plugin->signal_ids;
725 SignalConnection *sc;
727 if (signal_ids == NULL)
728 return;
730 foreach_array(SignalConnection, sc, signal_ids)
731 g_signal_handler_disconnect(sc->object, sc->handler_id);
733 g_array_free(signal_ids, TRUE);
737 static gboolean is_active_plugin(Plugin *plugin)
739 return (g_list_find(active_plugin_list, plugin) != NULL);
743 /* Clean up anything used by an active plugin */
744 static void
745 plugin_cleanup(Plugin *plugin)
747 GtkWidget *widget;
749 if (plugin->cleanup)
750 plugin->cleanup();
752 remove_callbacks(plugin);
754 if (plugin->key_group)
755 keybindings_free_group(plugin->key_group);
757 widget = plugin->toolbar_separator.widget;
758 if (widget)
759 gtk_widget_destroy(widget);
761 geany_debug("Unloaded: %s", plugin->filename);
765 static void
766 plugin_free(Plugin *plugin)
768 g_return_if_fail(plugin);
769 g_return_if_fail(plugin->module);
771 if (is_active_plugin(plugin))
772 plugin_cleanup(plugin);
774 active_plugin_list = g_list_remove(active_plugin_list, plugin);
776 if (! g_module_close(plugin->module))
777 g_warning("%s: %s", plugin->filename, g_module_error());
779 plugin_list = g_list_remove(plugin_list, plugin);
781 g_free(plugin->filename);
782 g_free(plugin);
783 plugin = NULL;
787 /* load active plugins at startup */
788 static void
789 load_active_plugins(void)
791 guint i, len;
793 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
794 return;
796 for (i = 0; i < len; i++)
798 const gchar *fname = active_plugins_pref[i];
800 if (NZV(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
802 if (plugin_new(fname, TRUE, FALSE) == NULL)
803 failed_plugins_list = g_list_append(failed_plugins_list, g_strdup(fname));
809 static void
810 load_plugins_from_path(const gchar *path)
812 GSList *list, *item;
813 gchar *fname, *tmp;
815 list = utils_get_file_list(path, NULL, NULL);
817 for (item = list; item != NULL; item = g_slist_next(item))
819 tmp = strrchr(item->data, '.');
820 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
821 continue;
823 fname = g_strconcat(path, G_DIR_SEPARATOR_S, item->data, NULL);
824 plugin_new(fname, FALSE, TRUE);
825 g_free(fname);
828 g_slist_foreach(list, (GFunc) g_free, NULL);
829 g_slist_free(list);
833 #ifdef G_OS_WIN32
834 static gchar *get_plugin_path()
836 gchar *install_dir = win32_get_installation_dir();
837 gchar *path;
839 path = g_strconcat(install_dir, "\\lib", NULL);
840 g_free(install_dir);
842 return path;
844 #endif
847 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
848 static void load_all_plugins(void)
850 gchar *path;
852 path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", NULL);
853 /* first load plugins in ~/.config/geany/plugins/ */
854 load_plugins_from_path(path);
855 g_free(path);
857 /* load plugins from a custom path */
858 if (NZV(prefs.custom_plugin_path))
859 load_plugins_from_path(prefs.custom_plugin_path);
861 /* finally load plugins from $prefix/lib/geany */
862 #ifdef G_OS_WIN32
863 path = get_plugin_path();
864 #else
865 path = g_strconcat(GEANY_LIBDIR, G_DIR_SEPARATOR_S "geany", NULL);
866 #endif
867 load_plugins_from_path(path);
868 g_free(path);
872 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
874 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
875 guint i = 0;
876 gboolean have_plugin_menu_items = FALSE;
878 for (item = list; item != NULL; item = g_list_next(item))
880 if (item->data == menu_separator)
882 if (i < g_list_length(list) - 1)
884 have_plugin_menu_items = TRUE;
885 break;
888 i++;
890 g_list_free(list);
892 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
896 /* Calling this starts up plugin support */
897 void plugins_load_active(void)
899 GtkWidget *widget;
901 want_plugins = TRUE;
903 geany_data_init();
905 widget = gtk_separator_menu_item_new();
906 gtk_widget_show(widget);
907 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
909 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
910 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
911 gtk_widget_show(widget);
912 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
914 menu_separator = gtk_separator_menu_item_new();
915 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
916 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
918 load_active_plugins();
922 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
924 const Plugin *pa = a;
925 const Plugin *pb = b;
927 return strcmp(pa->info.name, pb->info.name);
931 static void update_active_plugins_pref(void)
933 gint i = 0;
934 GList *list;
935 gsize count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
937 g_strfreev(active_plugins_pref);
939 if (count == 0)
941 active_plugins_pref = NULL;
942 return;
945 /* sort the list so next time tools menu items are sorted by plugin name
946 * (not ideal to do here, but better than nothing) */
947 active_plugin_list = g_list_sort(active_plugin_list, cmp_plugin_names);
949 active_plugins_pref = g_new0(gchar*, count + 1);
951 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
953 Plugin *plugin = list->data;
955 active_plugins_pref[i] = g_strdup(plugin->filename);
956 i++;
958 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
960 const gchar *fname = list->data;
962 active_plugins_pref[i] = g_strdup(fname);
963 i++;
965 active_plugins_pref[i] = NULL;
969 static void on_save_settings(GKeyFile *config)
971 /* if plugins are disabled, don't clear list of active plugins */
972 if (want_plugins)
973 update_active_plugins_pref();
977 /* called even if plugin support is disabled */
978 void plugins_init(void)
980 StashGroup *group;
982 group = stash_group_new("plugins");
983 configuration_add_pref_group(group, TRUE);
985 stash_group_add_toggle_button(group, &prefs.load_plugins,
986 "load_plugins", TRUE, "check_plugins");
987 stash_group_add_entry(group, &prefs.custom_plugin_path,
988 "custom_plugin_path", "", "extra_plugin_path_entry");
990 g_signal_connect(geany_object, "save-settings", G_CALLBACK(on_save_settings), NULL);
991 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
995 /* called even if plugin support is disabled */
996 void plugins_finalize(void)
998 if (failed_plugins_list != NULL)
1000 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1001 g_list_free(failed_plugins_list);
1003 if (active_plugin_list != NULL)
1005 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1006 g_list_free(active_plugin_list);
1008 g_strfreev(active_plugins_pref);
1012 /* Check whether there are any plugins loaded which provide a configure symbol */
1013 gboolean plugins_have_preferences(void)
1015 GList *item;
1017 if (active_plugin_list == NULL)
1018 return FALSE;
1020 foreach_list(item, active_plugin_list)
1022 Plugin *plugin = item->data;
1023 if (plugin->configure != NULL || plugin->configure_single != NULL)
1024 return TRUE;
1027 return FALSE;
1031 /* Plugin Manager */
1033 enum
1035 PLUGIN_COLUMN_CHECK = 0,
1036 PLUGIN_COLUMN_NAME,
1037 PLUGIN_COLUMN_FILE,
1038 PLUGIN_COLUMN_PLUGIN,
1039 PLUGIN_N_COLUMNS,
1040 PM_BUTTON_CONFIGURE,
1041 PM_BUTTON_HELP
1044 typedef struct
1046 GtkWidget *dialog;
1047 GtkWidget *tree;
1048 GtkListStore *store;
1049 GtkWidget *description_label;
1050 GtkWidget *configure_button;
1051 GtkWidget *help_button;
1053 PluginManagerWidgets;
1055 static PluginManagerWidgets pm_widgets;
1058 static void pm_update_buttons(Plugin *p)
1060 gboolean is_active;
1062 is_active = is_active_plugin(p);
1063 gtk_widget_set_sensitive(pm_widgets.configure_button,
1064 (p->configure || p->configure_single) && is_active);
1065 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
1069 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1071 GtkTreeIter iter;
1072 GtkTreeModel *model;
1073 Plugin *p;
1075 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1077 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1079 if (p != NULL)
1081 gchar *text;
1082 PluginInfo *pi;
1084 pi = &p->info;
1085 text = g_strdup_printf(
1086 _("Plugin: %s %s\nDescription: %s\nAuthor(s): %s"),
1087 pi->name, pi->version, pi->description, pi->author);
1089 geany_wrap_label_set_text(GTK_LABEL(pm_widgets.description_label), text);
1090 g_free(text);
1092 pm_update_buttons(p);
1098 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1100 gboolean old_state, state;
1101 gchar *file_name;
1102 GtkTreeIter iter;
1103 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1104 Plugin *p;
1106 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
1107 gtk_tree_path_free(path);
1109 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
1110 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
1112 /* no plugins item */
1113 if (p == NULL)
1114 return;
1116 state = ! old_state; /* toggle the state */
1118 /* save the filename of the plugin */
1119 file_name = g_strdup(p->filename);
1121 /* unload plugin module */
1122 if (!state)
1123 /* save shortcuts (only need this group, but it doesn't take long) */
1124 keybindings_write_to_file();
1126 plugin_free(p);
1128 /* reload plugin module and initialize it if item is checked */
1129 p = plugin_new(file_name, state, TRUE);
1130 if (!p)
1132 /* plugin file may no longer be on disk, or is now incompatible */
1133 gtk_list_store_remove(pm_widgets.store, &iter);
1135 else
1137 if (state)
1138 keybindings_load_keyfile(); /* load shortcuts */
1140 /* update model */
1141 gtk_list_store_set(pm_widgets.store, &iter,
1142 PLUGIN_COLUMN_CHECK, state,
1143 PLUGIN_COLUMN_PLUGIN, p, -1);
1145 /* set again the sensitiveness of the configure and help buttons */
1146 pm_update_buttons(p);
1148 g_free(file_name);
1152 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1154 GtkCellRenderer *text_renderer, *checkbox_renderer;
1155 GtkTreeViewColumn *column;
1156 GtkTreeIter iter;
1157 GList *list;
1158 GtkTreeSelection *sel;
1160 checkbox_renderer = gtk_cell_renderer_toggle_new();
1161 column = gtk_tree_view_column_new_with_attributes(
1162 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1163 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1164 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1166 text_renderer = gtk_cell_renderer_text_new();
1167 column = gtk_tree_view_column_new_with_attributes(
1168 _("Plugin"), text_renderer, "text", PLUGIN_COLUMN_NAME, NULL);
1169 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1171 text_renderer = gtk_cell_renderer_text_new();
1172 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1173 column = gtk_tree_view_column_new_with_attributes(
1174 _("File"), text_renderer, "text", PLUGIN_COLUMN_FILE, NULL);
1175 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1177 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1178 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1179 gtk_tree_sortable_set_sort_column_id(
1180 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_NAME, GTK_SORT_ASCENDING);
1182 /* selection handling */
1183 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1184 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1185 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1187 list = g_list_first(plugin_list);
1188 if (list == NULL)
1190 gtk_list_store_append(store, &iter);
1191 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1192 PLUGIN_COLUMN_NAME, _("No plugins available."),
1193 PLUGIN_COLUMN_FILE, "", PLUGIN_COLUMN_PLUGIN, NULL, -1);
1195 else
1197 Plugin *p;
1198 for (; list != NULL; list = list->next)
1200 p = list->data;
1202 gtk_list_store_append(store, &iter);
1203 gtk_list_store_set(store, &iter,
1204 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1205 PLUGIN_COLUMN_NAME, p->info.name,
1206 PLUGIN_COLUMN_FILE, p->filename,
1207 PLUGIN_COLUMN_PLUGIN, p,
1208 -1);
1211 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1212 g_object_unref(store);
1216 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1218 GtkTreeModel *model;
1219 GtkTreeSelection *selection;
1220 GtkTreeIter iter;
1221 Plugin *p;
1223 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1224 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1226 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1228 if (p != NULL)
1230 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1231 plugin_show_configure(&p->public);
1232 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1233 p->help();
1239 static void
1240 free_non_active_plugin(gpointer data, gpointer user_data)
1242 Plugin *plugin = data;
1244 /* don't do anything when closing the plugin manager and it is an active plugin */
1245 if (is_active_plugin(plugin))
1246 return;
1248 plugin_free(plugin);
1252 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1254 if (plugin_list != NULL)
1256 /* remove all non-active plugins from the list */
1257 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1258 g_list_free(plugin_list);
1259 plugin_list = NULL;
1261 gtk_widget_destroy(GTK_WIDGET(dialog));
1265 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1267 GtkWidget *vbox, *vbox2, *label_vbox, *hbox, *swin, *label, *label2, *desc_win;
1269 /* before showing the dialog, we need to create the list of available plugins */
1270 load_all_plugins();
1272 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1273 GTK_DIALOG_DESTROY_WITH_PARENT,
1274 GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
1275 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1276 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1277 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1279 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1281 pm_widgets.tree = gtk_tree_view_new();
1282 pm_widgets.store = gtk_list_store_new(
1283 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1284 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1286 swin = gtk_scrolled_window_new(NULL, NULL);
1287 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1288 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1289 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1290 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1292 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1294 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1295 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1296 g_signal_connect(pm_widgets.configure_button, "clicked",
1297 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1299 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1300 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1301 g_signal_connect(pm_widgets.help_button, "clicked",
1302 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1304 label2 = gtk_label_new(_("<b>Plugin details:</b>"));
1305 gtk_label_set_use_markup(GTK_LABEL(label2), TRUE);
1306 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
1308 pm_widgets.description_label = geany_wrap_label_new("");
1309 desc_win = gtk_scrolled_window_new(NULL, NULL);
1310 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win),
1311 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1312 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win),
1313 pm_widgets.description_label);
1315 hbox = gtk_hbox_new(FALSE, 0);
1316 gtk_box_pack_start(GTK_BOX(hbox), label2, TRUE, TRUE, 0);
1317 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 4);
1318 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1320 label_vbox = gtk_vbox_new(FALSE, 3);
1321 gtk_box_pack_start(GTK_BOX(label_vbox), hbox, FALSE, FALSE, 0);
1322 gtk_box_pack_start(GTK_BOX(label_vbox), desc_win, FALSE, FALSE, 0);
1324 vbox2 = gtk_vbox_new(FALSE, 3);
1325 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1326 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1327 gtk_box_pack_start(GTK_BOX(vbox2), label_vbox, FALSE, FALSE, 0);
1329 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1331 gtk_container_add(GTK_CONTAINER(vbox), vbox2);
1332 gtk_widget_show_all(pm_widgets.dialog);
1336 #endif