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 #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
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;
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"