Build with non-standard boost locations.
[kdepim.git] / nepomuk_email_feeder / messagesearch.cpp
blob64d25ec22594b1947738c226f0c7062d1ec31d97
1 /*
2 Copyright (c) 2009 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 "messagesearch.h"
22 #include <akonadi/item.h>
23 #include <akonadi/linkjob.h>
25 #include <Nepomuk/ResourceManager>
26 #include <KDebug>
27 #include <KUrl>
28 #include <Soprano/QueryResultIterator>
29 #include <Soprano/StatementIterator>
30 #include <Soprano/Model>
32 MessageSearch::MessageSearch(const QString& query, const Akonadi::Collection& destination, QObject* parent) :
33 Task(parent),
34 m_query( query ),
35 m_destination( destination ),
36 m_refCount( 0 )
38 kDebug() << query << destination;
39 ref();
40 foreach ( const QByteArray &keyId, listCryptoContainers() )
41 searchInContainer( keyId );
42 deref();
45 void MessageSearch::searchInContainer(const QByteArray& keyId)
47 kDebug() << keyId;
48 if ( !mountCryptoContainer( keyId ) ) {
49 kDebug() << "Unable to mount crypto container for key" << keyId;
50 return;
53 Q_ASSERT( !hasActiveCryptoModel() );
54 Soprano::Model* model = cryptoModel( keyId );
56 if ( model ) {
57 #if 0 // Temporary debugging code, will need this again tomorrow
59 Soprano::StatementIterator it = model->listStatements();
60 foreach ( Soprano::Statement s, it.allStatements() )
61 kDebug() << "###" << s.subject().toString() << s.predicate().toString() << s.object().toString();
63 #endif
65 Soprano::QueryResultIterator it = model->executeQuery( m_query, Soprano::Query::QUERY_LANGUAGE_SPARQL );
66 kDebug() << model->lastError().message();
67 Akonadi::Item::List result;
68 while ( it.next() ) {
69 kDebug() << "Found:" << it.binding( 0 ).toString();
70 Akonadi::Item item = Akonadi::Item::fromUrl( KUrl( it.binding( 0 ).toString() ) );
71 if ( item.isValid() )
72 result.append( item );
74 it.close();
76 ref();
77 Akonadi::LinkJob *linkJob = new Akonadi::LinkJob( m_destination, result, this );
78 connect( linkJob, SIGNAL(result(KJob*)), SLOT(linkResult(KJob*)) );
81 resetModel();
82 unmountCryptoContainer();
83 Q_ASSERT( !hasActiveCryptoModel() );
86 void MessageSearch::linkResult(KJob* job)
88 if ( job->error() )
89 kWarning() << job->errorText();
90 deref();
93 void MessageSearch::ref()
95 ++m_refCount;
98 void MessageSearch::deref()
100 --m_refCount;
101 if ( m_refCount <= 0 )
102 deleteLater();
105 #include "messagesearch.moc"