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 #include <KJob> 00029 00030 #include "entitytreemodel.h" 00031 #include "mimetypechecker.h" 00032 #include "pastehelper_p.h" 00033 00034 using namespace Akonadi; 00035 00039 class FavoriteCollectionsModel::Private 00040 { 00041 public: 00042 Private( const KConfigGroup &group, FavoriteCollectionsModel *parent ) 00043 : q( parent ), configGroup( group ) 00044 { 00045 } 00046 00047 QString labelForCollection( Collection::Id collectionId ) const 00048 { 00049 if ( labelMap.contains( collectionId ) ) { 00050 return labelMap[ collectionId ]; 00051 } 00052 00053 const QModelIndex collectionIdx = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) ); 00054 00055 QString accountName; 00056 00057 const QString nameOfCollection = collectionIdx.data().toString(); 00058 00059 QModelIndex idx = collectionIdx.parent(); 00060 while ( idx != QModelIndex() ) { 00061 accountName = idx.data().toString(); 00062 idx = idx.parent(); 00063 } 00064 00065 if ( accountName.isEmpty() ) 00066 return nameOfCollection; 00067 else 00068 return nameOfCollection + QLatin1String( " (" ) + accountName + QLatin1Char( ')' ); 00069 } 00070 00071 void clearAndUpdateSelection() 00072 { 00073 q->selectionModel()->clear(); 00074 updateSelection(); 00075 } 00076 00077 void updateSelectionId( const Collection::Id &collectionId ) 00078 { 00079 const QModelIndex index = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) ); 00080 00081 if ( index.isValid() ) 00082 q->selectionModel()->select( index, QItemSelectionModel::Select ); 00083 } 00084 00085 void updateSelection() 00086 { 00087 foreach ( const Collection::Id &collectionId, collectionIds ) { 00088 updateSelectionId( collectionId ); 00089 } 00090 } 00091 00092 void loadConfig() 00093 { 00094 collectionIds = configGroup.readEntry( "FavoriteCollectionIds", QList<qint64>() ); 00095 const QStringList labels = configGroup.readEntry( "FavoriteCollectionLabels", QStringList() ); 00096 const int numberOfCollection( collectionIds.size() ); 00097 const int numberOfLabels( labels.size() ); 00098 for ( int i = 0; i < numberOfCollection; ++i ) { 00099 if ( i<numberOfLabels ) { 00100 labelMap[ collectionIds[i] ] = labels[i]; 00101 } 00102 } 00103 } 00104 00105 void saveConfig() 00106 { 00107 QStringList labels; 00108 00109 foreach ( const Collection::Id &collectionId, collectionIds ) { 00110 labels << labelForCollection( collectionId ); 00111 } 00112 00113 configGroup.writeEntry( "FavoriteCollectionIds", collectionIds ); 00114 configGroup.writeEntry( "FavoriteCollectionLabels", labels ); 00115 configGroup.config()->sync(); 00116 } 00117 00118 FavoriteCollectionsModel * const q; 00119 00120 QList<Collection::Id> collectionIds; 00121 QHash<qint64, QString> labelMap; 00122 KConfigGroup configGroup; 00123 }; 00124 00125 FavoriteCollectionsModel::FavoriteCollectionsModel( QAbstractItemModel *source, const KConfigGroup &group, QObject *parent ) 00126 : Akonadi::SelectionProxyModel( new QItemSelectionModel( source, parent ), parent ), 00127 d( new Private( group, this ) ) 00128 { 00129 setSourceModel( source ); 00130 setFilterBehavior( ExactSelection ); 00131 00132 d->loadConfig(); 00133 connect( source, SIGNAL(modelReset()), this, SLOT(clearAndUpdateSelection()) ); 00134 connect( source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(updateSelection()) ); 00135 d->updateSelection(); 00136 } 00137 00138 FavoriteCollectionsModel::~FavoriteCollectionsModel() 00139 { 00140 delete d; 00141 } 00142 00143 void FavoriteCollectionsModel::setCollections( const Collection::List &collections ) 00144 { 00145 d->collectionIds.clear(); 00146 foreach(const Collection &col, collections) { 00147 d->collectionIds << col.id(); 00148 } 00149 d->labelMap.clear(); 00150 d->clearAndUpdateSelection(); 00151 d->saveConfig(); 00152 } 00153 00154 void FavoriteCollectionsModel::addCollection( const Collection &collection ) 00155 { 00156 d->collectionIds << collection.id(); 00157 d->updateSelectionId(collection.id()); 00158 d->saveConfig(); 00159 } 00160 00161 void FavoriteCollectionsModel::removeCollection( const Collection &collection ) 00162 { 00163 d->collectionIds.removeAll( collection.id() ); 00164 d->labelMap.remove( collection.id() ); 00165 00166 const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection ); 00167 00168 if ( !idx.isValid() ) 00169 return; 00170 00171 selectionModel()->select( idx, 00172 QItemSelectionModel::Deselect ); 00173 00174 d->updateSelection(); 00175 d->saveConfig(); 00176 } 00177 00178 Akonadi::Collection::List FavoriteCollectionsModel::collections() const 00179 { 00180 Collection::List cols; 00181 foreach (const Collection::Id &colId, d->collectionIds) { 00182 const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), Collection(colId) ); 00183 const Collection collection = sourceModel()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>(); 00184 cols << collection; 00185 } 00186 return cols; 00187 } 00188 00189 QList<Collection::Id> FavoriteCollectionsModel::collectionIds() const 00190 { 00191 return d->collectionIds; 00192 } 00193 00194 void Akonadi::FavoriteCollectionsModel::setFavoriteLabel( const Collection &collection, const QString &label ) 00195 { 00196 Q_ASSERT( d->collectionIds.contains( collection.id() ) ); 00197 d->labelMap[ collection.id() ] = label; 00198 d->saveConfig(); 00199 00200 const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection ); 00201 00202 if ( !idx.isValid() ) 00203 return; 00204 00205 const QModelIndex index = mapFromSource( idx ); 00206 emit dataChanged( index, index ); 00207 } 00208 00209 QVariant Akonadi::FavoriteCollectionsModel::data( const QModelIndex &index, int role ) const 00210 { 00211 if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) { 00212 const QModelIndex sourceIndex = mapToSource( index ); 00213 const Collection::Id collectionId = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionIdRole ).toLongLong(); 00214 00215 return d->labelForCollection( collectionId ); 00216 } else { 00217 return KSelectionProxyModel::data( index, role ); 00218 } 00219 } 00220 00221 bool FavoriteCollectionsModel::setData(const QModelIndex& index, const QVariant& value, int role) 00222 { 00223 if ( index.isValid() && index.column() == 0 && role == Qt::EditRole ) { 00224 const QString newLabel = value.toString(); 00225 if ( newLabel.isEmpty() ) 00226 return false; 00227 const QModelIndex sourceIndex = mapToSource( index ); 00228 const Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>(); 00229 setFavoriteLabel( collection, newLabel ); 00230 return true; 00231 } 00232 return Akonadi::SelectionProxyModel::setData(index, value, role); 00233 } 00234 00235 QString Akonadi::FavoriteCollectionsModel::favoriteLabel( const Akonadi::Collection & collection ) 00236 { 00237 if ( !collection.isValid() ) 00238 return QString(); 00239 return d->labelForCollection( collection.id() ); 00240 } 00241 00242 QVariant FavoriteCollectionsModel::headerData( int section, Qt::Orientation orientation, int role ) const 00243 { 00244 if ( section == 0 00245 && orientation == Qt::Horizontal 00246 && role == Qt::DisplayRole ) { 00247 return i18n( "Favorite Folders" ); 00248 } else { 00249 return KSelectionProxyModel::headerData( section, orientation, role ); 00250 } 00251 } 00252 00253 bool FavoriteCollectionsModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) 00254 { 00255 Q_UNUSED( action ); 00256 Q_UNUSED( row ); 00257 Q_UNUSED( column ); 00258 if ( data->hasFormat( QLatin1String( "text/uri-list" ) ) ) { 00259 const KUrl::List urls = KUrl::List::fromMimeData( data ); 00260 00261 const QModelIndex sourceIndex = mapToSource( parent ); 00262 const Collection destCollection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>(); 00263 00264 MimeTypeChecker mimeChecker; 00265 mimeChecker.setWantedMimeTypes( destCollection.contentMimeTypes() ); 00266 00267 foreach ( const KUrl &url, urls ) { 00268 const Collection col = Collection::fromUrl( url ); 00269 if ( col.isValid() ) { 00270 addCollection( col ); 00271 } else { 00272 const Item item = Item::fromUrl( url ); 00273 if ( item.isValid() ) 00274 { 00275 if ( item.parentCollection().id() == destCollection.id() && action != Qt::CopyAction ) { 00276 kDebug() << "Error: source and destination of move are the same."; 00277 return false; 00278 } 00279 #if 0 00280 if ( !mimeChecker.isWantedItem( item ) ) { 00281 kDebug() << "unwanted item" << mimeChecker.wantedMimeTypes() << item.mimeType(); 00282 return false; 00283 } 00284 #endif 00285 KJob *job = PasteHelper::pasteUriList( data, destCollection, action ); 00286 if ( !job ) 00287 return false; 00288 connect( job, SIGNAL(result(KJob*)), SLOT(pasteJobDone(KJob*)) ); 00289 // Accept the event so that it doesn't propagate. 00290 return true; 00291 00292 } 00293 } 00294 00295 } 00296 return true; 00297 } 00298 return false; 00299 } 00300 00301 QStringList FavoriteCollectionsModel::mimeTypes() const 00302 { 00303 QStringList mts = Akonadi::SelectionProxyModel::mimeTypes(); 00304 if ( !mts.contains( QLatin1String( "text/uri-list" ) ) ) 00305 mts.append( QLatin1String( "text/uri-list" ) ); 00306 return mts; 00307 } 00308 00309 Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex& index) const 00310 { 00311 Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags( index ); 00312 if ( !index.isValid() ) 00313 fs |= Qt::ItemIsDropEnabled; 00314 return fs; 00315 } 00316 00317 void FavoriteCollectionsModel::pasteJobDone( KJob *job ) 00318 { 00319 if ( job->error() ) { 00320 kDebug()<< job->errorString(); 00321 } 00322 } 00323 00324 #include "favoritecollectionsmodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:22 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:22 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.