File: CDDB.pm

package info (click to toggle)
lltag 0.14.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 448 kB
  • ctags: 301
  • sloc: perl: 2,833; makefile: 120; sh: 17
file content (714 lines) | stat: -rw-r--r-- 21,046 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package Lltag::CDDB ;

use strict ;

use Lltag::Misc ;

use I18N::Langinfo qw(langinfo CODESET) ;

# return values that are passed to lltag
use constant CDDB_SUCCESS => 0 ;
use constant CDDB_ABORT => -1 ;

# local return values
use constant CDDB_ABORT_TO_KEYWORDS => -10 ;
use constant CDDB_ABORT_TO_CDIDS => -11 ;

# keep track of where we were during the previous CDDB access
my $previous_cdids = undef ;
my $previous_cd = undef ;
my $previous_track = undef ;

# confirmation behavior
my $current_cddb_yes_opt = undef ;

# HTTP browser
my $browser ;

#########################################
# init

my $cddb_supported = 1 ;

my $cddb_track_usage_forced ;
my $cddb_cd_usage_forced ;
my $cddb_keywords_usage_forced ;

sub init_cddb {
    my $self = shift ;

    if (not eval { require LWP ; } ) {
	print "LWP (libwww-perl package) does not seem to be available, disabling CDDB.\n"
	    if $self->{verbose_opt} ;
	$cddb_supported = 0 ;
	return ;
    }

    # default confirmation behavior
    $current_cddb_yes_opt = $self->{yes_opt} ;

    # HTTP browser
    $browser = LWP::UserAgent->new;
    # use HTTP_PROXY environment variable
    $browser->env_proxy ;

    # need to show menu usage once ?
    $cddb_track_usage_forced = $self->{menu_usage_once_opt} ;
    $cddb_cd_usage_forced = $self->{menu_usage_once_opt} ;
    $cddb_keywords_usage_forced = $self->{menu_usage_once_opt} ;
}

#########################################
# freedb.org specific code
# NOT USED ANYMORE since Magix acquired freedb.org
# and closed the online search module for now
#########################################

sub freedborg_cddb_response {
    my $self = shift ;
    my $path = shift ;

    print "      Sending CDDB request...\n" ;
    print "        '$path'\n" if $self->{verbose_opt} ;
    my $response = $browser->get(
	"http://"
	. $self->{cddb_server_name}
	. ($self->{cddb_server_port} != 80 ? $self->{cddb_server_port} : "")
	. $path
	. "\n"
	) ;

    if (!$response->is_success) {
	Lltag::Misc::print_error ("  ",
		"HTTP request to CDDB server ("
		. $self->{cddb_server_name} .":". $self->{cddb_server_port}
		. ") failed.") ;
	return undef ;
    }
    if ($response->content_type ne 'text/html') {
	Lltag::Misc::print_error ("  ",
		"Weird CDDB response (type ".$response->content_type.") from server "
		. $self->{cddb_server_name} .":". $self->{cddb_server_port}
		. ".") ;
	return undef ;
    }
    # TODO: grep for something to be sure it worked

    return $response->content ;
}

