1
/* XMMS2 - X Music Multiplexer System
2
* Copyright (C) 2003-2007 XMMS2 Team
4
* PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
17
#include "configuration.h"
19
const gchar *const default_config =
22
"SERVER_AUTOSTART=true\n"
23
"AUTO_UNIQUE_COMPLETE=true\n"
24
"SHELL_START_MESSAGE=true\n"
25
"PLAYLIST_MARKER=->\n"
28
"CLASSIC_LIST_FORMAT=${artist} - ${title}\n"
30
"STATUS_FORMAT=${playback_status}: ${artist} - ${title}: ${playtime} of ${duration}\n\n"
33
"clear = playlist clear\n"
34
"quit = server shutdown\n"
36
"mute = server volume 0\n"
37
"scap = stop ; playlist clear ; add $@ ; play\n"
38
"current = status -f $1\n"
39
"addpls = add -f -P $@\n";
41
/* Load a section from a keyfile to a hash-table
42
(replace existing keys in the hash) */
44
section_to_hash (GKeyFile *file, const gchar *section, GHashTable *hash)
51
keys = g_key_file_get_keys (file, section, NULL, &error);
53
g_printf ("Error: Couldn't load configuration section %s!\n", section);
56
for (i = 0; keys[i] != NULL; i++) {
57
gchar *uncompressed_value;
59
uncompressed_value = g_key_file_get_value (file, section, keys[i],
61
g_hash_table_insert (hash,
63
g_strcompress (uncompressed_value));
64
g_free (uncompressed_value);
70
configuration_init (const gchar *path)
72
configuration_t *config;
75
config = g_new0 (configuration_t, 1);
79
dir = g_new0 (char, XMMS_PATH_MAX);
80
xmmsc_userconfdir_get (dir, XMMS_PATH_MAX);
81
config->path = g_strdup_printf ("%s/clients/nycli.conf", dir);
84
config->path = g_strdup (path);
88
config->values = g_hash_table_new_full (g_str_hash, g_str_equal,
91
/* no aliases initially */
92
config->aliases = g_hash_table_new_full (g_str_hash, g_str_equal,
95
if (!g_file_test (config->path, G_FILE_TEST_EXISTS)) {
96
g_fprintf (stderr, "Creating %s...\n", config->path);
98
if (!g_file_set_contents (config->path, default_config,
99
strlen (default_config), NULL)) {
100
g_fprintf (stderr, "Error: Can't create configuration file!\n");
104
/* load the defaults */
105
config->file = g_key_file_new ();
106
g_key_file_load_from_data (config->file, default_config,
107
strlen (default_config), G_KEY_FILE_NONE,
109
section_to_hash (config->file, "main", config->values);
110
g_key_file_free (config->file);
112
config->file = g_key_file_new ();
113
if (g_file_test (config->path, G_FILE_TEST_EXISTS)) {
114
if (!g_key_file_load_from_file (config->file, config->path,
115
G_KEY_FILE_NONE, NULL)) {
116
g_printf ("Error: Couldn't load configuration file!\n");
118
/* load keys to hash table overriding default values */
119
section_to_hash (config->file, "main", config->values);
122
section_to_hash (config->file, "alias", config->aliases);
126
history_file = configuration_get_string (config, "HISTORY_FILE");
127
if (!history_file || !*history_file) {
128
gchar cfile[PATH_MAX];
130
xmms_usercachedir_get (cfile, PATH_MAX);
131
config->histpath = g_build_filename (cfile, HISTORY_FILE_BASE, NULL);
133
config->histpath = strdup (history_file);
140
configuration_free (configuration_t *config)
142
g_free (config->path);
143
g_free (config->histpath);
144
g_key_file_free (config->file);
145
g_hash_table_destroy (config->values);
146
g_hash_table_destroy (config->aliases);
151
configuration_get_aliases (configuration_t *config)
153
return config->aliases;
157
configuration_get_boolean (configuration_t *config, const gchar *key)
161
if (!(val = g_hash_table_lookup (config->values, key))) {
165
if (!strcmp (val, "true") || !strcmp (val, "1")) {
173
configuration_get_string (configuration_t *config, const gchar *key)
175
return g_hash_table_lookup (config->values, key);