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";
47 ($screen_width) = GetTerminalSize
();
51 my ($album, $track) = @_;
55 $album = substr($album, 0, $w_al - length($nr));
56 if(length($album) != $w_al - length($nr)) { $album .= " "; }
59 $album = substr($album, 0, $w_al);
68 $val =~ s/\b\s+\b/,/g;
70 foreach(split /,/, $val) {
75 for($i = $b; $i <= $e; $i++) {
87 ($song_id) = kill_song
('', $killid);
88 if(!defined($song_id)) {
89 print "not killing (song $killid not playing anymore)\n";
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;
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";
107 open F
, $conf{statusfile
} or exit;
111 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
113 my $song = add_song_next
($dbh, $nowplaying, $user);
115 print "Adding next song ($song->{track}, $song->{title}).\n";
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) {
129 if(s/^-//) { $q = " NOT"; }
130 s/^\^// or $_ = "%$_";
131 s/\$$// or $_ .= "%";
134 $q .= " artist.name LIKE ?";
138 $q .= " song.title LIKE ?";
142 $q .= " (song.title LIKE ? OR artist.name LIKE ? OR album.name LIKE ?)";
148 push @q, "track > 0";
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";
159 $sth = $dbh->prepare($q);
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);
166 while($_ = $sth->fetchrow_hashref) {
168 my $f = $_->{filename
};
170 $f =~ s
|^(.*)/|| or die;
172 $d =~ s
|^.*/|| or die;
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";
179 $g_output .= ">$dirname\n";
181 $_->{title
} =~ s/^(\W+)/<$1>/;
182 $g_output .= "-$_->{track}\n" if $_->{track
};
183 $g_output .= "/$f\n";
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
};
200 print STDERR
"\nAdd (a=all): ";
204 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
207 for($n = 1; $n < $i; $n++) {
208 add_song
($dbh, $user, $id[$n]) or warn "can't add song.\n";
211 foreach(splitrange
($_, $i)) {
212 add_song
($dbh, $user, $id[$_]) or warn "can't add song.\n"
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;
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
}; }
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);
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]);
243 print STDERR
"\nDelete (a=all): ";
249 $sth = $dbh->prepare("DELETE FROM queue");
253 foreach(splitrange
($_, $i)) {
254 if($_ == 1) { killit
($nowid); }
255 else { del_song
($dbh, $delid[$_]) if $delid[$_]; }