File: oobs-selfconfig.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 (288 lines) | stat: -rw-r--r-- 7,747 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
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2007 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 <glib-object.h>
#include <string.h>
#include <unistd.h>

#include "oobs-object.h"
#include "oobs-object-private.h"
#include "oobs-selfconfig.h"
#include "oobs-usersconfig.h"
#include "oobs-user.h"
#include "utils.h"

/**
 * SECTION:oobs-selfconfig
 * @title: OobsSelfConfig
 * @short_description: Object that represents the current user
 **/

#define SELF_CONFIG_REMOTE_OBJECT "SelfConfig2"
#define OOBS_SELF_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_SELF_CONFIG, OobsSelfConfigPrivate))

typedef struct _OobsSelfConfigPrivate OobsSelfConfigPrivate;

struct _OobsSelfConfigPrivate
{
  uid_t     uid;
  OobsUser *user;
};

static void oobs_self_config_class_init  (OobsSelfConfigClass *class);
static void oobs_self_config_init        (OobsSelfConfig      *config);
static void oobs_self_config_constructed (GObject             *object);
static void oobs_self_config_finalize    (GObject             *object);

static void oobs_self_config_update             (OobsObject   *object);
static void oobs_self_config_commit             (OobsObject   *object);
static void oobs_self_config_get_update_message (OobsObject   *object);


G_DEFINE_TYPE (OobsSelfConfig, oobs_self_config, OOBS_TYPE_OBJECT);


static void
oobs_self_config_class_init (OobsSelfConfigClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  OobsObjectClass *oobs_object_class = OOBS_OBJECT_CLASS (class);

  object_class->constructed  = oobs_self_config_constructed;
  object_class->finalize     = oobs_self_config_finalize;

  oobs_object_class->commit  = oobs_self_config_commit;
  oobs_object_class->update  = oobs_self_config_update;
  oobs_object_class->get_update_message = oobs_self_config_get_update_message;

  g_type_class_add_private (object_class,
			    sizeof (OobsSelfConfigPrivate));
}

static void
oobs_self_config_init (OobsSelfConfig *config)
{
  OobsSelfConfigPrivate *priv;

  priv = OOBS_SELF_CONFIG_GET_PRIVATE (config);

  priv->uid = getuid ();

  config->_priv = priv;
}

static void
oobs_self_config_users_updated (OobsSelfConfig  *self,
				OobsUsersConfig *users)
{
  OobsSelfConfigPrivate *priv;
  OobsList *list;
  OobsListIter iter;
  gboolean valid;

  priv = self->_priv;
  list = oobs_users_config_get_users (users);
  valid = oobs_list_get_iter_first (list, &iter);

  if (priv->user)
    {
      g_object_unref (priv->user);
      priv->user = NULL;
    }

  while (valid && !priv->user)
    {
      OobsUser *user;

      user = OOBS_USER (oobs_list_get (list, &iter));

      if (oobs_user_get_uid (user) == priv->uid)
	priv->user = g_object_ref (user);

      g_object_unref (user);
      valid = oobs_list_iter_next (list, &iter);
    }
}

static void
oobs_self_config_constructed (GObject *object)
{
  /* stay tuned to users config updates */
  g_signal_connect_swapped (oobs_users_config_get (), "updated",
			    G_CALLBACK (oobs_self_config_users_updated), object);
}

