• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

specialcollections.cpp

00001 /*
00002     Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
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 #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     // Retrieve the list of special folders for the resource the collection belongs to
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           // The collection to be removed is a special folder
00089           it.remove();
00090           emitChanged( collection.resource() );
00091         }
00092       }
00093     }
00094 
00095     if ( folders.isEmpty() ) {
00096       // This resource has no more folders, so remove it completely.
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     // We do not know any folders in this resource.
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     // We do not know any folders in this resource.
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     // We do not know any folders in this resource yet.
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"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal