akonadi
favoritecollectionsmodel.cpp
00001 /* 00002 Copyright (c) 2009 Kevin Ottens <ervin@kde.org> 00003 00004 00005 This library is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU Library General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or (at your 00008 option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, but WITHOUT 00011 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to the 00017 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00018 02110-1301, USA. 00019 */ 00020 00021 #include "favoritecollectionsmodel.h" 00022 00023 #include <QtGui/QItemSelectionModel> 00024 #include <QtCore/QMimeData> 00025 00026 #include <kconfiggroup.h> 00027 #include <klocale.h> 00028 00029 #include "entitytreemodel.h" 00030 00031 using namespace Akonadi; 00032 00036 class FavoriteCollectionsModel::Private 00037 { 00038 public: 00039 Private( const KConfigGroup &group, FavoriteCollectionsModel *parent ) 00040 : q( parent ), configGroup( group ) 00041 { 00042 } 00043 00044 QString labelForCollection( Collection::Id collectionId ) const 00045 { 00046 if ( labelMap.contains( collectionId ) ) { 00047 return labelMap[ collectionId ]; 00048 } 00049 00050 const QModelIndex collectionIdx = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) ); 00051 00052 QString accountName; 00053 00054 const QString nameOfCollection = collectionIdx.data().toString(); 00055 00056 QModelIndex idx = collectionIdx.parent(); 00057 while ( idx != QModelIndex() ) { 00058 accountName = idx.data().toString(); 00059 idx = idx.parent(); 00060 } 00061 00062 if ( accountName.isEmpty() ) 00063 return nameOfCollection; 00064 else 00065 return nameOfCollection + QLatin1String( " (" ) + accountName + QLatin1Char( ')' ); 00066 } 00067 00068 void clearAndUpdateSelection() 00069 { 00070 q->selectionModel()->clear(); 00071 updateSelection(); 00072 } 00073 00074 void updateSelection() 00075 { 00076 foreach ( const Collection::Id &collectionId, collectionIds ) { 00077 const QModelIndex index = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) ); 00078 00079 if ( !index.isValid() ) 00080 continue; 00081 00082 q->selectionModel()->select( index, QItemSelectionModel::Select ); 00083 } 00084 } 00085 00086 void loadConfig() 00087 { 00088 collectionIds = configGroup.readEntry( "FavoriteCollectionIds", QList<qint64>() ); 00089 const QStringList labels = configGroup.readEntry( "FavoriteCollectionLabels", QStringList() ); 00090 00091 for ( int i = 0; i < collectionIds.size(); ++i ) { 00092 if ( i<labels.size() ) { 00093 labelMap[ collectionIds[i] ] = labels[i]; 00094 } 00095 } 00096 } 00097 00098 void saveConfig() 00099 { 00100 QStringList labels; 00101 00102 foreach ( const Collection::Id &collectionId, collectionIds ) { 00103 labels << labelForCollection( collectionId ); 00104 } 00105 00106 configGroup.writeEntry( "FavoriteCollectionIds", collectionIds ); 00107 configGroup.writeEntry( "FavoriteCollectionLabels", labels ); 00108 configGroup.config()->sync(); 00109 } 00110 00111 FavoriteCollectionsModel * const q; 00112 00113 QList<Collection::Id> collectionIds; 00114 QHash<qint64, QString> labelMap; 00115 KConfigGroup configGroup; 00116 }; 00117 00118 FavoriteCollectionsModel::FavoriteCollectionsModel( QAbstractItemModel *source, const KConfigGroup &group, QObject *parent ) 00119 : Akonadi::SelectionProxyModel( new QItemSelectionModel( source, parent ), parent ), 00120 d( new Private( group, this ) ) 00121 { 00122 setSourceModel( source ); 00123 setFilterBehavior( ExactSelection ); 00124 00125 connect( source, SIGNAL( modelReset() ), this, SLOT( clearAndUpdateSelection() ) ); 00126 connect( source, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), this, SLOT( updateSelection() ) ); 00127 00128 d->loadConfig(); 00129 d->clearAndUpdateSelection(); 00130 } 00131 00132 FavoriteCollectionsModel::~FavoriteCollectionsModel() 00133 { 00134 delete d; 00135 } 00136 00137 void FavoriteCollectionsModel::setCollections( const Collection::List &collections ) 00138 { 00139 d->collectionIds.clear(); 00140 foreach(const Collection &col, collections) { 00141 d->collectionIds << col.id(); 00142 } 00143 d->labelMap.clear(); 00144 d->clearAndUpdateSelection(); 00145 d->saveConfig(); 00146 } 00147 00148 void FavoriteCollectionsModel::addCollection( const Collection &collection ) 00149 { 00150 d->collectionIds << collection.id(); 00151 d->updateSelection(); 00152 d->saveConfig(); 00153 } 00154 00155 void FavoriteCollectionsModel::removeCollection( const Collection &collection ) 00156 { 00157 d->collectionIds.removeAll( collection.id() ); 00158 d->labelMap.remove( collection.id() ); 00159 00160 const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection ); 00161 00162 if ( !idx.isValid() ) 00163 return; 00164 00165 selectionModel()->select( idx, 00166 QItemSelectionModel::Deselect ); 00167 00168 d->updateSelection(); 00169 d->saveConfig(); 00170 } 00171 00172 Akonadi::Collection::List FavoriteCollectionsModel::collections() const 00173 { 00174 Collection::List cols; 00175 foreach (const Collection::Id &colId, d->collectionIds) 00176 cols << Collection(colId); 00177 return cols; 00178 } 00179 00180 QList<Collection::Id> FavoriteCollectionsModel::collectionIds() const 00181 { 00182 return d->collectionIds; 00183 } 00184 00185 void Akonadi::FavoriteCollectionsModel::setFavoriteLabel( const Collection &collection, const QString &label ) 00186 { 00187 Q_ASSERT( d->collectionIds.contains( collection.id() ) ); 00188 d->labelMap[ collection.id() ] = label; 00189 d->saveConfig(); 00190 00191 const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection ); 00192 00193 if ( !idx.isValid() ) 00194 return; 00195 00196 const QModelIndex index = mapFromSource( idx ); 00197 emit dataChanged( index, index ); 00198 } 00199 00200 QVariant Akonadi::FavoriteCollectionsModel::data( const QModelIndex &index, int role ) const 00201 { 00202 if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) { 00203 const QModelIndex sourceIndex = mapToSource( index ); 00204 const Collection::Id collectionId = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionIdRole ).toLongLong(); 00205 00206 return d->labelForCollection( collectionId ); 00207 } else { 00208 return KSelectionProxyModel::data( index, role ); 00209 } 00210 } 00211 00212 bool FavoriteCollectionsModel::setData(const QModelIndex& index, const QVariant& value, int role) 00213 { 00214 if ( index.isValid() && index.column() == 0 && role == Qt::EditRole ) { 00215 const QString newLabel = value.toString(); 00216 if ( newLabel.isEmpty() ) 00217 return false; 00218 const QModelIndex sourceIndex = mapToSource( index ); 00219 const Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>(); 00220 setFavoriteLabel( collection, newLabel ); 00221 return true; 00222 } 00223 return Akonadi::SelectionProxyModel::setData(index, value, role); 00224 } 00225 00226 QString Akonadi::FavoriteCollectionsModel::favoriteLabel( const Akonadi::Collection & collection ) 00227 { 00228 if ( !collection.isValid() ) 00229 return QString(); 00230 return d->labelForCollection( collection.id() ); 00231 } 00232 00233 QVariant FavoriteCollectionsModel::headerData( int section, Qt::Orientation orientation, int role ) const 00234 { 00235 if ( section == 0 00236 && orientation == Qt::Horizontal 00237 && role == Qt::DisplayRole ) { 00238 return i18n( "Favorite Folders" ); 00239 } else { 00240 return KSelectionProxyModel::headerData( section, orientation, role ); 00241 } 00242 } 00243 00244 bool FavoriteCollectionsModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) 00245 { 00246 Q_UNUSED( action ); 00247 Q_UNUSED( row ); 00248 Q_UNUSED( column ); 00249 Q_UNUSED( parent ); 00250 if ( data->hasFormat( QLatin1String( "text/uri-list" ) ) ) { 00251 const KUrl::List urls = KUrl::List::fromMimeData( data ); 00252 foreach ( const KUrl &url, urls ) { 00253 const Collection col = Collection::fromUrl( url ); 00254 if ( col.isValid() ) 00255 addCollection( col ); 00256 } 00257 return true; 00258 } 00259 return false; 00260 } 00261 00262 QStringList FavoriteCollectionsModel::mimeTypes() const 00263 { 00264 QStringList mts = Akonadi::SelectionProxyModel::mimeTypes(); 00265 if ( !mts.contains( QLatin1String( "text/uri-list" ) ) ) 00266 mts.append( QLatin1String( "text/uri-list" ) ); 00267 return mts; 00268 } 00269 00270 Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex& index) const 00271 { 00272 Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags( index ); 00273 if ( !index.isValid() ) 00274 fs |= Qt::ItemIsDropEnabled; 00275 return fs; 00276 } 00277 00278 #include "favoritecollectionsmodel.moc"