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