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
);
80 Plugin
*plugin
; /* &builtin_so_proxy_plugin for native plugins */
84 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, const gchar
*filename
, gpointer pdata
);
85 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, gpointer load_data
, gpointer pdata
);
87 static Plugin builtin_so_proxy_plugin
= {
89 .load
= plugin_load_gmodule
,
90 .unload
= plugin_unload_gmodule
,
92 /* rest of Plugin can be NULL/0 */
95 static PluginProxy builtin_so_proxy
= {
96 .extension
= G_MODULE_SUFFIX
,
97 .plugin
= &builtin_so_proxy_plugin
,
100 static GQueue active_proxies
= G_QUEUE_INIT
;
102 static void plugin_free(Plugin
*plugin
);
104 static GeanyData geany_data
;
107 geany_data_init(void)
122 NULL
, /* Remove field on next ABI break (abi-todo) */
130 /* In order to have nested proxies work the count of dependent plugins must propagate up.
131 * This prevents that any plugin in the tree is unloaded while a leaf plugin is active. */
132 static void proxied_count_inc(Plugin
*proxy
)
136 proxy
->proxied_count
+= 1;
137 proxy
= proxy
->proxy
;
138 } while (proxy
!= NULL
);
142 static void proxied_count_dec(Plugin
*proxy
)
144 g_warn_if_fail(proxy
->proxied_count
> 0);
148 proxy
->proxied_count
-= 1;
149 proxy
= proxy
->proxy
;
150 } while (proxy
!= NULL
);
154 /* Prevent the same plugin filename being loaded more than once.
155 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
157 plugin_loaded(Plugin
*plugin
)
159 gchar
*basename_module
, *basename_loaded
;
162 basename_module
= g_path_get_basename(plugin
->filename
);
163 for (item
= plugin_list
; item
!= NULL
; item
= g_list_next(item
))
165 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
167 if (utils_str_equal(basename_module
, basename_loaded
))
169 g_free(basename_loaded
);
170 g_free(basename_module
);
173 g_free(basename_loaded
);
175 /* Look also through the list of active plugins. This prevents problems when we have the same
176 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
177 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
178 * the plugin manager would list both plugins. Additionally, unloading the active plugin
179 * would cause a crash. */
180 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
182 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
184 if (utils_str_equal(basename_module
, basename_loaded
))
186 g_free(basename_loaded
);
187 g_free(basename_module
);
190 g_free(basename_loaded
);
192 g_free(basename_module
);
197 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
201 g_return_val_if_fail(filename
, FALSE
);
203 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
205 if (utils_str_equal(filename
, ((Plugin
*)item
->data
)->filename
))
213 /* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
214 #define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
217 plugin_check_version(Plugin
*plugin
, int plugin_version_code
)
220 if (plugin_version_code
< 0)
222 gchar
*name
= g_path_get_basename(plugin
->filename
);
223 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
224 "release of Geany - please recompile it."), name
);
225 geany_debug("Plugin \"%s\" is not binary compatible with this "
226 "release of Geany - recompile it.", name
);
230 else if (plugin_version_code
> GEANY_API_VERSION
)
232 gchar
*name
= g_path_get_basename(plugin
->filename
);
233 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
234 name
, plugin_version_code
);
243 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
250 cb
= &callbacks
[len
];
251 if (!cb
->signal_name
|| !cb
->callback
)
258 for (i
= 0; i
< len
; i
++)
262 /* Pass the callback data as default user_data if none was set by the plugin itself */
263 plugin_signal_connect(&plugin
->public, NULL
, cb
->signal_name
, cb
->after
,
264 cb
->callback
, cb
->user_data
? cb
->user_data
: plugin
->cb_data
);
269 static void read_key_group(Plugin
*plugin
)
271 GeanyKeyGroupInfo
*p_key_info
;
272 GeanyKeyGroup
**p_key_group
;
273 GModule
*module
= plugin
->proxy_data
;
275 g_module_symbol(module
, "plugin_key_group_info", (void *) &p_key_info
);
276 g_module_symbol(module
, "plugin_key_group", (void *) &p_key_group
);
277 if (p_key_info
&& p_key_group
)
279 GeanyKeyGroupInfo
*key_info
= p_key_info
;
282 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
283 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
289 GeanyKeyGroup
*key_group
=
290 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
292 *p_key_group
= key_group
;
295 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
296 "count field is zero. Maybe use plugin_set_key_group() instead?",
300 else if (p_key_info
|| p_key_group
)
301 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
302 "Maybe use plugin_set_key_group() instead?",
307 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
309 const Plugin
*pa
= a
;
310 const Plugin
*pb
= b
;
312 return strcmp(pa
->info
.name
, pb
->info
.name
);
316 /** Register a plugin to Geany.
318 * The plugin will show up in the plugin manager. The user can interact with
319 * it based on the functions it provides and installed GUI elements.
321 * You must initialize the info and funcs fields of @ref GeanyPlugin
322 * appropriately prior to calling this, otherwise registration will fail. For
323 * info at least a valid name must be set (possibly localized). For funcs,
324 * at least init() and cleanup() functions must be implemented and set.
326 * The return value must be checked. It may be FALSE if the plugin failed to register which can
327 * mainly happen for two reasons (future Geany versions may add new failure conditions):
328 * - Not all mandatory fields of GeanyPlugin have been set.
329 * - The ABI or API versions reported by the plugin are incompatible with the running Geany.
331 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
332 * handles @a api_version and @a abi_version.
334 * @param plugin The plugin provided by Geany
335 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
336 * @param min_api_version The minimum API version required by the plugin
337 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
339 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
341 * @since 1.26 (API 225)
342 * @see GEANY_PLUGIN_REGISTER()
345 gboolean
geany_plugin_register(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
349 GeanyPluginFuncs
*cbs
= plugin
->funcs
;
351 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
354 /* already registered successfully */
355 g_return_val_if_fail(!PLUGIN_LOADED_OK(p
), FALSE
);
357 /* Prevent registering incompatible plugins. */
358 if (! plugin_check_version(p
, PLUGIN_VERSION_CODE(api_version
, abi_version
)))
361 /* Only init and cleanup callbacks are truly mandatory. */
362 if (! cbs
->init
|| ! cbs
->cleanup
)
364 gchar
*name
= g_path_get_basename(p
->filename
);
365 geany_debug("Plugin '%s' has no %s function - ignoring plugin!", name
,
366 cbs
->init
? "cleanup" : "init");
371 /* Yes, name is checked again later on, however we want return FALSE here
372 * to signal the error back to the plugin (but we don't print the message twice) */
373 if (! EMPTY(p
->info
.name
))
374 p
->flags
= LOADED_OK
;
377 /* If it ever becomes necessary we can save the api version in Plugin
378 * and apply compat code on a per-plugin basis, because we learn about
379 * the requested API version here. For now it's not necessary. */
381 return PLUGIN_LOADED_OK(p
);
385 /** Register a plugin to Geany, with plugin-defined data.
387 * This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
388 * Refer to that function for more details on registering in general.
390 * @p pdata is the pointer going to be passed to the individual plugin callbacks
391 * of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
392 * @p pdata, which connects the data to the plugin's module life time.
394 * You cannot use geany_plugin_set_data() after registering with this function. Use
395 * geany_plugin_register() if you need to.
397 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
398 * handles @p api_version and @p abi_version.
400 * @param plugin The plugin provided by Geany.
401 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
402 * @param min_api_version The minimum API version required by the plugin.
403 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
404 * @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
405 * @param free_func Function used to deallocate @a pdata, may be @c NULL.
407 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
409 * @since 1.26 (API 225)
410 * @see GEANY_PLUGIN_REGISTER_FULL()
411 * @see geany_plugin_register()
414 gboolean
geany_plugin_register_full(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
415 gint abi_version
, gpointer pdata
, GDestroyNotify free_func
)
417 if (geany_plugin_register(plugin
, api_version
, min_api_version
, abi_version
))
419 geany_plugin_set_data(plugin
, pdata
, free_func
);
420 /* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
421 * as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
422 * after GeanyPluginFuncs::cleanup() */
423 plugin
->priv
->flags
|= LOAD_DATA
;
429 struct LegacyRealFuncs
431 void (*init
) (GeanyData
*data
);
432 GtkWidget
* (*configure
) (GtkDialog
*dialog
);
434 void (*cleanup
) (void);
437 /* Wrappers to support legacy plugins are below */
438 static gboolean
legacy_init(GeanyPlugin
*plugin
, gpointer pdata
)
440 struct LegacyRealFuncs
*h
= pdata
;
441 h
->init(plugin
->geany_data
);
445 static void legacy_cleanup(GeanyPlugin
*plugin
, gpointer pdata
)
447 struct LegacyRealFuncs
*h
= pdata
;
448 /* Can be NULL because it's optional for legacy plugins */
453 static void legacy_help(GeanyPlugin
*plugin
, gpointer pdata
)
455 struct LegacyRealFuncs
*h
= pdata
;
459 static GtkWidget
*legacy_configure(GeanyPlugin
*plugin
, GtkDialog
*parent
, gpointer pdata
)
461 struct LegacyRealFuncs
*h
= pdata
;
462 return h
->configure(parent
);
465 static void free_legacy_cbs(gpointer data
)
467 g_slice_free(struct LegacyRealFuncs
, data
);
470 /* This function is the equivalent of geany_plugin_register() for legacy-style
471 * plugins which we continue to load for the time being. */
472 static void register_legacy_plugin(Plugin
*plugin
, GModule
*module
)
474 gint (*p_version_check
) (gint abi_version
);
475 void (*p_set_info
) (PluginInfo
*info
);
476 void (*p_init
) (GeanyData
*geany_data
);
477 GeanyData
**p_geany_data
;
478 struct LegacyRealFuncs
*h
;
480 #define CHECK_FUNC(__x) \
481 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
483 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
484 g_module_name(module)); \
487 CHECK_FUNC(version_check
);
488 CHECK_FUNC(set_info
);
492 /* We must verify the version first. If the plugin has become incompatible any
493 * further actions should be considered invalid and therefore skipped. */
494 if (! plugin_check_version(plugin
, p_version_check(GEANY_ABI_VERSION
)))
497 h
= g_slice_new(struct LegacyRealFuncs
);
499 /* Since the version check passed we can proceed with setting basic fields and
500 * calling its set_info() (which might want to call Geany functions already). */
501 g_module_symbol(module
, "geany_data", (void *) &p_geany_data
);
503 *p_geany_data
= &geany_data
;
504 /* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
505 p_set_info(&plugin
->info
);
507 /* If all went well we can set the remaining callbacks and let it go for good. */
509 g_module_symbol(module
, "plugin_configure", (void *) &h
->configure
);
510 g_module_symbol(module
, "plugin_configure_single", (void *) &plugin
->configure_single
);
511 g_module_symbol(module
, "plugin_help", (void *) &h
->help
);
512 g_module_symbol(module
, "plugin_cleanup", (void *) &h
->cleanup
);
513 /* pointer to callbacks struct can be stored directly, no wrapper necessary */
514 g_module_symbol(module
, "plugin_callbacks", (void *) &plugin
->cbs
.callbacks
);
517 if (h
->configure
&& plugin
->configure_single
)
518 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
519 "only plugin_configure() will be used!",
521 if (h
->cleanup
== NULL
)
522 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
526 plugin
->cbs
.init
= legacy_init
;
527 plugin
->cbs
.cleanup
= legacy_cleanup
;
528 plugin
->cbs
.configure
= h
->configure
? legacy_configure
: NULL
;
529 plugin
->cbs
.help
= h
->help
? legacy_help
: NULL
;
531 plugin
->flags
= LOADED_OK
| IS_LEGACY
;
532 geany_plugin_set_data(&plugin
->public, h
, free_legacy_cbs
);
537 plugin_load(Plugin
*plugin
)
539 gboolean init_ok
= TRUE
;
541 /* Start the plugin. Legacy plugins require additional cruft. */
542 if (PLUGIN_IS_LEGACY(plugin
) && plugin
->proxy
== &builtin_so_proxy_plugin
)
544 GeanyPlugin
**p_geany_plugin
;
546 PluginFields
**plugin_fields
;
547 GModule
*module
= plugin
->proxy_data
;
548 /* set these symbols before plugin_init() is called
549 * we don't set geany_data since it is set directly by plugin_new() */
550 g_module_symbol(module
, "geany_plugin", (void *) &p_geany_plugin
);
552 *p_geany_plugin
= &plugin
->public;
553 g_module_symbol(module
, "plugin_info", (void *) &p_info
);
555 *p_info
= &plugin
->info
;
556 g_module_symbol(module
, "plugin_fields", (void *) &plugin_fields
);
558 *plugin_fields
= &plugin
->fields
;
559 read_key_group(plugin
);
561 /* Legacy plugin_init() cannot fail. */
562 plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
564 /* now read any plugin-owned data that might have been set in plugin_init() */
565 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
567 ui_add_document_sensitive(plugin
->fields
.menu_item
);
572 init_ok
= plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
578 /* new-style plugins set their callbacks in geany_load_module() */
579 if (plugin
->cbs
.callbacks
)
580 add_callbacks(plugin
, plugin
->cbs
.callbacks
);
582 /* remember which plugins are active.
583 * keep list sorted so tools menu items and plugin preference tabs are
584 * sorted by plugin name */
585 active_plugin_list
= g_list_insert_sorted(active_plugin_list
, plugin
, cmp_plugin_names
);
586 proxied_count_inc(plugin
->proxy
);
588 geany_debug("Loaded: %s (%s)", plugin
->filename
, plugin
->info
.name
);
593 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, const gchar
*fname
, gpointer pdata
)
596 void (*p_geany_load_module
)(GeanyPlugin
*);
598 g_return_val_if_fail(g_module_supported(), NULL
);
599 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
600 * causing a segfault. Without that flag the module will safely fail to load.
601 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
602 * otherwise not be detected due to the shadowing of Geany's app variable.
603 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
604 * function from a plugin will be shadowed. */
605 module
= g_module_open(fname
, G_MODULE_BIND_LOCAL
);
608 geany_debug("Can't load plugin: %s", g_module_error());
612 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
613 g_module_symbol(module
, "geany_load_module", (void *) &p_geany_load_module
);
614 if (p_geany_load_module
)
616 /* set this here already so plugins can call i.e. plugin_module_make_resident()
617 * right from their geany_load_module() */
618 subplugin
->priv
->proxy_data
= module
;
620 /* This is a new style plugin. It should fill in plugin->info and then call
621 * geany_plugin_register() in its geany_load_module() to successfully load.
622 * The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
623 * We check the LOADED_OK flag separately to protect us against buggy plugins
624 * who ignore the result of geany_plugin_register() and register anyway */
625 p_geany_load_module(subplugin
);
629 /* This is the legacy / deprecated code path. It does roughly the same as
630 * geany_load_module() and geany_plugin_register() together for the new ones */
631 register_legacy_plugin(subplugin
->priv
, module
);
633 /* We actually check the LOADED_OK flag later */
638 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, gpointer load_data
, gpointer pdata
)
640 GModule
*module
= (GModule
*) load_data
;
642 g_return_if_fail(module
!= NULL
);
644 if (! g_module_close(module
))
645 g_warning("%s: %s", subplugin
->priv
->filename
, g_module_error());
649 /* Load and optionally init a plugin.
650 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
651 * called, the plugin will be started, if not the plugin will be read only (for the list of
652 * available plugins in the plugin manager).
653 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
655 plugin_new(Plugin
*proxy
, const gchar
*fname
, gboolean load_plugin
, gboolean add_to_list
)
659 g_return_val_if_fail(fname
, NULL
);
660 g_return_val_if_fail(proxy
, NULL
);
662 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
664 plugin
= find_active_plugin_by_name(fname
);
667 geany_debug("Plugin \"%s\" already loaded.", fname
);
670 /* do not add to the list twice */
671 if (g_list_find(plugin_list
, plugin
) != NULL
)
674 plugin_list
= g_list_prepend(plugin_list
, plugin
);
679 plugin
= g_new0(Plugin
, 1);
680 plugin
->filename
= g_strdup(fname
);
681 plugin
->proxy
= proxy
;
682 plugin
->public.geany_data
= &geany_data
;
683 plugin
->public.priv
= plugin
;
684 /* Fields of plugin->info/funcs must to be initialized by the plugin */
685 plugin
->public.info
= &plugin
->info
;
686 plugin
->public.funcs
= &plugin
->cbs
;
687 plugin
->public.proxy_funcs
= &plugin
->proxy_cbs
;
689 if (plugin_loaded(plugin
))
691 geany_debug("Plugin \"%s\" already loaded.", fname
);
695 /* Load plugin, this should read its name etc. It must also call
696 * geany_plugin_register() for the following PLUGIN_LOADED_OK condition */
697 plugin
->proxy_data
= proxy
->proxy_cbs
.load(&proxy
->public, &plugin
->public, fname
, proxy
->cb_data
);
699 if (! PLUGIN_LOADED_OK(plugin
))
701 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname
);
705 /* The proxy assumes success, therefore we have to call unload from here
706 * on in case of errors */
707 if (EMPTY(plugin
->info
.name
))
709 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname
);
713 /* cb_data_destroy() frees plugin->cb_data. If that pointer also passed to unload() afterwards
714 * then that would become a use-after-free. Disallow this combination. If a proxy
715 * needs the same pointer it must not use a destroy func but free manually in its unload(). */
716 if (plugin
->proxy_data
== proxy
->cb_data
&& plugin
->cb_data_destroy
)
718 geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname
);
719 plugin
->proxy_data
= NULL
;
723 if (load_plugin
&& !plugin_load(plugin
))
725 /* Handle failing init same as failing to load for now. In future we
726 * could present a informational UI or something */
727 geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname
);
732 plugin_list
= g_list_prepend(plugin_list
, plugin
);
737 if (plugin
->cb_data_destroy
)
738 plugin
->cb_data_destroy(plugin
->cb_data
);
739 proxy
->proxy_cbs
.unload(&proxy
->public, &plugin
->public, plugin
->proxy_data
, proxy
->cb_data
);
741 g_free(plugin
->filename
);
747 static void on_object_weak_notify(gpointer data
, GObject
*old_ptr
)
749 Plugin
*plugin
= data
;
752 g_return_if_fail(plugin
&& plugin
->signal_ids
);
754 for (i
= 0; i
< plugin
->signal_ids
->len
; i
++)
756 SignalConnection
*sc
= &g_array_index(plugin
->signal_ids
, SignalConnection
, i
);
758 if (sc
->object
== old_ptr
)
760 g_array_remove_index_fast(plugin
->signal_ids
, i
);
761 /* we can break the loop right after finding the first match,
762 * because we will get one notification per connected signal */
769 /* add an object to watch for destruction, and release pointers to it when destroyed.
770 * this should only be used by plugin_signal_connect() to add a watch on
771 * the object lifetime and nuke out references to it in plugin->signal_ids */
772 void plugin_watch_object(Plugin
*plugin
, gpointer object
)
774 g_object_weak_ref(object
, on_object_weak_notify
, plugin
);
778 static void remove_callbacks(Plugin
*plugin
)
780 GArray
*signal_ids
= plugin
->signal_ids
;
781 SignalConnection
*sc
;
783 if (signal_ids
== NULL
)
786 foreach_array(SignalConnection
, sc
, signal_ids
)
788 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
789 g_object_weak_unref(sc
->object
, on_object_weak_notify
, plugin
);
792 g_array_free(signal_ids
, TRUE
);
796 static void remove_sources(Plugin
*plugin
)
800 item
= plugin
->sources
;
803 GList
*next
= item
->next
; /* cache the next pointer because current item will be freed */
805 g_source_destroy(item
->data
);
808 /* don't free the list here, it is allocated inside each source's data */
812 /* Make the GModule backing plugin resident (if it's GModule-backed at all) */
813 void plugin_make_resident(Plugin
*plugin
)
815 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
817 g_return_if_fail(plugin
->proxy_data
!= NULL
);
818 g_module_make_resident(plugin
->proxy_data
);
821 g_warning("Skipping g_module_make_resident() for non-native plugin");
825 /* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
826 gpointer
plugin_get_module_symbol(Plugin
*plugin
, const gchar
*sym
)
830 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
832 g_return_val_if_fail(plugin
->proxy_data
!= NULL
, NULL
);
833 if (g_module_symbol(plugin
->proxy_data
, sym
, &symbol
))
836 g_warning("Failed to locate signal handler for '%s': %s",
837 sym
, g_module_error());
839 else /* TODO: Could possibly support this via a new proxy hook */
840 g_warning("Failed to locate signal handler for '%s': Not supported for non-native plugins",
846 static gboolean
is_active_plugin(Plugin
*plugin
)
848 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
852 /* Clean up anything used by an active plugin */
854 plugin_cleanup(Plugin
*plugin
)
858 /* With geany_register_plugin cleanup is mandatory */
859 plugin
->cbs
.cleanup(&plugin
->public, plugin
->cb_data
);
861 remove_callbacks(plugin
);
862 remove_sources(plugin
);
864 if (plugin
->key_group
)
865 keybindings_free_group(plugin
->key_group
);
867 widget
= plugin
->toolbar_separator
.widget
;
869 gtk_widget_destroy(widget
);
871 if (!PLUGIN_HAS_LOAD_DATA(plugin
) && plugin
->cb_data_destroy
)
873 /* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
874 * if it was already set through geany_plugin_register_full() because we couldn't call
875 * its init() anymore (not without completely reloading it anyway). */
876 plugin
->cb_data_destroy(plugin
->cb_data
);
877 plugin
->cb_data
= NULL
;
878 plugin
->cb_data_destroy
= NULL
;
881 proxied_count_dec(plugin
->proxy
);
882 geany_debug("Unloaded: %s", plugin
->filename
);
886 /* Remove all plugins that proxy is a proxy for from plugin_list (and free) */
887 static void free_subplugins(Plugin
*proxy
)
894 GList
*next
= g_list_next(item
);
895 if (proxy
== ((Plugin
*) item
->data
)->proxy
)
897 /* plugin_free modifies plugin_list */
898 plugin_free((Plugin
*) item
->data
);
905 /* Returns true if the removal was successful (=> never for non-proxies) */
906 static gboolean
unregister_proxy(Plugin
*proxy
)
908 gboolean is_proxy
= FALSE
;
911 /* Remove the proxy from the proxy list first. It might appear more than once (once
912 * for each extension), but if it doesn't appear at all it's not actually a proxy */
913 foreach_list_safe(node
, active_proxies
.head
)
915 PluginProxy
*p
= node
->data
;
916 if (p
->plugin
== proxy
)
919 g_queue_delete_link(&active_proxies
, node
);
926 /* Cleanup a plugin and free all resources allocated on behalf of it.
928 * If the plugin is a proxy then this also takes special care to unload all
929 * subplugin loaded through it (make sure none of them is active!) */
931 plugin_free(Plugin
*plugin
)
935 g_return_if_fail(plugin
);
936 g_return_if_fail(plugin
->proxy
);
937 g_return_if_fail(plugin
->proxied_count
== 0);
939 proxy
= plugin
->proxy
;
940 /* If this a proxy remove all depending subplugins. We can assume none of them is *activated*
941 * (but potentially loaded). Note that free_subplugins() might call us through recursion */
942 if (is_active_plugin(plugin
))
944 if (unregister_proxy(plugin
))
945 free_subplugins(plugin
);
946 plugin_cleanup(plugin
);
949 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
950 plugin_list
= g_list_remove(plugin_list
, plugin
);
952 /* cb_data_destroy might be plugin code and must be called before unloading the module. */
953 if (plugin
->cb_data_destroy
)
954 plugin
->cb_data_destroy(plugin
->cb_data
);
955 proxy
->proxy_cbs
.unload(&proxy
->public, &plugin
->public, plugin
->proxy_data
, proxy
->cb_data
);
957 g_free(plugin
->filename
);
962 static gchar
*get_custom_plugin_path(const gchar
*plugin_path_config
,
963 const gchar
*plugin_path_system
)
965 gchar
*plugin_path_custom
;
967 if (EMPTY(prefs
.custom_plugin_path
))
970 plugin_path_custom
= utils_get_locale_from_utf8(prefs
.custom_plugin_path
);
971 utils_tidy_path(plugin_path_custom
);
973 /* check whether the custom plugin path is one of the system or user plugin paths
975 if (utils_str_equal(plugin_path_custom
, plugin_path_config
) ||
976 utils_str_equal(plugin_path_custom
, plugin_path_system
))
978 g_free(plugin_path_custom
);
981 return plugin_path_custom
;
985 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
986 * so we need to check active plugins are in the right place before loading */
987 static gboolean
check_plugin_path(const gchar
*fname
)
989 gchar
*plugin_path_config
;
990 gchar
*plugin_path_system
;
991 gchar
*plugin_path_custom
;
992 gboolean ret
= FALSE
;
994 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
995 if (g_str_has_prefix(fname
, plugin_path_config
))
998 plugin_path_system
= get_plugin_path();
999 if (g_str_has_prefix(fname
, plugin_path_system
))
1002 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1003 if (plugin_path_custom
)
1005 if (g_str_has_prefix(fname
, plugin_path_custom
))
1008 g_free(plugin_path_custom
);
1010 g_free(plugin_path_config
);
1011 g_free(plugin_path_system
);
1016 /* Returns NULL if this ain't a plugin,
1017 * otherwise it returns the appropriate PluginProxy instance to load it */
1018 static PluginProxy
* is_plugin(const gchar
*file
)
1023 /* extract file extension to avoid g_str_has_suffix() in the loop */
1024 ext
= (const gchar
*)strrchr(file
, '.');
1027 /* ensure the dot is really part of the filename */
1028 else if (strchr(ext
, G_DIR_SEPARATOR
) != NULL
)
1032 /* O(n*m), (m being extensions per proxy) doesn't scale very well in theory
1033 * but not a problem in practice yet */
1034 foreach_list(node
, active_proxies
.head
)
1036 PluginProxy
*proxy
= node
->data
;
1037 if (utils_str_casecmp(ext
, proxy
->extension
) == 0)
1039 Plugin
*p
= proxy
->plugin
;
1040 gint ret
= PROXY_MATCHED
;
1042 if (p
->proxy_cbs
.probe
)
1043 ret
= p
->proxy_cbs
.probe(&p
->public, file
, p
->cb_data
);
1048 case PROXY_MATCHED
|PROXY_NOLOAD
:
1051 if (ret
!= PROXY_IGNORED
)
1052 g_warning("Ignoring bogus return from proxy probe!\n");
1061 /* load active plugins at startup */
1063 load_active_plugins(void)
1065 guint i
, len
, proxies
;
1067 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
1070 /* If proxys are loaded we have to restart to load plugins that sort before their proxy */
1073 proxies
= active_proxies
.length
;
1074 g_list_free_full(failed_plugins_list
, (GDestroyNotify
) g_free
);
1075 failed_plugins_list
= NULL
;
1076 for (i
= 0; i
< len
; i
++)
1078 gchar
*fname
= active_plugins_pref
[i
];
1081 /* ensure we have canonical paths */
1083 while ((p
= strchr(p
, '/')) != NULL
)
1084 *p
= G_DIR_SEPARATOR
;
1087 if (!EMPTY(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
1089 PluginProxy
*proxy
= NULL
;
1090 if (check_plugin_path(fname
))
1091 proxy
= is_plugin(fname
);
1092 if (proxy
== NULL
|| plugin_new(proxy
->plugin
, fname
, TRUE
, FALSE
) == NULL
)
1093 failed_plugins_list
= g_list_prepend(failed_plugins_list
, g_strdup(fname
));
1096 } while (proxies
!= active_proxies
.length
);
1101 load_plugins_from_path(const gchar
*path
)
1103 GSList
*list
, *item
;
1106 list
= utils_get_file_list(path
, NULL
, NULL
);
1108 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
1110 gchar
*fname
= g_build_filename(path
, item
->data
, NULL
);
1111 PluginProxy
*proxy
= is_plugin(fname
);
1113 if (proxy
!= NULL
&& plugin_new(proxy
->plugin
, fname
, FALSE
, TRUE
))
1119 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
1123 geany_debug("Added %d plugin(s) in '%s'.", count
, path
);
1127 static gchar
*get_plugin_path(void)
1129 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN
));
1133 /* See load_all_plugins(), this simply sorts items with lower hierarchy level first
1134 * (where hierarchy level == number of intermediate proxies before the builtin so loader) */
1135 static gint
cmp_plugin_by_proxy(gconstpointer a
, gconstpointer b
)
1137 const Plugin
*pa
= a
;
1138 const Plugin
*pb
= b
;
1142 if (pa
->proxy
== pb
->proxy
)
1144 else if (pa
->proxy
== &builtin_so_proxy_plugin
)
1146 else if (pb
->proxy
== &builtin_so_proxy_plugin
)
1155 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1156 static void load_all_plugins(void)
1158 gchar
*plugin_path_config
;
1159 gchar
*plugin_path_system
;
1160 gchar
*plugin_path_custom
;
1162 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1163 plugin_path_system
= get_plugin_path();
1165 /* first load plugins in ~/.config/geany/plugins/ */
1166 load_plugins_from_path(plugin_path_config
);
1168 /* load plugins from a custom path */
1169 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1170 if (plugin_path_custom
)
1172 load_plugins_from_path(plugin_path_custom
);
1173 g_free(plugin_path_custom
);
1176 /* finally load plugins from $prefix/lib/geany */
1177 load_plugins_from_path(plugin_path_system
);
1179 /* It is important to sort any plugins that are proxied after their proxy because
1180 * pm_populate() needs the proxy to be loaded and active (if selected by user) in order
1181 * to properly set the value for the PLUGIN_COLUMN_CAN_UNCHECK column. The order between
1182 * sub-plugins does not matter, only between sub-plugins and their proxy, thus
1183 * sorting by hierarchy level is perfectly sufficient */
1184 plugin_list
= g_list_sort(plugin_list
, cmp_plugin_by_proxy
);
1186 g_free(plugin_path_config
);
1187 g_free(plugin_path_system
);
1191 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
1193 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
1195 gboolean have_plugin_menu_items
= FALSE
;
1197 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
1199 if (item
->data
== menu_separator
)
1201 if (i
< g_list_length(list
) - 1)
1203 have_plugin_menu_items
= TRUE
;
1211 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
1215 /* Calling this starts up plugin support */
1216 void plugins_load_active(void)
1220 want_plugins
= TRUE
;
1224 widget
= gtk_separator_menu_item_new();
1225 gtk_widget_show(widget
);
1226 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1228 widget
= gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1229 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1230 gtk_widget_show(widget
);
1231 g_signal_connect(widget
, "activate", G_CALLBACK(pm_show_dialog
), NULL
);
1233 menu_separator
= gtk_separator_menu_item_new();
1234 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
1235 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
1237 load_active_plugins();
1241 /* Update the global active plugins list so it's up-to-date when configuration
1242 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1243 static void update_active_plugins_pref(void)
1249 /* if plugins are disabled, don't clear list of active plugins */
1253 count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
1255 g_strfreev(active_plugins_pref
);
1259 active_plugins_pref
= NULL
;
1263 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
1265 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
1267 Plugin
*plugin
= list
->data
;
1269 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
1272 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
1274 const gchar
*fname
= list
->data
;
1276 active_plugins_pref
[i
] = g_strdup(fname
);
1279 active_plugins_pref
[i
] = NULL
;
1283 /* called even if plugin support is disabled */
1284 void plugins_init(void)
1289 path
= get_plugin_path();
1290 geany_debug("System plugin path: %s", path
);
1293 group
= stash_group_new("plugins");
1294 configuration_add_pref_group(group
, TRUE
);
1296 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
1297 "load_plugins", TRUE
, "check_plugins");
1298 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
1299 "custom_plugin_path", "", "extra_plugin_path_entry");
1301 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(update_active_plugins_pref
), NULL
);
1302 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
1304 g_queue_push_head(&active_proxies
, &builtin_so_proxy
);
1308 /* Same as plugin_free(), except it does nothing for proxies-in-use, to be called on
1309 * finalize in a loop */
1310 static void plugin_free_leaf(Plugin
*p
)
1312 if (p
->proxied_count
== 0)
1317 /* called even if plugin support is disabled */
1318 void plugins_finalize(void)
1320 if (failed_plugins_list
!= NULL
)
1322 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
1323 g_list_free(failed_plugins_list
);
1325 /* Have to loop because proxys cannot be unloaded until after all their
1326 * plugins are unloaded as well (the second loop should should catch all the remaining ones) */
1327 while (active_plugin_list
!= NULL
)
1328 g_list_foreach(active_plugin_list
, (GFunc
) plugin_free_leaf
, NULL
);
1330 g_strfreev(active_plugins_pref
);
1334 /* Check whether there are any plugins loaded which provide a configure symbol */
1335 gboolean
plugins_have_preferences(void)
1339 if (active_plugin_list
== NULL
)
1342 foreach_list(item
, active_plugin_list
)
1344 Plugin
*plugin
= item
->data
;
1345 if (plugin
->configure_single
!= NULL
|| plugin
->cbs
.configure
!= NULL
)
1353 /* Plugin Manager */
1357 PLUGIN_COLUMN_CHECK
= 0,
1358 PLUGIN_COLUMN_CAN_UNCHECK
,
1359 PLUGIN_COLUMN_PLUGIN
,
1361 PM_BUTTON_KEYBINDINGS
,
1362 PM_BUTTON_CONFIGURE
,
1370 GtkTreeStore
*store
;
1371 GtkWidget
*filter_entry
;
1372 GtkWidget
*configure_button
;
1373 GtkWidget
*keybindings_button
;
1374 GtkWidget
*help_button
;
1375 GtkWidget
*popup_menu
;
1376 GtkWidget
*popup_configure_menu_item
;
1377 GtkWidget
*popup_keybindings_menu_item
;
1378 GtkWidget
*popup_help_menu_item
;
1380 PluginManagerWidgets
;
1382 static PluginManagerWidgets pm_widgets
;
1385 static void pm_update_buttons(Plugin
*p
)
1387 gboolean has_configure
= FALSE
;
1388 gboolean has_help
= FALSE
;
1389 gboolean has_keybindings
= FALSE
;
1391 if (p
!= NULL
&& is_active_plugin(p
))
1393 has_configure
= p
->cbs
.configure
|| p
->configure_single
;
1394 has_help
= p
->cbs
.help
!= NULL
;
1395 has_keybindings
= p
->key_group
&& p
->key_group
->plugin_key_count
;
1398 gtk_widget_set_sensitive(pm_widgets
.configure_button
, has_configure
);
1399 gtk_widget_set_sensitive(pm_widgets
.help_button
, has_help
);
1400 gtk_widget_set_sensitive(pm_widgets
.keybindings_button
, has_keybindings
);
1402 gtk_widget_set_sensitive(pm_widgets
.popup_configure_menu_item
, has_configure
);
1403 gtk_widget_set_sensitive(pm_widgets
.popup_help_menu_item
, has_help
);
1404 gtk_widget_set_sensitive(pm_widgets
.popup_keybindings_menu_item
, has_keybindings
);
1408 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
1411 GtkTreeModel
*model
;
1414 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1416 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1419 pm_update_buttons(p
);
1424 static gboolean
find_iter_for_plugin(Plugin
*p
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
1429 for (valid
= gtk_tree_model_get_iter_first(model
, iter
);
1431 valid
= gtk_tree_model_iter_next(model
, iter
))
1433 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &pp
, -1);
1442 static void pm_populate(GtkTreeStore
*store
);
1445 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
1447 gboolean old_state
, state
;
1450 GtkTreeIter store_iter
;
1451 GtkTreePath
*path
= gtk_tree_path_new_from_string(pth
);
1452 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
1455 guint prev_num_proxies
;
1457 gtk_tree_model_get_iter(model
, &iter
, path
);
1459 gtk_tree_model_get(model
, &iter
,
1460 PLUGIN_COLUMN_CHECK
, &old_state
,
1461 PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1463 /* no plugins item */
1466 gtk_tree_path_free(path
);
1470 gtk_tree_model_filter_convert_iter_to_child_iter(
1471 GTK_TREE_MODEL_FILTER(model
), &store_iter
, &iter
);
1473 state
= ! old_state
; /* toggle the state */
1475 /* save the filename and proxy of the plugin */
1476 file_name
= g_strdup(p
->filename
);
1478 prev_num_proxies
= active_proxies
.length
;
1480 /* unload plugin module */
1482 /* save shortcuts (only need this group, but it doesn't take long) */
1483 keybindings_write_to_file();
1485 /* plugin_new() below may cause a tree view refresh with invalid p - set to NULL */
1486 gtk_tree_store_set(pm_widgets
.store
, &store_iter
,
1487 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1490 /* reload plugin module and initialize it if item is checked */
1491 p
= plugin_new(proxy
, file_name
, state
, TRUE
);
1494 /* plugin file may no longer be on disk, or is now incompatible */
1495 gtk_tree_store_remove(pm_widgets
.store
, &store_iter
);
1500 keybindings_load_keyfile(); /* load shortcuts */
1503 gtk_tree_store_set(pm_widgets
.store
, &store_iter
,
1504 PLUGIN_COLUMN_CHECK
, state
,
1505 PLUGIN_COLUMN_PLUGIN
, p
, -1);
1507 /* set again the sensitiveness of the configure and help buttons */
1508 pm_update_buttons(p
);
1510 /* Depending on the state disable the checkbox for the proxy of this plugin, and
1511 * only re-enable if the proxy is not used by any other plugin */
1512 if (p
->proxy
!= &builtin_so_proxy_plugin
)
1515 gboolean can_uncheck
;
1516 GtkTreePath
*store_path
= gtk_tree_model_filter_convert_path_to_child_path(
1517 GTK_TREE_MODEL_FILTER(model
), path
);
1519 g_warn_if_fail(store_path
!= NULL
);
1520 if (gtk_tree_path_up(store_path
))
1522 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets
.store
), &parent
, store_path
);
1525 can_uncheck
= FALSE
;
1527 can_uncheck
= p
->proxy
->proxied_count
== 0;
1529 gtk_tree_store_set(pm_widgets
.store
, &parent
,
1530 PLUGIN_COLUMN_CAN_UNCHECK
, can_uncheck
, -1);
1532 gtk_tree_path_free(store_path
);
1535 /* We need to find out if a proxy was added or removed because that affects the plugin list
1536 * presented by the plugin manager */
1537 if (prev_num_proxies
!= active_proxies
.length
)
1539 /* Rescan the plugin list as we now support more. Gives some "already loaded" warnings
1540 * they are unproblematic */
1541 if (prev_num_proxies
< active_proxies
.length
)
1544 pm_populate(pm_widgets
.store
);
1545 gtk_tree_view_expand_row(GTK_TREE_VIEW(pm_widgets
.tree
), path
, FALSE
);
1548 gtk_tree_path_free(path
);
1552 static void pm_populate(GtkTreeStore
*store
)
1557 gtk_tree_store_clear(store
);
1558 list
= g_list_first(plugin_list
);
1561 gtk_tree_store_append(store
, &iter
, NULL
);
1562 gtk_tree_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1563 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1567 for (; list
!= NULL
; list
= list
->next
)
1569 Plugin
*p
= list
->data
;
1572 if (p
->proxy
!= &builtin_so_proxy_plugin
1573 && find_iter_for_plugin(p
->proxy
, GTK_TREE_MODEL(pm_widgets
.store
), &parent
))
1574 gtk_tree_store_append(store
, &iter
, &parent
);
1576 gtk_tree_store_append(store
, &iter
, NULL
);
1578 gtk_tree_store_set(store
, &iter
,
1579 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1580 PLUGIN_COLUMN_PLUGIN
, p
,
1581 PLUGIN_COLUMN_CAN_UNCHECK
, (p
->proxied_count
== 0),
1587 static gboolean
pm_treeview_query_tooltip(GtkWidget
*widget
, gint x
, gint y
,
1588 gboolean keyboard_mode
, GtkTooltip
*tooltip
, gpointer user_data
)
1590 GtkTreeModel
*model
;
1594 gboolean can_uncheck
= TRUE
;
1596 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget
), &x
, &y
, keyboard_mode
,
1597 &model
, &path
, &iter
))
1600 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, PLUGIN_COLUMN_CAN_UNCHECK
, &can_uncheck
, -1);
1603 gchar
*prefix
, *suffix
, *details
, *markup
;
1606 uchk
= can_uncheck
?
1607 "" : _("\n<i>Other plugins depend on this. Disable them first to allow deactivation.</i>\n");
1608 /* Four allocations is less than ideal but meh */
1609 details
= g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1610 p
->info
.version
, p
->info
.author
, p
->filename
);
1611 prefix
= g_markup_printf_escaped("<b>%s</b>\n%s\n", p
->info
.name
, p
->info
.description
);
1612 suffix
= g_markup_printf_escaped("<small><i>\n%s</i></small>", details
);
1613 markup
= g_strconcat(prefix
, uchk
, suffix
, NULL
);
1615 gtk_tooltip_set_markup(tooltip
, markup
);
1616 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget
), tooltip
, path
);
1623 gtk_tree_path_free(path
);
1629 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn
*column
, GtkCellRenderer
*cell
,
1630 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer data
)
1634 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1637 g_object_set(cell
, "text", _("No plugins available."), NULL
);
1640 gchar
*markup
= g_markup_printf_escaped("<b>%s</b>\n%s", p
->info
.name
, p
->info
.description
);
1642 g_object_set(cell
, "markup", markup
, NULL
);
1648 static gboolean
pm_treeview_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
1649 G_GNUC_UNUSED gpointer user_data
)
1651 if (event
->button
== 3)
1653 gtk_menu_popup(GTK_MENU(pm_widgets
.popup_menu
), NULL
, NULL
, NULL
, NULL
,
1654 event
->button
, event
->time
);
1660 static gint
pm_tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1665 gtk_tree_model_get(model
, a
, PLUGIN_COLUMN_PLUGIN
, &pa
, -1);
1666 gtk_tree_model_get(model
, b
, PLUGIN_COLUMN_PLUGIN
, &pb
, -1);
1669 return strcmp(pa
->info
.name
, pb
->info
.name
);
1675 static gboolean
pm_tree_search(const gchar
*key
, const gchar
*haystack
)
1677 gchar
*normalized_string
= NULL
;
1678 gchar
*normalized_key
= NULL
;
1679 gchar
*case_normalized_string
= NULL
;
1680 gchar
*case_normalized_key
= NULL
;
1681 gboolean matched
= TRUE
;
1683 normalized_string
= g_utf8_normalize(haystack
, -1, G_NORMALIZE_ALL
);
1684 normalized_key
= g_utf8_normalize(key
, -1, G_NORMALIZE_ALL
);
1686 if (normalized_string
!= NULL
&& normalized_key
!= NULL
)
1688 GString
*stripped_key
;
1689 gchar
**subkey
, **subkeys
;
1691 case_normalized_string
= g_utf8_casefold(normalized_string
, -1);
1692 case_normalized_key
= g_utf8_casefold(normalized_key
, -1);
1693 stripped_key
= g_string_new(case_normalized_key
);
1694 do {} while (utils_string_replace_all(stripped_key
, " ", " "));
1695 subkeys
= g_strsplit(stripped_key
->str
, " ", -1);
1696 g_string_free(stripped_key
, TRUE
);
1697 foreach_strv(subkey
, subkeys
)
1699 if (strstr(case_normalized_string
, *subkey
) == NULL
)
1705 g_strfreev(subkeys
);
1708 g_free(normalized_key
);
1709 g_free(normalized_string
);
1710 g_free(case_normalized_key
);
1711 g_free(case_normalized_string
);
1717 static gboolean
pm_tree_filter_func(GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1722 gchar
*haystack
, *filename
;
1724 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &plugin
, -1);
1728 key
= gtk_entry_get_text(GTK_ENTRY(pm_widgets
.filter_entry
));
1730 filename
= g_path_get_basename(plugin
->filename
);
1731 haystack
= g_strjoin(" ", plugin
->info
.name
, plugin
->info
.description
,
1732 plugin
->info
.author
, filename
, NULL
);
1733 matched
= pm_tree_search(key
, haystack
);
1741 static void on_pm_tree_filter_entry_changed_cb(GtkEntry
*entry
, gpointer user_data
)
1743 GtkTreeModel
*filter_model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
1744 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model
));
1748 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry
*entry
, GtkEntryIconPosition icon_pos
,
1749 GdkEvent
*event
, gpointer user_data
)
1751 if (event
->button
.button
== 1 && icon_pos
== GTK_ENTRY_ICON_PRIMARY
)
1752 on_pm_tree_filter_entry_changed_cb(entry
, user_data
);
1756 static void pm_prepare_treeview(GtkWidget
*tree
, GtkTreeStore
*store
)
1758 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1759 GtkTreeViewColumn
*column
;
1760 GtkTreeModel
*filter_model
;
1761 GtkTreeSelection
*sel
;
1763 g_signal_connect(tree
, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip
), NULL
);
1764 gtk_widget_set_has_tooltip(tree
, TRUE
);
1765 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
1767 checkbox_renderer
= gtk_cell_renderer_toggle_new();
1768 column
= gtk_tree_view_column_new_with_attributes(
1769 _("Active"), checkbox_renderer
,
1770 "active", PLUGIN_COLUMN_CHECK
, "activatable", PLUGIN_COLUMN_CAN_UNCHECK
, NULL
);
1771 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1772 g_signal_connect(checkbox_renderer
, "toggled", G_CALLBACK(pm_plugin_toggled
), NULL
);
1774 text_renderer
= gtk_cell_renderer_text_new();
1775 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1776 column
= gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer
, NULL
);
1777 gtk_tree_view_column_set_cell_data_func(column
, text_renderer
,
1778 pm_treeview_text_cell_data_func
, NULL
, NULL
);
1779 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1781 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
1782 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree
), FALSE
);
1783 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
,
1784 pm_tree_sort_func
, NULL
, NULL
);
1785 gtk_tree_sortable_set_sort_column_id(
1786 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
, GTK_SORT_ASCENDING
);
1788 /* selection handling */
1789 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
1790 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_SINGLE
);
1791 g_signal_connect(sel
, "changed", G_CALLBACK(pm_selection_changed
), NULL
);
1793 g_signal_connect(tree
, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb
), NULL
);
1796 filter_model
= gtk_tree_model_filter_new(GTK_TREE_MODEL(store
), NULL
);
1797 gtk_tree_model_filter_set_visible_func(
1798 GTK_TREE_MODEL_FILTER(filter_model
), pm_tree_filter_func
, NULL
, NULL
);
1800 /* set model to tree view */
1801 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), filter_model
);
1802 g_object_unref(filter_model
);
1808 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton
*button
, gpointer user_data
)
1810 GtkTreeModel
*model
;
1811 GtkTreeSelection
*selection
;
1815 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1816 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1818 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1822 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1823 plugin_show_configure(&p
->public);
1824 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
)
1825 p
->cbs
.help(&p
->public, p
->cb_data
);
1826 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_KEYBINDINGS
&& p
->key_group
&& p
->key_group
->plugin_key_count
> 0)
1827 keybindings_dialog_show_prefs_scroll(p
->info
.name
);
1834 free_non_active_plugin(gpointer data
, gpointer user_data
)
1836 Plugin
*plugin
= data
;
1838 /* don't do anything when closing the plugin manager and it is an active plugin */
1839 if (is_active_plugin(plugin
))
1842 plugin_free(plugin
);
1846 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1847 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1848 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1852 case GTK_RESPONSE_CLOSE
:
1853 case GTK_RESPONSE_DELETE_EVENT
:
1854 if (plugin_list
!= NULL
)
1856 /* remove all non-active plugins from the list */
1857 g_list_foreach(plugin_list
, free_non_active_plugin
, NULL
);
1858 g_list_free(plugin_list
);
1861 gtk_widget_destroy(GTK_WIDGET(dialog
));
1863 configuration_save();
1865 case PM_BUTTON_CONFIGURE
:
1866 case PM_BUTTON_HELP
:
1867 case PM_BUTTON_KEYBINDINGS
:
1868 /* forward event to the generic handler */
1869 pm_on_plugin_button_clicked(NULL
, GINT_TO_POINTER(response
));
1875 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1877 GtkWidget
*vbox
, *vbox2
, *swin
, *label
, *menu_item
, *filter_entry
;
1879 /* before showing the dialog, we need to create the list of available plugins */
1882 pm_widgets
.dialog
= gtk_dialog_new();
1883 gtk_window_set_title(GTK_WINDOW(pm_widgets
.dialog
), _("Plugins"));
1884 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets
.dialog
), GTK_WINDOW(main_widgets
.window
));
1885 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets
.dialog
), TRUE
);
1887 vbox
= ui_dialog_vbox_new(GTK_DIALOG(pm_widgets
.dialog
));
1888 gtk_widget_set_name(pm_widgets
.dialog
, "GeanyDialog");
1889 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
1891 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
1893 pm_widgets
.help_button
= gtk_dialog_add_button(
1894 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_HELP
, PM_BUTTON_HELP
);
1895 pm_widgets
.configure_button
= gtk_dialog_add_button(
1896 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_PREFERENCES
, PM_BUTTON_CONFIGURE
);
1897 pm_widgets
.keybindings_button
= gtk_dialog_add_button(
1898 GTK_DIALOG(pm_widgets
.dialog
), _("Keybindings"), PM_BUTTON_KEYBINDINGS
);
1899 gtk_dialog_add_button(GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
);
1900 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets
.dialog
), GTK_RESPONSE_CLOSE
);
1903 pm_widgets
.filter_entry
= filter_entry
= gtk_entry_new();
1904 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry
), GTK_ENTRY_ICON_PRIMARY
, GTK_STOCK_FIND
);
1905 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry
));
1906 g_signal_connect(filter_entry
, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb
), NULL
);
1907 g_signal_connect(filter_entry
, "icon-release",
1908 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb
), NULL
);
1910 /* prepare treeview */
1911 pm_widgets
.tree
= gtk_tree_view_new();
1912 pm_widgets
.store
= gtk_tree_store_new(
1913 PLUGIN_N_COLUMNS
, G_TYPE_BOOLEAN
, G_TYPE_BOOLEAN
, G_TYPE_POINTER
);
1914 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1915 gtk_tree_view_expand_all(GTK_TREE_VIEW(pm_widgets
.tree
));
1916 g_object_unref(pm_widgets
.store
);
1918 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1919 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
1920 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1921 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1922 gtk_container_add(GTK_CONTAINER(swin
), pm_widgets
.tree
);
1924 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1926 /* plugin popup menu */
1927 pm_widgets
.popup_menu
= gtk_menu_new();
1929 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
1930 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1931 g_signal_connect(menu_item
, "activate",
1932 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_CONFIGURE
));
1933 pm_widgets
.popup_configure_menu_item
= menu_item
;
1935 menu_item
= gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1936 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1937 g_signal_connect(menu_item
, "activate",
1938 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS
));
1939 pm_widgets
.popup_keybindings_menu_item
= menu_item
;
1941 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP
, NULL
);
1942 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1943 g_signal_connect(menu_item
, "activate",
1944 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_HELP
));
1945 pm_widgets
.popup_help_menu_item
= menu_item
;
1947 /* put it together */
1948 vbox2
= gtk_vbox_new(FALSE
, 6);
1949 gtk_box_pack_start(GTK_BOX(vbox2
), label
, FALSE
, FALSE
, 0);
1950 gtk_box_pack_start(GTK_BOX(vbox2
), filter_entry
, FALSE
, FALSE
, 0);
1951 gtk_box_pack_start(GTK_BOX(vbox2
), swin
, TRUE
, TRUE
, 0);
1953 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
1955 gtk_box_pack_start(GTK_BOX(vbox
), vbox2
, TRUE
, TRUE
, 0);
1956 gtk_widget_show_all(pm_widgets
.dialog
);
1957 gtk_widget_show_all(pm_widgets
.popup_menu
);
1959 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
1960 pm_update_buttons(NULL
);
1961 gtk_widget_grab_focus(pm_widgets
.filter_entry
);
1965 /** Register the plugin as a proxy for other plugins
1967 * Proxy plugins register a list of file extensions and a set of callbacks that are called
1968 * appropriately. A plugin can be a proxy for multiple types of sub-plugins by handling
1969 * separate file extensions, however they must share the same set of hooks, because this
1970 * function can only be called at most once per plugin.
1972 * Each callback receives the plugin-defined data as parameter (see geany_plugin_register()). The
1973 * callbacks must be set prior to calling this, by assigning to @a plugin->proxy_funcs.
1974 * GeanyProxyFuncs::load and GeanyProxyFuncs::unload must be implemented.
1976 * Nested proxies are unsupported at this point (TODO).
1978 * @note It is entirely up to the proxy to provide access to Geany's plugin API. Native code
1979 * can naturally call Geany's API directly, for interpreted languages the proxy has to
1980 * implement some kind of bindings that the plugin can use.
1982 * @see proxy for detailed documentation and an example.
1984 * @param plugin The pointer to the plugin's GeanyPlugin instance
1985 * @param extensions A @c NULL-terminated string array of file extensions, excluding the dot.
1986 * @return @c TRUE if the proxy was successfully registered, otherwise @c FALSE
1988 * @since 1.26 (API 226)
1991 gboolean
geany_plugin_register_proxy(GeanyPlugin
*plugin
, const gchar
**extensions
)
1998 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
1999 g_return_val_if_fail(extensions
!= NULL
, FALSE
);
2000 g_return_val_if_fail(*extensions
!= NULL
, FALSE
);
2001 g_return_val_if_fail(plugin
->proxy_funcs
->load
!= NULL
, FALSE
);
2002 g_return_val_if_fail(plugin
->proxy_funcs
->unload
!= NULL
, FALSE
);
2005 /* Check if this was called already. We want to reserve for the use case of calling
2006 * this again to set new supported extensions (for example, based on proxy configuration). */
2007 foreach_list(node
, active_proxies
.head
)
2010 g_return_val_if_fail(p
!= proxy
->plugin
, FALSE
);
2013 foreach_strv(ext
, extensions
)
2015 proxy
= g_new(PluginProxy
, 1);
2016 g_strlcpy(proxy
->extension
, *ext
, sizeof(proxy
->extension
));
2018 /* prepend, so that plugins automatically override core providers for a given extension */
2019 g_queue_push_head(&active_proxies
, proxy
);