Created
June 30, 2010 21:27
-
-
Save squentin/459246 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (C) 2009 Quentin Sculo <squentin@free.fr> | |
# | |
# This file is part of Gmusicbrowser. | |
# Gmusicbrowser is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License version 3, as | |
# published by the Free Software Foundation | |
=gmbplugin LOG | |
Log | |
Logging plugin | |
Put now-playing info in a file, and log recently played info in a file. | |
=cut | |
package GMB::Plugin::LOG; | |
use strict; | |
use warnings; | |
use constant | |
{ OPT => 'PLUGIN_LOG_', | |
}; | |
my $handle; | |
my $lasttime; | |
::SetDefaultOptions(OPT, nowformat => '%t by %a', recentformat => '%t by %a', maxlines => 10, timeformat => '%c'); | |
::SetDefaultOptions(OPT, nowfile => '/mnt/ramdisk/now', recentfile => '/mnt/ramdisk/log'); | |
sub Start | |
{ $handle={}; | |
::Watch($handle,'Played',\&Played); | |
::Watch($handle,'SongID',\&Changed); | |
::Watch($handle,'Playing',\&Changed); | |
} | |
sub Stop | |
{ ::UnWatch($handle,'Played'); | |
::UnWatch($handle,'Playing'); | |
} | |
sub prefbox | |
{ my $vbox=Gtk2::VBox->new(::FALSE, 2); | |
my $sg1=Gtk2::SizeGroup->new('horizontal'); | |
my $sg2=Gtk2::SizeGroup->new('horizontal'); | |
my $max=::NewPrefSpinButton(OPT.'maxlines',undef,1,0,1,1000,1,10,_"Maximum number of lines in the recently played file",undef,$sg1,$sg2); | |
my $nowfile= ::NewPrefFileEntry(OPT.'nowfile', _"File for now-playing :", undef,undef,undef,$sg1,$sg2); | |
my $nowline= ::NewPrefEntry (OPT.'nowformat', _"Line format for now-playing", undef, $sg1,$sg2); | |
my $recentfile= ::NewPrefFileEntry(OPT.'recentfile', _"Recently played file :", undef,undef,undef,$sg1,$sg2); | |
my $recentline= ::NewPrefEntry (OPT.'recentformat', _"Recently played line format", undef, $sg1,$sg2); | |
my $time= ::NewPrefEntry (OPT.'timeformat', _"Time format", undef,$sg1,$sg2, undef, _"\$starttime / \$endtime will be replaced by the starting/ending time in this date format"); | |
my $invert= ::NewPrefCheckButton(OPT.'invertorder', _"Most Recently played song on top"); | |
# for v1.1.x | |
#my $file=::NewPrefFileEntry(OPT.'file',_"Log file :", sizeg1=>$sg1,sizeg2=>$sg2); | |
#my $entry=::NewPrefEntry(OPT.'format', _"Line format", sizeg1=>$sg1,sizeg2=>$sg2); | |
#my $time=::NewPrefEntry(OPT.'timeformat', _"Time format", sizeg1=>$sg1,sizeg2=>$sg2, tip=> _"\$starttime / \$endtime will be replaced by the starting/ending time in this date format"); | |
$vbox->pack_start($_,::FALSE,::FALSE,2) for $nowfile,$nowline, | |
Gtk2::HSeparator->new, $recentfile,$recentline,$max,$invert, | |
Gtk2::HSeparator->new, $time; | |
return $vbox; | |
} | |
sub Changed | |
{ return unless defined $::SongID && $::TogPlay; | |
return if $lasttime && $::StartTime==$lasttime; #if song hasn't really changed | |
$lasttime=$::StartTime; | |
my $ID=$::SongID; | |
my $file= $::Options{OPT.'nowfile'}; | |
my $format= $::Options{OPT.'nowformat'} || '%t by %a'; | |
my $timeformat= $::Options{OPT.'timeformat'} || '%c'; | |
return unless $file; | |
my $line= ::ReplaceFieldsAndEsc($ID,$format)."\n"; #the fields are escaped with g_markup_escape_text | |
my $starttime=::strftime($timeformat,localtime($::StartTime)); | |
$line=~s/\$starttime/$starttime/g; #escape ? | |
open my($fh),'>:utf8',$file or do { warn "Can't open $file for writing : $!\n"; return; }; | |
print $fh $line; | |
close $fh; | |
} | |
sub Played | |
{ my $ID=$::PlayingID; | |
my $file= $::Options{OPT.'recentfile'}; | |
my $format= $::Options{OPT.'recentformat'} || '%t by %a'; | |
my $max= $::Options{OPT.'maxlines'}; | |
my $timeformat= $::Options{OPT.'timeformat'} || '%c'; | |
my $invert= $::Options{OPT.'invertorder'}; | |
return unless $file; | |
my $newline= ::ReplaceFieldsAndEsc($ID,$format)."\n"; #the fields are escaped with g_markup_escape_text | |
my $starttime=::strftime($timeformat,localtime($::StartTime)); | |
my $endtime= ::strftime($timeformat,localtime); | |
$newline=~s/\$starttime/$starttime/g; #escape ? | |
$newline=~s/\$endtime/$endtime/g; | |
my @lines; | |
if (-e $file) | |
{ open my($fh),'<:utf8',$file or do { warn "Can't open $file : $!\n"; return; }; | |
@lines=(<$fh>); | |
close $fh; | |
} | |
if ($invert) { unshift @lines,$newline; $#lines=$max+1 if @lines>$max; } | |
else { push @lines,$newline; splice @lines,0,@lines-$max if @lines>$max; } | |
open my($fh),'>:utf8',$file or do { warn "Can't open $file for writing : $!\n"; return; }; | |
print $fh $_ for @lines; | |
close $fh; | |
} | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment