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

akonadi

  • akonadi
collectionmodel.cpp
1 /*
2  Copyright (c) 2006 - 2008 Volker Krause <vkrause@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 "collectionmodel.h"
21 #include "collectionmodel_p.h"
22 
23 #include "collectionutils_p.h"
24 #include "collectionmodifyjob.h"
25 #include "entitydisplayattribute.h"
26 #include "monitor.h"
27 #include "pastehelper_p.h"
28 #include "session.h"
29 
30 #include <kdebug.h>
31 #include <kurl.h>
32 #include <kicon.h>
33 
34 #include <QtCore/QMimeData>
35 
36 using namespace Akonadi;
37 
38 CollectionModel::CollectionModel( QObject * parent ) :
39  QAbstractItemModel( parent ),
40  d_ptr( new CollectionModelPrivate( this ) )
41 {
42  Q_D( CollectionModel );
43  d->init();
44 }
45 
46 //@cond PRIVATE
47 CollectionModel::CollectionModel( CollectionModelPrivate *d,
48  QObject *parent )
49  : QAbstractItemModel( parent ),
50  d_ptr( d )
51 {
52  d->init();
53 }
54 //@endcond
55 
56 CollectionModel::~CollectionModel()
57 {
58  Q_D( CollectionModel );
59  d->childCollections.clear();
60  d->collections.clear();
61 
62  delete d->monitor;
63  d->monitor = 0;
64 
65  delete d;
66 }
67 
68 int CollectionModel::columnCount( const QModelIndex & parent ) const
69 {
70  if ( parent.isValid() && parent.column() != 0 ) {
71  return 0;
72  }
73  return 1;
74 }
75 
76 QVariant CollectionModel::data( const QModelIndex & index, int role ) const
77 {
78  Q_D( const CollectionModel );
79  if ( !index.isValid() ) {
80  return QVariant();
81  }
82 
83  const Collection col = d->collections.value( index.internalId() );
84  if ( !col.isValid() ) {
85  return QVariant();
86  }
87 
88  if ( index.column() == 0 && ( role == Qt::DisplayRole || role == Qt::EditRole ) ) {
89  return col.displayName();
90  }
91 
92  switch ( role ) {
93  case Qt::DecorationRole:
94  if ( index.column() == 0 ) {
95  if ( col.hasAttribute<EntityDisplayAttribute>() &&
96  !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() ) {
97  return col.attribute<EntityDisplayAttribute>()->icon();
98  }
99  return KIcon( CollectionUtils::defaultIconName( col ) );
100  }
101  break;
102  case OldCollectionIdRole: // fall-through
103  case CollectionIdRole:
104  return col.id();
105  case OldCollectionRole: // fall-through
106  case CollectionRole:
107  return QVariant::fromValue( col );
108  }
109  return QVariant();
110 }
111 
112 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
113 {
114  Q_D( const CollectionModel );
115  if ( column >= columnCount() || column < 0 ) {
116  return QModelIndex();
117  }
118 
119  QVector<Collection::Id> list;
120  if ( !parent.isValid() ) {
121  list = d->childCollections.value( Collection::root().id() );
122  } else {
123  if ( parent.column() > 0 ) {
124  return QModelIndex();
125  }
126  list = d->childCollections.value( parent.internalId() );
127  }
128 
129  if ( row < 0 || row >= list.size() ) {
130  return QModelIndex();
131  }
132  if ( !d->collections.contains( list.at( row ) ) ) {
133  return QModelIndex();
134  }
135  return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
136 }
137 
138 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
139 {
140  Q_D( const CollectionModel );
141  if ( !index.isValid() ) {
142  return QModelIndex();
143  }
144 
145  const Collection col = d->collections.value( index.internalId() );
146  if ( !col.isValid() ) {
147  return QModelIndex();
148  }
149 
150  const Collection parentCol = d->collections.value( col.parentCollection().id() );
151  if ( !parentCol.isValid() ) {
152  return QModelIndex();
153  }
154  QVector<Collection::Id> list;
155  list = d->childCollections.value( parentCol.parentCollection().id() );
156 
157  int parentRow = list.indexOf( parentCol.id() );
158  if ( parentRow < 0 ) {
159  return QModelIndex();
160  }
161 
162  return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
163 }
164 
165 int CollectionModel::rowCount( const QModelIndex & parent ) const
166 {
167  const Q_D( CollectionModel );
168  QVector<Collection::Id> list;
169  if ( parent.isValid() ) {
170  list = d->childCollections.value( parent.internalId() );
171  } else {
172  list = d->childCollections.value( Collection::root().id() );
173  }
174 
175  return list.size();
176 }
177 
178 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
179 {
180  const Q_D( CollectionModel );
181 
182  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
183  return d->headerContent;
184  }
185  return QAbstractItemModel::headerData( section, orientation, role );
186 }
187 
188 bool CollectionModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role )
189 {
190  Q_D( CollectionModel );
191 
192  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) {
193  d->headerContent = value.toString();
194  return true;
195  }
196 
197  return false;
198 }
199 
200 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
201 {
202  Q_D( CollectionModel );
203  if ( index.column() == 0 && role == Qt::EditRole ) {
204  // rename collection
205  Collection col = d->collections.value( index.internalId() );
206  if ( !col.isValid() || value.toString().isEmpty() ) {
207  return false;
208  }
209  col.setName( value.toString() );
210  CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
211  connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
212  return true;
213  }
214  return QAbstractItemModel::setData( index, value, role );
215 }
216 
217 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
218 {
219  Q_D( const CollectionModel );
220 
221  // Pass modeltest.
222  if ( !index.isValid() ) {
223  return 0;
224  }
225 
226  Qt::ItemFlags flags = QAbstractItemModel::flags( index );
227 
228  flags = flags | Qt::ItemIsDragEnabled;
229 
230  Collection col;
231  if ( index.isValid() ) {
232  col = d->collections.value( index.internalId() );
233  Q_ASSERT( col.isValid() );
234  } else {
235  return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
236  }
237 
238  if ( col.isValid() ) {
239  if ( col.rights() & ( Collection::CanChangeCollection |
240  Collection::CanCreateCollection |
241  Collection::CanDeleteCollection |
242  Collection::CanCreateItem ) ) {
243  if ( index.column() == 0 ) {
244  flags = flags | Qt::ItemIsEditable;
245  }
246  flags = flags | Qt::ItemIsDropEnabled;
247  }
248  }
249 
250  return flags;
251 }
252 
253 Qt::DropActions CollectionModel::supportedDropActions() const
254 {
255  return Qt::CopyAction | Qt::MoveAction;
256 }
257 
258 QStringList CollectionModel::mimeTypes() const
259 {
260  return QStringList() << QLatin1String( "text/uri-list" );
261 }
262 
263 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
264 {
265  QMimeData *data = new QMimeData();
266  KUrl::List urls;
267  foreach ( const QModelIndex &index, indexes ) {
268  if ( index.column() != 0 ) {
269  continue;
270  }
271 
272  urls << Collection( index.internalId() ).url();
273  }
274  urls.populateMimeData( data );
275 
276  return data;
277 }
278 
279 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
280 {
281  Q_D( CollectionModel );
282  if ( !( action & supportedDropActions() ) ) {
283  return false;
284  }
285 
286  // handle drops onto items as well as drops between items
287  QModelIndex idx;
288  if ( row >= 0 && column >= 0 ) {
289  idx = index( row, column, parent );
290  } else {
291  idx = parent;
292  }
293 
294  if ( !idx.isValid() ) {
295  return false;
296  }
297 
298  const Collection parentCol = d->collections.value( idx.internalId() );
299  if ( !parentCol.isValid() ) {
300  return false;
301  }
302 
303  KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
304  connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
305  return true;
306 }
307 
308 Collection CollectionModel::collectionForId(Collection::Id id) const
309 {
310  Q_D( const CollectionModel );
311  return d->collections.value( id );
312 }
313 
314 void CollectionModel::fetchCollectionStatistics(bool enable)
315 {
316  Q_D( CollectionModel );
317  d->fetchStatistics = enable;
318  d->monitor->fetchCollectionStatistics( enable );
319 }
320 
321 void CollectionModel::includeUnsubscribed(bool include)
322 {
323  Q_D( CollectionModel );
324  d->unsubscribed = include;
325 }
326 
327 #include "moc_collectionmodel.cpp"
Akonadi::CollectionModifyJob
Job that modifies a collection in the Akonadi storage.
Definition: collectionmodifyjob.h:82
Akonadi::Monitor::fetchCollectionStatistics
void fetchCollectionStatistics(bool enable)
Enables automatic fetching of changed collection statistics information from the Akonadi storage...
Definition: monitor.cpp:227
Akonadi::CollectionModelPrivate
Definition: collectionmodel_p.h:44
Akonadi::Collection::displayName
QString displayName() const
Returns the display name (EntityDisplayAttribute::displayName()) if set, and Collection::name() other...
Definition: collection.cpp:86
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::PasteHelper::paste
KJob * paste(const QMimeData *mimeData, const Collection &collection, bool copy=true, Session *session=0)
Paste/drop the given mime data into the given collection.
Definition: pastehelper.cpp:91
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
Akonadi::Collection::CanCreateCollection
Can create new subcollections in this collection.
Definition: collection.h:92
Akonadi::Collection::setName
void setName(const QString &name)
Sets the i18n'ed name of the collection.
Definition: collection.cpp:93
Akonadi::CollectionModel::OldCollectionRole
The actual collection object. For binary compatibility to <4.3.
Definition: collectionmodel.h:64
Akonadi::Entity::attribute
Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition: entity.cpp:165
Akonadi::CollectionModel::collectionForId
Collection collectionForId(Collection::Id id) const
Returns the collection for a given collection id.
Definition: collectionmodel.cpp:308
Akonadi::Entity::parentCollection
Collection parentCollection() const
Returns the parent collection of this object.
Definition: entity.cpp:186
Akonadi::CollectionModel::fetchCollectionStatistics
void fetchCollectionStatistics(bool enable)
Sets whether collection statistics information shall be provided by the model.
Definition: collectionmodel.cpp:314
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::Collection::CanDeleteCollection
Can delete this collection.
Definition: collection.h:93
Akonadi::Entity::id
Id id() const
Returns the unique identifier of the entity.
Definition: entity.cpp:72
Akonadi::Collection::rights
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:99
Akonadi::CollectionModel::includeUnsubscribed
void includeUnsubscribed(bool include=true)
Sets whether unsubscribed collections shall be listed in the model.
Definition: collectionmodel.cpp:321
Akonadi::CollectionModel::~CollectionModel
virtual ~CollectionModel()
Destroys the collection model.
Definition: collectionmodel.cpp:56
Akonadi::CollectionModel::CollectionRole
The actual collection object.
Definition: collectionmodel.h:66
Akonadi::CollectionModel::CollectionModel
CollectionModel(QObject *parent=0)
Creates a new collection model.
Definition: collectionmodel.cpp:38
Akonadi::Entity::hasAttribute
bool hasAttribute(const QByteArray &name) const
Returns true if the entity has an attribute of the given type name, false otherwise.
Definition: entity.cpp:146
Akonadi::CollectionModel::OldCollectionIdRole
The collection identifier. For binary compatibility to <4.3.
Definition: collectionmodel.h:63
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::CollectionModel::CollectionIdRole
The collection identifier.
Definition: collectionmodel.h:65
Akonadi::CollectionModel
A model for collections.
Definition: collectionmodel.h:54
Akonadi::EntityDisplayAttribute
Attribute that stores the properties that are used to display an entity.
Definition: entitydisplayattribute.h:39
Akonadi::Collection::CanChangeCollection
Can change this collection.
Definition: collection.h:91
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:51 by doxygen 1.8.6 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.13.3 API Reference

Skip menu "kdepimlibs-4.13.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