1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2012 Leo Franchi <lfranchi@kde.org>
5
* Tomahawk is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Tomahawk is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
19
#include "IdThreadWorker.h"
24
#include "DatabaseImpl.h"
27
#define ID_THREAD_DEBUG 0
29
#include <QtCore/qfutureinterface.h>
31
using namespace Tomahawk;
41
static QWaitCondition s_waitCond;
42
static QMutex s_mutex;
46
QFutureInterface<unsigned int> promise;
53
// TODO Q_GLOBAL_STATIC
54
QQueue< QueueItem* > IdThreadWorker::s_workQueue = QQueue< QueueItem* >();
56
IdThreadWorker::IdThreadWorker( Database* db )
64
IdThreadWorker::~IdThreadWorker()
71
IdThreadWorker::stop()
74
QMutexLocker l( &s_mutex );
83
internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate, QueryType type )
85
QueueItem* item = new QueueItem;
86
item->artist = artist;
89
item->create = autoCreate;
90
item->promise.reportStarted();
97
IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
99
QueueItem* item = internalGet( artist, album_ptr(), autoCreate, ArtistType );
100
artist->setIdFuture( item->promise.future() );
103
qDebug() << "QUEUEING ARTIST:" << artist->name();
107
s_workQueue.enqueue( item );
109
s_waitCond.wakeOne();
111
qDebug() << "DONE WOKE UP THREAD:" << artist->name();
117
IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate )
119
QueueItem* item = internalGet( artist_ptr(), album, autoCreate, AlbumType );
120
album->setIdFuture( item->promise.future() );
123
qDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name();
126
s_workQueue.enqueue( item );
128
s_waitCond.wakeOne();
130
qDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name();
136
IdThreadWorker::run()
138
m_impl = Database::instance()->impl();
144
qDebug() << "IdWorkerThread waiting on condition...";
146
s_waitCond.wait( &s_mutex );
148
qDebug() << "IdWorkerThread WOKEN UP";
151
while ( !s_workQueue.isEmpty() )
153
QueueItem* item = s_workQueue.dequeue();
157
qDebug() << "WITH CONTENTS:" << (item->type == ArtistType ? item->artist->name() : item->album->artist()->name() + " _ " + item->album->name());
159
if ( item->type == ArtistType )
161
unsigned int id = m_impl->artistId( item->artist->name(), item->create );
162
item->promise.reportFinished( &id );
167
else if ( item->type == AlbumType )
169
unsigned int artistId = m_impl->artistId( item->album->artist()->name(), item->create );
170
unsigned int albumId = m_impl->albumId( artistId, item->album->name(), item->create );
171
item->promise.reportFinished( &albumId );