~ubuntu-branches/ubuntu/saucy/gnome-bluetooth/saucy-proposed

« back to all changes in this revision

Viewing changes to applet/agent.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-07-03 16:07:57 UTC
  • mfrom: (1.1.37) (2.1.21 sid)
  • Revision ID: package-import@ubuntu.com-20130703160757-26mu1hl6drmsl0lu
Tags: 3.8.1-2ubuntu1
* Sync with Debian. Remaining changes:
  - debian/control.in:
    + Don't depend on consolekit
    + Recommend gnome-user-share, it's used for file transfers
  - debian/rules:
    + don't build with -Wl,-Bsymbolic-functions that creates issues when
      the --as-needed option is used
    + use dh-autoreconf
  - debian/gnome-bluetooth.maintscript:
    + Remove obsolete indicator autostart file too
  - debian/patches/menu_update_on_rfkill.patch:
    + make sure the menu gets updated properly when the killswitch state is
      changed. Specifically, notify for the applet to show the full menu,
      so that it can be displayed again when coming back from BLOCKED state.
      This fixes an issue specific to Dell systems where the devices are
      removed then re-added when the rfkill state is toggled.
* debian/patches/revert-drop-sendto-plugin.patch:
  - Continue to build nautilus-sendto plugin for now
* debian/control.in:
  - Build-Depend on dh-autoreconf and nautilus-sendto

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  BlueZ - Bluetooth protocol stack for Linux
4
 
 *
5
 
 *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
6
 
 *  Copyright (C) 2006-2007  Bastien Nocera <hadess@hadess.net>
7
 
 *
8
 
 *
9
 
 *  This program is free software; you can redistribute it and/or modify
10
 
 *  it under the terms of the GNU General Public License as published by
11
 
 *  the Free Software Foundation; either version 2 of the License, or
12
 
 *  (at your option) any later version.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program; if not, write to the Free Software
21
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 
 *
23
 
 */
24
 
 
25
 
#ifdef HAVE_CONFIG_H
26
 
#include <config.h>
27
 
#endif
28
 
 
29
 
#include <stdlib.h>
30
 
 
31
 
#include <glib/gi18n.h>
32
 
#include <gtk/gtk.h>
33
 
 
34
 
#include "bluetooth-applet.h"
35
 
#include "notify.h"
36
 
#include "agent.h"
37
 
 
38
 
static GList *input_list = NULL;
39
 
 
40
 
typedef struct input_data input_data;
41
 
struct input_data {
42
 
        BluetoothApplet *applet;
43
 
        char *path;
44
 
        char *uuid;
45
 
        gboolean numeric;
46
 
        GtkWidget *dialog;
47
 
        GtkWidget *button;
48
 
        GtkWidget *entry;
49
 
};
50
 
 
51
 
static void input_free(input_data *input)
52
 
{
53
 
        gtk_widget_destroy(input->dialog);
54
 
 
55
 
        g_object_unref (G_OBJECT (input->applet));
56
 
        g_free(input->uuid);
57
 
        g_free(input->path);
58
 
 
59
 
        input_list = g_list_remove(input_list, input);
60
 
 
61
 
        g_free(input);
62
 
 
63
 
}
64
 
 
65
 
static void pin_callback(GtkWidget *dialog,
66
 
                                gint response, gpointer user_data)
67
 
{
68
 
        input_data *input = user_data;
69
 
 
70
 
        if (response == GTK_RESPONSE_OK) {
71
 
                const char *text;
72
 
 
73
 
                text = gtk_entry_get_text(GTK_ENTRY(input->entry));
74
 
 
75
 
                if (input->numeric == TRUE) {
76
 
                        guint pin = atoi(text);
77
 
                        bluetooth_applet_agent_reply_passkey (input->applet, input->path, pin);
78
 
                } else
79
 
                        bluetooth_applet_agent_reply_pincode (input->applet, input->path, text);
80
 
        } else {
81
 
                if (input->numeric == TRUE)
82
 
                        bluetooth_applet_agent_reply_passkey (input->applet, input->path, -1);
83
 
                else
84
 
                        bluetooth_applet_agent_reply_pincode (input->applet, input->path, NULL);
85
 
        }
86
 
 
87
 
        input_free(input);
88
 
}
89
 
 
90
 
static void
91
 
confirm_callback (GtkWidget *dialog,
92
 
                  gint response,
93
 
                  gpointer user_data)
94
 
{
95
 
        input_data *input = user_data;
96
 
 
97
 
        bluetooth_applet_agent_reply_confirm (input->applet, input->path, response == GTK_RESPONSE_ACCEPT);
98
 
 
99
 
        input_free(input);
100
 
}
101
 
 
102
 
static void
103
 
auth_callback (GtkWidget *dialog,
104
 
               gint response,
105
 
               gpointer user_data)
106
 
{
107
 
        input_data *input = user_data;
108
 
 
109
 
        if (response == GTK_RESPONSE_ACCEPT) {
110
 
                gboolean trusted = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (input->button));
111
 
                bluetooth_applet_agent_reply_auth (input->applet, input->path, TRUE, trusted);
112
 
        } else
113
 
                bluetooth_applet_agent_reply_auth (input->applet, input->path, FALSE, FALSE);
114
 
 
115
 
        input_free(input);
116
 
}
117
 
 
118
 
static void insert_callback(GtkEditable *editable, const gchar *text,
119
 
                        gint length, gint *position, gpointer user_data)
120
 
{
121
 
        input_data *input = user_data;
122
 
        gint i;
123
 
 
124
 
        if (input->numeric == FALSE)
125
 
                return;
126
 
 
127
 
        for (i = 0; i < length; i++) {
128
 
                if (g_ascii_isdigit(text[i]) == FALSE) {
129
 
                        g_signal_stop_emission_by_name(editable,
130
 
                                                        "insert-text");
131
 
                }
132
 
        }
133
 
}
134
 
 
135
 
static void changed_callback(GtkWidget *editable, gpointer user_data)
136
 
{
137
 
        input_data *input = user_data;
138
 
        const gchar *text;
139
 
 
140
 
        text = gtk_entry_get_text(GTK_ENTRY(input->entry));
141
 
 
142
 
        gtk_dialog_set_response_sensitive (GTK_DIALOG (input->dialog),
143
 
                                           GTK_RESPONSE_OK,
144
 
                                           *text != '\0' ? TRUE : FALSE);
145
 
}
146
 
 
147
 
static void toggled_callback(GtkWidget *button, gpointer user_data)
148
 
{
149
 
        input_data *input = user_data;
150
 
        gboolean mode;
151
 
 
152
 
        mode = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
153
 
 
154
 
        gtk_entry_set_visibility (GTK_ENTRY (input->entry), mode);
155
 
}
156
 
 
157
 
static void
158
 
pin_dialog (BluetoothApplet *applet,
159
 
            const char *path,
160
 
            const char *name,
161
 
            const char *long_name,
162
 
            gboolean numeric)
163
 
{
164
 
        GtkWidget *dialog;
165
 
        GtkWidget *button;
166
 
        GtkWidget *entry;
167
 
        GtkBuilder *xml;
168
 
        char *str;
169
 
        input_data *input;
170
 
 
171
 
        xml = gtk_builder_new ();
172
 
        if (gtk_builder_add_from_file (xml, "passkey-dialogue.ui", NULL) == 0)
173
 
                gtk_builder_add_from_file (xml, PKGDATADIR "/passkey-dialogue.ui", NULL);
174
 
 
175
 
        input = g_new0 (input_data, 1);
176
 
        input->applet = g_object_ref (G_OBJECT (applet));
177
 
        input->path = g_strdup (path);
178
 
        input->numeric = numeric;
179
 
 
180
 
        dialog = GTK_WIDGET (gtk_builder_get_object (xml, "dialog"));
181
 
 
182
 
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
183
 
        if (notification_supports_actions () != FALSE)
184
 
                gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);
