diff --git a/akonadi/preprocessorbase.cpp b/akonadi/preprocessorbase.cpp index e8655717f..793fbbbea 100644 --- a/akonadi/preprocessorbase.cpp +++ b/akonadi/preprocessorbase.cpp @@ -1,107 +1,110 @@ /****************************************************************************** * * Copyright (c) 2009 Szymon Stefanek * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA, 02110-1301, USA. * *****************************************************************************/ #include "preprocessorbase.h" #include "agentbase_p.h" #include "item.h" +#include "collection.h" #include "preprocessoradaptor.h" #include namespace Akonadi { class PreprocessorBasePrivate : public AgentBasePrivate { public: PreprocessorBasePrivate( PreprocessorBase *parent ) : AgentBasePrivate( parent ), mInDelayedProcessing( false ), mDelayedProcessingItemId( 0 ) { } Q_DECLARE_PUBLIC( PreprocessorBase ) void delayedInit() { if ( !QDBusConnection::sessionBus().registerService( QLatin1String( "org.freedesktop.Akonadi.Preprocessor." ) + mId ) ) kFatal() << "Unable to register service at D-Bus: " << QDBusConnection::sessionBus().lastError().message(); AgentBasePrivate::delayedInit(); } bool mInDelayedProcessing; qlonglong mDelayedProcessingItemId; }; -} +} // namespace Akonadi using namespace Akonadi; PreprocessorBase::PreprocessorBase( const QString &id ) : AgentBase( new PreprocessorBasePrivate( this ), id ) { new PreprocessorAdaptor( this ); } PreprocessorBase::~PreprocessorBase() { } void PreprocessorBase::terminateProcessing( ProcessingResult result ) { Q_D( PreprocessorBase ); Q_ASSERT_X( result != ProcessingDelayed, "PreprocessorBase::terminateProcessing", "You should never pass ProcessingDelayed to this function" ); Q_ASSERT_X( d->mInDelayedProcessing, "PreprocessorBase::terminateProcessing", "terminateProcessing() called while not in delayed processing mode" ); d->mInDelayedProcessing = false; emit itemProcessed( d->mDelayedProcessingItemId ); } -void PreprocessorBase::beginProcessItem( qlonglong id ) +void PreprocessorBase::beginProcessItem( qlonglong itemId, qlonglong collectionId, const QString &mimeType ) { Q_D( PreprocessorBase ); - kDebug() << "PreprocessorBase: about to process item " << id; + kDebug() << "PreprocessorBase: about to process item " << itemId << " in collection " << collectionId << " and mimetype " << mimeType; - switch( processItem( Item( id ) ) ) + switch( processItem( static_cast< Item::Id >( itemId ), static_cast< Collection::Id >( collectionId ), mimeType ) ) { case ProcessingFailed: case ProcessingRefused: case ProcessingCompleted: - kDebug() << "PreprocessorBase: item processed, emitting signal (" << id << ")"; + kDebug() << "PreprocessorBase: item processed, emitting signal (" << itemId << ")"; + + // TODO: Handle the different status codes appropriately - emit itemProcessed( id ); + emit itemProcessed( itemId ); - kDebug() << "PreprocessorBase: item processed, signal emitted (" << id << ")"; + kDebug() << "PreprocessorBase: item processed, signal emitted (" << itemId << ")"; break; case ProcessingDelayed: - kDebug() << "PreprocessorBase: item processing delayed (" << id << ")"; + kDebug() << "PreprocessorBase: item processing delayed (" << itemId << ")"; d->mInDelayedProcessing = true; - d->mDelayedProcessingItemId = id; + d->mDelayedProcessingItemId = itemId; break; } } #include "preprocessorbase.moc" diff --git a/akonadi/preprocessorbase.h b/akonadi/preprocessorbase.h index 77227b040..097ead2b4 100644 --- a/akonadi/preprocessorbase.h +++ b/akonadi/preprocessorbase.h @@ -1,169 +1,185 @@ /****************************************************************************** * * Copyright (c) 2009 Szymon Stefanek * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA, 02110-1301, USA. * *****************************************************************************/ #ifndef AKONADI_PREPROCESSORBASE_H #define AKONADI_PREPROCESSORBASE_H #include "akonadi_export.h" #include +#include +#include class PreprocessorAdaptor; namespace Akonadi { -class Item; class PreprocessorBasePrivate; /** * @short The base class for all Akonadi preprocessor agents. * * This class should be used as a base class by all preprocessor agents * since it encapsulates large parts of the protocol between * preprocessor agent, agent manager and the Akonadi storage. * * Preprocessor agents are special agents that are informed about newly * added items before any other agents. This allows them to do filtering * on the items or any other task that shall be done before the new item * is visible in the Akonadi storage system. * * The method all the preprocessors must implement is processItem(). * * @author Szymon Stefanek * @since 4.4 */ class AKONADI_EXPORT PreprocessorBase : public AgentBase { friend class PreprocessorAdaptor; Q_OBJECT public: /** * Describes the possible return values of the processItem() method. */ enum ProcessingResult { /** * Processing completed succesfully for this item. * The Akonadi server will push in a new item when it's available. */ ProcessingCompleted, /** * Processing was delayed to a later stage. * This must be returned when implementing asynchronous preprocessing. * * If this value is returned, terminateProcessing() has to be called * when processing is done. */ ProcessingDelayed, /** * Processing for this item failed (and the failure is unrecoverable). * The Akonadi server will push in a new item when it's available, * after possibly logging the failure. */ ProcessingFailed, /** * Processing for this item was refused. This is very * similar to ProcessingFailed above but additionally remarks * that the item that the Akonadi server pushed in wasn't * meant for this Preprocessor. * The Akonadi server will push in a new item when it's available, * after possibly logging the failure and maybe taking some additional action. */ ProcessingRefused }; /** - * This method has to be implement by every preprocessor subclass. + * This method must be implemented by every preprocessor subclass. * - * Returns ProcessingCompleted on success, ProcessingDelayed + * It must realize the preprocessing of the item with the specified itemId, + * which is actually in the collection with collectionId and has the specified mimetype. + * + * The Akonadi server will push in for preprocessing any newly created item: + * it's your responsability to decide if you want to process the item or not. + * + * The method should return ProcessingCompleted on success, ProcessingDelayed * if processing is implemented asynchronously and * ProcessingRefused or ProcessingFailed if the processing * didn't complete. + * + * If your operation is asynchronous then you should also + * connect to the abortRequested() signal and handle it + * appropriately (as the server MAY abort your async job + * if it decides that it's taking too long). */ - virtual ProcessingResult processItem( const Item &item ) = 0; + virtual ProcessingResult processItem( Item::Id itemId, Collection::Id collectionId, const QString &mimeType ) = 0; /** * This method must be called if processing is implemented asynchronously. * + * You should call it when you have completed the processing + * or if an abortRequest() signal arrives (and in this case you + * will probably use ProcessingFailed as result). + * * Valid values for @p result are ProcessingCompleted, * PocessingRefused and ProcessingFailed. Passing any * other value will lead to a runtime assertion. */ void terminateProcessing( ProcessingResult result ); Q_SIGNALS: /** * This signal is emitted to report item processing termination * to the Akonadi server. * * @note This signal is only for internal use. */ void itemProcessed( qlonglong id ); protected: /** * Creates a new preprocessor base agent. * * @param id The instance id of the preprocessor base agent. */ PreprocessorBase( const QString &id ); /** * Destroys the preprocessor base agent. */ virtual ~PreprocessorBase(); /** * This dbus method is called by the Akonadi server * in order to trigger the processing of an item. * * @note Do not call it manually! */ - void beginProcessItem( qlonglong itemId ); + void beginProcessItem( qlonglong itemId, qlonglong collectionId, const QString &mimeType ); private: // dbus Preprocessor interface friend class ::PreprocessorAdaptor; Q_DECLARE_PRIVATE( PreprocessorBase ) }; // class PreprocessorBase } // namespace Akonadi #ifndef AKONADI_PREPROCESSOR_MAIN /** * Convenience Macro for the most common main() function for Akonadi preprocessors. */ #define AKONADI_PREPROCESSOR_MAIN( preProcessorClass ) \ int main( int argc, char **argv ) \ { \ return Akonadi::PreprocessorBase::init( argc, argv ); \ } #endif //!AKONADI_RESOURCE_MAIN #endif //!_PREPROCESSORBASE_H_