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 "MemoryQueryMakerHelper.h"
18
19 #include "core-impl/collections/support/MemoryCustomValue.h"
20 #include "core/meta/Meta.h"
21
22 #include <QList>
23 #include <QSet>
24 #include <QStack>
25 #include <QtAlgorithms>
26
27 #include <KRandomSequence>
28 #include <KSortableList>
29
30 using namespace Collections;
31
32 template <class PointerType>
33 QList<PointerType>
orderListByName(const QList<PointerType> & list,bool descendingOrder)34 MemoryQueryMakerHelper::orderListByName( const QList<PointerType> &list, bool descendingOrder )
35 {
36 QList<PointerType> resultList = list;
37 KSortableList<PointerType, QString> sortList;
38 foreach( const PointerType &pointer, list )
39 {
40 sortList.insert( pointer->name(), pointer );
41 }
42 sortList.sort();
43 QList<PointerType> tmpList;
44 typedef KSortableItem<PointerType,QString> SortItem;
45 foreach( const SortItem &item, sortList )
46 {
47 tmpList.append( item.second );
48 }
49 if( descendingOrder )
50 {
51 //KSortableList uses qSort, which orders a list in ascending order
52 resultList = reverse<PointerType>( tmpList );
53 }
54 else
55 {
56 resultList = tmpList;
57 }
58 return resultList;
59 }
60
61 Meta::YearList
orderListByYear(const Meta::YearList & list,bool descendingOrder)62 MemoryQueryMakerHelper::orderListByYear( const Meta::YearList &list, bool descendingOrder )
63 {
64 KSortableList<Meta::YearPtr, double> sortList;
65 foreach( Meta::YearPtr pointer, list )
66 {
67 sortList.insert( pointer->name().toDouble(), pointer );
68 }
69 sortList.sort();
70 QList<Meta::YearPtr> tmpList;
71 typedef KSortableItem<Meta::YearPtr,double> SortItem;
72 foreach( const SortItem &item, sortList )
73 {
74 tmpList.append( item.second );
75 }
76 if( descendingOrder )
77 {
78 //KSortableList uses qSort, which orders a list in ascending order
79 return reverse<Meta::YearPtr>( tmpList );
80 }
81 else
82 {
83 return tmpList;
84 }
85 }
86
87 template<typename T>
88 QList<T>
reverse(const QList<T> & l)89 MemoryQueryMakerHelper::reverse(const QList<T> &l)
90 {
91 QList<T> ret;
92 for (int i=l.size() - 1; i>=0; --i)
93 ret.append(l.at(i));
94 return ret;
95 }
96
97 Meta::TrackList
orderListByString(const Meta::TrackList & tracks,qint64 value,bool orderDescending)98 MemoryQueryMakerHelper::orderListByString( const Meta::TrackList &tracks, qint64 value, bool orderDescending )
99 {
100 Meta::TrackList resultList = tracks;
101 CustomReturnValue *crv = CustomValueFactory::returnValue( value );
102 if( crv )
103 {
104 KSortableList<Meta::TrackPtr, QString> sortList;
105 foreach( const Meta::TrackPtr &pointer, tracks )
106 {
107 sortList.insert( crv->value( pointer ), pointer );
108 }
109 sortList.sort();
110 Meta::TrackList tmpList;
111 typedef KSortableItem<Meta::TrackPtr,QString> SortItem;
112 foreach( const SortItem &item, sortList )
113 {
114 tmpList.append( item.second );
115 }
116 if( orderDescending )
117 {
118 //KSortableList uses qSort, which orders a list in ascending order
119 resultList = reverse<Meta::TrackPtr>( tmpList );
120 }
121 else
122 {
123 resultList = tmpList;
124 }
125 }
126 delete crv;
127 return resultList;
128 }
129
130 Meta::TrackList
orderListByNumber(const Meta::TrackList & tracks,qint64 value,bool orderDescending)131 MemoryQueryMakerHelper::orderListByNumber( const Meta::TrackList &tracks, qint64 value, bool orderDescending )
132 {
133 Meta::TrackList resultList = tracks;
134 CustomReturnValue *crv = CustomValueFactory::returnValue( value );
135 if( crv )
136 {
137 KSortableList<Meta::TrackPtr, double> sortList;
138 foreach( const Meta::TrackPtr &pointer, tracks )
139 {
140 sortList.insert( crv->value( pointer ).toDouble(), pointer );
141 }
142 sortList.sort();
143 Meta::TrackList tmpList;
144 typedef KSortableItem<Meta::TrackPtr,double> SortItem;
145 foreach( const SortItem &item, sortList )
146 {
147 tmpList.append( item.second );
148 }
149 if( orderDescending )
150 {
151 //KSortableList uses qSort, which orders a list in ascending order
152 resultList = reverse<Meta::TrackPtr>( tmpList );
153 }
154 else
155 {
156 resultList = tmpList;
157 }
158 }
159 delete crv;
160 return resultList;
161 }
162
163 template QList<Meta::AlbumPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::AlbumPtr > &list, bool );
164 template QList<Meta::ArtistPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::ArtistPtr > &list, bool );
165 template QList<Meta::GenrePtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::GenrePtr > &list, bool );
166 template QList<Meta::ComposerPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::ComposerPtr > &list, bool );
167 template QList<Meta::LabelPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::LabelPtr > &list, bool );
168
169