File: plugins.c

package info (click to toggle)
roger-router 1.8.14-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,256 kB
  • sloc: ansic: 24,958; sh: 4,322; makefile: 702; xml: 511; cpp: 130
file content (137 lines) | stat: -rw-r--r-- 4,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * The libroutermanager project
 * Copyright (c) 2012-2014 Jan-Michael Brummer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <libpeas/peas.h>

#include <libroutermanager/appobject.h>
#include <libroutermanager/profile.h>
#include <libroutermanager/plugins.h>

/** Internal search path list */
static GSList *search_path_list = NULL;
/** Internal peas extension set */
static PeasExtensionSet *exten = NULL;
/** peas engine */
PeasEngine *engine = NULL;

/**
 * \brief Add additional search path for peas
 * \param path path to search for plugins for
 */
void routermanager_plugins_add_search_path(gchar *path)
{
	search_path_list = g_slist_append(search_path_list, g_strdup(path));
}

/**
 * \brief Add extension callback
 * \param set peas extension set
 * \param info peas plugin info
 * \param exten peas extension
 * \param unused unused data poiner
 */
static void plugins_extension_added_cb(PeasExtensionSet *set, PeasPluginInfo *info, PeasExtension *exten, gpointer unused)
{
	/* Active plugin now */
	g_debug(" + %s (%s) activated", peas_plugin_info_get_name(info), peas_plugin_info_get_module_name(info));
	peas_activatable_activate(PEAS_ACTIVATABLE(exten));
}

/**
 * \brief Remove extension callback
 * \param set peas extension set
 * \param info peas plugin info
 * \param exten peas extension
 * \param unused unused data poiner
 */
static void plugins_extension_removed_cb(PeasExtensionSet *set, PeasPluginInfo *info, PeasExtension *exten, gpointer unused)
{
	/* Remove plugin now */
	if (!peas_plugin_info_is_builtin(info)) {
		g_debug(" - %s (%s) deactivated", peas_plugin_info_get_name(info), peas_plugin_info_get_module_name(info));
	}
	peas_activatable_deactivate(PEAS_ACTIVATABLE(exten));
}

/**
 * \brief Find and load builtin plugins
 */
void plugins_init(void)
{
	GSList *slist;
	const GList *list;

	/* Get default engine */
	engine = peas_engine_get_default();

	/* Set app object as object to engine */
	exten = peas_extension_set_new(engine, PEAS_TYPE_ACTIVATABLE, "object", app_object, NULL);

	/* Connect extension added/removed signals */
	g_signal_connect(exten, "extension-added", G_CALLBACK(plugins_extension_added_cb), NULL);
	g_signal_connect(exten, "extension-removed", G_CALLBACK(plugins_extension_removed_cb), NULL);

	/* Look for plugins in plugin_dir */
	peas_engine_add_search_path(engine, ROUTERMANAGER_PLUGINS, ROUTERMANAGER_PLUGINS);

	/* And all other directories */
	for (slist = search_path_list; slist != NULL; slist = slist->next) {
		gchar *plugin_dir = slist->data;

		peas_engine_add_search_path(engine, plugin_dir, plugin_dir);
	}

	/* In addition to C we want to support python3 plugins */
	peas_engine_enable_loader(engine, "python3");

	/* Traverse through detected plugins and loaded builtin plugins now */
	for (list = peas_engine_get_plugin_list(engine); list != NULL; list = list->next) {
		PeasPluginInfo *info = list->data;

		if (peas_plugin_info_is_builtin(info)) {
			peas_engine_load_plugin(engine, info);
		}
	}
}

/**
 * \brief Add plugins information to profile settings
 */
void plugins_user_plugins(void)
{
	g_settings_bind(profile_get_active()->settings, "active-plugins", engine, "loaded-plugins", G_SETTINGS_BIND_DEFAULT);
}

/**
 * \brief Shutdown plugins
 */
void plugins_shutdown(void)
{
	if (!exten) {
		return;
	}

	g_signal_handlers_disconnect_by_func(exten, G_CALLBACK(plugins_extension_added_cb), NULL);
	g_signal_handlers_disconnect_by_func(exten, G_CALLBACK(plugins_extension_removed_cb), NULL);

	peas_extension_set_foreach(exten, plugins_extension_removed_cb, NULL);

	g_clear_object(&exten);
	g_clear_object(&engine);
}