Fixup various filedefs mappings
[geany-mirror.git] / src / plugins.c
blob03efa8aa52aa33bb08e90ddcd790899ede30a6b6
1 /*
2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 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.
23 /* Code to manage, load and unload plugins. */
25 #include "geany.h"
27 #ifdef HAVE_PLUGINS
29 #include <string.h>
31 #include "Scintilla.h"
32 #include "ScintillaWidget.h"
34 #include "prefix.h"
35 #include "plugins.h"
36 #include "plugindata.h"
37 #include "support.h"
38 #include "utils.h"
39 #include "document.h"
40 #include "filetypes.h"
41 #include "templates.h"
42 #include "sciwrappers.h"
43 #include "ui_utils.h"
44 #include "editor.h"
45 #include "dialogs.h"
46 #include "msgwindow.h"
47 #include "prefs.h"
48 #include "geanywraplabel.h"
49 #include "build.h"
50 #include "encodings.h"
51 #include "search.h"
52 #include "highlighting.h"
53 #include "keybindings.h"
54 #include "navqueue.h"
55 #include "main.h"
56 #include "toolbar.h"
57 #include "stash.h"
58 #include "symbols.h"
59 #include "keyfile.h"
60 #include "win32.h"
61 #include "pluginutils.h"
62 #include "pluginprivate.h"
65 GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
68 static gboolean want_plugins = FALSE;
70 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
71 * opened, afterwards it will be destroyed */
72 static GList *plugin_list = NULL;
73 static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
74 static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
76 static GtkWidget *menu_separator = NULL;
78 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
81 static PluginFuncs plugin_funcs = {
82 &plugin_add_toolbar_item,
83 &plugin_module_make_resident,
84 &plugin_signal_connect,
85 &plugin_set_key_group,
86 &plugin_show_configure,
87 &plugin_timeout_add,
88 &plugin_timeout_add_seconds,
89 &plugin_idle_add
92 static DocumentFuncs doc_funcs = {
93 &document_new_file,
94 &document_get_current,
95 &document_get_from_page,
96 &document_find_by_filename,
97 &document_find_by_real_path,
98 &document_save_file,
99 &document_open_file,
100 &document_open_files,
101 &document_remove_page,
102 &document_reload_file,
103 &document_set_encoding,
104 &document_set_text_changed,
105 &document_set_filetype,
106 &document_close,
107 &document_index,
108 &document_save_file_as,
109 &document_rename_file,
110 &document_get_status_color,
111 &document_get_basename_for_display,
112 &document_get_notebook_page,
113 &document_compare_by_display_name,
114 &document_compare_by_tab_order,
115 &document_compare_by_tab_order_reverse
118 static EditorFuncs editor_funcs = {
119 &editor_get_indent_prefs,
120 &editor_create_widget,
121 &editor_indicator_set_on_range,
122 &editor_indicator_set_on_line,
123 &editor_indicator_clear,
124 &editor_set_indent_type,
125 &editor_get_word_at_pos,
126 &editor_get_eol_char_name,
127 &editor_get_eol_char_len,
128 &editor_get_eol_char,
129 &editor_insert_text_block,
130 &editor_get_eol_char_mode,
131 &editor_goto_pos,
132 &editor_find_snippet,
133 &editor_insert_snippet
136 static ScintillaFuncs scintilla_funcs = {
137 &scintilla_send_message,
138 &scintilla_new
141 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
142 #define _scintilla_send_message_macro scintilla_send_message
144 static SciFuncs sci_funcs = {
145 &_scintilla_send_message_macro,
146 &sci_send_command,
147 &sci_start_undo_action,
148 &sci_end_undo_action,
149 &sci_set_text,
150 &sci_insert_text,
151 &sci_get_text,
152 &sci_get_length,
153 &sci_get_current_position,
154 &sci_set_current_position,
155 &sci_get_col_from_position,
156 &sci_get_line_from_position,
157 &sci_get_position_from_line,
158 &sci_replace_sel,
159 &sci_get_selected_text,
160 &sci_get_selected_text_length,
161 &sci_get_selection_start,
162 &sci_get_selection_end,
163 &sci_get_selection_mode,
164 &sci_set_selection_mode,
165 &sci_set_selection_start,
166 &sci_set_selection_end,
167 &sci_get_text_range,
168 &sci_get_line,
169 &sci_get_line_length,
170 &sci_get_line_count,
171 &sci_get_line_is_visible,
172 &sci_ensure_line_is_visible,
173 &sci_scroll_caret,
174 &sci_find_matching_brace,
175 &sci_get_style_at,
176 &sci_get_char_at,
177 &sci_get_current_line,
178 &sci_has_selection,
179 &sci_get_tab_width,
180 &sci_indicator_clear,
181 &sci_indicator_set,
182 &sci_get_contents,
183 &sci_get_contents_range,
184 &sci_get_selection_contents,
185 &sci_set_font,
186 &sci_get_line_end_position,
187 &sci_set_target_start,
188 &sci_set_target_end,
189 &sci_replace_target,
190 &sci_set_marker_at_line,
191 &sci_delete_marker_at_line,
192 &sci_is_marker_set_at_line,
193 &sci_goto_line,
194 &sci_find_text,
195 &sci_set_line_indentation,
196 &sci_get_line_indentation,
197 &sci_get_lexer
200 static TemplateFuncs template_funcs = {
201 &templates_get_template_fileheader
204 static UtilsFuncs utils_funcs = {
205 &utils_str_equal,
206 &utils_string_replace_all,
207 &utils_get_file_list,
208 &utils_write_file,
209 &utils_get_locale_from_utf8,
210 &utils_get_utf8_from_locale,
211 &utils_remove_ext_from_filename,
212 &utils_mkdir,
213 &utils_get_setting_boolean,
214 &utils_get_setting_integer,
215 &utils_get_setting_string,
216 &utils_spawn_sync,
217 &utils_spawn_async,
218 &utils_str_casecmp,
219 &utils_get_date_time,
220 &utils_open_browser,
221 &utils_string_replace_first,
222 &utils_str_middle_truncate,
223 &utils_str_remove_chars,
224 &utils_get_file_list_full,
225 &utils_copy_environment,
226 &utils_find_open_xml_tag,
227 &utils_find_open_xml_tag_pos
230 static UIUtilsFuncs uiutils_funcs = {
231 &ui_dialog_vbox_new,
232 &ui_frame_new_with_alignment,
233 &ui_set_statusbar,
234 &ui_table_add_row,
235 &ui_path_box_new,
236 &ui_button_new_with_image,
237 &ui_add_document_sensitive,
238 &ui_widget_set_tooltip_text,
239 &ui_image_menu_item_new,
240 &ui_lookup_widget,
241 &ui_progress_bar_start,
242 &ui_progress_bar_stop,
243 &ui_entry_add_clear_icon,
244 &ui_menu_add_document_items,
245 &ui_widget_modify_font_from_string,
246 &ui_is_keyval_enter_or_return,
247 &ui_get_gtk_settings_integer,
248 &ui_combo_box_add_to_history,
249 &ui_menu_add_document_items_sorted
252 static DialogFuncs dialog_funcs = {
253 &dialogs_show_question,
254 &dialogs_show_msgbox,
255 &dialogs_show_save_as,
256 &dialogs_show_input_numeric,
257 &dialogs_show_input
260 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
261 #define _lookup_widget_macro ui_lookup_widget
263 /* deprecated */
264 static SupportFuncs support_funcs = {
265 &_lookup_widget_macro
268 static MsgWinFuncs msgwin_funcs = {
269 &msgwin_status_add,
270 &msgwin_compiler_add,
271 &msgwin_msg_add,
272 &msgwin_clear_tab,
273 &msgwin_switch_tab,
274 &msgwin_set_messages_dir
277 static EncodingFuncs encoding_funcs = {
278 &encodings_convert_to_utf8,
279 &encodings_convert_to_utf8_from_charset,
280 &encodings_get_charset_from_index
283 static KeybindingFuncs keybindings_funcs = {
284 &keybindings_send_command,
285 &keybindings_set_item,
286 &keybindings_get_item
289 static TagManagerFuncs tagmanager_funcs = {
290 &tm_get_real_path,
291 &tm_source_file_new,
292 &tm_workspace_add_object,
293 &tm_source_file_update,
294 &tm_work_object_free,
295 &tm_workspace_remove_object
298 static SearchFuncs search_funcs = {
299 &search_show_find_in_files_dialog
302 static HighlightingFuncs highlighting_funcs = {
303 &highlighting_get_style,
304 &highlighting_set_styles,
305 &highlighting_is_string_style,
306 &highlighting_is_comment_style,
307 &highlighting_is_code_style
310 static FiletypeFuncs filetype_funcs = {
311 &filetypes_detect_from_file,
312 &filetypes_lookup_by_name,
313 &filetypes_index,
314 &filetypes_get_display_name,
315 &filetypes_get_sorted_by_name
318 static NavQueueFuncs navqueue_funcs = {
319 &navqueue_goto_line
322 static MainFuncs main_funcs = {
323 &main_reload_configuration,
324 &main_locale_init,
325 &main_is_realized
328 static StashFuncs stash_funcs = {
329 &stash_group_new,
330 &stash_group_add_boolean,
331 &stash_group_add_integer,
332 &stash_group_add_string,
333 &stash_group_add_string_vector,
334 &stash_group_load_from_key_file,
335 &stash_group_save_to_key_file,
336 &stash_group_free,
337 &stash_group_load_from_file,
338 &stash_group_save_to_file,
339 &stash_group_add_toggle_button,
340 &stash_group_add_radio_buttons,
341 &stash_group_add_spin_button_integer,
342 &stash_group_add_combo_box,
343 &stash_group_add_combo_box_entry,
344 &stash_group_add_entry,
345 &stash_group_add_widget_property,
346 &stash_group_display,
347 &stash_group_update
350 static SymbolsFuncs symbols_funcs = {
351 &symbols_get_context_separator
354 static GeanyFunctions geany_functions = {
355 &doc_funcs,
356 &sci_funcs,
357 &template_funcs,
358 &utils_funcs,
359 &uiutils_funcs,
360 &support_funcs,
361 &dialog_funcs,
362 &msgwin_funcs,
363 &encoding_funcs,
364 &keybindings_funcs,
365 &tagmanager_funcs,
366 &search_funcs,
367 &highlighting_funcs,
368 &filetype_funcs,
369 &navqueue_funcs,
370 &editor_funcs,
371 &main_funcs,
372 &plugin_funcs,
373 &scintilla_funcs,
374 &msgwin_funcs,
375 &stash_funcs,
376 &symbols_funcs
379 static GeanyData geany_data;
382 static void
383 geany_data_init(void)
385 GeanyData gd = {
386 app,
387 &main_widgets,
388 documents_array,
389 filetypes_array,
390 &prefs,
391 &interface_prefs,
392 &toolbar_prefs,
393 &editor_prefs,
394 &file_prefs,
395 &search_prefs,
396 &tool_prefs,
397 &template_prefs,
398 &build_info,
399 filetypes_by_title
402 geany_data = gd;
406 /* Prevent the same plugin filename being loaded more than once.
407 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
408 static gboolean
409 plugin_loaded(GModule *module)
411 gchar *basename_module, *basename_loaded;
412 GList *item;
414 basename_module = g_path_get_basename(g_module_name(module));
415 for (item = plugin_list; item != NULL; item = g_list_next(item))
417 basename_loaded = g_path_get_basename(
418 g_module_name(((Plugin*)item->data)->module));
420 if (utils_str_equal(basename_module, basename_loaded))
422 g_free(basename_loaded);
423 g_free(basename_module);
424 return TRUE;
426 g_free(basename_loaded);
428 /* Look also through the list of active plugins. This prevents problems when we have the same
429 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
430 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
431 * the plugin manager would list both plugins. Additionally, unloading the active plugin
432 * would cause a crash. */
433 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
435 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
437 if (utils_str_equal(basename_module, basename_loaded))
439 g_free(basename_loaded);
440 g_free(basename_module);
441 return TRUE;
443 g_free(basename_loaded);
445 g_free(basename_module);
446 return FALSE;
450 static Plugin *find_active_plugin_by_name(const gchar *filename)
452 GList *item;
454 g_return_val_if_fail(filename, FALSE);
456 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
458 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
459 return item->data;
462 return NULL;
466 static gboolean
467 plugin_check_version(GModule *module)
469 gint (*version_check)(gint) = NULL;
471 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
473 if (G_UNLIKELY(! version_check))
475 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
476 g_module_name(module));
477 return FALSE;
479 else
481 gint result = version_check(GEANY_ABI_VERSION);
483 if (result < 0)
485 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
486 "release of Geany - please recompile it."), g_module_name(module));
487 geany_debug("Plugin \"%s\" is not binary compatible with this "
488 "release of Geany - recompile it.", g_module_name(module));
489 return FALSE;
491 if (result > GEANY_API_VERSION)
493 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
494 g_module_name(module), result);
495 return FALSE;
498 return TRUE;
502 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
504 PluginCallback *cb;
505 guint i, len = 0;
507 while (TRUE)
509 cb = &callbacks[len];
510 if (!cb->signal_name || !cb->callback)
511 break;
512 len++;
514 if (len == 0)
515 return;
517 for (i = 0; i < len; i++)
519 cb = &callbacks[i];
521 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
522 cb->callback, cb->user_data);
527 static void read_key_group(Plugin *plugin)
529 GeanyKeyGroupInfo *p_key_info;
530 GeanyKeyGroup **p_key_group;
532 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
533 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
534 if (p_key_info && p_key_group)
536 GeanyKeyGroupInfo *key_info = p_key_info;
538 if (*p_key_group)
539 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
540 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
541 plugin->info.name);
542 else
544 if (key_info->count)
546 GeanyKeyGroup *key_group =
547 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
548 if (key_group)
549 *p_key_group = key_group;
551 else
552 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
553 "count field is zero. Maybe use plugin_set_key_group() instead?",
554 plugin->info.name);
557 else if (p_key_info || p_key_group)
558 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
559 "Maybe use plugin_set_key_group() instead?",
560 plugin->info.name);
564 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
566 const Plugin *pa = a;
567 const Plugin *pb = b;
569 return strcmp(pa->info.name, pb->info.name);
573 static void
574 plugin_init(Plugin *plugin)
576 GeanyPlugin **p_geany_plugin;
577 PluginCallback *callbacks;
578 PluginInfo **p_info;
579 PluginFields **plugin_fields;
581 /* set these symbols before plugin_init() is called
582 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
583 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
584 if (p_geany_plugin)
585 *p_geany_plugin = &plugin->public;
586 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
587 if (p_info)
588 *p_info = &plugin->info;
589 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
590 if (plugin_fields)
591 *plugin_fields = &plugin->fields;
592 read_key_group(plugin);
594 /* start the plugin */
595 g_return_if_fail(plugin->init);
596 plugin->init(&geany_data);
598 /* store some function pointers for later use */
599 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
600 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
601 if (app->debug_mode && plugin->configure && plugin->configure_single)
602 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
603 "only plugin_configure() will be used!",
604 plugin->info.name);
606 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
607 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
608 if (plugin->cleanup == NULL)
610 if (app->debug_mode)
611 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
612 plugin->info.name);
615 /* now read any plugin-owned data that might have been set in plugin_init() */
617 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
619 ui_add_document_sensitive(plugin->fields.menu_item);
622 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
623 if (callbacks)
624 add_callbacks(plugin, callbacks);
626 /* remember which plugins are active.
627 * keep list sorted so tools menu items and plugin preference tabs are
628 * sorted by plugin name */
629 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
631 geany_debug("Loaded: %s (%s)", plugin->filename,
632 NVL(plugin->info.name, "<Unknown>"));
636 /* Load and optionally init a plugin.
637 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
638 * called, the plugin will be started, if not the plugin will be read only (for the list of
639 * available plugins in the plugin manager).
640 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
641 static Plugin*
642 plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
644 Plugin *plugin;
645 GModule *module;
646 GeanyData **p_geany_data;
647 GeanyFunctions **p_geany_functions;
648 void (*plugin_set_info)(PluginInfo*);
650 g_return_val_if_fail(fname, NULL);
651 g_return_val_if_fail(g_module_supported(), NULL);
653 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
654 * load the module */
655 plugin = find_active_plugin_by_name(fname);
656 if (plugin != NULL)
658 geany_debug("Plugin \"%s\" already loaded.", fname);
659 if (add_to_list)
661 /* do not add the to list twice */
662 if (g_list_find(plugin_list, plugin) != NULL)
663 return NULL;
665 plugin_list = g_list_prepend(plugin_list, plugin);
667 return plugin;
670 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
671 * causing a segfault. Without that flag the module will safely fail to load.
672 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
673 * otherwise not be detected due to the shadowing of Geany's app variable.
674 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
675 * function from a plugin will be shadowed. */
676 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
677 if (! module)
679 geany_debug("Can't load plugin: %s", g_module_error());
680 return NULL;
683 if (plugin_loaded(module))
685 geany_debug("Plugin \"%s\" already loaded.", fname);
687 if (! g_module_close(module))
688 g_warning("%s: %s", fname, g_module_error());
689 return NULL;
692 if (! plugin_check_version(module))
694 if (! g_module_close(module))
695 g_warning("%s: %s", fname, g_module_error());
696 return NULL;
699 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
700 if (plugin_set_info == NULL)
702 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
704 if (! g_module_close(module))
705 g_warning("%s: %s", fname, g_module_error());
706 return NULL;
709 plugin = g_new0(Plugin, 1);
711 /* set basic fields here to allow plugins to call Geany functions in set_info() */
712 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
713 if (p_geany_data)
714 *p_geany_data = &geany_data;
715 g_module_symbol(module, "geany_functions", (void *) &p_geany_functions);
716 if (p_geany_functions)
717 *p_geany_functions = &geany_functions;
719 /* read plugin name, etc. */
720 plugin_set_info(&plugin->info);
721 if (G_UNLIKELY(! NZV(plugin->info.name)))
723 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
724 fname);
726 if (! g_module_close(module))
727 g_warning("%s: %s", fname, g_module_error());
728 g_free(plugin);
729 return NULL;
732 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
733 if (plugin->init == NULL)
735 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
736 plugin->info.name);
738 if (! g_module_close(module))
739 g_warning("%s: %s", fname, g_module_error());
740 g_free(plugin);
741 return NULL;
743 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
745 plugin->filename = g_strdup(fname);
746 plugin->module = module;
747 plugin->public.info = &plugin->info;
748 plugin->public.priv = plugin;
750 if (init_plugin)
751 plugin_init(plugin);
753 if (add_to_list)
754 plugin_list = g_list_prepend(plugin_list, plugin);
756 return plugin;
760 static void remove_callbacks(Plugin *plugin)
762 GArray *signal_ids = plugin->signal_ids;
763 SignalConnection *sc;
765 if (signal_ids == NULL)
766 return;
768 foreach_array(SignalConnection, sc, signal_ids)
769 g_signal_handler_disconnect(sc->object, sc->handler_id);
771 g_array_free(signal_ids, TRUE);
775 static void remove_sources(Plugin *plugin)
777 GList *item;
779 item = plugin->sources;
780 while (item != NULL)
782 GList *next = item->next; /* cache the next pointer because current item will be freed */
784 g_source_destroy(item->data);
785 item = next;
787 /* don't free the list here, it is allocated inside each source's data */
791 static gboolean is_active_plugin(Plugin *plugin)
793 return (g_list_find(active_plugin_list, plugin) != NULL);
797 /* Clean up anything used by an active plugin */
798 static void
799 plugin_cleanup(Plugin *plugin)
801 GtkWidget *widget;
803 if (plugin->cleanup)
804 plugin->cleanup();
806 remove_callbacks(plugin);
807 remove_sources(plugin);
809 if (plugin->key_group)
810 keybindings_free_group(plugin->key_group);
812 widget = plugin->toolbar_separator.widget;
813 if (widget)
814 gtk_widget_destroy(widget);
816 geany_debug("Unloaded: %s", plugin->filename);
820 static void
821 plugin_free(Plugin *plugin)
823 g_return_if_fail(plugin);
824 g_return_if_fail(plugin->module);
826 if (is_active_plugin(plugin))
827 plugin_cleanup(plugin);
829 active_plugin_list = g_list_remove(active_plugin_list, plugin);
831 if (! g_module_close(plugin->module))
832 g_warning("%s: %s", plugin->filename, g_module_error());
834 plugin_list = g_list_remove(plugin_list, plugin);
836 g_free(plugin->filename);
837 g_free(plugin);
838 plugin = NULL;
842 /* load active plugins at startup */
843 static void
844 load_active_plugins(void)
846 guint i, len;
848 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
849 return;
851 for (i = 0; i < len; i++)
853 const gchar *fname = active_plugins_pref[i];
855 if (NZV(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
857 if (plugin_new(fname, TRUE, FALSE) == NULL)
858 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
864 static void
865 load_plugins_from_path(const gchar *path)
867 GSList *list, *item;
868 gchar *fname, *tmp;
869 gint count = 0;
871 list = utils_get_file_list(path, NULL, NULL);
873 for (item = list; item != NULL; item = g_slist_next(item))
875 tmp = strrchr(item->data, '.');
876 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
877 continue;
879 fname = g_strconcat(path, G_DIR_SEPARATOR_S, item->data, NULL);
880 if (plugin_new(fname, FALSE, TRUE))
881 count++;
882 g_free(fname);
885 g_slist_foreach(list, (GFunc) g_free, NULL);
886 g_slist_free(list);
888 if (count)
889 geany_debug("Found %d plugin(s) in '%s'.", count, path);
893 static gchar *get_plugin_path(void)
895 #ifdef G_OS_WIN32
896 gchar *path;
897 gchar *install_dir = win32_get_installation_dir();
899 path = g_strconcat(install_dir, "\\lib", NULL);
900 g_free(install_dir);
902 return path;
903 #else
904 return g_strconcat(GEANY_LIBDIR, G_DIR_SEPARATOR_S "geany", NULL);
905 #endif
909 static gboolean validate_custom_plugin_path(const gchar *plugin_path_custom,
910 const gchar *plugin_path_config,
911 const gchar *plugin_path_system)
913 /* check whether the custom plugin path is one of the system or user plugin paths
914 * and abort if so */
915 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
916 utils_str_equal(plugin_path_custom, plugin_path_system))
917 return FALSE;
919 return TRUE;
923 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
924 static void load_all_plugins(void)
926 gchar *plugin_path_config;
927 gchar *plugin_path_system;
929 plugin_path_config = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", NULL);
930 plugin_path_system = get_plugin_path();
932 /* first load plugins in ~/.config/geany/plugins/ */
933 load_plugins_from_path(plugin_path_config);
935 /* load plugins from a custom path */
936 if (NZV(prefs.custom_plugin_path))
938 gchar *plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
939 utils_tidy_path(plugin_path_custom);
941 if (validate_custom_plugin_path(plugin_path_custom, plugin_path_config, plugin_path_system))
942 load_plugins_from_path(plugin_path_custom);
944 g_free(plugin_path_custom);
947 /* finally load plugins from $prefix/lib/geany */
948 load_plugins_from_path(plugin_path_system);
950 g_free(plugin_path_config);
951 g_free(plugin_path_system);
955 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
957 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
958 guint i = 0;
959 gboolean have_plugin_menu_items = FALSE;
961 for (item = list; item != NULL; item = g_list_next(item))
963 if (item->data == menu_separator)
965 if (i < g_list_length(list) - 1)
967 have_plugin_menu_items = TRUE;
968 break;
971 i++;
973 g_list_free(list);
975 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
979 /* Calling this starts up plugin support */
980 void plugins_load_active(void)
982 GtkWidget *widget;
984 want_plugins = TRUE;
986 geany_data_init();
988 widget = gtk_separator_menu_item_new();
989 gtk_widget_show(widget);
990 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
992 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
993 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
994 gtk_widget_show(widget);
995 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
997 menu_separator = gtk_separator_menu_item_new();
998 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
999 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
1001 load_active_plugins();
1005 static void update_active_plugins_pref(void)
1007 gint i = 0;
1008 GList *list;
1009 gsize count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
1011 g_strfreev(active_plugins_pref);
1013 if (count == 0)
1015 active_plugins_pref = NULL;
1016 return;
1019 active_plugins_pref = g_new0(gchar*, count + 1);
1021 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
1023 Plugin *plugin = list->data;
1025 active_plugins_pref[i] = g_strdup(plugin->filename);
1026 i++;
1028 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
1030 const gchar *fname = list->data;
1032 active_plugins_pref[i] = g_strdup(fname);
1033 i++;
1035 active_plugins_pref[i] = NULL;
1039 static void on_save_settings(GKeyFile *config)
1041 /* if plugins are disabled, don't clear list of active plugins */
1042 if (want_plugins)
1043 update_active_plugins_pref();
1047 /* called even if plugin support is disabled */
1048 void plugins_init(void)
1050 StashGroup *group;
1052 group = stash_group_new("plugins");
1053 configuration_add_pref_group(group, TRUE);
1055 stash_group_add_toggle_button(group, &prefs.load_plugins,
1056 "load_plugins", TRUE, "check_plugins");
1057 stash_group_add_entry(group, &prefs.custom_plugin_path,
1058 "custom_plugin_path", "", "extra_plugin_path_entry");
1060 g_signal_connect(geany_object, "save-settings", G_CALLBACK(on_save_settings), NULL);
1061 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1065 /* called even if plugin support is disabled */
1066 void plugins_finalize(void)
1068 if (failed_plugins_list != NULL)
1070 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1071 g_list_free(failed_plugins_list);
1073 if (active_plugin_list != NULL)
1075 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1076 g_list_free(active_plugin_list);
1078 g_strfreev(active_plugins_pref);
1082 /* Check whether there are any plugins loaded which provide a configure symbol */
1083 gboolean plugins_have_preferences(void)
1085 GList *item;
1087 if (active_plugin_list == NULL)
1088 return FALSE;
1090 foreach_list(item, active_plugin_list)
1092 Plugin *plugin = item->data;
1093 if (plugin->configure != NULL || plugin->configure_single != NULL)
1094 return TRUE;
1097 return FALSE;
1101 /* Plugin Manager */
1103 enum
1105 PLUGIN_COLUMN_CHECK = 0,
1106 PLUGIN_COLUMN_NAME,
1107 PLUGIN_COLUMN_FILE,
1108 PLUGIN_COLUMN_PLUGIN,
1109 PLUGIN_N_COLUMNS,
1110 PM_BUTTON_CONFIGURE,
1111 PM_BUTTON_HELP
1114 typedef struct
1116 GtkWidget *dialog;
1117 GtkWidget *tree;
1118 GtkListStore *store;
1119 GtkWidget *plugin_label;
1120 GtkWidget *description_label;
1121 GtkWidget *author_label;
1122 GtkWidget *configure_button;
1123 GtkWidget *help_button;
1125 PluginManagerWidgets;
1127 static PluginManagerWidgets pm_widgets;
1130 static void pm_update_buttons(Plugin *p)
1132 gboolean is_active;
1134 is_active = is_active_plugin(p);
1135 gtk_widget_set_sensitive(pm_widgets.configure_button,
1136 (p->configure || p->configure_single) && is_active);
1137 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
1141 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1143 GtkTreeIter iter;
1144 GtkTreeModel *model;
1145 Plugin *p;
1147 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1149 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1151 if (p != NULL)
1153 gchar *text;
1154 PluginInfo *pi;
1156 pi = &p->info;
1157 /* Translators: <plugin name> <plugin version> */
1158 text = g_strdup_printf(_("%s %s"), pi->name, pi->version);
1159 gtk_label_set_text(GTK_LABEL(pm_widgets.plugin_label), text);
1160 gtk_label_set_text(GTK_LABEL(pm_widgets.description_label), pi->description);
1161 gtk_label_set_text(GTK_LABEL(pm_widgets.author_label), pi->author);
1162 g_free(text);
1164 pm_update_buttons(p);
1170 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1172 gboolean old_state, state;
1173 gchar *file_name;
1174 GtkTreeIter iter;
1175 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1176 Plugin *p;
1178 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
1179 gtk_tree_path_free(path);
1181 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
1182 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
1184 /* no plugins item */
1185 if (p == NULL)
1186 return;
1188 state = ! old_state; /* toggle the state */
1190 /* save the filename of the plugin */
1191 file_name = g_strdup(p->filename);
1193 /* unload plugin module */
1194 if (!state)
1195 /* save shortcuts (only need this group, but it doesn't take long) */
1196 keybindings_write_to_file();
1198 plugin_free(p);
1200 /* reload plugin module and initialize it if item is checked */
1201 p = plugin_new(file_name, state, TRUE);
1202 if (!p)
1204 /* plugin file may no longer be on disk, or is now incompatible */
1205 gtk_list_store_remove(pm_widgets.store, &iter);
1207 else
1209 if (state)
1210 keybindings_load_keyfile(); /* load shortcuts */
1212 /* update model */
1213 gtk_list_store_set(pm_widgets.store, &iter,
1214 PLUGIN_COLUMN_CHECK, state,
1215 PLUGIN_COLUMN_PLUGIN, p, -1);
1217 /* set again the sensitiveness of the configure and help buttons */
1218 pm_update_buttons(p);
1220 g_free(file_name);
1224 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1226 GtkCellRenderer *text_renderer, *checkbox_renderer;
1227 GtkTreeViewColumn *column;
1228 GtkTreeIter iter;
1229 GList *list;
1230 GtkTreeSelection *sel;
1232 checkbox_renderer = gtk_cell_renderer_toggle_new();
1233 column = gtk_tree_view_column_new_with_attributes(
1234 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1235 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1236 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1238 text_renderer = gtk_cell_renderer_text_new();
1239 column = gtk_tree_view_column_new_with_attributes(
1240 _("Plugin"), text_renderer, "text", PLUGIN_COLUMN_NAME, NULL);
1241 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1243 text_renderer = gtk_cell_renderer_text_new();
1244 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1245 column = gtk_tree_view_column_new_with_attributes(
1246 _("File"), text_renderer, "text", PLUGIN_COLUMN_FILE, NULL);
1247 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1249 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1250 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1251 gtk_tree_sortable_set_sort_column_id(
1252 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_NAME, GTK_SORT_ASCENDING);
1254 /* selection handling */
1255 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1256 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1257 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1259 list = g_list_first(plugin_list);
1260 if (list == NULL)
1262 gtk_list_store_append(store, &iter);
1263 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1264 PLUGIN_COLUMN_NAME, _("No plugins available."),
1265 PLUGIN_COLUMN_FILE, "", PLUGIN_COLUMN_PLUGIN, NULL, -1);
1267 else
1269 Plugin *p;
1270 for (; list != NULL; list = list->next)
1272 p = list->data;
1274 gtk_list_store_append(store, &iter);
1275 gtk_list_store_set(store, &iter,
1276 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1277 PLUGIN_COLUMN_NAME, p->info.name,
1278 PLUGIN_COLUMN_FILE, p->filename,
1279 PLUGIN_COLUMN_PLUGIN, p,
1280 -1);
1283 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1284 g_object_unref(store);
1288 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1290 GtkTreeModel *model;
1291 GtkTreeSelection *selection;
1292 GtkTreeIter iter;
1293 Plugin *p;
1295 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1296 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1298 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1300 if (p != NULL)
1302 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1303 plugin_show_configure(&p->public);
1304 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1305 p->help();
1311 static void
1312 free_non_active_plugin(gpointer data, gpointer user_data)
1314 Plugin *plugin = data;
1316 /* don't do anything when closing the plugin manager and it is an active plugin */
1317 if (is_active_plugin(plugin))
1318 return;
1320 plugin_free(plugin);
1324 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1326 if (plugin_list != NULL)
1328 /* remove all non-active plugins from the list */
1329 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1330 g_list_free(plugin_list);
1331 plugin_list = NULL;
1333 gtk_widget_destroy(GTK_WIDGET(dialog));
1337 static GtkWidget *create_table_label(const gchar *text)
1339 GtkWidget *label;
1340 PangoAttrList *attrs;
1342 attrs = pango_attr_list_new();
1343 pango_attr_list_insert(attrs, pango_attr_weight_new(PANGO_WEIGHT_BOLD));
1344 label = gtk_label_new(text);
1345 gtk_label_set_attributes(GTK_LABEL(label), attrs);
1346 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1347 pango_attr_list_unref(attrs);
1349 return label;
1353 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1355 GtkWidget *vbox, *vbox2, *label_vbox, *hbox, *swin, *label, *label2, *desc_win, *table, *paned;
1357 /* before showing the dialog, we need to create the list of available plugins */
1358 load_all_plugins();
1360 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1361 GTK_DIALOG_DESTROY_WITH_PARENT,
1362 GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
1363 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1364 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1365 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1367 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1369 pm_widgets.tree = gtk_tree_view_new();
1370 pm_widgets.store = gtk_list_store_new(
1371 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1372 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1374 swin = gtk_scrolled_window_new(NULL, NULL);
1375 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1376 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1377 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1378 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1380 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1382 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1383 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1384 g_signal_connect(pm_widgets.configure_button, "clicked",
1385 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1387 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1388 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1389 g_signal_connect(pm_widgets.help_button, "clicked",
1390 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1392 label2 = gtk_label_new(_("<b>Plugin details:</b>"));
1393 gtk_label_set_use_markup(GTK_LABEL(label2), TRUE);
1394 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
1396 table = gtk_table_new(3, 2, FALSE);
1397 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
1398 pm_widgets.plugin_label = geany_wrap_label_new(NULL);
1399 pm_widgets.description_label = geany_wrap_label_new(NULL);
1400 pm_widgets.author_label = geany_wrap_label_new(NULL);
1401 gtk_table_attach(GTK_TABLE(table), create_table_label(_("Plugin:")), 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
1402 gtk_table_attach(GTK_TABLE(table), create_table_label(_("Description:")), 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
1403 gtk_table_attach(GTK_TABLE(table), create_table_label(_("Author(s):")), 0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
1404 gtk_table_attach(GTK_TABLE(table), pm_widgets.plugin_label, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1405 gtk_table_attach(GTK_TABLE(table), pm_widgets.description_label, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1406 gtk_table_attach(GTK_TABLE(table), pm_widgets.author_label, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1408 desc_win = gtk_scrolled_window_new(NULL, NULL);
1409 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win),
1410 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1411 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win), table);
1413 hbox = gtk_hbox_new(FALSE, 0);
1414 gtk_box_pack_start(GTK_BOX(hbox), label2, TRUE, TRUE, 0);
1415 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 4);
1416 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1418 label_vbox = gtk_vbox_new(FALSE, 3);
1419 gtk_box_pack_start(GTK_BOX(label_vbox), hbox, FALSE, FALSE, 0);
1420 gtk_box_pack_start(GTK_BOX(label_vbox), desc_win, TRUE, TRUE, 0);
1422 paned = gtk_vpaned_new();
1423 gtk_paned_pack1(GTK_PANED(paned), swin, TRUE, FALSE);
1424 gtk_paned_pack2(GTK_PANED(paned), label_vbox, FALSE, FALSE);
1426 vbox2 = gtk_vbox_new(FALSE, 3);
1427 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1428 gtk_box_pack_start(GTK_BOX(vbox2), paned, TRUE, TRUE, 0);
1430 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1432 gtk_container_add(GTK_CONTAINER(vbox), vbox2);
1433 gtk_widget_show_all(pm_widgets.dialog);
1437 #endif