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 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
13 #include <gio/gdesktopappinfo.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 *sessions = NULL;
39 compare_session (gconstpointer a, gconstpointer b)
41 LightDMSessionPrivate *priv_a = GET_PRIVATE (a);
42 LightDMSessionPrivate *priv_b = GET_PRIVATE (b);
43 return strcmp (priv_a->name, priv_b->name);
46 static LightDMSession *
47 load_session (GKeyFile *key_file, const gchar *key)
50 LightDMSession *session;
51 LightDMSessionPrivate *priv;
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))
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);
61 domain = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Gettext-Domain", NULL);
63 name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, domain, NULL);
66 g_warning ("Ignoring session without name");
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);
76 full_path = g_find_program_in_path (try_exec);
88 session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
89 priv = GET_PRIVATE (session);
92 priv->key = g_strdup (key);
97 g_free (priv->comment);
98 priv->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, domain, NULL);
100 priv->comment = g_strdup ("");
102 sessions = g_list_insert_sorted (sessions, session, compare_session);
110 update_sessions (void)
113 GError *error = NULL;
118 directory = g_dir_open (XSESSIONS_DIR, 0, &error);
120 g_warning ("Failed to open sessions directory: %s", error->message);
121 g_clear_error (&error);
127 const gchar *filename;
132 filename = g_dir_read_name (directory);
133 if (filename == NULL)
136 if (!g_str_has_suffix (filename, ".desktop"))
139 path = g_build_filename (XSESSIONS_DIR, filename, NULL);
141 key_file = g_key_file_new ();
142 result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
144 g_warning ("Failed to load session file %s: %s:", path, error->message);
145 g_clear_error (&error);
150 LightDMSession *session;
152 key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
153 session = load_session (key_file, key);
155 g_debug ("Loaded session %s (%s, %s)", path, GET_PRIVATE (session)->name, GET_PRIVATE (session)->comment);
157 g_debug ("Ignoring session %s", path);
162 g_key_file_free (key_file);
165 g_dir_close (directory);
167 have_sessions = TRUE;
171 * lightdm_get_sessions:
173 * Get the available sessions.
175 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
178 lightdm_get_sessions (void)
185 * lightdm_session_get_key
186 * @session: A #LightDMSession
188 * Get the key for a session
190 * Return value: The session key
193 lightdm_session_get_key (LightDMSession *session)
195 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
196 return GET_PRIVATE (session)->key;
200 * lightdm_session_get_name
201 * @session: A #LightDMSession
203 * Get the name for a session
205 * Return value: The session name
208 lightdm_session_get_name (LightDMSession *session)
210 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
211 return GET_PRIVATE (session)->name;
215 * lightdm_session_get_comment
216 * @session: A #LightDMSession
218 * Get the comment for a session
220 * Return value: The session comment
223 lightdm_session_get_comment (LightDMSession *session)
225 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
226 return GET_PRIVATE (session)->comment;
230 lightdm_session_init (LightDMSession *session)
235 lightdm_session_set_property (GObject *object,
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 lightdm_session_get_property (GObject *object,
249 LightDMSession *self;
251 self = LIGHTDM_SESSION (object);
255 g_value_set_string (value, lightdm_session_get_key (self));
258 g_value_set_string (value, lightdm_session_get_name (self));
261 g_value_set_string (value, lightdm_session_get_comment (self));
264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 lightdm_session_finalize (GObject *object)
272 LightDMSession *self = LIGHTDM_SESSION (object);
273 LightDMSessionPrivate *priv = GET_PRIVATE (self);
277 g_free (priv->comment);
281 lightdm_session_class_init (LightDMSessionClass *klass)
283 GObjectClass *object_class = G_OBJECT_CLASS (klass);
285 g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
287 object_class->set_property = lightdm_session_set_property;
288 object_class->get_property = lightdm_session_get_property;
289 object_class->finalize = lightdm_session_finalize;
291 g_object_class_install_property (object_class,
293 g_param_spec_string ("key",
298 g_object_class_install_property (object_class,
300 g_param_spec_string ("name",
305 g_object_class_install_property (object_class,
307 g_param_spec_string ("comment",