1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright 2008 Red Hat, Inc, |
---|
4 | * 2007 William Jon McCann <mccann@jhu.edu> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | * |
---|
20 | * Written by : William Jon McCann <mccann@jhu.edu> |
---|
21 | * Ray Strode <rstrode@redhat.com> |
---|
22 | */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <stdlib.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <string.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <dirent.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | |
---|
34 | #include <glib.h> |
---|
35 | #include <glib/gi18n.h> |
---|
36 | #include <glib/gstdio.h> |
---|
37 | |
---|
38 | #include "gdm-sessions.h" |
---|
39 | |
---|
40 | typedef struct _GdmSessionFile { |
---|
41 | char *id; |
---|
42 | char *path; |
---|
43 | char *translated_name; |
---|
44 | char *translated_comment; |
---|
45 | } GdmSessionFile; |
---|
46 | |
---|
47 | static GHashTable *gdm_available_sessions_map; |
---|
48 | |
---|
49 | static gboolean gdm_sessions_map_is_initialized = FALSE; |
---|
50 | |
---|
51 | /* adapted from gnome-menus desktop-entries.c */ |
---|
52 | static gboolean |
---|
53 | key_file_is_relevant (GKeyFile *key_file) |
---|
54 | { |
---|
55 | GError *error; |
---|
56 | gboolean no_display; |
---|
57 | gboolean hidden; |
---|
58 | gboolean tryexec_failed; |
---|
59 | char *tryexec; |
---|
60 | |
---|
61 | error = NULL; |
---|
62 | no_display = g_key_file_get_boolean (key_file, |
---|
63 | G_KEY_FILE_DESKTOP_GROUP, |
---|
64 | "NoDisplay", |
---|
65 | &error); |
---|
66 | if (error) { |
---|
67 | no_display = FALSE; |
---|
68 | g_error_free (error); |
---|
69 | } |
---|
70 | |
---|
71 | error = NULL; |
---|
72 | hidden = g_key_file_get_boolean (key_file, |
---|
73 | G_KEY_FILE_DESKTOP_GROUP, |
---|
74 | "Hidden", |
---|
75 | &error); |
---|
76 | if (error) { |
---|
77 | hidden = FALSE; |
---|
78 | g_error_free (error); |
---|
79 | } |
---|
80 | |
---|
81 | tryexec_failed = FALSE; |
---|
82 | tryexec = g_key_file_get_string (key_file, |
---|
83 | G_KEY_FILE_DESKTOP_GROUP, |
---|
84 | "TryExec", |
---|
85 | NULL); |
---|
86 | if (tryexec) { |
---|
87 | char *path; |
---|
88 | |
---|
89 | path = g_find_program_in_path (g_strstrip (tryexec)); |
---|
90 | |
---|
91 | tryexec_failed = (path == NULL); |
---|
92 | |
---|
93 | g_free (path); |
---|
94 | g_free (tryexec); |
---|
95 | } |
---|
96 | |
---|
97 | if (no_display || hidden || tryexec_failed) { |
---|
98 | return FALSE; |
---|
99 | } |
---|
100 | |
---|
101 | return TRUE; |
---|
102 | } |
---|
103 | |
---|
104 | static void |
---|
105 | load_session_file (const char *id, |
---|
106 | const char *path) |
---|
107 | { |
---|
108 | GKeyFile *key_file; |
---|
109 | GError *error; |
---|
110 | gboolean res; |
---|
111 | GdmSessionFile *session; |
---|
112 | |
---|
113 | key_file = g_key_file_new (); |
---|
114 | |
---|
115 | error = NULL; |
---|
116 | res = g_key_file_load_from_file (key_file, path, 0, &error); |
---|
117 | |
---|
118 | if (!res) { |
---|
119 | g_debug ("Failed to load \"%s\": %s\n", path, error->message); |
---|
120 | g_error_free (error); |
---|
121 | goto out; |
---|
122 | } |
---|
123 | |
---|
124 | if (! g_key_file_has_group (key_file, G_KEY_FILE_DESKTOP_GROUP)) { |
---|
125 | goto out; |
---|
126 | } |
---|
127 | |
---|
128 | res = g_key_file_has_key (key_file, G_KEY_FILE_DESKTOP_GROUP, "Name", NULL); |
---|
129 | if (! res) { |
---|
130 | g_debug ("\"%s\" contains no \"Name\" key\n", path); |
---|
131 | goto out; |
---|
132 | } |
---|
133 | |
---|
134 | if (!key_file_is_relevant (key_file)) { |
---|
135 | g_debug ("\"%s\" is hidden or contains non-executable TryExec program\n", path); |
---|
136 | goto out; |
---|
137 | } |
---|
138 | |
---|
139 | session = g_new0 (GdmSessionFile, 1); |
---|
140 | |
---|
141 | session->id = g_strdup (id); |
---|
142 | session->path = g_strdup (path); |
---|
143 | |
---|
144 | session->translated_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "Name", NULL, NULL); |
---|
145 | session->translated_comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "Comment", NULL, NULL); |
---|
146 | |
---|
147 | g_hash_table_insert (gdm_available_sessions_map, |
---|
148 | g_strdup (id), |
---|
149 | session); |
---|
150 | out: |
---|
151 | g_key_file_free (key_file); |
---|
152 | } |
---|
153 | |
---|
154 | static void |
---|
155 | collect_sessions_from_directory (const char *dirname) |
---|
156 | { |
---|
157 | GDir *dir; |
---|
158 | const char *filename; |
---|
159 | |
---|
160 | /* FIXME: add file monitor to directory */ |
---|
161 | |
---|
162 | dir = g_dir_open (dirname, 0, NULL); |
---|
163 | if (dir == NULL) { |
---|
164 | return; |
---|
165 | } |
---|
166 | |
---|
167 | while ((filename = g_dir_read_name (dir))) { |
---|
168 | char *id; |
---|
169 | char *full_path; |
---|
170 | |
---|
171 | if (! g_str_has_suffix (filename, ".desktop")) { |
---|
172 | continue; |
---|
173 | } |
---|
174 | id = g_strndup (filename, strlen (filename) - strlen (".desktop")); |
---|
175 | |
---|
176 | full_path = g_build_filename (dirname, filename, NULL); |
---|
177 | |
---|
178 | load_session_file (id, full_path); |
---|
179 | |
---|
180 | g_free (id); |
---|
181 | g_free (full_path); |
---|
182 | } |
---|
183 | |
---|
184 | g_dir_close (dir); |
---|
185 | } |
---|
186 | |
---|
187 | static void |
---|
188 | collect_sessions (void) |
---|
189 | { |
---|
190 | int i; |
---|
191 | const char *search_dirs[] = { |
---|
192 | "/etc/X11/sessions/", |
---|
193 | DMCONFDIR "/Sessions/", |
---|
194 | DATADIR "/gdm/BuiltInSessions/", |
---|
195 | DATADIR "/xsessions/", |
---|
196 | NULL |
---|
197 | }; |
---|
198 | |
---|
199 | if (gdm_available_sessions_map == NULL) { |
---|
200 | gdm_available_sessions_map = g_hash_table_new_full (g_str_hash, g_str_equal, |
---|
201 | g_free, g_free); |
---|
202 | } |
---|
203 | |
---|
204 | for (i = 0; search_dirs [i] != NULL; i++) { |
---|
205 | collect_sessions_from_directory (search_dirs [i]); |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | char ** |
---|
210 | gdm_get_all_sessions (void) |
---|
211 | { |
---|
212 | GHashTableIter iter; |
---|
213 | gpointer key, value; |
---|
214 | GPtrArray *array; |
---|
215 | |
---|
216 | if (!gdm_sessions_map_is_initialized) { |
---|
217 | collect_sessions (); |
---|
218 | |
---|
219 | gdm_sessions_map_is_initialized = TRUE; |
---|
220 | } |
---|
221 | |
---|
222 | array = g_ptr_array_new (); |
---|
223 | g_hash_table_iter_init (&iter, gdm_available_sessions_map); |
---|
224 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
---|
225 | GdmSessionFile *session; |
---|
226 | |
---|
227 | session = (GdmSessionFile *) value; |
---|
228 | |
---|
229 | g_ptr_array_add (array, g_strdup (session->id)); |
---|
230 | } |
---|
231 | g_ptr_array_add (array, NULL); |
---|
232 | |
---|
233 | return (char **) g_ptr_array_free (array, FALSE); |
---|
234 | } |
---|
235 | |
---|
236 | gboolean |
---|
237 | gdm_get_details_for_session (const char *id, |
---|
238 | char **name, |
---|
239 | char **comment) |
---|
240 | { |
---|
241 | GdmSessionFile *session; |
---|
242 | |
---|
243 | if (!gdm_sessions_map_is_initialized) { |
---|
244 | collect_sessions (); |
---|
245 | |
---|
246 | gdm_sessions_map_is_initialized = TRUE; |
---|
247 | } |
---|
248 | |
---|
249 | session = (GdmSessionFile *) g_hash_table_lookup (gdm_available_sessions_map, |
---|
250 | id); |
---|
251 | |
---|
252 | if (session == NULL) { |
---|
253 | return FALSE; |
---|
254 | } |
---|
255 | |
---|
256 | if (name != NULL) { |
---|
257 | *name = g_strdup (session->translated_name); |
---|
258 | } |
---|
259 | |
---|
260 | if (comment != NULL) { |
---|
261 | *comment = g_strdup (session->translated_comment); |
---|
262 | } |
---|
263 | |
---|
264 | return TRUE; |
---|
265 | } |
---|