gsm-session-fill.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006, 2010 Novell, Inc.
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include <config.h>

22 23
#include "gsm-session-fill.h"

24
#include "gsm-system.h"
25
#include "gsm-manager.h"
26
#include "gsm-process-helper.h"
27
#include "gsm-util.h"
28

29
#define GSM_KEYFILE_SESSION_GROUP "GNOME Session"
30 31
#define GSM_KEYFILE_RUNNABLE_KEY "IsRunnableHelper"
#define GSM_KEYFILE_FALLBACK_KEY "FallbackSession"
32
#define GSM_KEYFILE_REQUIRED_COMPONENTS_KEY "RequiredComponents"
33

34 35
/* See https://bugzilla.gnome.org/show_bug.cgi?id=641992 for discussion */
#define GSM_RUNNABLE_HELPER_TIMEOUT 3000 /* ms */
36

37 38 39 40
typedef void (*GsmFillHandleComponent) (const char *component,
                                        const char *app_path,
                                        gpointer    user_data);

41
static void
42 43 44
handle_required_components (GKeyFile               *keyfile,
                            GsmFillHandleComponent  callback,
                            gpointer                user_data)
45
{
46 47 48
        char **required_components;
        int    i;

49 50
        g_assert (keyfile != NULL);
        g_assert (callback != NULL);
51

52
        required_components = g_key_file_get_string_list (keyfile,
53 54
                                                          GSM_KEYFILE_SESSION_GROUP,
                                                          GSM_KEYFILE_REQUIRED_COMPONENTS_KEY,
55
                                                          NULL, NULL);
56

57
        if (!required_components)
58
                return;
59

60 61 62
        for (i = 0; required_components[i] != NULL; i++) {
                char *app_path;

63
                app_path = gsm_util_find_desktop_file_for_app_name (required_components[i],
64
                                                                    FALSE, TRUE);
65
                callback (required_components[i], app_path, user_data);
66
                g_free (app_path);
67 68
        }

69
        g_strfreev (required_components);
70 71
}

72 73 74 75 76 77
static void
check_required_components_helper (const char *component,
                                  const char *app_path,
                                  gpointer    user_data)
{
        gboolean *error = user_data;
78

79 80 81
        if (app_path == NULL) {
                g_warning ("Unable to find required component '%s'", component);
                *error = TRUE;
82
        }
83
}
84

85 86 87 88
static gboolean
check_required (GKeyFile *keyfile)
{
        gboolean error = FALSE;
89

90
        g_debug ("fill: *** Checking required components");
91

92
        handle_required_components (keyfile,
93
                                    check_required_components_helper, &error);
94

95
        g_debug ("fill: *** Done checking required components");
96

97
        return !error;
98 99
}

100 101 102
static void
maybe_load_saved_session_apps (GsmManager *manager)
{
103
        GsmSystem *system;
104 105
        gboolean is_login;

106 107 108
        system = gsm_get_system ();
        is_login = gsm_system_is_login_session (system);
        g_object_unref (system);
109 110

        if (is_login)
111
                return;
112

113
        gsm_manager_add_autostart_apps_from_dir (manager, gsm_util_get_saved_session_dir ());
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129
static void
append_required_components_helper (const char *component,
                                   const char *app_path,
                                   gpointer    user_data)
{
        GsmManager *manager = user_data;

        if (app_path == NULL)
                g_warning ("Unable to find required component '%s'", component);
        else
                gsm_manager_add_required_app (manager, app_path, NULL);
}


130 131
static void
load_standard_apps (GsmManager *manager,
132
                    GKeyFile   *keyfile)
133
{
134
        g_debug ("fill: *** Adding required components");
135
        handle_required_components (keyfile,
136 137
                                    append_required_components_helper, manager);
        g_debug ("fill: *** Done adding required components");
138 139

        if (!gsm_manager_get_failsafe (manager)) {
140 141 142 143 144
                char **autostart_dirs;
                int    i;

                autostart_dirs = gsm_util_get_autostart_dirs ();

145 146 147 148 149 150
                maybe_load_saved_session_apps (manager);

                for (i = 0; autostart_dirs[i]; i++) {
                        gsm_manager_add_autostart_apps_from_dir (manager,
                                                                 autostart_dirs[i]);
                }
151

152
                g_strfreev (autostart_dirs);
153 154 155
        }
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static GKeyFile *
get_session_keyfile_if_valid (const char *path)
{
        GKeyFile  *keyfile;
        gsize      len;
        char     **list;

        g_debug ("fill: *** Looking if %s is a valid session file", path);

        keyfile = g_key_file_new ();

        if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL)) {
                g_debug ("Cannot use session '%s': non-existing or invalid file.", path);
                goto error;
        }

        if (!g_key_file_has_group (keyfile, GSM_KEYFILE_SESSION_GROUP)) {
                g_warning ("Cannot use session '%s': no '%s' group.", path, GSM_KEYFILE_SESSION_GROUP);
                goto error;
        }

177 178 179 180 181 182 183 184
        list = g_key_file_get_string_list (keyfile,
                                           GSM_KEYFILE_SESSION_GROUP,
                                           GSM_KEYFILE_REQUIRED_COMPONENTS_KEY,
                                           &len, NULL);
        if (list)
                g_strfreev (list);
        if (len == 0)
                g_warning ("Session '%s': no component in the session.", path);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

        return keyfile;

error:
        g_key_file_free (keyfile);
        return NULL;
}

