akonadi
specialcollections.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "specialcollections.h"
00021
00022 #include "specialcollections_p.h"
00023 #include "specialcollectionattribute_p.h"
00024
00025 #include "akonadi/agentinstance.h"
00026 #include "akonadi/agentmanager.h"
00027 #include "akonadi/collectionmodifyjob.h"
00028 #include "akonadi/monitor.h"
00029
00030 #include <KDebug>
00031 #include <kcoreconfigskeleton.h>
00032
00033 #include <QtCore/QHash>
00034 #include <QtCore/QObject>
00035 #include <QtCore/QSet>
00036
00037 using namespace Akonadi;
00038
00039 SpecialCollectionsPrivate::SpecialCollectionsPrivate( KCoreConfigSkeleton *settings, SpecialCollections *qq )
00040 : q( qq ),
00041 mSettings( settings ),
00042 mBatchMode( false )
00043 {
00044 mMonitor = new Monitor( q );
00045 QObject::connect( mMonitor, SIGNAL( collectionRemoved( const Akonadi::Collection& ) ),
00046 q, SLOT( collectionRemoved( const Akonadi::Collection& ) ) );
00047 }
00048
00049 SpecialCollectionsPrivate::~SpecialCollectionsPrivate()
00050 {
00051 }
00052
00053 QString SpecialCollectionsPrivate::defaultResourceId() const
00054 {
00055 const KConfigSkeletonItem *item = mSettings->findItem( QLatin1String( "DefaultResourceId" ) );
00056 Q_ASSERT( item );
00057
00058 return item->property().toString();
00059 }
00060
00061 void SpecialCollectionsPrivate::emitChanged( const QString &resourceId )
00062 {
00063 if ( mBatchMode ) {
00064 mToEmitChangedFor.insert( resourceId );
00065 } else {
00066 kDebug() << "Emitting changed for" << resourceId;
00067 const AgentInstance agentInstance = AgentManager::self()->instance( resourceId );
00068 emit q->collectionsChanged( agentInstance );
00069 if ( resourceId == defaultResourceId() ) {
00070 kDebug() << "Emitting defaultFoldersChanged.";
00071 emit q->defaultCollectionsChanged();
00072 }
00073 }
00074 }
00075
00076 void SpecialCollectionsPrivate::collectionRemoved( const Collection &collection )
00077 {
00078 kDebug() << "Collection" << collection.id() << "resource" << collection.resource();
00079 if ( mFoldersForResource.contains( collection.resource() ) ) {
00080
00081
00082 QHash<QByteArray, Collection> &folders = mFoldersForResource[ collection.resource() ];
00083 {
00084 QMutableHashIterator<QByteArray, Collection> it( folders );
00085 while ( it.hasNext() ) {
00086 it.next();
00087 if ( it.value() == collection ) {
00088
00089 it.remove();
00090 emitChanged( collection.resource() );
00091 }
00092 }
00093 }
00094
00095 if ( folders.isEmpty() ) {
00096
00097 mFoldersForResource.remove( collection.resource() );
00098 }
00099 }
00100 }
00101
00102 void SpecialCollectionsPrivate::beginBatchRegister()
00103 {
00104 Q_ASSERT( !mBatchMode );
00105 mBatchMode = true;
00106 Q_ASSERT( mToEmitChangedFor.isEmpty() );
00107 }
00108
00109 void SpecialCollectionsPrivate::endBatchRegister()
00110 {
00111 Q_ASSERT( mBatchMode );
00112 mBatchMode = false;
00113
00114 foreach ( const QString &resourceId, mToEmitChangedFor ) {
00115 emitChanged( resourceId );
00116 }
00117
00118 mToEmitChangedFor.clear();
00119 }
00120
00121 void SpecialCollectionsPrivate::forgetFoldersForResource( const QString &resourceId )
00122 {
00123 if ( mFoldersForResource.contains( resourceId ) ) {
00124 const Collection::List folders = mFoldersForResource[ resourceId ].values();
00125
00126 foreach ( const Collection &collection, folders ) {
00127 mMonitor->setCollectionMonitored( collection, false );
00128 }
00129
00130 mFoldersForResource.remove( resourceId );
00131 emitChanged( resourceId );
00132 }
00133 }
00134
00135 AgentInstance SpecialCollectionsPrivate::defaultResource() const
00136 {
00137 const QString identifier = defaultResourceId();
00138 return AgentManager::self()->instance( identifier );
00139 }
00140
00141
00142 SpecialCollections::SpecialCollections( KCoreConfigSkeleton *settings, QObject *parent )
00143 : QObject( parent ),
00144 d( new SpecialCollectionsPrivate( settings, this ) )
00145 {
00146 }
00147
00148 SpecialCollections::~SpecialCollections()
00149 {
00150 delete d;
00151 }
00152
00153 bool SpecialCollections::hasCollection( const QByteArray &type, const AgentInstance &instance ) const
00154 {
00155 kDebug() << "Type" << type << "resourceId" << instance.identifier();
00156
00157 if ( !d->mFoldersForResource.contains( instance.identifier() ) ) {
00158
00159 return false;
00160 }
00161
00162 return d->mFoldersForResource[ instance.identifier() ].contains( type );
00163 }
00164
00165 Collection SpecialCollections::collection( const QByteArray &type, const AgentInstance &instance ) const
00166 {
00167 if ( !d->mFoldersForResource.contains( instance.identifier() ) ) {
00168
00169 return Collection( -1 );
00170 }
00171 return d->mFoldersForResource[ instance.identifier() ][ type ];
00172 }
00173
00174 bool SpecialCollections::registerCollection( const QByteArray &type, const Collection &collection )
00175 {
00176 if ( !collection.isValid() ) {
00177 kWarning() << "Invalid collection.";
00178 return false;
00179 }
00180
00181 const QString &resourceId = collection.resource();
00182 if ( resourceId.isEmpty() ) {
00183 kWarning() << "Collection has empty resourceId.";
00184 return false;
00185 }
00186
00187 {
00188 Collection attributeCollection( collection );
00189 SpecialCollectionAttribute *attribute = attributeCollection.attribute<SpecialCollectionAttribute>( Collection::AddIfMissing );
00190 attribute->setCollectionType( type );
00191
00192 CollectionModifyJob *job = new CollectionModifyJob( attributeCollection );
00193 job->start();
00194 }
00195
00196 if ( !d->mFoldersForResource.contains( resourceId ) ) {
00197
00198 d->mFoldersForResource.insert( resourceId, QHash<QByteArray, Collection>() );
00199 }
00200
00201 if ( !d->mFoldersForResource[ resourceId ].contains( type ) )
00202 d->mFoldersForResource[ resourceId ].insert( type, Collection() );
00203
00204 if ( d->mFoldersForResource[ resourceId ][ type ] != collection ) {
00205 d->mMonitor->setCollectionMonitored( d->mFoldersForResource[ resourceId ][ type ], false );
00206 d->mMonitor->setCollectionMonitored( collection, true );
00207 d->mFoldersForResource[ resourceId ].insert( type, collection );
00208 d->emitChanged( resourceId );
00209 }
00210
00211 kDebug() << "Registered collection" << collection.id() << "as folder of type" << type
00212 << "in resource" << resourceId;
00213 return true;
00214 }
00215
00216 bool SpecialCollections::hasDefaultCollection( const QByteArray &type ) const
00217 {
00218 return hasCollection( type, d->defaultResource() );
00219 }
00220
00221 Collection SpecialCollections::defaultCollection( const QByteArray &type ) const
00222 {
00223 return collection( type, d->defaultResource() );
00224 }
00225
00226 #include "specialcollections.moc"