files.cpp 8.09 KB
Newer Older
Stepan Salenikovich's avatar
Stepan Salenikovich committed
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
Stepan Salenikovich's avatar
Stepan Salenikovich committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
 *
 *  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 3 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

20 21 22 23 24 25 26 27 28 29 30 31
#include "files.h"

#include <memory>
#include "config.h"
#include <unistd.h> // to create symlinks
#include <errno.h>
#include <glib/gstdio.h> // for glib file utils
#include <string.h>

void
autostart_symlink(gboolean autostart)
{
32
    /* autostart is enabled by creating a symlink to jami-gnome.desktop in
33 34 35 36 37
     * $XDG_CONFIG_HOME/autostart (by default ~/.config/autostart)
     * and removing it to disable autostart
     */

    GError *error = NULL;
38
    gchar *autostart_path = g_strconcat(g_get_user_config_dir(), "/autostart/jami-gnome.desktop", NULL);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    if (autostart) {
        g_debug("enabling autostart");

        gchar *desktop_path = NULL;
        /* find where the .desktop file is by checking the following dirs in order
         *  - /usr/<data dir>
         *  - /usr/local/<data dir>
         *  - install data dir (check this last, as it might not be where the
         *    bin is actually isntalled on the system)
         *
         *  note: no point in checking the local bin dir, even if the .desktop is
         *  there, if the binary is not installed on the system, the .desktop will
         *  not find it
         */
        int num_paths = 3;
        gchar *desktop_paths[num_paths];
56 57
        desktop_paths[0] = g_strconcat("/usr", JAMI_DATA_DIR, "/jami-gnome.desktop", NULL);
        desktop_paths[1] = g_strconcat("/usr/local", JAMI_DATA_DIR, "/jami-gnome.desktop", NULL);
58
        desktop_paths[2] = g_strconcat(JAMI_CLIENT_INSTALL, JAMI_DATA_DIR, "/jami-gnome.desktop", NULL);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

        for (int i = 0; i < num_paths && !desktop_path; ++i) {
            g_debug("checking %s", desktop_paths[i]);
            if (g_file_test(desktop_paths[i], G_FILE_TEST_IS_REGULAR))
                desktop_path = desktop_paths[i];
        }

        if (!desktop_path) {
            /* could not find where the .desktop is... default to the install one */
            desktop_path = desktop_paths[1];
            g_warning("cannot locate '%s', will try to create a symlink to it anyways", desktop_path);
        }

        /* we want autostart_path to be a symlink to the current desktop_path
         * if it is not, delete it and create one */

        gboolean symlink_to_desktop = FALSE;
        if (g_file_test(autostart_path, G_FILE_TEST_IS_SYMLINK)) {
            /* is symlink, check if its to the right file */
            gchar *current_link = g_file_read_link(autostart_path, &error);
            if (error) {
                g_warning("could not read contents of symlink '%s': %s",autostart_path, error->message);
                g_clear_error(&error);
            }

            /* compare even if error occurs, as function handles nullptr */
            if (g_strcmp0(current_link, desktop_path) == 0) {
                g_debug("'%s' is already a symlink to '%s'", autostart_path, desktop_path);
                symlink_to_desktop = TRUE;
            } else {
                g_debug("'%s' exists but does not point to '%s', instead: '%s'", autostart_path, desktop_path, current_link);

                /* we need to delete it */
                if (g_remove(autostart_path) != 0) {
                    g_warning("could not remove '%s'", autostart_path);
                }
            }
            g_free(current_link);
        }

        if (!symlink_to_desktop) {
            g_debug("creating symlink");

            /* make sure the directory exists */
            gchar *autostart_dir = g_strconcat(g_get_user_config_dir(), "/autostart", NULL);
            if (g_mkdir_with_parents(autostart_dir, 0700) != 0)
                g_warning("'%s' dir doesn't exist and could not be created", autostart_dir);
            g_free(autostart_dir);

            if (symlink(desktop_path, autostart_path) != 0) {
                g_warning("could not create symlink: %s", strerror(errno));
            }
        }

        for (int i = 0; i < num_paths; ++i) {
            g_free(desktop_paths[i]);
        }
    } else {
        g_debug("disabling autostart");
        /* need to remove symlink or .desktop, if it exists
         * G_FILE_TEST_IS_REGULAR will test for both file and symlink, since it
         * follows symlinks */
        if (g_file_test(autostart_path, G_FILE_TEST_IS_REGULAR)) {
            g_debug("'%s' exists, removing", autostart_path);

            if (g_remove(autostart_path) != 0) {
                g_warning("could not remove '%s'", autostart_path);
            }
        } else {
            g_debug("'%s' doesn't exist, nothing to do", autostart_path);
        }

    }
    g_free(autostart_path);
}

GSettingsSchema *
136
get_settings_schema()
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
{
    static std::unique_ptr<GSettingsSchema, decltype(g_settings_schema_unref )&>
        ring_schema(nullptr, g_settings_schema_unref);

    if (ring_schema.get() == nullptr) {
        GSettingsSchema *schema = NULL;

        /* find gschema.compiled by checking the following dirs in order:
         *  - current bin dir
         *  - install data dir
         *  - default dir
         * note that the install and default dir may be the same
         */
        GError *error = NULL;

        /* try local first */
        g_debug("looking for schema locally");
        gchar *schema_dir_local = g_strconcat(".", NULL);
        GSettingsSchemaSource *schema_source_local = NULL;
        schema_source_local = g_settings_schema_source_new_from_directory(
            schema_dir_local,
            g_settings_schema_source_get_default(),
            FALSE,
            &error);

        if (!error) {
            schema = g_settings_schema_source_lookup(schema_source_local,
164
                                                     JAMI_CLIENT_APP_ID,
165 166 167 168 169 170 171 172 173 174 175
                                                     TRUE);
            g_settings_schema_source_unref(schema_source_local);
        } else {
            g_debug("error looking up ring schema locally: %s", error->message);
            g_clear_error(&error);
        }
        g_free(schema_dir_local);

        if (!schema) {
            /* try install dir */
            g_debug("looking for schema in insall dir");
176
            gchar *schema_dir_install = g_strconcat(JAMI_CLIENT_INSTALL, "/share/glib-2.0/schemas", NULL);
177 178 179 180 181 182 183 184 185
            GSettingsSchemaSource *schema_source_install = NULL;
            schema_source_install = g_settings_schema_source_new_from_directory(
                schema_dir_install,
                g_settings_schema_source_get_default(),
                TRUE,
                &error);

            if (!error) {
                schema = g_settings_schema_source_lookup(schema_source_install,
186
                                                         JAMI_CLIENT_APP_ID,
187 188 189 190 191 192 193 194 195 196 197 198 199 200
                                                         TRUE);
                g_settings_schema_source_unref(schema_source_install);
            } else {
                g_debug("error looking up ring schema in install dir: %s", error->message);
                g_clear_error(&error);
            }
            g_free(schema_dir_install);
        }

        if (!schema) {
            /* try default dir */
            g_debug("looking for schema in default dir");

            schema = g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
201
                                                     JAMI_CLIENT_APP_ID,
202 203 204 205 206 207 208
                                                     TRUE);
        }
        ring_schema.reset(schema);
    }

    return ring_schema.get();
}