]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/session.c
Remove unused variables.
[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 2 or version 3 of the License.
8  * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
9  */
10
11 #include <string.h>
12 #include <gio/gdesktopappinfo.h>
13
14 #include "lightdm/session.h"
15
16 enum {
17     PROP_0,
18     PROP_KEY,
19     PROP_NAME,
20     PROP_COMMENT
21 };
22
23 typedef struct
24 {
25     gchar *key;
26     gchar *type;
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 *local_sessions = NULL;
37 static GList *remote_sessions = NULL;
38
39 static gint 
40 compare_session (gconstpointer a, gconstpointer b)
41 {
42     LightDMSessionPrivate *priv_a = GET_PRIVATE (a);
43     LightDMSessionPrivate *priv_b = GET_PRIVATE (b);
44     return strcmp (priv_a->name, priv_b->name);
45 }
46
47 static LightDMSession *
48 load_session (GKeyFile *key_file, const gchar *key)
49 {
50     gchar *type, *domain, *name;
51     LightDMSession *session;
52     LightDMSessionPrivate *priv;
53     gchar *try_exec;
54   
55     if (g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) ||
56         g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL))
57         return NULL;
58
59     type = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-Session-Type", NULL);
60     if (!type)
61         type = "x";
62
63 #ifdef G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN
64     domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, NULL);
65 #else
66     domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Gettext-Domain", NULL);
67 #endif
68     name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, domain, NULL);
69     if (!name)
70     {
71         g_warning ("Ignoring session without name");
72         g_free (domain);
73         return NULL;
74     }
75
76     try_exec = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TRY_EXEC, domain, NULL);
77     if (try_exec)
78     {
79         gchar *full_path;
80
81         full_path = g_find_program_in_path (try_exec);
82         g_free (try_exec);
83
84         if (!full_path)
85         {
86             g_free (name);
87             g_free (domain);
88             return NULL;
89         }
90         g_free (full_path);
91     }
92
93     session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
94     priv = GET_PRIVATE (session);
95
96     g_free (priv->key);
97     priv->key = g_strdup (key);
98
99     g_free (priv->type);
100     priv->type = g_strdup (type);
101
102     g_free (priv->name);
103     priv->name = name;
104
105     g_free (priv->comment);
106     priv->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, domain, NULL);
107     if (!priv->comment)
108         priv->comment = g_strdup ("");
109
110     g_free (domain);
111
112     return session;
113 }
114
115 static GList *
116 load_sessions_dir (GList *sessions, const gchar *sessions_dir)
117 {
118     GDir *directory;
119     GError *error = NULL;
120
121     directory = g_dir_open (sessions_dir, 0, &error);
122     if (error)
123         g_warning ("Failed to open sessions directory: %s", error->message);
124     g_clear_error (&error);
125     if (!directory)
126         return sessions;
127
128     while (TRUE)
129     {
130         const gchar *filename;
131         gchar *path;
132         GKeyFile *key_file;
133         gboolean result;
134
135         filename = g_dir_read_name (directory);
136         if (filename == NULL)
137             break;
138
139         if (!g_str_has_suffix (filename, ".desktop"))
140             continue;
141
142         path = g_build_filename (sessions_dir, filename, NULL);
143
144         key_file = g_key_file_new ();
145         result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
146         if (error)
147             g_warning ("Failed to load session file %s: %s:", path, error->message);
148         g_clear_error (&error);
149
150         if (result)
151         {
152             gchar *key;
153             LightDMSession *session;
154
155             key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
156             session = load_session (key_file, key);
157             if (session)
158             {
159                 g_debug ("Loaded session %s (%s, %s)", path, GET_PRIVATE (session)->name, GET_PRIVATE (session)->comment);
160                 sessions = g_list_insert_sorted (sessions, session, compare_session);
161             }
162             else
163                 g_debug ("Ignoring session %s", path);
164             g_free (key);
165         }
166
167         g_free (path);
168         g_key_file_free (key_file);
169     }
170
171     g_dir_close (directory);
172   
173     return sessions;
174 }
175
176 static GList *
177 load_sessions (const gchar *sessions_dir)
178 {
179     GList *sessions = NULL;
180     gchar **dirs;
181     int i;
182
183     dirs = g_strsplit (sessions_dir, ":", -1);
184     for (i = 0; dirs[i]; i++)
185         sessions = load_sessions_dir (sessions, dirs[i]);
186     g_strfreev (dirs);
187   
188     return sessions;
189 }
190
191 static void
192 update_sessions (void)
193 {
194     GKeyFile *config_key_file = NULL;
195     gchar *config_path = NULL;
196     gchar *sessions_dir;
197     gchar *remote_sessions_dir;
198     gboolean result;
199     GError *error = NULL;
200
201     if (have_sessions)
202         return;
203
204     sessions_dir = g_strdup (SESSIONS_DIR);
205     remote_sessions_dir = g_strdup (REMOTE_SESSIONS_DIR);
206
207     /* Use session directory from configuration */
208     /* FIXME: This should be sent in the greeter connection */
209     config_path = g_build_filename (CONFIG_DIR, "lightdm.conf", NULL);
210     config_key_file = g_key_file_new ();
211     result = g_key_file_load_from_file (config_key_file, config_path, G_KEY_FILE_NONE, &error);
212     if (error)
213         g_warning ("Failed to open configuration file: %s", error->message);
214     g_clear_error (&error);
215     if (result)
216     {
217         gchar *value;
218       
219         value = g_key_file_get_string (config_key_file, "LightDM", "sessions-directory", NULL);
220         if (value)
221         {
222             g_free (sessions_dir);
223             sessions_dir = value;
224         }
225
226         value = g_key_file_get_string (config_key_file, "LightDM", "remote-sessions-directory", NULL);
227         if (value)
228         {
229             g_free (remote_sessions_dir);
230             remote_sessions_dir = value;
231         }
232     }
233     g_key_file_free (config_key_file);
234     g_free (config_path);
235
236     local_sessions = load_sessions (sessions_dir);
237     remote_sessions = load_sessions (remote_sessions_dir);
238
239     g_free (sessions_dir);
240     g_free (remote_sessions_dir);
241
242     have_sessions = TRUE;
243 }
244
245 /**
246  * lightdm_get_sessions:
247  *
248  * Get the available sessions.
249  *
250  * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
251  **/
252 GList *
253 lightdm_get_sessions (void)
254 {
255     update_sessions ();
256     return local_sessions;
257 }
258
259 /**
260  * lightdm_get_remote_sessions:
261  *
262  * Get the available remote sessions.
263  *
264  * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
265  **/
266 GList *
267 lightdm_get_remote_sessions (void)
268 {
269     update_sessions ();
270     return remote_sessions;
271 }
272
273 /**
274  * lightdm_session_get_key:
275  * @session: A #LightDMSession
276  * 
277  * Get the key for a session
278  * 
279  * Return value: The session key
280  **/
281 const gchar *
282 lightdm_session_get_key (LightDMSession *session)
283 {
284     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
285     return GET_PRIVATE (session)->key;
286 }
287
288 /**
289  * lightdm_session_get_session_type:
290  * @session: A #LightDMSession
291  * 
292  * Get the type a session
293  * 
294  * Return value: The session type, e.g. x or mir
295  **/
296 const gchar *
297 lightdm_session_get_session_type (LightDMSession *session)
298 {
299     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
300     return GET_PRIVATE (session)->type;
301 }
302
303 /**
304  * lightdm_session_get_name:
305  * @session: A #LightDMSession
306  * 
307  * Get the name for a session
308  * 
309  * Return value: The session name
310  **/
311 const gchar *
312 lightdm_session_get_name (LightDMSession *session)
313 {
314     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
315     return GET_PRIVATE (session)->name;
316 }
317
318 /**
319  * lightdm_session_get_comment:
320  * @session: A #LightDMSession
321  * 
322  * Get the comment for a session
323  * 
324  * Return value: The session comment
325  **/
326 const gchar *
327 lightdm_session_get_comment (LightDMSession *session)
328 {
329     g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
330     return GET_PRIVATE (session)->comment;
331 }
332
333 static void
334 lightdm_session_init (LightDMSession *session)
335 {
336 }
337
338 static void
339 lightdm_session_set_property (GObject      *object,
340                           guint         prop_id,
341                           const GValue *value,
342                           GParamSpec   *pspec)
343 {
344     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 }
346
347 static void
348 lightdm_session_get_property (GObject    *object,
349                           guint       prop_id,
350                           GValue     *value,
351                           GParamSpec *pspec)
352 {
353     LightDMSession *self;
354
355     self = LIGHTDM_SESSION (object);
356
357     switch (prop_id) {
358     case PROP_KEY:
359         g_value_set_string (value, lightdm_session_get_key (self));
360         break;
361     case PROP_NAME:
362         g_value_set_string (value, lightdm_session_get_name (self));
363         break;
364     case PROP_COMMENT:
365         g_value_set_string (value, lightdm_session_get_comment (self));
366         break;
367     default:
368         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369         break;
370     }
371 }
372
373 static void
374 lightdm_session_finalize (GObject *object)
375 {
376     LightDMSession *self = LIGHTDM_SESSION (object);
377     LightDMSessionPrivate *priv = GET_PRIVATE (self);
378
379     g_free (priv->key);
380     g_free (priv->type);
381     g_free (priv->name);
382     g_free (priv->comment);
383 }
384
385 static void
386 lightdm_session_class_init (LightDMSessionClass *klass)
387 {
388     GObjectClass *object_class = G_OBJECT_CLASS (klass);
389   
390     g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
391
392     object_class->set_property = lightdm_session_set_property;
393     object_class->get_property = lightdm_session_get_property;
394     object_class->finalize = lightdm_session_finalize;
395
396     g_object_class_install_property (object_class,
397                                      PROP_KEY,
398                                      g_param_spec_string ("key",
399                                                           "key",
400                                                           "Session key",
401                                                           NULL,
402                                                           G_PARAM_READABLE));
403     g_object_class_install_property (object_class,
404                                      PROP_NAME,
405                                      g_param_spec_string ("name",
406                                                           "name",
407                                                           "Session name",
408                                                           NULL,
409                                                           G_PARAM_READABLE));
410     g_object_class_install_property (object_class,
411                                      PROP_COMMENT,
412                                      g_param_spec_string ("comment",
413                                                           "comment",
414                                                           "Session comment",
415                                                           NULL,
416                                                           G_PARAM_READABLE));
417 }