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 "configuration.h"
15 #include "lightdm/session.h"
29 } LightDMSessionPrivate;
31 G_DEFINE_TYPE (LightDMSession, lightdm_session, G_TYPE_OBJECT);
33 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_SESSION, LightDMSessionPrivate)
35 static gboolean have_sessions = FALSE;
36 static GList *local_sessions = NULL;
37 static GList *remote_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, const gchar *default_type)
50 gchar *domain, *name, *type;
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 type = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-Session-Type", NULL);
91 type = strdup (default_type);
93 session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
94 priv = GET_PRIVATE (session);
97 priv->key = g_strdup (key);
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);
108 priv->comment = g_strdup ("");
116 load_sessions_dir (GList *sessions, const gchar *sessions_dir, const gchar *default_type)
119 GError *error = NULL;
121 directory = g_dir_open (sessions_dir, 0, &error);
122 if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
123 g_warning ("Failed to open sessions directory: %s", error->message);
124 g_clear_error (&error);
130 const gchar *filename;
135 filename = g_dir_read_name (directory);
136 if (filename == NULL)
139 if (!g_str_has_suffix (filename, ".desktop"))
142 path = g_build_filename (sessions_dir, filename, NULL);
144 key_file = g_key_file_new ();
145 result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
147 g_warning ("Failed to load session file %s: %s:", path, error->message);
148 g_clear_error (&error);
153 LightDMSession *session;
155 key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
156 session = load_session (key_file, key, default_type);
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);
163 g_debug ("Ignoring session %s", path);
168 g_key_file_free (key_file);
171 g_dir_close (directory);
177 load_sessions (const gchar *sessions_dir)
179 GList *sessions = NULL;
183 dirs = g_strsplit (sessions_dir, ":", -1);
184 for (i = 0; dirs[i]; i++)
186 const gchar *default_type = "x";
188 if (strcmp (dirs[i], WAYLAND_SESSIONS_DIR) == 0)
189 default_type = "wayland";
191 sessions = load_sessions_dir (sessions, dirs[i], default_type);
200 update_sessions (void)
203 gchar *remote_sessions_dir;
209 sessions_dir = g_strdup (SESSIONS_DIR);
210 remote_sessions_dir = g_strdup (REMOTE_SESSIONS_DIR);
212 /* Use session directory from configuration */
213 config_load_from_standard_locations (config_get_instance (), NULL, NULL);
215 value = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
218 g_free (sessions_dir);
219 sessions_dir = value;
222 value = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
225 g_free (remote_sessions_dir);
226 remote_sessions_dir = value;
229 local_sessions = load_sessions (sessions_dir);
230 remote_sessions = load_sessions (remote_sessions_dir);
232 g_free (sessions_dir);
233 g_free (remote_sessions_dir);
235 have_sessions = TRUE;
239 * lightdm_get_sessions:
241 * Get the available sessions.
243 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
246 lightdm_get_sessions (void)
249 return local_sessions;
253 * lightdm_get_remote_sessions:
255 * Get the available remote sessions.
257 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
260 lightdm_get_remote_sessions (void)
263 return remote_sessions;
267 * lightdm_session_get_key:
268 * @session: A #LightDMSession
270 * Get the key for a session
272 * Return value: The session key
275 lightdm_session_get_key (LightDMSession *session)
277 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
278 return GET_PRIVATE (session)->key;
282 * lightdm_session_get_session_type:
283 * @session: A #LightDMSession
285 * Get the type a session
287 * Return value: The session type, e.g. x or mir
290 lightdm_session_get_session_type (LightDMSession *session)
292 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
293 return GET_PRIVATE (session)->type;
297 * lightdm_session_get_name:
298 * @session: A #LightDMSession
300 * Get the name for a session
302 * Return value: The session name
305 lightdm_session_get_name (LightDMSession *session)
307 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
308 return GET_PRIVATE (session)->name;
312 * lightdm_session_get_comment:
313 * @session: A #LightDMSession
315 * Get the comment for a session
317 * Return value: The session comment
320 lightdm_session_get_comment (LightDMSession *session)
322 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
323 return GET_PRIVATE (session)->comment;
327 lightdm_session_init (LightDMSession *session)
332 lightdm_session_set_property (GObject *object,
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 lightdm_session_get_property (GObject *object,
346 LightDMSession *self;
348 self = LIGHTDM_SESSION (object);
352 g_value_set_string (value, lightdm_session_get_key (self));
355 g_value_set_string (value, lightdm_session_get_name (self));
358 g_value_set_string (value, lightdm_session_get_comment (self));
361 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 lightdm_session_finalize (GObject *object)
369 LightDMSession *self = LIGHTDM_SESSION (object);
370 LightDMSessionPrivate *priv = GET_PRIVATE (self);
375 g_free (priv->comment);
379 lightdm_session_class_init (LightDMSessionClass *klass)
381 GObjectClass *object_class = G_OBJECT_CLASS (klass);
383 g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
385 object_class->set_property = lightdm_session_set_property;
386 object_class->get_property = lightdm_session_get_property;
387 object_class->finalize = lightdm_session_finalize;
389 g_object_class_install_property (object_class,
391 g_param_spec_string ("key",
396 g_object_class_install_property (object_class,
398 g_param_spec_string ("name",
403 g_object_class_install_property (object_class,
405 g_param_spec_string ("comment",