~lightdm-team/lightdm/1.4

66 by robert.ancell at gmail
Make UserInfo a GObject
1
/*
2
 * Copyright (C) 2010 Robert Ancell.
3
 * Author: Robert Ancell <robert.ancell@canonical.com>
4
 * 
190 by robert.ancell at gmail
Change libldmgreeter library from GPL to LGPL
5
 * This library is free software; you can redistribute it and/or modify it under
6
 * the terms of the GNU Lesser General Public License as published by the Free
7
 * Software Foundation; either version 3 of the License, or (at your option) any
8
 * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
66 by robert.ancell at gmail
Make UserInfo a GObject
9
 * license.
10
 */
11
432 by Robert Ancell
Start liblightdm-gobject understanding user changes
12
#include "user-private.h"
66 by robert.ancell at gmail
Make UserInfo a GObject
13
14
enum {
15
    PROP_0,
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
16
    PROP_GREETER,
66 by robert.ancell at gmail
Make UserInfo a GObject
17
    PROP_NAME,
18
    PROP_REAL_NAME,
19
    PROP_DISPLAY_NAME,
437 by Robert Ancell
Move user manager into liblightdm
20
    PROP_HOME_DIRECTORY,
66 by robert.ancell at gmail
Make UserInfo a GObject
21
    PROP_IMAGE,
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
22
    PROP_LANGUAGE,
23
    PROP_LAYOUT,
24
    PROP_SESSION,
66 by robert.ancell at gmail
Make UserInfo a GObject
25
    PROP_LOGGED_IN
26
};
27
84 by robert.ancell at gmail
Fix up documentation and tweak UI
28
struct _LdmUserPrivate
66 by robert.ancell at gmail
Make UserInfo a GObject
29
{
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
30
    LdmGreeter *greeter;
31
66 by robert.ancell at gmail
Make UserInfo a GObject
32
    gchar *name;
33
    gchar *real_name;
437 by Robert Ancell
Move user manager into liblightdm
34
    gchar *home_directory;
66 by robert.ancell at gmail
Make UserInfo a GObject
35
    gchar *image;
36
    gboolean logged_in;
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
37
38
    gboolean have_defaults;
39
    gchar *language;
40
    gchar *layout;
41
    gchar *session;
66 by robert.ancell at gmail
Make UserInfo a GObject
42
};
43
44
G_DEFINE_TYPE (LdmUser, ldm_user, G_TYPE_OBJECT);
45
84 by robert.ancell at gmail
Fix up documentation and tweak UI
46
/**
47
 * ldm_user_new:
48
 * 
49
 * Create a new user.
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
50
 * @greeter: The greeter the user is connected to
84 by robert.ancell at gmail
Fix up documentation and tweak UI
51
 * @name: The username
52
 * @real_name: The real name of the user
437 by Robert Ancell
Move user manager into liblightdm
53
 * @home_directory: The home directory of the user
84 by robert.ancell at gmail
Fix up documentation and tweak UI
54
 * @image: The image URI
55
 * @logged_in: TRUE if this user is currently logged in
56
 * 
57
 * Return value: the new #LdmUser
58
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
59
LdmUser *
437 by Robert Ancell
Move user manager into liblightdm
60
ldm_user_new (LdmGreeter *greeter, const gchar *name, const gchar *real_name, const gchar *home_directory, const gchar *image, gboolean logged_in)
66 by robert.ancell at gmail
Make UserInfo a GObject
61
{
437 by Robert Ancell
Move user manager into liblightdm
62
    return g_object_new (LDM_TYPE_USER, "greeter", greeter, "name", name, "real-name", real_name, "home-directory", home_directory, "image", image, "logged-in", logged_in, NULL);
66 by robert.ancell at gmail
Make UserInfo a GObject
63
}
64
438 by Robert Ancell
Tidy up user updating code
65
gboolean
66
ldm_user_update (LdmUser *user, const gchar *real_name, const gchar *home_directory, const gchar *image, gboolean logged_in)
67
{
68
    if (g_strcmp0 (user->priv->real_name, real_name) != 0 ||
69
        g_strcmp0 (user->priv->home_directory, home_directory) != 0 ||
70
        g_strcmp0 (user->priv->image, image) != 0 ||
71
        user->priv->logged_in != logged_in)
72
        return FALSE;
73
  
74
    g_free (user->priv->real_name);
75
    user->priv->real_name = g_strdup (real_name);
76
    g_free (user->priv->home_directory);
77
    user->priv->home_directory = g_strdup (home_directory);
78
    g_free (user->priv->image);
79
    user->priv->image = g_strdup (image);
80
    user->priv->logged_in = logged_in;
81
82
    return TRUE;
83
}
84
84 by robert.ancell at gmail
Fix up documentation and tweak UI
85
/**
86
 * ldm_user_get_name:
87
 * @user: A #LdmUser
88
 * 
89
 * Get the name of a user.
90
 * 
91
 * Return value: The name of the given user
92
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
93
const gchar *
94
ldm_user_get_name (LdmUser *user)
95
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
96
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
66 by robert.ancell at gmail
Make UserInfo a GObject
97
    return user->priv->name;
98
}
99
437 by Robert Ancell
Move user manager into liblightdm
100
void
101
ldm_user_set_name (LdmUser *user, const gchar *name)
102
{
103
    g_return_if_fail (LDM_IS_USER (user));
104
    g_free (user->priv->name);
105
    user->priv->name = g_strdup (name);
106
}
107
84 by robert.ancell at gmail
Fix up documentation and tweak UI
108
/**
109
 * ldm_user_get_real_name:
110
 * @user: A #LdmUser
111
 * 
112
 * Get the real name of a user.
113
 *
114
 * Return value: The real name of the given user (may be blank)
115
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
116
const gchar *
117
ldm_user_get_real_name (LdmUser *user)
118
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
119
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
66 by robert.ancell at gmail
Make UserInfo a GObject
120
    return user->priv->real_name;
121
}
122
432 by Robert Ancell
Start liblightdm-gobject understanding user changes
123
void
124
ldm_user_set_real_name (LdmUser *user, const gchar *real_name)
125
{
126
    g_return_if_fail (LDM_IS_USER (user));
127
    g_free (user->priv->real_name);
128
    user->priv->real_name = g_strdup (real_name);
129
}
130
84 by robert.ancell at gmail
Fix up documentation and tweak UI
131
/**
132
 * ldm_user_get_display_name:
133
 * @user: A #LdmUser
134
 * 
135
 * Get the display name of a user.
136
 * 
137
 * Return value: The display name of the given user
138
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
139
const gchar *
140
ldm_user_get_display_name (LdmUser *user)
141
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
142
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
143
437 by Robert Ancell
Move user manager into liblightdm
144
    if (user->priv->real_name)
66 by robert.ancell at gmail
Make UserInfo a GObject
145
        return user->priv->real_name;
146
    else
147
        return user->priv->name;
148
}
149
84 by robert.ancell at gmail
Fix up documentation and tweak UI
150
/**
437 by Robert Ancell
Move user manager into liblightdm
151
 * ldm_user_get_home_directory:
152
 * @user: A #LdmUser
153
 * 
154
 * Get the home directory for a user.
155
 * 
156
 * Return value: The users home directory
157
 */
