~robert-ancell/lightdm/weston-compositor-merge

« back to all changes in this revision

Viewing changes to liblightdm-gobject/session.c

  • Committer: Robert Ancell
  • Date: 2011-07-19 06:40:33 UTC
  • Revision ID: robert.ancell@canonical.com-20110719064033-zrm9i25392n2gw4v
Really tidy up liblightdm

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * license.
10
10
 */
11
11
 
 
12
#include <string.h>
 
13
#include <gio/gdesktopappinfo.h>
 
14
 
12
15
#include "lightdm/session.h"
13
16
 
14
17
enum {
29
32
 
30
33
#define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_SESSION, LightDMSessionPrivate)
31
34
 
 
35
static gboolean have_sessions = FALSE;
 
36
static GList *sessions = NULL;
 
37
 
 
38
static void
 
39
update_sessions (void)
 
40
{
 
41
    GDir *directory;
 
42
    GError *error = NULL;
 
43
 
 
44
    if (have_sessions)
 
45
        return;
 
46
 
 
47
    directory = g_dir_open (XSESSIONS_DIR, 0, &error);
 
48
    if (!directory)
 
49
        g_warning ("Failed to open sessions directory: %s", error->message);
 
50
    g_clear_error (&error);
 
51
    if (!directory)
 
52
        return;
 
53
 
 
54
    while (TRUE)
 
55
    {
 
56
        const gchar *filename;
 
57
        GKeyFile *key_file;
 
58
        gchar *key, *path;
 
59
        gboolean result;
 
60
 
 
61
        filename = g_dir_read_name (directory);
 
62
        if (filename == NULL)
 
63
            break;
 
64
 
 
65
        if (!g_str_has_suffix (filename, ".desktop"))
 
66
            continue;
 
67
 
 
68
        key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
 
69
        path = g_build_filename (XSESSIONS_DIR, filename, NULL);
 
70
        g_debug ("Loading session %s", path);
 
71
 
 
72
        key_file = g_key_file_new ();
 
73
        result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
 
74
        if (!result)
 
75
            g_warning ("Failed to load session file %s: %s:", path, error->message);
 
76
        g_clear_error (&error);
 
77
 
 
78
        if (result && !g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL))
 
79
        {
 
80
            gchar *domain, *name, *comment;
 
81
 
 
82
#ifdef G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN
 
83
            domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, NULL);
 
84
#else
 
85
            domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Gettext-Domain", NULL);
 
86
#endif
 
87
            name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, domain, NULL);
 
88
            comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, domain, NULL);
 
89
            if (!comment)
 
90
                comment = g_strdup ("");
 
91
            if (name)
 
92
            {
 
93
                g_debug ("Loaded session %s (%s, %s)", key, name, comment);
 
94
                sessions = g_list_append (sessions, g_object_new (LIGHTDM_TYPE_SESSION, "key", key, "name", name, "comment", comment, NULL));
 
95
            }
 
96
            else
 
97
                g_warning ("Invalid session %s: %s", path, error->message);
 
98
            g_free (domain);
 
99
            g_free (name);
 
100
            g_free (comment);
 
101
        }
 
102
 
 
103
        g_free (key);
 
104
        g_free (path);
 
105
        g_key_file_free (key_file);
 
106
    }
 
107
 
 
108
    g_dir_close (directory);
 
109
 
 
110
    have_sessions = TRUE;
 
111
}
 
112
 
 
113
/**
 
114
 * lightdm_get_sessions:
 
115
 *
 
116
 * Get the available sessions.
 
117
 *
 
118
 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
 
119
 **/
 
120
GList *
 
121
lightdm_get_sessions (void)
 
122
{
 
123
    update_sessions ();
 
124
    return sessions;
 
125
}
 
126
 
32
127
/**
33
128
 * lightdm_session_get_key
34
129
 * @session: A #LightDMSession