id3fs-tag: view tags
[id3fs.git] / lib / ID3FS / AudioFile / Mp3.pm
index 8ac84c4..cce6a29 100644 (file)
@@ -1,3 +1,19 @@
+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 package ID3FS::AudioFile::Mp3;
 
 use strict;
@@ -14,47 +30,64 @@ sub new
     $self->{path}=shift;
     $self->{mp3}=MP3::Tag->new($self->{path});
     $self->get_tags();
-    $self->{v1}=undef;
-    $self->{v1}=$self->{mp3}->{ID3v1} if(exists($self->{mp3}->{ID3v1}));
-    $self->{v2}=undef;
-    $self->{v2}=$self->{mp3}->{ID3v2} if(exists($self->{mp3}->{ID3v2}));
-
     $self->{tags}={};
 
     return $self;
 }
 
-sub choose
+sub set
 {
-    my ($self, $func, $verbose)=@_;
-    my $thing=undef;
-    if(defined($self->{v2}))
+    my ($self, $func, $value)=@_;
+    return $self->choose($func) unless($value);
+    unless(exists($self->{mp3}->{ID3v1}))
     {
-       $thing=$self->{v2}->$func();
+       $self->{mp3}->new_tag("ID3v1");
     }
-    if(defined($self->{v1}) && (!defined($thing) || !length($thing)))
+    unless(exists($self->{mp3}->{ID3v2}))
     {
-       $thing=$self->{v1}->$func();
+       $self->{mp3}->new_tag("ID3v2");
     }
-    if(!defined($thing) || !length($thing))
+    my $method=$func . "_set";
+    $self->{mp3}->$method($value, 1);
+    return $value;
+}
+
+sub choose
+{
+    my($self, $func)=@_;
+    my $thing=undef;
+    if(exists($self->{mp3}->{ID3v2}))
     {
-       warn("$self->{path}: no $func defined in ID3 tags\n") if($verbose);
-       return undef;
+       $thing=$self->{mp3}->{ID3v2}->$func();
+    }
+    if(exists($self->{mp3}->{ID3v1}) && (!defined($thing) || !length($thing)))
+    {
+       $thing=$self->{mp3}->{ID3v1}->$func();
     }
     return $thing;
 }
 
-sub artist    { shift->choose("artist", 1); }
-sub album     { shift->choose("album", 1);  }
-# We don't care if year is not set
-sub year      { shift->choose("year", 0);   }
-sub audiotype { return "mp3";               }
-sub haspic    { return undef;               } # FIXME
+sub year      { return(shift->set("year",     @_)); }
+sub artist    { return(shift->set("artist",   @_)); }
+sub album     { return(shift->set("album",    @_)); }
+sub track     { return(shift->set("title",    @_)); }
+sub tracknum  { return(shift->set("track",    @_)); }
+sub comment   { return(shift->set("comment",  @_)); }
+
+sub audiotype { return "mp3";         }
+sub haspic    { return undef;         } # NEXTVERSION
+
 sub v1genre
 {
-    my($self)=@_;
+    my($self, $val)=@_;
+    if($val)
+    {
+       $self->{mp3}->new_tag("ID3v1") unless(defined($self->{mp3}->{ID3v1}));
+       $self->{mp3}->{ID3v1}->genre($val);
+       return $val;
+    }
     my $genre=undef;
-    $genre=$self->{v1}->genre() if(defined($self->{v1}));
+    $genre=$self->{ID3v1}->genre() if(defined($self->{ID3v1}));
     return $genre;
 }
 
@@ -85,4 +118,10 @@ sub get_tags
     }
 }
 
+sub write
+{
+    my $self=shift;
+    $self->{mp3}->update_tags();
+}
+
 1;