~ubuntu-branches/ubuntu/oneiric/xmms2/oneiric

« back to all changes in this revision

Viewing changes to .pc/remove-path_max.patch/src/clients/nycli/configuration.c

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2010-03-24 01:13:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100324011334-owt2x7txvq93wv4b
Tags: 0.7DrNo-4ubuntu1
* Merge from Debian unstable; remaining changes:
  - Use PulseAudio as default output plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  XMMS2 - X Music Multiplexer System
 
2
 *  Copyright (C) 2003-2007 XMMS2 Team
 
3
 *
 
4
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 */
 
16
 
 
17
#include "configuration.h"
 
18
 
 
19
const gchar *const default_config =
 
20
"[main]\n\n"
 
21
"PROMPT=xmms2> \n"
 
22
"SERVER_AUTOSTART=true\n"
 
23
"AUTO_UNIQUE_COMPLETE=true\n"
 
24
"SHELL_START_MESSAGE=true\n"
 
25
"PLAYLIST_MARKER=->\n"
 
26
"GUESS_PLS=false\n"
 
27
"CLASSIC_LIST=true\n"
 
28
"CLASSIC_LIST_FORMAT=${artist} - ${title}\n"
 
29
"HISTORY_FILE=\n"
 
30
"STATUS_FORMAT=${playback_status}: ${artist} - ${title}: ${playtime} of ${duration}\n\n"
 
31
"[alias]\n\n"
 
32
"ls = list\n"
 
33
"clear = playlist clear\n"
 
34
"quit = server shutdown\n"
 
35
"repeat = seek 0\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";
 
40
 
 
41
/* Load a section from a keyfile to a hash-table
 
42
   (replace existing keys in the hash) */
 
43
static void
 
44
section_to_hash (GKeyFile *file, const gchar *section, GHashTable *hash)
 
45
{
 
46
        GError *error;
 
47
        gchar **keys;
 
48
        gint i;
 
49
 
 
50
        error = NULL;
 
51
        keys = g_key_file_get_keys (file, section, NULL, &error);
 
52
        if (error) {
 
53
                g_printf ("Error: Couldn't load configuration section %s!\n", section);
 
54
        }
 
55
 
 
56
        for (i = 0; keys[i] != NULL; i++) {
 
57
                gchar *uncompressed_value;
 
58
 
 
59
                uncompressed_value = g_key_file_get_value (file, section, keys[i],
 
60
                                                           NULL);
 
61
                g_hash_table_insert (hash,
 
62
                                     g_strdup (keys[i]),
 
63
                                     g_strcompress (uncompressed_value));
 
64
                g_free (uncompressed_value);
 
65
        }
 
66
        g_strfreev (keys);
 
67
}
 
68
 
 
69
configuration_t *
 
70
configuration_init (const gchar *path)
 
71
{
 
72
        configuration_t *config;
 
73
        gchar *history_file;
 
74
 
 
75
        config = g_new0 (configuration_t, 1);
 
76
 
 
77
        if (path == NULL) {
 
78
                char *dir;
 
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);
 
82
                g_free (dir);
 
83
        } else {
 
84
                config->path = g_strdup (path);
 
85
        }
 
86
 
 
87
        /* init hash */
 
88
        config->values = g_hash_table_new_full (g_str_hash, g_str_equal,
 
89
                                                g_free, g_free);
 
90
 
 
91
        /* no aliases initially */
 
92
        config->aliases = g_hash_table_new_full (g_str_hash, g_str_equal,
 
93
                                                 g_free, g_free);
 
94
 
 
95
        if (!g_file_test (config->path, G_FILE_TEST_EXISTS)) {
 
96
                g_fprintf (stderr, "Creating %s...\n", config->path);
 
97
 
 
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");
 
101
                }
 
102
        }
 
103
 
 
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,
 
108
                                   NULL);
 
109
        section_to_hash (config->file, "main", config->values);
 
110
        g_key_file_free (config->file);
 
111
 
 
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");
 
117
                } else {
 
118
                        /* load keys to hash table overriding default values */
 
119
                        section_to_hash (config->file, "main", config->values);
 
120
 
 
121
                        /* load aliases */
 
122
                        section_to_hash (config->file, "alias", config->aliases);
 
123
                }
 
124
        }
 
125
 
 
126
        history_file = configuration_get_string (config, "HISTORY_FILE");
 
127
        if (!history_file || !*history_file) {
 
128
                gchar cfile[PATH_MAX];
 
129
 
 
130
                xmms_usercachedir_get (cfile, PATH_MAX);
 
131
                config->histpath = g_build_filename (cfile, HISTORY_FILE_BASE, NULL);
 
132
        } else {
 
133
                config->histpath = strdup (history_file);
 
134
        }
 
135
 
 
136
        return config;
 
137
}
 
138
 
 
139
void
 
140
configuration_free (configuration_t *config)
 
141
{
 
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);
 
147
        g_free (config);
 
148
}
 
149
 
 
150
GHashTable *
 
151
configuration_get_aliases (configuration_t *config)
 
152
{
 
153
        return config->aliases;
 
154
}
 
155
 
 
156
gboolean
 
157
configuration_get_boolean (configuration_t *config, const gchar *key)
 
158
{
 
159
        gchar *val;
 
160
 
 
161
        if (!(val = g_hash_table_lookup (config->values, key))) {
 
162
                return FALSE;
 
163
        }
 
164
 
 
165
        if (!strcmp (val, "true") || !strcmp (val, "1")) {
 
166
                return TRUE;
 
167
        }
 
168
 
 
169
        return FALSE;
 
170
}
 
171
 
 
172
gchar *
 
173
configuration_get_string (configuration_t *config, const gchar *key)
 
174
{
 
175
        return g_hash_table_lookup (config->values, key);
 
176
}