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,
25 /* Code to manage, load and unload plugins. */
33 #include "Scintilla.h"
34 #include "ScintillaWidget.h"
38 #include "plugindata.h"
42 #include "filetypes.h"
43 #include "templates.h"
44 #include "sciwrappers.h"
48 #include "msgwindow.h"
50 #include "geanywraplabel.h"
52 #include "encodings.h"
54 #include "highlighting.h"
55 #include "keybindings.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
= {
93 &document_get_current
,
94 &document_get_from_page
,
95 &document_find_by_filename
,
96 &document_find_by_real_path
,
100 &document_remove_page
,
101 &document_reload_file
,
102 &document_set_encoding
,
103 &document_set_text_changed
,
104 &document_set_filetype
,
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
,
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
,
139 &sci_start_undo_action
,
140 &sci_end_undo_action
,
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
,
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
,
161 &sci_get_line_length
,
163 &sci_get_line_is_visible
,
164 &sci_ensure_line_is_visible
,
166 &sci_find_matching_brace
,
169 &sci_get_current_line
,
172 &sci_indicator_clear
,
175 &sci_get_contents_range
,
176 &sci_get_selection_contents
,
178 &sci_get_line_end_position
,
179 &sci_set_target_start
,
182 &sci_set_marker_at_line
,
183 &sci_delete_marker_at_line
,
184 &sci_is_marker_set_at_line
,
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
= {
197 &utils_string_replace_all
,
198 &utils_get_file_list
,
200 &utils_get_locale_from_utf8
,
201 &utils_get_utf8_from_locale
,
202 &utils_remove_ext_from_filename
,
204 &utils_get_setting_boolean
,
205 &utils_get_setting_integer
,
206 &utils_get_setting_string
,
210 &utils_get_date_time
,
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
= {
221 &ui_frame_new_with_alignment
,
225 &ui_button_new_with_image
,
226 &ui_add_document_sensitive
,
227 &ui_widget_set_tooltip_text
,
228 &ui_image_menu_item_new
,
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
250 static SupportFuncs support_funcs
= {
251 &_lookup_widget_macro
254 static MsgWinFuncs msgwin_funcs
= {
256 &msgwin_compiler_add
,
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
= {
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
,
298 static NavQueueFuncs navqueue_funcs
= {
302 static MainFuncs main_funcs
= {
303 &main_reload_configuration
,
308 static StashFuncs stash_funcs
= {
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
,
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
,
330 static SymbolsFuncs symbols_funcs
= {
331 &symbols_get_context_separator
334 static GeanyFunctions geany_functions
= {
359 static GeanyData geany_data
;
363 geany_data_init(void)
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. */
388 plugin_loaded(GModule
*module
)
390 gchar
*basename_module
, *basename_loaded
;
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
);
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
);
422 g_free(basename_loaded
);
424 g_free(basename_module
);
429 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
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
))
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
));
460 gint result
= version_check(GEANY_ABI_VERSION
);
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
));
472 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
473 g_module_name(module
), result
);
481 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
488 cb
= &callbacks
[len
];
489 if (!cb
->signal_name
|| !cb
->callback
)
496 for (i
= 0; i
< len
; 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
;
518 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
519 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
525 GeanyKeyGroup
*key_group
=
526 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
528 *p_key_group
= key_group
;
531 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
532 "count field is zero. Maybe use plugin_set_key_group() instead?",
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?",
544 plugin_init(Plugin
*plugin
)
546 GeanyPlugin
**p_geany_plugin
;
547 PluginCallback
*callbacks
;
549 PluginFields
**plugin_fields
;
550 GeanyData
**p_geany_data
;
551 GeanyFunctions
**p_geany_functions
;
553 /* set these symbols before plugin_init() is called */
554 g_module_symbol(plugin
->module
, "geany_plugin", (void *) &p_geany_plugin
);
556 *p_geany_plugin
= &plugin
->public;
557 g_module_symbol(plugin
->module
, "plugin_info", (void *) &p_info
);
559 *p_info
= &plugin
->info
;
560 g_module_symbol(plugin
->module
, "geany_data", (void *) &p_geany_data
);
562 *p_geany_data
= &geany_data
;
563 g_module_symbol(plugin
->module
, "geany_functions", (void *) &p_geany_functions
);
564 if (p_geany_functions
)
565 *p_geany_functions
= &geany_functions
;
566 g_module_symbol(plugin
->module
, "plugin_fields", (void *) &plugin_fields
);
568 *plugin_fields
= &plugin
->fields
;
569 read_key_group(plugin
);
571 /* start the plugin */
572 g_return_if_fail(plugin
->init
);
573 plugin
->init(&geany_data
);
575 /* store some function pointers for later use */
576 g_module_symbol(plugin
->module
, "plugin_configure", (void *) &plugin
->configure
);
577 g_module_symbol(plugin
->module
, "plugin_configure_single", (void *) &plugin
->configure_single
);
578 if (app
->debug_mode
&& plugin
->configure
&& plugin
->configure_single
)
579 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
580 "only plugin_configure() will be used!",
583 g_module_symbol(plugin
->module
, "plugin_help", (void *) &plugin
->help
);
584 g_module_symbol(plugin
->module
, "plugin_cleanup", (void *) &plugin
->cleanup
);
585 if (plugin
->cleanup
== NULL
)
588 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
592 /* now read any plugin-owned data that might have been set in plugin_init() */
594 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
596 ui_add_document_sensitive(plugin
->fields
.menu_item
);
599 g_module_symbol(plugin
->module
, "plugin_callbacks", (void *) &callbacks
);
601 add_callbacks(plugin
, callbacks
);
603 /* remember which plugins are active */
604 active_plugin_list
= g_list_append(active_plugin_list
, plugin
);
606 geany_debug("Loaded: %s (%s)", plugin
->filename
,
607 NVL(plugin
->info
.name
, "<Unknown>"));
611 /* Load and optionally init a plugin.
612 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
613 * called, the plugin will be started, if not the plugin will be read only (for the list of
614 * available plugins in the plugin manager).
615 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
617 plugin_new(const gchar
*fname
, gboolean init_plugin
, gboolean add_to_list
)
621 void (*plugin_set_info
)(PluginInfo
*);
623 g_return_val_if_fail(fname
, NULL
);
624 g_return_val_if_fail(g_module_supported(), NULL
);
626 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
628 plugin
= find_active_plugin_by_name(fname
);
631 geany_debug("Plugin \"%s\" already loaded.", fname
);
633 plugin_list
= g_list_append(plugin_list
, plugin
);
637 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
638 * causing a segfault. Without that flag the module will safely fail to load.
639 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
640 * otherwise not be detected due to the shadowing of Geany's app variable.
641 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
642 * function from a plugin will be shadowed. */
643 module
= g_module_open(fname
, G_MODULE_BIND_LOCAL
);
646 geany_debug("Can't load plugin: %s", g_module_error());
650 if (plugin_loaded(module
))
652 geany_debug("Plugin \"%s\" already loaded.", fname
);
654 if (! g_module_close(module
))
655 g_warning("%s: %s", fname
, g_module_error());
659 if (! plugin_check_version(module
))
661 if (! g_module_close(module
))
662 g_warning("%s: %s", fname
, g_module_error());
666 g_module_symbol(module
, "plugin_set_info", (void *) &plugin_set_info
);
667 if (plugin_set_info
== NULL
)
669 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname
);
671 if (! g_module_close(module
))
672 g_warning("%s: %s", fname
, g_module_error());
676 plugin
= g_new0(Plugin
, 1);
678 /* read plugin name, etc. */
679 plugin_set_info(&plugin
->info
);
680 if (!NZV(plugin
->info
.name
))
682 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
685 if (! g_module_close(module
))
686 g_warning("%s: %s", fname
, g_module_error());
691 g_module_symbol(module
, "plugin_init", (void *) &plugin
->init
);
692 if (plugin
->init
== NULL
)
694 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
697 if (! g_module_close(module
))
698 g_warning("%s: %s", fname
, g_module_error());
702 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
704 plugin
->filename
= g_strdup(fname
);
705 plugin
->module
= module
;
706 plugin
->public.info
= &plugin
->info
;
707 plugin
->public.priv
= plugin
;
713 plugin_list
= g_list_append(plugin_list
, plugin
);
719 static void remove_callbacks(Plugin
*plugin
)
721 GArray
*signal_ids
= plugin
->signal_ids
;
722 SignalConnection
*sc
;
724 if (signal_ids
== NULL
)
727 foreach_array(SignalConnection
, sc
, signal_ids
)
728 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
730 g_array_free(signal_ids
, TRUE
);
734 static gboolean
is_active_plugin(Plugin
*plugin
)
736 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
740 /* Clean up anything used by an active plugin */
742 plugin_cleanup(Plugin
*plugin
)
749 remove_callbacks(plugin
);
751 if (plugin
->key_group
)
752 keybindings_free_group(plugin
->key_group
);
754 widget
= plugin
->toolbar_separator
.widget
;
756 gtk_widget_destroy(widget
);
758 geany_debug("Unloaded: %s", plugin
->filename
);
763 plugin_free(Plugin
*plugin
)
765 g_return_if_fail(plugin
);
766 g_return_if_fail(plugin
->module
);
768 if (is_active_plugin(plugin
))
769 plugin_cleanup(plugin
);
771 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
773 if (! g_module_close(plugin
->module
))
774 g_warning("%s: %s", plugin
->filename
, g_module_error());
776 plugin_list
= g_list_remove(plugin_list
, plugin
);
778 g_free(plugin
->filename
);
784 /* load active plugins at startup */
786 load_active_plugins(void)
790 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
793 for (i
= 0; i
< len
; i
++)
795 const gchar
*fname
= active_plugins_pref
[i
];
797 if (NZV(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
799 if (plugin_new(fname
, TRUE
, FALSE
) == NULL
)
800 failed_plugins_list
= g_list_append(failed_plugins_list
, g_strdup(fname
));
807 load_plugins_from_path(const gchar
*path
)
812 list
= utils_get_file_list(path
, NULL
, NULL
);
814 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
816 tmp
= strrchr(item
->data
, '.');
817 if (tmp
== NULL
|| utils_str_casecmp(tmp
, "." G_MODULE_SUFFIX
) != 0)
820 fname
= g_strconcat(path
, G_DIR_SEPARATOR_S
, item
->data
, NULL
);
821 plugin_new(fname
, FALSE
, TRUE
);
825 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
831 static gchar
*get_plugin_path()
833 gchar
*install_dir
= win32_get_installation_dir();
836 path
= g_strconcat(install_dir
, "\\lib", NULL
);
844 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
845 static void load_all_plugins(void)
849 path
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "plugins", NULL
);
850 /* first load plugins in ~/.config/geany/plugins/ */
851 load_plugins_from_path(path
);
854 /* load plugins from a custom path */
855 if (NZV(prefs
.custom_plugin_path
))
856 load_plugins_from_path(prefs
.custom_plugin_path
);
858 /* finally load plugins from $prefix/lib/geany */
860 path
= get_plugin_path();
862 path
= g_strconcat(GEANY_LIBDIR
, G_DIR_SEPARATOR_S
"geany", NULL
);
864 load_plugins_from_path(path
);
869 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
871 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
873 gboolean have_plugin_menu_items
= FALSE
;
875 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
877 if (item
->data
== menu_separator
)
879 if (i
< g_list_length(list
) - 1)
881 have_plugin_menu_items
= TRUE
;
889 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
893 /* Calling this starts up plugin support */
894 void plugins_load_active(void)
902 widget
= gtk_separator_menu_item_new();
903 gtk_widget_show(widget
);
904 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
906 widget
= gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
907 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
908 gtk_widget_show(widget
);
909 g_signal_connect(widget
, "activate", G_CALLBACK(pm_show_dialog
), NULL
);
911 menu_separator
= gtk_separator_menu_item_new();
912 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
913 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
915 load_active_plugins();
919 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
921 const Plugin
*pa
= a
;
922 const Plugin
*pb
= b
;
924 return strcmp(pa
->info
.name
, pb
->info
.name
);
928 static void update_active_plugins_pref(void)
932 gsize count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
934 g_strfreev(active_plugins_pref
);
938 active_plugins_pref
= NULL
;
942 /* sort the list so next time tools menu items are sorted by plugin name
943 * (not ideal to do here, but better than nothing) */
944 active_plugin_list
= g_list_sort(active_plugin_list
, cmp_plugin_names
);
946 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
948 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
950 Plugin
*plugin
= list
->data
;
952 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
955 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
957 const gchar
*fname
= list
->data
;
959 active_plugins_pref
[i
] = g_strdup(fname
);
962 active_plugins_pref
[i
] = NULL
;
966 static void on_save_settings(GKeyFile
*config
)
968 /* if plugins are disabled, don't clear list of active plugins */
970 update_active_plugins_pref();
974 /* called even if plugin support is disabled */
975 void plugins_init(void)
979 group
= stash_group_new("plugins");
980 configuration_add_pref_group(group
, TRUE
);
982 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
983 "load_plugins", TRUE
, "check_plugins");
984 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
985 "custom_plugin_path", "", "extra_plugin_path_entry");
987 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(on_save_settings
), NULL
);
988 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
992 /* called even if plugin support is disabled */
993 void plugins_finalize(void)
995 if (failed_plugins_list
!= NULL
)
997 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
998 g_list_free(failed_plugins_list
);
1000 if (active_plugin_list
!= NULL
)
1002 g_list_foreach(active_plugin_list
, (GFunc
) plugin_free
, NULL
);
1003 g_list_free(active_plugin_list
);
1005 g_strfreev(active_plugins_pref
);
1009 /* Check whether there are any plugins loaded which provide a configure symbol */
1010 gboolean
plugins_have_preferences(void)
1014 if (active_plugin_list
== NULL
)
1017 foreach_list(item
, active_plugin_list
)
1019 Plugin
*plugin
= item
->data
;
1020 if (plugin
->configure
!= NULL
|| plugin
->configure_single
!= NULL
)
1028 /* Plugin Manager */
1032 PLUGIN_COLUMN_CHECK
= 0,
1035 PLUGIN_COLUMN_PLUGIN
,
1037 PM_BUTTON_CONFIGURE
,
1045 GtkListStore
*store
;
1046 GtkWidget
*description_label
;
1047 GtkWidget
*configure_button
;
1048 GtkWidget
*help_button
;
1050 PluginManagerWidgets
;
1052 static PluginManagerWidgets pm_widgets
;
1055 static void pm_update_buttons(Plugin
*p
)
1059 is_active
= is_active_plugin(p
);
1060 gtk_widget_set_sensitive(pm_widgets
.configure_button
,
1061 (p
->configure
|| p
->configure_single
) && is_active
);
1062 gtk_widget_set_sensitive(pm_widgets
.help_button
, p
->help
!= NULL
&& is_active
);
1066 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
1069 GtkTreeModel
*model
;
1072 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1074 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1082 text
= g_strdup_printf(
1083 _("Plugin: %s %s\nDescription: %s\nAuthor(s): %s"),
1084 pi
->name
, pi
->version
, pi
->description
, pi
->author
);
1086 geany_wrap_label_set_text(GTK_LABEL(pm_widgets
.description_label
), text
);
1089 pm_update_buttons(p
);
1095 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
1097 gboolean old_state
, state
;
1100 GtkTreePath
*path
= gtk_tree_path_new_from_string(pth
);
1103 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets
.store
), &iter
, path
);
1104 gtk_tree_path_free(path
);
1106 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets
.store
), &iter
,
1107 PLUGIN_COLUMN_CHECK
, &old_state
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1109 /* no plugins item */
1113 state
= ! old_state
; /* toggle the state */
1115 /* save the filename of the plugin */
1116 file_name
= g_strdup(p
->filename
);
1118 /* unload plugin module */
1120 /* save shortcuts (only need this group, but it doesn't take long) */
1121 keybindings_write_to_file();
1125 /* reload plugin module and initialize it if item is checked */
1126 p
= plugin_new(file_name
, state
, TRUE
);
1129 /* plugin file may no longer be on disk, or is now incompatible */
1130 gtk_list_store_remove(pm_widgets
.store
, &iter
);
1135 keybindings_load_keyfile(); /* load shortcuts */
1138 gtk_list_store_set(pm_widgets
.store
, &iter
,
1139 PLUGIN_COLUMN_CHECK
, state
,
1140 PLUGIN_COLUMN_PLUGIN
, p
, -1);
1142 /* set again the sensitiveness of the configure and help buttons */
1143 pm_update_buttons(p
);
1149 static void pm_prepare_treeview(GtkWidget
*tree
, GtkListStore
*store
)
1151 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1152 GtkTreeViewColumn
*column
;
1155 GtkTreeSelection
*sel
;
1157 checkbox_renderer
= gtk_cell_renderer_toggle_new();
1158 column
= gtk_tree_view_column_new_with_attributes(
1159 _("Active"), checkbox_renderer
, "active", PLUGIN_COLUMN_CHECK
, NULL
);
1160 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1161 g_signal_connect(checkbox_renderer
, "toggled", G_CALLBACK(pm_plugin_toggled
), NULL
);
1163 text_renderer
= gtk_cell_renderer_text_new();
1164 column
= gtk_tree_view_column_new_with_attributes(
1165 _("Plugin"), text_renderer
, "text", PLUGIN_COLUMN_NAME
, NULL
);
1166 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1168 text_renderer
= gtk_cell_renderer_text_new();
1169 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1170 column
= gtk_tree_view_column_new_with_attributes(
1171 _("File"), text_renderer
, "text", PLUGIN_COLUMN_FILE
, NULL
);
1172 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1174 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
1175 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree
), FALSE
);
1176 gtk_tree_sortable_set_sort_column_id(
1177 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_NAME
, GTK_SORT_ASCENDING
);
1179 /* selection handling */
1180 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
1181 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_SINGLE
);
1182 g_signal_connect(sel
, "changed", G_CALLBACK(pm_selection_changed
), NULL
);
1184 list
= g_list_first(plugin_list
);
1187 gtk_list_store_append(store
, &iter
);
1188 gtk_list_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1189 PLUGIN_COLUMN_NAME
, _("No plugins available."),
1190 PLUGIN_COLUMN_FILE
, "", PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1195 for (; list
!= NULL
; list
= list
->next
)
1199 gtk_list_store_append(store
, &iter
);
1200 gtk_list_store_set(store
, &iter
,
1201 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1202 PLUGIN_COLUMN_NAME
, p
->info
.name
,
1203 PLUGIN_COLUMN_FILE
, p
->filename
,
1204 PLUGIN_COLUMN_PLUGIN
, p
,
1208 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), GTK_TREE_MODEL(store
));
1209 g_object_unref(store
);
1213 static void pm_on_plugin_button_clicked(GtkButton
*button
, gpointer user_data
)
1215 GtkTreeModel
*model
;
1216 GtkTreeSelection
*selection
;
1220 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1221 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1223 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1227 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1228 plugin_show_configure(&p
->public);
1229 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
&& p
->help
!= NULL
)
1237 free_non_active_plugin(gpointer data
, gpointer user_data
)
1239 Plugin
*plugin
= data
;
1241 /* don't do anything when closing the plugin manager and it is an active plugin */
1242 if (is_active_plugin(plugin
))
1245 plugin_free(plugin
);
1249 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1251 if (plugin_list
!= NULL
)
1253 /* remove all non-active plugins from the list */
1254 g_list_foreach(plugin_list
, free_non_active_plugin
, NULL
);
1255 g_list_free(plugin_list
);
1258 gtk_widget_destroy(GTK_WIDGET(dialog
));
1262 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1264 GtkWidget
*vbox
, *vbox2
, *label_vbox
, *hbox
, *swin
, *label
, *label2
, *desc_win
;
1266 /* before showing the dialog, we need to create the list of available plugins */
1269 pm_widgets
.dialog
= gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets
.window
),
1270 GTK_DIALOG_DESTROY_WITH_PARENT
,
1271 GTK_STOCK_OK
, GTK_RESPONSE_CANCEL
, NULL
);
1272 vbox
= ui_dialog_vbox_new(GTK_DIALOG(pm_widgets
.dialog
));
1273 gtk_widget_set_name(pm_widgets
.dialog
, "GeanyDialog");
1274 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
1276 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
1278 pm_widgets
.tree
= gtk_tree_view_new();
1279 pm_widgets
.store
= gtk_list_store_new(
1280 PLUGIN_N_COLUMNS
, G_TYPE_BOOLEAN
, G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
);
1281 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1283 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1284 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
1285 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1286 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1287 gtk_container_add(GTK_CONTAINER(swin
), pm_widgets
.tree
);
1289 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1291 pm_widgets
.configure_button
= gtk_button_new_from_stock(GTK_STOCK_PREFERENCES
);
1292 gtk_widget_set_sensitive(pm_widgets
.configure_button
, FALSE
);
1293 g_signal_connect(pm_widgets
.configure_button
, "clicked",
1294 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_CONFIGURE
));
1296 pm_widgets
.help_button
= gtk_button_new_from_stock(GTK_STOCK_HELP
);
1297 gtk_widget_set_sensitive(pm_widgets
.help_button
, FALSE
);
1298 g_signal_connect(pm_widgets
.help_button
, "clicked",
1299 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_HELP
));
1301 label2
= gtk_label_new(_("<b>Plugin details:</b>"));
1302 gtk_label_set_use_markup(GTK_LABEL(label2
), TRUE
);
1303 gtk_misc_set_alignment(GTK_MISC(label2
), 0, 0.5);
1305 pm_widgets
.description_label
= geany_wrap_label_new("");
1306 desc_win
= gtk_scrolled_window_new(NULL
, NULL
);
1307 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win
),
1308 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1309 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win
),
1310 pm_widgets
.description_label
);
1312 hbox
= gtk_hbox_new(FALSE
, 0);
1313 gtk_box_pack_start(GTK_BOX(hbox
), label2
, TRUE
, TRUE
, 0);
1314 gtk_box_pack_start(GTK_BOX(hbox
), pm_widgets
.help_button
, FALSE
, FALSE
, 4);
1315 gtk_box_pack_start(GTK_BOX(hbox
), pm_widgets
.configure_button
, FALSE
, FALSE
, 0);
1317 label_vbox
= gtk_vbox_new(FALSE
, 3);
1318 gtk_box_pack_start(GTK_BOX(label_vbox
), hbox
, FALSE
, FALSE
, 0);
1319 gtk_box_pack_start(GTK_BOX(label_vbox
), desc_win
, FALSE
, FALSE
, 0);
1321 vbox2
= gtk_vbox_new(FALSE
, 3);
1322 gtk_box_pack_start(GTK_BOX(vbox2
), label
, FALSE
, FALSE
, 5);
1323 gtk_box_pack_start(GTK_BOX(vbox2
), swin
, TRUE
, TRUE
, 0);
1324 gtk_box_pack_start(GTK_BOX(vbox2
), label_vbox
, FALSE
, FALSE
, 0);
1326 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
1328 gtk_container_add(GTK_CONTAINER(vbox
), vbox2
);
1329 gtk_widget_show_all(pm_widgets
.dialog
);