summaryrefslogtreecommitdiff
path: root/lib/bluetooth-plugin-manager.c
blob: ae125f8ff671a4b364245bffb37df879fbbf6efd (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2009  Bastien Nocera <hadess@hadess.net>
 *
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/**
 * SECTION:bluetooth-plugin-manager
 * @short_description: Bluetooth plug-in manager
 * @stability: Stable
 * @include: bluetooth-plugin-manager.h
 *
 * The plugin manager is used to extend set up wizards, or preferences.
 **/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <bluetooth-plugin.h>
#include "bluetooth-plugin-manager.h"

#include "bluetooth-client.h"
#include "bluetooth-utils.h"

static GList *plugin_list = NULL;

static void
bluetooth_plugin_dir_process (const char *plugindir)
{
	GDir *dir;
	const char *item;
	GbtPlugin *p = NULL;
	GError *err = NULL;
	gboolean (*gbt_init_plugin)(GbtPlugin *p);

	dir = g_dir_open (plugindir, 0, &err);

	if (dir == NULL) {
		g_warning ("Can't open the plugins dir: %s", err ? err->message : "No reason");
		if (err)
			g_error_free (err);
	} else {
		while ((item = g_dir_read_name(dir))) {
			if (g_str_has_suffix (item, "." G_MODULE_SUFFIX)) {
				char *module_path;

				p = g_new0(GbtPlugin, 1); 
				module_path = g_module_build_path (plugindir, item);
				p->module = g_module_open (module_path, G_MODULE_BIND_LAZY);
				if (!p->module) {
					g_warning ("error opening %s: %s", module_path, g_module_error ());
					g_free (module_path);
					continue;
				}
				g_free (module_path);

				if (!g_module_symbol (p->module, "gbt_init_plugin", (gpointer *) &gbt_init_plugin)) {
					g_warning ("error: %s", g_module_error ());
					g_module_close (p->module);
					continue;
				}

				gbt_init_plugin (p);

				plugin_list = g_list_append (plugin_list, p);
			}
		}
		g_dir_close (dir);
	}
}

/**
 * bluetooth_plugin_manager_init:
 *
 * Initialise the plugin manager for Bluetooth plugins.
 *
 * Return value: %TRUE if the initialisation succedeed, %FALSE otherwise.
 **/
gboolean
bluetooth_plugin_manager_init (void)
{
	bluetooth_plugin_dir_process (PLUGINDIR);

	return g_list_length (plugin_list) != 0;
}

/**
 * bluetooth_plugin_manager_cleanup:
 *
 * Free all the resources used by the Bluetooth plugins.
 **/
void
bluetooth_plugin_manager_cleanup (void)
{
	GList *l;

	for (l = plugin_list; l != NULL; l = l->next) {
		GbtPlugin *p = l->data;

		/* Disabled as it causes crashes when plugins use
		 * the GObject type system */
		/* g_module_close (p->module); */
		g_free (p);
	}
	g_list_free (plugin_list);
	plugin_list = NULL;
}

/**
 * bluetooth_plugin_manager_get_widgets:
 * @bdaddr: a Bluetooth address representing a device
 * @uuids: an array of UUIDs supported by the device.
 *
 * Returns a list of widgets suitable for configuring the device
 * represented by @address, for the services listed in @uuids.
 *
 * Return value: a #GList of #GtkWidget, or %NULL is none.
 **/
GList *
bluetooth_plugin_manager_get_widgets (const char *bdaddr,
				      const char **uuids)
{
	GList *ret = NULL;
	GList *l;

	g_return_val_if_fail (bluetooth_verify_address (bdaddr), NULL);

	for (l = plugin_list; l != NULL; l = l->next) {
		GbtPlugin *p = l->data;

		if (p->info->has_config_widget (bdaddr, uuids))
			ret = g_list_prepend (ret, p->info->get_config_widgets (bdaddr, uuids));
	}

	return ret;
}

/**
 * bluetooth_plugin_manager_device_deleted:
 * @bdaddr: a Bluetooth address representing a device
 *
 * Removes all existing configuration for the device, as
 * supported by the plug-ins.
 **/
void
bluetooth_plugin_manager_device_deleted (const char *bdaddr)
{
	GList *l;

	g_return_if_fail (bluetooth_verify_address (bdaddr));

	for (l = plugin_list; l != NULL; l = l->next) {
		GbtPlugin *p = l->data;

		if (p->info->device_removed != NULL)
			p->info->device_removed (bdaddr);
	}
}