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
20 #include "asyncnepomukresourceretriever.h"
22 #include <QtCore/QMutex>
23 #include <QtCore/QRunnable>
24 #include <QtCore/QThreadPool>
27 #include <Nepomuk/Resource>
28 #include <Nepomuk/Variant>
30 using namespace MessageCore
;
32 namespace MessageCore
{
34 class NepomukResourceRetrieverRunnable
: public QRunnable
37 NepomukResourceRetrieverRunnable( const QUrl
& url
, const QVector
<QUrl
> &props
, QObject
*retriever
) :
40 m_retriever( retriever
)
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
) );
54 QVector
<QUrl
> m_props
;
58 class AsyncNepomukResourceRetrieverPrivate
61 AsyncNepomukResourceRetrieverPrivate( AsyncNepomukResourceRetriever
*parent
) : m_parent( parent
), m_running( false )
63 m_nepomukPool
.setMaxThreadCount( 1 );
64 qRegisterMetaType
<Nepomuk::Resource
>();
69 Q_ASSERT( !m_pendingRequests
.isEmpty() );
70 Q_ASSERT( !m_running
);
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
);
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
);
89 if ( !m_pendingRequests
.isEmpty() )
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
;
105 AsyncNepomukResourceRetriever::AsyncNepomukResourceRetriever(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
) )
116 d
->m_pendingRequests
.push_back( url
);
117 d
->m_requestedProperties
.insert( url
, properties
);
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"