source: ProjectBuilder/projects/CDDBeditor/bin/CDDBeditor@ 846

Last change on this file since 846 was 846, checked in by Bruno Cornec, 15 years ago

Solve encoding issues for title, artist and tracks

  • Property svn:executable set to *
File size: 10.2 KB
Line 
1#!/usr/bin/perl -w
2#
3# Reads CDDB info, allow modifications and send back to CDDB server
4#
5# $Id$
6#
7# Copyright B. Cornec 2009
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use ProjectBuilder::Base;
17use ProjectBuilder::Conf;
18use Mail::Sendmail;
19use POSIX qw(strftime :sys_wait_h);
20use Newt qw(NEWT_FLAG_WRAP NEWT_ENTRY_SCROLL NEWT_ANCHOR_LEFT :entry :textbox :macros);
21use lib qw (lib);
22use locale;
23use CDDB_get qw( get_cddb get_discids );
24use CDDB;
25use Encode;
26
27# Global variables
28my %opts; # CLI Options
29my $action; # action to realize
30
31my @date = pb_get_date();
32my $pbdate = strftime("%Y-%m-%d", @date);
33
34=pod
35
36=head1 NAME
37
38CDDBeditor, read, modidy and send your CDDB contrinutions
39
40=head1 DESCRIPTION
41
42CDDBeditor helps you read, modidy and send your CDDB contrinutions.
43
44=head1 SYNOPSIS
45
46CDDBeditor [-vhmq][-t|-g]
47
48CDDBeditor [--verbose][--help][--man][--quiet][--text|--graphic]
49
50=head1 OPTIONS
51
52=over 4
53
54=item B<-v|--verbose>
55
56Print a brief help message and exits.
57
58=item B<-q|--quiet>
59
60Do not print any output.
61
62=item B<-h|--help>
63
64Print a brief help message and exits.
65
66=item B<--man>
67
68Prints the manual page and exits.
69
70=item B<-t|--text>
71=item B<-g|--graphic>
72
73Interface is either text mode or graphical mode (newt based)
74
75=back
76
77=head1 ARGUMENTS
78
79=head1 WEB SITES
80
81The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
82
83=head1 USER MAILING LIST
84
85None exists for the moment.
86
87=head1 CONFIGURATION FILES
88
89=head1 AUTHORS
90
91The CDDBeditor team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
92
93=head1 COPYRIGHT
94
95CDDBeditor is distributed under the GPL v2.0 license
96described in the file C<COPYING> included with the distribution.
97
98=cut
99
100# ---------------------------------------------------------------------------
101
102#Avoids an error from Newt
103no AutoLoader;
104
105
106# Initialize the syntax string
107
108my $projver = "PBVER";
109my $projrev = "PBREV";
110
111pb_syntax_init("CDDBeditor Version $projver-$projrev\n");
112
113GetOptions("help|?|h" => \$opts{'h'},
114 "man" => \$opts{'man'},
115 "verbose|v+" => \$opts{'v'},
116 "quiet|q" => \$opts{'q'},
117 "text|t" => \$opts{'t'},
118 "graphic|g" => \$opts{'g'},
119 "version|V=s" => \$opts{'V'},
120) || pb_syntax(-1,0);
121
122if (defined $opts{'h'}) {
123 pb_syntax(0,1);
124}
125if (defined $opts{'man'}) {
126 pb_syntax(0,2);
127}
128if (defined $opts{'v'}) {
129 $pbdebug = $opts{'v'};
130}
131if (defined $opts{'q'}) {
132 $pbdebug=-1;
133}
134pb_log_init($pbdebug, $pbLOG);
135
136
137Newt::Init();
138my ($ce_sl, $ce_sh) = Newt::GetScreenSize();
139
140# String definitions
141my $ce_title = "CDDBeditor";
142my $ce_help = "(c) Bruno Cornec 2009 - All right reversed under the GPL v2";
143my $artist_label = Newt::Label("Artist: ");
144my $data_label = Newt::Label("Data: ");
145my $raw_label = Newt::Label("Raw: ");
146my $title_label = Newt::Label("Title: ");
147my $year_label = Newt::Label("Year: ");
148my $id_label = Newt::Label("Id: ");
149my $genre_label = Newt::Label("Genre: ");
150my $category_label = Newt::Label("Category: ");
151my $info_label = Newt::Label("Info: ");
152my $tr_track_label = Newt::Label("Track");
153my $tr_frames_label = Newt::Label("Frames");
154my $tno_label = Newt::Label("Track #:");
155my $tr_title_label = Newt::Label("Title");
156my $tr_author_label = Newt::Label("Author");
157my $tr_length_label = Newt::Label("Length");
158
159my $host_label = Newt::Label("Host: ");
160my $port_label = Newt::Label("Port: ");
161my $mode_label = Newt::Label("Mode: ");
162my $dev_label = Newt::Label("Device: ");
163my $proxy_label = Newt::Label("Proxy: ");
164my $user_label = Newt::Label("Multi CD: ");
165
166my $wait_label = Newt::Label("Please wait while searching ...");
167
168# To be put in conf file
169my %ce_config;
170$ce_config{"CDDB_HOST"}="freedb.freedb.org";
171$ce_config{"CDDB_PORT"}=80;
172my %ce_cddb_mode;
173$ce_cddb_mode{"0"} = "http";
174$ce_cddb_mode{"1"} = "cddb";
175
176$ce_config{"CDDB_MODE"}=$ce_cddb_mode{"0"};
177$ce_config{"CD_DEVICE"}="/dev/sr0";
178$ce_config{"HTTP_PROXY"}=$ENV{http_proxy} if $ENV{http_proxy};
179$ce_config{"input"}=0;
180
181# First panel for configuration parameters
182Newt::Cls();
183Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
184Newt::PushHelpLine($ce_help);
185Newt::Refresh();
186
187my $flag = NEWT_ENTRY_SCROLL;
188my $width = 40;
189my $host_entry = Newt::Entry($width, $flag, $ce_config{"CDDB_HOST"});
190$width = 5;
191my $port_entry = Newt::Entry($width, $flag, $ce_config{"CDDB_PORT"});
192my $mode_group;
193my $proxy_entry = undef;
194if ($ce_config{"CDDB_MODE"} eq $ce_cddb_mode{"0"} ) {
195 $mode_group = Newt::HRadiogroup($ce_cddb_mode{"0"}, $ce_cddb_mode{"1"});
196 $proxy_entry = Newt::Entry($width, $flag, $ce_config{"HTTP_PROXY"});
197} else {
198 $mode_group = Newt::HRadiogroup($ce_cddb_mode{"1"}, $ce_cddb_mode{"0"});
199}
200$width = 40;
201my $dev_entry = Newt::Entry($width, $flag, $ce_config{"CD_DEVICE"});
202my $user_group;
203if ($ce_config{input} == 0 ) {
204 $user_group = Newt::HRadiogroup('FALSE', 'TRUE');
205} else {
206 $user_group = Newt::HRadiogroup('TRUE', 'FALSE');
207}
208my $quit_button = Newt::Button("Quit");
209$quit_button->Tag("Quit");
210my $next_button = Newt::Button("Next");
211$next_button->Tag("Next");
212
213my $ce_panel = Newt::Panel(2, 20, "CDDBeditor Configuration");
214my $flage = NEWT_ANCHOR_LEFT;
215$ce_panel->Add(0, 0, $host_label);
216$ce_panel->Add(1, 0, $host_entry, $flage);
217$ce_panel->Add(0, 1, $port_label);
218$ce_panel->Add(1, 1, $port_entry, $flage);
219$ce_panel->Add(0, 2, $mode_label);
220$ce_panel->Add(1, 2, $mode_group, $flage);
221$ce_panel->Add(0, 3, $proxy_label);
222$ce_panel->Add(1, 3, $proxy_entry, $flage);
223$ce_panel->Add(0, 4, $dev_label);
224$ce_panel->Add(1, 4, $dev_entry, $flage);
225$ce_panel->Add(0, 5, $user_label);
226$ce_panel->Add(1, 5, $user_group, $flage);
227$ce_panel->Add(0, 6, $next_button);
228$ce_panel->Add(1, 6, $quit_button);
229
230Newt::Refresh();
231my ($reason, $data) = $ce_panel->Run();
232
233Newt::Cls();
234exit 0 if ($data->Tag() eq "Quit");
235
236$ce_config{"CDDB_HOST"}=$host_entry->Get();
237$ce_config{"CDDB_PORT"}=$port_entry->Get();
238$ce_config{"CDDB_MODE"}=$ce_cddb_mode{$mode_group->Get()};
239$ce_config{"CD_DEVICE"}=$dev_entry->Get();
240$ce_config{"HTTP_PROXY"}=$proxy_entry->Get();
241$ce_config{"input"}=$user_group->Get();
242
243# Components
244my $reload_button = Newt::Button("Reload");
245$reload_button->Tag("Reload");
246my $send_button = Newt::Button("Send");
247$send_button->Tag("Send");
248
249# Entering the loop
250for (my $i=0 ; ; $i++) {
251 # Second panel to make user wait
252 Newt::Cls();
253 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
254 Newt::PushHelpLine($ce_help);
255 $ce_panel = Newt::Panel(3, 20, "CDDB Search");
256 $ce_panel->Add(0, 0, $wait_label);
257
258 ($reason, $data) = $ce_panel->Draw();
259 Newt::Refresh();
260
261 # CDDB query
262 my %ce_cd = get_cddb(\%ce_config);
263
264 # Modify enconding of some fields
265 $ce_cd{artist} = decode("iso8859-1",$ce_cd{artist});
266 $ce_cd{title} = decode("iso8859-1",$ce_cd{artist});
267
268 # Third panel to display CD Infos
269 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
270 Newt::PushHelpLine($ce_help);
271
272 # Components
273 $flag = NEWT_ENTRY_SCROLL;
274 $width = 40;
275 my $artist_entry = Newt::Entry($width, $flag, $ce_cd{artist});
276 my $title_entry = Newt::Entry($width, $flag, $ce_cd{title});
277 my $category_entry = Newt::Entry($width, $flag, $ce_cd{cat});
278 my $genre_entry = Newt::Entry($width, $flag, $ce_cd{genre});
279 my $data_entry = Newt::Entry($width, $flag, Dumper(@{$ce_cd{data}}));
280 my $raw_entry = Newt::Entry($width, $flag, Dumper(@{$ce_cd{raw}}));
281 my $tno_entry = Newt::Label($ce_cd{tno});
282 $width = 5;
283 my $year_entry = Newt::Entry($width, $flag, $ce_cd{year});
284
285 # Build interface
286 $ce_panel = Newt::Panel(3, $ce_sh, "CDDB Info");
287 $ce_panel->Add(0, 0, $artist_label);
288 $ce_panel->Add(1, 0, $artist_entry, $flage);
289 $ce_panel->Add(0, 1, $title_label);
290 $ce_panel->Add(1, 1, $title_entry, $flage);
291 $ce_panel->Add(0, 2, $year_label);
292 $ce_panel->Add(1, 2, $year_entry, $flage);
293 $ce_panel->Add(0, 3, $category_label);
294 $ce_panel->Add(1, 3, $category_entry, $flage);
295 $ce_panel->Add(0, 4, $genre_label);
296 $ce_panel->Add(1, 4, $genre_entry, $flage);
297 $ce_panel->Add(0, 5, $data_label);
298 $ce_panel->Add(1, 5, $data_entry, $flage);
299 $ce_panel->Add(0, 6, $raw_label);
300 $ce_panel->Add(1, 6, $raw_entry, $flage);
301 $ce_panel->Add(0, 7, $tno_label);
302 $ce_panel->Add(1, 7, $tno_entry, $flage);
303 #$ce_panel->Add(0, 5, $info_label);
304 #$ce_panel->Add(1, 5, $info_entry, $flage);
305
306 my $n=1;
307 $width = 40;
308 while ( $n <= $ce_cd{tno} ) {
309 last if ($n > $ce_sh - 8);
310 my $from=$ce_cd{frames}[$n-1];
311 my $to=$ce_cd{frames}[$n]-1;
312 my $dur=$to-$from;
313 my $min=int($dur/75/60);
314 my $sec=int($dur/75)-$min*60;
315 my $frm=($dur-$sec*75-$min*75*60)*100/75;
316 my $tr = sprintf "Track %2d:", $n;
317 my $track_label = Newt::Label($tr);
318 $tr = sprintf " %2d':%.2d", $min, $sec;
319 my $dur_label = Newt::Label($tr);
320 $flag = NEWT_FLAG_WRAP|NEWT_ENTRY_SCROLL;
321 # Adapt encoding of track
322 $ce_cd{track}[$n -1] = decode("iso8859-1",$ce_cd{track}[$n -1]);
323 my $track_entry = Newt::Entry($width, $flag, $ce_cd{track}[$n-1]);
324 $ce_panel->Add(0, 8+$n, $track_label);
325 $ce_panel->Add(1, 8+$n, $track_entry, $flage);
326 $ce_panel->Add(2, 8+$n, $dur_label, $flage);
327 $n++;
328 }
329
330 $ce_panel->Add(0, 8+$n, $send_button);
331 $ce_panel->Add(1, 8+$n, $reload_button);
332 $ce_panel->Add(2, 8+$n, $quit_button);
333
334 Newt::Cls();
335 Newt::Refresh();
336 # Build interface
337 ($reason, $data) = $ce_panel->Run();
338
339 $ce_cd{artist} = $artist_entry->Get();
340 $ce_cd{title} = $title_entry->Get();
341 $ce_cd{year} = $year_entry->Get();
342 $ce_cd{cat} = $category_entry->Get();
343 $ce_cd{genre} = $genre_entry->Get();
344 Newt::Refresh();
345 Newt::Cls();
346 exit 0 if ($data->Tag() eq "Quit");
347
348 # Send the data
349 if ($data->Tag() eq "Send") {
350 $flag = NEWT_FLAG_WRAP;
351 $width = 40;
352 my $height = 25;
353 my $info_tb = Newt::Textbox($width, $height, $flag, Dumper(keys %ce_cd));
354
355 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
356 Newt::PushHelpLine($ce_help);
357
358 Newt::Cls();
359 $ce_panel = Newt::Panel(3, $width, "CDDB Sending Infos");
360 $ce_panel->Add(0, 0, $wait_label);
361 $ce_panel->Add(0, 1, $info_tb);
362 $ce_panel->Add(0, 2, $quit_button);
363 Newt::Refresh();
364 ($reason, $data) = $ce_panel->Run();
365 }
366}
367Newt::Finished();
368exit 0;
369
370# return the char to start printing a centered string based on screen size
371sub ce_center_string {
372
373 my $string = shift || "";
374
375 my $res = ($ce_sl / 2 ) - (length($string) / 2);
376 if ($res < 0 ) {
377 return 0;
378 } else {
379 return ($res);
380 }
381}
382
3831;
Note: See TracBrowser for help on using the repository browser.