Make the boss happy.
[kdepim.git] / messagecore / asyncnepomukresourceretriever.cpp
blob90b8fd2f4033e6ce64f64ab4ec97d7eed1d4a42f
1 /*
2 Copyright (c) 2011 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "asyncnepomukresourceretriever.h"
22 #include <QtCore/QMutex>
23 #include <QtCore/QRunnable>
24 #include <QtCore/QThreadPool>
26 #include <KDebug>
27 #include <Nepomuk/Resource>
28 #include <Nepomuk/Variant>
30 using namespace MessageCore;
32 namespace MessageCore {
34 class NepomukResourceRetrieverRunnable : public QRunnable
36 public:
37 NepomukResourceRetrieverRunnable( const QUrl& url, const QVector<QUrl> &props, QObject *retriever ) :
38 m_url( url ),
39 m_props( props ),
40 m_retriever( retriever )
43 void run()
45 Nepomuk::Resource resource( m_url );
46 foreach ( const QUrl &prop, m_props )
47 resource.property( prop ); // loads and caches this property
48 QMetaObject::invokeMethod( m_retriever, "resourceRetrievalDone", Qt::QueuedConnection,
49 Q_ARG(QUrl, m_url), Q_ARG(Nepomuk::Resource, resource) );
52 private:
53 QUrl m_url;
54 QVector<QUrl> m_props;
55 QObject* m_retriever;
58 class AsyncNepomukResourceRetrieverPrivate
60 public:
61 AsyncNepomukResourceRetrieverPrivate( AsyncNepomukResourceRetriever *parent ) : m_parent( parent ), m_running( false )
63 m_nepomukPool.setMaxThreadCount( 1 );
64 qRegisterMetaType<Nepomuk::Resource>();
67 void createRunnable()
69 Q_ASSERT( !m_pendingRequests.isEmpty() );
70 Q_ASSERT( !m_running );
71 m_running = true;
72 const QUrl url = m_pendingRequests.last();
73 m_nepomukPool.start( new NepomukResourceRetrieverRunnable( url, m_requestedProperties.value( url ), m_parent ) );
76 void removeRequest( const QUrl &url )
78 const int index = m_pendingRequests.lastIndexOf( url );
79 if ( index >= 0 )
80 m_pendingRequests.remove( index );
81 m_requestedProperties.remove( url );
84 void resourceRetrievalDone( const QUrl &url, const Nepomuk::Resource &res )
86 QMutexLocker locker( &m_mutex );
87 m_running = false;
88 removeRequest( url );
89 if ( !m_pendingRequests.isEmpty() )
90 createRunnable();
91 locker.unlock();
92 m_parent->resourceAvailable( url, res );
95 AsyncNepomukResourceRetriever *m_parent;
96 QThreadPool m_nepomukPool;
97 QVector<QUrl> m_pendingRequests;
98 QHash<QUrl, QVector<QUrl> > m_requestedProperties;
99 QMutex m_mutex;
100 bool m_running;
105 AsyncNepomukResourceRetriever::AsyncNepomukResourceRetriever(QObject* parent) :
106 QObject( parent ),
107 d( new AsyncNepomukResourceRetrieverPrivate( this ) )
111 void AsyncNepomukResourceRetriever::requestResource(const QUrl& url, const QVector<QUrl> &properties)
113 QMutexLocker locker( &d->m_mutex );
114 if ( d->m_pendingRequests.contains( url ) )
115 return;
116 d->m_pendingRequests.push_back( url );
117 d->m_requestedProperties.insert( url, properties );
118 if ( !d->m_running )
119 d->createRunnable();
122 void AsyncNepomukResourceRetriever::cancelRequest(const QUrl & url)
124 QMutexLocker locker( &d->m_mutex );
125 d->removeRequest( url );
128 void AsyncNepomukResourceRetriever::resourceAvailable(const QUrl& url, const Nepomuk::Resource& resource)
130 emit resourceReceived( url, resource );
134 #include "asyncnepomukresourceretriever.moc"