36 #include <Soprano/Model>
40 #include <QtCore/QThreadPool>
41 #include <QtCore/QMutexLocker>
42 #include <QtDBus/QDBusConnection>
47 m_isSparqlQueryFolder( false ),
50 m_currentSearchRunnable( 0 ),
51 m_currentCountQueryRunnable( 0 )
60 m_isSparqlQueryFolder( true ),
61 m_sparqlQuery( query ),
62 m_requestProperties( requestProps ),
64 m_currentSearchRunnable( 0 ),
65 m_currentCountQueryRunnable( 0 )
71 using namespace Nepomuk2::Query;
72 using namespace Nepomuk2;
78 initWatcherForGroupTerms( watcher, term.
toAndTerm(), emptyProperty );
80 initWatcherForGroupTerms( watcher, term.
toOrTerm(), emptyProperty );
88 if( prop.isEmpty() ) {
98 QList<Term> terms = groupTerm.
subTerms();
99 foreach(
const Term& term, terms) {
100 initWatcherForTerm( watcher, term, emptyProperty );
107 bool emptyProperty =
false;
108 initWatcherForTerm( watcher, query.
term(), emptyProperty );
115 void Nepomuk2::Query::Folder::init()
118 m_initialListingDone =
false;
119 m_storageChanged =
false;
121 m_updateTimer.setSingleShot(
true );
122 m_updateTimer.setInterval( 2000 );
125 initWatcherForQuery( watcher, m_query );
128 this, SLOT(slotStorageChanged()) );
130 this, SLOT(slotStorageChanged()) );
132 this, SLOT(slotStorageChanged()) );
133 connect( watcher, SIGNAL(resourceRemoved(QUrl,QList<QUrl>)),
134 this, SLOT(slotStorageChanged()) );
136 this, SLOT(slotStorageChanged()) );
138 this, SLOT(slotStorageChanged()) );
141 connect( &m_updateTimer, SIGNAL( timeout() ),
142 this, SLOT( slotUpdateTimeout() ) );
148 if( m_currentSearchRunnable ){
149 m_currentSearchRunnable->cancel();
151 m_currentSearchRunnable = 0;
153 if( m_currentCountQueryRunnable ){
154 m_currentCountQueryRunnable->cancel();
155 m_currentCountQueryRunnable = 0;
159 while ( !m_connections.isEmpty() )
160 delete m_connections.first();
164 void Nepomuk2::Query::Folder::update()
166 if ( !m_currentSearchRunnable ) {
167 m_currentSearchRunnable =
new SearchRunnable( m_model, sparqlQuery(), requestPropertyMap() );
171 connect( m_currentSearchRunnable, SIGNAL(listingFinished()),
172 this, SLOT(listingFinished()), Qt::QueuedConnection );
178 if ( !m_initialListingDone &&
179 !m_isSparqlQueryFolder &&
180 m_query.limit() == 0 ) {
182 connect( m_currentCountQueryRunnable, SIGNAL(countQueryFinished(
int)),
183 this, SLOT(countQueryFinished(
int)), Qt::QueuedConnection );
193 return m_results.values();
199 return m_initialListingDone;
205 if ( !m_isSparqlQueryFolder )
206 return m_query.toSparqlQuery();
208 return m_sparqlQuery;
214 if ( !m_isSparqlQueryFolder )
215 return m_query.requestPropertyMap();
217 return m_requestProperties;
221 void Nepomuk2::Query::Folder::addResult(
const Result& result)
224 m_newResults.insert( resUri, result );
226 if ( !m_results.contains( resUri ) ) {
227 emit newEntries( QList<Result>() << result );
232 void Nepomuk2::Query::Folder::listingFinished()
234 m_currentSearchRunnable = 0;
237 QList<Result> removedResults;
240 foreach(
const Result& result, m_results ) {
241 if ( !m_newResults.contains( result.
resource().
uri() ) ) {
242 removedResults << result;
243 emit entriesRemoved( QList<QUrl>() << KUrl(result.resource().uri()).url() );
248 if( !removedResults.isEmpty() ) {
249 emit entriesRemoved( removedResults );
253 m_results = m_newResults;
254 m_newResults.clear();
256 if ( !m_initialListingDone ) {
257 kDebug() <<
"Listing done. Total:" << m_results.count();
258 m_initialListingDone =
true;
259 emit finishedListing();
265 QMetaObject::invokeMethod( &m_updateTimer,
"start", Qt::QueuedConnection );
269 void Nepomuk2::Query::Folder::slotStorageChanged()
271 m_updateTimer.start();
272 m_storageChanged =
true;
276 void Nepomuk2::Query::Folder::slotUpdateTimeout()
278 if ( m_storageChanged && !m_currentSearchRunnable ) {
279 m_storageChanged =
false;
285 void Nepomuk2::Query::Folder::countQueryFinished(
int count )
287 m_currentCountQueryRunnable = 0;
289 m_resultCount = count;
290 kDebug() << m_resultCount;
292 emit resultCount( m_resultCount );
298 Q_ASSERT( conn != 0 );
299 Q_ASSERT( !m_connections.contains( conn ) );
301 m_connections.append( conn );
307 Q_ASSERT( conn != 0 );
308 Q_ASSERT( m_connections.contains( conn ) );
310 m_connections.removeAll( conn );
312 if ( m_connections.isEmpty() ) {
313 kDebug() <<
"Folder unused. Deleting.";
314 emit aboutToBeDeleted(
this );
322 return m_connections;
332 #include "folder.moc"
bool isOptionalTerm() const
bool isComparisonTerm() const
OptionalTerm toOptionalTerm() const
Interpret this term as a OptionalTerm.
void setProperties(const QList< Types::Property > &properties_)
Set the properties to be watched.
QUrl uri() const
The URI of the resource.
The base class for all term types.
Abstract base class for AndTerm and OrTerm which maintains a list of sub-terms.
void addProperty(const Types::Property &property)
Add a property to be watched.
Term term() const
The root term of the query.
Term subTerm() const
The sub term to match against.
QString sparqlQuery() const
QList< Term > subTerms() const
The sub terms that are combined in this group.
QList< Result > entries() const
uint qHash(const Nepomuk2::Query::Query &)
QList< FolderConnection * > openConnections() const
bool isNegationTerm() const
ComparisonTerm toComparisonTerm() const
Interpret this term as a ComparisonTerm.
Folder(Soprano::Model *model, const Query &query, QObject *parent=0)
QHash< QString, Nepomuk2::Types::Property > RequestPropertyMap
Convinience definition for request property mappings as used in QueryServiceClient::sparqlQuery() and...
bool initialListingDone() const
bool start()
Start the signalling of changes.
OrTerm toOrTerm() const
Interpret this term as a OrTerm.
A Class is a resource of type rdf:Class.
A property is a resource of type rdf:Property which relates a domain with a range.
Selectively monitor the nepomuk repository for changes.
AndTerm toAndTerm() const
Interpret this term as a AndTerm.
Resource is the central object type in Nepomuk.
Resource resource() const
The result resource.
QUrl uri() const
The URI of the resource, uniquely identifying it.
static QThreadPool * searchThreadPool()
RequestPropertyMap requestPropertyMap() const
Types::Property property() const
A property used for ComparisonTerm Terms.
NegationTerm toNegationTerm() const
Interpret this term as a NegationTerm.