/**
 * find_valid_session_keyfile:
 * @session: name of session
 *
 * We look for the session file in XDG_CONFIG_HOME, XDG_CONFIG_DIRS and
 * XDG_DATA_DIRS. This enables users and sysadmins to override a specific
 * session that is shipped in XDG_DATA_DIRS.
 */
static GKeyFile *
find_valid_session_keyfile (const char *session)
{
        GPtrArray          *dirs;
        const char * const *system_config_dirs;
        const char * const *system_data_dirs;
        int                 i;
        GKeyFile           *keyfile;
        char               *basename;
        char               *path;

        dirs = g_ptr_array_new ();

        g_ptr_array_add (dirs, (gpointer) g_get_user_config_dir ());

        system_config_dirs = g_get_system_config_dirs ();
        for (i = 0; system_config_dirs[i]; i++)
                g_ptr_array_add (dirs, (gpointer) system_config_dirs[i]);

        system_data_dirs = g_get_system_data_dirs ();
        for (i = 0; system_data_dirs[i]; i++)
                g_ptr_array_add (dirs, (gpointer) system_data_dirs[i]);

        keyfile = NULL;
        basename = g_strdup_printf ("%s.session", session);
        path = NULL;

        for (i = 0; i < dirs->len; i++) {
                path = g_build_filename (dirs->pdata[i], "gnome-session", "sessions", basename, NULL);
                keyfile = get_session_keyfile_if_valid (path);
                if (keyfile != NULL)
                        break;
        }

        if (dirs)
                g_ptr_array_free (dirs, TRUE);
        if (basename)
                g_free (basename);
        if (path)
                g_free (path);

        return keyfile;
}

245
static GKeyFile *
246
get_session_keyfile (const char *session,
247
                     char      **actual_session,
248
                     gboolean   *is_fallback)
249 250 251 252
{
        GKeyFile *keyfile;
        gboolean  session_runnable;
        char     *value;
253
        GError *error = NULL;
254

255 256
        *actual_session = NULL;

257 258 259 260
        g_debug ("fill: *** Getting session '%s'", session);

        keyfile = find_valid_session_keyfile (session);

Vincent Untz's avatar
Vincent Untz committed
261
        if (!keyfile)
262 263
                return NULL;

264 265 266 267 268 269 270
        session_runnable = TRUE;

        value = g_key_file_get_string (keyfile,
                                       GSM_KEYFILE_SESSION_GROUP, GSM_KEYFILE_RUNNABLE_KEY,
                                       NULL);
        if (!IS_STRING_EMPTY (value)) {
                g_debug ("fill: *** Launching helper '%s' to know if session is runnable", value);
271 272
                session_runnable = gsm_process_helper (value, GSM_RUNNABLE_HELPER_TIMEOUT, &error);
                if (!session_runnable) {
273 274
                        g_warning ("Session '%s' runnable check failed: %s", session,
                                   error->message);
275 276
                        g_clear_error (&error);
                }
277 278 279
        }
        g_free (value);

280
        if (session_runnable) {
281
                session_runnable = check_required (keyfile);
282 283
        }

284
        if (session_runnable) {
285
                *actual_session = g_strdup (session);
286 287
                if (is_fallback)
                        *is_fallback = FALSE;
288
                return keyfile;
289
        }
290 291 292 293 294 295 296 297 298 299 300

        g_debug ("fill: *** Session is not runnable");

        /* We can't run this session, so try to use the fallback */
        value = g_key_file_get_string (keyfile,
                                       GSM_KEYFILE_SESSION_GROUP, GSM_KEYFILE_FALLBACK_KEY,
                                       NULL);

        g_key_file_free (keyfile);
        keyfile = NULL;

301 302 303
        if (!IS_STRING_EMPTY (value)) {
                if (is_fallback)
                        *is_fallback = TRUE;
304
                keyfile = get_session_keyfile (value, actual_session, NULL);
305
        }
306 307 308 309 310
        g_free (value);

        return keyfile;
}

311
gboolean
312
gsm_session_fill (GsmManager  *manager,
313
                  const char  *session)
314
{
315
        GKeyFile *keyfile;
316
        gboolean is_fallback;
317
        char *actual_session;
318

319
        keyfile = get_session_keyfile (session, &actual_session, &is_fallback);
320 321 322 323

        if (!keyfile)
                return FALSE;

324 325 326
        _gsm_manager_set_active_session (manager, actual_session, is_fallback);

        g_free (actual_session);
327

328 329 330 331 332
        load_standard_apps (manager, keyfile);

        g_key_file_free (keyfile);

        return TRUE;
333
}