• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
favoritecollectionsmodel.cpp
1 /*
2  Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
3 
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "favoritecollectionsmodel.h"
22 
23 #include <QtGui/QItemSelectionModel>
24 #include <QtCore/QMimeData>
25 
26 #include <kconfiggroup.h>
27 #include <klocale.h>
28 #include <KJob>
29 
30 #include "entitytreemodel.h"
31 #include "mimetypechecker.h"
32 #include "pastehelper_p.h"
33 
34 using namespace Akonadi;
35 
39 class FavoriteCollectionsModel::Private
40 {
41  public:
42  Private( const KConfigGroup &group, FavoriteCollectionsModel *parent )
43  : q( parent ), configGroup( group )
44  {
45  }
46 
47  QString labelForCollection( Collection::Id collectionId ) const
48  {
49  if ( labelMap.contains( collectionId ) ) {
50  return labelMap[ collectionId ];
51  }
52 
53  const QModelIndex collectionIdx = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) );
54 
55  QString accountName;
56 
57  const QString nameOfCollection = collectionIdx.data().toString();
58 
59  QModelIndex idx = collectionIdx.parent();
60  while ( idx != QModelIndex() ) {
61  accountName = idx.data().toString();
62  idx = idx.parent();
63  }
64 
65  if ( accountName.isEmpty() )
66  return nameOfCollection;
67  else
68  return nameOfCollection + QLatin1String( " (" ) + accountName + QLatin1Char( ')' );
69  }
70 
71  void clearAndUpdateSelection()
72  {
73  q->selectionModel()->clear();
74  updateSelection();
75  }
76 
77  void updateSelectionId( const Collection::Id &collectionId )
78  {
79  const QModelIndex index = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) );
80 
81  if ( index.isValid() )
82  q->selectionModel()->select( index, QItemSelectionModel::Select );
83  }
84 
85  void updateSelection()
86  {
87  foreach ( const Collection::Id &collectionId, collectionIds ) {
88  updateSelectionId( collectionId );
89  }
90  }
91 
92  void loadConfig()
93  {
94  collectionIds = configGroup.readEntry( "FavoriteCollectionIds", QList<qint64>() );
95  const QStringList labels = configGroup.readEntry( "FavoriteCollectionLabels", QStringList() );
96  const int numberOfCollection( collectionIds.size() );
97  const int numberOfLabels( labels.size() );
98  for ( int i = 0; i < numberOfCollection; ++i ) {
99  if ( i<numberOfLabels ) {
100  labelMap[ collectionIds[i] ] = labels[i];
101  }
102  }
103  }
104 
105  void saveConfig()
106  {
107  QStringList labels;
108 
109  foreach ( const Collection::Id &collectionId, collectionIds ) {
110  labels << labelForCollection( collectionId );
111  }
112 
113  configGroup.writeEntry( "FavoriteCollectionIds", collectionIds );
114  configGroup.writeEntry( "FavoriteCollectionLabels", labels );
115  configGroup.config()->sync();
116  }
117 
118  FavoriteCollectionsModel * const q;
119 
120  QList<Collection::Id> collectionIds;
121  QHash<qint64, QString> labelMap;
122  KConfigGroup configGroup;
123 };
124 
125 FavoriteCollectionsModel::FavoriteCollectionsModel( QAbstractItemModel *source, const KConfigGroup &group, QObject *parent )
126  : Akonadi::SelectionProxyModel( new QItemSelectionModel( source, parent ), parent ),
127  d( new Private( group, this ) )
128 {
129  setSourceModel( source );
130  setFilterBehavior( ExactSelection );
131 
132  d->loadConfig();
133  connect( source, SIGNAL(modelReset()), this, SLOT(clearAndUpdateSelection()) );
134  connect( source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(updateSelection()) );
135  d->updateSelection();
136 }
137 
138 FavoriteCollectionsModel::~FavoriteCollectionsModel()
139 {
140  delete d;
141 }
142 
143 void FavoriteCollectionsModel::setCollections( const Collection::List &collections )
144 {
145  d->collectionIds.clear();
146  foreach(const Collection &col, collections) {
147  d->collectionIds << col.id();
148  }
149  d->labelMap.clear();
150  d->clearAndUpdateSelection();
151  d->saveConfig();
152 }
153 
154 void FavoriteCollectionsModel::addCollection( const Collection &collection )
155 {
156  d->collectionIds << collection.id();
157  d->updateSelectionId(collection.id());
158  d->saveConfig();
159 }
160 
161 void FavoriteCollectionsModel::removeCollection( const Collection &collection )
162 {
163  d->collectionIds.removeAll( collection.id() );
164  d->labelMap.remove( collection.id() );
165 
166  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection );
167 
168  if ( !idx.isValid() )
169  return;
170 
171  selectionModel()->select( idx,
172  QItemSelectionModel::Deselect );
173 
174  d->updateSelection();
175  d->saveConfig();
176 }
177 
178 Akonadi::Collection::List FavoriteCollectionsModel::collections() const
179 {
180  Collection::List cols;
181  foreach (const Collection::Id &colId, d->collectionIds) {
182  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), Collection(colId) );
183  const Collection collection = sourceModel()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>();
184  cols << collection;
185  }
186  return cols;
187 }
188 
189 QList<Collection::Id> FavoriteCollectionsModel::collectionIds() const
190 {
191  return d->collectionIds;
192 }
193 
194 void Akonadi::FavoriteCollectionsModel::setFavoriteLabel( const Collection &collection, const QString &label )
195 {
196  Q_ASSERT( d->collectionIds.contains( collection.id() ) );
197  d->labelMap[ collection.id() ] = label;
198  d->saveConfig();
199 
200  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection );
201 
202  if ( !idx.isValid() )
203  return;
204 
205  const QModelIndex index = mapFromSource( idx );
206  emit dataChanged( index, index );
207 }
208 
209 QVariant Akonadi::FavoriteCollectionsModel::data( const QModelIndex &index, int role ) const
210 {
211  if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) {
212  const QModelIndex sourceIndex = mapToSource( index );
213  const Collection::Id collectionId = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionIdRole ).toLongLong();
214 
215  return d->labelForCollection( collectionId );
216  } else {
217  return KSelectionProxyModel::data( index, role );
218  }
219 }
220 
221 bool FavoriteCollectionsModel::setData(const QModelIndex& index, const QVariant& value, int role)
222 {
223  if ( index.isValid() && index.column() == 0 && role == Qt::EditRole ) {
224  const QString newLabel = value.toString();
225  if ( newLabel.isEmpty() )
226  return false;
227  const QModelIndex sourceIndex = mapToSource( index );
228  const Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
229  setFavoriteLabel( collection, newLabel );
230  return true;
231  }
232  return Akonadi::SelectionProxyModel::setData(index, value, role);
233 }
234 
235 QString Akonadi::FavoriteCollectionsModel::favoriteLabel( const Akonadi::Collection & collection )
236 {
237  if ( !collection.isValid() )
238  return QString();
239  return d->labelForCollection( collection.id() );
240 }
241 
242 QVariant FavoriteCollectionsModel::headerData( int section, Qt::Orientation orientation, int role ) const
243 {
244  if ( section == 0
245  && orientation == Qt::Horizontal
246  && role == Qt::DisplayRole ) {
247  return i18n( "Favorite Folders" );
248  } else {
249  return KSelectionProxyModel::headerData( section, orientation, role );
250  }
251 }
252 
253 bool FavoriteCollectionsModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
254 {
255  Q_UNUSED( action );
256  Q_UNUSED( row );
257  Q_UNUSED( column );
258  if ( data->hasFormat( QLatin1String( "text/uri-list" ) ) ) {
259  const KUrl::List urls = KUrl::List::fromMimeData( data );
260 
261  const QModelIndex sourceIndex = mapToSource( parent );
262  const Collection destCollection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
263 
264  MimeTypeChecker mimeChecker;
265  mimeChecker.setWantedMimeTypes( destCollection.contentMimeTypes() );
266 
267  foreach ( const KUrl &url, urls ) {
268  const Collection col = Collection::fromUrl( url );
269  if ( col.isValid() ) {
270  addCollection( col );
271  } else {
272  const Item item = Item::fromUrl( url );
273  if ( item.isValid() )
274  {
275  if ( item.parentCollection().id() == destCollection.id() && action != Qt::CopyAction ) {
276  kDebug() << "Error: source and destination of move are the same.";
277  return false;
278  }
279 #if 0
280  if ( !mimeChecker.isWantedItem( item ) ) {
281  kDebug() << "unwanted item" << mimeChecker.wantedMimeTypes() << item.mimeType();
282  return false;
283  }
284 #endif
285  KJob *job = PasteHelper::pasteUriList( data, destCollection, action );
286  if ( !job )
287  return false;
288  connect( job, SIGNAL(result(KJob*)), SLOT(pasteJobDone(KJob*)) );
289  // Accept the event so that it doesn't propagate.
290  return true;
291 
292  }
293  }
294 
295  }
296  return true;
297  }
298  return false;
299 }
300 
301 QStringList FavoriteCollectionsModel::mimeTypes() const
302 {
303  QStringList mts = Akonadi::SelectionProxyModel::mimeTypes();
304  if ( !mts.contains( QLatin1String( "text/uri-list" ) ) )
305  mts.append( QLatin1String( "text/uri-list" ) );
306  return mts;
307 }
308 
309 Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex& index) const
310 {
311  Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags( index );
312  if ( !index.isValid() )
313  fs |= Qt::ItemIsDropEnabled;
314  return fs;
315 }
316 
317 void FavoriteCollectionsModel::pasteJobDone( KJob *job )
318 {
319  if ( job->error() ) {
320  kDebug()<< job->errorString();
321  }
322 }
323 
324 #include "favoritecollectionsmodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:04 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal