• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 #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  if ( col.hasAttribute<EntityDisplayAttribute>() &&
91  !col.attribute<EntityDisplayAttribute>()->displayName().isEmpty() ) {
92  return col.attribute<EntityDisplayAttribute>()->displayName();
93  }
94  return col.name();
95  }
96 
97  switch ( role ) {
98  case Qt::DecorationRole:
99  if ( index.column() == 0 ) {
100  if ( col.hasAttribute<EntityDisplayAttribute>() &&
101  !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() ) {
102  return col.attribute<EntityDisplayAttribute>()->icon();
103  }
104  return KIcon( CollectionUtils::defaultIconName( col ) );
105  }
106  break;
107  case OldCollectionIdRole: // fall-through
108  case CollectionIdRole:
109  return col.id();
110  case OldCollectionRole: // fall-through
111  case CollectionRole:
112  return QVariant::fromValue( col );
113  }
114  return QVariant();
115 }
116 
117 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
118 {
119  Q_D( const CollectionModel );
120  if ( column >= columnCount() || column < 0 ) {
121  return QModelIndex();
122  }
123 
124  QVector<Collection::Id> list;
125  if ( !parent.isValid() ) {
126  list = d->childCollections.value( Collection::root().id() );
127  } else {
128  if ( parent.column() > 0 ) {
129  return QModelIndex();
130  }
131  list = d->childCollections.value( parent.internalId() );
132  }
133 
134  if ( row < 0 || row >= list.size() ) {
135  return QModelIndex();
136  }
137  if ( !d->collections.contains( list.at( row ) ) ) {
138  return QModelIndex();
139  }
140  return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
141 }
142 
143 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
144 {
145  Q_D( const CollectionModel );
146  if ( !index.isValid() ) {
147  return QModelIndex();
148  }
149 
150  Collection col = d->collections.value( index.internalId() );
151  if ( !col.isValid() ) {
152  return QModelIndex();
153  }
154 
155 
156  Collection parentCol = d->collections.value( col.parentCollection().id() );
157  if ( !parentCol.isValid() ) {
158  return QModelIndex();
159  }
160  QVector<Collection::Id> list;
161  list = d->childCollections.value( parentCol.parentCollection().id() );
162 
163  int parentRow = list.indexOf( parentCol.id() );
164  if ( parentRow < 0 ) {
165  return QModelIndex();
166  }
167 
168  return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
169 }
170 
171 int CollectionModel::rowCount( const QModelIndex & parent ) const
172 {
173  const Q_D( CollectionModel );
174  QVector<Collection::Id> list;
175  if ( parent.isValid() ) {
176  list = d->childCollections.value( parent.internalId() );
177  } else {
178  list = d->childCollections.value( Collection::root().id() );
179  }
180 
181  return list.size();
182 }
183 
184 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
185 {
186  const Q_D( CollectionModel );
187 
188  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
189  return d->headerContent;
190  }
191  return QAbstractItemModel::headerData( section, orientation, role );
192 }
193 
194 bool CollectionModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role )
195 {
196  Q_D( CollectionModel );
197 
198  if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) {
199  d->headerContent = value.toString();
200  return true;
201  }
202 
203  return false;
204 }
205 
206 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
207 {
208  Q_D( CollectionModel );
209  if ( index.column() == 0 && role == Qt::EditRole ) {
210  // rename collection
211  Collection col = d->collections.value( index.internalId() );
212  if ( !col.isValid() || value.toString().isEmpty() ) {
213  return false;
214  }
215  col.setName( value.toString() );
216  CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
217  connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
218  return true;
219  }
220  return QAbstractItemModel::setData( index, value, role );
221 }
222 
223 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
224 {
225  Q_D( const CollectionModel );
226 
227  // Pass modeltest.
228  // http://labs.trolltech.com/forums/topic/79
229  if ( !index.isValid() ) {
230  return 0;
231  }
232 
233  Qt::ItemFlags flags = QAbstractItemModel::flags( index );
234 
235  flags = flags | Qt::ItemIsDragEnabled;
236 
237  Collection col;
238  if ( index.isValid() ) {
239  col = d->collections.value( index.internalId() );
240  Q_ASSERT( col.isValid() );
241  } else {
242  return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
243  }
244 
245  if ( col.isValid() ) {
246  if ( col.rights() & ( Collection::CanChangeCollection |
247  Collection::CanCreateCollection |
248  Collection::CanDeleteCollection |
249  Collection::CanCreateItem ) ) {
250  if ( index.column() == 0 ) {
251  flags = flags | Qt::ItemIsEditable;
252  }
253  flags = flags | Qt::ItemIsDropEnabled;
254  }
255  }
256 
257  return flags;
258 }
259 
260 Qt::DropActions CollectionModel::supportedDropActions() const
261 {
262  return Qt::CopyAction | Qt::MoveAction;
263 }
264 
265 QStringList CollectionModel::mimeTypes() const
266 {
267  return QStringList() << QLatin1String( "text/uri-list" );
268 }
269 
270 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
271 {
272  QMimeData *data = new QMimeData();
273  KUrl::List urls;
274  foreach ( const QModelIndex &index, indexes ) {
275  if ( index.column() != 0 ) {
276  continue;
277  }
278 
279  urls << Collection( index.internalId() ).url();
280  }
281  urls.populateMimeData( data );
282 
283  return data;
284 }
285 
286 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
287 {
288  Q_D( CollectionModel );
289  if ( !( action & supportedDropActions() ) ) {
290  return false;
291  }
292 
293  // handle drops onto items as well as drops between items
294  QModelIndex idx;
295  if ( row >= 0 && column >= 0 ) {
296  idx = index( row, column, parent );
297  } else {
298  idx = parent;
299  }
300 
301  if ( !idx.isValid() ) {
302  return false;
303  }
304 
305  const Collection parentCol = d->collections.value( idx.internalId() );
306  if ( !parentCol.isValid() ) {
307  return false;
308  }
309 
310  KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
311  connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
312  return true;
313 }
314 
315 Collection CollectionModel::collectionForId(Collection::Id id) const
316 {
317  Q_D( const CollectionModel );
318  return d->collections.value( id );
319 }
320 
321 void CollectionModel::fetchCollectionStatistics(bool enable)
322 {
323  Q_D( CollectionModel );
324  d->fetchStatistics = enable;
325  d->monitor->fetchCollectionStatistics( enable );
326 }
327 
328 void CollectionModel::includeUnsubscribed(bool include)
329 {
330  Q_D( CollectionModel );
331  d->unsubscribed = include;
332 }
333 
334 
335 
336 #include "moc_collectionmodel.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:33 by doxygen 1.8.3.1 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.10.5 API Reference

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