~ubuntu-branches/ubuntu/raring/accountsservice/raring

« back to all changes in this revision

Viewing changes to src/libaccountsservice/act-user.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-03-13 11:10:25 UTC
  • mto: (2.1.1 experimental) (14.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20110313111025-cx1ef9o80jgm4ljm
Tags: upstream-0.6.5
ImportĀ upstreamĀ versionĀ 0.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2004-2005 James M. Cape <jcape@ignore-your.tv>.
 
4
 * Copyright (C) 2007-2008 William Jon McCann <mccann@jhu.edu>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <float.h>
 
24
#include <string.h>
 
25
#include <sys/types.h>
 
26
#include <sys/stat.h>
 
27
#include <unistd.h>
 
28
 
 
29
#include <dbus/dbus-glib.h>
 
30
 
 
31
#include <glib.h>
 
32
#include <glib/gi18n.h>
 
33
#include <gio/gio.h>
 
34
 
 
35
#include "act-user-private.h"
 
36
 
 
37
#define ACT_USER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ACT_TYPE_USER, ActUserClass))
 
38
#define ACT_IS_USER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ACT_TYPE_USER))
 
39
#define ACT_USER_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), ACT_TYPE_USER, ActUserClass))
 
40
 
 
41
#define ACCOUNTS_NAME           "org.freedesktop.Accounts"
 
42
#define ACCOUNTS_USER_INTERFACE "org.freedesktop.Accounts.User"
 
43
 
 
44
enum {
 
45
        PROP_0,
 
46
        PROP_UID,
 
47
        PROP_USER_NAME,
 
48
        PROP_REAL_NAME,
 
49
        PROP_ACCOUNT_TYPE,
 
50
        PROP_PASSWORD_MODE,
 
51
        PROP_PASSWORD_HINT,
 
52
        PROP_HOME_DIR,
 
53
        PROP_SHELL,
 
54
        PROP_EMAIL,
 
55
        PROP_LOCATION,
 
56
        PROP_LOCKED,
 
57
        PROP_AUTOMATIC_LOGIN,
 
58
        PROP_LOGIN_FREQUENCY,
 
59
        PROP_ICON_FILE,
 
60
        PROP_LANGUAGE,
 
61
        PROP_X_SESSION,
 
62
        PROP_IS_LOADED
 
63
};
 
64
 
 
65
enum {
 
66
        CHANGED,
 
67
        SESSIONS_CHANGED,
 
68
        LAST_SIGNAL
 
69
};
 
70
 
 
71
struct _ActUser {
 
72
        GObject         parent;
 
73
 
 
74
        DBusGConnection *connection;
 
75
        DBusGProxy      *accounts_proxy;
 
76
        DBusGProxy      *object_proxy;
 
77
        DBusGProxyCall  *get_all_call;
 
78
        char            *object_path;
 
79
 
 
80
        uid_t           uid;
 
81
        char           *user_name;
 
82
        char           *real_name;
 
83
        char           *password_hint;
 
84
        char           *home_dir;
 
85
        char           *shell;
 
86
        char           *email;
 
87
        char           *location;
 
88
        char           *icon_file;
 
89
        char           *language;
 
90
        char           *x_session;
 
91
        GList          *sessions;
 
92
        int             login_frequency;
 
93
 
 
94
        ActUserAccountType  account_type;
 
95
        ActUserPasswordMode password_mode;
 
96
 
 
97
        guint           is_loaded : 1;
 
98
        guint           locked : 1;
 
99
        guint           automatic_login : 1;
 
100
};
 
101
 
 
102
struct _ActUserClass
 
103
{
 
104
        GObjectClass parent_class;
 
105
};
 
106
 
 
107
static void act_user_finalize     (GObject      *object);
 
108
 
 
109
static guint signals[LAST_SIGNAL] = { 0 };
 
110
 
 
111
G_DEFINE_TYPE (ActUser, act_user, G_TYPE_OBJECT)
 
112
 
 
113
static int
 
114
session_compare (const char *a,
 
115
                 const char *b)
 
116
{
 
117
        if (a == NULL) {
 
118
                return 1;
 
119
        } else if (b == NULL) {
 
120
                return -1;
 
121
        }
 
122
 
 
123
        return strcmp (a, b);
 
124
}
 
125
 
 
126
void
 
127
_act_user_add_session (ActUser    *user,
 
128
                       const char *ssid)
 
129
{
 
130
        GList *li;
 
131
 
 
132
        g_return_if_fail (ACT_IS_USER (user));
 
133
        g_return_if_fail (ssid != NULL);
 
134
 
 
135
        li = g_list_find_custom (user->sessions, ssid, (GCompareFunc)session_compare);
 
136
        if (li == NULL) {
 
137
                g_debug ("ActUser: adding session %s", ssid);
 
138
                user->sessions = g_list_prepend (user->sessions, g_strdup (ssid));
 
139
                g_signal_emit (user, signals[SESSIONS_CHANGED], 0);
 
140
        } else {
 
141
                g_debug ("ActUser: session already present: %s", ssid);
 
142
        }
 
143
}
 
144
 
 
145
void
 
146
_act_user_remove_session (ActUser    *user,
 
147
                          const char *ssid)
 
148
{
 
149
        GList *li;
 
150
 
 
151
        g_return_if_fail (ACT_IS_USER (user));
 
152
        g_return_if_fail (ssid != NULL);
 
153
 
 
154
        li = g_list_find_custom (user->sessions, ssid, (GCompareFunc)session_compare);
 
155
        if (li != NULL) {
 
156
                g_debug ("ActUser: removing session %s", ssid);
 
157
                g_free (li->data);
 
158
                user->sessions = g_list_delete_link (user->sessions, li);
 
159
                g_signal_emit (user, signals[SESSIONS_CHANGED], 0);
 
160
        } else {
 
161
                g_debug ("ActUser: session not found: %s", ssid);
 
162
        }
 
163
}
 
164
 
 
165
guint
 
166
act_user_get_num_sessions (ActUser    *user)
 
167
{
 
168
        return g_list_length (user->sessions);
 
169
}
 
170
 
 
171
static void
 
172
act_user_set_property (GObject      *object,
 
173
                       guint         param_id,
 
174
                       const GValue *value,
 
175
                       GParamSpec   *pspec)
 
176
{
 
177
        ActUser *user;
 
178
 
 
179
        user = ACT_USER (object);
 
180
 
 
181
        switch (param_id) {
 
182
        default:
 
183
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
184
                break;
 
185
        }
 
186
}
 
187
 
 
188
static void
 
189
act_user_get_property (GObject    *object,
 
190
                       guint       param_id,
 
191
                       GValue     *value,
 
192
                       GParamSpec *pspec)
 
193
{
 
194
        ActUser *user;
 
195
 
 
196
        user = ACT_USER (object);
 
197
 
 
198
        switch (param_id) {
 
199
        case PROP_UID:
 
200
                g_value_set_int (value, user->uid);
 
201
                break;
 
202
        case PROP_USER_NAME:
 
203
                g_value_set_string (value, user->user_name);
 
204
                break;
 
205
        case PROP_REAL_NAME:
 
206
                g_value_set_string (value, user->real_name);
 
207
                break;
 
208
        case PROP_ACCOUNT_TYPE:
 
209
                g_value_set_int (value, user->account_type);
 
210
                break;
 
211
        case PROP_PASSWORD_MODE:
 
212
                g_value_set_int (value, user->password_mode);
 
213
                break;
 
214
        case PROP_PASSWORD_HINT:
 
215
                g_value_set_string (value, user->password_hint);
 
216
                break;
 
217
        case PROP_HOME_DIR:
 
218
                g_value_set_string (value, user->home_dir);
 
219
                break;
 
220
        case PROP_LOGIN_FREQUENCY:
 
221
                g_value_set_int (value, user->login_frequency);
 
222
                break;
 
223
        case PROP_SHELL:
 
224
                g_value_set_string (value, user->shell);
 
225
                break;
 
226
        case PROP_EMAIL:
 
227
                g_value_set_string (value, user->email);
 
228
                break;
 
229
        case PROP_LOCATION:
 
230
                g_value_set_string (value, user->location);
 
231
                break;
 
232
        case PROP_ICON_FILE:
 
233
                g_value_set_string (value, user->icon_file);
 
234
                break;
 
235
        case PROP_LANGUAGE:
 
236
                g_value_set_string (value, user->language);
 
237
                break;
 
238
        case PROP_X_SESSION:
 
239
                g_value_set_string (value, user->x_session);
 
240
                break;
 
241
        case PROP_LOCKED:
 
242
                g_value_set_boolean (value, user->locked);
 
243
                break;
 
244
        case PROP_AUTOMATIC_LOGIN:
 
245
                g_value_set_boolean (value, user->automatic_login);
 
246
                break;
 
247
        case PROP_IS_LOADED:
 
248
                g_value_set_boolean (value, user->is_loaded);
 
249
                break;
 
250
        default:
 
251
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
252
                break;
 
253
        }
 
254
}
 
255
 
 
256
 
 
257
static void
 
258
act_user_class_init (ActUserClass *class)
 
259
{
 
260
        GObjectClass *gobject_class;
 
261
 
 
262
        gobject_class = G_OBJECT_CLASS (class);
 
263
 
 
264
        gobject_class->finalize = act_user_finalize;
 
265
        gobject_class->set_property = act_user_set_property;
 
266
        gobject_class->get_property = act_user_get_property;
 
267
 
 
268
        g_object_class_install_property (gobject_class,
 
269
                                         PROP_REAL_NAME,
 
270
                                         g_param_spec_string ("real-name",
 
271
                                                              "Real Name",
 
272
                                                              "The real name to display for this user.",
 
273
                                                              NULL,
 
274
                                                              G_PARAM_READABLE));
 
275
 
 
276
        g_object_class_install_property (gobject_class,
 
277
                                         PROP_ACCOUNT_TYPE,
 
278
                                         g_param_spec_int ("account-type",
 
279
                                                           "Account Type",
 
280
                                                           "The account type for this user.",
 
281
                                                           ACT_USER_ACCOUNT_TYPE_STANDARD,
 
282
                                                           ACT_USER_ACCOUNT_TYPE_SUPERVISED,
 
283
                                                           ACT_USER_ACCOUNT_TYPE_STANDARD,
 
284
                                                           G_PARAM_READABLE));
 
285
        g_object_class_install_property (gobject_class,
 
286
                                         PROP_PASSWORD_MODE,
 
287
                                         g_param_spec_int ("password-mode",
 
288
                                                           "Password Mode",
 
289
                                                           "The password mode for this user.",
 
290
                                                           ACT_USER_PASSWORD_MODE_REGULAR,
 
291
                                                           ACT_USER_PASSWORD_MODE_NONE,
 
292
                                                           ACT_USER_PASSWORD_MODE_REGULAR,
 
293
                                                           G_PARAM_READABLE));
 
294
 
 
295
        g_object_class_install_property (gobject_class,
 
296
                                         PROP_PASSWORD_HINT,
 
297
                                         g_param_spec_string ("password-hint",
 
298
                                                              "Password Hint",
 
299
                                                              "Hint to help this user remember his password",
 
300
                                                              NULL,
 
301
                                                              G_PARAM_READABLE));
 
302
 
 
303
        g_object_class_install_property (gobject_class,
 
304
                                         PROP_UID,
 
305
                                         g_param_spec_int ("uid",
 
306
                                                           "User ID",
 
307
                                                           "The UID for this user.",
 
308
                                                           0, G_MAXINT, 0,
 
309
                                                           G_PARAM_READABLE));
 
310
        g_object_class_install_property (gobject_class,
 
311
                                         PROP_USER_NAME,
 
312
                                         g_param_spec_string ("user-name",
 
313
                                                              "User Name",
 
314
                                                              "The login name for this user.",
 
315
                                                              NULL,
 
316
                                                              G_PARAM_READABLE));
 
317
        g_object_class_install_property (gobject_class,
 
318
                                         PROP_HOME_DIR,
 
319
                                         g_param_spec_string ("home-directory",
 
320
                                                              "Home Directory",
 
321
                                                              "The home directory for this user.",
 
322
                                                              NULL,
 
323
                                                              G_PARAM_READABLE));
 
324
        g_object_class_install_property (gobject_class,
 
325
                                         PROP_SHELL,
 
326
                                         g_param_spec_string ("shell",
 
327
                                                              "Shell",
 
328
                                                              "The shell for this user.",
 
329
                                                              NULL,
 
330
                                                              G_PARAM_READABLE));
 
331
        g_object_class_install_property (gobject_class,
 
332
                                         PROP_EMAIL,
 
333
                                         g_param_spec_string ("email",
 
334
                                                              "Email",
 
335
                                                              "The email address for this user.",
 
336
                                                              NULL,
 
337
                                                              G_PARAM_READABLE));
 
338
        g_object_class_install_property (gobject_class,
 
339
                                         PROP_LOCATION,
 
340
                                         g_param_spec_string ("location",
 
341
                                                              "Location",
 
342
                                                              "The location of this user.",
 
343
                                                              NULL,
 
344
                                                              G_PARAM_READABLE));
 
345
        g_object_class_install_property (gobject_class,
 
346
                                         PROP_LOGIN_FREQUENCY,
 
347
                                         g_param_spec_int ("login-frequency",
 
348
                                                           "login frequency",
 
349
                                                           "login frequency",
 
350
                                                           0,
 
351
                                                           G_MAXINT,
 
352
                                                           0,
 
353
                                                           G_PARAM_READABLE));
 
354
        g_object_class_install_property (gobject_class,
 
355
                                         PROP_ICON_FILE,
 
356
                                         g_param_spec_string ("icon-file",
 
357
                                                              "Icon File",
 
358
                                                              "The path to an icon for this user.",
 
359
                                                              NULL,
 
360
                                                              G_PARAM_READABLE));
 
361
        g_object_class_install_property (gobject_class,
 
362
                                         PROP_LANGUAGE,
 
363
                                         g_param_spec_string ("language",
 
364
                                                              "Language",
 
365
                                                              "User's locale.",
 
366
                                                              NULL,
 
367
                                                              G_PARAM_READABLE));
 
368
        g_object_class_install_property (gobject_class,
 
369
                                         PROP_X_SESSION,
 
370
                                         g_param_spec_string ("x-session",
 
371
                                                              "X session",
 
372
                                                              "User's X session.",
 
373
                                                              NULL,
 
374
                                                              G_PARAM_READABLE));
 
375
        g_object_class_install_property (gobject_class,
 
376
                                         PROP_IS_LOADED,
 
377
                                         g_param_spec_boolean ("is-loaded",
 
378
                                                               NULL,
 
379
                                                               NULL,
 
380
                                                               FALSE,
 
381
                                                               G_PARAM_READABLE));
 
382
        g_object_class_install_property (gobject_class,
 
383
                                         PROP_LOCKED,
 
384
                                         g_param_spec_boolean ("locked",
 
385
                                                               "Locked",
 
386
                                                               "Locked",
 
387
                                                               FALSE,
 
388
                                                              G_PARAM_READABLE));
 
389
 
 
390
        g_object_class_install_property (gobject_class,
 
391
                                         PROP_AUTOMATIC_LOGIN,
 
392
                                         g_param_spec_boolean ("automatic-login",
 
393
                                                               "Automatic Login",
 
394
                                                               "Automatic Login",
 
395
                                                               FALSE,
 
396
                                                               G_PARAM_READABLE));
 
397
 
 
398
        signals [CHANGED] =
 
399
                g_signal_new ("changed",
 
400
                              G_TYPE_FROM_CLASS (class),
 
401
                              G_SIGNAL_RUN_LAST,
 
402
                              0,
 
403
                              NULL, NULL,
 
404
                              g_cclosure_marshal_VOID__VOID,
 
405
                              G_TYPE_NONE, 0);
 
406
        signals [SESSIONS_CHANGED] =
 
407
                g_signal_new ("sessions-changed",
 
408
                              G_TYPE_FROM_CLASS (class),
 
409
                              G_SIGNAL_RUN_LAST,
 
410
                              0,
 
411
                              NULL, NULL,
 
412
                              g_cclosure_marshal_VOID__VOID,
 
413
                              G_TYPE_NONE, 0);
 
414
}
 
415
 
 
416
static void
 
417
act_user_init (ActUser *user)
 
418
{
 
419
        GError *error;
 
420
 
 
421
        user->user_name = NULL;
 
422
        user->real_name = NULL;
 
423
        user->sessions = NULL;
 
424
 
 
425
        error = NULL;
 
426
        user->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 
427
        if (user->connection == NULL) {
 
428
                g_warning ("Couldn't connect to system bus: %s", error->message);
 
429
        }
 
430
}
 
431
 
 
432
static void
 
433
act_user_finalize (GObject *object)
 
434
{
 
435
        ActUser *user;
 
436
 
 
437
        user = ACT_USER (object);
 
438
 
 
439
        g_free (user->user_name);
 
440
        g_free (user->real_name);
 
441
        g_free (user->icon_file);
 
442
        g_free (user->language);
 
443
        g_free (user->object_path);
 
444
        g_free (user->password_hint);
 
445
        g_free (user->home_dir);
 
446
        g_free (user->shell);
 
447
        g_free (user->email);
 
448
        g_free (user->location);
 
449
        g_free (user->language);
 
450
 
 
451
        if (user->accounts_proxy != NULL) {
 
452
                g_object_unref (user->accounts_proxy);
 
453
        }
 
454
 
 
455
        if (user->object_proxy != NULL) {
 
456
                g_object_unref (user->object_proxy);
 
457
        }
 
458
 
 
459
        if (user->connection != NULL) {
 
460
                dbus_g_connection_unref (user->connection);
 
461
        }
 
462
 
 
463
        if (G_OBJECT_CLASS (act_user_parent_class)->finalize)
 
464
                (*G_OBJECT_CLASS (act_user_parent_class)->finalize) (object);
 
465
}
 
466
 
 
467
static void
 
468
set_is_loaded (ActUser  *user,
 
469
               gboolean  is_loaded)
 
470
{
 
471
        if (user->is_loaded != is_loaded) {
 
472
                user->is_loaded = is_loaded;
 
473
                g_object_notify (G_OBJECT (user), "is-loaded");
 
474
        }
 
475
}
 
476
 
 
477
/**
 
478
 * act_user_get_uid:
 
479
 * @user: the user object to examine.
 
480
 *
 
481
 * Retrieves the ID of @user.
 
482
 *
 
483
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
484
 *  freed, or %NULL.
 
485
 **/
 
486
 
 
487
uid_t
 
488
act_user_get_uid (ActUser *user)
 
489
{
 
490
        g_return_val_if_fail (ACT_IS_USER (user), -1);
 
491
 
 
492
        return user->uid;
 
493
}
 
494
 
 
495
/**
 
496
 * act_user_get_real_name:
 
497
 * @user: the user object to examine.
 
498
 *
 
499
 * Retrieves the display name of @user.
 
500
 *
 
501
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
502
 *  freed, or %NULL.
 
503
 **/
 
504
const char *
 
505
act_user_get_real_name (ActUser *user)
 
506
{
 
507
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
508
 
 
509
        return (user->real_name ? user->real_name : user->user_name);
 
510
}
 
511
 
 
512
/**
 
513
 * act_user_get_account_type:
 
514
 * @user: the user object to examine.
 
515
 *
 
516
 * Retrieves the account type of @user.
 
517
 *
 
518
 * Returns: a #ActUserAccountType
 
519
 **/
 
520
ActUserAccountType
 
521
act_user_get_account_type (ActUser *user)
 
522
{
 
523
        g_return_val_if_fail (ACT_IS_USER (user), ACT_USER_ACCOUNT_TYPE_STANDARD);
 
524
 
 
525
        return user->account_type;
 
526
}
 
527
 
 
528
/**
 
529
 * act_user_get_password_mode:
 
530
 * @user: the user object to examine.
 
531
 *
 
532
 * Retrieves the password mode of @user.
 
533
 *
 
534
 * Returns: a #ActUserPasswordMode
 
535
 **/
 
536
ActUserPasswordMode
 
537
act_user_get_password_mode (ActUser *user)
 
538
{
 
539
        g_return_val_if_fail (ACT_IS_USER (user), ACT_USER_PASSWORD_MODE_REGULAR);
 
540
 
 
541
        return user->password_mode;
 
542
}
 
543
 
 
544
/**
 
545
 * act_user_get_password_hint:
 
546
 * @user: the user object to examine.
 
547
 *
 
548
 * Retrieves the password hint set by @user.
 
549
 *
 
550
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
551
 *  freed, or %NULL.
 
552
 **/
 
553
const char *
 
554
act_user_get_password_hint (ActUser *user)
 
555
{
 
556
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
557
 
 
558
        return user->password_hint;
 
559
}
 
560
 
 
561
/**
 
562
 * act_user_get_home_dir:
 
563
 * @user: the user object to examine.
 
564
 *
 
565
 * Retrieves the home directory for @user.
 
566
 *
 
567
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
568
 *  freed, or %NULL.
 
569
 **/
 
570
const char *
 
571
act_user_get_home_dir (ActUser *user)
 
572
{
 
573
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
574
 
 
575
        return user->home_dir;
 
576
}
 
577
 
 
578
/**
 
579
 * act_user_get_shell:
 
580
 * @user: the user object to examine.
 
581
 *
 
582
 * Retrieves the shell assigned to @user.
 
583
 *
 
584
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
585
 *  freed, or %NULL.
 
586
 **/
 
587
const char *
 
588
act_user_get_shell (ActUser *user)
 
589
{
 
590
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
591
 
 
592
        return user->shell;
 
593
}
 
594
 
 
595
/**
 
596
 * act_user_get_email:
 
597
 * @user: the user object to examine.
 
598
 *
 
599
 * Retrieves the email address set by @user.
 
600
 *
 
601
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
602
 *  freed, or %NULL.
 
603
 **/
 
604
const char *
 
605
act_user_get_email (ActUser *user)
 
606
{
 
607
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
608
 
 
609
        return user->email;
 
610
}
 
611
 
 
612
/**
 
613
 * act_user_get_location:
 
614
 * @user: the user object to examine.
 
615
 *
 
616
 * Retrieves the location set by @user.
 
617
 *
 
618
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
619
 *  freed, or %NULL.
 
620
 **/
 
621
const char *
 
622
act_user_get_location (ActUser *user)
 
623
{
 
624
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
625
 
 
626
        return user->location;
 
627
}
 
628
 
 
629
/**
 
630
 * act_user_get_user_name:
 
631
 * @user: the user object to examine.
 
632
 *
 
633
 * Retrieves the login name of @user.
 
634
 *
 
635
 * Returns: (transfer none): a pointer to an array of characters which must not be modified or
 
636
 *  freed, or %NULL.
 
637
 **/
 
638
 
 
639
const char *
 
640
act_user_get_user_name (ActUser *user)
 
641
{
 
642
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
643
 
 
644
        return user->user_name;
 
645
}
 
646
 
 
647
/**
 
648
 * act_user_get_login_frequency:
 
649
 * @user: a #ActUser
 
650
 *
 
651
 * Returns the number of times @user has logged in.
 
652
 *
 
653
 * Returns: the login frequency
 
654
 */
 
655
int
 
656
act_user_get_login_frequency (ActUser *user)
 
657
{
 
658
        g_return_val_if_fail (ACT_IS_USER (user), 0);
 
659
 
 
660
        return user->login_frequency;
 
661
}
 
662
 
 
663
int
 
664
act_user_collate (ActUser *user1,
 
665
                  ActUser *user2)
 
666
{
 
667
        const char *str1;
 
668
        const char *str2;
 
669
        int         num1;
 
670
        int         num2;
 
671
        guint       len1;
 
672
        guint       len2;
 
673
 
 
674
        g_return_val_if_fail (ACT_IS_USER (user1), 0);
 
675
        g_return_val_if_fail (ACT_IS_USER (user2), 0);
 
676
 
 
677
        num1 = user1->login_frequency;
 
678
        num2 = user2->login_frequency;
 
679
 
 
680
        if (num1 > num2) {
 
681
                return -1;
 
682
        }
 
683
 
 
684
        if (num1 < num2) {
 
685
                return 1;
 
686
        }
 
687
 
 
688
 
 
689
        len1 = g_list_length (user1->sessions);
 
690
        len2 = g_list_length (user2->sessions);
 
691
 
 
692
        if (len1 > len2) {
 
693
                return -1;
 
694
        }
 
695
 
 
696
        if (len1 < len2) {
 
697
                return 1;
 
698
        }
 
699
 
 
700
        /* if login frequency is equal try names */
 
701
        if (user1->real_name != NULL) {
 
702
                str1 = user1->real_name;
 
703
        } else {
 
704
                str1 = user1->user_name;
 
705
        }
 
706
 
 
707
        if (user2->real_name != NULL) {
 
708
                str2 = user2->real_name;
 
709
        } else {
 
710
                str2 = user2->user_name;
 
711
        }
 
712
 
 
713
        if (str1 == NULL && str2 != NULL) {
 
714
                return -1;
 
715
        }
 
716
 
 
717
        if (str1 != NULL && str2 == NULL) {
 
718
                return 1;
 
719
        }
 
720
 
 
721
        if (str1 == NULL && str2 == NULL) {
 
722
                return 0;
 
723
        }
 
724
 
 
725
        return g_utf8_collate (str1, str2);
 
726
}
 
727
 
 
728
/**
 
729
 * act_user_is_logged_in:
 
730
 * @user: a #ActUser
 
731
 *
 
732
 * Returns whether or not #ActUser is currently logged in.
 
733
 *
 
734
 * Returns: %TRUE or %FALSE
 
735
 */
 
736
gboolean
 
737
act_user_is_logged_in (ActUser *user)
 
738
{
 
739
        return user->sessions != NULL;
 
740
}
 
741
 
 
742
/**
 
743
 * act_user_get_locked:
 
744
 * @user: a #ActUser
 
745
 *
 
746
 * Returns whether or not the #ActUser account is locked.
 
747
 *
 
748
 * Returns: %TRUE or %FALSE
 
749
 */
 
750
gboolean
 
751
act_user_get_locked (ActUser *user)
 
752
{
 
753
        return user->locked;;
 
754
}
 
755
 
 
756
/**
 
757
 * act_user_get_automatic_login:
 
758
 * @user: a #ActUser
 
759
 *
 
760
 * Returns whether or not #ActUser is automatically logged in at boot time.
 
761
 *
 
762
 * Returns: %TRUE or %FALSE
 
763
 */
 
764
gboolean
 
765
act_user_get_automatic_login (ActUser *user)
 
766
{
 
767
        return user->automatic_login;
 
768
}
 
769
 
 
770
/**
 
771
 * act_user_get_icon_file:
 
772
 * @user: a #ActUser
 
773
 *
 
774
 * Returns the path to the account icon belonging to @user.
 
775
 *
 
776
 * Returns: (transfer none): a path to an icon
 
777
 */
 
778
const char *
 
779
act_user_get_icon_file (ActUser *user)
 
780
{
 
781
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
782
 
 
783
        return user->icon_file;
 
784
}
 
785
 
 
786
/**
 
787
 * act_user_get_language:
 
788
 * @user: a #ActUser
 
789
 *
 
790
 * Returns the path to the configured locale of @user.
 
791
 *
 
792
 * Returns: (transfer none): a path to an icon
 
793
 */
 
794
const char *
 
795
act_user_get_language (ActUser *user)
 
796
{
 
797
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
798
 
 
799
        return user->language;
 
800
}
 
801
 
 
802
/**
 
803
 * act_user_get_x_session:
 
804
 * @user: a #ActUser
 
805
 *
 
806
 * Returns the path to the configured X session for @user.
 
807
 *
 
808
 * Returns: (transfer none): a path to an icon
 
809
 */
 
810
const char *
 
811
act_user_get_x_session (ActUser *user)
 
812
{
 
813
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
814
 
 
815
        return user->x_session;
 
816
}
 
817
 
 
818
/**
 
819
 * act_user_get_object_path:
 
820
 * @user: a #ActUser
 
821
 *
 
822
 * Returns the user accounts service object path of @user,
 
823
 * or %NULL if @user doesn't have an object path associated
 
824
 * with it.
 
825
 *
 
826
 * Returns: (transfer none): the primary ConsoleKit session id of the user
 
827
 */
 
828
const char *
 
829
act_user_get_object_path (ActUser *user)
 
830
{
 
831
        g_return_val_if_fail (ACT_IS_USER (user), NULL);
 
832
 
 
833
        return user->object_path;
 
834
}
 
835
 
 
836
/**
 
837
 * act_user_get_primary_session_id:
 
838
 * @user: a #ActUser
 
839
 *
 
840
 * Returns the primary ConsoleKit session id of @user, or %NULL if @user isn't
 
841
 * logged in.
 
842
 *
 
843
 * Returns: (transfer none): the primary ConsoleKit session id of the user
 
844
 */
 
845
const char *
 
846
act_user_get_primary_session_id (ActUser *user)
 
847
{
 
848
        if (!act_user_is_logged_in (user)) {
 
849
                g_debug ("User %s is not logged in, so has no primary session",
 
850
                         act_user_get_user_name (user));
 
851
                return NULL;
 
852
        }
 
853
 
 
854
        /* FIXME: better way to choose? */
 
855
        return user->sessions->data;
 
856
}
 
857
 
 
858
static void
 
859
collect_props (const gchar    *key,
 
860
               const GValue   *value,
 
861
               ActUser        *user)
 
862
{
 
863
        gboolean handled = TRUE;
 
864
 
 
865
        if (strcmp (key, "Uid") == 0) {
 
866
                guint64 new_uid;
 
867
 
 
868
                new_uid = g_value_get_uint64 (value);
 
869
                if ((guint64) user->uid != new_uid) {
 
870
                        user->uid = (uid_t) new_uid;
 
871
                        g_object_notify (G_OBJECT (user), "uid");
 
872
                }
 
873
        } else if (strcmp (key, "UserName") == 0) {
 
874
                const char *new_user_name;
 
875
 
 
876
                new_user_name = g_value_get_string (value);
 
877
                if (g_strcmp0 (user->user_name, new_user_name) != 0) {
 
878
                        g_free (user->user_name);
 
879
                        user->user_name = g_strdup (new_user_name);
 
880
                        g_object_notify (G_OBJECT (user), "user-name");
 
881
                }
 
882
        } else if (strcmp (key, "RealName") == 0) {
 
883
                const char *new_real_name;
 
884
 
 
885
                new_real_name = g_value_get_string (value);
 
886
                if (g_strcmp0 (user->real_name, new_real_name) != 0) {
 
887
                        g_free (user->real_name);
 
888
                        user->real_name = g_strdup (new_real_name);
 
889
                        g_object_notify (G_OBJECT (user), "real-name");
 
890
                }
 
891
        } else if (strcmp (key, "AccountType") == 0) {
 
892
                int new_account_type;
 
893
 
 
894
                new_account_type = g_value_get_int (value);
 
895
                if ((int) user->account_type != new_account_type) {
 
896
                        user->account_type = (ActUserAccountType) new_account_type;
 
897
                        g_object_notify (G_OBJECT (user), "account-type");
 
898
                }
 
899
        } else if (strcmp (key, "PasswordMode") == 0) {
 
900
                int new_password_mode;
 
901
 
 
902
                new_password_mode = g_value_get_int (value);
 
903
                if ((int) user->password_mode != new_password_mode) {
 
904
                        user->password_mode = (ActUserPasswordMode) new_password_mode;
 
905
                        g_object_notify (G_OBJECT (user), "password-mode");
 
906
                }
 
907
        } else if (strcmp (key, "PasswordHint") == 0) {
 
908
                const char *new_password_hint;
 
909
 
 
910
                new_password_hint = g_value_get_string (value);
 
911
                if (g_strcmp0 (user->password_hint, new_password_hint) != 0) {
 
912
                        g_free (user->password_hint);
 
913
                        user->password_hint = g_value_dup_string (value);
 
914
                        g_object_notify (G_OBJECT (user), "password-hint");
 
915
                }
 
916
        } else if (strcmp (key, "HomeDirectory") == 0) {
 
917
                const char *new_home_dir;
 
918
 
 
919
                new_home_dir = g_value_get_string (value);
 
920
                if (g_strcmp0 (user->home_dir, new_home_dir) != 0) {
 
921
                        g_free (user->home_dir);
 
922
                        user->home_dir = g_strdup (new_home_dir);
 
923
                        g_object_notify (G_OBJECT (user), "home-directory");
 
924
                }
 
925
        } else if (strcmp (key, "Shell") == 0) {
 
926
                const char *new_shell;
 
927
 
 
928
                new_shell = g_value_get_string (value);
 
929
                if (g_strcmp0 (user->shell, new_shell) != 0) {
 
930
                        g_free (user->shell);
 
931
                        user->shell = g_strdup (new_shell);
 
932
                        g_object_notify (G_OBJECT (user), "shell");
 
933
                }
 
934
        } else if (strcmp (key, "Email") == 0) {
 
935
                const char *new_email;
 
936
 
 
937
                new_email = g_value_get_string (value);
 
938
                if (g_strcmp0 (user->email, new_email) != 0) {
 
939
                        g_free (user->email);
 
940
                        user->email = g_strdup (new_email);
 
941
                        g_object_notify (G_OBJECT (user), "email");
 
942
                }
 
943
        } else if (strcmp (key, "Location") == 0) {
 
944
                const char *new_location;
 
945
 
 
946
                new_location = g_value_get_string (value);
 
947
                if (g_strcmp0 (user->location, new_location) != 0) {
 
948
                        g_free (user->location);
 
949
                        user->location = g_strdup (new_location);
 
950
                        g_object_notify (G_OBJECT (user), "location");
 
951
                }
 
952
        } else if (strcmp (key, "Locked") == 0) {
 
953
                gboolean new_locked_state;
 
954
 
 
955
                new_locked_state = g_value_get_boolean (value);
 
956
                if (new_locked_state != user->locked) {
 
957
                        user->locked = new_locked_state;
 
958
                        g_object_notify (G_OBJECT (user), "locked");
 
959
                }
 
960
        } else if (strcmp (key, "AutomaticLogin") == 0) {
 
961
                gboolean new_automatic_login_state;
 
962
 
 
963
                new_automatic_login_state = g_value_get_boolean (value);
 
964
                if (new_automatic_login_state != user->automatic_login) {
 
965
                        user->automatic_login = new_automatic_login_state;
 
966
                        g_object_notify (G_OBJECT (user), "automatic-login");
 
967
                }
 
968
        } else if (strcmp (key, "LoginFrequency") == 0) {
 
969
                int new_login_frequency;
 
970
 
 
971
                new_login_frequency = (int) g_value_get_uint64 (value);
 
972
                if ((int) user->login_frequency != (int) new_login_frequency) {
 
973
                        user->login_frequency = new_login_frequency;
 
974
                        g_object_notify (G_OBJECT (user), "login-frequency");
 
975
                }
 
976
        } else if (strcmp (key, "IconFile") == 0) {
 
977
                const char *new_icon_file;
 
978
 
 
979
                new_icon_file = g_value_get_string (value);
 
980
                if (g_strcmp0 (user->icon_file, new_icon_file) != 0) {
 
981
                        g_free (user->icon_file);
 
982
                        user->icon_file = g_value_dup_string (value);
 
983
                        g_object_notify (G_OBJECT (user), "icon-file");
 
984
                }
 
985
        } else if (strcmp (key, "Language") == 0) {
 
986
                const char *new_language;
 
987
 
 
988
                new_language = g_value_get_string (value);
 
989
                if (g_strcmp0 (user->language, new_language) != 0) {
 
990
                        g_free (user->language);
 
991
                        user->language = g_value_dup_string (value);
 
992
                        g_object_notify (G_OBJECT (user), "language");
 
993
                }
 
994
        } else if (strcmp (key, "XSession") == 0) {
 
995
                const char *new_x_session;
 
996
 
 
997
                new_x_session = g_value_get_string (value);
 
998
                if (g_strcmp0 (user->x_session, new_x_session) != 0) {
 
999
                        g_free (user->x_session);
 
1000
                        user->x_session = g_value_dup_string (value);
 
1001
                        g_object_notify (G_OBJECT (user), "x-session");
 
1002
                }
 
1003
        } else {
 
1004
                handled = FALSE;
 
1005
        }
 
1006
 
 
1007
        if (!handled) {
 
1008
                g_debug ("unhandled property %s", key);
 
1009
        }
 
1010
}
 
1011
 
 
1012
static void
 
1013
on_get_all_finished (DBusGProxy     *proxy,
 
1014
                     DBusGProxyCall *call,
 
1015
                     ActUser        *user)
 
1016
{
 
1017
        GError      *error;
 
1018
        GHashTable  *hash_table;
 
1019
        gboolean     res;
 
1020
 
 
1021
        g_assert (user->get_all_call == call);
 
1022
        g_assert (user->object_proxy == proxy);
 
1023
 
 
1024
        error = NULL;
 
1025
        res = dbus_g_proxy_end_call (proxy,
 
1026
                                     call,
 
1027
                                     &error,
 
1028
                                     dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
 
1029
                                     &hash_table,
 
1030
                                     G_TYPE_INVALID);
 
1031
        user->get_all_call = NULL;
 
1032
        user->object_proxy = NULL;
 
1033
 
 
1034
        if (! res) {
 
1035
                g_debug ("Error calling GetAll() when retrieving properties for %s: %s",
 
1036
                         user->object_path, error->message);
 
1037
                g_error_free (error);
 
1038
                goto out;
 
1039
        }
 
1040
        g_hash_table_foreach (hash_table, (GHFunc) collect_props, user);
 
1041
        g_hash_table_unref (hash_table);
 
1042
 
 
1043
        if (!user->is_loaded) {
 
1044
                set_is_loaded (user, TRUE);
 
1045
        }
 
1046
 
 
1047
        g_signal_emit (user, signals[CHANGED], 0);
 
1048
 
 
1049
out:
 
1050
        g_object_unref (proxy);
 
1051
}
 
1052
 
 
1053
static gboolean
 
1054
update_info (ActUser *user)
 
1055
{
 
1056
        DBusGProxy     *proxy;
 
1057
        DBusGProxyCall *call;
 
1058
 
 
1059
        proxy = dbus_g_proxy_new_for_name (user->connection,
 
1060
                                           ACCOUNTS_NAME,
 
1061
                                           user->object_path,
 
1062
                                           DBUS_INTERFACE_PROPERTIES);
 
1063
 
 
1064
        call = dbus_g_proxy_begin_call (proxy,
 
1065
                                        "GetAll",
 
1066
                                        (DBusGProxyCallNotify)
 
1067
                                        on_get_all_finished,
 
1068
                                        user,
 
1069
                                        NULL,
 
1070
                                        G_TYPE_STRING,
 
1071
                                        ACCOUNTS_USER_INTERFACE,
 
1072
                                        G_TYPE_INVALID);
 
1073
 
 
1074
        if (call == NULL) {
 
1075
                g_warning ("ActUser: failed to make GetAll call");
 
1076
                goto failed;
 
1077
        }
 
1078
 
 
1079
        user->get_all_call = call;
 
1080
        user->object_proxy = proxy;
 
1081
        return TRUE;
 
1082
 
 
1083
failed:
 
1084
        if (proxy != NULL) {
 
1085
                g_object_unref (proxy);
 
1086
        }
 
1087
 
 
1088
        return FALSE;
 
1089
}
 
1090
 
 
1091
static void
 
1092
changed_handler (DBusGProxy *proxy,
 
1093
                 gpointer   *data)
 
1094
{
 
1095
        ActUser *user = ACT_USER (data);
 
1096
 
 
1097
        update_info (user);
 
1098
}
 
1099
 
 
1100
/**
 
1101
 * _act_user_update_from_object_path:
 
1102
 * @user: the user object to update.
 
1103
 * @object_path: the object path of the user to use.
 
1104
 *
 
1105
 * Updates the properties of @user from the accounts service via
 
1106
 * the object path in @object_path.
 
1107
 **/
 
1108
void
 
1109
_act_user_update_from_object_path (ActUser    *user,
 
1110
                                   const char *object_path)
 
1111
{
 
1112
        g_return_if_fail (ACT_IS_USER (user));
 
1113
        g_return_if_fail (object_path != NULL);
 
1114
        g_return_if_fail (user->object_path == NULL);
 
1115
 
 
1116
        user->object_path = g_strdup (object_path);
 
1117
 
 
1118
        user->accounts_proxy = dbus_g_proxy_new_for_name (user->connection,
 
1119
                                                          ACCOUNTS_NAME,
 
1120
                                                          user->object_path,
 
1121
                                                          ACCOUNTS_USER_INTERFACE);
 
1122
        dbus_g_proxy_set_default_timeout (user->accounts_proxy, INT_MAX);
 
1123
        dbus_g_proxy_add_signal (user->accounts_proxy, "Changed", G_TYPE_INVALID);
 
1124
 
 
1125
        dbus_g_proxy_connect_signal (user->accounts_proxy, "Changed",
 
1126
                                     G_CALLBACK (changed_handler), user, NULL);
 
1127
 
 
1128
        if (!update_info (user)) {
 
1129
                g_warning ("Couldn't update info for user with object path %s", object_path);
 
1130
        }
 
1131
}
 
1132
 
 
1133
void
 
1134
_act_user_update_login_frequency (ActUser    *user,
 
1135
                                  int         login_frequency)
 
1136
{
 
1137
        if (user->login_frequency != login_frequency) {
 
1138
                user->login_frequency = login_frequency;
 
1139
                g_object_notify (G_OBJECT (user), "login-frequency");
 
1140
        }
 
1141
}
 
1142
 
 
1143
/**
 
1144
 * act_user_is_loaded:
 
1145
 * @user: a #ActUser
 
1146
 *
 
1147
 * Determines whether or not the user object is loaded and ready to read from.
 
1148
 * #ActUserManager:is-loaded property must be %TRUE before calling
 
1149
 * act_user_manager_list_users()
 
1150
 *
 
1151
 * Returns: %TRUE or %FALSE
 
1152
 */
 
1153
gboolean
 
1154
act_user_is_loaded (ActUser *user)
 
1155
{
 
1156
        return user->is_loaded;
 
1157
}
 
1158
 
 
1159
/**
 
1160
 * act_user_set_email:
 
1161
 * @user: the user object to alter.
 
1162
 * @email: an email address
 
1163
 *
 
1164
 * Assigns a new email to @user.
 
1165
 *
 
1166
 * Note this function is synchronous and ignores errors.
 
1167
 **/
 
1168
void
 
1169
act_user_set_email (ActUser    *user,
 
1170
                    const char *email)
 
1171
{
 
1172
        GError *error = NULL;
 
1173
 
 
1174
        g_return_if_fail (ACT_IS_USER (user));
 
1175
        g_return_if_fail (email != NULL);
 
1176
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1177
 
 
1178
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1179
                                "SetEmail",
 
1180
                                &error,
 
1181
                                G_TYPE_STRING, email,
 
1182
                                G_TYPE_INVALID,
 
1183
                                G_TYPE_INVALID)) {
 
1184
                g_warning ("SetEmail call failed: %s", error->message);
 
1185
                g_error_free (error);
 
1186
                return;
 
1187
        }
 
