2 * Copyright (C) 2010 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
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.
12 #include <gio/gdesktopappinfo.h>
14 #include "lightdm/session.h"
28 } LightDMSessionPrivate;
30 G_DEFINE_TYPE (LightDMSession, lightdm_session, G_TYPE_OBJECT);
32 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_SESSION, LightDMSessionPrivate)
34 static gboolean have_sessions = FALSE;
35 static GList *local_sessions = NULL;
36 static GList *remote_sessions = NULL;
37 static GList *mir_sessions = NULL;
40 compare_session (gconstpointer a, gconstpointer b)
42 LightDMSessionPrivate *priv_a = GET_PRIVATE (a);
43 LightDMSessionPrivate *priv_b = GET_PRIVATE (b);
44 return strcmp (priv_a->name, priv_b->name);
47 static LightDMSession *
48 load_session (GKeyFile *key_file, const gchar *key)
51 LightDMSession *session;
52 LightDMSessionPrivate *priv;
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))
59 #ifdef G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN
60 domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, NULL);
62 domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Gettext-Domain", NULL);
64 name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, domain, NULL);
67 g_warning ("Ignoring session without name");
72 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 full_path = g_find_program_in_path (try_exec);
89 session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
90 priv = GET_PRIVATE (session);
93 priv->key = g_strdup (key);
98 g_free (priv->comment);
99 priv->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, domain, NULL);
101 priv->comment = g_strdup ("");
109 load_sessions (const gchar *sessions_dir)
112 GList *sessions = NULL;
113 GError *error = NULL;
115 directory = g_dir_open (sessions_dir, 0, &error);
117 g_warning ("Failed to open sessions directory: %s", error->message);
118 g_clear_error (&error);
124 const gchar *filename;
129 filename = g_dir_read_name (directory);
130 if (filename == NULL)
133 if (!g_str_has_suffix (filename, ".desktop"))
136 path = g_build_filename (sessions_dir, filename, NULL);
138 key_file = g_key_file_new ();
139 result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
141 g_warning ("Failed to load session file %s: %s:", path, error->message);
142 g_clear_error (&error);
147 LightDMSession *session;
149 key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
150 session = load_session (key_file, key);
153 g_debug ("Loaded session %s (%s, %s)", path, GET_PRIVATE (session)->name, GET_PRIVATE (session)->comment);
154 sessions = g_list_insert_sorted (sessions, session, compare_session);
157 g_debug ("Ignoring session %s", path);
162 g_key_file_free (key_file);
165 g_dir_close (directory);
171 update_sessions (void)
173 GKeyFile *config_key_file = NULL;
174 gchar *config_path = NULL;
175 gchar *xsessions_dir;
176 gchar *remote_sessions_dir;
177 gchar *mir_sessions_dir;
179 GError *error = NULL;
184 xsessions_dir = g_strdup (XSESSIONS_DIR);
185 remote_sessions_dir = g_strdup (REMOTE_SESSIONS_DIR);
186 mir_sessions_dir = g_strdup (MIR_SESSIONS_DIR);
188 /* Use session directory from configuration */
189 /* FIXME: This should be sent in the greeter connection */
190 config_path = g_build_filename (CONFIG_DIR, "lightdm.conf", NULL);
191 config_key_file = g_key_file_new ();
192 result = g_key_file_load_from_file (config_key_file, config_path, G_KEY_FILE_NONE, &error);
194 g_warning ("Failed to open configuration file: %s", error->message);
195 g_clear_error (&error);
200 value = g_key_file_get_string (config_key_file, "LightDM", "xsessions-directory", NULL);
203 g_free (xsessions_dir);
204 xsessions_dir = value;
207 value = g_key_file_get_string (config_key_file, "LightDM", "remote-sessions-directory", NULL);
210 g_free (remote_sessions_dir);
211 remote_sessions_dir = value;
214 value = g_key_file_get_string (config_key_file, "LightDM", "mir-sessions-directory", NULL);
217 g_free (mir_sessions_dir);
218 mir_sessions_dir = value;
221 g_key_file_free (config_key_file);
222 g_free (config_path);
224 local_sessions = load_sessions (xsessions_dir);
225 remote_sessions = load_sessions (remote_sessions_dir);
226 mir_sessions = load_sessions (mir_sessions_dir);
228 g_free (xsessions_dir);
229 g_free (remote_sessions_dir);
231 have_sessions = TRUE;
235 * lightdm_get_sessions:
237 * Get the available sessions.
239 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
242 lightdm_get_sessions (void)
245 return local_sessions;
249 * lightdm_get_remote_sessions:
251 * Get the available remote sessions.
253 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
256 lightdm_get_remote_sessions (void)
259 return remote_sessions;
263 * lightdm_get_mir_sessions:
265 * Get the available Mir sessions.
267 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
270 lightdm_get_mir_sessions (void)
277 * lightdm_session_get_key:
278 * @session: A #LightDMSession
280 * Get the key for a session
282 * Return value: The session key
285 lightdm_session_get_key (LightDMSession *session)
287 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
288 return GET_PRIVATE (session)->key;
292 * lightdm_session_get_name:
293 * @session: A #LightDMSession
295 * Get the name for a session
297 * Return value: The session name
300 lightdm_session_get_name (LightDMSession *session)
302 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
303 return GET_PRIVATE (session)->name;
307 * lightdm_session_get_comment:
308 * @session: A #LightDMSession
310 * Get the comment for a session
312 * Return value: The session comment
315 lightdm_session_get_comment (LightDMSession *session)
317 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
318 return GET_PRIVATE (session)->comment;
322 lightdm_session_init (LightDMSession *session)
327 lightdm_session_set_property (GObject *object,
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 lightdm_session_get_property (GObject *object,
341 LightDMSession *self;
343 self = LIGHTDM_SESSION (object);
347 g_value_set_string (value, lightdm_session_get_key (self));
350 g_value_set_string (value, lightdm_session_get_name (self));
353 g_value_set_string (value, lightdm_session_get_comment (self));
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
362 lightdm_session_finalize (GObject *object)
364 LightDMSession *self = LIGHTDM_SESSION (object);
365 LightDMSessionPrivate *priv = GET_PRIVATE (self);
369 g_free (priv->comment);
373 lightdm_session_class_init (LightDMSessionClass *klass)
375 GObjectClass *object_class = G_OBJECT_CLASS (klass);
377 g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
379 object_class->set_property = lightdm_session_set_property;
380 object_class->get_property = lightdm_session_get_property;
381 object_class->finalize = lightdm_session_finalize;
383 g_object_class_install_property (object_class,
385 g_param_spec_string ("key",
390 g_object_class_install_property (object_class,
392 g_param_spec_string ("name",
397 g_object_class_install_property (object_class,
399 g_param_spec_string ("comment",