• 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
kcalmodel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2008 Bruno Virlet <bvirlet@kdemail.net>
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 "kcalmodel.h"
21 
22 #include <Akonadi/Collection>
23 #include <Akonadi/Item>
24 #include <Akonadi/ItemFetchScope>
25 
26 #include <KCalCore/FreeBusy>
27 #include <KCalCore/Event>
28 #include <KCalCore/Journal>
29 #include <KCalCore/Todo>
30 
31 #include <KIconLoader>
32 #include <KLocale>
33 
34 #include <QPixmap>
35 
36 using namespace CalendarSupport;
37 
38 class KCalModel::Private
39 {
40  public:
41  Private( KCalModel *model )
42  :q( model )
43  {
44  }
45 
46  static QStringList allMimeTypes()
47  {
48  QStringList types;
49  types << KCalCore::Event::eventMimeType()
50  << KCalCore::Todo::todoMimeType()
51  << KCalCore::Journal::journalMimeType()
52  << KCalCore::FreeBusy::freeBusyMimeType();
53  return types;
54  }
55  bool collectionMatchesMimeTypes() const
56  {
57  Q_FOREACH ( const QString &type, allMimeTypes() ) {
58  if ( q->collection().contentMimeTypes().contains( type ) ) {
59  return true;
60  }
61  }
62  return false;
63  }
64 
65  bool collectionIsValid()
66  {
67  return
68  !q->collection().isValid() ||
69  collectionMatchesMimeTypes() ||
70  q->collection().contentMimeTypes() == QStringList( QLatin1String( "inode/directory" ) );
71  }
72 
73  private:
74  KCalModel *q;
75 };
76 
77 KCalModel::KCalModel( QObject *parent )
78  : ItemModel( parent ), d( new Private( this ) )
79 {
80  fetchScope().fetchFullPayload();
81 }
82 
83 KCalModel::~KCalModel()
84 {
85  delete d;
86 }
87 
88 QStringList KCalModel::mimeTypes() const
89 {
90  return
91  QStringList()
92  << QLatin1String( "text/uri-list" )
93  << d->allMimeTypes();
94 }
95 
96 int KCalModel::columnCount( const QModelIndex & ) const
97 {
98  if ( d->collectionIsValid() ) {
99  return 4;
100  } else {
101  return 1;
102  }
103 }
104 
105 int KCalModel::rowCount( const QModelIndex & ) const
106 {
107  if ( d->collectionIsValid() ) {
108  return ItemModel::rowCount();
109  } else {
110  return 1;
111  }
112 }
113 
114 QVariant KCalModel::data( const QModelIndex &index, int role ) const
115 {
116  if ( role == ItemModel::IdRole ) {
117  return ItemModel::data( index, role );
118  }
119 
120  if ( !index.isValid() || index.row() >= rowCount() ) {
121  return QVariant();
122  }
123 
124  // guard against use with collections that do not have the right contents
125  if ( !d->collectionIsValid() ) {
126  if ( role == Qt::DisplayRole ) {
127  return i18nc( "@info",
128  "This model can only handle event, task, journal or free-busy list folders. "
129  "The current collection holds mimetypes: %1",
130  collection().contentMimeTypes().join( QLatin1String( "," ) ) );
131  }
132  return QVariant();
133  }
134 
135  const Akonadi::Item item = itemForIndex( index );
136 
137  if ( !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
138  return QVariant();
139  }
140 
141  const KCalCore::Incidence::Ptr incidence = item.payload<KCalCore::Incidence::Ptr>();
142 
143  // Icon for the model entry
144  switch( role ) {
145  case Qt::DecorationRole:
146  if ( index.column() == 0 ) {
147  if ( incidence->type() == KCalCore::Incidence::TypeTodo ) {
148  return SmallIcon( QLatin1String( "view-pim-tasks" ) );
149  } else if ( incidence->type() == KCalCore::Incidence::TypeJournal ) {
150  return SmallIcon( QLatin1String( "view-pim-journal" ) );
151  } else if ( incidence->type() == KCalCore::Incidence::TypeEvent ) {
152  return SmallIcon( QLatin1String( "view-calendar" ) );
153  } else {
154  return SmallIcon( QLatin1String( "network-wired" ) );
155  }
156  }
157  break;
158  case Qt::DisplayRole:
159  switch( index.column() ) {
160  case Summary:
161  return incidence->summary();
162  break;
163  case DateTimeStart:
164  return incidence->dtStart().toString();
165  break;
166  case DateTimeEnd:
167  return incidence->dateTime( KCalCore::Incidence::RoleEnd ).toString();
168  break;
169  case Type:
170  return incidence->type();
171  break;
172  default:
173  break;
174  }
175  break;
176  default:
177  return QVariant();
178  break;
179  }
180 
181  return QVariant();
182 }
183 
184 QVariant KCalModel::headerData( int section, Qt::Orientation orientation, int role ) const
185 {
186  if ( !d->collectionIsValid() ) {
187  return QVariant();
188  }
189 
190  if ( role == Qt::DisplayRole && orientation == Qt::Horizontal ) {
191  switch( section ) {
192  case Summary:
193  return i18nc( "@title:column, calendar event summary", "Summary" );
194  case DateTimeStart:
195  return i18nc( "@title:column, calendar event start date and time", "Start date and time" );
196  case DateTimeEnd:
197  return i18nc( "@title:column, calendar event end date and time", "End date and time" );
198  case Type:
199  return i18nc( "@title:column, calendar event type", "Type" );
200  default:
201  return QString();
202  }
203  }
204 
205  return Akonadi::ItemModel::headerData( section, orientation, role );
206 }
CalendarSupport::KCalModel::mimeTypes
virtual QStringList mimeTypes() const
Reimplemented from QAbstractItemModel.
Definition: kcalmodel.cpp:88
CalendarSupport::KCalModel::DateTimeStart
Definition: kcalmodel.h:33
QObject
CalendarSupport::KCalModel::~KCalModel
virtual ~KCalModel()
Definition: kcalmodel.cpp:83
CalendarSupport::incidence
CALENDARSUPPORT_EXPORT KCalCore::Incidence::Ptr incidence(const Akonadi::Item &item)
returns the incidence from an akonadi item, or a null pointer if the item has no such payload ...
Definition: utils.cpp:75
CalendarSupport::KCalModel::DateTimeEnd
Definition: kcalmodel.h:34
CalendarSupport::KCalModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: kcalmodel.cpp:184
kcalmodel.h
CalendarSupport::KCalModel::KCalModel
KCalModel(QObject *parent=0)
Definition: kcalmodel.cpp:77
CalendarSupport::KCalModel::columnCount
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: kcalmodel.cpp:96
CalendarSupport::KCalModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: kcalmodel.cpp:105
CalendarSupport::KCalModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kcalmodel.cpp:114
CalendarSupport::KCalModel::Type
Definition: kcalmodel.h:35
CalendarSupport::KCalModel::Summary
Definition: kcalmodel.h:32
CalendarSupport::KCalModel
Definition: kcalmodel.h:28
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