~ubuntu-branches/ubuntu/edgy/gnupod-tools/edgy

1.1.1 by Brian Nelson
Import upstream version 0.98
1
###__PERLBIN__###
1 by Quôc Peyrot
Import upstream version 0.94rc1
2
#  Copyright (C) 2002-2004 Adrian Ulrich <pab at blinkenlights.ch>
3
#  Part of the gnupod-tools collection
4
#
5
#  URL: http://www.gnu.org/software/gnupod/
6
#
7
#    This program is free software; you can redistribute it and/or modify
8
#    it under the terms of the GNU General Public License as published by
9
#    the Free Software Foundation; either version 2 of the License, or
10
#    (at your option) any later version.
11
#
12
#    This program is distributed in the hope that it will be useful,
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#    GNU General Public License for more details.
16
#
17
#    You should have received a copy of the GNU General Public License
18
#    along with this program; if not, write to the Free Software
19
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
#
21
# iTunes and iPod are trademarks of Apple
22
#
23
# This product is not supported/written/published by Apple!
24
25
use strict;
26
use GNUpod::iTunesDB;
27
use GNUpod::XMLhelper;
28
use GNUpod::FooBar;
29
use Getopt::Long;
30
31
use vars qw(%opts);
32
$| = 1;
1.1.1 by Brian Nelson
Import upstream version 0.98
33
34
35
print "tunes2pod.pl Version ###__VERSION__### (C) Adrian Ulrich\n";
1 by Quôc Peyrot
Import upstream version 0.94rc1
36
37
$opts{mount} = $ENV{IPOD_MOUNTPOINT};
38
1.1.1 by Brian Nelson
Import upstream version 0.98
39
GetOptions(\%opts, "version", "force", "help|h", "mount|m=s");
40
GNUpod::FooBar::GetConfig(\%opts, {mount=>'s', force=>'b'}, "tunes2pod");
1 by Quôc Peyrot
Import upstream version 0.94rc1
41
42
43
usage() if $opts{help};
1.1.1 by Brian Nelson
Import upstream version 0.98
44
version() if $opts{version};
45
46
1 by Quôc Peyrot
Import upstream version 0.94rc1
47
48
#Normal operation
49
converter();
50
51
sub converter {
52
$opts{_no_sync} = 1;
53
my $con = GNUpod::FooBar::connect(\%opts);
54
usage("$con->{status}\n") if $con->{status};
55
56
#We disabled all autosyncing (_no_sync set to 1), so we do a test
57
#ourself
58
if(!$opts{force} && !(GNUpod::FooBar::_itb_needs_sync($con))) {
59
 print "I don't think that you have to run tunes2pod.pl\n";
60
 print "The GNUtunesDB looks up-to-date\n";
61
 print "\n";
62
 print "If you think i'm wrong, use '$0 --force'\n";
63
 exit(1);
64
}
65
66
67
68
GNUpod::iTunesDB::open_itunesdb($con->{itunesdb}) or usage("Could not open $con->{itunesdb}\n");
69
70
71
#Check where the FILES and PLAYLIST part starts..
72
#..and how many files are in this iTunesDB
73
my $itinfo = GNUpod::iTunesDB::get_starts();
1.1.1 by Brian Nelson
Import upstream version 0.98
74
75
if(!defined($itinfo)) {
76
  warn "File '$con->{itunesdb}' is not an iTunesDB, wrong magic in header!\n";
77
  exit(1);
78
}
79
1 by Quôc Peyrot
Import upstream version 0.94rc1
80
#This 2 will change while running..
81
my $pos = $itinfo->{position};
82
my $pdi = $itinfo->{pdi};
83
84
print "> Has $itinfo->{songs} songs";
85
86
#Get all files
87
my $href= undef;
88
my $ff = 0;
89
my %hout = ();
90
 for(my $i=0;$i<$itinfo->{songs};$i++) {
91
  ($pos,$href) = GNUpod::iTunesDB::get_mhits($pos); #get the mhit + all child mhods
92
  #Seek failed.. this shouldn't happen..  
93
  if($pos == -1) {
94
   print STDERR "\n*** FATAL: Expected to find $itinfo->{songs} files,\n";
95
   print STDERR "*** but i failed to get nr. $i\n";
96
   print STDERR "*** Your iTunesDB maybe corrupt or you found\n";
97
   print STDERR "*** a bug in GNUpod. Please send this\n";
98
   print STDERR "*** iTunesDB to pab\@blinkenlights.ch\n\n";
99
   exit(1);
100
  }
101
  GNUpod::XMLhelper::mkfile({file=>$href});  
102
  $ff++;
103
 }
104
105
106
#<files> part built
107
print STDOUT "\r> Found $ff files, ok\n";
108
109
110
#Now get each playlist
111
print STDOUT "> Found ".($itinfo->{playlists}-1)." playlists:\n";
112
for(my $i=0;$i<$itinfo->{playlists};$i++) {
113
  ($pdi, $href) = GNUpod::iTunesDB::get_pl($pdi); #Get an mhyp + all child mhods
114
  if($pdi == -1) {
115
   print STDERR "*** FATAL: Expected to find $itinfo->{playlists} playlists,\n";
116
   print STDERR "*** but i failed to get nr. $i\n";
117
   print STDERR "*** Your iTunesDB maybe corrupt or you found\n";
118
   print STDERR "*** a bug in GNUpod. Please send this\n";
119
   print STDERR "*** iTunesDB to pab\@blinkenlights.ch\n\n";
120
   exit(1);
121
  }
122
  next if $href->{type}; #Don't list the MPL
123
  $href->{name} = "NONAME" unless($href->{name}); #Don't create an empty pl
124
  if(ref($href->{splpref}) eq "HASH" && ref($href->{spldata}) eq "ARRAY") { #SPL Data present
125
    print ">> Smart-Playlist '$href->{name}' found\n";
1.1.1 by Brian Nelson
Import upstream version 0.98
126
    render_spl($href->{name},$href->{splpref}, $href->{spldata}, $href->{matchrule},
127
               $href->{content}, $href->{plid});
1 by Quôc Peyrot
Import upstream version 0.94rc1
128
  }
129
  else { #Normal playlist  
130
    print ">> Playlist '$href->{name}' with ".int(@{$href->{content}})." songs\n";
1.1.1 by Brian Nelson
Import upstream version 0.98
131
    GNUpod::XMLhelper::addpl($href->{name}, {plid=>$href->{plid}});
1 by Quôc Peyrot
Import upstream version 0.94rc1
132
    foreach(@{$href->{content}}) {
133
     my $plfh = ();
134
     $plfh->{add}->{id} = $_;
135
     GNUpod::XMLhelper::mkfile($plfh,{plname=>$href->{name}});
136
    }
137
  }
138
139
140
}
141
142
143
144
1.1.1 by Brian Nelson
Import upstream version 0.98
145
GNUpod::XMLhelper::writexml($con);
1 by Quôc Peyrot
Import upstream version 0.94rc1
146
GNUpod::FooBar::setsync_itunesdb($con);
1.1.1 by Brian Nelson
Import upstream version 0.98
147
GNUpod::FooBar::setvalid_otgdata($con);
1 by Quôc Peyrot
Import upstream version 0.94rc1
148
149
#The iTunes is now set to clean .. maybe we have to
150
#update the otg..
151
$opts{_no_sync} = 0;
152
GNUpod::FooBar::connect(\%opts);
153
154
print STDOUT "\n Done\n";
155
exit(0);
156
}
157
158
159
160
#######################################################
161
# create a spl
162
sub render_spl {
1.1.1 by Brian Nelson
Import upstream version 0.98
163
 my($name, $pref, $data, $mr, $content, $plid) = @_;
1 by Quôc Peyrot
Import upstream version 0.94rc1
164
 my $of = undef;
165
 $of->{liveupdate} = $pref->{live};
166
 $of->{moselected} = $pref->{mos};
167
 $of->{matchany}   = $mr;
168
 $of->{limitsort} = $pref->{isort};
169
 $of->{limitval}  = $pref->{value};
170
 $of->{limititem} = $pref->{iitem};
171
 $of->{checkrule} = $pref->{checkrule};
1.1.1 by Brian Nelson
Import upstream version 0.98
172
 $of->{plid}       = $plid;
1 by Quôc Peyrot
Import upstream version 0.94rc1
173
#create this playlist
174
GNUpod::XMLhelper::addspl($name, $of);
175
176
  foreach my $xr (@{$data}) { #Add spldata
177
    GNUpod::XMLhelper::mkfile({spl=>$xr}, {splname=>$name});
178
  }
179
  foreach my $cont(@{$content}) { #Add (old?) content
180
    GNUpod::XMLhelper::mkfile({splcont=>{id=>$cont}}, {splname=>$name});
181
  }
182
183
}
184
185
186
187
188
189
sub usage {
190
my($rtxt) = @_;
191
die << "EOF";
192
$rtxt
193
Usage: tunes2pod.pl [-h] [-m directory]
194
1.1.1 by Brian Nelson
Import upstream version 0.98
195
   -h, --help              display this help and exit
196
       --version           output version information and exit
197
   -m, --mount=directory   iPod mountpoint, default is \$IPOD_MOUNTPOINT
198
       --force             Disable 'sync' checking
199
200
Report bugs to <bug-gnupod\@nongnu.org>
201
EOF
202
}
203
204
sub version {
205
die << "EOF";
206
tunes2pod.pl (gnupod) ###__VERSION__###
207
Copyright (C) Adrian Ulrich 2002-2004
208
209
This is free software; see the source for copying conditions.  There is NO
210
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1 by Quôc Peyrot
Import upstream version 0.94rc1
211
212
EOF
213
}
214
215
216
217