• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 bool SpecialCollections::registerCollection( const QByteArray &type, const Collection &collection )
207 {
208  if ( !collection.isValid() ) {
209  kWarning() << "Invalid collection.";
210  return false;
211  }
212 
213  const QString &resourceId = collection.resource();
214  if ( resourceId.isEmpty() ) {
215  kWarning() << "Collection has empty resourceId.";
216  return false;
217  }
218 
219  if ( !collection.hasAttribute<SpecialCollectionAttribute>() || collection.attribute<SpecialCollectionAttribute>()->collectionType() != type ) {
220  Collection attributeCollection( collection );
221  SpecialCollectionAttribute *attribute = attributeCollection.attribute<SpecialCollectionAttribute>( Collection::AddIfMissing );
222  attribute->setCollectionType( type );
223 
224  new CollectionModifyJob( attributeCollection );
225  }
226 
227  const Collection oldCollection = d->mFoldersForResource.value(resourceId).value(type);
228  if (oldCollection != collection) {
229  if (oldCollection.isValid()) {
230  d->mMonitor->setCollectionMonitored(oldCollection, false);
231  }
232  d->mMonitor->setCollectionMonitored( collection, true );
233  d->mFoldersForResource[ resourceId ].insert( type, collection );
234  d->emitChanged( resourceId );
235  }
236 
237  return true;
238 }
239 
240 bool SpecialCollections::hasDefaultCollection( const QByteArray &type ) const
241 {
242  return hasCollection( type, d->defaultResource() );
243 }
244 
245 Akonadi::Collection SpecialCollections::defaultCollection( const QByteArray &type ) const
246 {
247  return collection( type, d->defaultResource() );
248 }
249 
250 #include "moc_specialcollections.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:41 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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