185
 
        else
186
 
                gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE);
187
 
        gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE);
188
 
        input->dialog = dialog;
189
 
 
190
 
        gtk_dialog_set_default_response (GTK_DIALOG (dialog),
191
 
                                         GTK_RESPONSE_OK);
192
 
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
193
 
                                           GTK_RESPONSE_OK,
194
 
                                           FALSE);
195
 
 
196
 
        str = g_strdup_printf (_("Device '%s' wants to pair with this computer"),
197
 
                               name);
198
 
        g_object_set (G_OBJECT (dialog), "text", str, NULL);
199
 
        g_free (str);
200
 
 
201
 
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
202
 
                                                  _("Please enter the PIN mentioned on device %s."),
203
 
                                                  long_name);
204
 
 
205
 
        entry = GTK_WIDGET (gtk_builder_get_object (xml, "entry"));
206
 
        if (numeric == TRUE) {
207
 
                gtk_entry_set_max_length (GTK_ENTRY (entry), 6);
208
 
                gtk_entry_set_width_chars (GTK_ENTRY (entry), 6);
209
 
                g_signal_connect (G_OBJECT (entry), "insert-text",
210
 
                                  G_CALLBACK (insert_callback), input);
211
 
        } else {
212
 
                gtk_entry_set_max_length (GTK_ENTRY (entry), 16);
213
 
                gtk_entry_set_width_chars (GTK_ENTRY (entry), 16);
214
 
                gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
215
 
        }
216
 
        gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
217
 
        input->entry = entry;
218
 
        g_signal_connect (G_OBJECT (entry), "changed",
219
 
                          G_CALLBACK (changed_callback), input);
220
 
 
221
 
        button = GTK_WIDGET (gtk_builder_get_object (xml, "showinput_button"));
222
 
        if (numeric == FALSE) {
223
 
                g_signal_connect (G_OBJECT (button), "toggled",
224
 
                                  G_CALLBACK (toggled_callback), input);
225
 
        } else {
226
 
                gtk_widget_set_no_show_all (button, TRUE);
227
 
                gtk_widget_hide (button);
228
 
        }
229
 
 
230
 
        input_list = g_list_append(input_list, input);
231
 
 
232
 
        g_signal_connect (G_OBJECT (dialog), "response",
233
 
                          G_CALLBACK (pin_callback), input);
234
 
}
235
 
 
236
 
static void
237
 
confirm_dialog (BluetoothApplet *applet,
238
 
                const char *path,
239
 
                const char *name,
240
 
                const char *long_name,
241
 
                const char *value)
242
 
{
243
 
        GtkWidget *dialog;
244
 
        GtkBuilder *xml;
245
 
        char *str;
246
 
        input_data *input;
247
 
 
248
 
        input = g_new0 (input_data, 1);
249
 
        input->applet = g_object_ref (G_OBJECT (applet));
250
 
        input->path = g_strdup (path);
251
 
 
252
 
        xml = gtk_builder_new ();
253
 
        if (gtk_builder_add_from_file (xml, "confirm-dialogue.ui", NULL) == 0)
254
 
                gtk_builder_add_from_file (xml, PKGDATADIR "/confirm-dialogue.ui", NULL);
255
 
 
256
 
        dialog = GTK_WIDGET (gtk_builder_get_object (xml, "dialog"));
257
 
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
258
 
        if (notification_supports_actions () != FALSE)
259
 
                gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);
260
 
        else
261
 
                gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE);
262
 
        gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE);
263
 
        input->dialog = dialog;
264
 
 
265
 
        str = g_strdup_printf (_("Device '%s' wants to pair with this computer"),
266
 
                               name);
267
 
        g_object_set (G_OBJECT (dialog), "text", str, NULL);
268
 
        g_free (str);
269
 
 
270
 
        str = g_strdup_printf ("<b>%s</b>", value);
271
 
        gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
272
 
                                                    _("Please confirm whether the PIN '%s' matches the one on device %s."),
273
 
                                                    str, long_name);
274
 
        g_free (str);
275
 
 
276
 
        input_list = g_list_append (input_list, input);
277
 
 
278
 
        g_signal_connect (G_OBJECT (dialog), "response",
279
 
                          G_CALLBACK (confirm_callback), input);
280
 
}
281
 
 
282
 
static void
283
 
auth_dialog (BluetoothApplet *applet,
284
 
             const char *path,
285
 
             const char *name,
286
 
             const char *long_name,
287
 
             const char *uuid)
288
 
{
289
 
        GtkBuilder *xml;
290
 
        GtkWidget *dialog;
291
 
        GtkWidget *button;
292
 
        char *str;
293
 
        input_data *input;
294
 
 
295
 
        input = g_new0 (input_data, 1);
296
 
        input->applet = g_object_ref (G_OBJECT (applet));
297
 
        input->path = g_strdup (path);
298
 
        input->uuid = g_strdup (uuid);
299
 
 
300
 
        xml = gtk_builder_new ();
301
 
        if (gtk_builder_add_from_file (xml, "authorisation-dialogue.ui", NULL) == 0)
302
 
                gtk_builder_add_from_file (xml, PKGDATADIR "/authorisation-dialogue.ui", NULL);
303
 
 
304
 
        dialog = GTK_WIDGET (gtk_builder_get_object (xml, "dialog"));
305
 
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
306
 
        if (notification_supports_actions () != FALSE)
307
 
                gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);
308
 
        else
309
 
                gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE);
310
 
        gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE);
311
 
        input->dialog = dialog;
312
 
 
313
 
        /* translators: Whether to grant access to a particular service */
314
 
        str = g_strdup_printf (_("Grant access to '%s'"), uuid);
315
 
        g_object_set (G_OBJECT (dialog), "text", str, NULL);
316
 
        g_free (str);
317
 
 
318
 
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
319
 
                                                  _("Device %s wants access to the service '%s'."),
320
 
                                                  long_name, uuid);
321
 
 
322
 
        button = GTK_WIDGET (gtk_builder_get_object (xml, "always_button"));
323
 
        input->button = button;
324
 
 
325
 
        input_list = g_list_append (input_list, input);
326
 
 
327
 
        g_signal_connect (G_OBJECT (dialog), "response",
328
 
                          G_CALLBACK (auth_callback), input);
329
 
}
330
 
 
331
 
static void show_dialog(gpointer data, gpointer user_data)
332
 
{
333
 
        input_data *input = data;
334
 
 
335
 
        gtk_widget_show_all(input->dialog);
336
 
 
337
 
        gtk_window_present(GTK_WINDOW(input->dialog));
338
 
}
339
 
 
340
 
static void present_notification_dialogs (void)
341
 
{
342
 
        g_list_foreach(input_list, show_dialog, NULL);
343
 
}
344
 
 
345
 
static void notification_closed(GObject *object, gpointer user_data)
346
 
{
347
 
        present_notification_dialogs ();
348
 
}
349
 
 
350
 
static gboolean
351
 
pin_request(BluetoothApplet *applet,
352
 
            char *path,
353
 
            char *name,
354
 
            char *long_name,
355
 
            gboolean numeric,
356
 
            gpointer user_data)
357
 
{
358
 
        char *line;
359
 
 
360
 
        pin_dialog(applet, path, name, long_name, numeric);
361
 
 
362
 
        if (notification_supports_actions () == FALSE) {
363
 
                present_notification_dialogs ();
364
 
                return TRUE;
365
 
        }
366
 
 
367
 
        /* translators: this is a popup telling you a particular device
368
 
         * has asked for pairing */
369
 
        line = g_strdup_printf(_("Pairing request for '%s'"), name);
370
 
 
371
 
        show_notification(_("Bluetooth device"),
372
 
                          line, _("Enter PIN"), 0,
373
 
                          G_CALLBACK(notification_closed));
374
 
 
375
 
        g_free(line);
376
 
 
377
 
        return TRUE;
378
 
}
379
 
 
380
 
static gboolean
381
 
confirm_request (BluetoothApplet *applet,
382
 
                 char *path,
383
 
                 char *name,
384
 
                 char *long_name,
385
 
                 guint pin,
386
 
                 gpointer user_data)
387
 
{
388
 
        char *text, *line;
389
 
 
390
 
        text = g_strdup_printf("%06d", pin);
391
 
        confirm_dialog(applet, path, name, long_name, text);
392
 
        g_free(text);
393
 
 
394
 
        /* translators: this is a popup telling you a particular device
395
 
         * has asked for pairing */
396
 
        line = g_strdup_printf(_("Pairing confirmation for '%s'"), name);
397
 
 
398
 
        /* translators:
399
 
         * This message is for Bluetooth 2.1 support, when the
400
 
         * action is clicked in the notification popup, the user
401
 
         * will get to check whether the PIN matches the one
402
 
         * showing on the Bluetooth device */
403
 
        if (notification_supports_actions () != FALSE)
404
 
                show_notification(_("Bluetooth device"),
405
 
                                    line, _("Verify PIN"), 0,
406
 
                                    G_CALLBACK(notification_closed));
407
 
        else
408
 
                present_notification_dialogs ();
409
 
 
410
 
        g_free(line);
411
 
 
412
 
        return TRUE;
413
 
}
414
 
 
415
 
static gboolean
416
 
authorize_request (BluetoothApplet *applet,
417
 
                   char *path,
418
 
                   char *name,
419
 
                   char *long_name,
420
 
                   char *uuid,
421
 
                   gpointer user_data)
422
 
{
423
 
        char *line;
424
 
 
425
 
        auth_dialog (applet, path, name, long_name, uuid);
426
 
 
427
 
        if (notification_supports_actions () == FALSE) {
428
 
                present_notification_dialogs ();
429
 
                return TRUE;
430
 
        }
431
 
 
432
 
        line = g_strdup_printf (_("Authorization request from '%s'"), name);
433
 
 
434
 
        show_notification (_("Bluetooth device"),
435
 
                           line, _("Check authorization"), 0,
436
 
                           G_CALLBACK (notification_closed));
437
 
 
438
 
        g_free (line);
439
 
 
440
 
        return TRUE;
441
 
}
442
 
 
443
 
static void input_free_list (gpointer data, gpointer user_data) {
444
 
        input_data *input = data;
445
 
 
446
 
        gtk_widget_destroy(input->dialog);
447
 
 
448
 
        g_object_unref (G_OBJECT (input->applet));
449
 
        g_free(input->uuid);
450
 
        g_free(input->path);
451
 
        g_free(input);
452
 
}
453
 
 
454
 
static void cancel_request(BluetoothApplet *applet,
455
 
                                                        gpointer user_data)
456
 
{
457
 
        g_list_foreach (input_list, input_free_list, NULL);
458
 
        g_list_free (input_list);
459
 
        input_list = NULL;
460
 
}
461
 
 
462
 
int setup_agents(BluetoothApplet *applet)
463
 
{
464
 
        g_signal_connect (applet, "pincode-request",
465
 
                        G_CALLBACK (pin_request), NULL);
466
 
        g_signal_connect (applet, "confirm-request",
467
 
                        G_CALLBACK (confirm_request), NULL);
468
 
        g_signal_connect (applet, "auth-request",
469
 
                        G_CALLBACK (authorize_request), NULL);
470
 
        g_signal_connect (applet, "cancel-request",
471
 
                        G_CALLBACK (cancel_request), NULL);
472
 
 
473
 
        return 0;
474
 
}
475
 
 
476
 
void show_agents(void)
477
 
{
478
 
        close_notification();
479
 
 
480
 
        g_list_foreach(input_list, show_dialog, NULL);
481
 
}