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 00048 class AKONADI_TESTS_EXPORT MonitorPrivate 00049 { 00050 public: 00051 MonitorPrivate( Monitor *parent ); 00052 virtual ~MonitorPrivate() {} 00053 void init(); 00054 00055 Monitor *q_ptr; 00056 Q_DECLARE_PUBLIC( Monitor ) 00057 org::freedesktop::Akonadi::NotificationSource *notificationSource; 00058 Collection::List collections; 00059 QSet<QByteArray> resources; 00060 QSet<Item::Id> items; 00061 QSet<QString> mimetypes; 00062 bool monitorAll; 00063 QList<QByteArray> sessions; 00064 ItemFetchScope mItemFetchScope; 00065 CollectionFetchScope mCollectionFetchScope; 00066 Session *session; 00067 CollectionCache collectionCache; 00068 ItemCache itemCache; 00069 QQueue<NotificationMessage> pendingNotifications; 00070 QQueue<NotificationMessage> pipeline; 00071 bool fetchCollection; 00072 bool fetchCollectionStatistics; 00073 00074 // Virtual so it can be overridden in FakeMonitor. 00075 virtual bool connectToNotificationManager(); 00076 bool acceptNotification( const NotificationMessage &msg ); 00077 void dispatchNotifications(); 00078 00079 // Called when the monitored item/collection changes, checks if the queued messages 00080 // are still accepted, if not they are removed 00081 void cleanOldNotifications(); 00082 00083 bool ensureDataAvailable( const NotificationMessage &msg ); 00088 virtual bool emitNotification( const NotificationMessage &msg ); 00089 void updatePendingStatistics( const NotificationMessage &msg ); 00090 void invalidateCaches( const NotificationMessage &msg ); 00091 00095 void invalidateCache( const Collection &col ); 00096 00097 virtual int pipelineSize() const; 00098 00099 // private slots 00100 void dataAvailable(); 00101 void slotSessionDestroyed( QObject* ); 00102 void slotStatisticsChangedFinished( KJob* ); 00103 void slotFlushRecentlyChangedCollections(); 00104 00105 void appendAndCompress( const NotificationMessage &msg ); 00106 00107 virtual void slotNotify( const NotificationMessage::List &msgs ); 00108 00113 bool emitItemNotification( const NotificationMessage &msg, const Item &item = Item(), 00114 const Collection &collection = Collection(), const Collection &collectionDest = Collection() ); 00119 bool emitCollectionNotification( const NotificationMessage &msg, const Collection &col = Collection(), 00120 const Collection &par = Collection(), const Collection &dest = Collection() ); 00121 00122 void serverStateChanged( Akonadi::ServerManager::State state ); 00123 00124 00137 class PurgeBuffer 00138 { 00139 // Buffer the most recent 10 unreferenced Collections 00140 static const int MAXBUFFERSIZE = 10; 00141 public: 00142 explicit PurgeBuffer() 00143 : m_index( 0 ), 00144 m_bufferSize( MAXBUFFERSIZE ) 00145 { 00146 } 00147 00153 Collection::Id buffer( Collection::Id id ); 00154 00158 void purge( Collection::Id id ); 00159 00160 bool isBuffered( Collection::Id id ) const 00161 { 00162 return m_buffer.contains( id ); 00163 } 00164 00165 private: 00166 QVector<Collection::Id> m_buffer; 00167 int m_index; 00168 int m_bufferSize; 00169 } m_buffer; 00170 00171 00172 QHash<Collection::Id, int> refCountMap; 00173 bool useRefCounting; 00174 void ref( Collection::Id id ); 00175 Collection::Id deref( Collection::Id id ); 00176 00177 private: 00178 // collections that need a statistics update 00179 QSet<Collection::Id> recentlyChangedCollections; 00180 QTimer statisticsCompressionTimer; 00181 00185 bool isLazilyIgnored( const NotificationMessage & msg ) const; 00186 00187 bool isCollectionMonitored( Collection::Id collection ) const 00188 { 00189 if ( collections.contains( Collection( collection ) ) ) 00190 return true; 00191 if ( collections.contains( Collection::root() ) ) 00192 return true; 00193 return false; 00194 } 00195 00196 bool isMimeTypeMonitored( const QString& mimetype ) const 00197 { 00198 if ( mimetypes.contains( mimetype ) ) 00199 return true; 00200 00201 KMimeType::Ptr mimeType = KMimeType::mimeType( mimetype, KMimeType::ResolveAliases ); 00202 if ( mimeType.isNull() ) 00203 return false; 00204 00205 foreach ( const QString &mt, mimetypes ) { 00206 if ( mimeType->is( mt ) ) 00207 return true; 00208 } 00209 00210 return false; 00211 } 00212 00213 bool isMoveDestinationResourceMonitored( const NotificationMessage &msg ) 00214 { 00215 if ( msg.operation() != NotificationMessage::Move || msg.itemParts().isEmpty() ) 00216 return false; 00217 const QByteArray res = *(msg.itemParts().begin()); 00218 return resources.contains( res ); 00219 } 00220 00221 void fetchStatistics( Collection::Id colId ) 00222 { 00223 CollectionStatisticsJob *job = new CollectionStatisticsJob( Collection( colId ), session ); 00224 QObject::connect( job, SIGNAL( result( KJob* ) ), q_ptr, SLOT( slotStatisticsChangedFinished( KJob* ) ) ); 00225 } 00226 00227 void notifyCollectionStatisticsWatchers( Collection::Id collection, const QByteArray &resource ); 00228 }; 00229 00230 } 00231 00232 #endif