]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - common/user-list.c
Launchpad automatic translations update.
[sojka/lightdm.git] / common / user-list.c
1 /* -*- Mode: C; indent-tabs-mode:nil; tab-width:4 -*-
2  *
3  * Copyright (C) 2010 Robert Ancell.
4  * Copyright (C) 2014 Canonical, Ltd.
5  * Authors: Robert Ancell <robert.ancell@canonical.com>
6  *          Michael Terry <michael.terry@canonical.com>
7  * 
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 2 or version 3 of the License.
11  * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
12  */
13
14 #include <config.h>
15
16 #include <errno.h>
17 #include <string.h>
18 #include <sys/utsname.h>
19 #include <pwd.h>
20 #include <gio/gio.h>
21
22 #include "dmrc.h"
23 #include "user-list.h"
24
25 enum
26 {
27     LIST_PROP_NUM_USERS = 1,
28     LIST_PROP_USERS,
29 };
30
31 enum
32 {
33     USER_PROP_NAME = 1,
34     USER_PROP_REAL_NAME,
35     USER_PROP_DISPLAY_NAME,
36     USER_PROP_HOME_DIRECTORY,
37     USER_PROP_SHELL,
38     USER_PROP_IMAGE,
39     USER_PROP_BACKGROUND,
40     USER_PROP_LANGUAGE,
41     USER_PROP_LAYOUT,
42     USER_PROP_LAYOUTS,
43     USER_PROP_SESSION,
44     USER_PROP_LOGGED_IN,
45     USER_PROP_HAS_MESSAGES,
46     USER_PROP_UID,
47     USER_PROP_GID,
48 };
49
50 enum
51 {
52     USER_ADDED,
53     USER_CHANGED,
54     USER_REMOVED,
55     LAST_LIST_SIGNAL
56 };
57 static guint list_signals[LAST_LIST_SIGNAL] = { 0 };
58
59 enum
60 {
61     CHANGED,
62     GET_LOGGED_IN,
63     LAST_USER_SIGNAL
64 };
65 static guint user_signals[LAST_USER_SIGNAL] = { 0 };
66
67 typedef struct
68 {
69     /* Bus connection being communicated on */
70     GDBusConnection *bus;
71
72     /* D-Bus signals for accounts service events */
73     guint user_added_signal;
74     guint user_removed_signal;
75
76     /* D-Bus signals for display manager events */
77     guint session_added_signal;
78     guint session_removed_signal;
79
80     /* File monitor for password file */
81     GFileMonitor *passwd_monitor;
82
83     /* TRUE if have scanned users */
84     gboolean have_users;
85
86     /* List of users */
87     GList *users;
88
89     /* List of sessions */
90     GList *sessions;
91 } CommonUserListPrivate;
92
93 typedef struct
94 {
95     /* TRUE if have loaded the DMRC file */
96     gboolean loaded_dmrc;
97
98     /* Bus we are listening for accounts service on */
99     GDBusConnection *bus;
100
101     /* Accounts service path */
102     gchar *path;
103
104     /* Update signal from accounts service */
105     guint changed_signal;
106
107     /* Username */
108     gchar *name;
109
110     /* Descriptive name for user */
111     gchar *real_name;
112
113     /* Home directory of user */
114     gchar *home_directory;
115
116     /* Shell for user */
117     gchar *shell;
118
119     /* Image for user */
120     gchar *image;
121
122     /* Background image for users */
123     gchar *background;
124
125     /* TRUE if this user has messages available */
126     gboolean has_messages;
127
128     /* UID of user */
129     guint64 uid;
130
131     /* GID of user */
132     guint64 gid;
133
134     /* User chosen language */
135     gchar *language;
136
137     /* User layout preferences */
138     gchar **layouts;
139
140     /* User default session */
141     gchar *session;
142 } CommonUserPrivate;
143
144 typedef struct
145 {
146     GObject parent_instance;
147     gchar *path;
148     gchar *username;
149 } CommonSession;
150
151 typedef struct
152 {
153     GObjectClass parent_class;
154 } CommonSessionClass;
155
156 G_DEFINE_TYPE (CommonUserList, common_user_list, G_TYPE_OBJECT);
157 G_DEFINE_TYPE (CommonUser, common_user, G_TYPE_OBJECT);
158 #define COMMON_SESSION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), common_session_get_type (), CommonSession))
159 GType common_session_get_type (void);
160 G_DEFINE_TYPE (CommonSession, common_session, G_TYPE_OBJECT);
161
162 #define GET_LIST_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), COMMON_TYPE_USER_LIST, CommonUserListPrivate)
163 #define GET_USER_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), COMMON_TYPE_USER, CommonUserPrivate)
164
165 #define PASSWD_FILE      "/etc/passwd"
166 #define USER_CONFIG_FILE "/etc/lightdm/users.conf"
167
168 static CommonUserList *singleton = NULL;
169
170 /**
171  * common_user_list_get_instance:
172  *
173  * Get the user list.
174  *
175  * Return value: (transfer none): the #CommonUserList
176  **/
177 CommonUserList *
178 common_user_list_get_instance (void)
179 {
180     if (!singleton)
181         singleton = g_object_new (COMMON_TYPE_USER_LIST, NULL);
182     return singleton;
183 }
184
185 void
186 common_user_list_cleanup (void)
187 {
188     g_clear_object (&singleton);
189 }
190
191 static CommonUser *
192 get_user_by_name (CommonUserList *user_list, const gchar *username)
193 {
194     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
195     GList *link;
196   
197     for (link = priv->users; link; link = link->next)
198     {
199         CommonUser *user = link->data;
200         if (g_strcmp0 (common_user_get_name (user), username) == 0)
201             return user;
202     }
203
204     return NULL;
205 }
206
207 static CommonUser *
208 get_user_by_path (CommonUserList *user_list, const gchar *path)
209 {
210     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
211     GList *link;
212   
213     for (link = priv->users; link; link = link->next)
214     {
215         CommonUser *user = link->data;
216         if (g_strcmp0 (GET_USER_PRIVATE (user)->path, path) == 0)
217             return user;
218     }
219
220     return NULL;
221 }
222   
223 static gint
224 compare_user (gconstpointer a, gconstpointer b)
225 {
226     CommonUser *user_a = (CommonUser *) a, *user_b = (CommonUser *) b;
227     return g_strcmp0 (common_user_get_display_name (user_a), common_user_get_display_name (user_b));
228 }
229
230 static gboolean
231 update_passwd_user (CommonUser *user, const gchar *real_name, const gchar *home_directory, const gchar *shell, const gchar *image)
232 {
233     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
234
235     /* Skip if already set to this */
236     if (g_strcmp0 (common_user_get_real_name (user), real_name) == 0 &&
237         g_strcmp0 (common_user_get_home_directory (user), home_directory) == 0 &&
238         g_strcmp0 (common_user_get_shell (user), shell) == 0 &&
239         g_strcmp0 (common_user_get_image (user), image) == 0)
240         return FALSE;
241
242     g_free (priv->real_name);
243     priv->real_name = g_strdup (real_name);
244     g_free (priv->home_directory);
245     priv->home_directory = g_strdup (home_directory);
246     g_free (priv->shell);
247     priv->shell = g_strdup (shell);
248     g_free (priv->image);
249     priv->image = g_strdup (image);
250
251     return TRUE;
252 }
253
254 static void load_sessions (CommonUserList *user_list);
255
256 static gboolean
257 get_logged_in_cb (CommonUser *user, CommonUserList *user_list)
258 {
259     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
260     const gchar *username;
261     GList *link;
262
263     // Lazily decide to load/listen to sessions
264     if (priv->session_added_signal == 0)
265         load_sessions (user_list);
266
267     username = GET_USER_PRIVATE (user)->name;
268     for (link = priv->sessions; link; link = link->next)
269     {
270         CommonSession *session = link->data;
271         if (strcmp (session->username, username) == 0)
272             return TRUE;
273     }
274
275     return FALSE;
276 }
277
278 static void
279 user_changed_cb (CommonUser *user, CommonUserList *user_list)
280 {
281     g_signal_emit (user_list, list_signals[USER_CHANGED], 0, user);
282 }
283
284 static CommonUser *
285 make_passwd_user (CommonUserList *user_list, struct passwd *entry)
286 {
287     CommonUser *user = g_object_new (COMMON_TYPE_USER, NULL);
288     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
289     char **tokens;
290     gchar *real_name, *image;
291
292     g_signal_connect (user, "get-logged-in", G_CALLBACK (get_logged_in_cb), user_list);  
293
294     tokens = g_strsplit (entry->pw_gecos, ",", -1);
295     if (tokens[0] != NULL && tokens[0][0] != '\0')
296         real_name = g_strdup (tokens[0]);
297     else
298         real_name = g_strdup ("");
299     g_strfreev (tokens);
300
301     image = g_build_filename (entry->pw_dir, ".face", NULL);
302     if (!g_file_test (image, G_FILE_TEST_EXISTS))
303     {
304         g_free (image);
305         image = g_build_filename (entry->pw_dir, ".face.icon", NULL);
306         if (!g_file_test (image, G_FILE_TEST_EXISTS))
307         {
308             g_free (image);
309             image = NULL;
310         }
311     }
312
313     priv->name = g_strdup (entry->pw_name);
314     priv->real_name = real_name;
315     priv->home_directory = g_strdup (entry->pw_dir);
316     priv->shell = g_strdup (entry->pw_shell);
317     priv->image = image;
318     priv->uid = entry->pw_uid;
319     priv->gid = entry->pw_gid;
320
321     return user;
322 }
323
324 static void
325 load_passwd_file (CommonUserList *user_list, gboolean emit_add_signal)
326 {
327     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
328     GKeyFile *config;
329     gchar *value;
330     gint minimum_uid;
331     gchar **hidden_users, **hidden_shells;
332     GList *users = NULL, *old_users, *new_users = NULL, *changed_users = NULL, *link;
333     GError *error = NULL;
334
335     g_debug ("Loading user config from %s", USER_CONFIG_FILE);
336
337     config = g_key_file_new ();
338     g_key_file_load_from_file (config, USER_CONFIG_FILE, G_KEY_FILE_NONE, &error);
339     if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
340         g_warning ("Failed to load configuration from %s: %s", USER_CONFIG_FILE, error->message);
341     g_clear_error (&error);
342
343     if (g_key_file_has_key (config, "UserList", "minimum-uid", NULL))
344         minimum_uid = g_key_file_get_integer (config, "UserList", "minimum-uid", NULL);
345     else
346         minimum_uid = 500;
347
348     value = g_key_file_get_string (config, "UserList", "hidden-users", NULL);
349     if (!value)
350         value = g_strdup ("nobody nobody4 noaccess");
351     hidden_users = g_strsplit (value, " ", -1);
352     g_free (value);
353
354     value = g_key_file_get_string (config, "UserList", "hidden-shells", NULL);
355     if (!value)
356         value = g_strdup ("/bin/false /usr/sbin/nologin");
357     hidden_shells = g_strsplit (value, " ", -1);
358     g_free (value);
359
360     g_key_file_free (config);
361
362     setpwent ();
363
364     while (TRUE)
365     {
366         struct passwd *entry;
367         CommonUser *user;
368         int i;
369
370         errno = 0;
371         entry = getpwent ();
372         if (!entry)
373             break;
374
375         /* Ignore system users */
376         if (entry->pw_uid < minimum_uid)
377             continue;
378
379         /* Ignore users disabled by shell */
380         if (entry->pw_shell)
381         {
382             for (i = 0; hidden_shells[i] && strcmp (entry->pw_shell, hidden_shells[i]) != 0; i++);
383             if (hidden_shells[i])
384                 continue;
385         }
386
387         /* Ignore certain users */
388         for (i = 0; hidden_users[i] && strcmp (entry->pw_name, hidden_users[i]) != 0; i++);
389         if (hidden_users[i])
390             continue;
391
392         user = make_passwd_user (user_list, entry);
393
394         /* Update existing users if have them */
395         for (link = priv->users; link; link = link->next)
396         {
397             CommonUser *info = link->data;
398             if (strcmp (common_user_get_name (info), common_user_get_name (user)) == 0)
399             {
400                 if (update_passwd_user (info, common_user_get_real_name (user), common_user_get_home_directory (user), common_user_get_shell (user), common_user_get_image (user)))
401                     changed_users = g_list_insert_sorted (changed_users, info, compare_user);
402                 g_object_unref (user);
403                 user = info;
404                 break;
405             }
406         }
407         if (!link)
408         {
409             /* Only notify once we have loaded the user list */
410             if (priv->have_users)
411                 new_users = g_list_insert_sorted (new_users, user, compare_user);
412         }
413         users = g_list_insert_sorted (users, user, compare_user);
414     }
415     g_strfreev (hidden_users);
416     g_strfreev (hidden_shells);
417
418     if (errno != 0)
419         g_warning ("Failed to read password database: %s", strerror (errno));
420
421     endpwent ();
422
423     /* Use new user list */
424     old_users = priv->users;
425     priv->users = users;
426   
427     /* Notify of changes */
428     for (link = new_users; link; link = link->next)
429     {
430         CommonUser *info = link->data;
431         g_debug ("User %s added", common_user_get_name (info));
432         g_signal_connect (info, USER_SIGNAL_CHANGED, G_CALLBACK (user_changed_cb), user_list);
433         if (emit_add_signal)
434             g_signal_emit (user_list, list_signals[USER_ADDED], 0, info);
435     }
436     g_list_free (new_users);
437     for (link = changed_users; link; link = link->next)
438     {
439         CommonUser *info = link->data;
440         g_debug ("User %s changed", common_user_get_name (info));
441         g_signal_emit (info, user_signals[CHANGED], 0);
442     }
443     g_list_free (changed_users);
444     for (link = old_users; link; link = link->next)
445     {
446         GList *new_link;
447
448         /* See if this user is in the current list */
449         for (new_link = priv->users; new_link; new_link = new_link->next)
450         {
451             if (new_link->data == link->data)
452                 break;
453         }
454
455         if (!new_link)
456         {
457             CommonUser *info = link->data;
458             g_debug ("User %s removed", common_user_get_name (info));
459             g_signal_emit (user_list, list_signals[USER_REMOVED], 0, info);
460             g_object_unref (info);
461         }
462     }
463     g_list_free (old_users);
464 }
465
466 static void
467 passwd_changed_cb (GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent event_type, CommonUserList *user_list)
468 {
469     if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
470     {
471         g_debug ("%s changed, reloading user list", g_file_get_path (file));
472         load_passwd_file (user_list, TRUE);
473     }
474 }
475
476 static gboolean load_accounts_user (CommonUser *user);
477
478 static void
479 accounts_user_changed_cb (GDBusConnection *connection,
480                           const gchar *sender_name,
481                           const gchar *object_path,
482                           const gchar *interface_name,
483                           const gchar *signal_name,
484                           GVariant *parameters,
485                           gpointer data)
486 {
487     CommonUser *user = data;
488     /*CommonUserPrivate *priv = GET_USER_PRIVATE (user);*/
489
490     /* Log message disabled as AccountsService can have arbitrary plugins that
491      * might cause us to log when properties change we don't use. LP: #1376357
492      */
493     /*g_debug ("User %s changed", priv->path);*/
494     if (load_accounts_user (user))
495         g_signal_emit (user, user_signals[CHANGED], 0);
496 }
497
498 static gboolean
499 load_accounts_user (CommonUser *user)
500 {
501     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
502     GVariant *result, *value;
503     GVariantIter *iter;
504     gchar *name;
505     gboolean system_account = FALSE;
506     GError *error = NULL;
507
508     /* Get the properties for this user */
509     if (!priv->changed_signal)
510         priv->changed_signal = g_dbus_connection_signal_subscribe (priv->bus,
511                                                                    "org.freedesktop.Accounts",
512                                                                    "org.freedesktop.Accounts.User",
513                                                                    "Changed",
514                                                                    priv->path,
515                                                                    NULL,
516                                                                    G_DBUS_SIGNAL_FLAGS_NONE,
517                                                                    accounts_user_changed_cb,
518                                                                    user,
519                                                                    NULL);
520     result = g_dbus_connection_call_sync (priv->bus,
521                                           "org.freedesktop.Accounts",
522                                           priv->path,
523                                           "org.freedesktop.DBus.Properties",
524                                           "GetAll",
525                                           g_variant_new ("(s)", "org.freedesktop.Accounts.User"),
526                                           G_VARIANT_TYPE ("(a{sv})"),
527                                           G_DBUS_CALL_FLAGS_NONE,
528                                           -1,
529                                           NULL,
530                                           &error);
531     if (error)
532         g_warning ("Error updating user %s: %s", priv->path, error->message);
533     g_clear_error (&error);
534     if (!result)
535         return FALSE;
536
537     /* Store the properties we need */
538     g_variant_get (result, "(a{sv})", &iter);
539     while (g_variant_iter_loop (iter, "{&sv}", &name, &value))
540     {
541         if (strcmp (name, "UserName") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
542         {
543             g_free (priv->name);
544             priv->name = g_variant_dup_string (value, NULL);
545         }
546         else if (strcmp (name, "RealName") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
547         {
548             g_free (priv->real_name);
549             priv->real_name = g_variant_dup_string (value, NULL);
550         }
551         else if (strcmp (name, "HomeDirectory") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
552         {
553             g_free (priv->home_directory);
554             priv->home_directory = g_variant_dup_string (value, NULL);
555         }
556         else if (strcmp (name, "Shell") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
557         {
558             g_free (priv->shell);
559             priv->shell = g_variant_dup_string (value, NULL);
560         }
561         else if (strcmp (name, "SystemAccount") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
562             system_account = g_variant_get_boolean (value);
563         else if (strcmp (name, "Language") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
564         {
565             if (priv->language)
566                 g_free (priv->language);
567             priv->language = g_variant_dup_string (value, NULL);
568         }
569         else if (strcmp (name, "IconFile") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
570         {
571             g_free (priv->image);
572             priv->image = g_variant_dup_string (value, NULL);
573             if (strcmp (priv->image, "") == 0)
574             {
575                 g_free (priv->image);
576                 priv->image = NULL;
577             }
578         }
579         else if (strcmp (name, "XSession") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
580         {
581             g_free (priv->session);
582             priv->session = g_variant_dup_string (value, NULL);
583         }
584         else if (strcmp (name, "BackgroundFile") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
585         {
586             g_free (priv->background);
587             priv->background = g_variant_dup_string (value, NULL);
588             if (strcmp (priv->background, "") == 0)
589             {
590                 g_free (priv->background);
591                 priv->background = NULL;
592             }
593         }
594         else if (strcmp (name, "XKeyboardLayouts") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING_ARRAY))
595         {
596             g_strfreev (priv->layouts);
597             priv->layouts = g_variant_dup_strv (value, NULL);
598             if (!priv->layouts)
599             {
600                 priv->layouts = g_malloc (sizeof (gchar *) * 1);
601                 priv->layouts[0] = NULL;
602             }
603         }
604         else if (strcmp (name, "XHasMessages") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
605             priv->has_messages = g_variant_get_boolean (value);
606         else if (strcmp (name, "Uid") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT64))
607             priv->uid = g_variant_get_uint64 (value);
608     }
609     g_variant_iter_free (iter);
610
611     g_variant_unref (result);
612
613     return !system_account;
614 }
615
616 static void
617 add_accounts_user (CommonUserList *user_list, const gchar *path, gboolean emit_signal)
618 {
619     CommonUserListPrivate *list_priv = GET_LIST_PRIVATE (user_list);
620     CommonUser *user;
621     CommonUserPrivate *priv;
622
623     user = g_object_new (COMMON_TYPE_USER, NULL);
624     priv = GET_USER_PRIVATE (user);
625
626     g_debug ("User %s added", path);
627     priv->bus = g_object_ref (list_priv->bus);
628     priv->path = g_strdup (path);
629     g_signal_connect (user, USER_SIGNAL_CHANGED, G_CALLBACK (user_changed_cb), user_list);
630     g_signal_connect (user, "get-logged-in", G_CALLBACK (get_logged_in_cb), user_list);  
631     if (load_accounts_user (user))
632     {
633         list_priv->users = g_list_insert_sorted (list_priv->users, user, compare_user);
634         if (emit_signal)      
635             g_signal_emit (user_list, list_signals[USER_ADDED], 0, user);
636     }
637     else
638         g_object_unref (user);
639 }
640
641 static void
642 accounts_user_added_cb (GDBusConnection *connection,
643                         const gchar *sender_name,
644                         const gchar *object_path,
645                         const gchar *interface_name,
646                         const gchar *signal_name,
647                         GVariant *parameters,
648                         gpointer data)
649 {
650     CommonUserList *user_list = data;
651     gchar *path;
652     CommonUser *user;
653   
654     if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(o)")))
655     {
656         g_warning ("Got UserAccounts signal UserAdded with unknown parameters %s", g_variant_get_type_string (parameters));
657         return;
658     }
659
660     g_variant_get (parameters, "(&o)", &path);
661
662     /* Add user if we haven't got them */
663     user = get_user_by_path (user_list, path);
664     if (!user)
665         add_accounts_user (user_list, path, TRUE);
666 }
667
668 static void
669 accounts_user_deleted_cb (GDBusConnection *connection,
670                           const gchar *sender_name,
671                           const gchar *object_path,
672                           const gchar *interface_name,
673                           const gchar *signal_name,
674                           GVariant *parameters,
675                           gpointer data)
676 {
677     CommonUserList *user_list = data;
678     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
679     gchar *path;
680     CommonUser *user;
681
682     if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(o)")))
683     {
684         g_warning ("Got UserAccounts signal UserDeleted with unknown parameters %s", g_variant_get_type_string (parameters));
685         return;
686     }
687
688     g_variant_get (parameters, "(&o)", &path);
689
690     /* Delete user if we know of them */
691     user = get_user_by_path (user_list, path);
692     if (user)
693     {
694         g_debug ("User %s deleted", path);
695         priv->users = g_list_remove (priv->users, user);
696
697         g_signal_emit (user_list, list_signals[USER_REMOVED], 0, user);
698
699         g_object_unref (user);
700     }
701 }
702
703 static CommonSession *
704 load_session (CommonUserList *user_list, const gchar *path)
705 {
706     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
707     CommonSession *session = NULL;
708     GVariant *result, *username;
709     GError *error = NULL;
710
711     result = g_dbus_connection_call_sync (priv->bus,
712                                           "org.freedesktop.DisplayManager",
713                                           path,
714                                           "org.freedesktop.DBus.Properties",
715                                           "Get",
716                                           g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Session", "UserName"),
717                                           G_VARIANT_TYPE ("(v)"),
718                                           G_DBUS_CALL_FLAGS_NONE,
719                                           -1,
720                                           NULL,
721                                           &error);
722     if (error)
723         g_warning ("Error getting UserName from org.freedesktop.DisplayManager.Session: %s", error->message);
724     g_clear_error (&error);
725     if (!result)
726         return NULL;
727
728     g_variant_get (result, "(v)", &username);
729     if (g_variant_is_of_type (username, G_VARIANT_TYPE_STRING))
730     {
731         gchar *name;
732
733         g_variant_get (username, "&s", &name);
734
735         g_debug ("Loaded session %s (%s)", path, name);
736         session = g_object_new (common_session_get_type (), NULL);
737         session->username = g_strdup (name);
738         session->path = g_strdup (path);
739         priv->sessions = g_list_append (priv->sessions, session);
740     }
741     g_variant_unref (username);
742     g_variant_unref (result);
743
744     return session;
745 }
746
747 static void
748 session_added_cb (GDBusConnection *connection,
749                   const gchar *sender_name,
750                   const gchar *object_path,
751                   const gchar *interface_name,
752                   const gchar *signal_name,
753                   GVariant *parameters,
754                   gpointer data)
755 {
756     CommonUserList *user_list = data;
757     gchar *path;
758     CommonSession *session;
759     CommonUser *user = NULL;
760
761     if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(o)")))
762     {
763         g_warning ("Got DisplayManager signal SessionAdded with unknown parameters %s", g_variant_get_type_string (parameters));
764         return;
765     }
766
767     g_variant_get (parameters, "(&o)", &path);
768     session = load_session (user_list, path);
769     if (session)
770         user = get_user_by_name (user_list, session->username);
771     if (user)
772         g_signal_emit (user, user_signals[CHANGED], 0);
773 }
774
775 static void
776 session_removed_cb (GDBusConnection *connection,
777                     const gchar *sender_name,
778                     const gchar *object_path,
779                     const gchar *interface_name,
780                     const gchar *signal_name,
781                     GVariant *parameters,
782                     gpointer data)
783 {
784     CommonUserList *user_list = data;
785     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
786     gchar *path;
787     GList *link;
788
789     if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(o)")))
790     {
791         g_warning ("Got DisplayManager signal SessionRemoved with unknown parameters %s", g_variant_get_type_string (parameters));
792         return;
793     }
794
795     g_variant_get (parameters, "(&o)", &path);
796
797     for (link = priv->sessions; link; link = link->next)
798     {
799         CommonSession *session = link->data;
800         if (strcmp (session->path, path) == 0)
801         {
802             CommonUser *user;
803
804             g_debug ("Session %s removed", path);
805             priv->sessions = g_list_delete_link (priv->sessions, link);
806             user = get_user_by_name (user_list, session->username);
807             if (user)
808                 g_signal_emit (user, user_signals[CHANGED], 0);
809             g_object_unref (session);
810             break;
811         }
812     }
813 }
814
815 static void
816 load_sessions (CommonUserList *user_list)
817 {
818     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
819     GVariant *result;
820     GError *error = NULL;
821
822     priv->session_added_signal = g_dbus_connection_signal_subscribe (priv->bus,
823                                                                      "org.freedesktop.DisplayManager",
824                                                                      "org.freedesktop.DisplayManager",
825                                                                      "SessionAdded",
826                                                                      "/org/freedesktop/DisplayManager",
827                                                                      NULL,
828                                                                      G_DBUS_SIGNAL_FLAGS_NONE,
829                                                                      session_added_cb,
830                                                                      user_list,
831                                                                      NULL);
832     priv->session_removed_signal = g_dbus_connection_signal_subscribe (priv->bus,
833                                                                        "org.freedesktop.DisplayManager",
834                                                                        "org.freedesktop.DisplayManager",
835                                                                        "SessionRemoved",
836                                                                        "/org/freedesktop/DisplayManager",
837                                                                        NULL,
838                                                                        G_DBUS_SIGNAL_FLAGS_NONE,
839                                                                        session_removed_cb,
840                                                                        user_list,
841                                                                        NULL);
842     result = g_dbus_connection_call_sync (priv->bus,
843                                           "org.freedesktop.DisplayManager",
844                                           "/org/freedesktop/DisplayManager",
845                                           "org.freedesktop.DBus.Properties",
846                                           "Get",
847                                           g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"),
848                                           G_VARIANT_TYPE ("(v)"),
849                                           G_DBUS_CALL_FLAGS_NONE,
850                                           -1,
851                                           NULL,
852                                           &error);
853     if (error)
854         g_warning ("Error getting session list from org.freedesktop.DisplayManager: %s", error->message);
855     g_clear_error (&error);
856     if (result)
857     {
858         if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(v)")))
859         {
860             GVariant *value;
861             GVariantIter *iter;
862             const gchar *path;
863
864             g_variant_get (result, "(v)", &value);
865
866             g_debug ("Loading sessions from org.freedesktop.DisplayManager");
867             g_variant_get (value, "ao", &iter);
868             while (g_variant_iter_loop (iter, "&o", &path))
869                 load_session (user_list, path);
870             g_variant_iter_free (iter);
871
872             g_variant_unref (value);
873         }
874         else
875             g_warning ("Unexpected type from org.freedesktop.DisplayManager.Sessions: %s", g_variant_get_type_string (result));
876
877         g_variant_unref (result);
878     }
879 }
880
881 static void
882 load_users (CommonUserList *user_list)
883 {
884     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
885     GVariant *result;
886     GError *error = NULL;
887
888     if (priv->have_users)
889         return;
890     priv->have_users = TRUE;
891
892     /* Get user list from accounts service and fall back to /etc/passwd if that fails */
893     priv->user_added_signal = g_dbus_connection_signal_subscribe (priv->bus,
894                                                                   "org.freedesktop.Accounts",
895                                                                   "org.freedesktop.Accounts",
896                                                                   "UserAdded",
897                                                                   "/org/freedesktop/Accounts",
898                                                                   NULL,
899                                                                   G_DBUS_SIGNAL_FLAGS_NONE,
900                                                                   accounts_user_added_cb,
901                                                                   user_list,
902                                                                   NULL);
903     priv->user_removed_signal = g_dbus_connection_signal_subscribe (priv->bus,
904                                                                     "org.freedesktop.Accounts",
905                                                                     "org.freedesktop.Accounts",
906                                                                     "UserDeleted",
907                                                                     "/org/freedesktop/Accounts",
908                                                                     NULL,
909                                                                     G_DBUS_SIGNAL_FLAGS_NONE,
910                                                                     accounts_user_deleted_cb,
911                                                                     user_list,
912                                                                     NULL);
913     result = g_dbus_connection_call_sync (priv->bus,
914                                           "org.freedesktop.Accounts",
915                                           "/org/freedesktop/Accounts",
916                                           "org.freedesktop.Accounts",
917                                           "ListCachedUsers",
918                                           g_variant_new ("()"),
919                                           G_VARIANT_TYPE ("(ao)"),
920                                           G_DBUS_CALL_FLAGS_NONE,
921                                           -1,
922                                           NULL,
923                                           &error);
924     if (error)
925         g_warning ("Error getting user list from org.freedesktop.Accounts: %s", error->message);
926     g_clear_error (&error);
927     if (result)
928     {
929         GVariantIter *iter;
930         const gchar *path;
931
932         g_debug ("Loading users from org.freedesktop.Accounts");
933         g_variant_get (result, "(ao)", &iter);
934         while (g_variant_iter_loop (iter, "&o", &path))
935             add_accounts_user (user_list, path, FALSE);
936         g_variant_iter_free (iter);
937         g_variant_unref (result);
938     }
939     else
940     {
941         GFile *passwd_file;
942
943         g_dbus_connection_signal_unsubscribe (priv->bus, priv->user_added_signal);
944         priv->user_added_signal = 0;
945         g_dbus_connection_signal_unsubscribe (priv->bus, priv->user_removed_signal);
946         priv->user_removed_signal = 0;
947
948         load_passwd_file (user_list, FALSE);
949
950         /* Watch for changes to user list */
951
952         passwd_file = g_file_new_for_path (PASSWD_FILE);
953         priv->passwd_monitor = g_file_monitor (passwd_file, G_FILE_MONITOR_NONE, NULL, &error);
954         g_object_unref (passwd_file);
955         if (error)
956             g_warning ("Error monitoring %s: %s", PASSWD_FILE, error->message);
957         else
958             g_signal_connect (priv->passwd_monitor, "changed", G_CALLBACK (passwd_changed_cb), user_list);
959         g_clear_error (&error);
960     }
961 }
962
963 /**
964  * common_user_list_get_length:
965  * @user_list: a #CommonUserList
966  *
967  * Return value: The number of users able to log in
968  **/
969 gint
970 common_user_list_get_length (CommonUserList *user_list)
971 {
972     g_return_val_if_fail (COMMON_IS_USER_LIST (user_list), 0);
973     load_users (user_list);
974     return g_list_length (GET_LIST_PRIVATE (user_list)->users);
975 }
976
977 /**
978  * common_user_list_get_users:
979  * @user_list: A #CommonUserList
980  *
981  * Get a list of users to present to the user.  This list may be a subset of the
982  * available users and may be empty depending on the server configuration.
983  *
984  * Return value: (element-type CommonUser) (transfer none): A list of #CommonUser that should be presented to the user.
985  **/
986 GList *
987 common_user_list_get_users (CommonUserList *user_list)
988 {
989     g_return_val_if_fail (COMMON_IS_USER_LIST (user_list), NULL);
990     load_users (user_list);
991     return GET_LIST_PRIVATE (user_list)->users;
992 }
993
994 /**
995  * common_user_list_get_user_by_name:
996  * @user_list: A #CommonUserList
997  * @username: Name of user to get.
998  *
999  * Get infomation about a given user or #NULL if this user doesn't exist.
1000  * Includes hidden and system users, unlike the list from
1001  * common_user_list_get_users.
1002  *
1003  * Return value: (transfer full): A #CommonUser entry for the given user.
1004  **/
1005 CommonUser *
1006 common_user_list_get_user_by_name (CommonUserList *user_list, const gchar *username)
1007 {
1008     g_return_val_if_fail (COMMON_IS_USER_LIST (user_list), NULL);
1009     g_return_val_if_fail (username != NULL, NULL);
1010
1011     load_users (user_list);
1012
1013     CommonUser *user = get_user_by_name (user_list, username);
1014     if (user)
1015         return g_object_ref (user);
1016
1017     /* Sometimes we need to look up users that aren't in AccountsService.
1018        Notably we need to look up the user that the greeter runs as, which
1019        is usually 'lightdm'. For such cases, we manually create a one-off
1020        CommonUser object and pre-seed with passwd info. */
1021     struct passwd *entry = getpwnam (username);
1022     if (entry != NULL)
1023         return make_passwd_user (user_list, entry);
1024
1025     return NULL;
1026 }
1027
1028 static void
1029 common_user_list_init (CommonUserList *user_list)
1030 {
1031     CommonUserListPrivate *priv = GET_LIST_PRIVATE (user_list);
1032
1033     priv->bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
1034 }
1035
1036 static void
1037 common_user_list_set_property (GObject      *object,
1038                                guint         prop_id,
1039                                const GValue *value,
1040                                GParamSpec   *pspec)
1041 {
1042     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1043 }
1044
1045 static void
1046 common_user_list_get_property (GObject    *object,
1047                                guint       prop_id,
1048                                GValue     *value,
1049                                GParamSpec *pspec)
1050 {
1051     CommonUserList *self;
1052
1053     self = COMMON_USER_LIST (object);
1054
1055     switch (prop_id)
1056     {
1057     case LIST_PROP_NUM_USERS:
1058         g_value_set_int (value, common_user_list_get_length (self));
1059         break;
1060     default:
1061         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1062         break;
1063     }
1064 }
1065
1066 static void
1067 common_user_list_finalize (GObject *object)
1068 {
1069     CommonUserList *self = COMMON_USER_LIST (object);
1070     CommonUserListPrivate *priv = GET_LIST_PRIVATE (self);
1071
1072     /* Remove children first, they might access us */
1073     g_list_free_full (priv->users, g_object_unref);
1074     g_list_free_full (priv->sessions, g_object_unref);
1075
1076     if (priv->user_added_signal)
1077         g_dbus_connection_signal_unsubscribe (priv->bus, priv->user_added_signal);
1078     if (priv->user_removed_signal)
1079         g_dbus_connection_signal_unsubscribe (priv->bus, priv->user_removed_signal);
1080     if (priv->session_added_signal)
1081         g_dbus_connection_signal_unsubscribe (priv->bus, priv->session_added_signal);
1082     if (priv->session_removed_signal)
1083         g_dbus_connection_signal_unsubscribe (priv->bus, priv->session_removed_signal);
1084     g_object_unref (priv->bus);
1085     g_clear_object (&priv->passwd_monitor);
1086
1087     G_OBJECT_CLASS (common_user_list_parent_class)->finalize (object);
1088 }
1089
1090 static void
1091 common_user_list_class_init (CommonUserListClass *klass)
1092 {
1093     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1094
1095     g_type_class_add_private (klass, sizeof (CommonUserListPrivate));
1096
1097     object_class->set_property = common_user_list_set_property;
1098     object_class->get_property = common_user_list_get_property;
1099     object_class->finalize = common_user_list_finalize;
1100
1101     g_object_class_install_property (object_class,
1102                                      LIST_PROP_NUM_USERS,
1103                                      g_param_spec_int ("num-users",
1104                                                        "num-users",
1105                                                        "Number of login users",
1106                                                        0, G_MAXINT, 0,
1107                                                        G_PARAM_READABLE));
1108     /**
1109      * CommonUserList::user-added:
1110      * @user_list: A #CommonUserList
1111      * @user: The #CommonUser that has been added.
1112      *
1113      * The ::user-added signal gets emitted when a user account is created.
1114      **/
1115     list_signals[USER_ADDED] =
1116         g_signal_new (USER_LIST_SIGNAL_USER_ADDED,
1117                       G_TYPE_FROM_CLASS (klass),
1118                       G_SIGNAL_RUN_LAST,
1119                       G_STRUCT_OFFSET (CommonUserListClass, user_added),
1120                       NULL, NULL,
1121                       NULL,
1122                       G_TYPE_NONE, 1, COMMON_TYPE_USER);
1123
1124     /**
1125      * CommonUserList::user-changed:
1126      * @user_list: A #CommonUserList
1127      * @user: The #CommonUser that has been changed.
1128      *
1129      * The ::user-changed signal gets emitted when a user account is modified.
1130      **/
1131     list_signals[USER_CHANGED] =
1132         g_signal_new (USER_LIST_SIGNAL_USER_CHANGED,
1133                       G_TYPE_FROM_CLASS (klass),
1134                       G_SIGNAL_RUN_LAST,
1135                       G_STRUCT_OFFSET (CommonUserListClass, user_changed),
1136                       NULL, NULL,
1137                       NULL,
1138                       G_TYPE_NONE, 1, COMMON_TYPE_USER);
1139
1140     /**
1141      * CommonUserList::user-removed:
1142      * @user_list: A #CommonUserList
1143      * @user: The #CommonUser that has been removed.
1144      *
1145      * The ::user-removed signal gets emitted when a user account is removed.
1146      **/
1147     list_signals[USER_REMOVED] =
1148         g_signal_new (USER_LIST_SIGNAL_USER_REMOVED,
1149                       G_TYPE_FROM_CLASS (klass),
1150                       G_SIGNAL_RUN_LAST,
1151                       G_STRUCT_OFFSET (CommonUserListClass, user_removed),
1152                       NULL, NULL,
1153                       NULL,
1154                       G_TYPE_NONE, 1, COMMON_TYPE_USER);
1155 }
1156
1157 static gboolean
1158 call_method (CommonUser *user, const gchar *method, GVariant *args,
1159              const gchar *expected, GVariant **result)
1160 {
1161     GVariant *answer;
1162     GError *error = NULL;
1163     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
1164
1165     answer = g_dbus_connection_call_sync (priv->bus,
1166                                           "org.freedesktop.Accounts",
1167                                           priv->path,
1168                                           "org.freedesktop.Accounts.User",
1169                                           method,
1170                                           args,
1171                                           G_VARIANT_TYPE (expected),
1172                                           G_DBUS_CALL_FLAGS_NONE,
1173                                           -1,
1174                                           NULL,
1175                                           &error);
1176     if (error)
1177         g_warning ("Could not call %s: %s", method, error->message);
1178     g_clear_error (&error);
1179
1180     if (!answer)
1181         return FALSE;
1182
1183     if (result)
1184         *result = answer;
1185     else
1186         g_variant_unref (answer);
1187
1188     return TRUE;
1189 }
1190
1191 static void
1192 save_string_to_dmrc (CommonUser *user, const gchar *group,
1193                      const gchar *key, const gchar *value)
1194 {
1195     GKeyFile *dmrc;
1196
1197     dmrc = dmrc_load (user);
1198     g_key_file_set_string (dmrc, group, key, value);
1199     dmrc_save (dmrc, user);
1200
1201     g_key_file_free (dmrc);
1202 }
1203
1204 /* Loads language/layout/session info for user */
1205 static void
1206 load_dmrc (CommonUser *user)
1207 {
1208     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
1209     GKeyFile *dmrc;
1210
1211     /* We're using Accounts service instead */
1212     if (priv->path)
1213         return;
1214
1215     if (priv->loaded_dmrc)
1216         return;
1217     priv->loaded_dmrc = TRUE;
1218     dmrc = dmrc_load (user);
1219
1220     // FIXME: Watch for changes
1221
1222     /* The Language field contains the locale */
1223     g_free (priv->language);
1224     priv->language = g_key_file_get_string (dmrc, "Desktop", "Language", NULL);
1225
1226     if (g_key_file_has_key (dmrc, "Desktop", "Layout", NULL))
1227     {
1228         g_strfreev (priv->layouts);
1229         priv->layouts = g_malloc (sizeof (gchar *) * 2);
1230         priv->layouts[0] = g_key_file_get_string (dmrc, "Desktop", "Layout", NULL);
1231         priv->layouts[1] = NULL;
1232     }
1233
1234     g_free (priv->session);
1235     priv->session = g_key_file_get_string (dmrc, "Desktop", "Session", NULL);
1236
1237     g_key_file_free (dmrc);
1238 }
1239
1240 /**
1241  * common_user_get_name:
1242  * @user: A #CommonUser
1243  * 
1244  * Get the name of a user.
1245  * 
1246  * Return value: The name of the given user
1247  **/
1248 const gchar *
1249 common_user_get_name (CommonUser *user)
1250 {
1251     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1252     return GET_USER_PRIVATE (user)->name;
1253 }
1254
1255 /**
1256  * common_user_get_real_name:
1257  * @user: A #CommonUser
1258  * 
1259  * Get the real name of a user.
1260  *
1261  * Return value: The real name of the given user
1262  **/
1263 const gchar *
1264 common_user_get_real_name (CommonUser *user)
1265 {
1266     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1267     return GET_USER_PRIVATE (user)->real_name;
1268 }
1269
1270 /**
1271  * common_user_get_display_name:
1272  * @user: A #CommonUser
1273  * 
1274  * Get the display name of a user.
1275  * 
1276  * Return value: The display name of the given user
1277  **/
1278 const gchar *
1279 common_user_get_display_name (CommonUser *user)
1280 {
1281     CommonUserPrivate *priv;
1282
1283     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1284
1285     priv = GET_USER_PRIVATE (user);
1286     if (!priv->real_name || strcmp (priv->real_name, "") == 0)
1287         return priv->name;
1288     else
1289         return priv->real_name;
1290 }
1291
1292 /**
1293  * common_user_get_home_directory:
1294  * @user: A #CommonUser
1295  * 
1296  * Get the home directory for a user.
1297  * 
1298  * Return value: The users home directory
1299  */
1300 const gchar *
1301 common_user_get_home_directory (CommonUser *user)
1302 {
1303     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1304     return GET_USER_PRIVATE (user)->home_directory;
1305 }
1306
1307 /**
1308  * common_user_get_shell:
1309  * @user: A #CommonUser
1310  * 
1311  * Get the shell for a user.
1312  * 
1313  * Return value: The user's shell
1314  */
1315 const gchar *
1316 common_user_get_shell (CommonUser *user)
1317 {
1318     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1319     return GET_USER_PRIVATE (user)->shell;
1320 }
1321
1322 /**
1323  * common_user_get_image:
1324  * @user: A #CommonUser
1325  * 
1326  * Get the image URI for a user.
1327  * 
1328  * Return value: The image URI for the given user or #NULL if no URI
1329  **/
1330 const gchar *
1331 common_user_get_image (CommonUser *user)
1332 {
1333     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1334     return GET_USER_PRIVATE (user)->image;
1335 }
1336
1337 /**
1338  * common_user_get_background:
1339  * @user: A #CommonUser
1340  * 
1341  * Get the background file path for a user.
1342  * 
1343  * Return value: The background file path for the given user or #NULL if no path
1344  **/
1345 const gchar *
1346 common_user_get_background (CommonUser *user)
1347 {
1348     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1349     return GET_USER_PRIVATE (user)->background;
1350 }
1351
1352 /**
1353  * common_user_get_language:
1354  * @user: A #CommonUser
1355  * 
1356  * Get the language for a user.
1357  * 
1358  * Return value: The language in the form of a local specification (e.g. "de_DE.UTF-8") for the given user or #NULL if using the system default locale.
1359  **/
1360 const gchar *
1361 common_user_get_language (CommonUser *user)
1362 {
1363     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1364     load_dmrc (user);
1365     const gchar *language = GET_USER_PRIVATE (user)->language;
1366     return (language && language[0] == 0) ? NULL : language; /* Treat "" as NULL */
1367 }
1368
1369 /**
1370  * common_user_set_language:
1371  * @user: A #CommonUser
1372  * @language: The user's new language
1373  * 
1374  * Set the language for a user.
1375  **/
1376 void
1377 common_user_set_language (CommonUser *user, const gchar *language)
1378 {
1379     g_return_if_fail (COMMON_IS_USER (user));
1380     if (g_strcmp0 (common_user_get_language (user), language) != 0)
1381     {
1382         call_method (user, "SetLanguage", g_variant_new ("(s)", language), "()", NULL);
1383         save_string_to_dmrc (user, "Desktop", "Language", language);
1384     }
1385 }
1386
1387 /**
1388  * common_user_get_layout:
1389  * @user: A #CommonUser
1390  * 
1391  * Get the keyboard layout for a user.
1392  * 
1393  * Return value: The keyboard layout for the given user or #NULL if using system defaults.  Copy the value if you want to use it long term.
1394  **/
1395 const gchar *
1396 common_user_get_layout (CommonUser *user)
1397 {
1398     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1399     load_dmrc (user);
1400     return GET_USER_PRIVATE (user)->layouts[0];
1401 }
1402
1403 /**
1404  * common_user_get_layouts:
1405  * @user: A #CommonUser
1406  * 
1407  * Get the configured keyboard layouts for a user.
1408  * 
1409  * Return value: (transfer none): A NULL-terminated array of keyboard layouts for the given user.  Copy the values if you want to use them long term.
1410  **/
1411 const gchar * const *
1412 common_user_get_layouts (CommonUser *user)
1413 {
1414     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1415     load_dmrc (user);
1416     return (const gchar * const *) GET_USER_PRIVATE (user)->layouts;
1417 }
1418
1419 /**
1420  * common_user_get_session:
1421  * @user: A #CommonUser
1422  * 
1423  * Get the session for a user.
1424  * 
1425  * Return value: The session for the given user or #NULL if using system defaults.
1426  **/
1427 const gchar *
1428 common_user_get_session (CommonUser *user)
1429 {
1430     g_return_val_if_fail (COMMON_IS_USER (user), NULL);
1431     load_dmrc (user);
1432     const gchar *session = GET_USER_PRIVATE (user)->session;
1433     return (session && session[0] == 0) ? NULL : session; /* Treat "" as NULL */
1434 }
1435
1436 /**
1437  * common_user_set_session:
1438  * @user: A #CommonUser
1439  * @language: The user's new session
1440  * 
1441  * Set the session for a user.
1442  **/
1443 void
1444 common_user_set_session (CommonUser *user, const gchar *session)
1445 {
1446     g_return_if_fail (COMMON_IS_USER (user));
1447     if (g_strcmp0 (common_user_get_session (user), session) != 0)
1448     {
1449         call_method (user, "SetXSession", g_variant_new ("(s)", session), "()", NULL);
1450         save_string_to_dmrc (user, "Desktop", "Session", session);
1451     }
1452 }
1453
1454 /**
1455  * common_user_get_logged_in:
1456  * @user: A #CommonUser
1457  * 
1458  * Check if a user is logged in.
1459  * 
1460  * Return value: #TRUE if the user is currently logged in.
1461  **/
1462 gboolean
1463 common_user_get_logged_in (CommonUser *user)
1464 {
1465     gboolean result;
1466
1467     g_return_val_if_fail (COMMON_IS_USER (user), FALSE);
1468
1469     g_signal_emit (user, user_signals[GET_LOGGED_IN], 0, &result);
1470
1471     return result;
1472 }
1473
1474 /**
1475  * common_user_get_has_messages:
1476  * @user: A #CommonUser
1477  * 
1478  * Check if a user has waiting messages.
1479  * 
1480  * Return value: #TRUE if the user has waiting messages.
1481  **/
1482 gboolean
1483 common_user_get_has_messages (CommonUser *user)
1484 {
1485     g_return_val_if_fail (COMMON_IS_USER (user), FALSE);
1486     return GET_USER_PRIVATE (user)->has_messages;
1487 }
1488
1489 /**
1490  * common_user_get_uid:
1491  * @user: A #CommonUser
1492  * 
1493  * Get the uid of a user
1494  * 
1495  * Return value: The user's uid
1496  **/
1497 uid_t
1498 common_user_get_uid (CommonUser *user)
1499 {
1500     g_return_val_if_fail (COMMON_IS_USER (user), 0);
1501     return GET_USER_PRIVATE (user)->uid;
1502 }
1503
1504 /**
1505  * common_user_get_gid:
1506  * @user: A #CommonUser
1507  * 
1508  * Get the gid of a user
1509  * 
1510  * Return value: The user's gid
1511  **/
1512 gid_t
1513 common_user_get_gid (CommonUser *user)
1514 {
1515     g_return_val_if_fail (COMMON_IS_USER (user), 0);
1516     /* gid is not actually stored in AccountsService, so if our user is from
1517        AccountsService, we have to look up manually in passwd.  gid won't
1518        change, so just look up the first time we're asked and never again. */
1519     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
1520     if (priv->uid != 0 && priv->gid == 0)
1521     {
1522         struct passwd *entry = getpwuid (priv->uid);
1523         if (entry != NULL)
1524             priv->gid = entry->pw_gid;
1525     }
1526     return priv->gid;
1527 }
1528
1529 static void
1530 common_user_init (CommonUser *user)
1531 {
1532     CommonUserPrivate *priv = GET_USER_PRIVATE (user);
1533     priv->layouts = g_malloc (sizeof (gchar *) * 1);
1534     priv->layouts[0] = NULL;
1535 }
1536
1537 static void
1538 common_user_set_property (GObject    *object,
1539                            guint       prop_id,
1540                            const GValue *value,
1541                            GParamSpec *pspec)
1542 {
1543     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1544 }
1545
1546 static void
1547 common_user_get_property (GObject    *object,
1548                            guint       prop_id,
1549                            GValue     *value,
1550                            GParamSpec *pspec)
1551 {
1552     CommonUser *self;
1553
1554     self = COMMON_USER (object);
1555
1556     switch (prop_id)
1557     {
1558     case USER_PROP_NAME:
1559         g_value_set_string (value, common_user_get_name (self));
1560         break;
1561     case USER_PROP_REAL_NAME:
1562         g_value_set_string (value, common_user_get_real_name (self));
1563         break;
1564     case USER_PROP_DISPLAY_NAME:
1565         g_value_set_string (value, common_user_get_display_name (self));
1566         break;
1567     case USER_PROP_HOME_DIRECTORY:
1568         g_value_set_string (value, common_user_get_home_directory (self));
1569         break;
1570     case USER_PROP_SHELL:
1571         g_value_set_string (value, common_user_get_shell (self));
1572         break;
1573     case USER_PROP_IMAGE:
1574         g_value_set_string (value, common_user_get_image (self));
1575         break;
1576     case USER_PROP_BACKGROUND:
1577         g_value_set_string (value, common_user_get_background (self));
1578         break;
1579     case USER_PROP_LANGUAGE:
1580         g_value_set_string (value, common_user_get_language (self));
1581         break;
1582     case USER_PROP_LAYOUT:
1583         g_value_set_string (value, common_user_get_layout (self));
1584         break;
1585     case USER_PROP_LAYOUTS:
1586         g_value_set_boxed (value, g_strdupv ((gchar **) common_user_get_layouts (self)));
1587         break;
1588     case USER_PROP_SESSION:
1589         g_value_set_string (value, common_user_get_session (self));
1590         break;
1591     case USER_PROP_LOGGED_IN:
1592         g_value_set_boolean (value, common_user_get_logged_in (self));
1593         break;
1594     case USER_PROP_HAS_MESSAGES:
1595         g_value_set_boolean (value, common_user_get_has_messages (self));
1596         break;
1597     case USER_PROP_UID:
1598         g_value_set_uint64 (value, common_user_get_uid (self));
1599         break;
1600     case USER_PROP_GID:
1601         g_value_set_uint64 (value, common_user_get_gid (self));
1602         break;
1603     default:
1604         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1605         break;
1606     }
1607 }
1608
1609 static void
1610 common_user_finalize (GObject *object)
1611 {
1612     CommonUser *self = COMMON_USER (object);
1613     CommonUserPrivate *priv = GET_USER_PRIVATE (self);
1614
1615     g_free (priv->path);
1616     if (priv->changed_signal)
1617         g_dbus_connection_signal_unsubscribe (priv->bus, priv->changed_signal);
1618     g_clear_object (&priv->bus);
1619     g_free (priv->name);
1620     g_free (priv->real_name);
1621     g_free (priv->home_directory);
1622     g_free (priv->shell);
1623     g_free (priv->image);
1624     g_free (priv->background);
1625     g_free (priv->language);
1626     g_strfreev (priv->layouts);
1627     g_free (priv->session);
1628 }
1629
1630 static void
1631 common_user_class_init (CommonUserClass *klass)
1632 {
1633     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1634   
1635     g_type_class_add_private (klass, sizeof (CommonUserPrivate));
1636
1637     object_class->set_property = common_user_set_property;
1638     object_class->get_property = common_user_get_property;
1639     object_class->finalize = common_user_finalize;
1640
1641     g_object_class_install_property (object_class,
1642                                      USER_PROP_NAME,
1643                                      g_param_spec_string ("name",
1644                                                           "name",
1645                                                           "Username",
1646                                                           NULL,
1647                                                           G_PARAM_READWRITE));
1648     g_object_class_install_property (object_class,
1649                                      USER_PROP_REAL_NAME,
1650                                      g_param_spec_string ("real-name",
1651                                                           "real-name",
1652                                                           "Users real name",
1653                                                           NULL,
1654                                                           G_PARAM_READWRITE));
1655     g_object_class_install_property (object_class,
1656                                      USER_PROP_DISPLAY_NAME,
1657                                      g_param_spec_string ("display-name",
1658                                                           "display-name",
1659                                                           "Users display name",
1660                                                           NULL,
1661                                                           G_PARAM_READABLE));
1662     g_object_class_install_property (object_class,
1663                                      USER_PROP_HOME_DIRECTORY,
1664                                      g_param_spec_string ("home-directory",
1665                                                           "home-directory",
1666                                                           "Home directory",
1667                                                           NULL,
1668                                                           G_PARAM_READWRITE));
1669     g_object_class_install_property (object_class,
1670                                      USER_PROP_SHELL,
1671                                      g_param_spec_string ("shell",
1672                                                           "shell",
1673                                                           "Shell",
1674                                                           NULL,
1675                                                           G_PARAM_READWRITE));
1676     g_object_class_install_property (object_class,
1677                                      USER_PROP_IMAGE,
1678                                      g_param_spec_string ("image",
1679                                                           "image",
1680                                                           "Avatar image",
1681                                                           NULL,
1682                                                           G_PARAM_READWRITE));
1683     g_object_class_install_property (object_class,
1684                                      USER_PROP_BACKGROUND,
1685                                      g_param_spec_string ("background",
1686                                                           "background",
1687                                                           "User background",
1688                                                           NULL,
1689                                                           G_PARAM_READWRITE));
1690     g_object_class_install_property (object_class,
1691                                      USER_PROP_LANGUAGE,
1692                                      g_param_spec_string ("language",
1693                                                          "language",
1694                                                          "Language used by this user",
1695                                                          NULL,
1696                                                          G_PARAM_READABLE));
1697     g_object_class_install_property (object_class,
1698                                      USER_PROP_LAYOUT,
1699                                      g_param_spec_string ("layout",
1700                                                           "layout",
1701                                                           "Keyboard layout used by this user",
1702                                                           NULL,
1703                                                           G_PARAM_READABLE));
1704     g_object_class_install_property (object_class,
1705                                      USER_PROP_LAYOUTS,
1706                                      g_param_spec_boxed ("layouts",
1707                                                          "layouts",
1708                                                          "Keyboard layouts used by this user",
1709                                                          G_TYPE_STRV,
1710                                                          G_PARAM_READABLE));
1711     g_object_class_install_property (object_class,
1712                                      USER_PROP_SESSION,
1713                                      g_param_spec_string ("session",
1714                                                           "session",
1715                                                           "Session used by this user",
1716                                                           NULL,
1717                                                           G_PARAM_READABLE));
1718     g_object_class_install_property (object_class,
1719                                      USER_PROP_LOGGED_IN,
1720                                      g_param_spec_boolean ("logged-in",
1721                                                            "logged-in",
1722                                                            "TRUE if the user is currently in a session",
1723                                                            FALSE,
1724                                                            G_PARAM_READWRITE));
1725     g_object_class_install_property (object_class,
1726                                      USER_PROP_LOGGED_IN,
1727                                      g_param_spec_boolean ("has-messages",
1728                                                            "has-messages",
1729                                                            "TRUE if the user is has waiting messages",
1730                                                            FALSE,
1731                                                            G_PARAM_READWRITE));
1732     g_object_class_install_property (object_class,
1733                                      USER_PROP_UID,
1734                                      g_param_spec_uint64 ("uid",
1735                                                           "uid",
1736                                                           "Uid",
1737                                                           0,
1738                                                           G_MAXUINT64,
1739                                                           0,
1740                                                           G_PARAM_READWRITE));
1741     g_object_class_install_property (object_class,
1742                                      USER_PROP_GID,
1743                                      g_param_spec_uint64 ("gd",
1744                                                           "gid",
1745                                                           "Gid",
1746                                                           0,
1747                                                           G_MAXUINT64,
1748                                                           0,
1749                                                           G_PARAM_READWRITE));
1750
1751     /**
1752      * CommonUser::changed:
1753      * @user: A #CommonUser
1754      *
1755      * The ::changed signal gets emitted this user account is modified.
1756      **/
1757     user_signals[CHANGED] =
1758         g_signal_new (USER_SIGNAL_CHANGED,
1759                       G_TYPE_FROM_CLASS (klass),
1760                       G_SIGNAL_RUN_LAST,
1761                       G_STRUCT_OFFSET (CommonUserClass, changed),
1762                       NULL, NULL,
1763                       NULL,
1764                       G_TYPE_NONE, 0);
1765
1766     user_signals[GET_LOGGED_IN] =
1767         g_signal_new ("get-logged-in",
1768                       G_TYPE_FROM_CLASS (klass),
1769                       G_SIGNAL_RUN_LAST,
1770                       0,
1771                       g_signal_accumulator_first_wins,
1772                       NULL,
1773                       NULL,
1774                       G_TYPE_BOOLEAN, 0);
1775 }
1776
1777 static void
1778 common_session_init (CommonSession *common_session)
1779 {
1780 }
1781
1782 static void
1783 common_session_finalize (GObject *object)
1784 {
1785     CommonSession *self = COMMON_SESSION (object);
1786
1787     g_free (self->path);
1788     g_free (self->username);
1789 }
1790
1791 static void
1792 common_session_class_init (CommonSessionClass *klass)
1793 {
1794     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1795     object_class->finalize = common_session_finalize;
1796 }