akonadi
subscriptionmodel.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "subscriptionmodel_p.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
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;
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_p.moc"