158
const gchar *
159
ldm_user_get_home_directory (LdmUser *user)
160
{
161
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
162
    return user->priv->home_directory;
163
}
164
165
void
166
ldm_user_set_home_directory (LdmUser *user, const gchar *home_directory)
167
{
168
    g_return_if_fail (LDM_IS_USER (user));
169
    g_free (user->priv->home_directory);
170
    user->priv->home_directory = g_strdup (home_directory);
171
}
172
173
/**
84 by robert.ancell at gmail
Fix up documentation and tweak UI
174
 * ldm_user_get_image:
175
 * @user: A #LdmUser
176
 * 
177
 * Get the image URI for a user.
178
 * 
179
 * Return value: The image URI for the given user or NULL if no URI
180
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
181
const gchar *
182
ldm_user_get_image (LdmUser *user)
183
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
184
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
66 by robert.ancell at gmail
Make UserInfo a GObject
185
    return user->priv->image;
186
}
187
432 by Robert Ancell
Start liblightdm-gobject understanding user changes
188
void
189
ldm_user_set_image (LdmUser *user, const gchar *image)
190
{
191
    g_return_if_fail (LDM_IS_USER (user));
192
    g_free (user->priv->image);
193
    user->priv->image = g_strdup (image);
194
}
195
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
196
static void
197
get_defaults (LdmUser *user)
198
{
199
    if (user->priv->have_defaults)
200
        return;
201
202
    if (ldm_greeter_get_user_defaults (user->priv->greeter, user->priv->name, &user->priv->language, &user->priv->layout, &user->priv->session))
203
        user->priv->have_defaults = TRUE;
204
}
205
206
/**
207
 * ldm_user_get_language
208
 * @user: A #LdmUser
209
 * 
210
 * Get the language for a user.
211
 * 
212
 * Return value: The language for the given user or NULL if using system defaults.
213
 **/
