summaryrefslogtreecommitdiff
path: root/lib/bluetooth-chooser-button.c
blob: 1648f9edd73872ba9cb538cd8b7fb321eb810b6c (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * (C) Copyright 2007-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 Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:bluetooth-chooser-button
 * @short_description: a Bluetooth chooser button
 * @stability: Stable
 * @include: bluetooth-chooser-button.h
 *
 * A button used to select Bluetooth devices which will pop-up a
 * #BluetoothChooser widget inside a dialogue when clicked.
 **/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib/gi18n.h>
#include <gtk/gtk.h>

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

struct _BluetoothChooserButton {
	GtkButton          parent;

	BluetoothClient   *client;
	GtkWidget         *image;
	GtkWidget         *dialog;
	GtkWidget         *chooser;
	char              *bdaddr;
	guint              is_available : 1;
	guint              has_selection : 1;
};

enum {
	PROP_0,
	PROP_DEVICE,
	PROP_IS_AVAILABLE,
};

enum {
	CHOOSER_CREATED,
	LAST_SIGNAL
};

static int signals[LAST_SIGNAL] = { 0 };

static void	bluetooth_chooser_button_class_init	(BluetoothChooserButtonClass * klass);
static void	bluetooth_chooser_button_init		(BluetoothChooserButton      * button);

static GtkButtonClass *parent_class;

G_DEFINE_TYPE(BluetoothChooserButton, bluetooth_chooser_button, GTK_TYPE_BUTTON);

#define DEFAULT_STR N_("Click to select device...")

static void
set_btdevname (BluetoothChooserButton *button, const char *bdaddr, const char *name, const char *icon)
{
	char *found_name, *found_icon;

	found_name = NULL;
	found_icon = NULL;

	if (bdaddr != NULL && (name == NULL || icon == NULL)) {
		GtkTreeModel *model;
		GtkTreeIter iter;
		gboolean cont = FALSE;

		model = bluetooth_client_get_device_model (button->client);
		if (model != NULL) {
			cont = gtk_tree_model_iter_children (GTK_TREE_MODEL(model),
							     &iter, NULL);
		}

		while (cont == TRUE) {
			char *value;

			gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
					   BLUETOOTH_COLUMN_ADDRESS, &value, -1);
			if (g_ascii_strcasecmp(bdaddr, value) == 0) {
				gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
						   BLUETOOTH_COLUMN_ALIAS, &found_name,
						   BLUETOOTH_COLUMN_ICON, &found_icon,
						   -1);
				g_free (value);
				break;
			}
			g_free (value);
			cont = gtk_tree_model_iter_next (GTK_TREE_MODEL(model), &iter);
		}

		if (model != NULL)
			g_object_unref (model);

		if (found_name == NULL) {
			found_name = g_strdup (bdaddr);
			g_strdelimit (found_name, ":", '-');
		}
		if (found_icon == NULL)
			found_icon = g_strdup ("bluetooth");
	}

	if (bdaddr != NULL) {
		/* Update the name */
		if (name == NULL)
			gtk_button_set_label (GTK_BUTTON (button), found_name);
		else
			gtk_button_set_label (GTK_BUTTON (button), name);
		/* And the icon */
		if (icon == NULL)
			gtk_image_set_from_icon_name (GTK_IMAGE (button->image), found_icon, GTK_ICON_SIZE_MENU);
		else
			gtk_image_set_from_icon_name (GTK_IMAGE (button->image), icon, GTK_ICON_SIZE_MENU);

		/* And our copy of the address, and notify if it's actually changed */
		if (button->bdaddr == NULL || strcmp (bdaddr, button->bdaddr) != 0) {
			g_free (button->bdaddr);
			button->bdaddr = g_strdup (bdaddr);
			g_object_notify (G_OBJECT (button), "device");
		}
	} else {
		gtk_button_set_label (GTK_BUTTON (button), _(DEFAULT_STR));
		if (button->bdaddr != NULL) {
			g_free (button->bdaddr);
			button->bdaddr = NULL;
			gtk_image_clear (GTK_IMAGE (button->image));
			g_object_notify (G_OBJECT (button), "device");
		}
	}

	g_free (found_name);
	g_free (found_icon);
}

static void select_device_changed(BluetoothChooser *self, gchar *address, gpointer data)
{
	BluetoothChooserButton *button = BLUETOOTH_CHOOSER_BUTTON (data);

	button->has_selection = (address != NULL);
	gtk_dialog_set_response_sensitive(GTK_DIALOG (button->dialog), GTK_RESPONSE_ACCEPT,
					  button->has_selection && button->is_available);
}

static void
dialog_response_cb (GtkDialog *dialog, int response_id, gpointer data)
{
	BluetoothChooserButton *button = BLUETOOTH_CHOOSER_BUTTON (data);
	char *bdaddr, *icon, *name;

	if (response_id == GTK_RESPONSE_ACCEPT) {
		BluetoothChooser *chooser = BLUETOOTH_CHOOSER (button->chooser);
		bdaddr = bluetooth_chooser_get_selected_device (chooser);
		name = bluetooth_chooser_get_selected_device_name (chooser);
		icon = bluetooth_chooser_get_selected_device_icon (chooser);
	}

	gtk_widget_destroy (GTK_WIDGET (dialog));
	button->dialog = NULL;

	if (response_id != GTK_RESPONSE_ACCEPT)
		return;

	set_btdevname (button, bdaddr, name, icon);
	g_free (bdaddr);
	g_free (name);
	g_free (icon);
}

static void
bluetooth_chooser_button_clicked (GtkButton *widget)
{
	BluetoothChooserButton *button = BLUETOOTH_CHOOSER_BUTTON (widget);
	GtkWidget *parent;

	if (button->dialog != NULL) {
		gtk_window_present (GTK_WINDOW (button->dialog));
		return;
	}

	parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
	//FIXME title
	button->dialog = gtk_dialog_new_with_buttons("", GTK_WINDOW (parent),
						     GTK_DIALOG_MODAL,
						     GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
						     GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
	g_signal_connect (button->dialog, "response",
			  G_CALLBACK (dialog_response_cb), button);
	gtk_dialog_set_response_sensitive (GTK_DIALOG(button->dialog),
					   GTK_RESPONSE_ACCEPT, FALSE);
	gtk_window_set_default_size (GTK_WINDOW(button->dialog), 480, 400);

	gtk_container_set_border_width (GTK_CONTAINER (button->dialog), 5);
	gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (button->dialog))), 2);

	/* Create the button->chooser */
	button->chooser = bluetooth_chooser_new ();
	g_signal_connect(button->chooser, "selected-device-changed",
			 G_CALLBACK(select_device_changed), button);
	g_signal_emit (G_OBJECT (button),
		       signals[CHOOSER_CREATED],
		       0, button->chooser);
	g_object_set (G_OBJECT (button->chooser), "device-selected", button->bdaddr, NULL);
	gtk_container_set_border_width (GTK_CONTAINER(button->chooser), 5);
	gtk_widget_show (button->chooser);
	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (button->dialog))), button->chooser,
			    TRUE, TRUE, 0);

	gtk_widget_show (button->dialog);
}

static void
default_adapter_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
	BluetoothChooserButton *button = BLUETOOTH_CHOOSER_BUTTON (data);
	char *adapter;
	gboolean powered;

	g_object_get (G_OBJECT (button->client),
		      "default-adapter", &adapter,
		      "default-adapter-powered", &powered,
		      NULL);
	if (adapter != NULL)
		button->is_available = powered;
	else
		button->is_available = FALSE;

	if (adapter != NULL && button->bdaddr != NULL)
		set_btdevname (button, button->bdaddr, NULL, NULL);
	g_free (adapter);

	if (button->dialog != NULL)
		gtk_dialog_set_response_sensitive (GTK_DIALOG (button->dialog), GTK_RESPONSE_ACCEPT,
						   button->has_selection && button->is_available);

	g_object_notify (G_OBJECT (button), "is-available");
}