1188
}
 
1189
 
 
1190
/**
 
1191
 * act_user_set_language:
 
1192
 * @user: the user object to alter.
 
1193
 * @language: a locale (e.g. en_US.utf8)
 
1194
 *
 
1195
 * Assigns a new locale for @user.
 
1196
 *
 
1197
 * Note this function is synchronous and ignores errors.
 
1198
 **/
 
1199
void
 
1200
act_user_set_language (ActUser    *user,
 
1201
                       const char *language)
 
1202
{
 
1203
        GError *error = NULL;
 
1204
 
 
1205
        g_return_if_fail (ACT_IS_USER (user));
 
1206
        g_return_if_fail (language != NULL);
 
1207
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1208
 
 
1209
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1210
                                "SetLanguage",
 
1211
                                &error,
 
1212
                                G_TYPE_STRING, language,
 
1213
                                G_TYPE_INVALID,
 
1214
                                G_TYPE_INVALID)) {
 
1215
                g_warning ("SetLanguage call failed: %s", error->message);
 
1216
                g_error_free (error);
 
1217
                return;
 
1218
        }
 
1219
}
 
1220
 
 
1221
/**
 
1222
 * act_user_set_x_session:
 
1223
 * @user: the user object to alter.
 
1224
 * @x_session: an x session (e.g. gnome)
 
1225
 *
 
1226
 * Assigns a new x session for @user.
 
1227
 *
 
1228
 * Note this function is synchronous and ignores errors.
 
1229
 **/
 