214
const gchar *
215
ldm_user_get_language (LdmUser *user)
216
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
217
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
218
    get_defaults (user);
219
    return user->priv->language;
220
}
221
222
/**
223
 * ldm_user_get_layout
224
 * @user: A #LdmUser
225
 * 
226
 * Get the keyboard layout for a user.
227
 * 
228
 * Return value: The keyboard layoyt for the given user or NULL if using system defaults.
229
 **/
230
const gchar *
231
ldm_user_get_layout (LdmUser *user)
232
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
233
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
234
    get_defaults (user);
235
    return user->priv->layout;
236
}
237
238
/**
239
 * ldm_user_get_session
240
 * @user: A #LdmUser
241
 * 
242
 * Get the session for a user.
243
 * 
244
 * Return value: The session for the given user or NULL if using system defaults.
245
 **/
246
const gchar *
247
ldm_user_get_session (LdmUser *user)
248
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
249
    g_return_val_if_fail (LDM_IS_USER (user), NULL);
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
250
    get_defaults (user);
251
    return user->priv->session; 
252
}
253
84 by robert.ancell at gmail
Fix up documentation and tweak UI
254
/**
255
 * ldm_user_get_logged_in:
256
 * @user: A #LdmUser
257
 * 
258
 * Check if a user is logged in.
259
 * 
260
 * Return value: TRUE if the user is currently logged in.
261
 **/
66 by robert.ancell at gmail
Make UserInfo a GObject
262
gboolean
263
ldm_user_get_logged_in (LdmUser *user)
264
{
411 by Robert Ancell
Add g_return_if_fail macros to all the liblightdm-gobject methods
265
    g_return_val_if_fail (LDM_IS_USER (user), FALSE);
66 by robert.ancell at gmail
Make UserInfo a GObject
266
    return user->priv->logged_in;
267
}
268
432 by Robert Ancell
Start liblightdm-gobject understanding user changes
269
void
270
ldm_user_set_logged_in (LdmUser *user, gboolean logged_in)
271
{
272
    g_return_if_fail (LDM_IS_USER (user));
273
    user->priv->logged_in = logged_in;
274
}
275
66 by robert.ancell at gmail
Make UserInfo a GObject
276
static void
277
ldm_user_init (LdmUser *user)
278
{
279
    user->priv = G_TYPE_INSTANCE_GET_PRIVATE (user, LDM_TYPE_USER, LdmUserPrivate);
280
}
281
282
static void
213 by Robert Ancell
whitespace
283
ldm_user_set_property (GObject      *object,
284
                       guint         prop_id,
285
                       const GValue *value,
286
                       GParamSpec   *pspec)
66 by robert.ancell at gmail
Make UserInfo a GObject
287
{
288
    LdmUser *self;
289
290
    self = LDM_USER (object);
291
292
    switch (prop_id) {
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
293
    case PROP_GREETER:
294
        self->priv->greeter = g_object_ref (g_value_get_object (value));
295
        break;
66 by robert.ancell at gmail
Make UserInfo a GObject
296
    case PROP_NAME:
437 by Robert Ancell
Move user manager into liblightdm
297
        ldm_user_set_name (self, g_value_get_string (value));
66 by robert.ancell at gmail
Make UserInfo a GObject
298
        break;
299
    case PROP_REAL_NAME:
437 by Robert Ancell
Move user manager into liblightdm
300
        ldm_user_set_real_name (self, g_value_get_string (value));
301
        break;
302
    case PROP_HOME_DIRECTORY:
303
        ldm_user_set_home_directory (self, g_value_get_string (value));
66 by robert.ancell at gmail
Make UserInfo a GObject
304
        break;
305
    case PROP_IMAGE:
437 by Robert Ancell
Move user manager into liblightdm
306
        ldm_user_set_image (self, g_value_get_string (value));
66 by robert.ancell at gmail
Make UserInfo a GObject
307
        break;
308
    case PROP_LOGGED_IN:
437 by Robert Ancell
Move user manager into liblightdm
309
        ldm_user_set_logged_in (self, g_value_get_boolean (value));
66 by robert.ancell at gmail
Make UserInfo a GObject
310
        break;
311
    default:
312
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313
        break;
314
    }
315
}
316
317
static void
213 by Robert Ancell
whitespace
318
ldm_user_get_property (GObject    *object,
319
                       guint       prop_id,
320
                       GValue     *value,
321
                       GParamSpec *pspec)
