akonadi
monitor_p.h
00001 /* 00002 Copyright (c) 2007 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #ifndef AKONADI_MONITOR_P_H 00021 #define AKONADI_MONITOR_P_H 00022 00023 #include "akonadiprivate_export.h" 00024 #include "monitor.h" 00025 #include "collection.h" 00026 #include "collectionstatisticsjob.h" 00027 #include "collectionfetchscope.h" 00028 #include "item.h" 00029 #include "itemfetchscope.h" 00030 #include "job.h" 00031 #include <akonadi/private/notificationmessage_p.h> 00032 #include "notificationsourceinterface.h" 00033 #include "entitycache_p.h" 00034 #include "servermanager.h" 00035 00036 #include <kmimetype.h> 00037 00038 #include <QtCore/QObject> 00039 #include <QtCore/QTimer> 00040 00041 namespace Akonadi { 00042 00043 class Monitor; 00044 00049 class AKONADI_TESTS_EXPORT MonitorDependeciesFactory 00050 { 00051 public: 00052 virtual ~MonitorDependeciesFactory() {} 00053 virtual QObject* createNotificationSource(QObject *parent); 00054 00055 virtual Akonadi::CollectionCache* createCollectionCache(int maxCapacity, Session *session); 00056 virtual Akonadi::ItemCache* createItemCache(int maxCapacity, Session *session); 00057 }; 00058 00059 00063 class AKONADI_TESTS_EXPORT MonitorPrivate 00064 { 00065 public: 00066 MonitorPrivate( MonitorDependeciesFactory *dependenciesFactory_, Monitor *parent ); 00067 virtual ~MonitorPrivate() { 00068 delete dependenciesFactory; 00069 delete collectionCache; 00070 delete itemCache; 00071 } 00072 void init(); 00073 00074 Monitor *q_ptr; 00075 Q_DECLARE_PUBLIC( Monitor ) 00076 MonitorDependeciesFactory *dependenciesFactory; 00077 QObject* notificationSource; 00078 Collection::List collections; 00079 QSet<QByteArray> resources; 00080 QSet<Item::Id> items; 00081 QSet<QString> mimetypes; 00082 bool monitorAll; 00083 QList<QByteArray> sessions; 00084 ItemFetchScope mItemFetchScope; 00085 CollectionFetchScope mCollectionFetchScope; 00086 Session *session; 00087 CollectionCache *collectionCache; 00088 ItemCache *itemCache; 00089 QQueue<NotificationMessage> pendingNotifications; 00090 QQueue<NotificationMessage> pipeline; 00091 bool fetchCollection; 00092 bool fetchCollectionStatistics; 00093 00094 // Virtual so it can be overridden in FakeMonitor. 00095 virtual bool connectToNotificationManager(); 00096 bool acceptNotification( const NotificationMessage &msg ); 00097 void dispatchNotifications(); 00098 void flushPipeline(); 00099 00100 // Called when the monitored item/collection changes, checks if the queued messages 00101 // are still accepted, if not they are removed 00102 void cleanOldNotifications(); 00103 00104 bool ensureDataAvailable( const NotificationMessage &msg ); 00109 virtual bool emitNotification( const NotificationMessage &msg ); 00110 void updatePendingStatistics( const NotificationMessage &msg ); 00111 void invalidateCaches( const NotificationMessage &msg ); 00112 00116 void invalidateCache( const Collection &col ); 00117 00118 virtual int pipelineSize() const; 00119 00120 // private slots 00121 void dataAvailable(); 00122 void slotSessionDestroyed( QObject* ); 00123 void slotStatisticsChangedFinished( KJob* ); 00124 void slotFlushRecentlyChangedCollections(); 00125 00129 bool translateAndCompress( QQueue<NotificationMessage> ¬ificationQueue, const NotificationMessage &msg ); 00130 00131 virtual void slotNotify( const NotificationMessage::List &msgs ); 00132 00137 bool emitItemNotification( const NotificationMessage &msg, const Item &item = Item(), 00138 const Collection &collection = Collection(), const Collection &collectionDest = Collection() ); 00143 bool emitCollectionNotification( const NotificationMessage &msg, const Collection &col = Collection(), 00144 const Collection &par = Collection(), const Collection &dest = Collection() ); 00145 00146 void serverStateChanged( Akonadi::ServerManager::State state ); 00147 00148 00161 class PurgeBuffer 00162 { 00163 // Buffer the most recent 10 unreferenced Collections 00164 static const int MAXBUFFERSIZE = 10; 00165 public: 00166 explicit PurgeBuffer() 00167 : m_index( 0 ), 00168 m_bufferSize( MAXBUFFERSIZE ) 00169 { 00170 } 00171 00177 Collection::Id buffer( Collection::Id id ); 00178 00182 void purge( Collection::Id id ); 00183 00184 bool isBuffered( Collection::Id id ) const 00185 { 00186 return m_buffer.contains( id ); 00187 } 00188 00189 private: 00190 QVector<Collection::Id> m_buffer; 00191 int m_index; 00192 int m_bufferSize; 00193 } m_buffer; 00194 00195 00196 QHash<Collection::Id, int> refCountMap; 00197 bool useRefCounting; 00198 void ref( Collection::Id id ); 00199 Collection::Id deref( Collection::Id id ); 00200 00201 private: 00202 // collections that need a statistics update 00203 QSet<Collection::Id> recentlyChangedCollections; 00204 QTimer statisticsCompressionTimer; 00205 00209 bool isLazilyIgnored( const NotificationMessage & msg ) const; 00210 00211 bool isCollectionMonitored( Collection::Id collection ) const 00212 { 00213 if (collection < 0) 00214 return false; 00215 if ( collections.contains( Collection( collection ) ) ) 00216 return true; 00217 if ( collections.contains( Collection::root() ) ) 00218 return true; 00219 return false; 00220 } 00221 00222 bool isMimeTypeMonitored( const QString& mimetype ) const 00223 { 00224 if ( mimetypes.contains( mimetype ) ) 00225 return true; 00226 00227 KMimeType::Ptr mimeType = KMimeType::mimeType( mimetype, KMimeType::ResolveAliases ); 00228 if ( mimeType.isNull() ) 00229 return false; 00230 00231 foreach ( const QString &mt, mimetypes ) { 00232 if ( mimeType->is( mt ) ) 00233 return true; 00234 } 00235 00236 return false; 00237 } 00238 00239 bool isMoveDestinationResourceMonitored( const NotificationMessage &msg ) 00240 { 00241 if ( msg.operation() != NotificationMessage::Move ) 00242 return false; 00243 return resources.contains( msg.destinationResource() ); 00244 } 00245 00246 void fetchStatistics( Collection::Id colId ) 00247 { 00248 CollectionStatisticsJob *job = new CollectionStatisticsJob( Collection( colId ), session ); 00249 QObject::connect( job, SIGNAL( result( KJob* ) ), q_ptr, SLOT( slotStatisticsChangedFinished( KJob* ) ) ); 00250 } 00251 00252 void notifyCollectionStatisticsWatchers( Collection::Id collection, const QByteArray &resource ); 00253 }; 00254 00255 } 00256 00257 #endif