2 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
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
21 #include "akonadibrowsermodel.h"
23 #include <kmime/kmime_message.h>
25 #include <kabc/addressee.h>
26 #include <kabc/contactgroup.h>
28 #include <kcalcore/incidence.h>
29 #include <kcalcore/event.h>
31 #include <boost/shared_ptr.hpp>
33 typedef boost::shared_ptr
<KMime::Message
> MessagePtr
;
34 typedef QSharedPointer
<KCalCore::Incidence
> IncidencePtr
;
36 class AkonadiBrowserModel::State
40 QStringList m_collectionHeaders
;
41 QStringList m_itemHeaders
;
43 virtual QVariant
entityData( const Item
&item
, int column
, int role
) const = 0;
47 class GenericState
: public AkonadiBrowserModel::State
52 m_collectionHeaders
<< "Collection";
53 m_itemHeaders
<< "Id" << "Remote Id" << "MimeType";
55 virtual ~GenericState() {}
57 QVariant
entityData( const Item
&item
, int column
, int role
) const
59 if (Qt::DisplayRole
!= role
)
67 return item
.remoteId();
69 return item
.mimeType();
77 class MailState
: public AkonadiBrowserModel::State
82 m_collectionHeaders
<< "Collection";
83 m_itemHeaders
<< "Subject" << "Sender" << "Date";
85 virtual ~MailState() {}
87 QVariant
entityData( const Item
&item
, int column
, int role
) const
89 if (Qt::DisplayRole
!= role
)
92 if (!item
.hasPayload
<MessagePtr
>())
96 const MessagePtr mail
= item
.payload
<MessagePtr
>();
101 return mail
->subject()->asUnicodeString();
103 return mail
->from()->asUnicodeString();
105 return mail
->date()->asUnicodeString();
113 class ContactsState
: public AkonadiBrowserModel::State
118 m_collectionHeaders
<< "Collection";
119 m_itemHeaders
<< "Given Name" << "Family Name" << "Email";
121 virtual ~ContactsState() {}
123 QVariant
entityData( const Item
&item
, int column
, int role
) const
125 if (Qt::DisplayRole
!= role
)
128 if ( !item
.hasPayload
<KABC::Addressee
>() && !item
.hasPayload
<KABC::ContactGroup
>() )
133 if ( item
.hasPayload
<KABC::Addressee
>() )
135 const KABC::Addressee addr
= item
.payload
<KABC::Addressee
>();
140 return addr
.givenName();
142 return addr
.familyName();
144 return addr
.preferredEmail();
148 if ( item
.hasPayload
<KABC::ContactGroup
>() ) {
153 const KABC::ContactGroup group
= item
.payload
<KABC::ContactGroup
>();
162 class CalendarState
: public AkonadiBrowserModel::State
167 m_collectionHeaders
<< "Collection";
168 m_itemHeaders
<< "Summary" << "DateTime start" << "DateTime End" << "Type";
170 virtual ~CalendarState() {}
172 QVariant
entityData( const Item
&item
, int column
, int role
) const
174 if (Qt::DisplayRole
!= role
)
177 if ( !item
.hasPayload
<IncidencePtr
>() )
181 const IncidencePtr incidence
= item
.payload
<IncidencePtr
>();
185 return incidence
->summary();
188 return incidence
->dtStart().toString();
191 return incidence
->dateTime( KCalCore::Incidence::RoleEnd
).toString();
194 return incidence
->typeStr();
203 AkonadiBrowserModel::AkonadiBrowserModel( ChangeRecorder
* monitor
, QObject
* parent
)
204 : EntityTreeModel( monitor
, parent
),
205 m_itemDisplayMode( GenericMode
)
208 m_genericState
= new GenericState();
209 m_mailState
= new MailState();
210 m_contactsState
= new ContactsState();
211 m_calendarState
= new CalendarState();
213 m_currentState
= m_genericState
;
216 QVariant
AkonadiBrowserModel::entityData( const Item
&item
, int column
, int role
) const
218 QVariant var
= m_currentState
->entityData( item
, column
, role
);
219 if ( !var
.isValid() )
222 return EntityTreeModel::entityData( item
, column
, role
);
229 QVariant
AkonadiBrowserModel::entityData(const Akonadi::Collection
& collection
, int column
, int role
) const
231 return Akonadi::EntityTreeModel::entityData( collection
, column
, role
);
234 int AkonadiBrowserModel::entityColumnCount( HeaderGroup headerGroup
) const
236 if ( ItemListHeaders
== headerGroup
)
238 return m_currentState
->m_itemHeaders
.size();
241 if ( CollectionTreeHeaders
== headerGroup
)
243 return m_currentState
->m_collectionHeaders
.size();
245 // Practically, this should never happen.
246 return EntityTreeModel::entityColumnCount( headerGroup
);
250 QVariant
AkonadiBrowserModel::entityHeaderData( int section
, Qt::Orientation orientation
, int role
, HeaderGroup headerGroup
) const
255 if ( orientation
== Qt::Vertical
)
256 return EntityTreeModel::entityHeaderData( section
, orientation
, role
, headerGroup
);
258 if ( headerGroup
== EntityTreeModel::CollectionTreeHeaders
)
260 if ( role
== Qt::DisplayRole
)
262 if ( section
>= m_currentState
->m_collectionHeaders
.size() )
264 return m_currentState
->m_collectionHeaders
.at( section
);
266 } else if ( headerGroup
== EntityTreeModel::ItemListHeaders
)
268 if ( role
== Qt::DisplayRole
)
270 if ( section
>= m_currentState
->m_itemHeaders
.size() )
272 return m_currentState
->m_itemHeaders
.at( section
);
275 return EntityTreeModel::entityHeaderData( section
, orientation
, role
, headerGroup
);
278 AkonadiBrowserModel::ItemDisplayMode
AkonadiBrowserModel::itemDisplayMode() const
280 return m_itemDisplayMode
;
283 void AkonadiBrowserModel::setItemDisplayMode( AkonadiBrowserModel::ItemDisplayMode itemDisplayMode
)
286 m_itemDisplayMode
= itemDisplayMode
;
287 switch (itemDisplayMode
)
290 m_currentState
= m_mailState
;
293 m_currentState
= m_contactsState
;
296 m_currentState
= m_calendarState
;
300 m_currentState
= m_genericState
;