]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/session.c
Use paranoid error checking in liblightdm-gobject
[sojka/lightdm.git] / liblightdm-gobject / session.c
1 /*
2  * Copyright (C) 2010 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
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
9  * license.
10  */
11
12 #include <string.h>
13 #include <gio/gdesktopappinfo.h>
14
15 #include "lightdm/session.h"
16
17 enum {
18     PROP_0,
19     PROP_KEY,
20     PROP_NAME,
21     PROP_COMMENT
22 };
23
24 typedef struct
25 {
26     gchar *key;
27     gchar *name;
28     gchar *comment;
29 } LightDMSessionPrivate;
30
31 G_DEFINE_TYPE (LightDMSession, lightdm_session, G_TYPE_OBJECT);
32
33 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_SESSION, LightDMSessionPrivate)
34
35 static gboolean have_sessions = FALSE;
36 static GList *sessions = NULL;
37
38 static gint 
39 compare_session (gconstpointer a, gconstpointer b)
40 {
41     LightDMSessionPrivate *priv_a = GET_PRIVATE (a);
42     LightDMSessionPrivate *priv_b = GET_PRIVATE (b);
43     return strcmp (priv_a->name, priv_b->name);
44 }
45
46 static LightDMSession *
47 load_session (GKeyFile *key_file, const gchar *key)
48 {
49     gchar *domain, *name;
50     LightDMSession *session;
51     LightDMSessionPrivate *priv;
52     gchar *try_exec;
53   
54     if (g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) ||
55         g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL))
56         return NULL;
57
58 #ifdef G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN
59     domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, NULL);
60 #else
61     domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Gettext-Domain", NULL);
62 #endif
63     name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, domain, NULL);
64     if (!name)
65     {
66         g_warning ("Ignoring session without name");
67         g_free (domain);
68         return NULL;
69     }
70
71     try_exec = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TRY_EXEC, domain, NULL);
72     if (try_exec)
73     {
74         gchar *full_path;
75
76         full_path = g_find_program_in_path (try_exec);
77         g_free (try_exec);
78
79         if (!full_path)
80         {
81             g_free (domain);
82             return NULL;
83         }
84         g_free (full_path);
85     }
86
87     session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
88     priv = GET_PRIVATE (session);
89
90     g_free (priv->key);
91     priv->key = g_strdup (key);
92
93     g_free (priv->name);
94     priv->name = name;
95
96     g_free (priv->comment);
97     priv->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, domain, NULL);
98     if (!priv->comment)
99         priv->comment = g_strdup ("");
100
101     sessions = g_list_insert_sorted (sessions, session, compare_session);
102
103     g_free (domain);
104
105     return session;
106 }
107
108 static void
109 update_sessions (void)
110 {
111     GDir *directory;
112     GError *error = NULL;
113
114     if (have_sessions)
115         return;
116
117     directory = g_dir_open (XSESSIONS_DIR, 0, &error);
118     if (error)
119         g_warning ("Failed to open sessions directory: %s", error->message);
120     g_clear_error (&error);
121     if (!directory)
122         return;
123
124     while (TRUE)
125     {
126         const gchar *filename;
127         gchar *path;
128         GKeyFile *key_file;
129         gboolean result;
130
131         filename = g_dir_read_name (directory);
132         if (filename == NULL)
133             break;
134
135         if (!g_str_has_suffix (filename, ".desktop"))
136             continue;
137
138         path = g_build_filename (XSESSIONS_DIR, filename, NULL);
139
140         key_file = g_key_file_new ();
141         result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
142         if (error)
143             g_warning ("Failed to load session file %s: %s:", path, error->message);
144         g_clear_error (&error);
145
146         if (result)
147         {
148             gchar *key;
149             LightDMSession *session;
150
151             key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
152             session = load_session (key_file, key);
153             if (session)
154                 g_debug ("Loaded session %s (%s, %s)", path, GET_PRIVATE (session)->name, GET_PRIVATE (session)->comment);
155             else
156                 g_debug ("Ignoring session %s", path);
157             g_free (key);
158         }
159
160         g_free (path);
161         g_key_file_free (key_file);
162     }
163
164     g_dir_close (directory);
165
166     have_sessions = TRUE;
167 }
168
169 /**
170  * lightdm_get_sessions:
171  *
172  * Get the available sessions.
173  *
174  * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
175  **/
176 GList *
177 lightdm_get_sessions (void)
178 {
179     update_sessions ();
180     return sessions;
181 }
182
183 /**
184  * lightdm_session_get_key
185  * @session: A #LightDMSession
186  * 
187  * Get the key for a session
188  * 
189  * Return value: The session key
190  **/
191 const gchar *
192 lightdm_session_get_key (LightDMSession *session)
193 {
194     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
195     return GET_PRIVATE (session)->key;
196 }
197
198 /**
199  * lightdm_session_get_name
200  * @session: A #LightDMSession
201  * 
202  * Get the name for a session
203  * 
204  * Return value: The session name
205  **/
206 const gchar *
207 lightdm_session_get_name (LightDMSession *session)
208 {
209     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
210     return GET_PRIVATE (session)->name;
211 }
212
213 /**
214  * lightdm_session_get_comment
215  * @session: A #LightDMSession
216  * 
217  * Get the comment for a session
218  * 
219  * Return value: The session comment
220  **/
221 const gchar *
222 lightdm_session_get_comment (LightDMSession *session)
223 {
224     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
225     return GET_PRIVATE (session)->comment;
226 }
227
228 static void
229 lightdm_session_init (LightDMSession *session)
230 {
231 }
232
233 static void
234 lightdm_session_set_property (GObject      *object,
235                           guint         prop_id,
236                           const GValue *value,
237                           GParamSpec   *pspec)
238 {
239     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 }
241
242 static void
243 lightdm_session_get_property (GObject    *object,
244                           guint       prop_id,
245                           GValue     *value,
246                           GParamSpec *pspec)
247 {
248     LightDMSession *self;
249
250     self = LIGHTDM_SESSION (object);
251
252     switch (prop_id) {
253     case PROP_KEY:
254         g_value_set_string (value, lightdm_session_get_key (self));
255         break;
256     case PROP_NAME:
257         g_value_set_string (value, lightdm_session_get_name (self));
258         break;
259     case PROP_COMMENT:
260         g_value_set_string (value, lightdm_session_get_comment (self));
261         break;
262     default:
263         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264         break;
265     }
266 }
267
268 static void
269 lightdm_session_class_init (LightDMSessionClass *klass)
270 {
271     GObjectClass *object_class = G_OBJECT_CLASS (klass);
272   
273     g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
274
275     object_class->set_property = lightdm_session_set_property;
276     object_class->get_property = lightdm_session_get_property;
277
278     g_object_class_install_property (object_class,
279                                      PROP_KEY,
280                                      g_param_spec_string ("key",
281                                                           "key",
282                                                           "Session key",
283                                                           NULL,
284                                                           G_PARAM_READABLE));
285     g_object_class_install_property (object_class,
286                                      PROP_NAME,
287                                      g_param_spec_string ("name",
288                                                           "name",
289                                                           "Session name",
290                                                           NULL,
291                                                           G_PARAM_READABLE));
292     g_object_class_install_property (object_class,
293                                      PROP_COMMENT,
294                                      g_param_spec_string ("comment",
295                                                           "comment",
296                                                           "Session comment",
297                                                           NULL,
298                                                           G_PARAM_READABLE));
299 }