static void
bluetooth_chooser_button_finalize (GObject *object)
{
	BluetoothChooserButton *button = BLUETOOTH_CHOOSER_BUTTON (object);

	if (button->client != NULL) {
		g_object_unref (button->client);
		button->client = NULL;
	}
	if (button->dialog != NULL) {
		gtk_widget_destroy (button->dialog);
		button->dialog = NULL;
		button->chooser = NULL;
	}

	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
bluetooth_chooser_button_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
	BluetoothChooserButton *button;

	g_return_if_fail (BLUETOOTH_IS_CHOOSER_BUTTON (object));
	button = BLUETOOTH_CHOOSER_BUTTON (object);

	switch (property_id) {
	case PROP_DEVICE: {
		const char *str = g_value_get_string (value);
		g_return_if_fail (str == NULL || bluetooth_verify_address (str));
		set_btdevname (button, str, NULL, NULL);
		break;
		}
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
bluetooth_chooser_button_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	BluetoothChooserButton *button;

	g_return_if_fail (BLUETOOTH_IS_CHOOSER_BUTTON (object));
	button = BLUETOOTH_CHOOSER_BUTTON (object);

	switch (property_id) {
	case PROP_DEVICE:
		g_value_set_string (value, button->bdaddr);
		break;
	case PROP_IS_AVAILABLE:
		g_value_set_boolean (value, button->is_available);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
bluetooth_chooser_button_class_init (BluetoothChooserButtonClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	object_class->finalize = bluetooth_chooser_button_finalize;
	object_class->set_property = bluetooth_chooser_button_set_property;
	object_class->get_property = bluetooth_chooser_button_get_property;

	button_class->clicked = bluetooth_chooser_button_clicked;

	/**
	 * BluetoothChooserButton::chooser-created:
	 * @self: a #BluetoothChooserButton widget
	 * @chooser: a #BluetoothChooser widget
	 *
	 * The signal is sent when a popup dialogue is created for the user to select
	 * a device. This signal allows you to change the configuration and filtering
	 * of the tree from its defaults.
	 **/
	signals[CHOOSER_CREATED] =
		g_signal_new ("chooser-created",
			      G_TYPE_FROM_CLASS (klass),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (BluetoothChooserButtonClass, chooser_created),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__OBJECT,
			      G_TYPE_NONE, 1, G_TYPE_OBJECT);

	/**
	 * BluetoothChooserButton:device:
	 *
	 * The Bluetooth address of the selected device or %NULL.
	 **/
	g_object_class_install_property (object_class, PROP_DEVICE,
					 g_param_spec_string ("device", "Device", "The Bluetooth address of the selected device.",
							      NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
	/**
	 * BluetoothChooserButton:is-available:
	 *
	 * %TRUE if there is a powered Bluetooth adapter available.
	 *
	 * See also: bluetooth_chooser_button_available()
	 **/
	g_object_class_install_property (object_class, PROP_IS_AVAILABLE,
					 g_param_spec_boolean ("is-available", "Bluetooth is available", "Whether Bluetooth is available.",
							       TRUE, G_PARAM_READABLE));
}

static void
bluetooth_chooser_button_init (BluetoothChooserButton *button)
{
	gtk_button_set_label (GTK_BUTTON (button), _(DEFAULT_STR));

	button->image = gtk_image_new ();
	gtk_button_set_image (GTK_BUTTON (button), button->image);

	button->bdaddr = NULL;
	button->dialog = NULL;

	button->client = bluetooth_client_new ();
	g_signal_connect (G_OBJECT (button->client), "notify::default-adapter",
			  G_CALLBACK (default_adapter_changed), button);
	g_signal_connect (G_OBJECT (button->client), "notify::default-adapter-powered",
			  G_CALLBACK (default_adapter_changed), button);

	/* And set the default value already */
	default_adapter_changed (NULL, NULL, button);
}

/**
 * bluetooth_chooser_button_new:
 *
 * Returns a new #BluetoothChooserButton widget.
 *
 * Return value: a #BluetoothChooserButton widget.
 **/
GtkWidget *
bluetooth_chooser_button_new (void)
{
	return g_object_new (BLUETOOTH_TYPE_CHOOSER_BUTTON,
			     NULL);
}

/**
 * bluetooth_chooser_button_available:
 * @button: a #BluetoothChooserButton
 *
 * Returns whether there is a powered Bluetooth adapter.
 *
 * Return value: %TRUE if there is a powered Bluetooth adapter available, and the button should be sensitive.
 **/
gboolean
bluetooth_chooser_button_available (BluetoothChooserButton *button)
{
	g_return_val_if_fail (BLUETOOTH_IS_CHOOSER_BUTTON (button), FALSE);

	return button->is_available;
}