blob: 0b53f7691d9ea3b8b53ef0213a2e2815ce5be2df [file] [log] [blame]
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05001/*
2 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040020#include "files.h"
21
22#include <memory>
23#include "config.h"
24#include <unistd.h> // to create symlinks
25#include <errno.h>
26#include <glib/gstdio.h> // for glib file utils
27#include <string.h>
28
29void
30autostart_symlink(gboolean autostart)
31{
aviauc634cbe2016-06-06 17:31:01 -040032 /* autostart is enabled by creating a symlink to ring-gnome.desktop in
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040033 * $XDG_CONFIG_HOME/autostart (by default ~/.config/autostart)
34 * and removing it to disable autostart
35 */
36
37 GError *error = NULL;
aviauc634cbe2016-06-06 17:31:01 -040038 gchar *autostart_path = g_strconcat(g_get_user_config_dir(), "/autostart/ring-gnome.desktop", NULL);
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040039
40 if (autostart) {
41 g_debug("enabling autostart");
42
43 gchar *desktop_path = NULL;
44 /* find where the .desktop file is by checking the following dirs in order
45 * - /usr/<data dir>
46 * - /usr/local/<data dir>
47 * - install data dir (check this last, as it might not be where the
48 * bin is actually isntalled on the system)
49 *
50 * note: no point in checking the local bin dir, even if the .desktop is
51 * there, if the binary is not installed on the system, the .desktop will
52 * not find it
53 */
54 int num_paths = 3;
55 gchar *desktop_paths[num_paths];
aviauc634cbe2016-06-06 17:31:01 -040056 desktop_paths[0] = g_strconcat("/usr", RING_DATA_DIR, "/ring-gnome.desktop", NULL);
57 desktop_paths[1] = g_strconcat("/usr/local", RING_DATA_DIR, "/ring-gnome.desktop", NULL);
58 desktop_paths[2] = g_strconcat(RING_CLIENT_INSTALL, RING_DATA_DIR, "/ring-gnome.desktop", NULL);
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040059
60 for (int i = 0; i < num_paths && !desktop_path; ++i) {
61 g_debug("checking %s", desktop_paths[i]);
62 if (g_file_test(desktop_paths[i], G_FILE_TEST_IS_REGULAR))
63 desktop_path = desktop_paths[i];
64 }
65
66 if (!desktop_path) {
67 /* could not find where the .desktop is... default to the install one */
68 desktop_path = desktop_paths[1];
69 g_warning("cannot locate '%s', will try to create a symlink to it anyways", desktop_path);
70 }
71
72 /* we want autostart_path to be a symlink to the current desktop_path
73 * if it is not, delete it and create one */
74
75 gboolean symlink_to_desktop = FALSE;
76 if (g_file_test(autostart_path, G_FILE_TEST_IS_SYMLINK)) {
77 /* is symlink, check if its to the right file */
78 gchar *current_link = g_file_read_link(autostart_path, &error);
79 if (error) {
80 g_warning("could not read contents of symlink '%s': %s",autostart_path, error->message);
81 g_clear_error(&error);
82 }
83
84 /* compare even if error occurs, as function handles nullptr */
85 if (g_strcmp0(current_link, desktop_path) == 0) {
86 g_debug("'%s' is already a symlink to '%s'", autostart_path, desktop_path);
87 symlink_to_desktop = TRUE;
88 } else {
89 g_debug("'%s' exists but does not point to '%s', instead: '%s'", autostart_path, desktop_path, current_link);
90
91 /* we need to delete it */
92 if (g_remove(autostart_path) != 0) {
93 g_warning("could not remove '%s'", autostart_path);
94 }
95 }
96 g_free(current_link);
97 }
98
99 if (!symlink_to_desktop) {
100 g_debug("creating symlink");
101
102 /* make sure the directory exists */
103 gchar *autostart_dir = g_strconcat(g_get_user_config_dir(), "/autostart", NULL);
104 if (g_mkdir_with_parents(autostart_dir, 0700) != 0)
105 g_warning("'%s' dir doesn't exist and could not be created", autostart_dir);
106 g_free(autostart_dir);
107
108 if (symlink(desktop_path, autostart_path) != 0) {
109 g_warning("could not create symlink: %s", strerror(errno));
110 }
111 }
112
113 for (int i = 0; i < num_paths; ++i) {
114 g_free(desktop_paths[i]);
115 }
116 } else {
117 g_debug("disabling autostart");
118 /* need to remove symlink or .desktop, if it exists
119 * G_FILE_TEST_IS_REGULAR will test for both file and symlink, since it
120 * follows symlinks */
121 if (g_file_test(autostart_path, G_FILE_TEST_IS_REGULAR)) {
122 g_debug("'%s' exists, removing", autostart_path);
123
124 if (g_remove(autostart_path) != 0) {
125 g_warning("could not remove '%s'", autostart_path);
126 }
127 } else {
128 g_debug("'%s' doesn't exist, nothing to do", autostart_path);
129 }
130
131 }
132 g_free(autostart_path);
133}
134
135GSettingsSchema *
136get_ring_schema()
137{
138 static std::unique_ptr<GSettingsSchema, decltype(g_settings_schema_unref )&>
139 ring_schema(nullptr, g_settings_schema_unref);
140
141 if (ring_schema.get() == nullptr) {
142 GSettingsSchema *schema = NULL;
143
144 /* find gschema.compiled by checking the following dirs in order:
145 * - current bin dir
146 * - install data dir
147 * - default dir
148 * note that the install and default dir may be the same
149 */
150 GError *error = NULL;
151
152 /* try local first */
153 g_debug("looking for schema locally");
154 gchar *schema_dir_local = g_strconcat(".", NULL);
155 GSettingsSchemaSource *schema_source_local = NULL;
156 schema_source_local = g_settings_schema_source_new_from_directory(
157 schema_dir_local,
158 g_settings_schema_source_get_default(),
159 FALSE,
160 &error);
161
162 if (!error) {
163 schema = g_settings_schema_source_lookup(schema_source_local,
164 RING_CLIENT_APP_ID,
165 TRUE);
166 g_settings_schema_source_unref(schema_source_local);
167 } else {
168 g_debug("error looking up ring schema locally: %s", error->message);
169 g_clear_error(&error);
170 }
171 g_free(schema_dir_local);
172
173 if (!schema) {
174 /* try install dir */
175 g_debug("looking for schema in insall dir");
176 gchar *schema_dir_install = g_strconcat(RING_CLIENT_INSTALL, "/share/glib-2.0/schemas", NULL);
177 GSettingsSchemaSource *schema_source_install = NULL;
178 schema_source_install = g_settings_schema_source_new_from_directory(
179 schema_dir_install,
180 g_settings_schema_source_get_default(),
181 TRUE,
182 &error);
183
184 if (!error) {
185 schema = g_settings_schema_source_lookup(schema_source_install,
186 RING_CLIENT_APP_ID,
187 TRUE);
188 g_settings_schema_source_unref(schema_source_install);
189 } else {
190 g_debug("error looking up ring schema in install dir: %s", error->message);
191 g_clear_error(&error);
192 }
193 g_free(schema_dir_install);
194 }
195
196 if (!schema) {
197 /* try default dir */
198 g_debug("looking for schema in default dir");
199
200 schema = g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
201 RING_CLIENT_APP_ID,
202 TRUE);
203 }
204 ring_schema.reset(schema);
205 }
206
207 return ring_schema.get();
208}