File: oobs-session.c

package info (click to toggle)
liboobs 3.0.0-4.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,284 kB
  • sloc: sh: 10,131; ansic: 9,920; makefile: 176; xml: 1
file content (515 lines) | stat: -rw-r--r-- 14,415 bytes parent folder | download | duplicates (7)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2005 Carlos Garnacho
 *
 * This program 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 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <glib-object.h>
#include <glib.h>
#include "oobs-session.h"
#include "oobs-session-private.h"
#include "oobs-object.h"
#include "utils.h"

/**
 * SECTION:oobs-session
 * @title: OobsSession
 * @short_description: Manager of the connection to the backends
 **/

#define OOBS_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_SESSION, OobsSessionPrivate))
#define PLATFORMS_PATH OOBS_DBUS_PATH_PREFIX "/Platform"
#define PLATFORMS_INTERFACE OOBS_DBUS_METHOD_PREFIX ".Platform"
#define POLKIT_ACTION "org.freedesktop.systemtoolsbackends.set"

typedef struct _OobsSessionPrivate OobsSessionPrivate;

struct _OobsSessionPrivate
{
  DBusConnection *connection;
  DBusError       dbus_error;

  GList    *session_objects;
  gboolean  is_authenticated;

  gchar    *platform;
  GList    *supported_platforms;
};

static void oobs_session_class_init (OobsSessionClass *class);
static void oobs_session_init       (OobsSession      *session);

static GObject * oobs_session_constructor (GType                  type,
					   guint                  n_construct_properties,
					   GObjectConstructParam *construct_params);

static void oobs_session_set_property (GObject      *object,
				       guint         prop_id,
				       const GValue *value,
				       GParamSpec   *pspec);
static void oobs_session_get_property (GObject      *object,
				       guint         prop_id,
				       GValue       *value,
				       GParamSpec   *pspec);

enum
{
  PROP_0,
  PROP_PLATFORM
};

G_DEFINE_TYPE (OobsSession, oobs_session, G_TYPE_OBJECT);

static void
oobs_session_class_init (OobsSessionClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->constructor  = oobs_session_constructor;
  object_class->set_property = oobs_session_set_property;
  object_class->get_property = oobs_session_get_property;

  g_object_class_install_property (object_class,
				   PROP_PLATFORM,
				   g_param_spec_string ("platform",
							"Platform",
							"Name of the platform the session is running on",
							NULL,
							G_PARAM_READWRITE));
  g_type_class_add_private (object_class,
			    sizeof (OobsSessionPrivate));
}

static void
oobs_session_init (OobsSession *session)
{
  OobsSessionPrivate *priv;

  g_return_if_fail (OOBS_IS_SESSION (session));
  priv = OOBS_SESSION_GET_PRIVATE (session);

  dbus_error_init (&priv->dbus_error);
  priv->connection = dbus_bus_get (DBUS_BUS_SYSTEM, &priv->dbus_error);

  if (dbus_error_is_set (&priv->dbus_error))
    g_warning ("%s", priv->dbus_error.message);
  else
    dbus_connection_setup_with_g_main (priv->connection, NULL);

  priv->session_objects  = NULL;
  priv->is_authenticated = FALSE;
  session->_priv = priv;
}

static GObject *
oobs_session_constructor (GType                  type,
			  guint                  n_construct_properties,
			  GObjectConstructParam *construct_params)
{
  static GObject *session = NULL;

  if (!session)
    session = (* G_OBJECT_CLASS (oobs_session_parent_class)->constructor) (type,
									   n_construct_properties,
									   construct_params);
  return session;
}

static void
oobs_session_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
  OobsSession *session;
  OobsSessionPrivate *priv;

  g_return_if_fail (OOBS_IS_SESSION (object));

  session = OOBS_SESSION (object);
  priv    = session->_priv;

  switch (prop_id)
    {
    case PROP_PLATFORM:
      oobs_session_set_platform (session, g_value_get_string (value));
      break;
    }
}

static void
oobs_session_get_property (GObject      *object,
			   guint         prop_id,
			   GValue       *value,
			   GParamSpec   *pspec)
{
  OobsSessionPrivate *priv;

  g_return_if_fail (OOBS_IS_SESSION (object));

  priv = OOBS_SESSION (object)->_priv;

  switch (prop_id)
    {
    case PROP_PLATFORM:
      g_value_set_string (value, priv->platform);
      break;
    }
}

/**
 * oobs_session_get:
 * 
 * Returns the #OobsSession singleton, which represents
 * the session with the system tools backends.
 * 
 * Return Value: the singleton #OobSession object.
 **/
OobsSession*
oobs_session_get (void)
{
  static OobsSession *session = NULL;

  if (!session)
    session = g_object_new (OOBS_TYPE_SESSION, NULL);

  return session;
}

/**
 * oobs_session_commit:
 * @session: an #OobsSession
 * 
 * Commits inmediately all the changes to the configuration
 * objects that have been requested through this #OobsSession.
 * Note that it will stop if it finds any error.
 *
 * Return Value: An #OobsResult representing the error.
 **/
