24
24
/* application settings */
26
#define _POSIX_C_SOURCE 1 /* PATH_MAX */
26
27
#define _BSD_SOURCE /* strdup() */
28
29
#include <string.h>
29
30
#include <stdlib.h>
32
34
#include "settings.h"
33
35
#include "config.h"
37
#define streq(a, b) (strcmp (a, b) == 0)
35
39
/* tries to guess your config dir; somehow conforming to
36
40
* http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
37
41
* @param name of the config file (can contain subdirs too)
85
91
* @return nothing yet
87
93
void BarSettingsRead (BarSettings_t *settings) {
88
/* FIXME: what is the max length of a path? */
89
char configfile[1024], key[256], val[256];
94
char configfile[PATH_MAX], key[256], val[256];
91
96
/* _must_ have same order as in BarKeyShortcutId_t */
92
97
static const char defaultKeys[] = {'?', '+', '-', 'a', 'c', 'd', 'e', 'g',
93
98
'h', 'i', 'j', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'x', '$',
96
101
static const char *shortcutFileKeys[] = {
97
102
"act_help", "act_songlove", "act_songban", "act_stationaddmusic",
100
105
"act_addshared", "act_songmove", "act_songnext", "act_songpause",
101
106
"act_quit", "act_stationrename", "act_stationchange",
102
107
"act_songtired", "act_upcoming", "act_stationselectquickmix",
103
"act_debug", "act_bookmark",
108
"act_debug", "act_bookmark", "act_voldown", "act_volup",
106
111
/* apply defaults */
114
119
settings->history = 5;
120
settings->volume = 0;
115
121
settings->sortOrder = BAR_SORT_NAME_AZ;
116
122
memcpy (settings->keys, defaultKeys, sizeof (defaultKeys));
123
settings->loveIcon = strdup ("<3");
124
settings->banIcon = strdup ("</3");
118
126
BarGetXdgConfigDir (PACKAGE "/config", configfile, sizeof (configfile));
119
127
if ((configfd = fopen (configfile, "r")) == NULL) {
129
137
/* invalid config line */
132
if (strcmp ("control_proxy", key) == 0) {
140
if (streq ("control_proxy", key)) {
133
141
settings->controlProxy = strdup (val);
134
} else if (strcmp ("proxy", key) == 0) {
142
} else if (streq ("proxy", key)) {
135
143
settings->proxy = strdup (val);
136
} else if (strcmp ("user", key) == 0) {
144
} else if (streq ("user", key)) {
137
145
settings->username = strdup (val);
138
} else if (strcmp ("password", key) == 0) {
146
} else if (streq ("password", key)) {
139
147
settings->password = strdup (val);
140
148
} else if (memcmp ("act_", key, 4) == 0) {
142
150
/* keyboard shortcuts */
143
151
for (i = 0; i < BAR_KS_COUNT; i++) {
144
if (strcmp (shortcutFileKeys[i], key) == 0) {
152
if (streq (shortcutFileKeys[i], key)) {
145
153
settings->keys[i] = val[0];
149
} else if (strcmp ("audio_format", key) == 0) {
150
if (strcmp (val, "aacplus") == 0) {
157
} else if (streq ("audio_format", key)) {
158
if (streq (val, "aacplus")) {
151
159
settings->audioFormat = PIANO_AF_AACPLUS;
152
} else if (strcmp (val, "mp3") == 0) {
160
} else if (streq (val, "mp3")) {
153
161
settings->audioFormat = PIANO_AF_MP3;
154
} else if (strcmp (val, "mp3-hifi") == 0) {
162
} else if (streq (val, "mp3-hifi")) {
155
163
settings->audioFormat = PIANO_AF_MP3_HI;
157
} else if (strcmp ("autostart_station", key) == 0) {
165
} else if (streq ("autostart_station", key)) {
158
166
settings->autostartStation = strdup (val);
159
} else if (strcmp ("event_command", key) == 0) {
167
} else if (streq ("event_command", key)) {
160
168
settings->eventCmd = strdup (val);
161
} else if (strcmp ("history", key) == 0) {
169
} else if (streq ("history", key)) {
162
170
settings->history = atoi (val);
163
} else if (strcmp ("sort", key) == 0) {
171
} else if (streq ("sort", key)) {
165
173
static const char *mapping[] = {"name_az",
170
178
"quickmix_10_name_za",
172
180
for (i = 0; i < BAR_SORT_COUNT; i++) {
173
if (strcmp (mapping[i], val) == 0) {
181
if (streq (mapping[i], val)) {
174
182
settings->sortOrder = i;
186
} else if (streq ("love_icon", key)) {
187
free (settings->loveIcon);
188
settings->loveIcon = strdup (val);
189
} else if (streq ("ban_icon", key)) {
190
free (settings->banIcon);
191
settings->banIcon = strdup (val);
192
} else if (streq ("volume", key)) {
193
settings->volume = atoi (val);