21 #include "favoritecollectionsmodel.h"
23 #include <QtGui/QItemSelectionModel>
24 #include <QtCore/QMimeData>
26 #include <kconfiggroup.h>
30 #include "entitytreemodel.h"
31 #include "mimetypechecker.h"
32 #include "pastehelper_p.h"
34 using namespace Akonadi;
39 class FavoriteCollectionsModel::Private
43 : q( parent ), configGroup( group )
49 if ( labelMap.contains( collectionId ) ) {
50 return labelMap[ collectionId ];
57 const QString nameOfCollection = collectionIdx.data().toString();
59 QModelIndex idx = collectionIdx.parent();
60 while ( idx != QModelIndex() ) {
61 accountName = idx.data().toString();
65 if ( accountName.isEmpty() )
66 return nameOfCollection;
68 return nameOfCollection + QLatin1String(
" (" ) + accountName + QLatin1Char(
')' );
71 void clearAndUpdateSelection()
73 q->selectionModel()->clear();
81 if ( index.isValid() )
82 q->selectionModel()->select( index, QItemSelectionModel::Select );
85 void updateSelection()
88 updateSelectionId( collectionId );
94 collectionIds = configGroup.readEntry(
"FavoriteCollectionIds", QList<qint64>() );
95 const QStringList labels = configGroup.readEntry(
"FavoriteCollectionLabels", QStringList() );
97 const int numberOfLabels( labels.size() );
98 for (
int i = 0; i < numberOfCollection; ++i ) {
99 if ( i<numberOfLabels ) {
110 labels << labelForCollection( collectionId );
113 configGroup.writeEntry(
"FavoriteCollectionIds",
collectionIds );
114 configGroup.writeEntry(
"FavoriteCollectionLabels", labels );
115 configGroup.config()->sync();
121 QHash<qint64, QString> labelMap;
122 KConfigGroup configGroup;
127 d( new Private( group, this ) )
129 setSourceModel( source );
130 setFilterBehavior( ExactSelection );
133 connect( source, SIGNAL(modelReset()),
this, SLOT(clearAndUpdateSelection()) );
134 connect( source, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
this, SLOT(updateSelection()) );
135 d->updateSelection();
145 d->collectionIds.clear();
147 d->collectionIds << col.
id();
150 d->clearAndUpdateSelection();
156 d->collectionIds << collection.
id();
157 d->updateSelectionId(collection.
id());
163 d->collectionIds.removeAll( collection.
id() );
164 d->labelMap.remove( collection.
id() );
168 if ( !idx.isValid() )
171 selectionModel()->select( idx,
172 QItemSelectionModel::Deselect );
174 d->updateSelection();
191 return d->collectionIds;
196 Q_ASSERT( d->collectionIds.contains( collection.
id() ) );
197 d->labelMap[ collection.
id() ] = label;
202 if ( !idx.isValid() )
205 const QModelIndex index = mapFromSource( idx );
206 emit dataChanged( index, index );
209 QVariant Akonadi::FavoriteCollectionsModel::data(
const QModelIndex &index,
int role )
const
211 if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) {
212 const QModelIndex sourceIndex = mapToSource( index );
215 return d->labelForCollection( collectionId );
217 return KSelectionProxyModel::data( index, role );
221 bool FavoriteCollectionsModel::setData(
const QModelIndex& index,
const QVariant& value,
int role)
223 if ( index.isValid() && index.column() == 0 && role == Qt::EditRole ) {
224 const QString newLabel = value.toString();
225 if ( newLabel.isEmpty() )
227 const QModelIndex sourceIndex = mapToSource( index );
232 return Akonadi::SelectionProxyModel::setData(index, value, role);
239 return d->labelForCollection( collection.
id() );
242 QVariant FavoriteCollectionsModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
245 && orientation == Qt::Horizontal
246 && role == Qt::DisplayRole ) {
247 return i18n(
"Favorite Folders" );
249 return KSelectionProxyModel::headerData( section, orientation, role );
253 bool FavoriteCollectionsModel::dropMimeData(
const QMimeData* data, Qt::DropAction action,
int row,
int column,
const QModelIndex& parent)
258 if ( data->hasFormat( QLatin1String(
"text/uri-list" ) ) ) {
259 const KUrl::List urls = KUrl::List::fromMimeData( data );
261 const QModelIndex sourceIndex = mapToSource( parent );
267 foreach (
const KUrl &url, urls ) {
276 kDebug() <<
"Error: source and destination of move are the same.";
280 if ( !mimeChecker.isWantedItem( item ) ) {
281 kDebug() <<
"unwanted item" << mimeChecker.wantedMimeTypes() << item.
mimeType();
288 connect( job, SIGNAL(result(KJob*)), SLOT(pasteJobDone(KJob*)) );
301 QStringList FavoriteCollectionsModel::mimeTypes()
const
303 QStringList mts = Akonadi::SelectionProxyModel::mimeTypes();
304 if ( !mts.contains( QLatin1String(
"text/uri-list" ) ) )
305 mts.append( QLatin1String(
"text/uri-list" ) );
309 Qt::ItemFlags FavoriteCollectionsModel::flags(
const QModelIndex& index)
const
311 Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags( index );
312 if ( !index.isValid() )
313 fs |= Qt::ItemIsDropEnabled;
317 void FavoriteCollectionsModel::pasteJobDone( KJob *job )
319 if ( job->error() ) {
320 kDebug()<< job->errorString();
324 #include "favoritecollectionsmodel.moc"