No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | tracker-network-provider-network-manager.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 * Copyright (C) 2010, Adrien Bustany <abustany@gnome.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21
22 #include <glib-object.h>
23
24 #include <libnm-glib/nm-client.h>
25
26 #ifndef NM_CHECK_VERSION
27 #define NM_CHECK_VERSION(x,y,z) (0)
28 #endif
29
30 #include <libnm-glib/nm-device-ethernet.h>
31 #include <libnm-glib/nm-device-wifi.h>
32 #if (NM_CHECK_VERSION (0,8,992))
33 #include <libnm-glib/nm-device-modem.h>
34 #include <libnm-glib/nm-device-wimax.h>
35 #else
36 #include <libnm-glib/nm-gsm-device.h>
37 #include <libnm-glib/nm-cdma-device.h>
38 #endif
39
40 #include "tracker-network-provider.h"
41
42 #define TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER (tracker_network_provider_network_manager_get_type())
43 #define TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER, TrackerNetworkProviderNetworkManager))
44 #define TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER, TrackerNetworkProviderNetworkManagerClass))
45 #define TRACKER_IS_NETWORK_PROVIDER_NETWORK_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER))
46 #define TRACKER_IS_NETWORK_PROVIDER_NETWORK_MANAGER_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER))
47 #define TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER, TrackerNetworkProviderNetworkManagerClass))
48
49 #define TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER, TrackerNetworkProviderNetworkManagerPrivate))
50
51 #define NETWORK_PROVIDER_NETWORK_MANAGER_NAME "NetworkManager"
52
53 typedef struct TrackerNetworkProviderNetworkManager TrackerNetworkProviderNetworkManager;
54 typedef struct TrackerNetworkProviderNetworkManagerClass TrackerNetworkProviderNetworkManagerClass;
55 typedef struct TrackerNetworkProviderNetworkManagerPrivate TrackerNetworkProviderNetworkManagerPrivate;
56
57 struct TrackerNetworkProviderNetworkManager {
58 GObject parent;
59 };
60
61 struct TrackerNetworkProviderNetworkManagerClass {
62 GObjectClass parent_class;
63 };
64
65 struct TrackerNetworkProviderNetworkManagerPrivate {
66 gchar *name;
67
68 NMClient *client;
69 TrackerNetworkProviderStatus last_emitted_status;
70 };
71
72 GType tracker_network_provider_network_manager_get_type (void) G_GNUC_CONST;
73 static void tracker_network_provider_iface_init (TrackerNetworkProviderIface *iface);
74 static void network_provider_finalize (GObject *object);
75 static void network_provider_set_property (GObject *object,
76 guint prop_id,
77 const GValue *value,
78 GParamSpec *pspec);
79 static void network_provider_get_property (GObject *object,
80 guint prop_id,
81 GValue *value,
82 GParamSpec *pspec);
83 static void network_provider_connections_changed (GObject *object,
84 GParamSpec *pspec,
85 gpointer user_data);
86 static TrackerNetworkProviderStatus
87 network_provider_get_status (TrackerNetworkProvider *provider);
88
89 enum {
90 PROP_0,
91 PROP_NAME
92 };
93
94 G_DEFINE_TYPE_WITH_CODE (TrackerNetworkProviderNetworkManager,
95 tracker_network_provider_network_manager,
96 G_TYPE_OBJECT,
97 G_IMPLEMENT_INTERFACE (TRACKER_TYPE_NETWORK_PROVIDER,
98 tracker_network_provider_iface_init))
99
100 static void
101 tracker_network_provider_network_manager_class_init (TrackerNetworkProviderNetworkManagerClass *klass)
102 {
103 GObjectClass *object_class = G_OBJECT_CLASS (klass);
104
105 object_class->set_property = network_provider_set_property;
106 object_class->get_property = network_provider_get_property;
107 object_class->finalize = network_provider_finalize;
108
109 g_object_class_override_property (object_class, PROP_NAME, "name");
110
111 g_type_class_add_private (object_class, sizeof (TrackerNetworkProviderNetworkManagerPrivate));
112 }
113
114 static void
115 tracker_network_provider_network_manager_init (TrackerNetworkProviderNetworkManager *provider)
116 {
117 TrackerNetworkProviderNetworkManagerPrivate *priv;
118
119 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (provider);
120
121 priv->client = nm_client_new ();
122 priv->last_emitted_status = TRACKER_NETWORK_PROVIDER_DISCONNECTED;
123 g_signal_connect (priv->client, "notify::state",
124 G_CALLBACK (network_provider_connections_changed),
125 provider);
126 }
127
128 static void
129 tracker_network_provider_iface_init (TrackerNetworkProviderIface *iface)
130 {
131 iface->get_status = network_provider_get_status;
132 }
133
134 /*
135 * Returns the first NMActiveConnection with the "default" property set, or
136 * NULL if none is found.
137 */
138 static NMActiveConnection*
139 find_default_active_connection (NMClient *client)
140 {
141 const GPtrArray *active_connections;
142 gint i;
143 NMActiveConnection *active_connection = NULL;
144
145 active_connections = nm_client_get_active_connections (client);
146
147 for (i = 0; i < active_connections->len; i++) {
148 active_connection = g_ptr_array_index (active_connections, i);
149
150 if (nm_active_connection_get_default (active_connection)) {
151 break;
152 }
153 }
154
155 return active_connection;
156 }
157
158 static void
159 network_provider_finalize (GObject *object)
160 {
161 TrackerNetworkProviderNetworkManagerPrivate *priv;
162
163 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (object);
164
165 g_object_unref (priv->client);
166 g_free (priv->name);
167 }
168
169 static void
170 network_provider_set_property (GObject *object,
171 guint prop_id,
172 const GValue *value,
173 GParamSpec *pspec)
174 {
175 TrackerNetworkProviderNetworkManagerPrivate *priv;
176
177 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (object);
178
179 switch (prop_id) {
180 case PROP_NAME:
181 g_free (priv->name);
182 priv->name = g_value_dup_string (value);
183 g_object_notify (object, "name");
184 break;
185
186 default:
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 break;
189 };
190 }
191
192 static void
193 network_provider_get_property (GObject *object,
194 guint prop_id,
195 GValue *value,
196 GParamSpec *pspec)
197 {
198 TrackerNetworkProviderNetworkManagerPrivate *priv;
199
200 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (object);
201
202 switch (prop_id) {
203 case PROP_NAME:
204 g_value_set_string (value, priv->name);
205 break;
206
207 default:
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 break;
210 };
211 }
212
213 static void
214 network_provider_connections_changed (GObject *object,
215 GParamSpec *pspec,
216 gpointer user_data)
217 {
218 TrackerNetworkProviderNetworkManagerPrivate *priv;
219 TrackerNetworkProvider *provider;
220 TrackerNetworkProviderStatus status;
221
222 g_return_if_fail (TRACKER_IS_NETWORK_PROVIDER_NETWORK_MANAGER (user_data));
223
224 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (object);
225
226 provider = user_data;
227 status = tracker_network_provider_get_status (provider);
228
229 if (status == priv->last_emitted_status) {
230 return;
231 }
232
233 priv->last_emitted_status = status;
234
235 g_signal_emit_by_name (provider, "status-changed", status);
236 }
237
238 static TrackerNetworkProviderStatus
239 network_provider_get_status (TrackerNetworkProvider *provider)
240 {
241 TrackerNetworkProviderNetworkManagerPrivate *priv;
242 NMActiveConnection *default_active_connection;
243 const GPtrArray *devices;
244 NMDevice *device;
245
246 priv = TRACKER_NETWORK_PROVIDER_NETWORK_MANAGER_GET_PRIVATE (provider);
247
248 if (!nm_client_get_manager_running (priv->client)) {
249 return TRACKER_NETWORK_PROVIDER_UNKNOWN;
250 }
251
252 switch (nm_client_get_state (priv->client)) {
253 case NM_STATE_UNKNOWN:
254 return TRACKER_NETWORK_PROVIDER_UNKNOWN;
255 case NM_STATE_CONNECTED:
256 break;
257 default:
258 return TRACKER_NETWORK_PROVIDER_DISCONNECTED;
259 }
260
261 default_active_connection = find_default_active_connection (priv->client);
262
263 if (!default_active_connection) {
264 return TRACKER_NETWORK_PROVIDER_DISCONNECTED;
265 }
266
267 switch (nm_active_connection_get_state (default_active_connection)) {
268 case NM_ACTIVE_CONNECTION_STATE_UNKNOWN:
269 return TRACKER_NETWORK_PROVIDER_UNKNOWN;
270 case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
271 break;
272 default:
273 return TRACKER_NETWORK_PROVIDER_DISCONNECTED;
274 }
275
276 devices = nm_active_connection_get_devices (default_active_connection);
277
278 if (!devices->len) {
279 return TRACKER_NETWORK_PROVIDER_DISCONNECTED;
280 }
281
282 /* Pick the first device, I don't know when there are more than one */
283 device = g_ptr_array_index (devices, 0);
284
285 switch (nm_device_get_state (device)) {
286 case NM_DEVICE_STATE_UNKNOWN:
287 return TRACKER_NETWORK_PROVIDER_UNKNOWN;
288 break;
289 case NM_DEVICE_STATE_ACTIVATED:
290 break;
291 default:
292 return TRACKER_NETWORK_PROVIDER_DISCONNECTED;
293 }
294
295 if (NM_IS_DEVICE_ETHERNET (device) || NM_IS_DEVICE_WIFI (device)) {
296 return TRACKER_NETWORK_PROVIDER_LAN;
297 }
298
299 #if (NM_CHECK_VERSION (0,8,992))
300 if (NM_IS_DEVICE_MODEM (device) || NM_IS_DEVICE_WIMAX (device)) {
301 return TRACKER_NETWORK_PROVIDER_3G;
302 }
303 #else
304 if (NM_IS_GSM_DEVICE (device) || NM_IS_CDMA_DEVICE (device)) {
305 return TRACKER_NETWORK_PROVIDER_3G;
306 }
307 #endif
308
309 /* We know the device is activated, but we don't know the type of device */
310 return TRACKER_NETWORK_PROVIDER_UNKNOWN;
311 }
312
313 TrackerNetworkProvider *
314 tracker_network_provider_get (void)
315 {
316 static TrackerNetworkProvider *instance = NULL;
317 static GMutex mutex;
318
319 g_mutex_lock (&mutex);
320
321 if (!instance) {
322 instance = g_object_new (TRACKER_TYPE_NETWORK_PROVIDER_NETWORK_MANAGER,
323 "name", NETWORK_PROVIDER_NETWORK_MANAGER_NAME,
324 NULL);
325 }
326
327 g_mutex_unlock (&mutex);
328
329 return instance;
330 }