]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session-config.c
Load and use session types
[sojka/lightdm.git] / src / session-config.c
1 /*
2  * Copyright (C) 2013 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include "session-config.h"
13
14 struct SessionConfigPrivate
15 {
16     /* Session type */
17     gchar *session_type;
18
19     /* Command to run */
20     gchar *command;
21 };
22
23 G_DEFINE_TYPE (SessionConfig, session_config, G_TYPE_OBJECT);
24
25 SessionConfig *
26 session_config_new_from_file (const gchar *filename, GError **error)
27 {
28     GKeyFile *desktop_file;
29     SessionConfig *config;
30     gchar *command;
31
32     desktop_file = g_key_file_new ();
33     if (!g_key_file_load_from_file (desktop_file, filename, G_KEY_FILE_NONE, error))
34         return NULL;
35     command = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
36     if (!command)
37     {
38         g_set_error (error,
39                      G_KEY_FILE_ERROR,
40                      G_KEY_FILE_ERROR_KEY_NOT_FOUND,
41                      "No Exec option in session file: %s", filename);
42         return NULL;
43     }
44
45     config = g_object_new (SESSION_CONFIG_TYPE, NULL);
46     config->priv->command = command;
47     config->priv->session_type = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-Session-Type", NULL);
48     if (!config->priv->session_type)
49         config->priv->session_type = g_strdup ("x");
50
51     g_key_file_free (desktop_file);
52
53     return config;
54 }
55
56 const gchar *
57 session_config_get_command (SessionConfig *config)
58 {
59     g_return_val_if_fail (config != NULL, NULL);
60     return config->priv->command;
61 }
62
63 const gchar *
64 session_config_get_session_type (SessionConfig *config)
65 {
66     g_return_val_if_fail (config != NULL, NULL);
67     return config->priv->session_type;
68 }
69
70 static void
71 session_config_init (SessionConfig *config)
72 {
73     config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, SESSION_CONFIG_TYPE, SessionConfigPrivate);
74 }
75
76 static void
77 session_config_finalize (GObject *object)
78 {
79     SessionConfig *self = SESSION_CONFIG (object);
80
81     g_free (self->priv->session_type);
82     g_free (self->priv->command);
83
84     G_OBJECT_CLASS (session_config_parent_class)->finalize (object);
85 }
86
87 static void
88 session_config_class_init (SessionConfigClass *klass)
89 {
90     GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
92     object_class->finalize = session_config_finalize;
93
94     g_type_class_add_private (klass, sizeof (SessionConfigPrivate));
95 }