20 #include "subscriptionmodel_p.h"
21 #include "collectionfetchjob.h"
22 #include "collectionutils_p.h"
23 #include "specialcollectionattribute_p.h"
25 #include "entityhiddenattribute.h"
29 #include <QtCore/QStringList>
30 #include <QtGui/QFont>
32 using namespace Akonadi;
37 class SubscriptionModel::Private
40 Private( SubscriptionModel* parent ) : q( parent ), showHiddenCollection(false) {}
42 QHash<Collection::Id, bool> subscriptions;
43 QSet<Collection::Id> changes;
44 bool showHiddenCollection;
46 Collection::List changedSubscriptions(
bool subscribed )
48 Collection::List list;
49 foreach ( Collection::Id
id, changes ) {
50 if ( subscriptions.value(
id ) == subscribed )
51 list << Collection(
id );
56 void listResult( KJob* job )
60 kWarning() << job->errorString();
63 Collection::List cols =
static_cast<CollectionFetchJob*
>( job )->collections();
64 foreach (
const Collection &col, cols )
65 if ( !CollectionUtils::isStructural( col ) )
66 subscriptions[ col.id() ] =
true;
71 bool isSubscribable( Collection::Id
id )
73 Collection col = q->collectionForId(
id );
74 if ( CollectionUtils::isStructural( col ) || CollectionUtils::isVirtual( col ) )
76 if ( col.hasAttribute<SpecialCollectionAttribute>() )
78 if ( col.contentMimeTypes().isEmpty() )
84 SubscriptionModel::SubscriptionModel(QObject * parent) :
85 CollectionModel( parent ),
86 d( new Private( this ) )
88 includeUnsubscribed();
89 CollectionFetchJob* job =
new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive,
this );
90 connect( job, SIGNAL(result(KJob*)),
this, SLOT(listResult(KJob*)) );
93 SubscriptionModel::~ SubscriptionModel()
98 QVariant SubscriptionModel::data(
const QModelIndex & index,
int role)
const
101 case Qt::CheckStateRole:
103 const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
104 if ( !d->isSubscribable( col ) )
106 if ( d->subscriptions.value( col ) )
108 return Qt::Unchecked;
110 case SubscriptionChangedRole:
112 const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
113 if ( d->changes.contains( col ) )
119 const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
121 QFont font = CollectionModel::data( index, role ).value<QFont>();
122 font.setBold( d->changes.contains( col ) );
128 if ( role == CollectionIdRole ) {
129 return CollectionModel::data( index, CollectionIdRole );
131 const Collection::Id collectionId = index.data( CollectionIdRole ).toLongLong();
132 const Collection collection = collectionForId( collectionId );
133 if( collection.hasAttribute<EntityHiddenAttribute>() ) {
134 if(d->showHiddenCollection) {
135 return CollectionModel::data( index, role );
140 return CollectionModel::data( index, role );
145 Qt::ItemFlags SubscriptionModel::flags(
const QModelIndex & index)
const
147 Qt::ItemFlags flags = CollectionModel::flags( index );
148 if ( d->isSubscribable( index.data( CollectionIdRole ).toLongLong() ) )
149 return flags | Qt::ItemIsUserCheckable;
153 bool SubscriptionModel::setData(
const QModelIndex & index,
const QVariant & value,
int role)
155 if ( role == Qt::CheckStateRole ) {
156 const Collection::Id col = index.data( CollectionIdRole ).toLongLong();
157 if ( d->subscriptions.contains( col ) && d->subscriptions.value( col ) == (value == Qt::Checked) )
159 d->subscriptions[ col ] = value == Qt::Checked;
160 if ( d->changes.contains( col ) )
161 d->changes.remove( col );
163 d->changes.insert( col );
164 emit dataChanged( index, index );
167 return CollectionModel::setData( index, value, role );
172 return d->changedSubscriptions(
true );
177 return d->changedSubscriptions(
false );
180 void SubscriptionModel::showHiddenCollection(
bool showHidden)
182 d->showHiddenCollection = showHidden;
185 #include "subscriptionmodel_p.moc"