]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session-config.c
Use a SessionConfig class for the session .desktop file
[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     /* Command to run */
17     gchar *command;
18 };
19
20 G_DEFINE_TYPE (SessionConfig, session_config, G_TYPE_OBJECT);
21
22 SessionConfig *
23 session_config_new_from_file (const gchar *filename, GError **error)
24 {
25     GKeyFile *desktop_file;
26     SessionConfig *config;
27     gchar *command;
28
29     desktop_file = g_key_file_new ();
30     if (!g_key_file_load_from_file (desktop_file, filename, G_KEY_FILE_NONE, error))
31         return NULL;
32     command = g_key_file_get_string (desktop_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
33     if (!command)
34     {
35         g_set_error (error,
36                      G_KEY_FILE_ERROR,
37                      G_KEY_FILE_ERROR_KEY_NOT_FOUND,
38                      "No Exec option in session file: %s", filename);
39         return NULL;
40     }
41     g_key_file_free (desktop_file);
42
43     config = g_object_new (SESSION_CONFIG_TYPE, NULL);
44     config->priv->command = command;
45
46     return config;
47 }
48
49 const gchar *
50 session_config_get_command (SessionConfig *config)
51 {
52     g_return_val_if_fail (config != NULL, NULL);
53
54     return config->priv->command;
55 }
56
57 static void
58 session_config_init (SessionConfig *config)
59 {
60     config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, SESSION_CONFIG_TYPE, SessionConfigPrivate);
61 }
62
63 static void
64 session_config_finalize (GObject *object)
65 {
66     SessionConfig *self = SESSION_CONFIG (object);
67
68     g_free (self->priv->command);
69
70     G_OBJECT_CLASS (session_config_parent_class)->finalize (object);
71 }
72
73 static void
74 session_config_class_init (SessionConfigClass *klass)
75 {
76     GObjectClass *object_class = G_OBJECT_CLASS (klass);
77
78     object_class->finalize = session_config_finalize;
79
80     g_type_class_add_private (klass, sizeof (SessionConfigPrivate));
81 }