66 by robert.ancell at gmail
Make UserInfo a GObject
322
{
323
    LdmUser *self;
324
325
    self = LDM_USER (object);
326
327
    switch (prop_id) {
328
    case PROP_NAME:
329
        g_value_set_string (value, ldm_user_get_name (self));
330
        break;
331
    case PROP_REAL_NAME:
332
        g_value_set_string (value, ldm_user_get_real_name (self));
333
        break;
334
    case PROP_DISPLAY_NAME:
335
        g_value_set_string (value, ldm_user_get_display_name (self));
336
        break;
437 by Robert Ancell
Move user manager into liblightdm
337
    case PROP_HOME_DIRECTORY:
338
        g_value_set_string (value, ldm_user_get_home_directory (self));
339
        break;
66 by robert.ancell at gmail
Make UserInfo a GObject
340
    case PROP_IMAGE:
341
        g_value_set_string (value, ldm_user_get_image (self));
342
        break;
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
343
    case PROP_LANGUAGE:
344
        g_value_set_string (value, ldm_user_get_language (self));
345
        break;
346
    case PROP_LAYOUT:
347
        g_value_set_string (value, ldm_user_get_layout (self));
348
        break;
349
    case PROP_SESSION:
350
        g_value_set_string (value, ldm_user_get_session (self));
351
        break;
66 by robert.ancell at gmail
Make UserInfo a GObject
352
    case PROP_LOGGED_IN:
353
        g_value_set_boolean (value, ldm_user_get_logged_in (self));
354
        break;
355
    default:
356
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357
        break;
358
    }
359
}
360
361
static void
362
ldm_user_class_init (LdmUserClass *klass)
363
{
364
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
365
  
366
    g_type_class_add_private (klass, sizeof (LdmUserPrivate));
367
368
    object_class->set_property = ldm_user_set_property;
369
    object_class->get_property = ldm_user_get_property;
370
371
    g_object_class_install_property(object_class,
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
372
                                    PROP_GREETER,
373
                                    g_param_spec_object("greeter",
374
                                                        "greeter",
375
                                                        "Greeter",
376
                                                        LDM_TYPE_GREETER,
377
                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
378
    g_object_class_install_property(object_class,
66 by robert.ancell at gmail
Make UserInfo a GObject
379
                                    PROP_NAME,
380
                                    g_param_spec_string("name",
381
                                                        "name",
382
                                                        "Username",
383
                                                        NULL,
384
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
385
    g_object_class_install_property(object_class,
386
                                    PROP_REAL_NAME,
387
                                    g_param_spec_string("real-name",
388
                                                        "real-name",
389
                                                        "Users real name",
390
                                                        NULL,
391
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
392
    g_object_class_install_property(object_class,
393
                                    PROP_DISPLAY_NAME,
394
                                    g_param_spec_string("display-name",
395
                                                        "display-name",
396
                                                        "Users display name",
397
                                                        NULL,
398
                                                        G_PARAM_READABLE));
399
    g_object_class_install_property(object_class,
437 by Robert Ancell
Move user manager into liblightdm
400
                                    PROP_HOME_DIRECTORY,
401
                                    g_param_spec_string("home-directory",
402
                                                        "home-directory",
403
                                                        "Home directory",
404
                                                        NULL,
405
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
406
    g_object_class_install_property(object_class,
66 by robert.ancell at gmail
Make UserInfo a GObject
407
                                    PROP_IMAGE,
408
                                    g_param_spec_string("image",
409
                                                        "image",
410
                                                        "Avatar image",
411
                                                        NULL,
412
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
413
    g_object_class_install_property(object_class,
255 by robert.ancell at canonical
Change language/layout/session when user selected in GTK+ greeter
414
                                    PROP_LANGUAGE,
415
                                    g_param_spec_string("language",
416
                                                        "language",
417
                                                        "Language used by this user",
418
                                                        NULL,
419
                                                        G_PARAM_READABLE));
420
    g_object_class_install_property(object_class,
421
                                    PROP_LAYOUT,
422
                                    g_param_spec_string("layout",
423
                                                        "layout",
424
                                                        "Keyboard layout used by this user",
425
                                                        NULL,
426
                                                        G_PARAM_READABLE));
427
    g_object_class_install_property(object_class,
428
                                    PROP_SESSION,
429
                                    g_param_spec_string("session",
430
                                                        "session",
431
                                                        "Session used by this user",
432
                                                        NULL,
433
                                                        G_PARAM_READABLE));
434
    g_object_class_install_property(object_class,
66 by robert.ancell at gmail
Make UserInfo a GObject
435
                                    PROP_LOGGED_IN,
436
                                    g_param_spec_boolean("logged-in",
437
                                                         "logged-in",
438
                                                         "TRUE if the user is currently in a session",
439
                                                         FALSE,
440
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
441
}