2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
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 ############################################################################
27 # find program directory
30 my $l = readlink or die "readlink $_: $!\n";
31 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
34 my $progdir = abs_path
($1);
36 require "$progdir/soepkiptng.lib";
40 read_configfile
(\
%conf, $opt_c);
42 eval "use Term::ReadKey;";
44 warn "Term::ReadKey not found; assuming 80-column terminal.\n";
48 ($screen_width) = GetTerminalSize
();
55 my ($album, $track) = @_;
59 $album = substr($album, 0, $w_al - length($nr));
60 if(length($album) != $w_al - length($nr)) { $album .= " "; }
63 $album = substr($album, 0, $w_al);
72 $val =~ s/\b\s+\b/,/g;
74 foreach(split /,/, $val) {
79 for($i = $b; $i <= $e; $i++) {
91 ($song_id) = kill_song
('', $killid);
92 if(!defined($song_id)) {
93 print "not killing (song $killid not playing anymore)\n";
96 ($t, $a, $al, $tr) = $dbh->selectrow_array(
97 "SELECT song.title,artist.name,album.name,song.track" .
98 " FROM song,artist,album" .
99 " WHERE song.id=$song_id" .
100 " AND song.artist_id=artist.id AND song.album_id=album.id"
102 printf "next: %s - %02d. %s [%s]\n", $a, $tr, $t, $al;
107 $dbh = DBI
->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
108 $conf{db_user
}, $conf{db_pass
}) or die "can't connect to database";
111 open F
, $conf{statusfile
} or exit;
115 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
117 my $song = add_song_next
($dbh, $nowplaying, $user);
119 print "Adding next song ($song->{track}, $song->{title}).\n";
123 $screen_width = 80 if $screen_width == 0; # just in case
124 $w_a = $screen_width * 25 / 100;
125 $w_t = $screen_width * 45 / 100;
126 $w_al = $screen_width - $w_a - $w_t - 7;
127 $w_a > 0 && $w_t > 0 && $w_al > 0 or die "screen size too small.\n";
129 if($0 =~ /qr$/ || @ARGV) {
133 if(s/^-//) { $q = " NOT"; }
134 s/^\^// or $_ = "%$_";
135 s/\$$// or $_ .= "%";
138 $q .= " artist.name LIKE ?";
142 $q .= " album.name LIKE ?";
146 $q .= " song.title LIKE ?";
150 $q .= " (song.title LIKE ? OR artist.name LIKE ? OR album.name LIKE ?)";
156 push @q, "track > 0";
159 push @q, " last_played=0 AND (unix_timestamp(now()) - unix_timestamp(time_added)) < 7*86400";
161 $q = "SELECT title,artist.name as artist,album.name as album,song.id as id,track,filename,length,encoding" .
162 " FROM song,artist,album" .
163 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
164 " AND present AND " . join(" AND ", @q) .
165 " ORDER BY artist.name,album.name,song.track,song.title";
167 $sth = $dbh->prepare($q);
170 $head = sprintf("\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
171 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al);
174 while($_ = $sth->fetchrow_hashref) {
176 my $f = $_->{filename
};
178 $f =~ s
|^(.*)/|| or die;
180 $d =~ s
|^.*/|| or die;
182 $f =~ /([^-\w])/ and die "\nERROR: invalid character ($1) in filename ($f)!\n";
183 if(defined($dirname)) {
184 $dirname eq $d or die "\nERROR: inconsistent dirname ($dirname, $d)\n";
187 $g_output .= ">$dirname\n";
189 $_->{title
} =~ s/^(\W+)/<$1>/;
190 $g_output .= "-$_->{track}\n" if $_->{track
};
191 $g_output .= "/$f\n";
196 printf STDERR
"%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
197 "$i.", $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
198 $opt_v and printf STDERR
"%d:%02d %s %s\n", $_->{length} / 60, $_->{length} % 60,
199 $_->{encoding
}, $_->{filename
};
209 $i == 2 or die "More than 1 hit\n";
212 print STDERR
"\nAdd (a=all): ";
217 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
220 for($n = 1; $n < $i; $n++) {
221 add_song
($dbh, $user, $id[$n]) or warn "can't add song.\n";
224 foreach(splitrange
($_, $i)) {
225 add_song
($dbh, $user, $id[$_]) or warn "can't add song.\n"
231 printf "\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
232 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al;
235 if($_ = get_nowplaying
($dbh)) {
236 printf "1.* %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
237 $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
238 if($_->{id
}) { $delid[$i++] = $_->{id
}; }
242 $query = "SELECT song.title,artist.name,album.name,song.id,song.track" .
243 " FROM song,artist,album,queue" .
244 " WHERE song.id=queue.song_id" .
245 " AND song.artist_id=artist.id AND song.album_id=album.id" .
246 " ORDER BY queue.song_order";
247 $sth = $dbh->prepare($query);
249 while(@q = $sth->fetchrow_array) {
250 printf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
251 "$i.", $q[1], $q[0], albumtrack
($q[2], $q[4]);
257 print STDERR
"\nDelete (a=all): ";
263 $sth = $dbh->prepare("DELETE FROM queue");
267 foreach(splitrange
($_, $i)) {
268 if($_ == 1) { killit
($nowid); }
269 else { del_song
($dbh, $delid[$_]) if $delid[$_]; }