• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 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 <QItemSelectionModel>
24 #include <QtCore/QMimeData>
25 
26 #include <kconfiggroup.h>
27 #include <klocale.h>
28 #include <klocalizedstring.h>
29 #include <KJob>
30 #include <KUrl>
31 
32 #include "entitytreemodel.h"
33 #include "mimetypechecker.h"
34 #include "pastehelper_p.h"
35 
36 using namespace Akonadi;
37 
41 class FavoriteCollectionsModel::Private
42 {
43  public:
44  Private( const KConfigGroup &group, FavoriteCollectionsModel *parent )
45  : q( parent ), configGroup( group )
46  {
47  }
48 
49  QString labelForCollection( Collection::Id collectionId ) const
50  {
51  if ( labelMap.contains( collectionId ) ) {
52  return labelMap[ collectionId ];
53  }
54 
55  const QModelIndex collectionIdx = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) );
56 
57  QString accountName;
58 
59  const QString nameOfCollection = collectionIdx.data().toString();
60 
61  QModelIndex idx = collectionIdx.parent();
62  while ( idx != QModelIndex() ) {
63  accountName = idx.data().toString();
64  idx = idx.parent();
65  }
66 
67  if ( accountName.isEmpty() ) {
68  return nameOfCollection;
69  } else {
70  return nameOfCollection + QLatin1String( " (" ) + accountName + QLatin1Char( ')' );
71  }
72  }
73 
74  void clearAndUpdateSelection()
75  {
76  q->selectionModel()->clear();
77  updateSelection();
78  }
79 
80  void updateSelectionId( const Collection::Id &collectionId )
81  {
82  const QModelIndex index = EntityTreeModel::modelIndexForCollection( q->sourceModel(), Collection( collectionId ) );
83 
84  if ( index.isValid() ) {
85  q->selectionModel()->select( index, QItemSelectionModel::Select );
86  }
87  }
88 
89  void updateSelection()
90  {
91  foreach ( const Collection::Id &collectionId, collectionIds ) {
92  updateSelectionId( collectionId );
93  }
94  }
95 
96  void loadConfig()
97  {
98  collectionIds = configGroup.readEntry( "FavoriteCollectionIds", QList<qint64>() );
99  const QStringList labels = configGroup.readEntry( "FavoriteCollectionLabels", QStringList() );
100  const int numberOfCollection( collectionIds.size() );
101  const int numberOfLabels( labels.size() );
102  for ( int i = 0; i < numberOfCollection; ++i ) {
103  if ( i<numberOfLabels ) {
104  labelMap[ collectionIds[i] ] = labels[i];
105  }
106  }
107  }
108 
109  void saveConfig()
110  {
111  QStringList labels;
112 
113  foreach ( const Collection::Id &collectionId, collectionIds ) {
114  labels << labelForCollection( collectionId );
115  }
116 
117  configGroup.writeEntry( "FavoriteCollectionIds", collectionIds );
118  configGroup.writeEntry( "FavoriteCollectionLabels", labels );
119  configGroup.config()->sync();
120  }
121 
122  FavoriteCollectionsModel * const q;
123 
124  QList<Collection::Id> collectionIds;
125  QHash<qint64, QString> labelMap;
126  KConfigGroup configGroup;
127 };
128 
129 FavoriteCollectionsModel::FavoriteCollectionsModel( QAbstractItemModel *source, const KConfigGroup &group, QObject *parent )
130  : Akonadi::SelectionProxyModel( new QItemSelectionModel( source, parent ), parent ),
131  d( new Private( group, this ) )
132 {
133  setSourceModel( source );
134  setFilterBehavior( ExactSelection );
135 
136  d->loadConfig();
137  connect( source, SIGNAL(modelReset()), this, SLOT(clearAndUpdateSelection()) );
138  connect( source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(updateSelection()) );
139  d->updateSelection();
140 }
141 
142 FavoriteCollectionsModel::~FavoriteCollectionsModel()
143 {
144  delete d;
145 }
146 
147 void FavoriteCollectionsModel::setCollections( const Collection::List &collections )
148 {
149  d->collectionIds.clear();
150  foreach ( const Collection &col, collections ) {
151  d->collectionIds << col.id();
152  }
153  d->labelMap.clear();
154  d->clearAndUpdateSelection();
155  d->saveConfig();
156 }
157 
158 void FavoriteCollectionsModel::addCollection( const Collection &collection )
159 {
160  d->collectionIds << collection.id();
161  d->updateSelectionId( collection.id() );
162  d->saveConfig();
163 }
164 
165 void FavoriteCollectionsModel::removeCollection( const Collection &collection )
166 {
167  d->collectionIds.removeAll( collection.id() );
168  d->labelMap.remove( collection.id() );
169 
170  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection );
171 
172  if ( !idx.isValid() ) {
173  return;
174  }
175 
176  selectionModel()->select( idx,
177  QItemSelectionModel::Deselect );
178 
179  d->updateSelection();
180  d->saveConfig();
181 }
182 
183 Akonadi::Collection::List FavoriteCollectionsModel::collections() const
184 {
185  Collection::List cols;
186  foreach ( const Collection::Id &colId, d->collectionIds ) {
187  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), Collection( colId ) );
188  const Collection collection = sourceModel()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>();
189  cols << collection;
190  }
191  return cols;
192 }
193 
194 QList<Collection::Id> FavoriteCollectionsModel::collectionIds() const
195 {
196  return d->collectionIds;
197 }
198 
199 void Akonadi::FavoriteCollectionsModel::setFavoriteLabel( const Collection &collection, const QString &label )
200 {
201  Q_ASSERT( d->collectionIds.contains( collection.id() ) );
202  d->labelMap[ collection.id() ] = label;
203  d->saveConfig();
204 
205  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( sourceModel(), collection );
206 
207  if ( !idx.isValid() ) {
208  return;
209  }
210 
211  const QModelIndex index = mapFromSource( idx );
212  emit dataChanged( index, index );
213 }
214 
215 QVariant Akonadi::FavoriteCollectionsModel::data( const QModelIndex &index, int role ) const
216 {
217  if ( index.column() == 0 &&
218  ( role == Qt::DisplayRole ||
219  role == Qt::EditRole ) ) {
220  const QModelIndex sourceIndex = mapToSource( index );
221  const Collection::Id collectionId = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionIdRole ).toLongLong();
222 
223  return d->labelForCollection( collectionId );
224  } else {
225  return KSelectionProxyModel::data( index, role );
226  }
227 }
228 
229 bool FavoriteCollectionsModel::setData(const QModelIndex& index, const QVariant& value, int role)
230 {
231  if ( index.isValid() && index.column() == 0 &&
232  role == Qt::EditRole ) {
233  const QString newLabel = value.toString();
234  if ( newLabel.isEmpty() ) {
235  return false;
236  }
237  const QModelIndex sourceIndex = mapToSource( index );
238  const Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
239  setFavoriteLabel( collection, newLabel );
240  return true;
241  }
242  return Akonadi::SelectionProxyModel::setData( index, value, role );
243 }
244 
245 QString Akonadi::FavoriteCollectionsModel::favoriteLabel( const Akonadi::Collection & collection )
246 {
247  if ( !collection.isValid() ) {
248  return QString();
249  }
250  return d->labelForCollection( collection.id() );
251 }
252 
253 QVariant FavoriteCollectionsModel::headerData( int section, Qt::Orientation orientation, int role ) const
254 {
255  if ( section == 0 &&
256  orientation == Qt::Horizontal &&
257  role == Qt::DisplayRole ) {
258  return i18n( "Favorite Folders" );
259  } else {
260  return KSelectionProxyModel::headerData( section, orientation, role );
261  }
262 }
263 
264 bool FavoriteCollectionsModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
265 {
266  Q_UNUSED( action );
267  Q_UNUSED( row );
268  Q_UNUSED( column );
269  if ( data->hasFormat( QLatin1String( "text/uri-list" ) ) ) {
270  const KUrl::List urls = KUrl::List::fromMimeData( data );
271 
272  const QModelIndex sourceIndex = mapToSource( parent );
273  const Collection destCollection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
274 
275  MimeTypeChecker mimeChecker;
276  mimeChecker.setWantedMimeTypes( destCollection.contentMimeTypes() );
277 
278  foreach ( const KUrl &url, urls ) {
279  const Collection col = Collection::fromUrl( url );
280  if ( col.isValid() ) {
281  addCollection( col );
282  } else {
283  const Item item = Item::fromUrl( url );
284  if ( item.isValid() ) {
285  if ( item.parentCollection().id() == destCollection.id() &&
286  action != Qt::CopyAction ) {
287  kDebug() << "Error: source and destination of move are the same.";
288  return false;
289  }
290 #if 0
291  if ( !mimeChecker.isWantedItem( item ) ) {
292  kDebug() << "unwanted item" << mimeChecker.wantedMimeTypes() << item.mimeType();
293  return false;
294  }
295 #endif
296  KJob *job = PasteHelper::pasteUriList( data, destCollection, action );
297  if ( !job ) {
298  return false;
299  }
300  connect( job, SIGNAL(result(KJob*)), SLOT(pasteJobDone(KJob*)) );
301  // Accept the event so that it doesn't propagate.
302  return true;
303 
304  }
305  }
306 
307  }
308  return true;
309  }
310  return false;
311 }
312 
313 QStringList FavoriteCollectionsModel::mimeTypes() const
314 {
315  QStringList mts = Akonadi::SelectionProxyModel::mimeTypes();
316  if ( !mts.contains( QLatin1String( "text/uri-list" ) ) ) {
317  mts.append( QLatin1String( "text/uri-list" ) );
318  }
319  return mts;
320 }
321 
322 Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex& index) const
323 {
324  Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags( index );
325  if ( !index.isValid() ) {
326  fs |= Qt::ItemIsDropEnabled;
327  }
328  return fs;
329 }
330 
331 void FavoriteCollectionsModel::pasteJobDone( KJob *job )
332 {
333  if ( job->error() ) {
334  kDebug() << job->errorString();
335  }
336 }
337 
338 #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:147
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:64
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:129
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:199
Akonadi::FavoriteCollectionsModel::addCollection
void addCollection(const Collection &collection)
Adds a collection to the list of favorite collections.
Definition: favoritecollectionsmodel.cpp:158
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:1191
Akonadi::FavoriteCollectionsModel::collections
Collection::List collections() const
Returns the list of favorite collections.
Definition: favoritecollectionsmodel.cpp:183
Akonadi::FavoriteCollectionsModel::removeCollection
void removeCollection(const Collection &collection)
Removes a collection from the list of favorite collections.
Definition: favoritecollectionsmodel.cpp:165
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:245
Akonadi::FavoriteCollectionsModel::~FavoriteCollectionsModel
virtual ~FavoriteCollectionsModel()
Destroys the favorite collections model.
Definition: favoritecollectionsmodel.cpp:142
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:194
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-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:17 by doxygen 1.8.5 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.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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