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);
87 session = g_object_new (LIGHTDM_TYPE_SESSION, NULL);
88 priv = GET_PRIVATE (session);
91 priv->key = g_strdup (key);
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);
99 priv->comment = g_strdup ("");
101 sessions = g_list_insert_sorted (sessions, session, compare_session);
109 update_sessions (void)
112 GError *error = NULL;
117 directory = g_dir_open (XSESSIONS_DIR, 0, &error);
119 g_warning ("Failed to open sessions directory: %s", error->message);
120 g_clear_error (&error);
126 const gchar *filename;
131 filename = g_dir_read_name (directory);
132 if (filename == NULL)
135 if (!g_str_has_suffix (filename, ".desktop"))
138 path = g_build_filename (XSESSIONS_DIR, filename, NULL);
140 key_file = g_key_file_new ();
141 result = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
143 g_warning ("Failed to load session file %s: %s:", path, error->message);
144 g_clear_error (&error);
149 LightDMSession *session;
151 key = g_strndup (filename, strlen (filename) - strlen (".desktop"));
152 session = load_session (key_file, key);
154 g_debug ("Loaded session %s (%s, %s)", path, GET_PRIVATE (session)->name, GET_PRIVATE (session)->comment);
156 g_debug ("Ignoring session %s", path);
161 g_key_file_free (key_file);
164 g_dir_close (directory);
166 have_sessions = TRUE;
170 * lightdm_get_sessions:
172 * Get the available sessions.
174 * Return value: (element-type LightDMSession) (transfer none): A list of #LightDMSession
177 lightdm_get_sessions (void)
184 * lightdm_session_get_key
185 * @session: A #LightDMSession
187 * Get the key for a session
189 * Return value: The session key
192 lightdm_session_get_key (LightDMSession *session)
194 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
195 return GET_PRIVATE (session)->key;
199 * lightdm_session_get_name
200 * @session: A #LightDMSession
202 * Get the name for a session
204 * Return value: The session name
207 lightdm_session_get_name (LightDMSession *session)
209 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
210 return GET_PRIVATE (session)->name;
214 * lightdm_session_get_comment
215 * @session: A #LightDMSession
217 * Get the comment for a session
219 * Return value: The session comment
222 lightdm_session_get_comment (LightDMSession *session)
224 g_return_val_if_fail (LIGHTDM_IS_SESSION (session), NULL);
225 return GET_PRIVATE (session)->comment;
229 lightdm_session_init (LightDMSession *session)
234 lightdm_session_set_property (GObject *object,
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 lightdm_session_get_property (GObject *object,
248 LightDMSession *self;
250 self = LIGHTDM_SESSION (object);
254 g_value_set_string (value, lightdm_session_get_key (self));
257 g_value_set_string (value, lightdm_session_get_name (self));
260 g_value_set_string (value, lightdm_session_get_comment (self));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 lightdm_session_class_init (LightDMSessionClass *klass)
271 GObjectClass *object_class = G_OBJECT_CLASS (klass);
273 g_type_class_add_private (klass, sizeof (LightDMSessionPrivate));
275 object_class->set_property = lightdm_session_set_property;
276 object_class->get_property = lightdm_session_get_property;
278 g_object_class_install_property (object_class,
280 g_param_spec_string ("key",
285 g_object_class_install_property (object_class,
287 g_param_spec_string ("name",
292 g_object_class_install_property (object_class,
294 g_param_spec_string ("comment",