• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
specialcollections.cpp
1 /*
2  Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "specialcollections.h"
21 
22 #include "specialcollections_p.h"
23 #include "specialcollectionattribute_p.h"
24 
25 #include "akonadi/agentinstance.h"
26 #include "akonadi/agentmanager.h"
27 #include "akonadi/collectionmodifyjob.h"
28 #include "akonadi/collectionfetchjob.h"
29 #include "akonadi/monitor.h"
30 
31 #include <KDebug>
32 #include <kcoreconfigskeleton.h>
33 
34 #include <QtCore/QHash>
35 #include <QtCore/QObject>
36 #include <QtCore/QSet>
37 #include <akonadi/collectionfetchscope.h>
38 
39 using namespace Akonadi;
40 
41 SpecialCollectionsPrivate::SpecialCollectionsPrivate( KCoreConfigSkeleton *settings, SpecialCollections *qq )
42  : q( qq ),
43  mSettings( settings ),
44  mBatchMode( false )
45 {
46  mMonitor = new Monitor( q );
47  mMonitor->fetchCollectionStatistics( true );
48 
53  QObject::connect( mMonitor, SIGNAL(collectionRemoved(Akonadi::Collection)),
54  q, SLOT(collectionRemoved(Akonadi::Collection)) );
55  QObject::connect( mMonitor, SIGNAL(collectionStatisticsChanged(Akonadi::Collection::Id,Akonadi::CollectionStatistics)),
56  q, SLOT(collectionStatisticsChanged(Akonadi::Collection::Id,Akonadi::CollectionStatistics)) );
57 }
58 
59 SpecialCollectionsPrivate::~SpecialCollectionsPrivate()
60 {
61 }
62 
63 QString SpecialCollectionsPrivate::defaultResourceId() const
64 {
65  if ( mDefaultResourceId.isEmpty() ) {
66  mSettings->readConfig();
67  const KConfigSkeletonItem *item = mSettings->findItem( QLatin1String( "DefaultResourceId" ) );
68  Q_ASSERT( item );
69 
70  mDefaultResourceId = item->property().toString();
71  }
72  return mDefaultResourceId;
73 }
74 
75 void SpecialCollectionsPrivate::emitChanged( const QString &resourceId )
76 {
77  if ( mBatchMode ) {
78  mToEmitChangedFor.insert( resourceId );
79  } else {
80  kDebug() << "Emitting changed for" << resourceId;
81  const AgentInstance agentInstance = AgentManager::self()->instance( resourceId );
82  emit q->collectionsChanged( agentInstance );
83  // first compare with local value then with config value (which also updates the local value)
84  if ( resourceId == mDefaultResourceId || resourceId == defaultResourceId() ) {
85  kDebug() << "Emitting defaultFoldersChanged.";
86  emit q->defaultCollectionsChanged();
87  }
88  }
89 }
90 
91 void SpecialCollectionsPrivate::collectionRemoved( const Collection &collection )
92 {
93  kDebug() << "Collection" << collection.id() << "resource" << collection.resource();
94  if ( mFoldersForResource.contains( collection.resource() ) ) {
95 
96  // Retrieve the list of special folders for the resource the collection belongs to
97  QHash<QByteArray, Collection> &folders = mFoldersForResource[ collection.resource() ];
98  {
99  QMutableHashIterator<QByteArray, Collection> it( folders );
100  while ( it.hasNext() ) {
101  it.next();
102  if ( it.value() == collection ) {
103  // The collection to be removed is a special folder
104  it.remove();
105  emitChanged( collection.resource() );
106  }
107  }
108  }
109 
110  if ( folders.isEmpty() ) {
111  // This resource has no more folders, so remove it completely.
112  mFoldersForResource.remove( collection.resource() );
113  }
114  }
115 }
116 
117 void SpecialCollectionsPrivate::collectionStatisticsChanged( Akonadi::Collection::Id collectionId, const Akonadi::CollectionStatistics &statistics )
118 {
119  // need to get the name of the collection in order to be able to check if we are storing it,
120  // but we have the id from the monitor, so fetch the name.
121  Akonadi::CollectionFetchJob* fetchJob = new Akonadi::CollectionFetchJob( Collection( collectionId ), Akonadi::CollectionFetchJob::Base );
122  fetchJob->fetchScope().setAncestorRetrieval( Akonadi::CollectionFetchScope::None );
123  fetchJob->setProperty( "statistics", QVariant::fromValue( statistics ) );
124 
125  q->connect( fetchJob, SIGNAL(result(KJob*)), q, SLOT(collectionFetchJobFinished(KJob*)) );
126 }
127 
128 
129 void SpecialCollectionsPrivate::collectionFetchJobFinished( KJob* job )
130 {
131  if ( job->error() ) {
132  kWarning() << "Error fetching collection to get name from id for statistics updating in specialcollections!";
133  return;
134  }
135 
136  const Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
137 
138  Q_ASSERT( fetchJob->collections().size() > 0 );
139  const Akonadi::Collection collection = fetchJob->collections().first();
140  const Akonadi::CollectionStatistics statistics = fetchJob->property( "statistics" ).value<Akonadi::CollectionStatistics>();
141 
142  mFoldersForResource[ collection.resource() ][ collection.name().toUtf8() ].setStatistics( statistics );
143 }
144 
145 void SpecialCollectionsPrivate::beginBatchRegister()
146 {
147  Q_ASSERT( !mBatchMode );
148  mBatchMode = true;
149  Q_ASSERT( mToEmitChangedFor.isEmpty() );
150 }
151 
152 void SpecialCollectionsPrivate::endBatchRegister()
153 {
154  Q_ASSERT( mBatchMode );
155  mBatchMode = false;
156 
157  foreach ( const QString &resourceId, mToEmitChangedFor ) {
158  emitChanged( resourceId );
159  }
160 
161  mToEmitChangedFor.clear();
162 }
163 
164 void SpecialCollectionsPrivate::forgetFoldersForResource( const QString &resourceId )
165 {
166  if ( mFoldersForResource.contains( resourceId ) ) {
167  const Collection::List folders = mFoldersForResource[ resourceId ].values();
168 
169  foreach ( const Collection &collection, folders ) {
170  mMonitor->setCollectionMonitored( collection, false );
171  }
172 
173  mFoldersForResource.remove( resourceId );
174  emitChanged( resourceId );
175  }
176 }
177 
178 AgentInstance SpecialCollectionsPrivate::defaultResource() const
179 {
180  const QString identifier = defaultResourceId();
181  return AgentManager::self()->instance( identifier );
182 }
183 
184 
185 SpecialCollections::SpecialCollections( KCoreConfigSkeleton *settings, QObject *parent )
186  : QObject( parent ),
187  d( new SpecialCollectionsPrivate( settings, this ) )
188 {
189 }
190 
191 SpecialCollections::~SpecialCollections()
192 {
193  delete d;
194 }
195 
196 bool SpecialCollections::hasCollection( const QByteArray &type, const AgentInstance &instance ) const
197 {
198  return d->mFoldersForResource.value(instance.identifier()).contains(type);
199 }
200 
201 Akonadi::Collection SpecialCollections::collection( const QByteArray &type, const AgentInstance &instance ) const
202 {
203  return d->mFoldersForResource.value(instance.identifier()).value(type);
204 }
205 
206 void SpecialCollections::setSpecialCollectionType(const QByteArray &type, const Akonadi::Collection &collection)
207 {
208  if (!collection.hasAttribute<SpecialCollectionAttribute>() || collection.attribute<SpecialCollectionAttribute>()->collectionType() != type) {
209  Collection attributeCollection(collection);
210  SpecialCollectionAttribute *attribute = attributeCollection.attribute<SpecialCollectionAttribute>(Collection::AddIfMissing);
211  attribute->setCollectionType(type);
212  new CollectionModifyJob(attributeCollection);
213  }
214 }
215 
216 bool SpecialCollections::registerCollection( const QByteArray &type, const Collection &collection )
217 {
218  if ( !collection.isValid() ) {
219  kWarning() << "Invalid collection.";
220  return false;
221  }
222 
223  const QString &resourceId = collection.resource();
224  if ( resourceId.isEmpty() ) {
225  kWarning() << "Collection has empty resourceId.";
226  return false;
227  }
228 
229  setSpecialCollectionType(type, collection);
230 
231  const Collection oldCollection = d->mFoldersForResource.value(resourceId).value(type);
232  if (oldCollection != collection) {
233  if (oldCollection.isValid()) {
234  d->mMonitor->setCollectionMonitored(oldCollection, false);
235  }
236  d->mMonitor->setCollectionMonitored( collection, true );
237  d->mFoldersForResource[ resourceId ].insert( type, collection );
238  d->emitChanged( resourceId );
239  }
240 
241  return true;
242 }
243 
244 bool SpecialCollections::hasDefaultCollection( const QByteArray &type ) const
245 {
246  return hasCollection( type, d->defaultResource() );
247 }
248 
249 Akonadi::Collection SpecialCollections::defaultCollection( const QByteArray &type ) const
250 {
251  return collection( type, d->defaultResource() );
252 }
253 
254 #include "moc_specialcollections.cpp"
Akonadi::CollectionModifyJob
Job that modifies a collection in the Akonadi storage.
Definition: collectionmodifyjob.h:82
Akonadi::Monitor::fetchCollectionStatistics
void fetchCollectionStatistics(bool enable)
Enables automatic fetching of changed collection statistics information from the Akonadi storage...
Definition: monitor.cpp:142
Akonadi::SpecialCollectionsPrivate
Definition: specialcollections_p.h:44
Akonadi::SpecialCollections::registerCollection
bool registerCollection(const QByteArray &type, const Akonadi::Collection &collection)
Registers the given collection as a special collection with the given type.
Definition: specialcollections.cpp:216
Akonadi::SpecialCollections
An interface to special collections.
Definition: specialcollections.h:65
Akonadi::CollectionFetchScope::setAncestorRetrieval
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Definition: collectionfetchscope.cpp:134
Akonadi::CollectionFetchJob::collections
Collection::List collections() const
Returns the list of fetched collection.
Definition: collectionfetchjob.cpp:175
Akonadi::SpecialCollections::hasCollection
bool hasCollection(const QByteArray &type, const AgentInstance &instance) const
Returns whether the given agent instance has a special collection of the given type.
Definition: specialcollections.cpp:196
Akonadi::SpecialCollections::defaultCollectionsChanged
void defaultCollectionsChanged()
Emitted when the special collections for the default resource have been changed (for example...
Akonadi::CollectionStatistics
Provides statistics information of a Collection.
Definition: collectionstatistics.h:69
Akonadi::SpecialCollections::hasDefaultCollection
bool hasDefaultCollection(const QByteArray &type) const
Returns whether the default resource has a special collection of the given type.
Definition: specialcollections.cpp:244
Akonadi::Monitor::setCollectionMonitored
void setCollectionMonitored(const Collection &collection, bool monitored=true)
Sets whether the specified collection shall be monitored for changes.
Definition: monitor.cpp:68
Akonadi::SpecialCollectionsPrivate::beginBatchRegister
void beginBatchRegister()
Avoids emitting the foldersChanged() signal until endBatchRegister() is called.
Definition: specialcollections.cpp:145
Akonadi::CollectionFetchJob::fetchScope
CollectionFetchScope & fetchScope()
Returns the collection fetch scope.
Definition: collectionfetchjob.cpp:437
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::CollectionFetchJob
Job that fetches collections from the Akonadi storage.
Definition: collectionfetchjob.h:53
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:64
Akonadi::SpecialCollectionAttribute
An Attribute that stores the special collection type of a collection.
Definition: specialcollectionattribute_p.h:39
Akonadi::AgentInstance::identifier
QString identifier() const
Returns the unique identifier of the agent instance.
Definition: agentinstance.cpp:56
Akonadi::CollectionFetchJob::Base
Only fetch the base collection.
Definition: collectionfetchjob.h:62
Akonadi::Entity::attribute
Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition: entity.cpp:165
Akonadi::SpecialCollections::defaultCollection
Akonadi::Collection defaultCollection(const QByteArray &type) const
Returns the special collection of given type in the default resource, or an invalid collection if suc...
Definition: specialcollections.cpp:249
Akonadi::SpecialCollectionsPrivate::forgetFoldersForResource
void forgetFoldersForResource(const QString &resourceId)
Forgets all folders owned by the given resource.
Definition: specialcollections.cpp:164
Akonadi::AgentManager::instance
AgentInstance instance(const QString &identifier) const
Returns the agent instance with the given identifier or an invalid agent instance if the identifier d...
Definition: agentmanager.cpp:403
Akonadi::SpecialCollections::~SpecialCollections
~SpecialCollections()
Destroys the special collections object.
Definition: specialcollections.cpp:191
Akonadi::SpecialCollections::collection
Akonadi::Collection collection(const QByteArray &type, const AgentInstance &instance) const
Returns the special collection of the given type in the given agent instance, or an invalid collectio...
Definition: specialcollections.cpp:201
Akonadi::SpecialCollections::SpecialCollections
SpecialCollections(KCoreConfigSkeleton *config, QObject *parent=0)
Creates a new special collections object.
Definition: specialcollections.cpp:185
Akonadi::Entity::id
Id id() const
Returns the unique identifier of the entity.
Definition: entity.cpp:72
Akonadi::SpecialCollectionsPrivate::SpecialCollectionsPrivate
SpecialCollectionsPrivate(KCoreConfigSkeleton *settings, SpecialCollections *qq)
Definition: specialcollections.cpp:41
Akonadi::SpecialCollectionsPrivate::endBatchRegister
void endBatchRegister()
Definition: specialcollections.cpp:152
Akonadi::Monitor
Monitors an item or collection for changes.
Definition: monitor.h:72
Akonadi::SpecialCollectionAttribute::setCollectionType
void setCollectionType(const QByteArray &type)
Sets the special collections type of the collection.
Definition: specialcollectionattribute.cpp:69
Akonadi::Entity::hasAttribute
bool hasAttribute(const QByteArray &name) const
Returns true if the entity has an attribute of the given type name, false otherwise.
Definition: entity.cpp:146
Akonadi::SpecialCollectionAttribute::collectionType
QByteArray collectionType() const
Returns the special collections type of the collection.
Definition: specialcollectionattribute.cpp:74
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:379
Akonadi::Entity::AddIfMissing
Creates the attribute if it is missing.
Definition: entity.h:204
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::Collection::resource
QString resource() const
Returns the identifier of the resource owning the collection.
Definition: collection.cpp:207
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::CollectionFetchScope::None
No ancestor retrieval at all (the default)
Definition: collectionfetchscope.h:75
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
Akonadi::SpecialCollections::setSpecialCollectionType
static void setSpecialCollectionType(const QByteArray &type, const Akonadi::Collection &collection)
Sets the special collection attribute which marks collection as being a special collection of type ty...
Definition: specialcollections.cpp:206
Akonadi::SpecialCollections::collectionsChanged
void collectionsChanged(const Akonadi::AgentInstance &instance)
Emitted when the special collections for a resource have been changed (for example, some become available, or some become unavailable).
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:19 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal