~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to examples/etm_usage/mailmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Akonadi Mail example.
 
3
 *
 
4
 * Copyright 2009  Stephen Kelly <steveire@gmail.com>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 * 02110-1301  USA
 
20
 */
 
21
 
 
22
#include "mailmodel.h"
 
23
 
 
24
#include <kmime/kmime_message.h>
 
25
 
 
26
#include <boost/shared_ptr.hpp>
 
27
 
 
28
#include <KDebug>
 
29
 
 
30
typedef boost::shared_ptr<KMime::Message> MessagePtr;
 
31
 
 
32
using namespace Akonadi;
 
33
 
 
34
class MailModelPrivate
 
35
{
 
36
public:
 
37
  MailModelPrivate(MailModel *model)
 
38
    : q_ptr(model)
 
39
  {
 
40
    m_collectionHeaders << QLatin1String( "Folder" ) << QLatin1String( "Count" );
 
41
    m_itemHeaders << QLatin1String( "Subject" ) << QLatin1String( "From" ) << QLatin1String( "Date" );
 
42
  }
 
43
  Q_DECLARE_PUBLIC(MailModel)
 
44
  MailModel *q_ptr;
 
45
 
 
46
  QStringList m_itemHeaders;
 
47
  QStringList m_collectionHeaders;
 
48
 
 
49
};
 
50
 
 
51
MailModel::MailModel( ChangeRecorder *monitor, QObject *parent)
 
52
  : EntityTreeModel(monitor, parent), d_ptr(new MailModelPrivate(this))
 
53
{
 
54
 
 
55
}
 
56
 
 
57
MailModel::~MailModel()
 
58
{
 
59
   delete d_ptr;
 
60
}
 
61
 
 
62
QVariant MailModel::entityData(const Item &item, int column, int role) const
 
63
{
 
64
  if (!item.hasPayload<MessagePtr>())
 
65
  {
 
66
    kWarning() << "Not a message" << item.id() << item.remoteId() << item.mimeType();
 
67
    return QVariant();
 
68
  }
 
69
  const MessagePtr mail = item.payload<MessagePtr>();
 
70
  if (role == Qt::DisplayRole)
 
71
  {
 
72
    switch (column)
 
73
    {
 
74
    case 0:
 
75
      return mail->subject()->asUnicodeString();
 
76
    case 1:
 
77
      return mail->from()->asUnicodeString();
 
78
    case 2:
 
79
      return mail->date()->asUnicodeString();
 
80
    }
 
81
  } else if (role == Qt::ToolTipRole)
 
82
  {
 
83
    QString d;
 
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()));
 
87
    return d;
 
88
  }
 
89
  return EntityTreeModel::entityData(item, column, role);
 
90
}
 
91
 
 
92
QVariant MailModel::entityData(const Collection &collection, int column, int role) const
 
93
{
 
94
  if (role == Qt::DisplayRole)
 
95
  {
 
96
    switch (column)
 
97
    {
 
98
    case 0:
 
99
      return EntityTreeModel::entityData(collection, column, role);
 
100
    case 1:
 
101
    {
 
102
      const QModelIndex index = EntityTreeModel::modelIndexForCollection( this, collection );
 
103
      Q_ASSERT( index.isValid() );
 
104
      return rowCount(index );
 
105
    }
 
106
    default:
 
107
      // Return a QString to pass modeltest.
 
108
      return QString();
 
109
  //     return QVariant();
 
110
    }
 
111
  }
 
112
  return EntityTreeModel::entityData(collection, column, role);
 
113
}
 
114
 
 
115
int MailModel::columnCount(const QModelIndex &index) const
 
116
{
 
117
  Q_UNUSED(index);
 
118
  return 3;
 
119
}
 
120
 
 
121
QVariant MailModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
 
122
{
 
123
  Q_D(const MailModel);
 
124
 
 
125
  if (orientation == Qt::Horizontal)
 
126
  {
 
127
    if (headerGroup == EntityTreeModel::CollectionTreeHeaders)
 
128
    {
 
129
      if (role == Qt::DisplayRole)
 
130
      {
 
131
        if (section >= d->m_collectionHeaders.size() )
 
132
          return QVariant();
 
133
        return d->m_collectionHeaders.at(section);
 
134
      }
 
135
    } else if (headerGroup == EntityTreeModel::ItemListHeaders)
 
136
    {
 
137
      if (role == Qt::DisplayRole)
 
138
      {
 
139
        if (section >= d->m_itemHeaders.size() )
 
140
          return QVariant();
 
141
        return d->m_itemHeaders.at(section);
 
142
      }
 
143
    }
 
144
  }
 
145
  return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
 
146
}