add colspan for "total time" in playlist
[soepkiptng.git] / q
blobe9d5ce5a07ff946756a1d75e31bf2a360d33fe0c
1 #!/usr/bin/perl
2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 # $Id$
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, version 2, as
8 # published by the Free Software Foundation.
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.
15 # A copy of the GNU General Public License is available on the World Wide Web
16 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
17 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 ############################################################################
21 use integer;
22 use Cwd 'abs_path';
23 use DBI;
24 use Socket;
25 use Getopt::Std;
27 # find program directory
28 $_ = $0;
29 while(-l) {
30 my $l = readlink or die "readlink $_: $!\n";
31 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
33 m|(.*)/|;
34 my $progdir = abs_path($1);
36 require "$progdir/soepkiptng.lib";
38 getopts('vnc:g');
40 read_configfile(\%conf, $opt_c);
42 eval "use Term::ReadKey;";
43 if($@) {
44 warn "Term::ReadKey not found; assuming 80-column terminal.\n";
45 $screen_width = 80;
46 } else {
47 ($screen_width) = GetTerminalSize();
50 sub albumtrack($$) {
51 my ($album, $track) = @_;
53 if($track) {
54 my $nr = "[$track]";
55 $album = substr($album, 0, $w_al - length($nr));
56 if(length($album) != $w_al - length($nr)) { $album .= " "; }
57 $album .= $nr;
58 } else {
59 $album = substr($album, 0, $w_al);
61 $album;
64 sub splitrange($$) {
65 my ($val, $max) = @_;
66 my @ret;
68 $val =~ s/\b\s+\b/,/g;
69 s/\s//g;
70 foreach(split /,/, $val) {
71 if(/(\d*)-(\d*)/) {
72 my ($b, $e, $i);
73 $b = $1;
74 $e = $2 || $max;
75 for($i = $b; $i <= $e; $i++) {
76 push @ret, $i;
78 } else {
79 push @ret, $_;
82 @ret;
85 sub killit($) {
86 my ($killid) = @_;
87 ($song_id) = kill_song('', $killid);
88 if(!defined($song_id)) {
89 print "not killing (song $killid not playing anymore)\n";
90 return undef;
92 ($t, $a, $al, $tr) = $dbh->selectrow_array(
93 "SELECT song.title,artist.name,album.name,song.track" .
94 " FROM song,artist,album" .
95 " WHERE song.id=$song_id" .
96 " AND song.artist_id=artist.id AND song.album_id=album.id"
98 printf "next: %s - %02d. %s [%s]\n", $a, $tr, $t, $al;
101 $| = 1;
103 $dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
104 $conf{db_user}, $conf{db_pass}) or die "can't connect to database";
106 if($opt_n) {
107 open F, $conf{statusfile} or exit;
108 $nowplaying = <F>;
109 close F;
111 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
112 $user =~ s/,.*//;
113 my $song = add_song_next($dbh, $nowplaying, $user);
115 print "Adding next song ($song->{track}, $song->{title}).\n";
116 exit;
119 $screen_width = 80 if $screen_width == 0; # just in case
120 $w_a = $screen_width * 25 / 100;
121 $w_t = $screen_width * 45 / 100;
122 $w_al = $screen_width - $w_a - $w_t - 7;
123 $w_a > 0 && $w_t > 0 && $w_al > 0 or die "screen size too small.\n";
125 if($0 =~ /qr$/ || @ARGV) {
126 my @a;
127 foreach(@ARGV) {
128 $q = '';
129 if(s/^-//) { $q = " NOT"; }
130 s/^\^// or $_ = "%$_";
131 s/\$$// or $_ .= "%";
133 if($0 =~ /qa$/) {
134 $q .= " artist.name LIKE ?";
135 push @a, $_;
137 elsif($0 =~ /qt$/) {
138 $q .= " song.title LIKE ?";
139 push @a, $_;
141 else {
142 $q .= " (song.title LIKE ? OR artist.name LIKE ? OR album.name LIKE ?)";
143 push @a, $_, $_, $_;
145 push @q, $q;
147 if($opt_g) {
148 push @q, "track > 0";
150 if($0 =~ /qr$/) {
151 push @q, " last_played=0 AND (unix_timestamp(now()) - unix_timestamp(time_added)) < 7*86400";
153 $q = "SELECT title,artist.name as artist,album.name as album,song.id as id,track,filename,length,encoding" .
154 " FROM song,artist,album" .
155 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
156 " AND present AND " . join(" AND ", @q) .
157 " ORDER BY artist.name,album.name,song.track,song.title";
158 #warn $q;
159 $sth = $dbh->prepare($q);
160 $sth->execute(@a);
162 $head = sprintf("\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
163 'Artist', 'Song', 'Album', '-'x$w_a, '-'x$w_t, '-'x$w_al);
165 $i = 1;
166 while($_ = $sth->fetchrow_hashref) {
167 if($opt_g) {
168 my $f = $_->{filename};
169 $f =~ s|//+|/|g;
170 $f =~ s|^(.*)/|| or die;
171 my $d = $1;
172 $d =~ s|^.*/|| or die;
173 $f =~ s|\.[^.]+$||;
174 $f =~ /([^-\w])/ and die "\nERROR: invalid character ($1) in filename ($f)!\n";
175 if(defined($dirname)) {
176 $dirname eq $d or die "\nERROR: inconsistent dirname ($dirname, $d)\n";
177 } else {
178 $dirname = $d;
179 $g_output .= ">$dirname\n";
181 $_->{title} =~ s/^(\W+)/<$1>/;
182 $g_output .= "-$_->{track}\n" if $_->{track};
183 $g_output .= "/$f\n";
184 next;
186 print STDERR $head;
187 $head = '';
188 printf STDERR "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
189 "$i.", $_->{artist}, $_->{title}, albumtrack($_->{album}, $_->{track});
190 $opt_v and printf STDERR "%d:%02d %s %s\n", $_->{length} / 60, $_->{length} % 60,
191 $_->{encoding}, $_->{filename};
192 $id[$i] = $_->{id};
193 $i++;
195 if($opt_g) {
196 print $g_output;
197 exit 0;
199 exit if $i == 1;
200 print STDERR "\nAdd (a=all): ";
201 $_ = <STDIN>;
202 exit unless /\S/;
204 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
205 $user =~ s/,.*//;
206 if(/^a/i) {
207 for($n = 1; $n < $i; $n++) {
208 add_song($dbh, $user, $id[$n]) or warn "can't add song.\n";
210 } else {
211 foreach(splitrange($_, $i)) {
212 add_song($dbh, $user, $id[$_]) or warn "can't add song.\n"
213 if $id[$_];
218 printf "\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
219 'Artist', 'Song', 'Album', '-'x$w_a, '-'x$w_t, '-'x$w_al;
221 $i = 1;
222 if($_ = get_nowplaying($dbh)) {
223 printf "1.* %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
224 $_->{artist}, $_->{title}, albumtrack($_->{album}, $_->{track});
225 if($_->{id}) { $delid[$i++] = $_->{id}; }
226 $nowid = $_->{id};
229 $query = "SELECT song.title,artist.name,album.name,song.id,song.track" .
230 " FROM song,artist,album,queue" .
231 " WHERE song.id=queue.song_id" .
232 " AND song.artist_id=artist.id AND song.album_id=album.id" .
233 " ORDER BY queue.song_order";
234 $sth = $dbh->prepare($query);
235 $rv = $sth->execute;
236 while(@q = $sth->fetchrow_array) {
237 printf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
238 "$i.", $q[1], $q[0], albumtrack($q[2], $q[4]);
239 $delid[$i] = $q[3];
240 $i++;
243 print STDERR "\nDelete (a=all): ";
244 $_ = <STDIN>;
245 exit unless /\S/;
246 if(/^S/) {
247 shuffle_queue($dbh);
248 } elsif(/^a/i) {
249 $sth = $dbh->prepare("DELETE FROM queue");
250 $sth->execute();
251 killit($nowid);
252 } else {
253 foreach(splitrange($_, $i)) {
254 if($_ == 1) { killit($nowid); }
255 else { del_song($dbh, $delid[$_]) if $delid[$_]; }
259 $sth->finish;
260 $dbh->disconnect;