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. */
34 #include "encodings.h"
35 #include "geanyobject.h"
36 #include "geanywraplabel.h"
37 #include "highlighting.h"
38 #include "keybindingsprivate.h"
41 #include "msgwindow.h"
43 #include "plugindata.h"
44 #include "pluginprivate.h"
45 #include "pluginutils.h"
47 #include "sciwrappers.h"
51 #include "templates.h"
57 #include "gtkcompat.h"
62 GList
*active_plugin_list
= NULL
; /* list of only actually loaded plugins, always valid */
65 static gboolean want_plugins
= FALSE
;
67 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
68 * opened, afterwards it will be destroyed */
69 static GList
*plugin_list
= NULL
;
70 static gchar
**active_plugins_pref
= NULL
; /* list of plugin filenames to load at startup */
71 static GList
*failed_plugins_list
= NULL
; /* plugins the user wants active but can't be used */
73 static GtkWidget
*menu_separator
= NULL
;
75 static gchar
*get_plugin_path(void);
76 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
);
78 static GeanyData geany_data
;
105 /* Prevent the same plugin filename being loaded more than once.
106 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
108 plugin_loaded(GModule
*module
)
110 gchar
*basename_module
, *basename_loaded
;
113 basename_module
= g_path_get_basename(g_module_name(module
));
114 for (item
= plugin_list
; item
!= NULL
; item
= g_list_next(item
))
116 basename_loaded
= g_path_get_basename(
117 g_module_name(((Plugin
*)item
->data
)->module
));
119 if (utils_str_equal(basename_module
, basename_loaded
))
121 g_free(basename_loaded
);
122 g_free(basename_module
);
125 g_free(basename_loaded
);
127 /* Look also through the list of active plugins. This prevents problems when we have the same
128 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
129 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
130 * the plugin manager would list both plugins. Additionally, unloading the active plugin
131 * would cause a crash. */
132 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
134 basename_loaded
= g_path_get_basename(g_module_name(((Plugin
*)item
->data
)->module
));
136 if (utils_str_equal(basename_module
, basename_loaded
))
138 g_free(basename_loaded
);
139 g_free(basename_module
);
142 g_free(basename_loaded
);
144 g_free(basename_module
);
149 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
153 g_return_val_if_fail(filename
, FALSE
);
155 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
157 if (utils_str_equal(filename
, ((Plugin
*)item
->data
)->filename
))
166 plugin_check_version(GModule
*module
)
168 gint (*version_check
)(gint
) = NULL
;
170 g_module_symbol(module
, "plugin_version_check", (void *) &version_check
);
172 if (G_UNLIKELY(! version_check
))
174 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
175 g_module_name(module
));
180 gint result
= version_check(GEANY_ABI_VERSION
);
184 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
185 "release of Geany - please recompile it."), g_module_name(module
));
186 geany_debug("Plugin \"%s\" is not binary compatible with this "
187 "release of Geany - recompile it.", g_module_name(module
));
190 if (result
> GEANY_API_VERSION
)
192 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
193 g_module_name(module
), result
);
201 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
208 cb
= &callbacks
[len
];
209 if (!cb
->signal_name
|| !cb
->callback
)
216 for (i
= 0; i
< len
; i
++)
220 plugin_signal_connect(&plugin
->public, NULL
, cb
->signal_name
, cb
->after
,
221 cb
->callback
, cb
->user_data
);
226 static void read_key_group(Plugin
*plugin
)
228 GeanyKeyGroupInfo
*p_key_info
;
229 GeanyKeyGroup
**p_key_group
;
231 g_module_symbol(plugin
->module
, "plugin_key_group_info", (void *) &p_key_info
);
232 g_module_symbol(plugin
->module
, "plugin_key_group", (void *) &p_key_group
);
233 if (p_key_info
&& p_key_group
)
235 GeanyKeyGroupInfo
*key_info
= p_key_info
;
238 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
239 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
245 GeanyKeyGroup
*key_group
=
246 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
248 *p_key_group
= key_group
;
251 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
252 "count field is zero. Maybe use plugin_set_key_group() instead?",
256 else if (p_key_info
|| p_key_group
)
257 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
258 "Maybe use plugin_set_key_group() instead?",
263 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
265 const Plugin
*pa
= a
;
266 const Plugin
*pb
= b
;
268 return strcmp(pa
->info
.name
, pb
->info
.name
);
273 plugin_load(Plugin
*plugin
)
275 GeanyPlugin
**p_geany_plugin
;
276 PluginCallback
*callbacks
;
278 PluginFields
**plugin_fields
;
280 /* set these symbols before plugin_init() is called
281 * we don't set geany_data since it is set directly by plugin_new() */
282 g_module_symbol(plugin
->module
, "geany_plugin", (void *) &p_geany_plugin
);
284 *p_geany_plugin
= &plugin
->public;
285 g_module_symbol(plugin
->module
, "plugin_info", (void *) &p_info
);
287 *p_info
= &plugin
->info
;
288 g_module_symbol(plugin
->module
, "plugin_fields", (void *) &plugin_fields
);
290 *plugin_fields
= &plugin
->fields
;
291 read_key_group(plugin
);
293 /* start the plugin */
294 g_return_if_fail(plugin
->init
);
295 plugin
->init(&geany_data
);
297 /* store some function pointers for later use */
298 g_module_symbol(plugin
->module
, "plugin_configure", (void *) &plugin
->configure
);
299 g_module_symbol(plugin
->module
, "plugin_configure_single", (void *) &plugin
->configure_single
);
300 if (app
->debug_mode
&& plugin
->configure
&& plugin
->configure_single
)
301 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
302 "only plugin_configure() will be used!",
305 g_module_symbol(plugin
->module
, "plugin_help", (void *) &plugin
->help
);
306 g_module_symbol(plugin
->module
, "plugin_cleanup", (void *) &plugin
->cleanup
);
307 if (plugin
->cleanup
== NULL
)
310 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
314 /* now read any plugin-owned data that might have been set in plugin_init() */
316 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
318 ui_add_document_sensitive(plugin
->fields
.menu_item
);
321 g_module_symbol(plugin
->module
, "plugin_callbacks", (void *) &callbacks
);
323 add_callbacks(plugin
, callbacks
);
325 /* remember which plugins are active.
326 * keep list sorted so tools menu items and plugin preference tabs are
327 * sorted by plugin name */
328 active_plugin_list
= g_list_insert_sorted(active_plugin_list
, plugin
, cmp_plugin_names
);
330 geany_debug("Loaded: %s (%s)", plugin
->filename
,
331 FALLBACK(plugin
->info
.name
, "<Unknown>"));
335 /* Load and optionally init a plugin.
336 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
337 * called, the plugin will be started, if not the plugin will be read only (for the list of
338 * available plugins in the plugin manager).
339 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
341 plugin_new(const gchar
*fname
, gboolean load_plugin
, gboolean add_to_list
)
345 GeanyData
**p_geany_data
;
346 void (*plugin_set_info
)(PluginInfo
*);
348 g_return_val_if_fail(fname
, NULL
);
349 g_return_val_if_fail(g_module_supported(), NULL
);
351 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
353 plugin
= find_active_plugin_by_name(fname
);
356 geany_debug("Plugin \"%s\" already loaded.", fname
);
359 /* do not add to the list twice */
360 if (g_list_find(plugin_list
, plugin
) != NULL
)
363 plugin_list
= g_list_prepend(plugin_list
, plugin
);
368 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
369 * causing a segfault. Without that flag the module will safely fail to load.
370 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
371 * otherwise not be detected due to the shadowing of Geany's app variable.
372 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
373 * function from a plugin will be shadowed. */
374 module
= g_module_open(fname
, G_MODULE_BIND_LOCAL
);
377 geany_debug("Can't load plugin: %s", g_module_error());
381 if (plugin_loaded(module
))
383 geany_debug("Plugin \"%s\" already loaded.", fname
);
385 if (! g_module_close(module
))
386 g_warning("%s: %s", fname
, g_module_error());
390 if (! plugin_check_version(module
))
392 if (! g_module_close(module
))
393 g_warning("%s: %s", fname
, g_module_error());
397 g_module_symbol(module
, "plugin_set_info", (void *) &plugin_set_info
);
398 if (plugin_set_info
== NULL
)
400 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname
);
402 if (! g_module_close(module
))
403 g_warning("%s: %s", fname
, g_module_error());
407 plugin
= g_new0(Plugin
, 1);
409 /* set basic fields here to allow plugins to call Geany functions in set_info() */
410 g_module_symbol(module
, "geany_data", (void *) &p_geany_data
);
412 *p_geany_data
= &geany_data
;
414 /* read plugin name, etc. */
415 plugin_set_info(&plugin
->info
);
416 if (G_UNLIKELY(EMPTY(plugin
->info
.name
)))
418 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
421 if (! g_module_close(module
))
422 g_warning("%s: %s", fname
, g_module_error());
427 g_module_symbol(module
, "plugin_init", (void *) &plugin
->init
);
428 if (plugin
->init
== NULL
)
430 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
433 if (! g_module_close(module
))
434 g_warning("%s: %s", fname
, g_module_error());
438 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
440 plugin
->filename
= g_strdup(fname
);
441 plugin
->module
= module
;
442 plugin
->public.info
= &plugin
->info
;
443 plugin
->public.priv
= plugin
;
449 plugin_list
= g_list_prepend(plugin_list
, plugin
);
455 static void on_object_weak_notify(gpointer data
, GObject
*old_ptr
)
457 Plugin
*plugin
= data
;
460 g_return_if_fail(plugin
&& plugin
->signal_ids
);
462 for (i
= 0; i
< plugin
->signal_ids
->len
; i
++)
464 SignalConnection
*sc
= &g_array_index(plugin
->signal_ids
, SignalConnection
, i
);
466 if (sc
->object
== old_ptr
)
468 g_array_remove_index_fast(plugin
->signal_ids
, i
);
469 /* we can break the loop right after finding the first match,
470 * because we will get one notification per connected signal */
477 /* add an object to watch for destruction, and release pointers to it when destroyed.
478 * this should only be used by plugin_signal_connect() to add a watch on
479 * the object lifetime and nuke out references to it in plugin->signal_ids */
480 void plugin_watch_object(Plugin
*plugin
, gpointer object
)
482 g_object_weak_ref(object
, on_object_weak_notify
, plugin
);
486 static void remove_callbacks(Plugin
*plugin
)
488 GArray
*signal_ids
= plugin
->signal_ids
;
489 SignalConnection
*sc
;
491 if (signal_ids
== NULL
)
494 foreach_array(SignalConnection
, sc
, signal_ids
)
496 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
497 g_object_weak_unref(sc
->object
, on_object_weak_notify
, plugin
);
500 g_array_free(signal_ids
, TRUE
);
504 static void remove_sources(Plugin
*plugin
)
508 item
= plugin
->sources
;
511 GList
*next
= item
->next
; /* cache the next pointer because current item will be freed */
513 g_source_destroy(item
->data
);
516 /* don't free the list here, it is allocated inside each source's data */
520 static gboolean
is_active_plugin(Plugin
*plugin
)
522 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
526 /* Clean up anything used by an active plugin */
528 plugin_cleanup(Plugin
*plugin
)
535 remove_callbacks(plugin
);
536 remove_sources(plugin
);
538 if (plugin
->key_group
)
539 keybindings_free_group(plugin
->key_group
);
541 widget
= plugin
->toolbar_separator
.widget
;
543 gtk_widget_destroy(widget
);
545 geany_debug("Unloaded: %s", plugin
->filename
);
550 plugin_free(Plugin
*plugin
)
552 g_return_if_fail(plugin
);
553 g_return_if_fail(plugin
->module
);
555 if (is_active_plugin(plugin
))
556 plugin_cleanup(plugin
);
558 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
560 if (! g_module_close(plugin
->module
))
561 g_warning("%s: %s", plugin
->filename
, g_module_error());
563 plugin_list
= g_list_remove(plugin_list
, plugin
);
565 g_free(plugin
->filename
);
571 static gchar
*get_custom_plugin_path(const gchar
*plugin_path_config
,
572 const gchar
*plugin_path_system
)
574 gchar
*plugin_path_custom
;
576 if (EMPTY(prefs
.custom_plugin_path
))
579 plugin_path_custom
= utils_get_locale_from_utf8(prefs
.custom_plugin_path
);
580 utils_tidy_path(plugin_path_custom
);
582 /* check whether the custom plugin path is one of the system or user plugin paths
584 if (utils_str_equal(plugin_path_custom
, plugin_path_config
) ||
585 utils_str_equal(plugin_path_custom
, plugin_path_system
))
587 g_free(plugin_path_custom
);
590 return plugin_path_custom
;
594 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
595 * so we need to check active plugins are in the right place before loading */
596 static gboolean
check_plugin_path(const gchar
*fname
)
598 gchar
*plugin_path_config
;
599 gchar
*plugin_path_system
;
600 gchar
*plugin_path_custom
;
601 gboolean ret
= FALSE
;
603 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
604 if (g_str_has_prefix(fname
, plugin_path_config
))
607 plugin_path_system
= get_plugin_path();
608 if (g_str_has_prefix(fname
, plugin_path_system
))
611 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
612 if (plugin_path_custom
)
614 if (g_str_has_prefix(fname
, plugin_path_custom
))
617 g_free(plugin_path_custom
);
619 g_free(plugin_path_config
);
620 g_free(plugin_path_system
);
625 /* load active plugins at startup */
627 load_active_plugins(void)
631 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
634 for (i
= 0; i
< len
; i
++)
636 const gchar
*fname
= active_plugins_pref
[i
];
638 if (!EMPTY(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
640 if (!check_plugin_path(fname
) || plugin_new(fname
, TRUE
, FALSE
) == NULL
)
641 failed_plugins_list
= g_list_prepend(failed_plugins_list
, g_strdup(fname
));
648 load_plugins_from_path(const gchar
*path
)
654 list
= utils_get_file_list(path
, NULL
, NULL
);
656 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
658 tmp
= strrchr(item
->data
, '.');
659 if (tmp
== NULL
|| utils_str_casecmp(tmp
, "." G_MODULE_SUFFIX
) != 0)
662 fname
= g_build_filename(path
, item
->data
, NULL
);
663 if (plugin_new(fname
, FALSE
, TRUE
))
668 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
672 geany_debug("Added %d plugin(s) in '%s'.", count
, path
);
676 static gchar
*get_plugin_path(void)
678 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN
));
682 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
683 static void load_all_plugins(void)
685 gchar
*plugin_path_config
;
686 gchar
*plugin_path_system
;
687 gchar
*plugin_path_custom
;
689 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
690 plugin_path_system
= get_plugin_path();
692 /* first load plugins in ~/.config/geany/plugins/ */
693 load_plugins_from_path(plugin_path_config
);
695 /* load plugins from a custom path */
696 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
697 if (plugin_path_custom
)
699 load_plugins_from_path(plugin_path_custom
);
700 g_free(plugin_path_custom
);
703 /* finally load plugins from $prefix/lib/geany */
704 load_plugins_from_path(plugin_path_system
);
706 g_free(plugin_path_config
);
707 g_free(plugin_path_system
);
711 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
713 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
715 gboolean have_plugin_menu_items
= FALSE
;
717 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
719 if (item
->data
== menu_separator
)
721 if (i
< g_list_length(list
) - 1)
723 have_plugin_menu_items
= TRUE
;
731 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
735 /* Calling this starts up plugin support */
736 void plugins_load_active(void)
744 widget
= gtk_separator_menu_item_new();
745 gtk_widget_show(widget
);
746 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
748 widget
= gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
749 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
750 gtk_widget_show(widget
);
751 g_signal_connect(widget
, "activate", G_CALLBACK(pm_show_dialog
), NULL
);
753 menu_separator
= gtk_separator_menu_item_new();
754 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
755 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
757 load_active_plugins();
761 /* Update the global active plugins list so it's up-to-date when configuration
762 * is saved. Called in response to GeanyObject's "save-settings" signal. */
763 static void update_active_plugins_pref(void)
769 /* if plugins are disabled, don't clear list of active plugins */
773 count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
775 g_strfreev(active_plugins_pref
);
779 active_plugins_pref
= NULL
;
783 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
785 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
787 Plugin
*plugin
= list
->data
;
789 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
792 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
794 const gchar
*fname
= list
->data
;
796 active_plugins_pref
[i
] = g_strdup(fname
);
799 active_plugins_pref
[i
] = NULL
;
803 /* called even if plugin support is disabled */
804 void plugins_init(void)
809 path
= get_plugin_path();
810 geany_debug("System plugin path: %s", path
);
813 group
= stash_group_new("plugins");
814 configuration_add_pref_group(group
, TRUE
);
816 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
817 "load_plugins", TRUE
, "check_plugins");
818 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
819 "custom_plugin_path", "", "extra_plugin_path_entry");
821 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(update_active_plugins_pref
), NULL
);
822 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
826 /* called even if plugin support is disabled */
827 void plugins_finalize(void)
829 if (failed_plugins_list
!= NULL
)
831 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
832 g_list_free(failed_plugins_list
);
834 if (active_plugin_list
!= NULL
)
836 g_list_foreach(active_plugin_list
, (GFunc
) plugin_free
, NULL
);
837 g_list_free(active_plugin_list
);
839 g_strfreev(active_plugins_pref
);
843 /* Check whether there are any plugins loaded which provide a configure symbol */
844 gboolean
plugins_have_preferences(void)
848 if (active_plugin_list
== NULL
)
851 foreach_list(item
, active_plugin_list
)
853 Plugin
*plugin
= item
->data
;
854 if (plugin
->configure
!= NULL
|| plugin
->configure_single
!= NULL
)
866 PLUGIN_COLUMN_CHECK
= 0,
867 PLUGIN_COLUMN_PLUGIN
,
869 PM_BUTTON_KEYBINDINGS
,
879 GtkWidget
*filter_entry
;
880 GtkWidget
*configure_button
;
881 GtkWidget
*keybindings_button
;
882 GtkWidget
*help_button
;
883 GtkWidget
*popup_menu
;
884 GtkWidget
*popup_configure_menu_item
;
885 GtkWidget
*popup_keybindings_menu_item
;
886 GtkWidget
*popup_help_menu_item
;
888 PluginManagerWidgets
;
890 static PluginManagerWidgets pm_widgets
;
893 static void pm_update_buttons(Plugin
*p
)
895 gboolean is_active
= FALSE
;
896 gboolean has_configure
= FALSE
;
897 gboolean has_help
= FALSE
;
898 gboolean has_keybindings
= FALSE
;
902 is_active
= is_active_plugin(p
);
903 has_configure
= (p
->configure
|| p
->configure_single
) && is_active
;
904 has_help
= p
->help
!= NULL
&& is_active
;
905 has_keybindings
= p
->key_group
&& p
->key_group
->plugin_key_count
> 0 && is_active
;
908 gtk_widget_set_sensitive(pm_widgets
.configure_button
, has_configure
);
909 gtk_widget_set_sensitive(pm_widgets
.help_button
, has_help
);
910 gtk_widget_set_sensitive(pm_widgets
.keybindings_button
, has_keybindings
);
912 gtk_widget_set_sensitive(pm_widgets
.popup_configure_menu_item
, has_configure
);
913 gtk_widget_set_sensitive(pm_widgets
.popup_help_menu_item
, has_help
);
914 gtk_widget_set_sensitive(pm_widgets
.popup_keybindings_menu_item
, has_keybindings
);
918 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
924 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
926 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
929 pm_update_buttons(p
);
934 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
936 gboolean old_state
, state
;
939 GtkTreeIter store_iter
;
940 GtkTreePath
*path
= gtk_tree_path_new_from_string(pth
);
941 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
944 gtk_tree_model_get_iter(model
, &iter
, path
);
945 gtk_tree_path_free(path
);
947 gtk_tree_model_get(model
, &iter
,
948 PLUGIN_COLUMN_CHECK
, &old_state
,
949 PLUGIN_COLUMN_PLUGIN
, &p
, -1);
951 /* no plugins item */
955 gtk_tree_model_filter_convert_iter_to_child_iter(
956 GTK_TREE_MODEL_FILTER(model
), &store_iter
, &iter
);
958 state
= ! old_state
; /* toggle the state */
960 /* save the filename of the plugin */
961 file_name
= g_strdup(p
->filename
);
963 /* unload plugin module */
965 /* save shortcuts (only need this group, but it doesn't take long) */
966 keybindings_write_to_file();
970 /* reload plugin module and initialize it if item is checked */
971 p
= plugin_new(file_name
, state
, TRUE
);
974 /* plugin file may no longer be on disk, or is now incompatible */
975 gtk_list_store_remove(pm_widgets
.store
, &store_iter
);
980 keybindings_load_keyfile(); /* load shortcuts */
983 gtk_list_store_set(pm_widgets
.store
, &store_iter
,
984 PLUGIN_COLUMN_CHECK
, state
,
985 PLUGIN_COLUMN_PLUGIN
, p
, -1);
987 /* set again the sensitiveness of the configure and help buttons */
988 pm_update_buttons(p
);
994 static gboolean
pm_treeview_query_tooltip(GtkWidget
*widget
, gint x
, gint y
,
995 gboolean keyboard_mode
, GtkTooltip
*tooltip
, gpointer user_data
)
1002 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget
), &x
, &y
, keyboard_mode
,
1003 &model
, &path
, &iter
))
1006 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1012 details
= g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1013 p
->info
.version
, p
->info
.author
, p
->filename
);
1014 markup
= g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
1015 p
->info
.name
, p
->info
.description
, details
);
1017 gtk_tooltip_set_markup(tooltip
, markup
);
1018 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget
), tooltip
, path
);
1023 gtk_tree_path_free(path
);
1029 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn
*column
, GtkCellRenderer
*cell
,
1030 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer data
)
1034 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1037 g_object_set(cell
, "text", _("No plugins available."), NULL
);
1040 gchar
*markup
= g_markup_printf_escaped("<b>%s</b>\n%s", p
->info
.name
, p
->info
.description
);
1042 g_object_set(cell
, "markup", markup
, NULL
);
1048 static gboolean
pm_treeview_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
1049 G_GNUC_UNUSED gpointer user_data
)
1051 if (event
->button
== 3)
1053 gtk_menu_popup(GTK_MENU(pm_widgets
.popup_menu
), NULL
, NULL
, NULL
, NULL
,
1054 event
->button
, event
->time
);
1060 static gint
pm_tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1065 gtk_tree_model_get(model
, a
, PLUGIN_COLUMN_PLUGIN
, &pa
, -1);
1066 gtk_tree_model_get(model
, b
, PLUGIN_COLUMN_PLUGIN
, &pb
, -1);
1069 return strcmp(pa
->info
.name
, pb
->info
.name
);
1075 static gboolean
pm_tree_search(const gchar
*key
, const gchar
*haystack
)
1077 gchar
*normalized_string
= NULL
;
1078 gchar
*normalized_key
= NULL
;
1079 gchar
*case_normalized_string
= NULL
;
1080 gchar
*case_normalized_key
= NULL
;
1081 gboolean matched
= TRUE
;
1083 normalized_string
= g_utf8_normalize(haystack
, -1, G_NORMALIZE_ALL
);
1084 normalized_key
= g_utf8_normalize(key
, -1, G_NORMALIZE_ALL
);
1086 if (normalized_string
!= NULL
&& normalized_key
!= NULL
)
1088 GString
*stripped_key
;
1089 gchar
**subkey
, **subkeys
;
1091 case_normalized_string
= g_utf8_casefold(normalized_string
, -1);
1092 case_normalized_key
= g_utf8_casefold(normalized_key
, -1);
1093 stripped_key
= g_string_new(case_normalized_key
);
1094 do {} while (utils_string_replace_all(stripped_key
, " ", " "));
1095 subkeys
= g_strsplit(stripped_key
->str
, " ", -1);
1096 g_string_free(stripped_key
, TRUE
);
1097 foreach_strv(subkey
, subkeys
)
1099 if (strstr(case_normalized_string
, *subkey
) == NULL
)
1105 g_strfreev(subkeys
);
1108 g_free(normalized_key
);
1109 g_free(normalized_string
);
1110 g_free(case_normalized_key
);
1111 g_free(case_normalized_string
);
1117 static gboolean
pm_tree_filter_func(GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1122 gchar
*haystack
, *filename
;
1124 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &plugin
, -1);
1125 key
= gtk_entry_get_text(GTK_ENTRY(pm_widgets
.filter_entry
));
1127 filename
= g_path_get_basename(plugin
->filename
);
1128 haystack
= g_strjoin(" ", plugin
->info
.name
, plugin
->info
.description
,
1129 plugin
->info
.author
, filename
, NULL
);
1130 matched
= pm_tree_search(key
, haystack
);
1138 static void on_pm_tree_filter_entry_changed_cb(GtkEntry
*entry
, gpointer user_data
)
1140 GtkTreeModel
*filter_model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
1141 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model
));
1145 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry
*entry
, GtkEntryIconPosition icon_pos
,
1146 GdkEvent
*event
, gpointer user_data
)
1148 if (event
->button
.button
== 1 && icon_pos
== GTK_ENTRY_ICON_PRIMARY
)
1149 on_pm_tree_filter_entry_changed_cb(entry
, user_data
);
1153 static void pm_prepare_treeview(GtkWidget
*tree
, GtkListStore
*store
)
1155 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1156 GtkTreeViewColumn
*column
;
1157 GtkTreeModel
*filter_model
;
1160 GtkTreeSelection
*sel
;
1162 g_signal_connect(tree
, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip
), NULL
);
1163 gtk_widget_set_has_tooltip(tree
, TRUE
);
1164 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
1166 checkbox_renderer
= gtk_cell_renderer_toggle_new();
1167 column
= gtk_tree_view_column_new_with_attributes(
1168 _("Active"), checkbox_renderer
, "active", PLUGIN_COLUMN_CHECK
, NULL
);
1169 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1170 g_signal_connect(checkbox_renderer
, "toggled", G_CALLBACK(pm_plugin_toggled
), NULL
);
1172 text_renderer
= gtk_cell_renderer_text_new();
1173 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1174 column
= gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer
, NULL
);
1175 gtk_tree_view_column_set_cell_data_func(column
, text_renderer
,
1176 pm_treeview_text_cell_data_func
, NULL
, NULL
);
1177 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1179 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
1180 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree
), FALSE
);
1181 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
,
1182 pm_tree_sort_func
, NULL
, NULL
);
1183 gtk_tree_sortable_set_sort_column_id(
1184 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
, GTK_SORT_ASCENDING
);
1186 /* selection handling */
1187 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
1188 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_SINGLE
);
1189 g_signal_connect(sel
, "changed", G_CALLBACK(pm_selection_changed
), NULL
);
1191 g_signal_connect(tree
, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb
), NULL
);
1193 list
= g_list_first(plugin_list
);
1196 gtk_list_store_append(store
, &iter
);
1197 gtk_list_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1198 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1203 for (; list
!= NULL
; list
= list
->next
)
1207 gtk_list_store_append(store
, &iter
);
1208 gtk_list_store_set(store
, &iter
,
1209 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1210 PLUGIN_COLUMN_PLUGIN
, p
,
1215 filter_model
= gtk_tree_model_filter_new(GTK_TREE_MODEL(store
), NULL
);
1216 gtk_tree_model_filter_set_visible_func(
1217 GTK_TREE_MODEL_FILTER(filter_model
), pm_tree_filter_func
, NULL
, NULL
);
1219 /* set model to tree view */
1220 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), filter_model
);
1221 g_object_unref(store
);
1222 g_object_unref(filter_model
);
1226 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton
*button
, gpointer user_data
)
1228 GtkTreeModel
*model
;
1229 GtkTreeSelection
*selection
;
1233 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1234 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1236 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1240 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1241 plugin_show_configure(&p
->public);
1242 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
&& p
->help
!= NULL
)
1244 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_KEYBINDINGS
&& p
->key_group
&& p
->key_group
->plugin_key_count
> 0)
1245 keybindings_dialog_show_prefs_scroll(p
->info
.name
);
1252 free_non_active_plugin(gpointer data
, gpointer user_data
)
1254 Plugin
*plugin
= data
;
1256 /* don't do anything when closing the plugin manager and it is an active plugin */
1257 if (is_active_plugin(plugin
))
1260 plugin_free(plugin
);
1264 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1265 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1266 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1270 case GTK_RESPONSE_CLOSE
:
1271 case GTK_RESPONSE_DELETE_EVENT
:
1272 if (plugin_list
!= NULL
)
1274 /* remove all non-active plugins from the list */
1275 g_list_foreach(plugin_list
, free_non_active_plugin
, NULL
);
1276 g_list_free(plugin_list
);
1279 gtk_widget_destroy(GTK_WIDGET(dialog
));
1281 configuration_save();
1283 case PM_BUTTON_CONFIGURE
:
1284 case PM_BUTTON_HELP
:
1285 case PM_BUTTON_KEYBINDINGS
:
1286 /* forward event to the generic handler */
1287 pm_on_plugin_button_clicked(NULL
, GINT_TO_POINTER(response
));
1293 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1295 GtkWidget
*vbox
, *vbox2
, *swin
, *label
, *menu_item
, *filter_entry
;
1297 /* before showing the dialog, we need to create the list of available plugins */
1300 pm_widgets
.dialog
= gtk_dialog_new();
1301 gtk_window_set_title(GTK_WINDOW(pm_widgets
.dialog
), _("Plugins"));
1302 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets
.dialog
), GTK_WINDOW(main_widgets
.window
));
1303 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets
.dialog
), TRUE
);
1305 vbox
= ui_dialog_vbox_new(GTK_DIALOG(pm_widgets
.dialog
));
1306 gtk_widget_set_name(pm_widgets
.dialog
, "GeanyDialog");
1307 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
1309 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
1311 pm_widgets
.help_button
= gtk_dialog_add_button(
1312 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_HELP
, PM_BUTTON_HELP
);
1313 pm_widgets
.configure_button
= gtk_dialog_add_button(
1314 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_PREFERENCES
, PM_BUTTON_CONFIGURE
);
1315 pm_widgets
.keybindings_button
= gtk_dialog_add_button(
1316 GTK_DIALOG(pm_widgets
.dialog
), _("Keybindings"), PM_BUTTON_KEYBINDINGS
);
1317 gtk_dialog_add_button(GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
);
1318 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets
.dialog
), GTK_RESPONSE_CLOSE
);
1321 pm_widgets
.filter_entry
= filter_entry
= gtk_entry_new();
1322 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry
), GTK_ENTRY_ICON_PRIMARY
, GTK_STOCK_FIND
);
1323 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry
));
1324 g_signal_connect(filter_entry
, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb
), NULL
);
1325 g_signal_connect(filter_entry
, "icon-release",
1326 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb
), NULL
);
1328 /* prepare treeview */
1329 pm_widgets
.tree
= gtk_tree_view_new();
1330 pm_widgets
.store
= gtk_list_store_new(
1331 PLUGIN_N_COLUMNS
, G_TYPE_BOOLEAN
, G_TYPE_POINTER
);
1332 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1334 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1335 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
1336 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1337 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1338 gtk_container_add(GTK_CONTAINER(swin
), pm_widgets
.tree
);
1340 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1342 /* plugin popup menu */
1343 pm_widgets
.popup_menu
= gtk_menu_new();
1345 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
1346 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1347 g_signal_connect(menu_item
, "activate",
1348 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_CONFIGURE
));
1349 pm_widgets
.popup_configure_menu_item
= menu_item
;
1351 menu_item
= gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1352 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1353 g_signal_connect(menu_item
, "activate",
1354 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS
));
1355 pm_widgets
.popup_keybindings_menu_item
= menu_item
;
1357 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP
, NULL
);
1358 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1359 g_signal_connect(menu_item
, "activate",
1360 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_HELP
));
1361 pm_widgets
.popup_help_menu_item
= menu_item
;
1363 /* put it together */
1364 vbox2
= gtk_vbox_new(FALSE
, 6);
1365 gtk_box_pack_start(GTK_BOX(vbox2
), label
, FALSE
, FALSE
, 0);
1366 gtk_box_pack_start(GTK_BOX(vbox2
), filter_entry
, FALSE
, FALSE
, 0);
1367 gtk_box_pack_start(GTK_BOX(vbox2
), swin
, TRUE
, TRUE
, 0);
1369 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
1371 gtk_box_pack_start(GTK_BOX(vbox
), vbox2
, TRUE
, TRUE
, 0);
1372 gtk_widget_show_all(pm_widgets
.dialog
);
1373 gtk_widget_show_all(pm_widgets
.popup_menu
);
1375 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
1376 pm_update_buttons(NULL
);
1377 gtk_widget_grab_focus(pm_widgets
.filter_entry
);