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

akonadi

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

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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