1 /****************************************************************************************
2 * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free Software *
6 * Foundation; either version 2 of the License, or (at your option) any later *
7 * version. *
8 * *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
11 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU General Public License along with *
14 * this program. If not, see <http://www.gnu.org/licenses/>. *
15 ****************************************************************************************/
16
17 #include "MemoryCustomValue.h"
18
19 #include "core/meta/Meta.h"
20 #include "core/meta/Statistics.h"
21
22 #include <QSet>
23
24 class SumReturnFunction : public CustomReturnFunction
25 {
26 public:
SumReturnFunction(CustomReturnValue * rv)27 SumReturnFunction( CustomReturnValue * rv ) : returnValue( rv ) {}
~SumReturnFunction()28 ~SumReturnFunction() override { delete returnValue; }
29
value(const Meta::TrackList & tracks) const30 QString value( const Meta::TrackList &tracks ) const override
31 {
32 if( !returnValue )
33 return QString::number( 0 );
34
35 double sum = 0.0;
36 foreach( const Meta::TrackPtr &track, tracks )
37 sum += returnValue->value( track ).toDouble();
38
39 return QString::number( sum );
40 }
41
42 private:
43 CustomReturnValue *returnValue;
44 };
45
46 class MinimumReturnFunction : public CustomReturnFunction
47 {
48 public:
MinimumReturnFunction(CustomReturnValue * rv)49 MinimumReturnFunction( CustomReturnValue * rv ) : returnValue( rv ) {}
~MinimumReturnFunction()50 ~MinimumReturnFunction() override { delete returnValue; }
51
value(const Meta::TrackList & tracks) const52 QString value( const Meta::TrackList &tracks ) const override
53 {
54 if( tracks.empty() || !returnValue )
55 return QString::number( 0 );
56
57 double min = returnValue->value( tracks.first() ).toDouble();
58 foreach( const Meta::TrackPtr &track, tracks )
59 min = qMin( min, returnValue->value( track ).toDouble() );
60
61 return QString::number( min );
62 }
63
64 private:
65 CustomReturnValue *returnValue;
66 };
67
68 class MaximumReturnFunction : public CustomReturnFunction
69 {
70 public:
MaximumReturnFunction(CustomReturnValue * rv)71 MaximumReturnFunction( CustomReturnValue * rv ) : returnValue( rv ) {}
~MaximumReturnFunction()72 ~MaximumReturnFunction() override { delete returnValue; }
73
value(const Meta::TrackList & tracks) const74 QString value( const Meta::TrackList &tracks ) const override
75 {
76 if( tracks.empty() || !returnValue )
77 return QString::number( 0 );
78
79 double max = returnValue->value( tracks.first() ).toDouble();
80 foreach( const Meta::TrackPtr &track, tracks )
81 max = qMax( max, returnValue->value( track ).toDouble() );
82
83 return QString::number( max );
84 }
85
86 private:
87 CustomReturnValue *returnValue;
88 };
89
90 CustomReturnFunction*
returnFunction(Collections::QueryMaker::ReturnFunction function,qint64 value)91 CustomValueFactory::returnFunction( Collections::QueryMaker::ReturnFunction function, qint64 value )
92 {
93 switch( function )
94 {
95 case Collections::QueryMaker::Count:
96 {
97 switch( value )
98 {
99 case Meta::valUrl:
100 case Meta::valTitle:
101 {
102 return new TrackCounter();
103 }
104 case Meta::valArtist:
105 {
106 return new ArtistCounter();
107 }
108 case Meta::valAlbum:
109 {
110 return new AlbumCounter();
111 }
112 case Meta::valGenre:
113 {
114 return new GenreCounter();
115 }
116 case Meta::valComposer:
117 {
118 return new ComposerCounter();
119 }
120 case Meta::valYear:
121 {
122 return new YearCounter();
123 }
124 default:
125 return 0;
126 }
127 }
128 case Collections::QueryMaker::Sum:
129 {
130 CustomReturnValue *crv = CustomValueFactory::returnValue( value );
131 return crv ? new SumReturnFunction( crv ) : 0;
132 }
133 case Collections::QueryMaker::Min:
134 {
135 CustomReturnValue *crv = CustomValueFactory::returnValue( value );
136 return crv ? new MinimumReturnFunction( crv ) : 0;
137 }
138 case Collections::QueryMaker::Max:
139 {
140 CustomReturnValue *crv = CustomValueFactory::returnValue( value );
141 return crv ? new MaximumReturnFunction( crv ) : 0;
142 }
143 default:
144 return 0;
145 }
146 }
147
CustomReturnFunction()148 CustomReturnFunction::CustomReturnFunction()
149 {
150 }
151
~CustomReturnFunction()152 CustomReturnFunction::~CustomReturnFunction()
153 {
154 }
155
CustomReturnValue()156 CustomReturnValue::CustomReturnValue()
157 {
158 }
159
~CustomReturnValue()160 CustomReturnValue::~CustomReturnValue()
161 {
162 }
163
TrackCounter()164 TrackCounter::TrackCounter()
165 {
166 }
167
~TrackCounter()168 TrackCounter::~TrackCounter()
169 {
170 }
171
172 QString
value(const Meta::TrackList & tracks) const173 TrackCounter::value( const Meta::TrackList &tracks ) const
174 {
175 return QString::number( tracks.count() );
176 }
177
ArtistCounter()178 ArtistCounter::ArtistCounter()
179 {
180 }
181
~ArtistCounter()182 ArtistCounter::~ArtistCounter()
183 {
184 }
185
186 QString
value(const Meta::TrackList & tracks) const187 ArtistCounter::value( const Meta::TrackList &tracks ) const
188 {
189 QSet<Meta::ArtistPtr> artists;
190 foreach( const Meta::TrackPtr &track, tracks )
191 {
192 if( track->artist() )
193 artists.insert( track->artist() );
194 }
195 return QString::number( artists.count() );
196 }
197
GenreCounter()198 GenreCounter::GenreCounter()
199 {
200 }
201
~GenreCounter()202 GenreCounter::~GenreCounter()
203 {
204 }
205
206 QString
value(const Meta::TrackList & tracks) const207 GenreCounter::value( const Meta::TrackList &tracks ) const
208 {
209 QSet<Meta::GenrePtr> genres;
210 foreach( const Meta::TrackPtr &track, tracks )
211 {
212 if( track->genre() )
213 genres.insert( track->genre() );
214 }
215 return QString::number( genres.count() );
216 }
217
AlbumCounter()218 AlbumCounter::AlbumCounter()
219 {
220 }
221
~AlbumCounter()222 AlbumCounter::~AlbumCounter()
223 {
224 }
225
226 QString
value(const Meta::TrackList & tracks) const227 AlbumCounter::value( const Meta::TrackList &tracks ) const
228 {
229 QSet<Meta::AlbumPtr> albums;
230 foreach( const Meta::TrackPtr &track, tracks )
231 {
232 if( track->album() )
233 albums.insert( track->album() );
234 }
235 return QString::number( albums.count() );
236 }
237
ComposerCounter()238 ComposerCounter::ComposerCounter()
239 {
240 }
241
~ComposerCounter()242 ComposerCounter::~ComposerCounter()
243 {
244 }
245
246 QString
value(const Meta::TrackList & tracks) const247 ComposerCounter::value( const Meta::TrackList &tracks ) const
248 {
249 QSet<Meta::ComposerPtr> composers;
250 foreach( const Meta::TrackPtr &track, tracks )
251 {
252 if( track->composer() )
253 composers.insert( track->composer() );
254 }
255 return QString::number( composers.count() );
256 }
257
YearCounter()258 YearCounter::YearCounter()
259 {
260 }
261
~YearCounter()262 YearCounter::~YearCounter()
263 {
264 }
265
266 QString
value(const Meta::TrackList & tracks) const267 YearCounter::value( const Meta::TrackList &tracks ) const
268 {
269 QSet<Meta::YearPtr> years;
270 foreach( const Meta::TrackPtr &track, tracks )
271 {
272 if( track->year() )
273 years.insert( track->year() );
274 }
275 return QString::number( years.count() );
276 }
277
278
279 //CustomReturnValues
280
TitleReturnValue()281 TitleReturnValue::TitleReturnValue()
282 {
283 }
284
~TitleReturnValue()285 TitleReturnValue::~TitleReturnValue()
286 {
287 }
288
289 QString
value(const Meta::TrackPtr & track) const290 TitleReturnValue::value( const Meta::TrackPtr &track ) const
291 {
292 return track->name();
293 }
294
UrlReturnValue()295 UrlReturnValue::UrlReturnValue()
296 {
297 }
298
~UrlReturnValue()299 UrlReturnValue::~UrlReturnValue()
300 {
301 }
302
303 QString
value(const Meta::TrackPtr & track) const304 UrlReturnValue::value( const Meta::TrackPtr &track ) const
305 {
306 return track->playableUrl().url();
307 }
308
309 class ArtistReturnValue : public CustomReturnValue
310 {
311 public:
ArtistReturnValue()312 ArtistReturnValue() {}
~ArtistReturnValue()313 ~ArtistReturnValue() override {}
value(const Meta::TrackPtr & track) const314 QString value( const Meta::TrackPtr &track ) const override { return track->artist() ? track->artist()->name() : QString(); }
315 };
316
317 class AlbumReturnValue : public CustomReturnValue
318 {
319 public:
AlbumReturnValue()320 AlbumReturnValue() {}
~AlbumReturnValue()321 ~AlbumReturnValue() override {}
value(const Meta::TrackPtr & track) const322 QString value( const Meta::TrackPtr &track ) const override { return track->album() ? track->album()->name() : QString(); }
323 };
324
325 class ComposerReturnValue : public CustomReturnValue
326 {
327 public:
ComposerReturnValue()328 ComposerReturnValue() {}
~ComposerReturnValue()329 ~ComposerReturnValue() override {}
value(const Meta::TrackPtr & track) const330 QString value( const Meta::TrackPtr &track ) const override { return track->composer() ? track->composer()->name() : QString(); }
331 };
332
333 class GenreReturnValue : public CustomReturnValue
334 {
335 public:
GenreReturnValue()336 GenreReturnValue() {}
~GenreReturnValue()337 ~GenreReturnValue() override {}
value(const Meta::TrackPtr & track) const338 QString value( const Meta::TrackPtr &track ) const override { return track->genre() ? track->genre()->name() : QString(); }
339 };
340
341 class YearReturnValue : public CustomReturnValue
342 {
343 public:
YearReturnValue()344 YearReturnValue() {}
~YearReturnValue()345 ~YearReturnValue() override {}
value(const Meta::TrackPtr & track) const346 QString value( const Meta::TrackPtr &track ) const override { return track->year() ? track->year()->name() : QString(); }
347 };
348
349 class CommentReturnValue : public CustomReturnValue
350 {
351 public:
CommentReturnValue()352 CommentReturnValue() {}
~CommentReturnValue()353 ~CommentReturnValue() override {}
value(const Meta::TrackPtr & track) const354 QString value( const Meta::TrackPtr &track ) const override { return track->comment(); }
355 };
356
357 class TrackNrReturnValue : public CustomReturnValue
358 {
359 public:
TrackNrReturnValue()360 TrackNrReturnValue() {}
~TrackNrReturnValue()361 ~TrackNrReturnValue() override {}
value(const Meta::TrackPtr & track) const362 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->trackNumber() ); }
363 };
364
365 class DiscNrReturnValue : public CustomReturnValue
366 {
367 public:
DiscNrReturnValue()368 DiscNrReturnValue() {}
~DiscNrReturnValue()369 ~DiscNrReturnValue() override {}
value(const Meta::TrackPtr & track) const370 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->discNumber() ); }
371 };
372
373 class ScoreReturnValue : public CustomReturnValue
374 {
375 public:
ScoreReturnValue()376 ScoreReturnValue() {}
~ScoreReturnValue()377 ~ScoreReturnValue() override {}
value(const Meta::TrackPtr & track) const378 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->statistics()->score() ); }
379 };
380
381 class RatingReturnValue : public CustomReturnValue
382 {
383 public:
RatingReturnValue()384 RatingReturnValue() {}
~RatingReturnValue()385 ~RatingReturnValue() override {}
value(const Meta::TrackPtr & track) const386 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->statistics()->rating() ); }
387 };
388
389 class PlaycountReturnValue : public CustomReturnValue
390 {
391 public:
PlaycountReturnValue()392 PlaycountReturnValue() {}
~PlaycountReturnValue()393 ~PlaycountReturnValue() override {}
value(const Meta::TrackPtr & track) const394 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->statistics()->playCount() ); }
395 };
396
397 class LengthReturnValue : public CustomReturnValue
398 {
399 public:
LengthReturnValue()400 LengthReturnValue() {}
~LengthReturnValue()401 ~LengthReturnValue() override {}
value(const Meta::TrackPtr & track) const402 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->length() / 1000 ); }
403 };
404
405 class BitrateReturnValue : public CustomReturnValue
406 {
407 public:
BitrateReturnValue()408 BitrateReturnValue() {}
~BitrateReturnValue()409 ~BitrateReturnValue() override {}
value(const Meta::TrackPtr & track) const410 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->bitrate() ); }
411 };
412
413 class FileSizeReturnValue : public CustomReturnValue
414 {
415 public:
FileSizeReturnValue()416 FileSizeReturnValue() {}
~FileSizeReturnValue()417 ~FileSizeReturnValue() override {}
value(const Meta::TrackPtr & track) const418 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->filesize() ); }
419 };
420
421 class SampleRateReturnValue : public CustomReturnValue
422 {
423 public:
SampleRateReturnValue()424 SampleRateReturnValue() {}
~SampleRateReturnValue()425 ~SampleRateReturnValue() override {}
value(const Meta::TrackPtr & track) const426 QString value( const Meta::TrackPtr &track ) const override { return QString::number( track->sampleRate() ); }
427 };
428
429 CustomReturnValue*
returnValue(qint64 value)430 CustomValueFactory::returnValue( qint64 value )
431 {
432 switch( value )
433 {
434 case Meta::valTitle:
435 {
436 return new TitleReturnValue();
437 }
438 case Meta::valUrl:
439 {
440 return new UrlReturnValue();
441 }
442 case Meta::valArtist:
443 {
444 return new ArtistReturnValue();
445 }
446 case Meta::valAlbum:
447 {
448 return new AlbumReturnValue();
449 }
450 case Meta::valGenre:
451 {
452 return new GenreReturnValue();
453 }
454 case Meta::valComposer:
455 {
456 return new ComposerReturnValue();
457 }
458 case Meta::valYear:
459 {
460 return new YearReturnValue();
461 }
462 case Meta::valComment:
463 {
464 return new CommentReturnValue();
465 }
466 case Meta::valTrackNr:
467 {
468 return new TrackNrReturnValue();
469 }
470 case Meta::valDiscNr:
471 {
472 return new DiscNrReturnValue();
473 }
474 case Meta::valScore:
475 {
476 return new ScoreReturnValue();
477 }
478 case Meta::valRating:
479 {
480 return new RatingReturnValue();
481 }
482 case Meta::valPlaycount:
483 {
484 return new PlaycountReturnValue();
485 }
486 case Meta::valLength:
487 {
488 return new LengthReturnValue();
489 }
490 case Meta::valBitrate:
491 {
492 return new BitrateReturnValue();
493 }
494 case Meta::valFilesize:
495 {
496 return new FileSizeReturnValue();
497 }
498 case Meta::valSamplerate:
499 {
500 return new SampleRateReturnValue();
501 }
502 default:
503 return 0;
504 }
505 }
506