1230
void
 
1231
act_user_set_x_session (ActUser    *user,
 
1232
                        const char *x_session)
 
1233
{
 
1234
        GError *error = NULL;
 
1235
 
 
1236
        g_return_if_fail (ACT_IS_USER (user));
 
1237
        g_return_if_fail (x_session != NULL);
 
1238
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1239
 
 
1240
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1241
                                "SetXSession",
 
1242
                                &error,
 
1243
                                G_TYPE_STRING, x_session,
 
1244
                                G_TYPE_INVALID,
 
1245
                                G_TYPE_INVALID)) {
 
1246
                g_warning ("SetXSession call failed: %s", error->message);
 
1247
                g_error_free (error);
 
1248
                return;
 
1249
        }
 
1250
}
 
1251
 
 
1252
 
 
1253
/**
 
1254
 * act_user_set_location:
 
1255
 * @user: the user object to alter.
 
1256
 * @location: a location
 
1257
 *
 
1258
 * Assigns a new location for @user.
 
1259
 *
 
1260
 * Note this function is synchronous and ignores errors.
 
1261
 **/
 
1262
void
 
1263
act_user_set_location (ActUser    *user,
 
1264
                       const char *location)
 
1265
{
 
1266
        GError *error = NULL;
 
1267
 
 
1268
        g_return_if_fail (ACT_IS_USER (user));
 
1269
        g_return_if_fail (location != NULL);
 
1270
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1271
 
 
1272
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1273
                                "SetLocation",
 
1274
                                &error,
 
1275
                                G_TYPE_STRING, location,
 
1276
                                G_TYPE_INVALID,
 
1277
                                G_TYPE_INVALID)) {
 
1278
                g_warning ("SetLocation call failed: %s", error->message);
 
1279
                g_error_free (error);
 
1280
                return;
 
1281
        }
 
