61
|
1 /*
|
|
2 * cdinfo.c Copyright 1999 Espen Skoglund <esk@ira.uka.de>
|
|
3 * Copyright 1999 Håvard Kvålen <havardk@sol.no>
|
|
4 *
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (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
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
19 */
|
|
20
|
|
21
|
|
22 #include "cdinfo.h"
|
|
23
|
|
24 #include <glib.h>
|
|
25 #include <glib/gi18n.h>
|
|
26 #include <glib/gprintf.h>
|
|
27
|
|
28 #include <libaudacious/rcfile.h>
|
|
29
|
|
30 #include "cdaudio.h"
|
|
31
|
|
32
|
|
33 /*
|
|
34 * Function cdda_cdinfo_flush (cdinfo)
|
|
35 *
|
|
36 * Free all information stored about the CD.
|
|
37 *
|
|
38 */
|
|
39 void
|
|
40 cdda_cdinfo_flush(cdinfo_t * cdinfo)
|
|
41 {
|
|
42 trackinfo_t *t;
|
|
43 gint i;
|
|
44
|
|
45 if (cdinfo->albname)
|
|
46 g_free(cdinfo->albname);
|
|
47 if (cdinfo->artname)
|
|
48 g_free(cdinfo->artname);
|
|
49
|
|
50 cdinfo->albname = cdinfo->artname = NULL;
|
|
51
|
|
52 for (t = cdinfo->tracks, i = 0; i < 100; i++, t++) {
|
|
53 if (t->artist)
|
|
54 g_free(t->artist);
|
|
55 if (t->title)
|
|
56 g_free(t->title);
|
|
57
|
|
58 t->artist = t->title = NULL;
|
|
59 t->num = -1;
|
|
60 }
|
|
61 cdinfo->is_valid = FALSE;
|
|
62 }
|
|
63
|
|
64
|
|
65 /*
|
|
66 * Function cdda_cdinfo_delete (cdinfo)
|
|
67 *
|
|
68 * Free the indicated `cdinfo' structure.
|
|
69 *
|
|
70 */
|
|
71 void
|
|
72 cdda_cdinfo_delete(cdinfo_t * cdinfo)
|
|
73 {
|
|
74 cdda_cdinfo_flush(cdinfo);
|
|
75 g_free(cdinfo);
|
|
76 }
|
|
77
|
|
78
|
|
79 /*
|
|
80 * Function cdda_cdinfo_new ()
|
|
81 *
|
|
82 * Allocate a new `cdinfo' structure and return it.
|
|
83 *
|
|
84 */
|
|
85 cdinfo_t *
|
|
86 cdda_cdinfo_new(void)
|
|
87 {
|
|
88 cdinfo_t *ret;
|
|
89 ret = g_malloc0(sizeof(cdinfo_t));
|
|
90 cdda_cdinfo_flush(ret);
|
|
91
|
|
92 return ret;
|
|
93 }
|
|
94
|
|
95
|
|
96 /*
|
|
97 * Function cdda_cdinfo_track_set (cdinfo, num, artist, title)
|
|
98 *
|
|
99 * Set `artist', and `title' for a track `num'. If the CD is a
|
|
100 * singleartist disc, the `artist' on each track should be set to
|
|
101 * NULL.
|
|
102 *
|
|
103 */
|
|
104 void
|
|
105 cdda_cdinfo_track_set(cdinfo_t * cdinfo, gint num, gchar * artist,
|
|
106 gchar * title)
|
|
107 {
|
|
108 trackinfo_t *track = cdinfo->tracks + num;
|
|
109
|
|
110 /* Check bounds */
|
|
111 if (num < 1 || num >= 100)
|
|
112 return;
|
|
113
|
|
114 track->artist = artist;
|
|
115 track->title = title;
|
|
116 track->num = num;
|
|
117 cdinfo->is_valid = TRUE;
|
|
118 }
|
|
119
|
|
120
|
|
121 /*
|
|
122 * Function cdda_cdinfo_cd_set (cdinfo, cdname, cdartist)
|
|
123 *
|
|
124 * Set name and artist for a cd. If CD is a multiartist disc, the
|
|
125 * `artist' should be set to NULL.
|
|
126 *
|
|
127 */
|
|
128 void
|
|
129 cdda_cdinfo_cd_set(cdinfo_t * cdinfo, gchar * cdname, gchar * cdartist)
|
|
130 {
|
|
131 cdinfo->albname = cdname;
|
|
132 cdinfo->artname = cdartist;
|
|
133 cdinfo->is_valid = TRUE;
|
|
134 }
|
|
135
|
|
136
|
|
137 /*
|
|
138 * Function cdda_cdinfo_get (cdinfo, num, artist, album, title)
|
|
139 *
|
|
140 * Get artist, album, and title of the indicated track (i.e. store
|
|
141 * them in the specified pointers). Return 0 upon success, or -1
|
|
142 * of track did not exist. The returned name must be subsequently
|
|
143 * freed using g_free().
|
|
144 *
|
|
145 */
|
|
146 gint
|
|
147 cdda_cdinfo_get(cdinfo_t * cdinfo, gint num, gchar ** artist,
|
|
148 gchar ** album, gchar ** title)
|
|
149 {
|
|
150 trackinfo_t *track = cdinfo->tracks + num;
|
|
151
|
|
152 /* Check validity */
|
|
153 if (!cdinfo->is_valid || num < 1 || num >= 100)
|
|
154 return -1;
|
|
155
|
|
156 *artist = track->artist ? track->artist :
|
|
157 cdinfo->artname ? cdinfo->artname : _("(unknown)");
|
|
158 *album = cdinfo->albname ? cdinfo->albname : _("(unknown)");
|
|
159 *title = track->title ? track->title : _("(unknown)");
|
|
160
|
|
161 return track->num == -1 ? -1 : 0;
|
|
162 }
|
|
163
|
|
164
|
|
165 /*
|
|
166 * Function cdda_cdinfo_write_file
|
|
167 *
|
|
168 * Writes the cdinfo_t structure to disk.
|
|
169 */
|
|
170
|
|
171
|
|
172 void
|
|
173 cdda_cdinfo_write_file(guint32 cddb_discid, cdinfo_t * cdinfo)
|
|
174 {
|
|
175 /*
|
|
176 * We currently identify cdinfo on disk with the CDDB-discid.
|
|
177 * Maybe it would be smarter to use the cdindex id instead?
|
|
178 */
|
|
179
|
|
180 gchar *filename;
|
|
181 RcFile *rcfile;
|
|
182 gchar sectionname[10], trackstr[16];
|
|
183 gint i, numtracks = cddb_discid & 0xff;
|
|
184
|
|
185 sprintf(sectionname, "%08x", cddb_discid);
|
|
186
|
|
187 filename =
|
|
188 g_strconcat(g_get_home_dir(), "/", BMP_RCPATH, "/cdinfo", NULL);
|
|
189 if ((rcfile = bmp_rcfile_open(filename)) == NULL)
|
|
190 rcfile = bmp_rcfile_new();
|
|
191
|
|
192 if (cdinfo->albname)
|
|
193 bmp_rcfile_write_string(rcfile, sectionname, "Albumname",
|
|
194 cdinfo->albname);
|
|
195 else
|
|
196 bmp_rcfile_write_string(rcfile, sectionname, "Albumname", "");
|
|
197 if (cdinfo->artname)
|
|
198 bmp_rcfile_write_string(rcfile, sectionname, "Artistname",
|
|
199 cdinfo->artname);
|
|
200 for (i = 1; i <= numtracks; i++) {
|
|
201 if (cdinfo->tracks[i].artist) {
|
|
202 sprintf(trackstr, "track_artist%d", i);
|
|
203 bmp_rcfile_write_string(rcfile, sectionname, trackstr,
|
|
204 cdinfo->tracks[i].artist);
|
|
205 }
|
|
206 if (cdinfo->tracks[i].title) {
|
|
207 sprintf(trackstr, "track_title%d", i);
|
|
208 bmp_rcfile_write_string(rcfile, sectionname, trackstr,
|
|
209 cdinfo->tracks[i].title);
|
|
210 }
|
|
211 }
|
|
212 if (!bmp_rcfile_write(rcfile, filename))
|
|
213 /*FIXME */ ;
|
|
214 bmp_rcfile_free(rcfile);
|
|
215 g_free(filename);
|
|
216 }
|
|
217
|
|
218 /*
|
|
219 * Function cdda_cdinfo_read_file
|
|
220 *
|
|
221 * Tries to find and read a album from the disk-cache.
|
|
222 *
|
|
223 * Returns true if the album is found.
|
|
224 */
|
|
225
|
|
226 gboolean
|
|
227 cdda_cdinfo_read_file(guint32 cddb_discid, cdinfo_t * cdinfo)
|
|
228 {
|
|
229 gchar *filename;
|
|
230 RcFile *rcfile;
|
|
231 gchar sectionname[10], trackstr[16];
|
|
232 gint i, numtracks = cddb_discid & 0xff;
|
|
233 gboolean track_found;
|
|
234
|
|
235 sprintf(sectionname, "%08x", cddb_discid);
|
|
236
|
|
237 // filename = g_strconcat(g_get_home_dir(), "/.audacious/cdinfo", NULL);
|
|
238
|
|
239 filename =
|
|
240 g_strconcat(g_get_home_dir(), "/", BMP_RCPATH, "/cdinfo", NULL);
|
|
241 if ((rcfile = bmp_rcfile_open(filename)) == NULL) {
|
|
242 g_free(filename);
|
|
243 return FALSE;
|
|
244 }
|
|
245 g_free(filename);
|
|
246
|
|
247 if (!bmp_rcfile_read_string
|
|
248 (rcfile, sectionname, "Albumname", &cdinfo->albname))
|
|
249 return FALSE;
|
|
250
|
|
251 bmp_rcfile_read_string(rcfile, sectionname, "Artistname",
|
|
252 &cdinfo->artname);
|
|
253
|
|
254 for (i = 1; i <= numtracks; i++) {
|
|
255 track_found = FALSE;
|
|
256 sprintf(trackstr, "track_artist%d", i);
|
|
257 if (bmp_rcfile_read_string
|
|
258 (rcfile, sectionname, trackstr, &cdinfo->tracks[i].artist))
|
|
259 track_found = TRUE;
|
|
260 sprintf(trackstr, "track_title%d", i);
|
|
261 if (bmp_rcfile_read_string
|
|
262 (rcfile, sectionname, trackstr, &cdinfo->tracks[i].title))
|
|
263 track_found = TRUE;
|
|
264 if (track_found)
|
|
265 cdinfo->tracks[i].num = i;
|
|
266 }
|
|
267 cdinfo->is_valid = TRUE;
|
|
268 bmp_rcfile_free(rcfile);
|
|
269 return TRUE;
|
|
270 }
|