doc fixes found while translating
[kdepim.git] / nepomuk_email_feeder / shared / nepomukfeederagentbase.h
blobc8b5ee1df6d55d61319202f4c13a284f0e4416fd
1 /*
2 Copyright (c) 2007 Tobias Koenig <tokoe@kde.org>
3 2008 Sebastian Trueg <trueg@kde.org>
4 2009 Volker Krause <vkrause@kde.org>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
22 #ifndef NEPOMUKFEEDERAGENTBASE_H
23 #define NEPOMUKFEEDERAGENTBASE_H
25 #include "resource.h"
26 #include <nie.h>
28 #include <akonadi/agentbase.h>
29 #include <akonadi/collection.h>
30 #include <akonadi/item.h>
31 #include <akonadi/mimetypechecker.h>
33 #include <nepomuk/query.h>
34 #include <nepomuk/resourceterm.h>
35 #include <nepomuk/resourcemanager.h>
37 #include <Soprano/Model>
38 #include <Soprano/NodeIterator>
39 #include <Soprano/Query/QueryLanguage>
40 #include <Soprano/QueryResultIterator>
41 #include <Soprano/Vocabulary/NRL>
42 #define USING_SOPRANO_NRLMODEL_UNSTABLE_API 1
43 #include <Soprano/NRLModel>
45 #include <QStringList>
46 #include <QtCore/QTimer>
47 #include <QtCore/QDateTime>
48 #include <QtCore/QQueue>
50 namespace NepomukFast
52 class PersonContact;
55 namespace Akonadi
57 class Item;
58 class ItemFetchScope;
61 namespace Soprano
63 class NRLModel;
66 namespace Strigi
68 class IndexManager;
71 class KJob;
73 /** Shared base class for all Nepomuk feeders. */
74 class NepomukFeederAgentBase : public Akonadi::AgentBase, public Akonadi::AgentBase::ObserverV2
76 Q_OBJECT
78 public:
79 NepomukFeederAgentBase(const QString& id);
80 ~NepomukFeederAgentBase();
82 /** Remove all references to the given item from Nepomuk. */
83 template <typename T>
84 static void removeEntityFromNepomuk( const T &entity )
86 // find the graph that contains our item and delete the complete graph
87 // FIXME: why isn't that in the ontology?
88 const Nepomuk::Query::ComparisonTerm term( QUrl( QLatin1String( "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#dataGraphFor" ) ),
89 Nepomuk::Query::ResourceTerm( entity.url() ) );
90 const Nepomuk::Query::Query query( term );
91 const QList<Soprano::Node> list = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(
92 query.toSparqlQuery(), Soprano::Query::QueryLanguageSparql ).iterateBindings( 0 ).allNodes();
94 foreach ( const Soprano::Node &node, list )
95 Nepomuk::ResourceManager::instance()->mainModel()->removeContext( node );
98 /** Adds tags to @p resource based on the given string list. */
99 static void tagsFromCategories( NepomukFast::Resource &resource, const QStringList &categories );
101 /** Add a supported mimetype. */
102 void addSupportedMimeType( const QString &mimeType );
104 /** Reimplement to do the actual work. */
105 virtual void updateItem( const Akonadi::Item &item, const QUrl &graphUri ) = 0;
106 virtual void updateCollection( const Akonadi::Collection &collection, const QUrl &graphUri ) = 0;
108 /** Reimplement to allow more aggressive initial indexing. */
109 virtual Akonadi::ItemFetchScope fetchScopeForCollection( const Akonadi::Collection &collection );
111 /** Create a graph for the given item with we use to mark all information created by the feeder agent. */
112 template <typename T>
113 QUrl createGraphForEntity( const T &item )
115 QUrl metaDataGraphUri;
116 const QUrl graphUri = mNrlModel->createGraph( Soprano::Vocabulary::NRL::InstanceBase(), &metaDataGraphUri );
118 // remember to which graph the item belongs to (used in search query in removeItemFromNepomuk())
119 mNrlModel->addStatement( graphUri,
120 QUrl::fromEncoded( "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#dataGraphFor", QUrl::StrictMode ),
121 item.url(), metaDataGraphUri );
123 return graphUri;
126 /** Finds (or if it doesn't exist creates) a PersonContact object for the given name and address.
127 @param found Used to indicate if the contact is already there are was just newly created. In the latter case you might
128 want to add additional information you have available for it.
130 static NepomukFast::PersonContact findOrCreateContact( const QString &email, const QString &name, const QUrl &graphUri, bool *found = 0 );
132 template <typename R, typename E>
133 static void setParent( R& res, const E &entity )
135 if ( entity.parentCollection().isValid() && entity.parentCollection() != Akonadi::Collection::root() )
136 res.addProperty( Vocabulary::NIE::isPartOf(), entity.parentCollection().url() );
140 Enables Strigi support for indexing attachments.
142 void setNeedsStrigi( bool enableStrigi );
145 Index the given data with Strigi. Use for e.g. attachments.
147 void indexData( const KUrl &url, const QByteArray &data, const QDateTime &mtime = QDateTime::currentDateTime() );
150 * Set the index compatibility level. If the current level is below this, a full re-indexing is performed.
152 void setIndexCompatibilityLevel( int level );
155 * Sets whether the 'Only feed when system is idle' functionality shall be used.
157 void disableIdleDetection( bool value );
159 public slots:
160 /** Trigger a complete update of all items. */
161 void updateAll();
163 signals:
164 void fullyIndexed();
166 protected:
167 void itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection );
168 void itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &partIdentifiers );
169 void itemRemoved(const Akonadi::Item &item);
171 void collectionAdded(const Akonadi::Collection& collection, const Akonadi::Collection& parent);
172 void collectionChanged(const Akonadi::Collection& collection, const QSet< QByteArray >& partIdentifiers);
173 using AgentBase::ObserverV2::collectionChanged;
174 void collectionRemoved(const Akonadi::Collection& collection);
176 void doSetOnline(bool online);
178 private:
179 void processNextCollection();
182 Overrides in subclasses to cause re-indexing on startup to only happen
183 when the format changes, for example. Base implementation checks the index compatibility level.
185 virtual bool needsReIndexing() const;
187 void checkOnline();
189 private slots:
190 void collectionsReceived( const Akonadi::Collection::List &collections );
191 void itemHeadersReceived( const Akonadi::Item::List &items );
192 void itemsReceived( const Akonadi::Item::List &items );
193 void notificationItemsReceived( const Akonadi::Item::List &items );
194 void itemFetchResult( KJob* job );
196 void selfTest();
197 void slotFullyIndexed();
198 void systemIdle();
199 void systemResumed();
201 void processPipeline();
203 private:
204 QStringList mSupportedMimeTypes;
205 Akonadi::MimeTypeChecker mMimeTypeChecker;
206 Akonadi::Collection::List mCollectionQueue;
207 Akonadi::Collection mCurrentCollection;
208 QQueue<Akonadi::Item> mItemPipeline;
209 int mTotalAmount, mProcessedAmount, mPendingJobs;
210 QTimer mNepomukStartupTimeout;
211 QTimer mProcessPipelineTimer;
212 Soprano::NRLModel *mNrlModel;
213 Strigi::IndexManager *mStrigiIndexManager;
214 int mIndexCompatLevel;
215 bool mNepomukStartupAttempted;
216 bool mInitialUpdateDone;
217 bool mNeedsStrigi;
218 bool mSelfTestPassed;
219 bool mSystemIsIdle;
220 bool mIdleDetectionDisabled;
223 #endif