/*
* 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 .
*/
#ifdef HAVE_CONFIG_H
#include
#endif
#include "profiles.h"
#include
#include
struct _MqProfiles {
gchar *file_name;
GKeyFile *key_file;
gchar *current;
};
static gboolean
create(MqProfiles *profiles)
{
GFile *file;
GFileOutputStream *stream;
file = g_file_new_for_path(profiles->file_name);
stream = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, NULL);
if (!stream) {
return FALSE;
}
/* TODO: Create configs. */
return FALSE;
}
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);
}
MqProfiles *
mq_profiles_new(void)
{
MqProfiles *profiles;
gchar *config_dir;
profiles = g_new0(MqProfiles, 1);
config_dir = g_build_filename(g_get_user_config_dir(), PACKAGE, NULL);
g_mkdir_with_parents(config_dir, 0700);
g_free(config_dir);
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);
}
/* TODO: Set up GFileMonitor. */
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 (g_key_file_get_boolean(profiles->key_file, ids[i],
"default", NULL)) {
def = g_strdup(ids[i]);
break;
}
}
g_strfreev(ids);
return def;
}
gchar *
mq_profiles_get_current(MqProfiles *profiles)
{
return g_strdup(profiles->current);
}
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 (profile && profile[0]) {
profiles->current = g_strdup(profile);
} else {
profiles->current = mq_profiles_get_default(profiles);
}
}
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" */
'_');
mq_profiles_set_name(profiles, id, name);
mq_profiles_set_color(profiles, id, color);
return id;
}
gboolean
mq_profiles_remove(MqProfiles *profiles, const gchar *profile)
{
return 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);
}