OobsResult
oobs_session_commit (OobsSession *session)
{
  OobsSessionPrivate *priv;
  GList              *node;
  OobsObject         *object;
  OobsResult          result = OOBS_RESULT_OK;

  g_return_val_if_fail (session != NULL, OOBS_RESULT_ERROR);
  g_return_val_if_fail (OOBS_IS_SESSION (session), OOBS_RESULT_ERROR);

  priv = session->_priv;
  node = priv->session_objects;

  while (node && (result == OOBS_RESULT_OK))
    {
      object = OOBS_OBJECT (node->data);
      result = oobs_object_commit (object);

      node = node->next;
    }

  return result;
}

/**
 * oobs_session_get_connected:
 * @session: An #OobsSession
 * 
 * Returns whether the connection with the backends is established.
 * 
 * Return Value: #TRUE if there's connection with the backends.
 **/
gboolean
oobs_session_get_connected (OobsSession *session)
{
  OobsSessionPrivate *priv;

  g_return_val_if_fail (OOBS_IS_SESSION (session), FALSE);

  priv = session->_priv;
  return (priv->connection != NULL);
}

/**
 * oobs_session_get_platform:
 * @session: An #OobsSession.
 * @platform: location to store the current platform, or #NULL. This
 *            string is of internal use, and must not be freed or modified.
 * 
 * Retrieves the platform your system has been identified with, or
 * #NULL in case your platform is not recognized or other error happens.
 * 
 * Return Value: An #OobsResult representing the error.
 **/
OobsResult
oobs_session_get_platform (OobsSession  *session,
			   gchar       **platform)
{
  OobsSessionPrivate *priv;
  DBusMessage *message, *reply;
  DBusMessageIter iter;
  OobsResult result;

  g_return_val_if_fail (OOBS_IS_SESSION (session), OOBS_RESULT_ERROR);

  priv = session->_priv;
  g_return_val_if_fail (priv->connection != NULL, OOBS_RESULT_ERROR);

  message = dbus_message_new_method_call (OOBS_DBUS_DESTINATION,
					  PLATFORMS_PATH,
					  PLATFORMS_INTERFACE,
					  "getPlatform");

  reply = dbus_connection_send_with_reply_and_block (priv->connection,
						     message, -1, &priv->dbus_error);
  dbus_message_unref (message);

  if (dbus_error_is_set (&priv->dbus_error))
    {
      if (dbus_error_has_name (&priv->dbus_error, DBUS_ERROR_ACCESS_DENIED))
        /* Warning: this can mean that D-Bus policy denied access, but also that PolicyKit refused it */
	result = OOBS_RESULT_ACCESS_DENIED;
      else
      {
	result = OOBS_RESULT_ERROR;
        g_warning ("There was an unknown error communicating with the backends: %s", priv->dbus_error.message);
      }

      dbus_error_free (&priv->dbus_error);

      if (platform)
	*platform = NULL;

      return result;
    }

  dbus_message_iter_init (reply, &iter);
  priv->platform = utils_dup_string (&iter);

  if (platform)
    *platform = priv->platform;

  dbus_message_unref (reply);
  return (priv->platform) ? OOBS_RESULT_OK : OOBS_RESULT_NO_PLATFORM;
}

/**
 * oobs_session_set_platform:
 * @session: An #OobsSession.
 * @platform: A string defining the platform. see
 *            oobs_session_get_platforms_list() to know where to get this string.
 * 
 * Identifies your platform as the one set in @platform. This is only necessary if
 * your platform could not be guessed (and thus oobs_session_get_platform() would
 * return #OOBS_RESULT_NO_PLATFORM in this case).
 * 
 * Return Value: An #OobsResult representing the error.
 **/
OobsResult
oobs_session_set_platform (OobsSession *session,
			   const gchar *platform)
{
  OobsSessionPrivate *priv;
  DBusMessage *message;
  DBusMessageIter iter;
  DBusError error;
  OobsResult result;

  g_return_val_if_fail (OOBS_IS_SESSION (session), OOBS_RESULT_ERROR);
  g_return_val_if_fail (platform != NULL, OOBS_RESULT_ERROR);

  priv = session->_priv;
  g_return_val_if_fail (priv->connection != NULL, OOBS_RESULT_ERROR);
  dbus_error_init (&error);

  priv->platform = g_strdup (platform);
  g_object_notify (G_OBJECT (session), "platform");

  message = dbus_message_new_method_call (OOBS_DBUS_DESTINATION,
					  PLATFORMS_PATH,
					  PLATFORMS_INTERFACE,
					  "setPlatform");
  dbus_message_iter_init_append (message, &iter);
  utils_append_string (&iter, priv->platform);

  dbus_connection_send_with_reply_and_block (priv->connection, message, -1, &error);

  if (dbus_error_is_set (&error))
    {
      if (dbus_error_has_name (&error, DBUS_ERROR_NO_REPLY))
	result = OOBS_RESULT_OK;
      if (dbus_error_has_name (&error, DBUS_ERROR_ACCESS_DENIED))
	result = OOBS_RESULT_ACCESS_DENIED;
      else
	result = OOBS_RESULT_ERROR;

      dbus_error_free (&error);
    }
  else
    result = OOBS_RESULT_OK;

  return result;
}

