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