sub freedborg_cddb_query_cd_by_keywords {
    my $self = shift ;
    my $keywords = shift ;

    # extract fields and cat from the keywords
    my @fields = () ;
    my @cats = () ;
    my @keywords_list = () ;
    foreach my $word (split / +/, $keywords) {
	if ($word =~ m/^fields=(.+)$/) {
	    push @fields, (split /\++/, $1) ;
	} elsif ($word =~ m/^cats=(.+)$/) {
	    push @cats, (split /\++/, $1) ;
	} else {
	    push @keywords_list, $word ;
	}
    }
    # assemble remaining keywords with "+"
    $keywords = join "+", @keywords_list ;

    # by default, search in all cats, within artist and title only
    @cats = ( "all" ) unless @cats ;
    @fields = ( "artist", "title" ) unless @fields ;

    my $query_fields = (grep { $_ eq "all" } @fields) ? "allfields=YES" : "allfields=NO".(join ("", map { "&fields=$_" } @fields)) ;
    my $query_cats = (grep { $_ eq "all" } @cats) ? "allcats=YES" : "allcats=NO".(join ("", map { "&cats=$_" } @cats)) ;

    my $response = freedborg_cddb_response $self, "/freedb_search.php?words=${keywords}&${query_fields}&${query_cats}&grouping=none&x=0&y=0" ;
    return (CDDB_ABORT, undef) unless defined $response ;

    my @cdids = () ;
    my $samename = undef ;
    my $same = 0 ;

    foreach my $line (split /\n/, $response) {
	next if $line !~ /<a href=\"/ ;
	if ($line =~ m/<tr>/) {
	    $same = 0 ;
	    $samename = undef ;
	} else {
	    $same = 1;
	}
	my @links = split (/<a href=\"/, $line) ;
	shift @links ;
	while (my $link = shift @links) {
	    if ($link =~ m@http://.*/freedb_search_fmt\.php\?cat=([a-z]+)\&id=([0-9a-f]+)\">(.*)</a>@) {
		my %cdid = ( CAT => $1, ID => $2, NAME => $same ? $samename : $3 ) ;
		push @cdids, \%cdid ;
		$samename = $cdid{NAME} unless $same ;
		$same = 1;
	    }
	}
    }

    return (CDDB_SUCCESS, \@cdids) ;
}

sub freedborg_cddb_query_tracks_by_id {
    my $self = shift ;
    my $cat = shift ;
    my $id = shift ;
    my $name = shift ;

    my $response = freedborg_cddb_response $self, "/freedb_search_fmt.php?cat=${cat}&id=${id}" ;
    return (CDDB_ABORT, undef) unless defined $response ;

    my $cd ;
    $cd->{CAT} = $cat ;
    $cd->{ID} = $id ;

    foreach my $line (split /\n/, $response) {
	if ($line =~ m/tracks: (\d+)/i) {
	    $cd->{TRACKS} = $1 ;
	} elsif ($line =~ m/total time: ([\d:]+)/i) {
	    $cd->{"TOTAL TIME"} = $1 ;
	} elsif ($line =~ m/genre: (\w+)/i) {
	    $cd->{GENRE} = $1 ;
	} elsif ($line =~ m/id3g: (\d+)/i) {
	    $cd->{ID3G} = $1 ;
	} elsif ($line =~ m/year: (\d+)/i) {
	    $cd->{DATE} = $1 ;
	} elsif ($line =~ m@ *(\d+)\.</td><td valign=top> *(-?[\d:]+)</td><td><b>(.*)</b>@) {
	    # '-?' because there are some buggy entries...
	    my %track = ( TITLE => $3, TIME => $2 ) ;
	    $cd->{$1} = \%track ;
	} elsif ($line =~ m@<h2>(.+ / .+)</h2>@) {
	    if (defined $name) {
		if ($name ne $1) {
		    Lltag::Misc::print_warning ("      ", "Found CD name '$1' instead of '$name', this entry might be corrupted") ;
		}
	    } else {
		$name = $1 ;
	    }
	}
    }

    return (CDDB_SUCCESS, undef)
	unless defined $name ;

    # FIXME: are we sure no artist or album may contain " / " ?
    $name =~ m@^(.+) / (.+)$@ ;
    $cd->{ARTIST} = $1 ;
    $cd->{ALBUM} = $2 ;

    # FIXME: check number and indexes of tracks ?

    return (CDDB_SUCCESS, $cd) ;
}

sub freedborg_cddb_query_cd_by_keywords_usage {
    my $indent = shift ;
    print $indent."<space-separated keywords> => CDDB query for CD matching the keywords\n" ;
    print $indent."  Search in all CD categories within fields 'artist' and 'title' by default\n" ;
    print $indent."    cats=foo+bar   => Search in CD categories 'foo' and 'bar' only\n" ;
    print $indent."    fields=all     => Search keywords in all fields\n" ;
    print $indent."    fields=foo+bar => Search keywords in fields 'foo' and 'bar'\n" ;
    print $indent."<category>/<hexadecinal id> => CDDB query for CD matching category and id\n" ;
}

my $freedborg_cddb_backend = {
    cddb_query_cd_by_keywords => \&freedborg_cddb_query_cd_by_keywords,
    cddb_query_tracks_by_id => \&freedborg_cddb_query_tracks_by_id,
    cddb_query_cd_by_keywords_usage => \&freedborg_cddb_query_cd_by_keywords_usage,
} ;

#########################################
# tracktype.org specific code
# USED since november 2006
#########################################

sub tracktypeorg_cddb_response {
    my $self = shift ;
    my $path = shift ;
    my $postdata = shift ;

    my $response ;
    print "      Sending CDDB request...\n" ;
    if (defined $postdata) {
	print "        'POST $path'\n" if $self->{verbose_opt} ;
	$response = $browser->post(
				   "http://"
				   . $self->{cddb_server_name}
				   . ($self->{cddb_server_port} != 80 ? $self->{cddb_server_port} : "")
				   . $path,
				   $postdata
				   ) ;
    } else {
	print "        'GET $path'\n" if $self->{verbose_opt} ;
	$response = $browser->get(
				  "http://"
				  . $self->{cddb_server_name}
				  . ($self->{cddb_server_port} != 80 ? $self->{cddb_server_port} : "")
				  . $path
				  ) ;
    }

    if (!$response->is_success) {
	Lltag::Misc::print_error ("  ",
		"HTTP request to CDDB server ("
		. $self->{cddb_server_name} .":". $self->{cddb_server_port}
		. ") failed.") ;
	return undef ;
    }
    if ($response->content_type ne 'text/plain') {
	Lltag::Misc::print_error ("  ",
		"Weird CDDB response (type ".$response->content_type.") from server "
		. $self->{cddb_server_name} .":". $self->{cddb_server_port}
		. ".") ;
	return undef ;
    }

    my $content = $response->content ;

    # deal with windows line-break
    $content =~ s/\r\n/\n/g ;

    # convert from utf8 if not using a utf8 locale
    utf8::decode($content)
	unless $self->{utf8} ;

    return $content ;
}

sub tracktypeorg_cddb_query_cd_by_keywords {
    my $self = shift ;
    my $keywords = shift ;

    my %postdata = ( "hello" => "lltag",
		     "proto" => 4,
		     "cmd" => "cddb album $keywords",
		     ) ;
    my $response = tracktypeorg_cddb_response $self, "/~cddb/cddb.cgi", \%postdata ;
    return (CDDB_ABORT, undef) unless defined $response ;

    my @lines = (split /\n/, $response) ;
    # check status in header
    my $header = shift @lines ;
    # TODO: check status

    my @cdids = () ;
    foreach my $line (@lines) {
	if ($line =~ m@^([^ ]+) ([^ ]+) (.+ / .+)$@) {
	    my %cdid = ( CAT => $1, ID => $2, NAME => $3 ) ;
	    push @cdids, \%cdid ;
	}
    }

    return (CDDB_SUCCESS, \@cdids) ;
}

sub tracktypeorg_cddb_query_tracks_by_id {
    my $self = shift ;
    my $cat = shift ;
    my $id = shift ;
    my $name = shift ;

    my $response = tracktypeorg_cddb_response $self, "/freedb/${cat}/${id}" ;
    return (CDDB_ABORT, undef) unless defined $response ;

    # TODO: grep for something to be sure it worked

    my $cd ;
    $cd->{CAT} = $cat ;
    $cd->{ID} = $id ;
    $cd->{TRACKS} = 0 ;

    foreach my $line (split /\n/, $response) {
	next if $line =~ /^#/ ;
	if ($line =~ m/^DISCID=(.+)/) {
	    if ($id ne $1) {
		    Lltag::Misc::print_warning ("      ", "Found CD id '$1' instead of '$id', this entry might be corrupted") ;
	    }
	} elsif ($line =~ m@^DTITLE=(.*)@) {
	    if (defined $name) {
		if ($name ne $1) {
		    Lltag::Misc::print_warning ("      ", "Found CD name '$1' instead of '$name', this entry might be corrupted") ;
		}
	    } else {
		$name = $1 ;
	    }
	} elsif ($line =~ m/^DYEAR=(.*)/) {
	    $cd->{DATE} = $1 ;
	} elsif ($line =~ m/^DGENRE=(.*)/) {
	    $cd->{GENRE} = $1 ;
	} elsif ($line =~ m/^TTITLE(\d+)=(.*)/) {
	    my $num = $1 + 1;
	    if ($num != $cd->{TRACKS} + 1) {
		Lltag::Misc::print_warning ("      ", "Found CD track '$num' instead of '".($cd->{TRACKS}+1)."', this entry might be corrupted") ;
	    }
	    my %track = ( TITLE => $2 ) ;
	    $cd->{$num} = \%track ;
	    $cd->{TRACKS} = $num ;
	}
    }

    return (CDDB_SUCCESS, undef)
	unless defined $name ;

    # FIXME: are we sure no artist or album may contain " / " ?
    $name =~ m@^(.+) / (.+)$@ ;
    $cd->{ARTIST} = $1 ;
    $cd->{ALBUM} = $2 ;

    return (CDDB_SUCCESS, $cd) ;
}

sub tracktypeorg_cddb_query_cd_by_keywords_usage {
    my $indent = shift ;
    print $indent."<space-separated keywords> => CDDB query for CD matching the keywords\n" ;
    print $indent."  Search in all CD categories within fields 'artist' OR 'album'\n" ;
    print $indent."<category>/<hexadecinal id> => CDDB query for CD matching category and id\n" ;
}

my $tracktypeorg_cddb_backend = {
    cddb_query_cd_by_keywords => \&tracktypeorg_cddb_query_cd_by_keywords,
    cddb_query_tracks_by_id => \&tracktypeorg_cddb_query_tracks_by_id,
    cddb_query_cd_by_keywords_usage => \&tracktypeorg_cddb_query_cd_by_keywords_usage,
} ;

my $cddb_backend = $tracktypeorg_cddb_backend ;

######################################################
# interactive menu to browse CDDB, tracks in a CD

sub cddb_track_usage {
    Lltag::Misc::print_usage_header ("    ", "Choose Track in CDDB CD") ;
    print "      <index> => Choose a track of the current CD (current default is Track $previous_track)\n" ;
    print "      <index> a => Choose a track and do not ask for confirmation anymore\n" ;
    print "      a => Use default track and do not ask for confirmation anymore\n" ;
    print "      E => Edit current CD common tags\n" ;
    print "      V => View the list of CD matching the keywords\n" ;
    print "      c => Change the CD chosen in keywords query results list\n" ;
    print "      k => Start again CDDB query with different keywords\n" ;
    print "      q => Quit CDDB query\n" ;
    print "      h => Show this help\n" ;

    $cddb_track_usage_forced = 0 ;
}

sub print_cd {
    my $cd = shift ;
    map {
	print "    $_: $cd->{$_}\n" ;
    } grep { $_ !~ /^\d+$/ } (keys %{$cd}) ;
    my $track_format = "    Track %0".(length $cd->{TRACKS})."d: %s%s\n" ;
    for(my $i=1; $i <= $cd->{TRACKS}; $i++) {
	my $track = $cd->{$i} ;
	my $title = "<unknown title>" ;
	$title = $track->{TITLE} if exists $track->{TITLE} and defined $track->{TITLE} ;
	my $time = "" ;
	$time = " ($track->{TIME})" if exists $track->{TIME} and defined $track->{TIME} ;
	printf ($track_format, $i, $title, $time) ;
    }
}

sub get_cddb_tags_from_tracks {
    my $self = shift ;
    my $cd = shift ;
    my $tracknumber = undef ;

    # update previous_track to 1 or ++
    $previous_track = 0
	unless defined $previous_track ;
    $previous_track++ ;

    # if automatic mode and still in the CD, let's go
    if ($current_cddb_yes_opt and $previous_track <= $cd->{TRACKS}) {
	$tracknumber = $previous_track ;
	Lltag::Misc::print_notice ("    ", "Automatically choosing next CDDB track, #$tracknumber...") ;
	goto FOUND ;
    }

    # either in non-automatic or reached the end of the CD, dump the contents
    print_cd $cd ;

    # reached the end of CD, reset to the beginning
    if ($previous_track == $cd->{TRACKS} + 1) {
	$previous_track = 1;
	if ($current_cddb_yes_opt) {
	    Lltag::Misc::print_notice ("  ", "Reached the end of the CD, returning to interactive mode") ;
	    # return to previous confirmation behavior
	    $current_cddb_yes_opt = $self->{yes_opt} ;
	}
    }

    cddb_track_usage
	if $cddb_track_usage_forced ;

    while (1) {
	my $reply = Lltag::Misc::readline ("  ", "Enter track index [<index>aEVckq]".
			" (default is Track $previous_track, h for help)", "", -1) ;

	# if ctrl-d, abort cddb
	$reply = 'q' unless defined $reply ;

	$reply = $previous_track
	    if $reply eq '' ;

	return (CDDB_ABORT, undef)
	    if $reply =~ m/^q/ ;

	return (CDDB_ABORT_TO_KEYWORDS, undef)
	    if $reply =~ m/^k/ ;

	return (CDDB_ABORT_TO_CDIDS, undef)
	    if $reply =~ m/^c/ ;

	if ($reply =~ m/^E/) {
	    # move editable values into a temporary hash
	    my $values_to_edit = {} ;
	    foreach my $key (keys %{$cd}) {
		next if $key eq 'TRACKS' or $key =~ /^\d+$/ ;
		$values_to_edit->{$key} = $cd->{$key} ;
		delete $cd->{$key} ;
	    }
	    # clone them so that we can restore them if canceled
	    my $values_edited = Lltag::Tags::clone_tag_values ($values_to_edit) ;
	    # edit them
	    my $res = Lltag::Tags::edit_values ($self, $values_edited) ;
	    # replace the edited values with the originals if canceled
	    $values_edited = $values_to_edit if $res == Lltag::Tags->EDIT_CANCEL ;
	    # move them back
	    foreach my $key (keys %{$values_edited}) {
		$cd->{$key} = $values_edited->{$key} ;
	    }
	    next ;
	}

	if ($reply =~ m/^V/) {
	    print_cd $cd ;
	    next ;
	} ;

	if ($reply =~ m/^a/) {
	    $reply = $previous_track ;
	    $current_cddb_yes_opt = 1 ;
	}
	if ($reply =~ m/^(\d+) *a/) {
	    $current_cddb_yes_opt = 1 ;
	    $reply = $1 ;
	}

	if ($reply =~ m/^\d+$/ and $reply >= 1 and $reply <= $cd->{TRACKS}) {
	    $tracknumber = $reply ;
	    last ;
	}

	cddb_track_usage () ;
    }

   FOUND:
    my $track = $cd->{$tracknumber} ;
    # get the track tags
    my %values ;
    foreach my $key (keys %{$cd}) {
	next if $key eq 'TRACKS' or $key =~ /^\d+$/ ;
	$values{$key} = $cd->{$key} ;
    }
    $values{TITLE} = $track->{TITLE} if exists $track->{TITLE} ;
    $values{NUMBER} = $tracknumber ;

    # save the previous track number
    $previous_track = $tracknumber ;

    return (CDDB_SUCCESS, \%values) ;
}

##########################################################
# interactive menu to browse CDDB, CDs in a query results

sub cddb_cd_usage {
    Lltag::Misc::print_usage_header ("    ", "Choose CD in CDDB Query Results") ;
    print "      <index> => Choose a CD in the current keywords query results list\n" ;
    print "      V => View the list of CD matching the keywords\n" ;
    print "      k => Start again CDDB query with different keywords\n" ;
    print "      q => Quit CDDB query\n" ;
    print "      h => Show this help\n" ;

    $cddb_cd_usage_forced = 0 ;
}

sub print_cdids {
    my $cdids = shift ;

    my $cdid_format = "    %0".(length (scalar @{$cdids}))."d: %s (cat=%s, id=%s)\n" ;
    for(my $i=0; $i < @{$cdids}; $i++) {
	my $cdid = $cdids->[$i] ;
	printf ($cdid_format, $i+1, $cdid->{NAME}, $cdid->{CAT}, $cdid->{ID}) ;
    }
}

# returns (SUCCESS, undef) if CDDB returned an bad/empty CD
sub get_cddb_tags_from_cdid {
    my $self = shift ;
    my $cdid = shift ;

    my $cddb_query_tracks_by_id_func = $cddb_backend->{cddb_query_tracks_by_id} ;
    my ($res, $cd) = &{$cddb_query_tracks_by_id_func} ($self, $cdid->{CAT}, $cdid->{ID}, $cdid->{NAME}) ;
    return (CDDB_ABORT, undef) if $res == CDDB_ABORT ;

    if (!$cd or !$cd->{TRACKS}) {
	print "    There is no tracks in this CD.\n" ;
	return (CDDB_SUCCESS, undef) ;
    }

    $previous_cd = $cd ;
    undef $previous_track ;

    return get_cddb_tags_from_tracks $self, $cd ;
}

sub get_cddb_tags_from_cdids {
    my $self = shift ;
    my $cdids = shift ;

  AGAIN:
    print_cdids $cdids ;

    cddb_cd_usage
	if $cddb_cd_usage_forced ;

    while (1) {
	my $reply = Lltag::Misc::readline ("  ", "Enter CD index [<index>Vkq] (no default, h for help)", "", -1) ;

	# if ctrl-d, abort cddb
	$reply = 'q' unless defined $reply ;

	next if $reply eq '' ;

	return (CDDB_ABORT, undef)
	    if $reply =~ m/^q/ ;

	return (CDDB_ABORT_TO_KEYWORDS, undef)
	    if $reply =~ m/^k/ ;

	goto AGAIN
	    if $reply =~ m/^V/ ;

	if ($reply =~ m/^\d+$/ and $reply >= 1 and $reply <= @{$cdids}) {
	    # do the actual query for CD contents
	    my ($res, $values) = get_cddb_tags_from_cdid $self, $cdids->[$reply-1] ;
	    goto AGAIN if $res == CDDB_ABORT_TO_CDIDS or ($res == CDDB_SUCCESS and not defined $values) ;
	    return ($res, $values) ;
	}

	cddb_cd_usage () ;
    }
}

##########################################################
# interactive menu to browse CDDB, keywords query

sub cddb_keywords_usage {
    Lltag::Misc::print_usage_header ("    ", "CDDB Query by Keywords") ;
    my $cddb_query_cd_by_keywords_usage_func = $cddb_backend->{cddb_query_cd_by_keywords_usage} ;
    &{$cddb_query_cd_by_keywords_usage_func} ("      ") ;
    print "      q => Quit CDDB query\n" ;
    print "      h => Show this help\n" ;

    $cddb_keywords_usage_forced = 0 ;
}

sub get_cddb_tags {
    my $self = shift ;
    my ($res, $values) ;

    if (!$cddb_supported) {
	print "  Cannot use CDDB without LWP (libwww-perl package).\n" ;
	goto ABORT ;
    }

    if (defined $previous_cd) {
	bless $previous_cd ;
	print "  Going back to previous CD cat=$previous_cd->{CAT} id=$previous_cd->{ID}\n" ;
	($res, $values) = get_cddb_tags_from_tracks $self, $previous_cd ;
	if ($res == CDDB_ABORT_TO_CDIDS) {
	    bless $previous_cdids ;
	    ($res, $values) = get_cddb_tags_from_cdids $self, $previous_cdids ;
	}
	goto OUT if $res == CDDB_SUCCESS ;
	goto ABORT if $res == CDDB_ABORT ;
    }

    cddb_keywords_usage
	if $cddb_keywords_usage_forced ;

    while (1) {
	my $keywords ;
	if (defined $self->{requested_cddb_query}) {
	    $keywords = $self->{requested_cddb_query} ;
	    print "  Using command-line given keywords '$self->{requested_cddb_query}'...\n" ;
	    undef $self->{requested_cddb_query} ;
	    # FIXME: either put it in the history, or preput it next time
	} else {
	    $keywords = Lltag::Misc::readline ("  ", "Enter CDDB query [<query>q] (no default, h for help)", "", -1) ;
	    # if ctrl-d, abort cddb
	    $keywords = 'q' unless defined $keywords ;
	}

	next if $keywords eq '' ;

	# be careful to match the whole reply, not only the first char
	# since multiple chars are valid keyword queries

	goto ABORT
	    if $keywords eq 'q' ;

	if ($keywords eq 'h') {
	    cddb_keywords_usage () ;
	    next ;
	}

	# it this a category/id ?
	if ($keywords =~ m@^\s*(\w+)/([\da-f]+)\s*$@) {
	    my $cdid ;
	    $cdid->{CAT} = $1 ;
	    $cdid->{ID} = $2 ;
	    # FIXME: do not show 'c' for goto to CD list in there
	    ($res, $values) = get_cddb_tags_from_cdid $self, $cdid ;
	    goto OUT if $res == CDDB_SUCCESS and defined $values ;
	    goto ABORT if $res == CDDB_ABORT ;
	    next ;
	}

	# do the actual query for CD id with keywords
	my $cdids ;
	my $cddb_query_cd_by_keywords_func = $cddb_backend->{cddb_query_cd_by_keywords} ;
	($res, $cdids) = &{$cddb_query_cd_by_keywords_func} ($self, $keywords) ;
	goto ABORT if $res == CDDB_ABORT ;

	if (!@{$cdids}) {
	    print "    No CD found.\n" ;
	    next ;
	}

	$previous_cdids = $cdids ;
	$previous_cd = undef ;

	($res, $values) = get_cddb_tags_from_cdids $self, $cdids ;
	next if $res == CDDB_ABORT_TO_KEYWORDS ;
	goto OUT ;
    }

 OUT:
    goto ABORT if $res == CDDB_ABORT ;
    return ($res, $values) ;

 ABORT:
    $previous_cdids = undef ;
    $previous_cd = undef ;
    $previous_track = undef ;
    return (CDDB_ABORT, undef);
}

1 ;