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