2 * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "BlockingQuery.h"
23 #include <QCoreApplication>
25 #include <QMutexLocker>
26 #include <QStringList>
30 struct BlockingQuery::Private
34 // QWaitCondition wait;
35 QStringList collectionIds
;
37 QHash
<QString
, DataList
> data
;
38 QHash
<QString
, TrackList
> track
;
39 QHash
<QString
, AlbumList
> album
;
40 QHash
<QString
, ArtistList
> artist
;
41 QHash
<QString
, GenreList
> genre
;
42 QHash
<QString
, ComposerList
> composer
;
43 QHash
<QString
, YearList
> year
;
44 QHash
<QString
, QStringList
> custom
;
48 BlockingQuery::BlockingQuery( QueryMaker
*qm
)
56 BlockingQuery::~BlockingQuery()
63 BlockingQuery::startQuery()
66 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::DataList
) ), SLOT( result( QString
, Meta::DataList
) ), Qt::DirectConnection
);
67 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::TrackList
) ), SLOT( result( QString
, Meta::TrackList
) ), Qt::DirectConnection
);
68 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::AlbumList
) ), SLOT( result( QString
, Meta::AlbumList
) ), Qt::DirectConnection
);
69 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::ArtistList
) ), SLOT( result( QString
, Meta::ArtistList
) ), Qt::DirectConnection
);
70 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::ComposerList
) ), SLOT( result( QString
, Meta::ComposerList
) ), Qt::DirectConnection
);
71 connect( d
->qm
, SIGNAL( newResultReady( QString
, Meta::YearList
) ), SLOT( result( QString
, Meta::YearList
) ), Qt::DirectConnection
);
72 connect( d
->qm
, SIGNAL( newResultReady( QString
, QStringList
) ), SLOT( result( QString
, QStringList
) ), Qt::DirectConnection
);
73 connect( d
->qm
, SIGNAL( queryDone() ), SLOT( queryDone() ), Qt::DirectConnection
);
80 QCoreApplication::instance()->processEvents( QEventLoop::AllEvents
);
87 BlockingQuery::collectionIds()
89 QMutexLocker
locker( &d
->dataMutex
);
90 return d
->collectionIds
;
94 BlockingQuery::data( const QString
&id
)
96 QMutexLocker
locker( &d
->dataMutex
);
97 return d
->data
.value( id
);
101 BlockingQuery::tracks( const QString
&id
)
103 QMutexLocker
locker( &d
->dataMutex
);
104 return d
->track
.value( id
);
108 BlockingQuery::albums( const QString
&id
)
110 QMutexLocker
locker( &d
->dataMutex
);
111 return d
->album
.value( id
);
115 BlockingQuery::artists( const QString
&id
)
117 QMutexLocker
locker( &d
->dataMutex
);
118 return d
->artist
.value( id
);
122 BlockingQuery::genres( const QString
&id
)
124 QMutexLocker
locker( &d
->dataMutex
);
125 return d
->genre
.value( id
);
129 BlockingQuery::composers( const QString
&id
)
131 QMutexLocker
locker( &d
->dataMutex
);
132 return d
->composer
.value( id
);
136 BlockingQuery::years( const QString
&id
)
138 QMutexLocker
locker( &d
->dataMutex
);
139 return d
->year
.value( id
);
143 BlockingQuery::customData( const QString
&id
)
145 QMutexLocker
locker( &d
->dataMutex
);
146 return d
->custom
.value( id
);
149 QHash
<QString
, DataList
>
150 BlockingQuery::data()
152 QMutexLocker
locker( &d
->dataMutex
);
156 QHash
<QString
, TrackList
>
157 BlockingQuery::tracks()
159 QMutexLocker
locker( &d
->dataMutex
);
163 QHash
<QString
, AlbumList
>
164 BlockingQuery::albums()
166 QMutexLocker
locker( &d
->dataMutex
);
170 QHash
<QString
, ArtistList
>
171 BlockingQuery::artists()
173 QMutexLocker
locker( &d
->dataMutex
);
177 QHash
<QString
, GenreList
>
178 BlockingQuery::genres()
180 QMutexLocker
locker( &d
->dataMutex
);
184 QHash
<QString
, ComposerList
>
185 BlockingQuery::composers()
187 QMutexLocker
locker( &d
->dataMutex
);
191 QHash
<QString
, YearList
>
192 BlockingQuery::years()
194 QMutexLocker
locker( &d
->dataMutex
);
198 QHash
<QString
, QStringList
>
199 BlockingQuery::customData()
201 QMutexLocker
locker( &d
->dataMutex
);
206 BlockingQuery::queryDone()
209 //lock the mutex so we can be sure that we wait in run()
217 BlockingQuery::result( const QString
&collectionId
, Meta::DataList data
)
220 d
->collectionIds
.append( collectionId
);
221 d
->data
.insert( collectionId
, data
);
222 d
->dataMutex
.unlock();
226 BlockingQuery::result( const QString
&collectionId
, Meta::TrackList tracks
)
229 d
->collectionIds
.append( collectionId
);
230 d
->track
.insert( collectionId
, tracks
);
231 d
->dataMutex
.unlock();
235 BlockingQuery::result( const QString
&collectionId
, Meta::ArtistList artists
)
238 d
->collectionIds
.append( collectionId
);
239 d
->artist
.insert( collectionId
, artists
);
240 d
->dataMutex
.unlock();
244 BlockingQuery::result( const QString
&collectionId
, Meta::AlbumList albums
)
247 d
->collectionIds
.append( collectionId
);
248 d
->album
.insert( collectionId
, albums
);
249 d
->dataMutex
.unlock();
253 BlockingQuery::result( const QString
&collectionId
, Meta::GenreList genres
)
256 d
->collectionIds
.append( collectionId
);
257 d
->genre
.insert( collectionId
, genres
);
258 d
->dataMutex
.unlock();
262 BlockingQuery::result( const QString
&collectionId
, Meta::ComposerList composers
)
265 d
->collectionIds
.append( collectionId
);
266 d
->composer
.insert( collectionId
, composers
);
267 d
->dataMutex
.unlock();
271 BlockingQuery::result( const QString
&collectionId
, Meta::YearList years
)
274 d
->collectionIds
.append( collectionId
);
275 d
->year
.insert( collectionId
, years
);
276 d
->dataMutex
.unlock();
280 BlockingQuery::result( const QString
&collectionId
, const QStringList
&list
)
283 d
->collectionIds
.append( collectionId
);
284 d
->custom
.insert( collectionId
, list
);
285 d
->dataMutex
.unlock();
288 #include "BlockingQuery.moc"