2
* This file is part of the Akonadi Mail example.
4
* Copyright 2009 Stephen Kelly <steveire@gmail.com>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#include "mailmodel.h"
24
#include <kmime/kmime_message.h>
26
#include <boost/shared_ptr.hpp>
30
typedef boost::shared_ptr<KMime::Message> MessagePtr;
32
using namespace Akonadi;
34
class MailModelPrivate
37
MailModelPrivate(MailModel *model)
40
m_collectionHeaders << QLatin1String( "Folder" ) << QLatin1String( "Count" );
41
m_itemHeaders << QLatin1String( "Subject" ) << QLatin1String( "From" ) << QLatin1String( "Date" );
43
Q_DECLARE_PUBLIC(MailModel)
46
QStringList m_itemHeaders;
47
QStringList m_collectionHeaders;
51
MailModel::MailModel( ChangeRecorder *monitor, QObject *parent)
52
: EntityTreeModel(monitor, parent), d_ptr(new MailModelPrivate(this))
57
MailModel::~MailModel()
62
QVariant MailModel::entityData(const Item &item, int column, int role) const
64
if (!item.hasPayload<MessagePtr>())
66
kWarning() << "Not a message" << item.id() << item.remoteId() << item.mimeType();
69
const MessagePtr mail = item.payload<MessagePtr>();
70
if (role == Qt::DisplayRole)
75
return mail->subject()->asUnicodeString();
77
return mail->from()->asUnicodeString();
79
return mail->date()->asUnicodeString();
81
} else if (role == Qt::ToolTipRole)
84
d.append(QString::fromLatin1("Subject: %1\n").arg(mail->subject()->asUnicodeString()));
85
d.append(QString::fromLatin1("From: %1\n").arg(mail->from()->asUnicodeString()));
86
d.append(QString::fromLatin1("Date: %1\n").arg(mail->date()->asUnicodeString()));
89
return EntityTreeModel::entityData(item, column, role);
92
QVariant MailModel::entityData(const Collection &collection, int column, int role) const
94
if (role == Qt::DisplayRole)
99
return EntityTreeModel::entityData(collection, column, role);
102
const QModelIndex index = EntityTreeModel::modelIndexForCollection( this, collection );
103
Q_ASSERT( index.isValid() );
104
return rowCount(index );
107
// Return a QString to pass modeltest.
109
// return QVariant();
112
return EntityTreeModel::entityData(collection, column, role);
115
int MailModel::columnCount(const QModelIndex &index) const
121
QVariant MailModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
123
Q_D(const MailModel);
125
if (orientation == Qt::Horizontal)
127
if (headerGroup == EntityTreeModel::CollectionTreeHeaders)
129
if (role == Qt::DisplayRole)
131
if (section >= d->m_collectionHeaders.size() )
133
return d->m_collectionHeaders.at(section);
135
} else if (headerGroup == EntityTreeModel::ItemListHeaders)
137
if (role == Qt::DisplayRole)
139
if (section >= d->m_itemHeaders.size() )
141
return d->m_itemHeaders.at(section);
145
return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);