static void
oobs_self_config_finalize (GObject *object)
{
  OobsSelfConfigPrivate *priv;

  priv = OOBS_SELF_CONFIG (object)->_priv;

  if (priv->user)
    g_object_unref (priv->user);

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

static void
oobs_self_config_update (OobsObject *object)
{
  OobsObject *users_config;

  /* We don't actually need to update, since we joint point to an OobsUser */

  users_config = oobs_users_config_get ();

  /* just update user if the users object was already
   * updated, update will be forced later if required
   */
  if (oobs_object_has_updated (users_config))
    oobs_self_config_users_updated (OOBS_SELF_CONFIG (object),
				    OOBS_USERS_CONFIG (users_config));
}

static void
oobs_self_config_commit (OobsObject *object)
{
  OobsSelfConfigPrivate *priv;
  DBusMessageIter iter, array_iter;
  DBusMessage *message;
  guint32 uid;

  priv = OOBS_SELF_CONFIG (object)->_priv;

  if (!priv->user)
    return;

  uid = priv->uid;
  message = _oobs_object_get_dbus_message (object);
  dbus_message_iter_init_append (message, &iter);

  utils_append_uint (&iter, uid);

  /* GECOS fields */
  dbus_message_iter_open_container (&iter,
				    DBUS_TYPE_ARRAY,
				    DBUS_TYPE_STRING_AS_STRING,
				    &array_iter);

  utils_append_string (&array_iter, oobs_user_get_full_name (priv->user));
  utils_append_string (&array_iter, oobs_user_get_room_number (priv->user));
  utils_append_string (&array_iter, oobs_user_get_work_phone_number (priv->user));
  utils_append_string (&array_iter, oobs_user_get_home_phone_number (priv->user));
  utils_append_string (&array_iter, oobs_user_get_other_data (priv->user));

  dbus_message_iter_close_container (&iter, &array_iter);

  utils_append_string (&iter, oobs_user_get_locale (priv->user));
  /* TODO: use location when the backends support it */
  utils_append_string (&iter, "");
}

/*
 * We need a custom update message containing the user's UID.
 */
static void
oobs_self_config_get_update_message (OobsObject *object)
{
  OobsSelfConfigPrivate *priv;
  DBusMessageIter iter;
  DBusMessage *message;
  guint32 uid;

  priv = OOBS_SELF_CONFIG (object)->_priv;

  message = _oobs_object_get_dbus_message (object);
  dbus_message_iter_init_append (message, &iter);

  uid = priv->uid;
  utils_append_uint (&iter, uid);
}

/**
 * oobs_self_config_get:
 *
 * Returns the #OobsSelfConfig singleton, which represents
 * the user configuration for the requester uid.
 *
 * Return Value: the singleton #OobsSelfConfig object.
 **/
OobsObject*
oobs_self_config_get (void)
{
  static OobsObject *the_object = NULL;

  if (!the_object)
    the_object = g_object_new (OOBS_TYPE_SELF_CONFIG,
                               "remote-object", SELF_CONFIG_REMOTE_OBJECT,
                               NULL);

  return the_object;
}

/**
 * oobs_self_config_get_user:
 * @config: An #OobsSelfConfig.
 * 
 * Returns the #OobsUser that represents the requester user.
 * 
 * Return Value: An #OobsUser, you must not reference this object.
 **/
OobsUser*
oobs_self_config_get_user (OobsSelfConfig *config)
{
  OobsSelfConfigPrivate *priv;

  g_return_val_if_fail (OOBS_IS_SELF_CONFIG (config), NULL);

  oobs_object_ensure_update (OOBS_OBJECT (config));
  priv = config->_priv;

  return priv->user;
}


/**
 * oobs_self_config_is_user_self:
 * @config: An #OobsSelfConfig.
 * @user: An #OobsUser.
 *
 * Check whether @user is the user represented by #OobsSelfConfig.
 *
 * Return Value: %TRUE if @user is self, %FALSE otherwise.
 **/
gboolean
oobs_self_config_is_user_self (OobsSelfConfig *config,
                               OobsUser       *user)
{
  OobsSelfConfigPrivate *priv;

  g_return_val_if_fail (OOBS_IS_SELF_CONFIG (config), FALSE);
  g_return_val_if_fail (OOBS_IS_USER (user), FALSE);

  oobs_object_ensure_update (OOBS_OBJECT (config));
  priv = config->_priv;

  return (priv->user == user);
}