1282
}
 
1283
 
 
1284
/**
 
1285
 * act_user_set_user_name:
 
1286
 * @user: the user object to alter.
 
1287
 * @user_name: a new user name
 
1288
 *
 
1289
 * Assigns a new username for @user.
 
1290
 *
 
1291
 * Note this function is synchronous and ignores errors.
 
1292
 **/
 
1293
void
 
1294
act_user_set_user_name (ActUser    *user,
 
1295
                        const char *user_name)
 
1296
{
 
1297
        GError *error = NULL;
 
1298
 
 
1299
        g_return_if_fail (ACT_IS_USER (user));
 
1300
        g_return_if_fail (user_name != NULL);
 
1301
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1302
 
 
1303
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1304
                                "SetUserName",
 
1305
                                &error,
 
1306
                                G_TYPE_STRING, user_name,
 
1307
                                G_TYPE_INVALID,
 
1308
                                G_TYPE_INVALID)) {
 
1309
                g_warning ("SetUserName call failed: %s", error->message);
 
1310
                g_error_free (error);
 
1311
                return;
 
1312
        }
 
1313
}
 
1314
 
 
1315
/**
 
1316
 * act_user_set_real_name:
 
1317
 * @user: the user object to alter.
 
1318
 * @real_name: a new name
 
1319
 *
 
1320
 * Assigns a new name for @user.
 
1321
 *
 
1322
 * Note this function is synchronous and ignores errors.
 
1323
 **/
 
