1 /*
2   Copyright (C) 2005-2018 Marius L. Jøhndal
3   Copyright (C) 2010 Tony Armitstead
4 
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9 
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14 
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24 #include <string.h>
25 #include <stdlib.h>
26 #include <glib/gstdio.h>
27 
28 #include "configuration.h"
29 
_read_channel_configuration_key(GKeyFile * kf,const gchar * identifier,const gchar * key)30 static gchar *_read_channel_configuration_key(GKeyFile *kf, const gchar *identifier,
31                                               const gchar *key)
32 {
33   GError *error = NULL;
34 
35   if (g_key_file_has_key(kf, identifier, key, &error))
36     return g_strdup(g_key_file_get_value(kf, identifier, key, &error));
37 
38   return NULL;
39 }
40 
channel_configuration_free(struct channel_configuration * c)41 void channel_configuration_free(struct channel_configuration *c)
42 {
43   g_free(c->identifier);
44 
45   if (c->url)
46     g_free(c->url);
47 
48   if (c->spool_directory)
49     g_free(c->spool_directory);
50 
51   if (c->filename_pattern)
52     g_free(c->filename_pattern);
53 
54   if (c->playlist)
55     g_free(c->playlist);
56 
57   if (c->id3_lead_artist)
58     g_free(c->id3_lead_artist);
59 
60   if (c->id3_content_group)
61     g_free(c->id3_content_group);
62 
63   if (c->id3_title)
64     g_free(c->id3_title);
65 
66   if (c->id3_album)
67     g_free(c->id3_album);
68 
69   if (c->id3_content_type)
70     g_free(c->id3_content_type);
71 
72   if (c->id3_year)
73     g_free(c->id3_year);
74 
75   if (c->id3_comment)
76     g_free(c->id3_comment);
77 
78   if (c->regex_filter)
79     g_free(c->regex_filter);
80 
81   g_free(c);
82 }
83 
channel_configuration_new(GKeyFile * kf,const gchar * identifier,struct channel_configuration * defaults)84 struct channel_configuration *channel_configuration_new(GKeyFile *kf, const gchar *identifier,
85                                                         struct channel_configuration *defaults)
86 {
87   struct channel_configuration *c;
88 
89   g_assert(g_key_file_has_group(kf, identifier));
90 
91   /* Allocate new structure and save identifierr. */
92   c = (struct channel_configuration *)g_malloc(sizeof(struct channel_configuration));
93 
94   c->identifier = g_strdup(identifier);
95 
96   /* Read keys from configuration file. */
97   c->url = _read_channel_configuration_key(kf, identifier, "url");
98   c->spool_directory = _read_channel_configuration_key(kf, identifier, "spool");
99   c->filename_pattern = _read_channel_configuration_key(kf, identifier, "filename");
100   c->playlist = _read_channel_configuration_key(kf, identifier, "playlist");
101   c->id3_lead_artist = _read_channel_configuration_key(kf, identifier, "id3leadartist");
102   c->id3_content_group = _read_channel_configuration_key(kf, identifier, "id3contentgroup");
103   c->id3_title = _read_channel_configuration_key(kf, identifier, "id3title");
104   c->id3_album = _read_channel_configuration_key(kf, identifier, "id3album");
105   c->id3_content_type = _read_channel_configuration_key(kf, identifier, "id3contenttype");
106   c->id3_year = _read_channel_configuration_key(kf, identifier, "id3year");
107   c->id3_comment = _read_channel_configuration_key(kf, identifier, "id3comment");
108   c->regex_filter = _read_channel_configuration_key(kf, identifier, "filter");
109 
110   /* Populate with defaults if necessary. */
111   if (defaults) {
112     if (!c->url && defaults->url)
113       c->url = g_strdup(defaults->url);
114 
115     if (!c->spool_directory && defaults->spool_directory)
116       c->spool_directory = g_strdup(defaults->spool_directory);
117 
118     if (!c->filename_pattern && defaults->filename_pattern)
119       c->filename_pattern = g_strdup(defaults->filename_pattern);
120 
121     if (!c->playlist && defaults->playlist)
122       c->playlist = g_strdup(defaults->playlist);
123 
124     if (!c->id3_lead_artist && defaults->id3_lead_artist)
125       c->id3_lead_artist = g_strdup(defaults->id3_lead_artist);
126 
127     if (!c->id3_content_group && defaults->id3_content_group)
128       c->id3_content_group = g_strdup(defaults->id3_content_group);
129 
130     if (!c->id3_title && defaults->id3_title)
131       c->id3_title = g_strdup(defaults->id3_title);
132 
133     if (!c->id3_album && defaults->id3_album)
134       c->id3_album = g_strdup(defaults->id3_album);
135 
136     if (!c->id3_content_type && defaults->id3_content_type)
137       c->id3_content_type = g_strdup(defaults->id3_content_type);
138 
139     if (!c->id3_year && defaults->id3_year)
140       c->id3_year = g_strdup(defaults->id3_year);
141 
142     if (!c->id3_comment && defaults->id3_comment)
143       c->id3_comment = g_strdup(defaults->id3_comment);
144 
145     if (!c->regex_filter && defaults->regex_filter)
146       c->regex_filter = g_strdup(defaults->regex_filter);
147   }
148 
149   return c;
150 }
151 
channel_configuration_verify_keys(GKeyFile * kf,const char * identifier)152 int channel_configuration_verify_keys(GKeyFile *kf, const char *identifier)
153 {
154   int i;
155   gchar **key_list;
156 
157   key_list = g_key_file_get_keys(kf, identifier, NULL, NULL);
158 
159   if (!key_list) {
160     fprintf(stderr, "Error reading keys in configuration of channel %s.\n", identifier);
161 
162     return -1;
163   }
164 
165   for (i = 0; key_list[i]; i++) {
166     if (! (!strcmp(key_list[i], "url") ||
167            !strcmp(key_list[i], "spool") ||
168            !strcmp(key_list[i], "filename") ||
169            !strcmp(key_list[i], "playlist") ||
170            !strcmp(key_list[i], "id3leadartist") ||
171            !strcmp(key_list[i], "id3contentgroup") ||
172            !strcmp(key_list[i], "id3title") ||
173            !strcmp(key_list[i], "id3album") ||
174            !strcmp(key_list[i], "id3contenttype") ||
175            !strcmp(key_list[i], "id3year") ||
176            !strcmp(key_list[i], "id3comment") ||
177            !strcmp(key_list[i], "filter"))) {
178       fprintf(stderr, "Invalid key %s in configuration of channel %s.\n", key_list[i], identifier);
179       return -1;
180     }
181   }
182 
183   g_strfreev(key_list);
184 
185   return 0;
186 }
187