Add a filter entry in the plugin manager dialog
[geany-mirror.git] / src / plugins.c
blob946e1c983c3e8b2b78f8fb200bf2064bdeebce1f
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);
79 static PluginFuncs plugin_funcs = {
80 &plugin_add_toolbar_item,
81 &plugin_module_make_resident,
82 &plugin_signal_connect,
83 &plugin_set_key_group,
84 &plugin_show_configure,
85 &plugin_timeout_add,
86 &plugin_timeout_add_seconds,
87 &plugin_idle_add,
88 &plugin_builder_connect_signals
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_force,
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,
112 &document_compare_by_display_name,
113 &document_compare_by_tab_order,
114 &document_compare_by_tab_order_reverse,
115 &document_find_by_id
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,
250 &ui_lookup_stock_label
253 static DialogFuncs dialog_funcs = {
254 &dialogs_show_question,
255 &dialogs_show_msgbox,
256 &dialogs_show_save_as,
257 &dialogs_show_input_numeric,
258 &dialogs_show_input
261 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
262 #define _lookup_widget_macro ui_lookup_widget
264 /* deprecated */
265 static SupportFuncs support_funcs = {
266 &_lookup_widget_macro
269 static MsgWinFuncs msgwin_funcs = {
270 &msgwin_status_add,
271 &msgwin_compiler_add,
272 &msgwin_msg_add,
273 &msgwin_clear_tab,
274 &msgwin_switch_tab,
275 &msgwin_set_messages_dir
278 static EncodingFuncs encoding_funcs = {
279 &encodings_convert_to_utf8,
280 &encodings_convert_to_utf8_from_charset,
281 &encodings_get_charset_from_index
284 static KeybindingFuncs keybindings_funcs = {
285 &keybindings_send_command,
286 &keybindings_set_item,
287 &keybindings_get_item,
288 &keybindings_get_modifiers
291 static TagManagerFuncs tagmanager_funcs = {
292 &tm_get_real_path,
293 &tm_source_file_new,
294 &tm_source_file_free,
295 &tm_workspace_add_source_file,
296 &tm_workspace_remove_source_file,
297 &tm_workspace_add_source_files,
298 &tm_workspace_remove_source_files
301 static SearchFuncs search_funcs = {
302 &search_show_find_in_files_dialog
305 static HighlightingFuncs highlighting_funcs = {
306 &highlighting_get_style,
307 &highlighting_set_styles,
308 &highlighting_is_string_style,
309 &highlighting_is_comment_style,
310 &highlighting_is_code_style
313 static FiletypeFuncs filetype_funcs = {
314 &filetypes_detect_from_file,
315 &filetypes_lookup_by_name,
316 &filetypes_index,
317 &filetypes_get_display_name,
318 &filetypes_get_sorted_by_name
321 static NavQueueFuncs navqueue_funcs = {
322 &navqueue_goto_line
325 static MainFuncs main_funcs = {
326 &main_reload_configuration,
327 &main_locale_init,
328 &main_is_realized
331 static StashFuncs stash_funcs = {
332 &stash_group_new,
333 &stash_group_add_boolean,
334 &stash_group_add_integer,
335 &stash_group_add_string,
336 &stash_group_add_string_vector,
337 &stash_group_load_from_key_file,
338 &stash_group_save_to_key_file,
339 &stash_group_free,
340 &stash_group_load_from_file,
341 &stash_group_save_to_file,
342 &stash_group_add_toggle_button,
343 &stash_group_add_radio_buttons,
344 &stash_group_add_spin_button_integer,
345 &stash_group_add_combo_box,
346 &stash_group_add_combo_box_entry,
347 &stash_group_add_entry,
348 &stash_group_add_widget_property,
349 &stash_group_display,
350 &stash_group_update,
351 &stash_group_free_settings
354 static SymbolsFuncs symbols_funcs = {
355 &symbols_get_context_separator
358 static BuildFuncs build_funcs = {
359 &build_activate_menu_item,
360 &build_get_current_menu_item,
361 &build_remove_menu_item,
362 &build_set_menu_item,
363 &build_get_group_count
366 static ProjectFuncs project_funcs = {
367 &project_write_config
370 static GeanyFunctions geany_functions = {
371 &doc_funcs,
372 &sci_funcs,
373 &template_funcs,
374 &utils_funcs,
375 &uiutils_funcs,
376 &support_funcs,
377 &dialog_funcs,
378 &msgwin_funcs,
379 &encoding_funcs,
380 &keybindings_funcs,
381 &tagmanager_funcs,
382 &search_funcs,
383 &highlighting_funcs,
384 &filetype_funcs,
385 &navqueue_funcs,
386 &editor_funcs,
387 &main_funcs,
388 &plugin_funcs,
389 &scintilla_funcs,
390 &msgwin_funcs,
391 &stash_funcs,
392 &symbols_funcs,
393 &build_funcs,
394 &project_funcs
397 static GeanyData geany_data;
400 static void
401 geany_data_init(void)
403 GeanyData gd = {
404 app,
405 &main_widgets,
406 documents_array,
407 filetypes_array,
408 &prefs,
409 &interface_prefs,
410 &toolbar_prefs,
411 &editor_prefs,
412 &file_prefs,
413 &search_prefs,
414 &tool_prefs,
415 &template_prefs,
416 &build_info,
417 filetypes_by_title
420 geany_data = gd;
424 /* Prevent the same plugin filename being loaded more than once.
425 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
426 static gboolean
427 plugin_loaded(GModule *module)
429 gchar *basename_module, *basename_loaded;
430 GList *item;
432 basename_module = g_path_get_basename(g_module_name(module));
433 for (item = plugin_list; item != NULL; item = g_list_next(item))
435 basename_loaded = g_path_get_basename(
436 g_module_name(((Plugin*)item->data)->module));
438 if (utils_str_equal(basename_module, basename_loaded))
440 g_free(basename_loaded);
441 g_free(basename_module);
442 return TRUE;
444 g_free(basename_loaded);
446 /* Look also through the list of active plugins. This prevents problems when we have the same
447 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
448 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
449 * the plugin manager would list both plugins. Additionally, unloading the active plugin
450 * would cause a crash. */
451 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
453 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
455 if (utils_str_equal(basename_module, basename_loaded))
457 g_free(basename_loaded);
458 g_free(basename_module);
459 return TRUE;
461 g_free(basename_loaded);
463 g_free(basename_module);
464 return FALSE;
468 static Plugin *find_active_plugin_by_name(const gchar *filename)
470 GList *item;
472 g_return_val_if_fail(filename, FALSE);
474 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
476 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
477 return item->data;
480 return NULL;
484 static gboolean
485 plugin_check_version(GModule *module)
487 gint (*version_check)(gint) = NULL;
489 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
491 if (G_UNLIKELY(! version_check))
493 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
494 g_module_name(module));
495 return FALSE;
497 else
499 gint result = version_check(GEANY_ABI_VERSION);
501 if (result < 0)
503 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
504 "release of Geany - please recompile it."), g_module_name(module));
505 geany_debug("Plugin \"%s\" is not binary compatible with this "
506 "release of Geany - recompile it.", g_module_name(module));
507 return FALSE;
509 if (result > GEANY_API_VERSION)
511 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
512 g_module_name(module), result);
513 return FALSE;
516 return TRUE;
520 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
522 PluginCallback *cb;
523 guint i, len = 0;
525 while (TRUE)
527 cb = &callbacks[len];
528 if (!cb->signal_name || !cb->callback)
529 break;
530 len++;
532 if (len == 0)
533 return;
535 for (i = 0; i < len; i++)
537 cb = &callbacks[i];
539 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
540 cb->callback, cb->user_data);
545 static void read_key_group(Plugin *plugin)
547 GeanyKeyGroupInfo *p_key_info;
548 GeanyKeyGroup **p_key_group;
550 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
551 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
552 if (p_key_info && p_key_group)
554 GeanyKeyGroupInfo *key_info = p_key_info;
556 if (*p_key_group)
557 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
558 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
559 plugin->info.name);
560 else
562 if (key_info->count)
564 GeanyKeyGroup *key_group =
565 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
566 if (key_group)
567 *p_key_group = key_group;
569 else
570 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
571 "count field is zero. Maybe use plugin_set_key_group() instead?",
572 plugin->info.name);
575 else if (p_key_info || p_key_group)
576 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
577 "Maybe use plugin_set_key_group() instead?",
578 plugin->info.name);
582 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
584 const Plugin *pa = a;
585 const Plugin *pb = b;
587 return strcmp(pa->info.name, pb->info.name);
591 static void
592 plugin_load(Plugin *plugin)
594 GeanyPlugin **p_geany_plugin;
595 PluginCallback *callbacks;
596 PluginInfo **p_info;
597 PluginFields **plugin_fields;
599 /* set these symbols before plugin_init() is called
600 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
601 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
602 if (p_geany_plugin)
603 *p_geany_plugin = &plugin->public;
604 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
605 if (p_info)
606 *p_info = &plugin->info;
607 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
608 if (plugin_fields)
609 *plugin_fields = &plugin->fields;
610 read_key_group(plugin);
612 /* start the plugin */
613 g_return_if_fail(plugin->init);
614 plugin->init(&geany_data);
616 /* store some function pointers for later use */
617 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
618 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
619 if (app->debug_mode && plugin->configure && plugin->configure_single)
620 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
621 "only plugin_configure() will be used!",
622 plugin->info.name);
624 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
625 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
626 if (plugin->cleanup == NULL)
628 if (app->debug_mode)
629 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
630 plugin->info.name);
633 /* now read any plugin-owned data that might have been set in plugin_init() */
635 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
637 ui_add_document_sensitive(plugin->fields.menu_item);
640 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
641 if (callbacks)
642 add_callbacks(plugin, callbacks);
644 /* remember which plugins are active.
645 * keep list sorted so tools menu items and plugin preference tabs are
646 * sorted by plugin name */
647 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
649 geany_debug("Loaded: %s (%s)", plugin->filename,
650 FALLBACK(plugin->info.name, "<Unknown>"));
654 /* Load and optionally init a plugin.
655 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
656 * called, the plugin will be started, if not the plugin will be read only (for the list of
657 * available plugins in the plugin manager).
658 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
659 static Plugin*
660 plugin_new(const gchar *fname, gboolean load_plugin, gboolean add_to_list)
662 Plugin *plugin;
663 GModule *module;
664 GeanyData **p_geany_data;
665 GeanyFunctions **p_geany_functions;
666 void (*plugin_set_info)(PluginInfo*);
668 g_return_val_if_fail(fname, NULL);
669 g_return_val_if_fail(g_module_supported(), NULL);
671 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
672 * load the module */
673 plugin = find_active_plugin_by_name(fname);
674 if (plugin != NULL)
676 geany_debug("Plugin \"%s\" already loaded.", fname);
677 if (add_to_list)
679 /* do not add to the list twice */
680 if (g_list_find(plugin_list, plugin) != NULL)
681 return NULL;
683 plugin_list = g_list_prepend(plugin_list, plugin);
685 return plugin;
688 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
689 * causing a segfault. Without that flag the module will safely fail to load.
690 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
691 * otherwise not be detected due to the shadowing of Geany's app variable.
692 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
693 * function from a plugin will be shadowed. */
694 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
695 if (! module)
697 geany_debug("Can't load plugin: %s", g_module_error());
698 return NULL;
701 if (plugin_loaded(module))
703 geany_debug("Plugin \"%s\" already loaded.", fname);
705 if (! g_module_close(module))
706 g_warning("%s: %s", fname, g_module_error());
707 return NULL;
710 if (! plugin_check_version(module))
712 if (! g_module_close(module))
713 g_warning("%s: %s", fname, g_module_error());
714 return NULL;
717 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
718 if (plugin_set_info == NULL)
720 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
722 if (! g_module_close(module))
723 g_warning("%s: %s", fname, g_module_error());
724 return NULL;
727 plugin = g_new0(Plugin, 1);
729 /* set basic fields here to allow plugins to call Geany functions in set_info() */
730 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
731 if (p_geany_data)
732 *p_geany_data = &geany_data;
733 g_module_symbol(module, "geany_functions", (void *) &p_geany_functions);
734 if (p_geany_functions)
735 *p_geany_functions = &geany_functions;
737 /* read plugin name, etc. */
738 plugin_set_info(&plugin->info);
739 if (G_UNLIKELY(EMPTY(plugin->info.name)))
741 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
742 fname);
744 if (! g_module_close(module))
745 g_warning("%s: %s", fname, g_module_error());
746 g_free(plugin);
747 return NULL;
750 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
751 if (plugin->init == NULL)
753 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
754 plugin->info.name);
756 if (! g_module_close(module))
757 g_warning("%s: %s", fname, g_module_error());
758 g_free(plugin);
759 return NULL;
761 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
763 plugin->filename = g_strdup(fname);
764 plugin->module = module;
765 plugin->public.info = &plugin->info;
766 plugin->public.priv = plugin;
768 if (load_plugin)
769 plugin_load(plugin);
771 if (add_to_list)
772 plugin_list = g_list_prepend(plugin_list, plugin);
774 return plugin;
778 static void on_object_weak_notify(gpointer data, GObject *old_ptr)
780 Plugin *plugin = data;
781 guint i = 0;
783 g_return_if_fail(plugin && plugin->signal_ids);
785 for (i = 0; i < plugin->signal_ids->len; i++)
787 SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
789 if (sc->object == old_ptr)
791 g_array_remove_index_fast(plugin->signal_ids, i);
792 /* we can break the loop right after finding the first match,
793 * because we will get one notification per connected signal */
794 break;
800 /* add an object to watch for destruction, and release pointers to it when destroyed.
801 * this should only be used by plugin_signal_connect() to add a watch on
802 * the object lifetime and nuke out references to it in plugin->signal_ids */
803 void plugin_watch_object(Plugin *plugin, gpointer object)
805 g_object_weak_ref(object, on_object_weak_notify, plugin);
809 static void remove_callbacks(Plugin *plugin)
811 GArray *signal_ids = plugin->signal_ids;
812 SignalConnection *sc;
814 if (signal_ids == NULL)
815 return;
817 foreach_array(SignalConnection, sc, signal_ids)
819 g_signal_handler_disconnect(sc->object, sc->handler_id);
820 g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
823 g_array_free(signal_ids, TRUE);
827 static void remove_sources(Plugin *plugin)
829 GList *item;
831 item = plugin->sources;
832 while (item != NULL)
834 GList *next = item->next; /* cache the next pointer because current item will be freed */
836 g_source_destroy(item->data);
837 item = next;
839 /* don't free the list here, it is allocated inside each source's data */
843 static gboolean is_active_plugin(Plugin *plugin)
845 return (g_list_find(active_plugin_list, plugin) != NULL);
849 /* Clean up anything used by an active plugin */
850 static void
851 plugin_cleanup(Plugin *plugin)
853 GtkWidget *widget;
855 if (plugin->cleanup)
856 plugin->cleanup();
858 remove_callbacks(plugin);
859 remove_sources(plugin);
861 if (plugin->key_group)
862 keybindings_free_group(plugin->key_group);
864 widget = plugin->toolbar_separator.widget;
865 if (widget)
866 gtk_widget_destroy(widget);
868 geany_debug("Unloaded: %s", plugin->filename);
872 static void
873 plugin_free(Plugin *plugin)
875 g_return_if_fail(plugin);
876 g_return_if_fail(plugin->module);
878 if (is_active_plugin(plugin))
879 plugin_cleanup(plugin);
881 active_plugin_list = g_list_remove(active_plugin_list, plugin);
883 if (! g_module_close(plugin->module))
884 g_warning("%s: %s", plugin->filename, g_module_error());
886 plugin_list = g_list_remove(plugin_list, plugin);
888 g_free(plugin->filename);
889 g_free(plugin);
890 plugin = NULL;
894 static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
895 const gchar *plugin_path_system)
897 gchar *plugin_path_custom;
899 if (EMPTY(prefs.custom_plugin_path))
900 return NULL;
902 plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
903 utils_tidy_path(plugin_path_custom);
905 /* check whether the custom plugin path is one of the system or user plugin paths
906 * and abort if so */
907 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
908 utils_str_equal(plugin_path_custom, plugin_path_system))
910 g_free(plugin_path_custom);
911 return NULL;
913 return plugin_path_custom;
917 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
918 * so we need to check active plugins are in the right place before loading */
919 static gboolean check_plugin_path(const gchar *fname)
921 gchar *plugin_path_config;
922 gchar *plugin_path_system;
923 gchar *plugin_path_custom;
924 gboolean ret = FALSE;
926 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
927 if (g_str_has_prefix(fname, plugin_path_config))
928 ret = TRUE;
930 plugin_path_system = get_plugin_path();
931 if (g_str_has_prefix(fname, plugin_path_system))
932 ret = TRUE;
934 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
935 if (plugin_path_custom)
937 if (g_str_has_prefix(fname, plugin_path_custom))
938 ret = TRUE;
940 g_free(plugin_path_custom);
942 g_free(plugin_path_config);
943 g_free(plugin_path_system);
944 return ret;
948 /* load active plugins at startup */
949 static void
950 load_active_plugins(void)
952 guint i, len;
954 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
955 return;
957 for (i = 0; i < len; i++)
959 const gchar *fname = active_plugins_pref[i];
961 if (!EMPTY(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
963 if (!check_plugin_path(fname) || plugin_new(fname, TRUE, FALSE) == NULL)
964 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
970 static void
971 load_plugins_from_path(const gchar *path)
973 GSList *list, *item;
974 gchar *fname, *tmp;
975 gint count = 0;
977 list = utils_get_file_list(path, NULL, NULL);
979 for (item = list; item != NULL; item = g_slist_next(item))
981 tmp = strrchr(item->data, '.');
982 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
983 continue;
985 fname = g_build_filename(path, item->data, NULL);
986 if (plugin_new(fname, FALSE, TRUE))
987 count++;
988 g_free(fname);
991 g_slist_foreach(list, (GFunc) g_free, NULL);
992 g_slist_free(list);
994 if (count)
995 geany_debug("Added %d plugin(s) in '%s'.", count, path);
999 static gchar *get_plugin_path(void)
1001 #ifdef G_OS_WIN32
1002 gchar *path;
1003 gchar *install_dir = win32_get_installation_dir();
1005 path = g_build_filename(install_dir, "lib", NULL);
1006 g_free(install_dir);
1008 return path;
1009 #else
1010 return g_build_filename(GEANY_LIBDIR, "geany", NULL);
1011 #endif
1015 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1016 static void load_all_plugins(void)
1018 gchar *plugin_path_config;
1019 gchar *plugin_path_system;
1020 gchar *plugin_path_custom;
1022 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
1023 plugin_path_system = get_plugin_path();
1025 /* first load plugins in ~/.config/geany/plugins/ */
1026 load_plugins_from_path(plugin_path_config);
1028 /* load plugins from a custom path */
1029 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
1030 if (plugin_path_custom)
1032 load_plugins_from_path(plugin_path_custom);
1033 g_free(plugin_path_custom);
1036 /* finally load plugins from $prefix/lib/geany */
1037 load_plugins_from_path(plugin_path_system);
1039 g_free(plugin_path_config);
1040 g_free(plugin_path_system);
1044 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
1046 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
1047 guint i = 0;
1048 gboolean have_plugin_menu_items = FALSE;
1050 for (item = list; item != NULL; item = g_list_next(item))
1052 if (item->data == menu_separator)
1054 if (i < g_list_length(list) - 1)
1056 have_plugin_menu_items = TRUE;
1057 break;
1060 i++;
1062 g_list_free(list);
1064 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
1068 /* Calling this starts up plugin support */
1069 void plugins_load_active(void)
1071 GtkWidget *widget;
1073 want_plugins = TRUE;
1075 geany_data_init();
1077 widget = gtk_separator_menu_item_new();
1078 gtk_widget_show(widget);
1079 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1081 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1082 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1083 gtk_widget_show(widget);
1084 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
1086 menu_separator = gtk_separator_menu_item_new();
1087 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
1088 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
1090 load_active_plugins();
1094 /* Update the global active plugins list so it's up-to-date when configuration
1095 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1096 static void update_active_plugins_pref(void)
1098 gint i = 0;
1099 GList *list;
1100 gsize count;
1102 /* if plugins are disabled, don't clear list of active plugins */
1103 if (!want_plugins)
1104 return;
1106 count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
1108 g_strfreev(active_plugins_pref);
1110 if (count == 0)
1112 active_plugins_pref = NULL;
1113 return;
1116 active_plugins_pref = g_new0(gchar*, count + 1);
1118 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
1120 Plugin *plugin = list->data;
1122 active_plugins_pref[i] = g_strdup(plugin->filename);
1123 i++;
1125 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
1127 const gchar *fname = list->data;
1129 active_plugins_pref[i] = g_strdup(fname);
1130 i++;
1132 active_plugins_pref[i] = NULL;
1136 /* called even if plugin support is disabled */
1137 void plugins_init(void)
1139 StashGroup *group;
1140 gchar *path;
1142 path = get_plugin_path();
1143 geany_debug("System plugin path: %s", path);
1144 g_free(path);
1146 group = stash_group_new("plugins");
1147 configuration_add_pref_group(group, TRUE);
1149 stash_group_add_toggle_button(group, &prefs.load_plugins,
1150 "load_plugins", TRUE, "check_plugins");
1151 stash_group_add_entry(group, &prefs.custom_plugin_path,
1152 "custom_plugin_path", "", "extra_plugin_path_entry");
1154 g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
1155 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1159 /* called even if plugin support is disabled */
1160 void plugins_finalize(void)
1162 if (failed_plugins_list != NULL)
1164 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1165 g_list_free(failed_plugins_list);
1167 if (active_plugin_list != NULL)
1169 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1170 g_list_free(active_plugin_list);
1172 g_strfreev(active_plugins_pref);
1176 /* Check whether there are any plugins loaded which provide a configure symbol */
1177 gboolean plugins_have_preferences(void)
1179 GList *item;
1181 if (active_plugin_list == NULL)
1182 return FALSE;
1184 foreach_list(item, active_plugin_list)
1186 Plugin *plugin = item->data;
1187 if (plugin->configure != NULL || plugin->configure_single != NULL)
1188 return TRUE;
1191 return FALSE;
1195 /* Plugin Manager */
1197 enum
1199 PLUGIN_COLUMN_CHECK = 0,
1200 PLUGIN_COLUMN_PLUGIN,
1201 PLUGIN_N_COLUMNS,
1202 PM_BUTTON_KEYBINDINGS,
1203 PM_BUTTON_CONFIGURE,
1204 PM_BUTTON_HELP
1207 typedef struct
1209 GtkWidget *dialog;
1210 GtkWidget *tree;
1211 GtkListStore *store;
1212 GtkWidget *filter_entry;
1213 GtkWidget *configure_button;
1214 GtkWidget *keybindings_button;
1215 GtkWidget *help_button;
1216 GtkWidget *popup_menu;
1217 GtkWidget *popup_configure_menu_item;
1218 GtkWidget *popup_keybindings_menu_item;
1219 GtkWidget *popup_help_menu_item;
1221 PluginManagerWidgets;
1223 static PluginManagerWidgets pm_widgets;
1226 static void pm_update_buttons(Plugin *p)
1228 gboolean is_active = FALSE;
1229 gboolean has_configure = FALSE;
1230 gboolean has_help = FALSE;
1231 gboolean has_keybindings = FALSE;
1233 if (p != NULL)
1235 is_active = is_active_plugin(p);
1236 has_configure = (p->configure || p->configure_single) && is_active;
1237 has_help = p->help != NULL && is_active;
1238 has_keybindings = p->key_group && p->key_group->plugin_key_count > 0 && is_active;
1241 gtk_widget_set_sensitive(pm_widgets.configure_button, has_configure);
1242 gtk_widget_set_sensitive(pm_widgets.help_button, has_help);
1243 gtk_widget_set_sensitive(pm_widgets.keybindings_button, has_keybindings);
1245 gtk_widget_set_sensitive(pm_widgets.popup_configure_menu_item, has_configure);
1246 gtk_widget_set_sensitive(pm_widgets.popup_help_menu_item, has_help);
1247 gtk_widget_set_sensitive(pm_widgets.popup_keybindings_menu_item, has_keybindings);
1251 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1253 GtkTreeIter iter;
1254 GtkTreeModel *model;
1255 Plugin *p;
1257 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1259 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1261 if (p != NULL)
1262 pm_update_buttons(p);
1267 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1269 gboolean old_state, state;
1270 gchar *file_name;
1271 GtkTreeIter iter;
1272 GtkTreeIter store_iter;
1273 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1274 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1275 Plugin *p;
1277 gtk_tree_model_get_iter(model, &iter, path);
1278 gtk_tree_path_free(path);
1280 gtk_tree_model_get(model, &iter,
1281 PLUGIN_COLUMN_CHECK, &old_state,
1282 PLUGIN_COLUMN_PLUGIN, &p, -1);
1284 /* no plugins item */
1285 if (p == NULL)
1286 return;
1288 gtk_tree_model_filter_convert_iter_to_child_iter(
1289 GTK_TREE_MODEL_FILTER(model), &store_iter, &iter);
1291 state = ! old_state; /* toggle the state */
1293 /* save the filename of the plugin */
1294 file_name = g_strdup(p->filename);
1296 /* unload plugin module */
1297 if (!state)
1298 /* save shortcuts (only need this group, but it doesn't take long) */
1299 keybindings_write_to_file();
1301 plugin_free(p);
1303 /* reload plugin module and initialize it if item is checked */
1304 p = plugin_new(file_name, state, TRUE);
1305 if (!p)
1307 /* plugin file may no longer be on disk, or is now incompatible */
1308 gtk_list_store_remove(pm_widgets.store, &store_iter);
1310 else
1312 if (state)
1313 keybindings_load_keyfile(); /* load shortcuts */
1315 /* update model */
1316 gtk_list_store_set(pm_widgets.store, &store_iter,
1317 PLUGIN_COLUMN_CHECK, state,
1318 PLUGIN_COLUMN_PLUGIN, p, -1);
1320 /* set again the sensitiveness of the configure and help buttons */
1321 pm_update_buttons(p);
1323 g_free(file_name);
1327 static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y,
1328 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
1330 GtkTreeModel *model;
1331 GtkTreeIter iter;
1332 GtkTreePath *path;
1333 Plugin *p = NULL;
1335 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget), &x, &y, keyboard_mode,
1336 &model, &path, &iter))
1337 return FALSE;
1339 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1340 if (p != NULL)
1342 gchar *markup;
1343 gchar *details;
1345 details = g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1346 p->info.version, p->info.author, p->filename);
1347 markup = g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
1348 p->info.name, p->info.description, details);
1350 gtk_tooltip_set_markup(tooltip, markup);
1351 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget), tooltip, path);
1353 g_free(details);
1354 g_free(markup);
1356 gtk_tree_path_free(path);
1358 return p != NULL;
1362 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell,
1363 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1365 Plugin *p;
1367 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1369 if (p == NULL)
1370 g_object_set(cell, "text", _("No plugins available."), NULL);
1371 else
1373 gchar *markup = g_markup_printf_escaped("<b>%s</b>\n%s", p->info.name, p->info.description);
1375 g_object_set(cell, "markup", markup, NULL);
1376 g_free(markup);
1381 static gboolean pm_treeview_button_press_cb(GtkWidget *widget, GdkEventButton *event,
1382 G_GNUC_UNUSED gpointer user_data)
1384 if (event->button == 3)
1386 gtk_menu_popup(GTK_MENU(pm_widgets.popup_menu), NULL, NULL, NULL, NULL,
1387 event->button, event->time);
1389 return FALSE;
1393 static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1394 gpointer user_data)
1396 Plugin *pa, *pb;
1398 gtk_tree_model_get(model, a, PLUGIN_COLUMN_PLUGIN, &pa, -1);
1399 gtk_tree_model_get(model, b, PLUGIN_COLUMN_PLUGIN, &pb, -1);
1401 if (pa && pb)
1402 return strcmp(pa->info.name, pb->info.name);
1403 else
1404 return pa - pb;
1408 static gboolean pm_tree_search(const gchar *key, const gchar *haystack)
1410 gchar *normalized_string = NULL;
1411 gchar *normalized_key = NULL;
1412 gchar *case_normalized_string = NULL;
1413 gchar *case_normalized_key = NULL;
1414 gboolean matched = FALSE;
1416 normalized_string = g_utf8_normalize(haystack, -1, G_NORMALIZE_ALL);
1417 normalized_key = g_utf8_normalize(key, -1, G_NORMALIZE_ALL);
1419 if (normalized_string != NULL && normalized_key != NULL)
1421 case_normalized_string = g_utf8_casefold(normalized_string, -1);
1422 case_normalized_key = g_utf8_casefold(normalized_key, -1);
1423 /* match not only start of plugin name but also any substring in the name */
1424 if (strstr(case_normalized_string, case_normalized_key) != NULL)
1425 matched = TRUE;
1428 g_free(normalized_key);
1429 g_free(normalized_string);
1430 g_free(case_normalized_key);
1431 g_free(case_normalized_string);
1433 return matched;
1437 static gboolean pm_tree_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
1439 Plugin *plugin;
1440 gboolean matched;
1441 const gchar *key;
1443 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &plugin, -1);
1444 key = gtk_entry_get_text(GTK_ENTRY(pm_widgets.filter_entry));
1446 /* first search the plugin name */
1447 matched = pm_tree_search(key, plugin->info.name);
1448 /* if not found, extend search to plugin description */
1449 if (! matched)
1450 matched = pm_tree_search(key, plugin->info.description);
1452 return matched;
1456 static void on_pm_tree_filter_entry_changed_cb(GtkEntry *entry, gpointer user_data)
1458 GtkTreeModel *filter_model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1459 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model));
1463 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos,
1464 GdkEvent *event, gpointer user_data)
1466 if (event->button.button == 1 && icon_pos == GTK_ENTRY_ICON_PRIMARY)
1467 on_pm_tree_filter_entry_changed_cb(entry, user_data);
1471 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1473 GtkCellRenderer *text_renderer, *checkbox_renderer;
1474 GtkTreeViewColumn *column;
1475 GtkTreeModel *filter_model;
1476 GtkTreeIter iter;
1477 GList *list;
1478 GtkTreeSelection *sel;
1480 g_signal_connect(tree, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip), NULL);
1481 gtk_widget_set_has_tooltip(tree, TRUE);
1482 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1484 checkbox_renderer = gtk_cell_renderer_toggle_new();
1485 column = gtk_tree_view_column_new_with_attributes(
1486 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1487 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1488 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1490 text_renderer = gtk_cell_renderer_text_new();
1491 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1492 column = gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer, NULL);
1493 gtk_tree_view_column_set_cell_data_func(column, text_renderer,
1494 pm_treeview_text_cell_data_func, NULL, NULL);
1495 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1497 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1498 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1499 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN,
1500 pm_tree_sort_func, NULL, NULL);
1501 gtk_tree_sortable_set_sort_column_id(
1502 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN, GTK_SORT_ASCENDING);
1504 /* selection handling */
1505 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1506 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1507 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1509 g_signal_connect(tree, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb), NULL);
1511 list = g_list_first(plugin_list);
1512 if (list == NULL)
1514 gtk_list_store_append(store, &iter);
1515 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1516 PLUGIN_COLUMN_PLUGIN, NULL, -1);
1518 else
1520 Plugin *p;
1521 for (; list != NULL; list = list->next)
1523 p = list->data;
1525 gtk_list_store_append(store, &iter);
1526 gtk_list_store_set(store, &iter,
1527 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1528 PLUGIN_COLUMN_PLUGIN, p,
1529 -1);
1532 /* filter */
1533 filter_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(store), NULL);
1534 gtk_tree_model_filter_set_visible_func(
1535 GTK_TREE_MODEL_FILTER(filter_model), pm_tree_filter_func, NULL, NULL);
1537 /* set model to tree view */
1538 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), filter_model);
1539 g_object_unref(store);
1540 g_object_unref(filter_model);
1544 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
1546 GtkTreeModel *model;
1547 GtkTreeSelection *selection;
1548 GtkTreeIter iter;
1549 Plugin *p;
1551 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1552 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1554 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1556 if (p != NULL)
1558 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1559 plugin_show_configure(&p->public);
1560 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1561 p->help();
1562 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_KEYBINDINGS && p->key_group && p->key_group->plugin_key_count > 0)
1563 keybindings_dialog_show_prefs_scroll(p->info.name);
1569 static void
1570 free_non_active_plugin(gpointer data, gpointer user_data)
1572 Plugin *plugin = data;
1574 /* don't do anything when closing the plugin manager and it is an active plugin */
1575 if (is_active_plugin(plugin))
1576 return;
1578 plugin_free(plugin);
1582 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1583 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1584 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1586 switch (response)
1588 case GTK_RESPONSE_CLOSE:
1589 case GTK_RESPONSE_DELETE_EVENT:
1590 if (plugin_list != NULL)
1592 /* remove all non-active plugins from the list */
1593 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1594 g_list_free(plugin_list);
1595 plugin_list = NULL;
1597 gtk_widget_destroy(GTK_WIDGET(dialog));
1599 configuration_save();
1600 break;
1601 case PM_BUTTON_CONFIGURE:
1602 case PM_BUTTON_HELP:
1603 case PM_BUTTON_KEYBINDINGS:
1604 /* forward event to the generic handler */
1605 pm_on_plugin_button_clicked(NULL, GINT_TO_POINTER(response));
1606 break;
1611 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1613 GtkWidget *vbox, *vbox2, *swin, *label, *menu_item, *filter_entry;
1615 /* before showing the dialog, we need to create the list of available plugins */
1616 load_all_plugins();
1618 pm_widgets.dialog = gtk_dialog_new();
1619 gtk_window_set_title(GTK_WINDOW(pm_widgets.dialog), _("Plugins"));
1620 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets.dialog), GTK_WINDOW(main_widgets.window));
1621 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets.dialog), TRUE);
1623 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1624 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1625 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1627 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1629 pm_widgets.help_button = gtk_dialog_add_button(
1630 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_HELP, PM_BUTTON_HELP);
1631 pm_widgets.configure_button = gtk_dialog_add_button(
1632 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_PREFERENCES, PM_BUTTON_CONFIGURE);
1633 pm_widgets.keybindings_button = gtk_dialog_add_button(
1634 GTK_DIALOG(pm_widgets.dialog), _("Keybindings"), PM_BUTTON_KEYBINDINGS);
1635 gtk_dialog_add_button(GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1636 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets.dialog), GTK_RESPONSE_CLOSE);
1638 /* filter */
1639 pm_widgets.filter_entry = filter_entry = gtk_entry_new();
1640 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
1641 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry));
1642 g_signal_connect(filter_entry, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb), NULL);
1643 g_signal_connect(filter_entry, "icon-release",
1644 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb), NULL);
1646 /* prepare treeview */
1647 pm_widgets.tree = gtk_tree_view_new();
1648 pm_widgets.store = gtk_list_store_new(
1649 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_POINTER);
1650 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1652 swin = gtk_scrolled_window_new(NULL, NULL);
1653 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1654 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1655 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1656 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1658 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1660 /* plugin popup menu */
1661 pm_widgets.popup_menu = gtk_menu_new();
1663 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
1664 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1665 g_signal_connect(menu_item, "activate",
1666 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1667 pm_widgets.popup_configure_menu_item = menu_item;
1669 menu_item = gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1670 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1671 g_signal_connect(menu_item, "activate",
1672 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS));
1673 pm_widgets.popup_keybindings_menu_item = menu_item;
1675 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
1676 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1677 g_signal_connect(menu_item, "activate",
1678 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1679 pm_widgets.popup_help_menu_item = menu_item;
1681 /* put it together */
1682 vbox2 = gtk_vbox_new(FALSE, 6);
1683 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
1684 gtk_box_pack_start(GTK_BOX(vbox2), filter_entry, FALSE, FALSE, 0);
1685 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1687 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1689 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
1690 gtk_widget_show_all(pm_widgets.dialog);
1691 gtk_widget_show_all(pm_widgets.popup_menu);
1693 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
1694 pm_update_buttons(NULL);
1695 gtk_widget_grab_focus(pm_widgets.filter_entry);
1699 #endif