1324
void
 
1325
act_user_set_real_name (ActUser    *user,
 
1326
                        const char *real_name)
 
1327
{
 
1328
        GError *error = NULL;
 
1329
 
 
1330
        g_return_if_fail (ACT_IS_USER (user));
 
1331
        g_return_if_fail (real_name != NULL);
 
1332
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1333
 
 
1334
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1335
                                "SetRealName",
 
1336
                                &error,
 
1337
                                G_TYPE_STRING, real_name,
 
1338
                                G_TYPE_INVALID,
 
1339
                                G_TYPE_INVALID)) {
 
1340
                g_warning ("SetRealName call failed: %s", error->message);
 
1341
                g_error_free (error);
 
1342
                return;
 
1343
        }
 
1344
}
 
1345
 
 
1346
/**
 
1347
 * act_user_set_icon_file:
 
1348
 * @user: the user object to alter.
 
1349
 * @icon_file: path to an icon
 
1350
 *
 
1351
 * Assigns a new icon for @user.
 
1352
 *
 
1353
 * Note this function is synchronous and ignores errors.
 
1354
 **/
 
1355
void
 
1356
act_user_set_icon_file (ActUser    *user,
 
1357
                        const char *icon_file)
 
1358
{
 
1359
        GError *error = NULL;
 
1360
 
 
1361
        g_return_if_fail (ACT_IS_USER (user));
 
1362
        g_return_if_fail (icon_file != NULL);
 
1363
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1364
 
 
1365
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1366
                                "SetIconFile",
 
1367
                                &error,
 
1368
                                G_TYPE_STRING, icon_file,
 
1369
                                G_TYPE_INVALID,
 
1370
                                G_TYPE_INVALID)) {
 
1371
                g_warning ("SetIconFile call failed: %s", error->message);
 
1372
                g_error_free (error);
 
1373
                return;
 
1374
        }
 
1375
}
 
1376
 
 
1377
/**
 
1378
 * act_user_set_account_type:
 
1379
 * @user: the user object to alter.
 
1380
 * @account_type: a #ActUserAccountType
 
1381
 *
 
1382
 * Changes the account type of @user.
 
1383
 *
 
1384
 * Note this function is synchronous and ignores errors.
 
1385
 **/
 
1386
void
 
1387
act_user_set_account_type (ActUser            *user,
 
1388
                           ActUserAccountType  account_type)
 
1389
{
 
1390
        GError *error = NULL;
 
1391
 
 
1392
        g_return_if_fail (ACT_IS_USER (user));
 
1393
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1394
 
 
1395
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1396
                                "SetAccountType",
 
1397
                                &error,
 
1398
                                G_TYPE_INT, account_type,
 
1399
                                G_TYPE_INVALID,
 
1400
                                G_TYPE_INVALID)) {
 
1401
                g_warning ("SetAccountType call failed: %s", error->message);
 
1402
                g_error_free (error);
 
1403
                return;
 
1404
        }
 
1405
}
 
1406
 
 
1407
static gchar
 
1408
salt_char (GRand *rand)
 
1409
{
 
1410
        gchar salt[] = "ABCDEFGHIJKLMNOPQRSTUVXYZ"
 
1411
                       "abcdefghijklmnopqrstuvxyz"
 
1412
                       "./0123456789";
 
1413
 
 
1414
        return salt[g_rand_int_range (rand, 0, G_N_ELEMENTS (salt))];
 
1415
}
 
