1 /****************************************************************************************
2 * Copyright (c) 2012 Bart Cerneels <bart.cerneels@kde.org> *
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 "MetaProxyWorker.h"
18
19 #include "core/meta/Meta.h"
20 #include "core-impl/collections/support/CollectionManager.h"
21
22 using namespace MetaProxy;
23
Worker(const QUrl & url,Collections::TrackProvider * provider)24 Worker::Worker( const QUrl &url, Collections::TrackProvider *provider )
25 : QObject()
26 , ThreadWeaver::Job()
27 , m_url( url )
28 , m_provider( provider )
29 , m_stepsDoneReceived( 0 )
30 {
31 }
32
33 void
run(ThreadWeaver::JobPointer self,ThreadWeaver::Thread * thread)34 Worker::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
35 {
36 Q_UNUSED(self);
37 Q_UNUSED(thread);
38 Meta::TrackPtr track;
39
40 if( m_provider )
41 {
42 track = m_provider->trackForUrl( m_url );
43 Q_EMIT finishedLookup( track );
44 return;
45 }
46
47 track = CollectionManager::instance()->trackForUrl( m_url );
48 if( track )
49 {
50 Q_EMIT finishedLookup( track );
51 return;
52 }
53
54 // no TrackProvider has a track for us yet, query new ones that are added.
55 if( !track )
56 {
57 connect( CollectionManager::instance(), &CollectionManager::trackProviderAdded,
58 this, &Worker::slotNewTrackProvider, Qt::DirectConnection ); // we may live in a thread w/out event loop
59 connect( CollectionManager::instance(),&CollectionManager::collectionAdded,
60 this, &Worker::slotNewCollection, Qt::DirectConnection ); // we may live in a thread w/out event loop
61 return;
62 }
63
64 }
65
66 void
defaultBegin(const ThreadWeaver::JobPointer & self,ThreadWeaver::Thread * thread)67 Worker::defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
68 {
69 Q_EMIT started(self);
70 ThreadWeaver::Job::defaultBegin(self, thread);
71 }
72
73 void
defaultEnd(const ThreadWeaver::JobPointer & self,ThreadWeaver::Thread * thread)74 Worker::defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
75 {
76 ThreadWeaver::Job::defaultEnd(self, thread);
77 if (!self->success()) {
78 Q_EMIT failed(self);
79 }
80 Q_EMIT done(self);
81 }
82
83 void
slotNewTrackProvider(Collections::TrackProvider * newTrackProvider)84 Worker::slotNewTrackProvider( Collections::TrackProvider *newTrackProvider )
85 {
86 if( !newTrackProvider )
87 return;
88
89 if( newTrackProvider->possiblyContainsTrack( m_url ) )
90 {
91 Meta::TrackPtr track = newTrackProvider->trackForUrl( m_url );
92 Q_EMIT finishedLookup( track );
93 }
94 }
95
96 void
slotNewCollection(Collections::Collection * newCollection)97 Worker::slotNewCollection( Collections::Collection *newCollection )
98 {
99 slotNewTrackProvider( newCollection );
100 }
101