use tagtree in queries
[id3fs.git] / lib / ID3FS / Path.pm
1 package ID3FS::Path;
2
3 use strict;
4 use warnings;
5 use ID3FS::PathElement::Artist;
6 use ID3FS::PathElement::Album;
7 use ID3FS::PathElement::Boolean;
8 use ID3FS::PathElement::File;
9 use ID3FS::PathElement::Tag;
10 use ID3FS::PathElement::Tagval;
11 use ID3FS::Path::Node;
12
13 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
14      $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
15      $STATE_FILE)=(0..7);
16
17 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
18
19 sub new
20 {
21     my $proto=shift;
22     my $class=ref($proto) || $proto;
23     my $self={};
24     bless($self,$class);
25
26     $self->{elements}=[];
27     $self->{db}=shift;
28     $self->{path}=shift;
29     $self->parse();
30 #    print "STATE: ", $self->state(), "\n";
31     return $self;
32 }
33
34 sub isdir
35 {
36     my($self)=@_;
37     if(($self->state() == $STATE_FILE) ||
38        ($self->state() == $STATE_INVALID))
39     {
40         return 0;
41     }
42     return 1;
43 }
44
45 sub isfile
46 {
47     my($self)=@_;
48     return 1 if($self->state() == $STATE_FILE);
49     return 0;
50 }
51
52 sub isvalid
53 {
54     my($self)=@_;
55     return($self->state() != $STATE_INVALID);
56 }
57
58 sub dest
59 {
60     my($self, $mountpoint)=@_;
61     if($self->state() == $STATE_FILE)
62     {
63         return $self->{db}->filename($mountpoint, $self);
64     }
65     return "ERROR"; #should never happen?
66 }
67
68 sub dirents
69 {
70     my($self)=@_;
71     my @dents=();
72     my $state=$self->state();
73 #    print "DIRENTS: STATE: $state\n";
74 #    print "DIRENTS: FILE: $self->{path}\n";
75     if($state==$STATE_TAG || $state==$STATE_TAGVAL)
76     {
77         my $tag=$self->{elements}->[$#{$self->{elements}}];
78         if($state==$STATE_TAG &&
79            defined($tag) &&
80            ref($tag) eq "ID3FS::PathElement::Tag" &&
81            $self->{db}->tag_has_values($tag->{id}))
82         {
83             @dents=$self->{db}->tags($self);
84         }
85         else
86         {
87             @dents=(qw(AND OR TRACKS NOARTIST),
88                     $self->{db}->artists($self));
89         }
90     }
91     elsif($state==$STATE_BOOLEAN)
92     {
93         my $parent=$self->{elements}->[$#{$self->{elements}}];
94         unless(defined($parent) &&
95                ref($parent) eq "ID3FS::PathElement::Boolean" &&
96                $parent->{name} eq "NOT")
97         {
98             push(@dents, "NOT");
99         }
100         push(@dents, $self->{db}->tags($self));
101     }
102     elsif($state==$STATE_ROOT)
103     {
104         @dents=(qw(ALL NOT), $self->{db}->tags($self));
105     }
106     elsif($state==$STATE_ALBUMS)
107     {
108         @dents=(qw(TRACKS NOALBUM),$self->{db}->albums($self));
109     }
110     elsif($state==$STATE_TRACKLIST)
111     {
112         @dents=$self->{db}->tracks($self);
113     }
114     else
115     {
116         print "DIRENTS: UNHANDLED STATE: $state\n";
117     }
118     return(@dents);
119 }
120
121 sub parse
122 {
123     my($self)=@_;
124     @{$self->{components}}=split(/\//, $self->{path});
125     shift @{$self->{components}}; # drop empty field before leading /
126 #    print "PATH: $self->{path}\n";
127     $self->state($STATE_ROOT);
128     return if($self->{path} eq "/");
129     my @parts=@{$self->{components}};
130     my($tag, $tagval);
131     $self->{elements}=[];
132     while(my $name=shift @parts)
133     {
134 #       print "NAME: $name\n";
135         my $state=$self->state();
136         if($state==$STATE_INVALID)
137         {
138 #           print "SM: INVALID: $name\n";
139             return;
140         }
141         elsif($state==$STATE_ROOT)
142         {
143 #           print "SM: ROOT: $name\n";
144             if($name eq "ALL")
145             {
146                 $self->state($STATE_TAG);
147             }
148             elsif($name eq "NOT")
149             {
150                 push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
151                 $self->state($STATE_BOOLEAN);
152             }
153             else
154             {
155                 $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
156                 if($tag)
157                 {
158                     push(@{$self->{elements}}, $tag);
159                     $self->state($STATE_TAG);
160                 }
161                 else
162                 {
163                     $self->state($STATE_INVALID);
164                 }
165             }
166         }
167         elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
168         {
169 #           print "SM: TAG/TAGVAL($state): $name\n";
170             my $tag=$self->{elements}->[$#{$self->{elements}}];
171             if($state==$STATE_TAG &&
172                defined($tag) &&
173                ref($tag) eq "ID3FS::PathElement::Tag" &&
174                $self->{db}->tag_has_values($tag->{id}))
175             {
176                 my $tagval=ID3FS::PathElement::Tag->new($self->{db}, $name, $tag->{id});
177                 if(defined($tagval))
178                 {
179                     $self->state($STATE_TAGVAL);
180                     # stay in tag state
181                     push(@{$self->{elements}}, $tagval);
182                 }
183                 else
184                 {
185                     $self->state($STATE_INVALID);
186                 }
187             }
188             elsif($name eq "TRACKS")
189             {
190                 $self->state($STATE_TRACKLIST);
191             }
192             elsif($name eq "NOARTIST")
193             {
194                 $self->state($STATE_TRACKLIST);
195             }
196             elsif($name eq "AND")
197             {
198                 $self->state($STATE_BOOLEAN);
199                 push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
200             }
201             elsif($name eq "OR")
202             {
203                 $self->state($STATE_BOOLEAN);
204                 push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
205             }
206             else
207             {
208                 my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
209                 if($artist)
210                 {
211                     push(@{$self->{elements}}, $artist);
212                     $self->state($STATE_ALBUMS);
213                 }
214                 else
215                 {
216                     $self->state($STATE_INVALID);
217                 }
218             }
219         }
220         elsif($state==$STATE_BOOLEAN)
221         {
222 #           print "SM: BOOLEAN: $name\n";
223             my $parent=$self->{elements}->[$#{$self->{elements}}];
224             my $allownot=1;
225             if(defined($parent) &&
226                ref($parent) eq "ID3FS::PathElement::Boolean" &&
227                $parent->{name} eq "NOT")
228             {
229                 $allownot=0;
230             }
231             if($allownot && $name eq "NOT")
232             {
233                 $self->state($STATE_BOOLEAN);
234                 push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
235             }
236             else
237             {
238                 my $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
239                 if($tag)
240                 {
241                     push(@{$self->{elements}}, $tag);
242                     $self->state($STATE_TAG);
243                 }
244                 else
245                 {
246                     $self->state($STATE_INVALID);
247                 }
248             }
249         }
250         elsif($state==$STATE_ALBUMS)
251         {
252 #           print "SM: ALBUM: $name\n";
253             if($name eq "TRACKS")
254             {
255                 $self->state($STATE_TRACKLIST);
256             }
257             elsif($name eq "NOALBUM")
258             {
259                 $self->state($STATE_TRACKLIST);
260             }
261             else
262             {
263                 my $album=ID3FS::PathElement::Album->new($self->{db}, $name);
264                 if($album)
265                 {
266                     push(@{$self->{elements}}, $album);
267                     $self->state($STATE_TRACKLIST);
268                 }
269                 else
270                 {
271                     $self->state($STATE_INVALID);
272                 }
273             }
274         }
275         elsif($state==$STATE_TRACKLIST)
276         {
277 #           print "SM: TRACKLIST: $name\n";
278             my $track=ID3FS::PathElement::File->new($self->{db}, $name);
279             if($track)
280             {
281                 push(@{$self->{elements}}, $track);
282                 $self->state($STATE_FILE);
283             }
284             else
285             {
286                 $self->state($STATE_INVALID);
287             }
288         }
289         elsif($state==$STATE_FILE)
290         {
291 #           print "SM: FILE: $name\n";
292             # Can't have anything after a filename
293             $self->state($STATE_INVALID);
294         }
295         else
296         {
297             print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
298             $self->state($STATE_INVALID);
299         }
300     }
301     # remove trailing boolean
302     if(@{$self->{elements}} &&
303        ref($self->{elements}->[$#{$self->{elements}}]) eq "ID3FS::PathElement::Boolean")
304     {
305         $self->{lastop}=pop @{$self->{elements}};
306     }
307     # sort elements by precedence
308     @{$self->{elements}}=$self->sort_elements(@{$self->{elements}});
309     $self->{tagtree}=$self->elements_to_tree([ @{$self->{elements}} ]);
310     if($self->{tagtree})
311     {
312         ($self->{sqlconditions},
313          $self->{andsneeded}) = $self->{tagtree}->to_sql();
314 #       print("SQL CONDITION(", $self->{andsneeded}, "): ",
315 #             $self->{sqlconditions}, "\n");
316 #       use Data::Dumper;
317 #       print Dumper $self->{tagtree};
318     }
319 }
320
321 sub state
322 {
323     my($self, $newstate)=@_;
324     $self->{state}=$newstate if(defined($newstate));
325     return $self->{state};
326 }
327
328 sub elements_to_tree
329 {
330     my($self, $elements)=@_;
331     return undef unless(@$elements);
332     my ($left, $right, $op)=(undef, undef, undef);
333     my $thing=pop @$elements;
334     if(ref($thing) eq "ID3FS::PathElement::Boolean")
335     {
336         my $op=$thing;
337         $right=$self->elements_to_tree($elements);
338         if($op->{name} ne "NOT")
339         {
340             $left=$self->elements_to_tree($elements);
341         }
342         return ID3FS::Path::Node->new($left, $op, $right);
343     }
344     else
345     {
346         return ID3FS::Path::Node->new($thing);
347     }
348 }
349
350 # Dijkstra's shunting-yard algorithm
351 sub sort_elements
352 {
353     my ($self, @input)=@_;
354     my @opstack=();
355     my @output=();
356 #    print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
357     while(my $thing = shift @input)
358     {
359         if(ref($thing) eq "ID3FS::PathElement::Tag")
360         {
361 #           print "Pushing $thing->{name} to output\n";
362             push(@output, $thing);
363 #           print "OPSTACK: ", join(', ', map { $_->{name}; } @opstack), "\n";
364 #           print "OUTPUT: ", join(', ', map { $_->{name}; } @output), "\n";
365         }
366         elsif(ref($thing) eq "ID3FS::PathElement::Boolean")
367         {
368 #           print "BOOL: $thing->{name}\n";
369             # bool
370 #           print "thing: $thing->{name}: $priorities{$thing->{name}} ";
371             if(@opstack)
372             {
373 #               print("topop: ", $opstack[$#opstack]->{name},
374 #                     ": ", $priorities{$opstack[$#opstack]->{name}}, "\n");
375             }
376             while(@opstack &&
377                   ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
378             {
379 #               print "Pushing ", $opstack[$#opstack]->{name}, " from opstack to output\n";
380                 push(@output, pop(@opstack));
381 #               print "OPSTACK: ", join(', ', map { $_->{name}; } @opstack), "\n";
382 #               print "OUTPUT: ", join(', ', map { $_->{name}; } @output), "\n";
383             }
384 #           print "Pushing $thing->{name} to opstack\n";
385             push(@opstack, $thing);
386 #           print "OPSTACK: ", join(', ', map { $_->{name}; } @opstack), "\n";
387 #           print "OUTPUT: ", join(', ', map { $_->{name}; } @output), "\n";
388         }
389     }
390     while(@opstack)
391     {
392         push(@output, pop(@opstack));
393     }
394 #    print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
395     return @output;
396 }
397
398 sub used_tags
399 {
400     my($self)=@_;
401     print "TAGTREE UNDEF\n" unless(defined($self->{tagtree}));
402     return undef unless(defined($self->{tagtree}));
403     return($self->{tagtree}->used_tags());
404 }
405
406 1;