1416
 
 
1417
static gchar *
 
1418
make_crypted (const gchar *plain)
 
1419
{
 
1420
        GString *salt;
 
1421
        gchar *result;
 
1422
        GRand *rand;
 
1423
        gint i;
 
1424
 
 
1425
        rand = g_rand_new ();
 
1426
        salt = g_string_sized_new (21);
 
1427
 
 
1428
        /* SHA 256 */
 
1429
        g_string_append (salt, "$6$");
 
1430
        for (i = 0; i < 16; i++) {
 
1431
                g_string_append_c (salt, salt_char (rand));
 
1432
        }
 
1433
        g_string_append_c (salt, '$');
 
1434
 
 
1435
        result = g_strdup (crypt (plain, salt->str));
 
1436
 
 
1437
        g_string_free (salt, TRUE);
 
1438
        g_rand_free (rand);
 
1439
 
 
1440
        return result;
 
1441
}
 
1442
 
 
1443
/**
 
1444
 * act_user_set_password:
 
1445
 * @user: the user object to alter.
 
1446
 * @password: a password
 
1447
 * @hint: a hint to help user recall password
 
1448
 *
 
1449
 * Changes the password of @user to @password.
 
1450
 * @hint is displayed to the user if they forget the password.
 
1451
 *
 
1452
 * Note this function is synchronous and ignores errors.
 
1453
 **/
 
