• 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
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 #include <QPixmap>
36 
37 using namespace Akonadi;
38 
39 CollectionModel::CollectionModel( QObject * parent ) :
40  QAbstractItemModel( parent ),
41  d_ptr( new CollectionModelPrivate( this ) )
42 {
43  Q_D( CollectionModel );
44  d->init();
45 }
46 
47 //@cond PRIVATE
48 CollectionModel::CollectionModel( CollectionModelPrivate *d,
49  QObject *parent )
50  : QAbstractItemModel( parent ),
51  d_ptr( d )
52 {
53  d->init();
54 }
55 //@endcond
56 
57 CollectionModel::~CollectionModel()
58 {
59  Q_D( CollectionModel );
60  d->childCollections.clear();
61  d->collections.clear();
62 
63  delete d->monitor;
64  d->monitor = 0;
65 
66  delete d;
67 }
68 
69 int CollectionModel::columnCount( const QModelIndex & parent ) const
70 {
71  if ( parent.isValid() && parent.column() != 0 ) {
72  return 0;
73  }
74  return 1;
75 }
76 
77 QVariant CollectionModel::data( const QModelIndex & index, int role ) const
78 {
79  Q_D( const CollectionModel );
80  if ( !index.isValid() ) {
81  return QVariant();
82  }
83 
84  const Collection col = d->collections.value( index.internalId() );
85  if ( !col.isValid() ) {
86  return QVariant();
87  }
88 
89  if ( index.column() == 0 && ( role == Qt::DisplayRole || role == Qt::EditRole ) ) {
90  return col.displayName();
91  }
92 
93  switch ( role ) {
94  case Qt::DecorationRole:
95  if ( index.column() == 0 ) {
96  if ( col.hasAttribute<EntityDisplayAttribute>() &&
97  !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() ) {
98  return col.attribute<EntityDisplayAttribute>()->icon();
99  }
100  return KIcon( CollectionUtils::defaultIconName( col ) );
101  }
102  break;
103  case OldCollectionIdRole: // fall-through
104  case CollectionIdRole:
105  return col.id();
106  case OldCollectionRole: // fall-through
107  case CollectionRole:
108  return QVariant::fromValue( col );
109  }
110  return QVariant();
111 }
112 
113 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
114 {
115  Q_D( const CollectionModel );
116  if ( column >= columnCount() || column < 0 ) {
117  return QModelIndex();
118  }
119 
120  QVector<Collection::Id> list;
121  if ( !parent.isValid() ) {
122  list = d->childCollections.value( Collection::root().id() );
123  } else {
124  if ( parent.column() > 0 ) {
125  return QModelIndex();
126  }
127  list = d->childCollections.value( parent.internalId() );
128  }
129 
130  if ( row < 0 || row >= list.size() ) {
131  return QModelIndex();
132  }
133  if ( !d->collections.contains( list.at( row ) ) ) {
134  return QModelIndex();
135  }
136  return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
137 }
138 
139 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
140 {
141  Q_D( const CollectionModel );
142  if ( !index.isValid() ) {
143  return QModelIndex();
144  }
145 
146  const Collection col = d->collections.value( index.internalId() );
147  if ( !col.isValid() ) {
148  return QModelIndex();
149  }
150 
151  const Collection parentCol = d->collections.value( col.parentCollection().id() );
152  if ( !parentCol.isValid() ) {
153  return QModelIndex();
154  }
155  QVector<Collection::Id> list;
156  list = d->childCollections.value( parentCol.parentCollection().id() );
157 
158  int parentRow = list.indexOf( parentCol.id() );
159  if ( parentRow < 0 ) {
160  return QModelIndex();
161  }
162 
163  return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
164 }
165 
166 int CollectionModel::rowCount( const QModelIndex & parent ) const
167 {
168  const Q_D( CollectionModel );
169  QVector<Collection::Id> list;
170  if ( parent.isValid() ) {
171  list = d->childCollections.value( parent.internalId() );
172  } else {
173  list = d->childCollections.value( Collection::root().id() );
174  }
175 
176  return list.size();
177 }
178 
179 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
180 {
181  const Q_D( CollectionModel );
182 
183  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
184  return d->headerContent;
185  }
186  return QAbstractItemModel::headerData( section, orientation, role );
187 }
188 
189 bool CollectionModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role )
190 {
191  Q_D( CollectionModel );
192 
193  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) {
194  d->headerContent = value.toString();
195  return true;
196  }
197 
198  return false;
199 }
200 
201 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
202 {
203  Q_D( CollectionModel );
204  if ( index.column() == 0 && role == Qt::EditRole ) {
205  // rename collection
206  Collection col = d->collections.value( index.internalId() );
207  if ( !col.isValid() || value.toString().isEmpty() ) {
208  return false;
209  }
210  col.setName( value.toString() );
211  CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
212  connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
213  return true;
214  }
215  return QAbstractItemModel::setData( index, value, role );
216 }
217 
218 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
219 {
220  Q_D( const CollectionModel );
221 
222  // Pass modeltest.
223  if ( !index.isValid() ) {
224  return 0;
225  }
226 
227  Qt::ItemFlags flags = QAbstractItemModel::flags( index );
228 
229  flags = flags | Qt::ItemIsDragEnabled;
230 
231  Collection col;
232  if ( index.isValid() ) {
233  col = d->collections.value( index.internalId() );
234  Q_ASSERT( col.isValid() );
235  } else {
236  return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
237  }
238 
239  if ( col.isValid() ) {
240  if ( col.rights() & ( Collection::CanChangeCollection |
241  Collection::CanCreateCollection |
242  Collection::CanDeleteCollection |
243  Collection::CanCreateItem ) ) {
244  if ( index.column() == 0 ) {
245  flags = flags | Qt::ItemIsEditable;
246  }
247  flags = flags | Qt::ItemIsDropEnabled;
248  }
249  }
250 
251  return flags;
252 }
253 
254 Qt::DropActions CollectionModel::supportedDropActions() const
255 {
256  return Qt::CopyAction | Qt::MoveAction;
257 }
258 
259 QStringList CollectionModel::mimeTypes() const
260 {
261  return QStringList() << QLatin1String( "text/uri-list" );
262 }
263 
264 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
265 {
266  QMimeData *data = new QMimeData();
267  KUrl::List urls;
268  foreach ( const QModelIndex &index, indexes ) {
269  if ( index.column() != 0 ) {
270  continue;
271  }
272 
273  urls << Collection( index.internalId() ).url();
274  }
275  urls.populateMimeData( data );
276 
277  return data;
278 }
279 
280 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
281 {
282  Q_D( CollectionModel );
283  if ( !( action & supportedDropActions() ) ) {
284  return false;
285  }
286 
287  // handle drops onto items as well as drops between items
288  QModelIndex idx;
289  if ( row >= 0 && column >= 0 ) {
290  idx = index( row, column, parent );
291  } else {
292  idx = parent;
293  }
294 
295  if ( !idx.isValid() ) {
296  return false;
297  }
298 
299  const Collection parentCol = d->collections.value( idx.internalId() );
300  if ( !parentCol.isValid() ) {
301  return false;
302  }
303 
304  KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
305  connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
306  return true;
307 }
308 
309 Collection CollectionModel::collectionForId(Collection::Id id) const
310 {
311  Q_D( const CollectionModel );
312  return d->collections.value( id );
313 }
314 
315 void CollectionModel::fetchCollectionStatistics(bool enable)
316 {
317  Q_D( CollectionModel );
318  d->fetchStatistics = enable;
319  d->monitor->fetchCollectionStatistics( enable );
320 }
321 
322 void CollectionModel::includeUnsubscribed(bool include)
323 {
324  Q_D( CollectionModel );
325  d->unsubscribed = include;
326 }
327 
328 #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:185
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:309
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:315
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:322
Akonadi::CollectionModel::~CollectionModel
virtual ~CollectionModel()
Destroys the collection model.
Definition: collectionmodel.cpp:57
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:39
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 Tue Oct 14 2014 23:00:26 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