2 Copyright (c) 2008 Bruno Virlet <bvirlet@kdemail.net>
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.
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.
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
20 #include "kcalmodel.h"
22 #include <Akonadi/Collection>
23 #include <Akonadi/Item>
24 #include <Akonadi/ItemFetchScope>
26 #include <KCalCore/FreeBusy>
27 #include <KCalCore/Event>
28 #include <KCalCore/Journal>
29 #include <KCalCore/Todo>
31 #include <KIconLoader>
36 using namespace CalendarSupport
;
38 class KCalModel::Private
41 Private( KCalModel
*model
)
46 static QStringList
allMimeTypes()
49 types
<< KCalCore::Event::eventMimeType()
50 << KCalCore::Todo::todoMimeType()
51 << KCalCore::Journal::journalMimeType()
52 << KCalCore::FreeBusy::freeBusyMimeType();
55 bool collectionMatchesMimeTypes() const
57 Q_FOREACH ( const QString
&type
, allMimeTypes() ) {
58 if ( q
->collection().contentMimeTypes().contains( type
) ) {
65 bool collectionIsValid()
68 !q
->collection().isValid() ||
69 collectionMatchesMimeTypes() ||
70 q
->collection().contentMimeTypes() == QStringList( QLatin1String( "inode/directory" ) );
77 KCalModel::KCalModel( QObject
*parent
)
78 : ItemModel( parent
), d( new Private( this ) )
80 fetchScope().fetchFullPayload();
83 KCalModel::~KCalModel()
88 QStringList
KCalModel::mimeTypes() const
92 << QLatin1String( "text/uri-list" )
96 int KCalModel::columnCount( const QModelIndex
& ) const
98 if ( d
->collectionIsValid() ) {
105 int KCalModel::rowCount( const QModelIndex
& ) const
107 if ( d
->collectionIsValid() ) {
108 return ItemModel::rowCount();
114 QVariant
KCalModel::data( const QModelIndex
&index
, int role
) const
116 if ( role
== ItemModel::IdRole
) {
117 return ItemModel::data( index
, role
);
120 if ( !index
.isValid() || index
.row() >= rowCount() ) {
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( "," ) ) );
135 const Akonadi::Item item
= itemForIndex( index
);
137 if ( !item
.hasPayload
<KCalCore::Incidence::Ptr
>() ) {
141 const KCalCore::Incidence::Ptr incidence
= item
.payload
<KCalCore::Incidence::Ptr
>();
143 // Icon for the model entry
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" ) );
154 return SmallIcon( QLatin1String( "network-wired" ) );
158 case Qt::DisplayRole
:
159 switch( index
.column() ) {
161 return incidence
->summary();
164 return incidence
->dtStart().toString();
167 return incidence
->dateTime( KCalCore::Incidence::RoleEnd
).toString();
170 return incidence
->type();
184 QVariant
KCalModel::headerData( int section
, Qt::Orientation orientation
, int role
) const
186 if ( !d
->collectionIsValid() ) {
190 if ( role
== Qt::DisplayRole
&& orientation
== Qt::Horizontal
) {
193 return i18nc( "@title:column, calendar event summary", "Summary" );
195 return i18nc( "@title:column, calendar event start date and time", "Start date and time" );
197 return i18nc( "@title:column, calendar event end date and time", "End date and time" );
199 return i18nc( "@title:column, calendar event type", "Type" );
205 return Akonadi::ItemModel::headerData( section
, orientation
, role
);