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

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