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

akonadi

subscriptionmodel.cpp

00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@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 #include "subscriptionmodel.h"
00021 #include "collectionfetchjob.h"
00022 #include "collectionutils_p.h"
00023 
00024 #include <kdebug.h>
00025 
00026 #include <QtCore/QStringList>
00027 
00028 using namespace Akonadi;
00029 
00033 class SubscriptionModel::Private
00034 {
00035   public:
00036     Private( SubscriptionModel* parent ) : q( parent ) {}
00037     SubscriptionModel* q;
00038     QHash<Collection::Id, bool> subscriptions;
00039     QSet<Collection::Id> changes;
00040 
00041     Collection::List changedSubscriptions( bool subscribed )
00042     {
00043       Collection::List list;
00044       foreach ( Collection::Id id, changes ) {
00045         if ( subscriptions.value( id ) == subscribed )
00046           list << Collection( id );
00047       }
00048       return list;
00049     }
00050 
00051     void listResult( KJob* job )
00052     {
00053       if ( job->error() ) {
00054         // TODO
00055         kWarning() << job->errorString();
00056         return;
00057       }
00058       Collection::List cols = static_cast<CollectionFetchJob*>( job )->collections();
00059       foreach( const Collection &col, cols )
00060         if ( !CollectionUtils::isStructural( col ) )
00061           subscriptions[ col.id() ] = true;
00062       q->reset();
00063       emit q->loaded();
00064     }
00065 
00066     bool isSubscribable( Collection::Id id )
00067     {
00068       Collection col = q->collectionForId( id );
00069       if ( CollectionUtils::isVirtualParent( col ) || CollectionUtils::isStructural( col ) )
00070         return false;
00071       if ( col.contentMimeTypes().isEmpty() )
00072         return false;
00073       return true;
00074     }
00075 };
00076 
00077 SubscriptionModel::SubscriptionModel(QObject * parent) :
00078     CollectionModel( parent ),
00079     d( new Private( this ) )
00080 {
00081   includeUnsubscribed();
00082   CollectionFetchJob* job = new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive, this );
00083   connect( job, SIGNAL(result(KJob*)), this, SLOT(listResult(KJob*)) );
00084 }
00085 
00086 SubscriptionModel::~ SubscriptionModel()
00087 {
00088   delete d;
00089 }
00090 
00091 QVariant SubscriptionModel::data(const QModelIndex & index, int role) const
00092 {
00093   switch ( role ) {
00094     case Qt::CheckStateRole:
00095     {
00096       const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
00097       if ( !d->isSubscribable( col ) )
00098         return QVariant();
00099       if ( d->subscriptions.value( col ) )
00100         return Qt::Checked;
00101       return Qt::Unchecked;
00102     }
00103     case SubscriptionChangedRole:
00104     {
00105       const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
00106       if ( d->changes.contains( col ) )
00107         return true;
00108       return false;
00109     }
00110   }
00111   return CollectionModel::data( index, role );
00112 }
00113 
00114 Qt::ItemFlags SubscriptionModel::flags(const QModelIndex & index) const
00115 {
00116   Qt::ItemFlags flags = CollectionModel::flags( index );
00117   if ( d->isSubscribable( index.data( CollectionIdRole ).toLongLong() ) )
00118     return flags | Qt::ItemIsUserCheckable;
00119   return flags;
00120 }
00121 
00122 bool SubscriptionModel::setData(const QModelIndex & index, const QVariant & value, int role)
00123 {
00124   if ( role == Qt::CheckStateRole ) {
00125     const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
00126     if ( d->subscriptions.contains( col ) && d->subscriptions.value( col ) == (value == Qt::Checked) )
00127       return true; // no change
00128     d->subscriptions[ col ] = value == Qt::Checked;
00129     if ( d->changes.contains( col ) )
00130       d->changes.remove( col );
00131     else
00132       d->changes.insert( col );
00133     emit dataChanged( index, index );
00134     return true;
00135   }
00136   return CollectionModel::setData( index, value, role );
00137 }
00138 
00139 Collection::List SubscriptionModel::subscribed() const
00140 {
00141   return d->changedSubscriptions( true );
00142 }
00143 
00144 Collection::List SubscriptionModel::unsubscribed() const
00145 {
00146   return d->changedSubscriptions( false );
00147 }
00148 
00149 #include "subscriptionmodel.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
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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