~timchen119/ubuntu/trusty/gnome-bluetooth/lp1035431

« back to all changes in this revision

Viewing changes to moblin/bluetooth-powerswitch.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2011-02-27 15:45:22 UTC
  • mfrom: (1.3.2 upstream)
  • mto: (2.2.3 experimental) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: james.westby@ubuntu.com-20110227154522-dnnoqasv5v3mv42a
Tags: upstream-2.91.5
ImportĀ upstreamĀ versionĀ 2.91.5

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-2009  Bastien Nocera <hadess@hadess.net>
 
7
 *  Copyright (C) 2010       Intel Corp
 
8
 *
 
9
 *
 
10
 *  This program is free software; you can redistribute it and/or modify
 
11
 *  it under the terms of the GNU General Public License as published by
 
12
 *  the Free Software Foundation; either version 2 of the License, or
 
13
 *  (at your option) any later version.
 
14
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU General Public License
 
21
 *  along with this program; if not, write to the Free Software
 
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
23
 *
 
24
 */
 
25
 
 
26
/**
 
27
 * SECTION:bluetooth-powerswitch
 
28
 * @short_description: a Bluetooth power switch object
 
29
 * @stability: Stable
 
30
 * @include: bluetooth-powerswitch.h
 
31
 *
 
32
 * An object to manipulate Bluetooth power switches.
 
33
 **/
 
34
 
 
35
#ifdef HAVE_CONFIG_H
 
36
#include <config.h>
 
37
#endif
 
38
 
 
39
#include <glib.h>
 
40
#include "bluetooth-client.h"
 
41
#include "bluetooth-powerswitch.h"
 
42
 
 
43
enum {
 
44
        STATE_CHANGED,
 
45
        LAST_SIGNAL
 
46
};
 
47
 
 
48
static int signals[LAST_SIGNAL] = { 0 };
 
49
 
 
50
#define BLUETOOTH_POWERSWITCH_GET_PRIVATE(obj)                          \
 
51
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitchPrivate))
 
52
 
 
53
struct _BluetoothPowerswitchPrivate {
 
54
        BluetoothClient *client;
 
55
        GtkTreeModel *adapters;
 
56
};
 
57
 
 
58
G_DEFINE_TYPE (BluetoothPowerswitch, bluetooth_powerswitch, G_TYPE_OBJECT)
 
59
 
 
60
static gboolean
 
61
set_state_foreach (GtkTreeModel *model,
 
62
                   GtkTreePath  *path,
 
63
                   GtkTreeIter  *iter,
 
64
                   gpointer      data)
 
65
{
 
66
        DBusGProxy *proxy = NULL;
 
67
        GValue value = { 0, };
 
68
        PowerswitchState state = GPOINTER_TO_INT (data);
 
69
 
 
70
        gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_PROXY, &proxy, -1);
 
71
        if (proxy == NULL)
 
72
                return FALSE;
 
73
 
 
74
        g_value_init (&value, G_TYPE_BOOLEAN);
 
75
        g_value_set_boolean (&value, state);
 
76
 
 
77
        /* TODO: do this async to avoid blocking. Maybe _no_reply will do? */
 
78
        dbus_g_proxy_call (proxy, "SetProperty", NULL,
 
79
                           G_TYPE_STRING, "Powered",
 
80
                           G_TYPE_VALUE, &value,
 
81
                           G_TYPE_INVALID,
 
82
                           G_TYPE_INVALID);
 
83
 
 
84
        g_value_unset (&value);
 
85
        g_object_unref (proxy);
 
86
 
 
87
        return FALSE;
 
88
}
 
89
 
 
90
void
 
91
bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch,
 
92
                                PowerswitchState state)
 
93
{
 
94
        BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
 
95
 
 
96
        g_return_if_fail (state == POWERSWITCH_STATE_OFF ||
 
97
                          state == POWERSWITCH_STATE_ON);
 
98
 
 
99
        gtk_tree_model_foreach (priv->adapters, set_state_foreach, GINT_TO_POINTER (state));
 
100
}
 
101
 
 
102
static gboolean
 
103
get_state_foreach (GtkTreeModel *model,
 
104
                   GtkTreePath  *path,
 
105
                   GtkTreeIter  *iter,
 
106
                   gpointer      data)
 
107
{
 
108
        gboolean powered = FALSE;
 
109
        gboolean *global_powered = data;
 
110
 
 
111
        gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_POWERED, &powered, -1);
 
