~ted/ubuntu-app-launch/snap-icon-unbreak

« back to all changes in this revision

Viewing changes to helpers-keyfile.c

Updated to failure-is-an-option

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 *     Ted Gould <ted.gould@canonical.com>
 
18
 */
 
19
 
 
20
#include "helpers.h"
 
21
 
 
22
/* Check to make sure we have the sections and keys we want */
 
23
static gboolean
 
24
verify_keyfile (GKeyFile * inkeyfile, const gchar * desktop)
 
25
{
 
26
        if (inkeyfile == NULL) return FALSE;
 
27
 
 
28
        if (!g_key_file_has_group(inkeyfile, "Desktop Entry")) {
 
29
                g_warning("Desktop file '%s' is missing the 'Desktop Entry' group", desktop);
 
30
                return FALSE;
 
31
        }
 
32
 
 
33
        if (!g_key_file_has_key(inkeyfile, "Desktop Entry", "Exec", NULL)) {
 
34
                g_warning("Desktop file '%s' is missing the 'Exec' key", desktop);
 
35
                return FALSE;
 
36
        }
 
37
 
 
38
        return TRUE;
 
39
}
 
40
 
 
41
/* Try to find a desktop file in a particular data directory */
 
42
static GKeyFile *
 
43
try_dir (const char * dir, const gchar * desktop)
 
44
{
 
45
        gchar * fullpath = g_build_filename(dir, "applications", desktop, NULL);
 
46
        GKeyFile * keyfile = g_key_file_new();
 
47
 
 
48
        /* NOTE: Leaving off the error here as we'll get a bunch of them,
 
49
           so individuals aren't really useful */
 
50
        gboolean loaded = g_key_file_load_from_file(keyfile, fullpath, G_KEY_FILE_NONE, NULL);
 
51
 
 
52
        g_free(fullpath);
 
53
 
 
54
        if (!loaded) {
 
55
                g_key_file_free(keyfile);
 
56
                return NULL;
 
57
        }
 
58
 
 
59
        if (!verify_keyfile(keyfile, desktop)) {
 
60
                g_key_file_free(keyfile);
 
61
                return NULL;
 
62
        }
 
63
 
 
64
        return keyfile;
 
65
}
 
66
 
 
67
/* Find the keyfile that we need for a particular AppID and return it.
 
68
   Or NULL if we can't find it. */
 
69
GKeyFile *
 
70
keyfile_for_appid (const gchar * appid, gchar ** desktopfile)
 
71
{
 
72
        gchar * desktop = g_strdup_printf("%s.desktop", appid);
 
73
 
 
74
        const char * const * data_dirs = g_get_system_data_dirs();
 
75
        GKeyFile * keyfile = NULL;
 
76
        int i;
 
77
 
 
78
        keyfile = try_dir(g_get_user_data_dir(), desktop);
 
79
        if (keyfile != NULL && desktopfile != NULL && *desktopfile == NULL) {
 
80
                *desktopfile = g_build_filename(g_get_user_data_dir(), "applications", desktop, NULL);
 
81
        }
 
82
 
 
83
        for (i = 0; data_dirs[i] != NULL && keyfile == NULL; i++) {
 
84
                keyfile = try_dir(data_dirs[i], desktop);
 
85
 
 
86
                if (keyfile != NULL && desktopfile != NULL && *desktopfile == NULL) {
 
87
                        *desktopfile = g_build_filename(data_dirs[i], "applications", desktop, NULL);
 
88
                }
 
89
        }
 
90
 
 
91
        g_free(desktop);
 
92
 
 
93
        return keyfile;
 
94
}
 
95