61
|
1 /*
|
|
2 * cddb.c Copyright 1999-2001 Håvard Kvålen <havardk@xmms.org>
|
|
3 *
|
|
4 *
|
|
5 * This program is free software; you can redistribute it and/or modify
|
|
6 * it under the terms of the GNU General Public License as published by
|
|
7 * the Free Software Foundation; either version 2 of the License, or
|
|
8 * (at your option) any later version.
|
|
9 *
|
|
10 * This program 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
|
|
13 * GNU General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License
|
|
16 * along with this program; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 */
|
|
19
|
|
20
|
|
21 #include "cddb.h"
|
|
22
|
|
23 #include <glib.h>
|
|
24 #include <glib/gprintf.h>
|
|
25 #include <gtk/gtk.h>
|
|
26 #include <stdarg.h>
|
|
27
|
|
28 #include <libaudacious/util.h>
|
|
29
|
|
30 #include "http.h"
|
|
31 #include "cdaudio.h"
|
|
32 #include "cdinfo.h"
|
|
33
|
|
34
|
|
35 static guint32 cached_id = 0;
|
|
36 static GtkWidget *server_dialog, *server_clist;
|
|
37 static GtkWidget *debug_window, *debug_clist;
|
|
38 static GList *debug_messages = NULL;
|
|
39 static GList *temp_messages = NULL;
|
|
40 static guint cddb_timeout_id;
|
|
41
|
|
42 G_LOCK_DEFINE_STATIC(list);
|
|
43
|
|
44 void configure_set_cddb_server(gchar * server);
|
|
45
|
|
46 static void
|
|
47 cddb_log(gchar * str, ...)
|
|
48 {
|
|
49 static GList *end_ptr = NULL;
|
|
50 static gint message_num = 0;
|
|
51 va_list args;
|
|
52 gchar *text;
|
|
53
|
|
54 va_start(args, str);
|
|
55 text = g_strdup_vprintf(str, args);
|
|
56 va_end(args);
|
|
57
|
|
58 message_num++;
|
|
59 debug_messages = g_list_prepend(debug_messages, text);
|
|
60 if (!end_ptr)
|
|
61 end_ptr = debug_messages;
|
|
62 if (message_num > CDDB_LOG_MAX) {
|
|
63 GList *temp;
|
|
64
|
|
65 temp = g_list_previous(end_ptr);
|
|
66 temp->next = NULL;
|
|
67 g_free(end_ptr->data);
|
|
68 g_list_free_1(end_ptr);
|
|
69 end_ptr = temp;
|
|
70 message_num--;
|
|
71 }
|
|
72 if (debug_window) {
|
|
73 G_LOCK(list);
|
|
74 temp_messages = g_list_append(temp_messages, g_strdup(text));
|
|
75 G_UNLOCK(list);
|
|
76 }
|
|
77 }
|
|
78
|
|
79 static gint
|
|
80 cddb_sum(gint in)
|
|
81 {
|
|
82 gint retval = 0;
|
|
83
|
|
84 while (in > 0) {
|
|
85 retval += in % 10;
|
|
86 in /= 10;
|
|
87 }
|
|
88 return retval;
|
|
89 }
|
|
90
|
|
91 guint32
|
|
92 cdda_cddb_compute_discid(cdda_disc_toc_t * info)
|
|
93 {
|
|
94 gint i;
|
|
95 guint high = 0, low;
|
|
96
|
|
97 for (i = info->first_track; i <= info->last_track; i++)
|
|
98 high += cddb_sum(info->track[i].minute * 60 + info->track[i].second);
|
|
99
|
|
100 low = (info->leadout.minute * 60 + info->leadout.second) -
|
|
101 (info->track[info->first_track].minute * 60 +
|
|
102 info->track[info->first_track].second);
|
|
103
|
|
104 return ((high % 0xff) << 24 | low << 8 | (info->last_track -
|
|
105 info->first_track + 1));
|
|
106 }
|
|
107
|
|
108 static gchar *
|
|
109 cddb_generate_offset_string(cdda_disc_toc_t * info)
|
|
110 {
|
|
111 gchar *buffer;
|
|
112 int i;
|
|
113
|
|
114 buffer = g_malloc(info->last_track * 7 + 1);
|
|
115
|
|
116 sprintf(buffer, "%d", LBA(info->track[info->first_track]));
|
|
117
|
|
118 for (i = info->first_track + 1; i <= info->last_track; i++)
|
|
119 sprintf(buffer, "%s+%d", buffer, LBA(info->track[i]));
|
|
120
|
|
121 return buffer;
|
|
122 }
|
|
123
|
|
124 static gchar *
|
|
125 cddb_generate_hello_string(void)
|
|
126 {
|
|
127 static gchar *buffer;
|
|
128
|
|
129 if (buffer == NULL) {
|
|
130 gchar *env, *client = NULL, *version = NULL, **strs = NULL;
|
|
131
|
|
132 env = getenv("XMMS_CDDB_CLIENT_NAME");
|
|
133 if (env) {
|
|
134 strs = g_strsplit(env, " ", 2);
|
|
135 if (strs && strs[0] && strs[1]) {
|
|
136 client = strs[0];
|
|
137 version = strs[1];
|
|
138 }
|
|
139 }
|
|
140
|
|
141 if (!client || !version) {
|
|
142 client = PACKAGE;
|
|
143 version = VERSION;
|
|
144 }
|
|
145
|
|
146 buffer = g_strdup_printf("&hello=nobody+localhost+%s+%s",
|
|
147 client, version);
|
|
148 if (strs)
|
|
149 g_strfreev(strs);
|
|
150 }
|
|
151 return buffer;
|
|
152 }
|
|
153
|
|
154 static gint
|
|
155 cddb_http_open_connection(const gchar * server, gint port)
|
|
156 {
|
|
157 gint sock;
|
|
158 gchar *status;
|
|
159
|
|
160 if ((sock = http_open_connection(server, 80)) == 0)
|
|
161 status = "Failed";
|
|
162 else
|
|
163 status = "Ok";
|
|
164
|
|
165 cddb_log("Connecting to CDDB-server %s: %s", server, status);
|
|
166 return sock;
|
|
167 }
|
|
168
|
|
169
|
|
170 static gboolean
|
|
171 cddb_query(gchar * server, cdda_disc_toc_t * info,
|
|
172 cddb_disc_header_t * cddb_info)
|
|
173 {
|
|
174 /*
|
|
175 * Query the cddb-server for the cd.
|
|
176 * Returns the *real* diskid and category.
|
|
177 */
|
|
178
|
|
179 gint sock;
|
|
180 gchar *offsets, *getstr;
|
|
181 gchar buffer[256];
|
|
182 gchar **response;
|
|
183 gint i;
|
|
184
|
|
185 if ((sock = cddb_http_open_connection(server, 80)) == 0)
|
|
186 return FALSE;
|
|
187
|
|
188 offsets = cddb_generate_offset_string(info);
|
|
189
|
|
190 cddb_log("Sending query-command. Disc ID: %08x",
|
|
191 cdda_cddb_compute_discid(info));
|
|
192
|
|
193 getstr =
|
|
194 g_strdup_printf
|
|
195 ("GET /~cddb/cddb.cgi?cmd=cddb+query+%08x+%d+%s+%d%s&proto=%d HTTP/1.0\r\n\r\n",
|
|
196 cdda_cddb_compute_discid(info),
|
|
197 info->last_track - info->first_track + 1, offsets,
|
|
198 (info->leadout.minute * 60 + info->leadout.second),
|
|
199 cddb_generate_hello_string(), cdda_cfg.cddb_protocol_level);
|
|
200
|
|
201 g_free(offsets);
|
|
202 write(sock, getstr, strlen(getstr));
|
|
203 g_free(getstr);
|
|
204
|
|
205 if (http_read_first_line(sock, buffer, 256) < 0) {
|
|
206 http_close_connection(sock);
|
|
207 return FALSE;
|
|
208 }
|
|
209
|
|
210 http_close_connection(sock);
|
|
211 response = g_strsplit(buffer, " ", 4);
|
|
212
|
|
213 cddb_log("Query response: %s", buffer);
|
|
214
|
|
215 switch (strtol(response[0], NULL, 10)) {
|
|
216 case 200:
|
|
217 /* One exact match */
|
|
218 for (i = 0; i < 4; i++) {
|
|
219 if (response[i] == NULL) {
|
|
220 g_strfreev(response);
|
|
221 return FALSE;
|
|
222 }
|
|
223 }
|
|
224 cddb_info->category = g_strdup(response[1]);
|
|
225 cddb_info->discid = strtoul(response[2], NULL, 16);
|
|
226 break;
|
|
227 default: /* FIXME: Handle other 2xx */
|
|
228 g_strfreev(response);
|
|
229 return FALSE;
|
|
230 }
|
|
231
|
|
232 g_strfreev(response);
|
|
233 return TRUE;
|
|
234 }
|
|
235
|
|
236 static gint
|
|
237 cddb_check_protocol_level(const gchar * server)
|
|
238 {
|
|
239 gint level = 0, sock, n;
|
|
240 gchar *str, buffer[256];
|
|
241
|
|
242 if ((sock = cddb_http_open_connection(server, 80)) == 0)
|
|
243 return 0;
|
|
244
|
|
245 str =
|
|
246 g_strdup_printf
|
|
247 ("GET /~cddb/cddb.cgi?cmd=stat%s&proto=1 HTTP/1.0\r\n\r\n",
|
|
248 cddb_generate_hello_string());
|
|
249
|
|
250 write(sock, str, strlen(str));
|
|
251 g_free(str);
|
|
252
|
|
253 if ((n = http_read_first_line(sock, buffer, 256)) < 0 ||
|
|
254 atoi(buffer) != 210) {
|
|
255 if (n > 0)
|
|
256 cddb_log("Getting cddb protocol level failed: %s", buffer);
|
|
257 else
|
|
258 cddb_log("Getting cddb protocol level failed.");
|
|
259
|
|
260 http_close_connection(sock);
|
|
261 return 0;
|
|
262 }
|
|
263
|
|
264 while (http_read_line(sock, buffer, 256) >= 0) {
|
|
265 g_strstrip(buffer);
|
|
266 if (!strncmp(buffer, "max proto:", 10))
|
|
267 level = atoi(buffer + 10);
|
|
268 if (!strcmp(buffer, "."))
|
|
269 break;
|
|
270 }
|
|
271 http_close_connection(sock);
|
|
272 cddb_log("Getting cddb protocol level. Got level %d", level);
|
|
273 return (MIN(level, CDDB_MAX_PROTOCOL_LEVEL));
|
|
274 }
|
|
275
|
|
276 #define BUF2SIZE (80*3)
|
|
277
|
|
278 static gboolean
|
|
279 cddb_read(gchar * server, cddb_disc_header_t * cddb_info, cdinfo_t * cdinfo)
|
|
280 {
|
|
281 gint sock;
|
|
282 gchar *readstr;
|
|
283 gchar buffer[256], buffer2[BUF2SIZE];
|
|
284 gchar *realstr, *temp;
|
|
285 gint len, command, bufs;
|
|
286 gint num, oldnum;
|
|
287
|
|
288 if ((sock = cddb_http_open_connection(server, 80)) == 0)
|
|
289 return FALSE;
|
|
290
|
|
291 cddb_log("Sending read-command. Disc ID: %08x. Category: %s",
|
|
292 cddb_info->discid, cddb_info->category);
|
|
293
|
|
294 readstr =
|
|
295 g_strdup_printf
|
|
296 ("GET /~cddb/cddb.cgi?cmd=cddb+read+%s+%08x%s&proto=%d HTTP/1.0\r\n\r\n",
|
|
297 cddb_info->category, cddb_info->discid,
|
|
298 cddb_generate_hello_string(), cdda_cfg.cddb_protocol_level);
|
|
299
|
|
300 write(sock, readstr, strlen(readstr));
|
|
301 g_free(readstr);
|
|
302
|
|
303 if (http_read_first_line(sock, buffer, 256) < 0) {
|
|
304 http_close_connection(sock);
|
|
305 return FALSE;
|
|
306 }
|
|
307
|
|
308 cddb_log("Read response: %s", buffer);
|
|
309
|
|
310 command = 1;
|
|
311 bufs = 0;
|
|
312 oldnum = -1;
|
|
313 do {
|
|
314 /* fprintf(stderr,"%s\n",buffer); */
|
|
315 realstr = strchr(buffer, '=');
|
|
316 if (buffer[0] == '#' || !realstr)
|
|
317 continue;
|
|
318
|
|
319 realstr++;
|
|
320 len = strlen(realstr);
|
|
321
|
|
322 switch (command) {
|
|
323 case 1:
|
|
324 if (!strncmp(buffer, "DISCID", 6))
|
|
325 break;
|
|
326 command++;
|
|
327 case 2:
|
|
328 if (!strncmp(buffer, "DTITLE", 6)) {
|
|
329 strncpy(buffer2 + bufs, realstr, BUF2SIZE - bufs);
|
|
330 bufs += len;
|
|
331 break;
|
|
332 }
|
|
333 if (bufs > 0) {
|
|
334 buffer2[BUF2SIZE - 1] = '\0';
|
|
335 if ((temp = strstr(buffer2, " / ")) != NULL) {
|
|
336 cdda_cdinfo_cd_set(cdinfo, g_strdup(temp + 3),
|
|
337 g_strndup(buffer2, temp - buffer2));
|
|
338 }
|
|
339 else
|
|
340 cdda_cdinfo_cd_set(cdinfo, g_strdup(buffer2),
|
|
341 g_strdup(buffer2));
|
|
342 bufs = 0;
|
|
343 }
|
|
344 command++;
|
|
345 case 3:
|
|
346 if (!strncmp(buffer, "TTITLE", 6)) {
|
|
347 num = atoi(buffer + 6);
|
|
348 if (oldnum < 0 || num == oldnum) {
|
|
349 strncpy(buffer2 + bufs, realstr, BUF2SIZE - bufs);
|
|
350 bufs += len;
|
|
351 }
|
|
352 else {
|
|
353 buffer2[BUF2SIZE - 1] = '\0';
|
|
354 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL,
|
|
355 g_strdup(buffer2));
|
|
356 strncpy(buffer2, realstr, BUF2SIZE);
|
|
357 bufs = len;
|
|
358 }
|
|
359 oldnum = num;
|
|
360 break;
|
|
361 }
|
|
362 if (oldnum >= 0)
|
|
363 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL,
|
|
364 g_strdup(buffer2));
|
|
365 bufs = 0;
|
|
366 oldnum = -1;
|
|
367 command++;
|
|
368 case 4:
|
|
369 if (!strncmp(buffer, "EXTD", 4)) {
|
|
370 break;
|
|
371 }
|
|
372 command++;
|
|
373 case 5:
|
|
374 if (!strncmp(buffer, "EXTT", 4)) {
|
|
375 break;
|
|
376 }
|
|
377 command++;
|
|
378 case 6:
|
|
379 if (!strncmp(buffer, "PLAYORDER", 9)) {
|
|
380 break;
|
|
381 }
|
|
382 command++;
|
|
383 default:
|
|
384 g_log(NULL, G_LOG_LEVEL_WARNING, "%s: illegal cddb-data: %s",
|
|
385 PACKAGE, buffer);
|
|
386 break;
|
|
387 }
|
|
388
|
|
389 } while (http_read_line(sock, buffer, 256) >= 0);
|
|
390
|
|
391 if (oldnum >= 0)
|
|
392 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL, g_strdup(buffer2));
|
|
393
|
|
394 http_close_connection(sock);
|
|
395 return TRUE;
|
|
396 }
|
|
397
|
|
398 static gint
|
|
399 cddb_get_protocol_level(void)
|
|
400 {
|
|
401 if (cdda_cfg.cddb_protocol_level < 1)
|
|
402 cdda_cfg.cddb_protocol_level =
|
|
403 cddb_check_protocol_level(cdda_cfg.cddb_server);
|
|
404
|
|
405 return cdda_cfg.cddb_protocol_level;
|
|
406 }
|
|
407
|
|
408 static GList *
|
|
409 cddb_get_server_list(const gchar * server, gint protocol_level)
|
|
410 {
|
|
411 gint sock;
|
|
412 gchar *getstr;
|
|
413 gchar buffer[256];
|
|
414 gchar **message;
|
|
415 GList *list = NULL;
|
|
416
|
|
417 if ((sock = cddb_http_open_connection(server, 80)) == 0)
|
|
418 return NULL;
|
|
419
|
|
420 cddb_log("Sending sites-command");
|
|
421
|
|
422 getstr =
|
|
423 g_strdup_printf
|
|
424 ("GET /~cddb/cddb.cgi?cmd=sites%s&proto=%d HTTP/1.0\r\n\r\n",
|
|
425 cddb_generate_hello_string(), protocol_level);
|
|
426
|
|
427 write(sock, getstr, strlen(getstr));
|
|
428 g_free(getstr);
|
|
429
|
|
430 if (http_read_first_line(sock, buffer, 256) < 0) {
|
|
431 http_close_connection(sock);
|
|
432 return NULL;
|
|
433 }
|
|
434
|
|
435 cddb_log("Sites response: %s", buffer);
|
|
436
|
|
437 switch (atoi(buffer)) {
|
|
438 case 210:
|
|
439 while ((http_read_line(sock, buffer, 256)) > 1) {
|
|
440 message = g_strsplit(buffer, " ", 6);
|
|
441 if (message && message[0] && message[1] &&
|
|
442 !strcasecmp(message[1], "http")) {
|
|
443 list = g_list_prepend(list, message);
|
|
444 }
|
|
445 else {
|
|
446 /* Ignore non-http servers */
|
|
447 g_strfreev(message);
|
|
448 }
|
|
449 }
|
|
450 list = g_list_reverse(list);
|
|
451 break;
|
|
452 case 401:
|
|
453 /* No site information available */
|
|
454 break;
|
|
455 default:
|
|
456 break;
|
|
457 }
|
|
458 http_close_connection(sock);
|
|
459 return list;
|
|
460 }
|
|
461
|
|
462 gint
|
|
463 search_for_discid(gchar * abs_filename, gchar ** cddb_file, guint32 disc_id)
|
|
464 {
|
|
465 GDir *dir;
|
|
466 const gchar *dir_entry;
|
|
467 gchar tmp_id[10];
|
|
468
|
|
469 if (!(dir = g_dir_open(abs_filename, 0, NULL)))
|
|
470 return (0);
|
|
471
|
|
472 memset(tmp_id, 0, 10);
|
|
473
|
|
474 snprintf(tmp_id, sizeof(tmp_id), "%08x", disc_id);
|
|
475 while ((dir_entry = g_dir_read_name(dir))) {
|
|
476 if (!strncmp(tmp_id, dir_entry, 8)) {
|
|
477 cddb_file[0] = g_build_filename(abs_filename, dir_entry, NULL);
|
|
478 g_dir_close(dir);
|
|
479 return (1);
|
|
480 }
|
|
481 }
|
|
482 g_dir_close(dir);
|
|
483
|
|
484 return (0);
|
|
485 }
|
|
486
|
|
487 gint
|
|
488 scan_cddb_dir(gchar * server, gchar ** cddb_file, guint32 disc_id)
|
|
489 {
|
|
490
|
|
491 GDir *dir;
|
|
492 const gchar *dir_entry;
|
|
493 gchar abs_filename[FILENAME_MAX];
|
|
494
|
|
495 if (!(dir = g_dir_open(&server[7], 0, NULL))) {
|
|
496 return 0;
|
|
497 }
|
|
498
|
|
499 while ((dir_entry = g_dir_read_name(dir))) {
|
|
500 strcpy(abs_filename, &server[7]);
|
|
501 if (abs_filename[strlen(abs_filename) - 1] != '/') {
|
|
502 strcat(abs_filename, "/");
|
|
503 }
|
|
504 strcat(abs_filename, dir_entry);
|
|
505
|
|
506 if (dir_entry[0] != '.' &&
|
|
507 g_file_test(abs_filename, G_FILE_TEST_IS_DIR) &&
|
|
508 search_for_discid(abs_filename, cddb_file, disc_id)) {
|
|
509 break;
|
|
510 }
|
|
511 }
|
|
512
|
|
513 g_dir_close(dir);
|
|
514 return (cddb_file[0] != NULL);
|
|
515 }
|
|
516
|
|
517 gint
|
|
518 cddb_read_file(gchar * file, cddb_disc_header_t * cddb_info,
|
|
519 cdinfo_t * cdinfo)
|
|
520 {
|
|
521 FILE *fd;
|
|
522 gchar buffer[256], buffer2[BUF2SIZE];
|
|
523 gchar *realstr, *temp;
|
|
524 gint len, command, bufs;
|
|
525 gint num, oldnum;
|
|
526
|
|
527 if ((fd = fopen(file, "r")) == NULL)
|
|
528 return 0;
|
|
529
|
|
530 command = 1;
|
|
531 bufs = 0;
|
|
532 oldnum = -1;
|
|
533 while (fgets(buffer, 256, fd) != NULL) {
|
|
534 realstr = strchr(buffer, '=');
|
|
535 if (buffer[0] == '#' || !realstr)
|
|
536 continue;
|
|
537
|
|
538 realstr++;
|
|
539 len = strlen(realstr);
|
|
540 if (realstr[len - 1] == '\n')
|
|
541 realstr[--len] = '\0'; /* remove newline */
|
|
542
|
|
543 switch (command) {
|
|
544 case 1:
|
|
545 if (!strncmp(buffer, "DISCID", 6))
|
|
546 break;
|
|
547 command++;
|
|
548 case 2:
|
|
549 if (!strncmp(buffer, "DTITLE", 6)) {
|
|
550 strncpy(buffer2 + bufs, realstr, BUF2SIZE - bufs);
|
|
551 bufs += len;
|
|
552 break;
|
|
553 }
|
|
554 if (bufs > 0) {
|
|
555 buffer2[BUF2SIZE - 1] = '\0';
|
|
556 if ((temp = strstr(buffer2, " / ")) != NULL) {
|
|
557 cdda_cdinfo_cd_set(cdinfo, g_strdup(temp + 3),
|
|
558 g_strndup(buffer2, temp - buffer2));
|
|
559 }
|
|
560 else
|
|
561 cdda_cdinfo_cd_set(cdinfo, g_strdup(buffer2),
|
|
562 g_strdup(buffer2));
|
|
563 bufs = 0;
|
|
564 }
|
|
565 command++;
|
|
566 case 3:
|
|
567 if (!strncmp(buffer, "TTITLE", 6)) {
|
|
568 num = atoi(buffer + 6);
|
|
569 if (oldnum < 0 || num == oldnum) {
|
|
570 strncpy(buffer2 + bufs, realstr, BUF2SIZE - bufs);
|
|
571 bufs += len;
|
|
572 }
|
|
573 else {
|
|
574 buffer2[BUF2SIZE - 1] = '\0';
|
|
575 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL,
|
|
576 g_strdup(buffer2));
|
|
577 strncpy(buffer2, realstr, BUF2SIZE);
|
|
578 bufs = len;
|
|
579 }
|
|
580 oldnum = num;
|
|
581 break;
|
|
582 }
|
|
583 if (oldnum >= 0)
|
|
584 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL,
|
|
585 g_strdup(buffer2));
|
|
586 bufs = 0;
|
|
587 oldnum = -1;
|
|
588 command++;
|
|
589 case 4:
|
|
590 if (!strncmp(buffer, "EXTD", 4)) {
|
|
591 break;
|
|
592 }
|
|
593 command++;
|
|
594 case 5:
|
|
595 if (!strncmp(buffer, "EXTT", 4)) {
|
|
596 break;
|
|
597 }
|
|
598 command++;
|
|
599 case 6:
|
|
600 if (!strncmp(buffer, "PLAYORDER", 9)) {
|
|
601 break;
|
|
602 }
|
|
603 command++;
|
|
604 default:
|
|
605 g_log(NULL, G_LOG_LEVEL_WARNING, "%s: illegal cddb-data: %s",
|
|
606 PACKAGE, buffer);
|
|
607 break;
|
|
608 }
|
|
609
|
|
610 }
|
|
611
|
|
612 if (oldnum >= 0)
|
|
613 cdda_cdinfo_track_set(cdinfo, oldnum + 1, NULL, g_strdup(buffer2));
|
|
614
|
|
615 fclose(fd);
|
|
616 return (1);
|
|
617 }
|
|
618
|
|
619
|
|
620 void
|
|
621 cdda_cddb_get_info(cdda_disc_toc_t * toc, cdinfo_t * cdinfo)
|
|
622 {
|
|
623 guint32 disc_id;
|
|
624 cddb_disc_header_t cddb_disc_info;
|
|
625 gchar *cddb_file[1];
|
|
626 disc_id = cdda_cddb_compute_discid(toc);
|
|
627 cddb_file[0] = NULL;
|
|
628
|
|
629 if ((cached_id != disc_id)
|
|
630 && (strncmp(cdda_cfg.cddb_server, "file://", 7) != 0)) {
|
|
631 if (cddb_get_protocol_level() == 0)
|
|
632 return;
|
|
633
|
|
634 cached_id = disc_id;
|
|
635 if (!cddb_query(cdda_cfg.cddb_server, toc, &cddb_disc_info))
|
|
636 return;
|
|
637 if (!cddb_read(cdda_cfg.cddb_server, &cddb_disc_info, cdinfo))
|
|
638 return;
|
|
639 cdinfo->is_valid = TRUE;
|
|
640
|
|
641 }
|
|
642 else if ((cached_id != disc_id)
|
|
643 && (strncmp(cdda_cfg.cddb_server, "file://", 7) == 0)) {
|
|
644 cached_id = disc_id;
|
|
645 if (!scan_cddb_dir(cdda_cfg.cddb_server, cddb_file, disc_id))
|
|
646 return;
|
|
647 if (!cddb_read_file(cddb_file[0], &cddb_disc_info, cdinfo)) {
|
|
648 g_free(cddb_file[0]);
|
|
649 return;
|
|
650 }
|
|
651 cdinfo->is_valid = TRUE;
|
|
652 g_free(cddb_file[0]);
|
|
653 }
|
|
654 }
|
|
655
|
|
656 void
|
|
657 cdda_cddb_set_server(const gchar * new_server)
|
|
658 {
|
|
659 if (strcmp(cdda_cfg.cddb_server, new_server)) {
|
|
660 g_free(cdda_cfg.cddb_server);
|
|
661 cdda_cfg.cddb_server = g_strdup(new_server);
|
|
662 cdda_cfg.cddb_protocol_level = 0;
|
|
663 cached_id = 0;
|
|
664 }
|
|
665 }
|
|
666
|
|
667
|
|
668 static gchar *
|
|
669 cddb_position_string(gchar * input)
|
|
670 {
|
|
671 gchar deg[4], min[3];
|
|
672 if (input == NULL || strlen(input) < 7)
|
|
673 return g_strdup("");
|
|
674 strncpy(deg, input + 1, 3);
|
|
675 deg[3] = '\0';
|
|
676 strncpy(min, input + 5, 2);
|
|
677 min[2] = '\0';
|
|
678 return g_strdup_printf("%2d°%s'%c", atoi(deg), min, input[0]);
|
|
679 }
|
|
680
|
|
681 static void
|
|
682 cddb_server_dialog_ok_cb(GtkWidget * w, gpointer data)
|
|
683 {
|
|
684 gchar *text;
|
|
685 gint pos;
|
|
686 GtkEntry *entry = GTK_ENTRY(data);
|
|
687
|
|
688 if (!GTK_CLIST(server_clist)->selection)
|
|
689 return;
|
|
690 pos = GPOINTER_TO_INT(GTK_CLIST(server_clist)->selection->data);
|
|
691 gtk_clist_get_text(GTK_CLIST(server_clist), pos, 0, &text);
|
|
692 cdda_cddb_set_server(text);
|
|
693 gtk_entry_set_text(entry, text);
|
|
694 gtk_widget_destroy(server_dialog);
|
|
695 }
|
|
696
|
|
697 static void
|
|
698 cddb_server_dialog_select(GtkWidget * w, gint row, gint column,
|
|
699 GdkEvent * event, gpointer data)
|
|
700 {
|
|
701 if (event->type == GDK_2BUTTON_PRESS)
|
|
702 cddb_server_dialog_ok_cb(NULL, NULL);
|
|
703 }
|
|
704
|
|
705 void
|
|
706 cdda_cddb_show_server_dialog(GtkWidget * w, gpointer data)
|
|
707 {
|
|
708 GtkWidget *vbox, *bbox, *okbutton, *cancelbutton;
|
|
709 GtkEntry *server_entry = GTK_ENTRY(data);
|
|
710 gchar *titles[] = { "Server", "Latitude", "Longitude", "Description" };
|
|
711 GList *servers;
|
|
712 const gchar *server;
|
|
713 gint level;
|
|
714
|
|
715 if (server_dialog)
|
|
716 return;
|
|
717
|
|
718 server = gtk_entry_get_text(server_entry);
|
|
719
|
|
720 if ((level = cddb_check_protocol_level(server)) < 3) {
|
|
721 if (!level)
|
|
722 xmms_show_message("CDDB",
|
|
723 "Unable to connect to CDDB-server",
|
|
724 "Ok", FALSE, NULL, NULL);
|
|
725 else
|
|
726 /* CDDB level < 3 has the "sites" command,
|
|
727 but the format is different. Not supported yet */
|
|
728 xmms_show_message("CDDB",
|
|
729 "Can't get server list from the current CDDB-server\n"
|
|
730 "Unsupported CDDB protocol level",
|
|
731 "Ok", FALSE, NULL, NULL);
|
|
732 return;
|
|
733 }
|
|
734
|
|
735 if ((servers = cddb_get_server_list(server, level)) == NULL) {
|
|
736 xmms_show_message("CDDB",
|
|
737 "No site information available",
|
|
738 "Ok", FALSE, NULL, NULL);
|
|
739 return;
|
|
740 }
|
|
741
|
|
742 server_dialog = gtk_dialog_new();
|
|
743 g_signal_connect(G_OBJECT(server_dialog), "destroy",
|
|
744 G_CALLBACK(gtk_widget_destroyed), &server_dialog);
|
|
745 gtk_window_set_title(GTK_WINDOW(server_dialog), "CDDB servers");
|
|
746 gtk_window_set_modal(GTK_WINDOW(server_dialog), TRUE);
|
|
747
|
|
748 vbox = gtk_vbox_new(FALSE, 0);
|
|
749 gtk_container_set_border_width(GTK_CONTAINER(vbox), 15);
|
|
750 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(server_dialog)->vbox), vbox,
|
|
751 TRUE, TRUE, 0);
|
|
752
|
|
753 server_clist = gtk_clist_new_with_titles(4, titles);
|
|
754 g_signal_connect(G_OBJECT(server_clist), "select-row",
|
|
755 G_CALLBACK(cddb_server_dialog_select), NULL);
|
|
756 gtk_box_pack_start(GTK_BOX(vbox), server_clist, TRUE, TRUE, 0);
|
|
757
|
|
758 bbox = gtk_hbutton_box_new();
|
|
759 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
|
|
760 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
|
|
761 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(server_dialog)->action_area),
|
|
762 bbox, TRUE, TRUE, 0);
|
|
763
|
|
764 okbutton = gtk_button_new_with_label("Ok");
|
|
765 g_signal_connect(G_OBJECT(okbutton), "clicked",
|
|
766 G_CALLBACK(cddb_server_dialog_ok_cb), data);
|
|
767 gtk_box_pack_start(GTK_BOX(bbox), okbutton, TRUE, TRUE, 0);
|
|
768 cancelbutton = gtk_button_new_with_label("Cancel");
|
|
769 g_signal_connect_swapped(G_OBJECT(cancelbutton), "clicked",
|
|
770 G_CALLBACK(gtk_widget_destroy),
|
|
771 GTK_OBJECT(server_dialog));
|
|
772 gtk_box_pack_start(GTK_BOX(bbox), cancelbutton, TRUE, TRUE, 0);
|
|
773 GTK_WIDGET_SET_FLAGS(okbutton, GTK_CAN_DEFAULT);
|
|
774 GTK_WIDGET_SET_FLAGS(cancelbutton, GTK_CAN_DEFAULT);
|
|
775 gtk_widget_grab_default(okbutton);
|
|
776
|
|
777 while (servers) {
|
|
778 gchar *row[4];
|
|
779 gint i;
|
|
780
|
|
781 row[0] = g_strdup(((gchar **) servers->data)[0]);
|
|
782 row[1] = cddb_position_string(((gchar **) servers->data)[4]);
|
|
783 row[2] = cddb_position_string(((gchar **) servers->data)[5]);
|
|
784 row[3] = g_strdup(((gchar **) servers->data)[6]);
|
|
785 gtk_clist_append(GTK_CLIST(server_clist), row);
|
|
786 for (i = 0; i < 4; i++)
|
|
787 g_free(row[i]);
|
|
788 g_strfreev(servers->data);
|
|
789 servers = g_list_next(servers);
|
|
790 }
|
|
791 g_list_free(servers);
|
|
792 gtk_clist_columns_autosize(GTK_CLIST(server_clist));
|
|
793 gtk_widget_show_all(server_dialog);
|
|
794 }
|
|
795
|
|
796 static gboolean
|
|
797 cddb_update_log_window(gpointer data)
|
|
798 {
|
|
799 if (!debug_window) {
|
|
800 cddb_timeout_id = 0;
|
|
801 return FALSE;
|
|
802 }
|
|
803
|
|
804 G_LOCK(list);
|
|
805 if (temp_messages != NULL) {
|
|
806 GList *temp;
|
|
807 GDK_THREADS_ENTER();
|
|
808 gtk_clist_freeze(GTK_CLIST(debug_clist));
|
|
809 for (temp = temp_messages; temp; temp = temp->next) {
|
|
810 gchar *text = temp->data;
|
|
811 gtk_clist_append(GTK_CLIST(debug_clist), &text);
|
|
812 g_free(text);
|
|
813 }
|
|
814 gtk_clist_columns_autosize(GTK_CLIST(debug_clist));
|
|
815 gtk_clist_thaw(GTK_CLIST(debug_clist));
|
|
816 gtk_clist_moveto(GTK_CLIST(debug_clist),
|
|
817 GTK_CLIST(debug_clist)->rows - 1, -1, 0.5, 0);
|
|
818 GDK_THREADS_LEAVE();
|
|
819 g_list_free(temp_messages);
|
|
820 temp_messages = NULL;
|
|
821 }
|
|
822 G_UNLOCK(list);
|
|
823 return TRUE;
|
|
824 }
|
|
825
|
|
826
|
|
827 void
|
|
828 cdda_cddb_show_network_window(GtkWidget * w, gpointer data)
|
|
829 {
|
|
830 GtkWidget *vbox, *bbox, *close, *scroll_win;
|
|
831 GList *temp;
|
|
832
|
|
833 if (debug_window)
|
|
834 return;
|
|
835
|
|
836 debug_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
837 g_signal_connect(G_OBJECT(debug_window), "destroy",
|
|
838 G_CALLBACK(gtk_widget_destroyed), &debug_window);
|
|
839 gtk_window_set_title(GTK_WINDOW(debug_window), "CDDB networkdebug");
|
|
840 gtk_window_set_resizable(GTK_WINDOW(debug_window), TRUE);
|
|
841 gtk_window_set_default_size(GTK_WINDOW(debug_window), 400, 150);
|
|
842 gtk_container_border_width(GTK_CONTAINER(debug_window), 10);
|
|
843
|
|
844 vbox = gtk_vbox_new(FALSE, 10);
|
|
845 gtk_container_add(GTK_CONTAINER(debug_window), vbox);
|
|
846
|
|
847 scroll_win = gtk_scrolled_window_new(NULL, NULL);
|
|
848 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_win),
|
|
849 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
|
|
850 debug_clist = gtk_clist_new(1);
|
|
851 gtk_container_add(GTK_CONTAINER(scroll_win), debug_clist);
|
|
852 gtk_box_pack_start(GTK_BOX(vbox), scroll_win, TRUE, TRUE, 0);
|
|
853
|
|
854 temp = debug_messages;
|
|
855 while (temp) {
|
|
856 gtk_clist_prepend(GTK_CLIST(debug_clist), (gchar **) & temp->data);
|
|
857 temp = g_list_next(temp);
|
|
858 }
|
|
859
|
|
860 bbox = gtk_hbutton_box_new();
|
|
861 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_SPREAD);
|
|
862 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
|
|
863 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
|
|
864
|
|
865 close = gtk_button_new_with_label("Close");
|
|
866 g_signal_connect_swapped(G_OBJECT(close), "clicked",
|
|
867 G_CALLBACK(gtk_widget_destroy),
|
|
868 GTK_OBJECT(debug_window));
|
|
869 GTK_WIDGET_SET_FLAGS(close, GTK_CAN_DEFAULT);
|
|
870 gtk_box_pack_start(GTK_BOX(bbox), close, TRUE, TRUE, 0);
|
|
871 gtk_widget_grab_default(close);
|
|
872
|
|
873 gtk_clist_columns_autosize(GTK_CLIST(debug_clist));
|
|
874 gtk_clist_set_button_actions(GTK_CLIST(debug_clist), 0,
|
|
875 GTK_BUTTON_IGNORED);
|
|
876 gtk_clist_moveto(GTK_CLIST(debug_clist),
|
|
877 GTK_CLIST(debug_clist)->rows - 1, -1, 0, 0);
|
|
878
|
|
879 cddb_timeout_id = gtk_timeout_add(500, cddb_update_log_window, NULL);
|
|
880 gtk_widget_show_all(debug_window);
|
|
881 }
|
|
882
|
|
883 void
|
|
884 cddb_quit(void)
|
|
885 {
|
|
886 if (cddb_timeout_id)
|
|
887 gtk_timeout_remove(cddb_timeout_id);
|
|
888 cddb_timeout_id = 0;
|
|
889 }
|