1454
void
 
1455
act_user_set_password (ActUser             *user,
 
1456
                       const gchar         *password,
 
1457
                       const gchar         *hint)
 
1458
{
 
1459
        GError *error = NULL;
 
1460
        gchar *crypted;
 
1461
 
 
1462
        g_return_if_fail (ACT_IS_USER (user));
 
1463
        g_return_if_fail (password != NULL);
 
1464
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1465
 
 
1466
        crypted = make_crypted (password);
 
1467
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1468
                                "SetPassword",
 
1469
                                &error,
 
1470
                                G_TYPE_STRING, crypted,
 
1471
                                G_TYPE_STRING, hint,
 
1472
                                G_TYPE_INVALID,
 
1473
                                G_TYPE_INVALID)) {
 
1474
                g_warning ("SetPassword call failed: %s", error->message);
 
1475
                g_error_free (error);
 
1476
        }
 
1477
        memset (crypted, 0, strlen (crypted));
 
1478
        g_free (crypted);
 
1479
}
 
1480
 
 
1481
/**
 
1482
 * act_user_set_password_mode:
 
1483
 * @user: the user object to alter.
 
1484
 * @password_mode: a #ActUserPasswordMode
 
1485
 *
 
1486
 * Changes the password of @user.  If @password_mode is
 
1487
 * ACT_USER_PASSWORD_MODE_SET_AT_LOGIN then the user will
 
1488
 * be asked for a new password at the next login.  If @password_mode
 
1489
 * is ACT_USER_PASSWORD_MODE_NONE then the user will not require
 
1490
 * a password to log in.
 
1491
 *
 
1492
 * Note this function is synchronous and ignores errors.
 
1493
 **/
 
1494
void
 
1495
act_user_set_password_mode (ActUser             *user,
 
1496
                            ActUserPasswordMode  password_mode)
 
1497
{
 
1498
        GError *error = NULL;
 
1499
 
 
1500
        g_return_if_fail (ACT_IS_USER (user));
 
1501
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1502
 
 
1503
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1504
                                "SetPasswordMode",
 
1505
                                &error,
 
1506
                                G_TYPE_INT, (int) password_mode,
 
1507
                                G_TYPE_INVALID,
 
1508
                                G_TYPE_INVALID)) {
 
1509
                g_warning ("SetPasswordMode call failed: %s", error->message);
 
1510
                g_error_free (error);
 
1511
        }
 
1512
}
 
1513
 
 
1514
/**
 
1515
 * act_user_set_locked:
 
1516
 * @user: the user object to alter.
 
1517
 * @locked: whether or not the account is locked
 
1518
 *
 
1519
 * Note this function is synchronous and ignores errors.
 
1520
 **/
 
1521
void
 
1522
act_user_set_locked (ActUser  *user,
 
1523
                     gboolean  locked)
 
1524
{
 
1525
        GError *error = NULL;
 
1526
 
 
1527
        g_return_if_fail (ACT_IS_USER (user));
 
1528
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1529
 
 
1530
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1531
                                "SetLocked",
 
1532
                                &error,
 
1533
                                G_TYPE_BOOLEAN, locked,
 
1534
                                G_TYPE_INVALID,
 
1535
                                G_TYPE_INVALID)) {
 
1536
                g_warning ("SetLocked call failed: %s", error->message);
 
1537
                g_error_free (error);
 
1538
        }
 
1539
}
 
1540
 
 
1541
/**
 
1542
 * act_user_set_automatic_login:
 
1543
 * @enabled: whether or not to autologin for user.
 
1544
 *
 
1545
 * If enabled is set to %TRUE then this user will automatically be logged in
 
1546
 * at boot up time.  Only one user can be configured to auto login at any given
 
1547
 * time, so subsequent calls to act_user_set_automatic_login() override previous
 
1548
 * calls.
 
1549
 *
 
1550
 * Note this function is synchronous and ignores errors.
 
1551
 **/
 
1552
void
 
1553
act_user_set_automatic_login (ActUser   *user,
 
1554
                              gboolean  enabled)
 
1555
{
 
1556
        GError *error = NULL;
 
1557
 
 
1558
        g_return_if_fail (ACT_IS_USER (user));
 
1559
        g_return_if_fail (DBUS_IS_G_PROXY (user->accounts_proxy));
 
1560
 
 
1561
        if (!dbus_g_proxy_call (user->accounts_proxy,
 
1562
                                "SetAutomaticLogin",
 
1563
                                &error,
 
1564
                                G_TYPE_BOOLEAN, enabled,
 
1565
                                G_TYPE_INVALID,
 
1566
                                G_TYPE_INVALID)) {
 
1567
                g_warning ("SetAutomaticLogin call failed: %s", error->message);
 
1568
                g_error_free (error);
 
1569
        }
 
1570
}