static OobsResult
get_supported_platforms (OobsSession *session, GList **list)
{
  OobsSessionPrivate *priv;
  DBusMessage *message, *reply;
  DBusMessageIter list_iter, iter;
  OobsPlatform *platform;
  OobsResult result;
  GList *platforms = NULL;

  priv = session->_priv;
  g_return_val_if_fail (priv->connection != NULL, OOBS_RESULT_ERROR);

  message = dbus_message_new_method_call (OOBS_DBUS_DESTINATION,
					  PLATFORMS_PATH,
					  PLATFORMS_INTERFACE,
					  "getPlatformList");

  reply = dbus_connection_send_with_reply_and_block (priv->connection,
						     message, -1, &priv->dbus_error);
  dbus_message_unref (message);

  if (dbus_error_is_set (&priv->dbus_error))
    {
      if (dbus_error_has_name (&priv->dbus_error, DBUS_ERROR_ACCESS_DENIED))
	result = OOBS_RESULT_ACCESS_DENIED;
      else
	result = OOBS_RESULT_ERROR;

      dbus_error_free (&priv->dbus_error);
      *list = NULL;
      return result;
    }

  dbus_message_iter_init (reply, &list_iter);
  dbus_message_iter_recurse (&list_iter, &list_iter);

  while (dbus_message_iter_get_arg_type (&list_iter) == DBUS_TYPE_STRUCT)
    {
      platform = g_new0 (OobsPlatform, 1);
      dbus_message_iter_recurse (&list_iter, &iter);

      platform->name = utils_dup_string (&iter);
      platform->version = utils_dup_string (&iter);
      platform->codename = utils_dup_string (&iter);
      platform->id = utils_dup_string (&iter);
      platforms = g_list_prepend (platforms, platform);

      dbus_message_iter_next (&list_iter);
    }

  *list = g_list_reverse (platforms);
  dbus_message_unref (reply);

  return OOBS_RESULT_OK;
}

/**
 * oobs_session_get_supported_platforms:
 * @session: An #OobsSession.
 * @platforms: return location for the list of platforms. It's a
 *             #GList of #OobsPlatform structs. You must free
 *             this list with g_list_free().
 * 
 * Retrieves the list of supported platforms, this is only necessary when
 + oobs_session_get_platform() has returned #OOBS_RESULT_NO_PLATFORM. To
 * specify a platform, you must use oobs_session_set_platform(), being
 * the platform string in that function the platform->id value inside
 * the #OobsPlatform struct.
 * 
 * Return Value: An #OobsResult representing the error.
 **/
OobsResult
oobs_session_get_supported_platforms (OobsSession  *session,
				      GList       **platforms)
{
  OobsSessionPrivate *priv;
  OobsResult result;

  /* it doesn't make any sense to call this function with platforms = NULL */
  g_return_val_if_fail (platforms != NULL, OOBS_RESULT_ERROR);
  g_return_val_if_fail (OOBS_IS_SESSION (session), OOBS_RESULT_ERROR);

  priv = session->_priv;

  if (!priv->supported_platforms)
    result = get_supported_platforms (session, &priv->supported_platforms);
  else
    {
      /* list is cached */
      result = OOBS_RESULT_OK;
    }

  *platforms = (priv->supported_platforms) ? g_list_copy (priv->supported_platforms) : NULL;
  return result;
}

/**
 * oobs_session_process_requests:
 * @session: An #OobsSession
 * 
 * Blocks until all pending asynchronous requests have been processed.
 **/
void
oobs_session_process_requests (OobsSession *session)
{
  OobsSessionPrivate *priv;

  g_return_if_fail (OOBS_IS_SESSION (session));

  priv = session->_priv;
  g_list_foreach (priv->session_objects, (GFunc) oobs_object_process_requests, NULL);
}

/**
 * oobs_session_get_authentication_action:
 * @session: An #OobsSession
 *
 * Returns the PolicyKit action the user has to be authenticated to in order to
 * commit changes to configuration objects in this session. If the user has not
 * the required permissions, any attempt to commit will return #OOBS_RESULT_ACCESS_DENIED.
 *
 * Return Value: string defining the PolicyKit action
 *               required to modify objects in the session.
 **/
G_CONST_RETURN gchar *
oobs_session_get_authentication_action (OobsSession *session)
{
  g_return_val_if_fail (OOBS_IS_SESSION (session), NULL);

  return POLKIT_ACTION;
}

/* protected methods */
DBusConnection*
_oobs_session_get_connection_bus (OobsSession *session)
{
  OobsSessionPrivate *priv;

  g_return_val_if_fail (session != NULL, NULL);
  g_return_val_if_fail (OOBS_IS_SESSION (session), NULL);

  priv = session->_priv;
  return priv->connection;
}