Build with clang.
[kdepim.git] / akonadiconsole / akonadibrowsermodel.cpp
blob8809554c366660c2237bec8c916eccd18a3e91c8
1 /*
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
17 02110-1301, USA.
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
38 public:
39 virtual ~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
49 public:
50 GenericState()
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)
60 return QVariant();
62 switch (column)
64 case 0:
65 return item.id();
66 case 1:
67 return item.remoteId();
68 case 2:
69 return item.mimeType();
71 return QVariant();
77 class MailState : public AkonadiBrowserModel::State
79 public:
80 MailState()
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)
90 return QVariant();
92 if (!item.hasPayload<MessagePtr>())
94 return QVariant();
96 const MessagePtr mail = item.payload<MessagePtr>();
98 switch (column)
100 case 0:
101 return mail->subject()->asUnicodeString();
102 case 1:
103 return mail->from()->asUnicodeString();
104 case 2:
105 return mail->date()->asUnicodeString();
108 return QVariant();
113 class ContactsState : public AkonadiBrowserModel::State
115 public:
116 ContactsState()
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)
126 return QVariant();
128 if ( !item.hasPayload<KABC::Addressee>() && !item.hasPayload<KABC::ContactGroup>() )
130 return QVariant();
133 if ( item.hasPayload<KABC::Addressee>() )
135 const KABC::Addressee addr = item.payload<KABC::Addressee>();
137 switch (column)
139 case 0:
140 return addr.givenName();
141 case 1:
142 return addr.familyName();
143 case 2:
144 return addr.preferredEmail();
146 return QVariant();
148 if ( item.hasPayload<KABC::ContactGroup>() ) {
150 switch (column)
152 case 0:
153 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
154 return group.name();
156 return QVariant();
158 return QVariant();
162 class CalendarState : public AkonadiBrowserModel::State
164 public:
165 CalendarState()
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)
175 return QVariant();
177 if ( !item.hasPayload<IncidencePtr>() )
179 return QVariant();
181 const IncidencePtr incidence = item.payload<IncidencePtr>();
182 switch (column)
184 case 0:
185 return incidence->summary();
186 break;
187 case 1:
188 return incidence->dtStart().toString();
189 break;
190 case 2:
191 return incidence->dateTime( KCalCore::Incidence::RoleEnd ).toString();
192 break;
193 case 3:
194 return incidence->typeStr();
195 break;
196 default:
197 break;
199 return QVariant();
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() )
221 if ( column < 1 )
222 return EntityTreeModel::entityData( item, column, role );
223 return QString();
226 return var;
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
252 if ( section < 0 )
253 return QVariant();
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() )
263 return QVariant();
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() )
271 return QVariant();
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 )
285 beginResetModel();
286 m_itemDisplayMode = itemDisplayMode;
287 switch (itemDisplayMode)
289 case MailMode:
290 m_currentState = m_mailState;
291 break;
292 case ContactsMode:
293 m_currentState = m_contactsState;
294 break;
295 case CalendarMode:
296 m_currentState = m_calendarState;
297 break;
298 case GenericMode:
299 default:
300 m_currentState = m_genericState;
301 break;
303 endResetModel();