• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

calendarsupport

  • sources
  • kde-4.12
  • kdepim
  • calendarsupport
incidenceattachmentmodel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3  Author: Stephen Kelly <stephen@kdab.com>
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 "incidenceattachmentmodel.h"
22 
23 #include <Akonadi/EntityTreeModel>
24 #include <Akonadi/ItemFetchJob>
25 #include <Akonadi/ItemFetchScope>
26 #include <Akonadi/Monitor>
27 
28 using namespace CalendarSupport;
29 using namespace Akonadi;
30 
31 namespace CalendarSupport {
32 
33 class IncidenceAttachmentModelPrivate
34 {
35  IncidenceAttachmentModelPrivate( IncidenceAttachmentModel *qq,
36  const QPersistentModelIndex &modelIndex,
37  Akonadi::Item item = Akonadi::Item() )
38  : q_ptr( qq ), m_modelIndex( modelIndex ), m_item( item ), m_monitor( 0 )
39  {
40  if ( modelIndex.isValid() ) {
41  QObject::connect( modelIndex.model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
42  qq, SLOT(resetModel()) );
43  } else if ( item.isValid() ) {
44  createMonitor();
45  resetInternalData();
46  }
47  QHash<int, QByteArray> roleNames = qq->roleNames();
48  roleNames.insert( IncidenceAttachmentModel::MimeTypeRole, "mimeType" );
49  roleNames.insert( IncidenceAttachmentModel::AttachmentUrl, "attachmentUrl" );
50  qq->setRoleNames( roleNames );
51  }
52 
53  void resetModel()
54  {
55  Q_Q( IncidenceAttachmentModel );
56  q->beginResetModel();
57  resetInternalData();
58  q->endResetModel();
59  emit q->rowCountChanged();
60  }
61 
62  void itemFetched( Akonadi::Item::List list )
63  {
64  Q_ASSERT( list.size() == 1 );
65  setItem( list.first() );
66  }
67 
68  void setItem( const Akonadi::Item &item );
69 
70  void createMonitor()
71  {
72  if ( m_monitor ) {
73  return;
74  }
75 
76  m_monitor = new Akonadi::Monitor( q_ptr );
77  m_monitor->setItemMonitored( m_item );
78  m_monitor->itemFetchScope().fetchFullPayload( true );
79  QObject::connect( m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
80  q_ptr, SLOT(resetModel()) );
81  QObject::connect( m_monitor, SIGNAL(itemRemoved(Akonadi::Item)),
82  q_ptr, SLOT(resetModel()) );
83  }
84 
85  void resetInternalData()
86  {
87  if ( m_incidence ) {
88  m_incidence->clearTempFiles();
89  }
90  Item item = m_item;
91  if ( m_modelIndex.isValid() ) {
92  item = m_modelIndex.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
93  }
94 
95  if ( !item.isValid() || !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
96  m_incidence = KCalCore::Incidence::Ptr();
97  return;
98  }
99  m_incidence = item.payload<KCalCore::Incidence::Ptr>();
100  }
101 
102  Q_DECLARE_PUBLIC( IncidenceAttachmentModel )
103  IncidenceAttachmentModel * const q_ptr;
104 
105  QModelIndex m_modelIndex;
106  Akonadi::Item m_item;
107  KCalCore::Incidence::Ptr m_incidence;
108  Akonadi::Monitor *m_monitor;
109 };
110 
111 }
112 
113 IncidenceAttachmentModel::IncidenceAttachmentModel( const QPersistentModelIndex &modelIndex,
114  QObject *parent )
115  : QAbstractListModel( parent ),
116  d_ptr( new IncidenceAttachmentModelPrivate( this, modelIndex ) )
117 {
118 
119 }
120 
121 IncidenceAttachmentModel::IncidenceAttachmentModel( const Akonadi::Item &item, QObject *parent )
122  : QAbstractListModel( parent ),
123  d_ptr( new IncidenceAttachmentModelPrivate( this, QModelIndex(), item ) )
124 {
125 
126 }
127 
128 IncidenceAttachmentModel::IncidenceAttachmentModel( QObject *parent )
129  : QAbstractListModel( parent ),
130  d_ptr( new IncidenceAttachmentModelPrivate( this, QModelIndex() ) )
131 {
132 
133 }
134 
135 IncidenceAttachmentModel::~IncidenceAttachmentModel()
136 {
137  delete d_ptr;
138 }
139 
140 KCalCore::Incidence::Ptr IncidenceAttachmentModel::incidence() const
141 {
142  Q_D( const IncidenceAttachmentModel );
143  return d->m_incidence;
144 }
145 
146 void IncidenceAttachmentModel::setIndex( const QPersistentModelIndex &modelIndex )
147 {
148  Q_D( IncidenceAttachmentModel );
149  beginResetModel();
150  d->m_modelIndex = modelIndex;
151  d->m_item = Akonadi::Item();
152  d->resetInternalData();
153  endResetModel();
154  emit rowCountChanged();
155 }
156 
157 void IncidenceAttachmentModel::setItem( const Akonadi::Item &item )
158 {
159  Q_D( IncidenceAttachmentModel );
160  if ( !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
161  ItemFetchJob *job = new ItemFetchJob( item );
162  job->fetchScope().fetchFullPayload( true );
163  connect( job, SIGNAL(itemsReceived(Akonadi::Item::List)),
164  SLOT(itemFetched(Akonadi::Item::List)) );
165  return;
166  }
167  d->setItem( item );
168 }
169 
170 void IncidenceAttachmentModelPrivate::setItem( const Akonadi::Item &item )
171 {
172  Q_Q( IncidenceAttachmentModel );
173  q->beginResetModel();
174  m_modelIndex = QModelIndex();
175  m_item = item;
176  createMonitor();
177  resetInternalData();
178  q->endResetModel();
179  emit q->rowCountChanged();
180 }
181 
182 int IncidenceAttachmentModel::rowCount( const QModelIndex & ) const
183 {
184  Q_D( const IncidenceAttachmentModel );
185  if ( !d->m_incidence ) {
186  return 0;
187  } else {
188  return d->m_incidence->attachments().size();
189  }
190 }
191 
192 QVariant IncidenceAttachmentModel::data( const QModelIndex &index, int role ) const
193 {
194  Q_D( const IncidenceAttachmentModel );
195  if ( !d->m_incidence ) {
196  return QVariant();
197  }
198 
199  KCalCore::Attachment::Ptr attachment = d->m_incidence->attachments().at( index.row() );
200  switch ( role ) {
201  case Qt::DisplayRole:
202  return attachment->label();
203  case AttachmentDataRole:
204  return attachment->decodedData();
205  case MimeTypeRole:
206  return attachment->mimeType();
207  case AttachmentUrl:
208  return d->m_incidence->writeAttachmentToTempFile( attachment );
209  }
210  return QVariant();
211 }
212 
213 QVariant IncidenceAttachmentModel::headerData( int section,
214  Qt::Orientation orientation,
215  int role ) const
216 {
217  return QAbstractItemModel::headerData( section, orientation, role );
218 }
219 
220 #include "incidenceattachmentmodel.moc"
CalendarSupport::IncidenceAttachmentModel::MimeTypeRole
Definition: incidenceattachmentmodel.h:49
CalendarSupport::IncidenceAttachmentModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: incidenceattachmentmodel.cpp:213
CalendarSupport::IncidenceAttachmentModel::rowCountChanged
void rowCountChanged()
incidenceattachmentmodel.h
QAbstractListModel
QObject
CalendarSupport::IncidenceAttachmentModel
Definition: incidenceattachmentmodel.h:41
CalendarSupport::IncidenceAttachmentModel::incidence
KCalCore::Incidence::Ptr incidence() const
Definition: incidenceattachmentmodel.cpp:140
CalendarSupport::IncidenceAttachmentModel::AttachmentDataRole
Definition: incidenceattachmentmodel.h:48
CalendarSupport::IncidenceAttachmentModel::IncidenceAttachmentModel
IncidenceAttachmentModel(const QPersistentModelIndex &modelIndex, QObject *parent=0)
Definition: incidenceattachmentmodel.cpp:113
CalendarSupport::IncidenceAttachmentModel::setIndex
void setIndex(const QPersistentModelIndex &modelIndex)
Definition: incidenceattachmentmodel.cpp:146
CalendarSupport::IncidenceAttachmentModel::~IncidenceAttachmentModel
~IncidenceAttachmentModel()
Definition: incidenceattachmentmodel.cpp:135
CalendarSupport::IncidenceAttachmentModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: incidenceattachmentmodel.cpp:192
CalendarSupport::IncidenceAttachmentModel::AttachmentUrl
Definition: incidenceattachmentmodel.h:50
CalendarSupport::IncidenceAttachmentModel::setItem
void setItem(const Akonadi::Item &item)
Definition: incidenceattachmentmodel.cpp:157
CalendarSupport::IncidenceAttachmentModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: incidenceattachmentmodel.cpp:182
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

calendarsupport

Skip menu "calendarsupport"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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