~ubuntu-branches/ubuntu/utopic/cdrdao/utopic

« back to all changes in this revision

Viewing changes to contrib/mp32dao/MediaHandler.pm

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This program is free software; you can redistribute it and/or modify
 
3
# it under the terms of the GNU General Public License as published by
 
4
# the Free Software Foundation; either version 2 of the License, or
 
5
# (at your option) any later version.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU Library General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
15
#
 
16
#Copyright 2001 Giuseppe Corbelli - cowo@lugbs.linux.it
 
17
#builds a list of handlers for all mediafiles in a directory
 
18
 
 
19
package MediaHandler;
 
20
require mp3handler;
 
21
require ogghandler;
 
22
require flachandler;
 
23
@ISA = qw(mp3handler ogghandler flachandler);
 
24
use strict; 
 
25
use vars qw ( @ext $AUTOLOAD );
 
26
 
 
27
#These are supported media files. Each time a new one is added
 
28
#its extension is put here and appropriate handler is instantiated in
 
29
#MediaHandler constructor.
 
30
@ext = ('.mp3', '.ogg', '.flac');
 
31
 
 
32
#2 args: self name and directory name
 
33
#returns blessed reference
 
34
#reads all filenames (extensions in @ext) in given directory,
 
35
#creates an handler for each one and puts istances in $self->{'LIST'}
 
36
sub new {
 
37
        my ($proto, $dirname) = @_;
 
38
        my ($class) = ref($proto)||$proto;
 
39
 
 
40
        my ($filename, $dh, @filelist, $self);
 
41
        my ($total)=0;
 
42
        $self->{'DIR'} = $dirname;
 
43
        die ("Cannot open directory $dirname\n") if (!opendir (my ($DH), "$dirname"));
 
44
        die ("No write permissions for directory $dirname\n") if (!-w "$dirname");
 
45
        while ($filename = readdir ($DH))       {
 
46
                next if ( (!-r "$dirname/$filename" ) || (-z "$dirname/$filename") || (-d "$dirname/$filename") );
 
47
                chomp ($filename);
 
48
                foreach (@ext)  {
 
49
                        if ($filename =~ m/\Q$_\E/i)    {
 
50
                                push (@filelist, $filename);
 
51
                                $total++;
 
52
                        }
 
53
                }
 
54
        }
 
55
        $self->{'TOTAL'} = $total;
 
56
        @filelist=sort(@filelist);
 
57
        foreach $filename (@filelist)   {
 
58
                if ($filename =~ m/\.ogg/i)     {
 
59
                        push (@{$self->{'LIST'}}, ogghandler->new("$dirname/$filename"));
 
60
                }
 
61
                if ($filename =~ m/\.wav/i)     {
 
62
                }
 
63
                if ($filename =~ m/\.mp3/i)     {
 
64
                        push (@{$self->{'LIST'}}, mp3handler->new("$dirname/$filename"));
 
65
                }
 
66
                if ($filename =~ m/\.flac/i)    {
 
67
                        push (@{$self->{'LIST'}}, flachandler->new("$dirname/$filename"));
 
68
                }
 
69
        }
 
70
        bless ($self, $class);
 
71
        return $self;
 
72
}
 
73
 
 
74
sub AUTOLOAD    {
 
75
        my ($self) = shift;
 
76
        return if $AUTOLOAD =~ /::DESTROY$/;
 
77
        $AUTOLOAD =~ s/^.*:://;
 
78
                ($AUTOLOAD =~ /^\s*dir\s*$/i) ? $AUTOLOAD = 'DIR' : 1;
 
79
        ($AUTOLOAD =~ /^\s*list\s*$/i) ? $AUTOLOAD = 'LIST' : 1;
 
80
        ($AUTOLOAD =~ /^\s*total\s*$/i) ? $AUTOLOAD = 'TOTAL' : 1;
 
81
        return ($self->{$AUTOLOAD});
 
82
}
 
83
 
 
84
1;