112
 
 
113
        if (powered) {
 
114
                /* Found a powered adaptor, so we are "on" */
 
115
                *global_powered = TRUE;
 
116
                return TRUE;
 
117
        } else {
 
118
                /* Found an unpowered adaptor, set "off" but continue looking */
 
119
                *global_powered = FALSE;
 
120
                return FALSE;
 
121
        }
 
122
}
 
123
 
 
124
PowerswitchState
 
125
bluetooth_powerswitch_get_state (BluetoothPowerswitch *powerswitch)
 
126
{
 
127
        BluetoothPowerswitchPrivate *priv;
 
128
        gboolean powered = FALSE;
 
129
 
 
130
        g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), POWERSWITCH_STATE_NO_ADAPTER);
 
131
 
 
132
        priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
 
133
 
 
134
        if (bluetooth_powerswitch_has_powerswitches (powerswitch)) {
 
135
                gtk_tree_model_foreach (priv->adapters, get_state_foreach, &powered);
 
136
                return powered;
 
137
        } else {
 
138
                return POWERSWITCH_STATE_NO_ADAPTER;
 
139
        }
 
140
}
 
141
 
 
142
gboolean
 
143
bluetooth_powerswitch_has_powerswitches (BluetoothPowerswitch *powerswitch)
 
144
{
 
145
        BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
 
146
 
 
147
        g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), FALSE);
 
148
 
 
149
        return gtk_tree_model_iter_n_children (priv->adapters, NULL);
 
150
}
 
151
 
 
152
static void
 
153
powered_notify_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
 
154
{
 
155
        BluetoothPowerswitch *powerswitch = BLUETOOTH_POWERSWITCH (user_data);
 
156
 
 
157
        g_signal_emit (G_OBJECT (powerswitch),
 
158
                       signals[STATE_CHANGED],
 
159
                       0, bluetooth_powerswitch_get_state (powerswitch));
 
160
}
 
161
 
 
162
static void
 
163
bluetooth_powerswitch_init (BluetoothPowerswitch *powerswitch)
 
164
{
 
165
        BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
 
166
 
 
167
        powerswitch->priv = priv;
 
168
 
 
169
        priv->client = bluetooth_client_new ();
 
170
        priv->adapters = bluetooth_client_get_adapter_model(priv->client);
 
171
 
 
172
        g_signal_connect (priv->client, "notify::default-adapter-powered",
 
173
                          G_CALLBACK (powered_notify_cb), powerswitch);
 
174
 
 
175
        g_signal_emit (G_OBJECT (powerswitch),
 
176
                       signals[STATE_CHANGED],
 
177
                       0, bluetooth_powerswitch_get_state (powerswitch));
 
178
}
 
179
 
 
180
static void
 
181
bluetooth_powerswitch_dispose (GObject *object)
 
182
{
 
183
        BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (object);
 
184
 
 
185
        if (priv->client) {
 
186
                g_object_unref (priv->client);
 
187
                priv->client = NULL;
 
188
        }
 
189
 
 
190
        G_OBJECT_CLASS(bluetooth_powerswitch_parent_class)->dispose(object);
 
191
}
 
192
 
 
193
static void
 
194
bluetooth_powerswitch_class_init(BluetoothPowerswitchClass *klass)
 
195
{
 
196
        GObjectClass *object_class = (GObjectClass *) klass;
 
197
 
 
198
        g_type_class_add_private(klass, sizeof(BluetoothPowerswitchPrivate));
 
199
        object_class->dispose = bluetooth_powerswitch_dispose;
 
200
 
 
201
        signals[STATE_CHANGED] =
 
202
                g_signal_new ("state-changed",
 
203
                              G_TYPE_FROM_CLASS (klass),
 
204
                              G_SIGNAL_RUN_LAST,
 
205
                              G_STRUCT_OFFSET (BluetoothPowerswitchClass, state_changed),
 
206
                              NULL, NULL,
 
207
                              g_cclosure_marshal_VOID__INT,
 
208
                              G_TYPE_NONE, 1, G_TYPE_INT);
 
209
 
 
210
}
 
211
 
 
212
BluetoothPowerswitch *
 
213
bluetooth_powerswitch_new (void)
 
214
{
 
215
        return BLUETOOTH_POWERSWITCH(g_object_new (BLUETOOTH_TYPE_POWERSWITCH, NULL));
 
216
}