1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
136
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* Configuration/browsing profiles
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee 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.
*
* Marquee 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 Marquee. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "profiles.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "../i18n.h"
#include "config.h"
struct _MqProfiles {
gchar *config_dir;
gchar *file_name;
GKeyFile *key_file;
gchar *current;
MqConfig *current_config;
};
static gboolean
create(MqProfiles *profiles)
{
GFile *file;
GFileOutputStream *stream;
gchar *default_profile;
gchar *private_profile;
MqConfig *config;
file = g_file_new_for_path(profiles->file_name);
stream = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, NULL);
g_object_unref(file);
if (!stream) {
return FALSE;
}
g_object_unref(stream);
default_profile = mq_profiles_insert(profiles, _("Default"), "#0000ff");
private_profile = mq_profiles_insert(profiles, _("Private"), "#6060a0");
if (default_profile) {
config = mq_config_new(default_profile,
MQ_CONFIG_PROFILE_DEFAULT);
mq_config_save(config);
mq_config_free(config);
mq_profiles_set_default(profiles, default_profile);
g_free(default_profile);
}
if (private_profile) {
config = mq_config_new(private_profile,
MQ_CONFIG_PROFILE_PRIVATE);
mq_config_save(config);
mq_config_free(config);
g_free(private_profile);
}
mq_profiles_save(profiles);
return TRUE;
}
static void
load(MqProfiles *profiles)
{
/* TODO: Handle parsing and ENOENT errors differently? */
g_key_file_load_from_file(profiles->key_file, profiles->file_name,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
}
static gboolean
insert(MqProfiles *profiles, const gchar *id, const gchar *name,
const gchar *color)
{
gchar *dir;
/* Make sure the profile directory can be created and doesn't already
* exist (or, on a case-insensitive file system, a directory by the same
* name but in a different case doesn't exist). */
dir = g_build_filename(profiles->config_dir, id, NULL);
if (g_mkdir(dir, 0700) != 0) {
g_free(dir);
return FALSE;
}
g_free(dir);
mq_profiles_set_name(profiles, id, name);
mq_profiles_set_color(profiles, id, color);
return TRUE;
}
static void
changed(GFileMonitor G_GNUC_UNUSED *monitor, GFile G_GNUC_UNUSED *file,
GFile G_GNUC_UNUSED *other_file,
GFileMonitorEvent G_GNUC_UNUSED event_type, MqProfiles *profiles)
{
gchar *cur_name;
gchar *cur_color;
if (event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
return;
}
/* Save the current profile. */
if (profiles->current) {
cur_name = mq_profiles_get_name( profiles, profiles->current);
cur_color = mq_profiles_get_color(profiles, profiles->current);
}
/* Get changes. */
load(profiles);
/* If another process removed the current profile, restore it. All
* other processes will reread the file and see the profile re-inserted.
* Multiple processes of the same profile may re-insert their profile,
* but they should all write the same contents to the file. But this
* can cause a thundering hurd as all running processes read and/or
* write the file in response to each other's changes. */
if (profiles->current) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" /* False positive */
if (!g_key_file_has_group(profiles->key_file,
profiles->current)) {
if (insert(profiles, profiles->current,
cur_name, cur_color))
{
mq_config_save(profiles->current_config);
mq_profiles_save(profiles);
}
}
g_free(cur_name);
g_free(cur_color);
#pragma GCC diagnostic pop
}
}
static void
monitor(MqProfiles *profiles)
{
GFile *file;
GFileMonitor *monitor;
file = g_file_new_for_path(profiles->file_name);
monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, NULL);
g_signal_connect(monitor, "changed", G_CALLBACK(changed), profiles);
}
MqProfiles *
mq_profiles_new(void)
{
MqProfiles *profiles;
profiles = g_new0(MqProfiles, 1);
profiles->config_dir = g_build_filename(g_get_user_config_dir(),
PACKAGE, NULL);
g_mkdir_with_parents(profiles->config_dir, 0700);
profiles->file_name = g_build_filename(g_get_user_config_dir(), PACKAGE,
"profiles", NULL);
profiles->key_file = g_key_file_new();
if (!create(profiles)) {
load(profiles);
}
monitor(profiles);
return profiles;
}
gchar **
mq_profiles_get_profiles(MqProfiles *profiles, gsize *length)
{
return g_key_file_get_groups(profiles->key_file, length);
}
gchar *
mq_profiles_get_name(MqProfiles *profiles, const gchar *profile)
{
return g_key_file_get_string(profiles->key_file, profile, "name", NULL);
}
gchar *
mq_profiles_get_color(MqProfiles *profiles, const gchar *profile)
{
return g_key_file_get_string(profiles->key_file, profile, "color",
NULL);
}
gboolean
mq_profiles_is_default(MqProfiles *profiles, const gchar *profile)
{
return g_key_file_get_boolean(profiles->key_file, profile, "default",
NULL);
}
gchar *
mq_profiles_get_default(MqProfiles *profiles)
{
gchar *def;
gchar **ids;
gsize length;
gsize i;
def = NULL;
ids = g_key_file_get_groups(profiles->key_file, &length);
for (i = 0; i < length; ++i) {
if (i == 0) {
/* In case no profile is marked as default, use the
* first one. */
def = g_strdup(ids[i]);
}
if (g_key_file_get_boolean(profiles->key_file, ids[i],
"default", NULL)) {
g_free(def);
def = g_strdup(ids[i]);
break;
}
}
g_strfreev(ids);
return def;
}
gchar *
mq_profiles_get_current(MqProfiles *profiles)
{
return g_strdup(profiles->current);
}
MqConfig *
mq_profiles_get_current_config(MqProfiles *profiles)
{
return profiles->current_config;
}
void
mq_profiles_set_name(MqProfiles *profiles, const gchar *profile,
const gchar *name)
{
g_key_file_set_string(profiles->key_file, profile, "name", name);
}
void
mq_profiles_set_color(MqProfiles *profiles, const gchar *profile,
const gchar *color)
{
g_key_file_set_string(profiles->key_file, profile, "color", color);
}
void
mq_profiles_set_default(MqProfiles *profiles, const gchar *profile)
{
gchar **ids;
gsize length;
gsize i;
ids = g_key_file_get_groups(profiles->key_file, &length);
for (i = 0; i < length; ++i) {
g_key_file_set_boolean(profiles->key_file, ids[i], "default",
FALSE);
}
g_strfreev(ids);
g_key_file_set_boolean(profiles->key_file, profile, "default", TRUE);
}
void
mq_profiles_set_current(MqProfiles *profiles, const gchar *profile)
{
g_free(profiles->current);
if (profiles->current_config) {
mq_config_free(profiles->current_config);
}
if (profile && profile[0]) {
profiles->current = g_strdup(profile);
} else {
profiles->current = mq_profiles_get_default(profiles);
}
profiles->current_config = mq_config_new(profiles->current,
MQ_CONFIG_PROFILE_DEFAULT);
mq_config_load(profiles->current_config);
}
gchar *
mq_profiles_insert(MqProfiles *profiles, const gchar *name, const gchar *color)
{
gchar *id;
/* The ID must be safe for both the file system and HTML5 "name" and
* "id" attributes. To make profile directories portable to other file
* systems, use the lowest common denominator of allowed characters. */
id = g_strdelimit(g_strdup(name),
"/\\:*\"?<>|" /* Lowest common denominator of modern FSes */
" \t\n\f\r", /* HTML5 "space characters" */
'_');
if (insert(profiles, id, name, color)) {
return id;
} else {
g_free(id);
return NULL;
}
}
static gboolean
unlink_recursive(const gchar *dir_path)
{
GDir *dir;
const gchar *entry_name;
gchar *entry_path;
dir = g_dir_open(dir_path, 0, NULL);
if (!dir) {
return FALSE;
}
while ((entry_name = g_dir_read_name(dir))) {
entry_path = g_build_filename(dir_path, entry_name, NULL);
if (g_file_test(entry_path, G_FILE_TEST_IS_DIR)) {
unlink_recursive(entry_path);
}
if (g_unlink(entry_path) != 0) {
return FALSE;
}
g_free(entry_path);
}
g_dir_close(dir);
if (g_rmdir(dir_path) != 0) {
return FALSE;
}
return TRUE;
}
gboolean
mq_profiles_remove(MqProfiles *profiles, const gchar *profile)
{
if (g_strcmp0(profile, profiles->current) == 0) {
return FALSE;
}
return unlink_recursive(
g_build_filename(profiles->config_dir, profile, NULL))
&& g_key_file_remove_group(profiles->key_file, profile, NULL);
}
gboolean
mq_profiles_save(MqProfiles *profiles)
{
/* TODO: Handle GFileError? */
return g_key_file_save_to_file(profiles->key_file, profiles->file_name,
NULL);
}
gboolean
mq_profile_launch(gchar *profile)
{
static gchar *program_path = NULL;
gchar *argv[4];
gboolean success;
GPid child_pid;
if (!program_path) {
#if defined(RUN_IN_PLACE) && RUN_IN_PLACE
program_path = g_build_filename(ABS_TOP_BUILDDIR, PROGRAM_NAME,
NULL);
#else
program_path = g_build_filename(BINDIR, PROGRAM_NAME, NULL);
#endif
}
argv[0] = g_strdup(program_path);
argv[1] = g_strdup("-P");
argv[2] = profile;
argv[3] = NULL;
success = g_spawn_async(NULL, argv, NULL, G_SPAWN_DEFAULT, NULL, NULL,
&child_pid, NULL);
g_spawn_close_pid(child_pid);
g_free(argv[0]);
g_free(argv[1]);
return success;
}
|