]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session-config.c
31de204b498a112a445376035683c82a62017234
[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     /* Desktop names */
20     gchar **desktop_names;
21
22     /* Command to run */
23     gchar *command;
24 };
25
26 G_DEFINE_TYPE (SessionConfig, session_config, G_TYPE_OBJECT);
27
28 SessionConfig *
29 session_config_new_from_file (const gchar *filename, const gchar *default_session_type, GError **error)
30 {
31     GKeyFile *desktop_file;
32     SessionConfig *config;
33     gchar *command;
34
35     desktop_file = g_key_file_new ();
36     if (!g_key_file_load_from_file (desktop_file, filename, G_KEY_FILE_NONE, error))
37         return NULL;
38     command = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
39     if (!command)
40     {
41         g_set_error (error,
42                      G_KEY_FILE_ERROR,
43                      G_KEY_FILE_ERROR_KEY_NOT_FOUND,
44                      "No Exec option in session file: %s", filename);
45         return NULL;
46     }
47
48     config = g_object_new (SESSION_CONFIG_TYPE, NULL);
49     config->priv->command = command;
50     config->priv->session_type = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-Session-Type", NULL);
51     if (!config->priv->session_type)
52         config->priv->session_type = g_strdup (default_session_type);
53
54     config->priv->desktop_names = g_key_file_get_string_list (desktop_file, G_KEY_FILE_DESKTOP_GROUP, "DesktopNames", NULL, NULL);
55     if (!config->priv->desktop_names)
56     {
57         gchar *name;
58
59         name = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, "X-LightDM-DesktopName", NULL);
60         if (name)
61         {
62             config->priv->desktop_names = g_malloc (sizeof (gchar *) * 2);
63             config->priv->desktop_names[0] = name;
64             config->priv->desktop_names[1] = NULL;
65         }
66     }
67
68     g_key_file_free (desktop_file);
69
70     return config;
71 }
72
73 const gchar *
74 session_config_get_command (SessionConfig *config)
75 {
76     g_return_val_if_fail (config != NULL, NULL);
77     return config->priv->command;
78 }
79
80 const gchar *
81 session_config_get_session_type (SessionConfig *config)
82 {
83     g_return_val_if_fail (config != NULL, NULL);
84     return config->priv->session_type;
85 }
86
87 gchar **
88 session_config_get_desktop_names (SessionConfig *config)
89 {
90     g_return_val_if_fail (config != NULL, NULL);
91     return config->priv->desktop_names;
92 }
93
94 static void
95 session_config_init (SessionConfig *config)
96 {
97     config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, SESSION_CONFIG_TYPE, SessionConfigPrivate);
98 }
99
100 static void
101 session_config_finalize (GObject *object)
102 {
103     SessionConfig *self = SESSION_CONFIG (object);
104
105     g_free (self->priv->session_type);
106     g_strfreev (self->priv->desktop_names);
107     g_free (self->priv->command);
108
109     G_OBJECT_CLASS (session_config_parent_class)->finalize (object);
110 }
111
112 static void
113 session_config_class_init (SessionConfigClass *klass)
114 {
115     GObjectClass *object_class = G_OBJECT_CLASS (klass);
116
117     object_class->finalize = session_config_finalize;
118
119     g_type_class_add_private (klass, sizeof (SessionConfigPrivate));
120 }