Akonadi Mime

messagemodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "messagemodel.h"
8 #include "messageparts.h"
9 
10 #include <Akonadi/ItemFetchScope>
11 #include <Akonadi/Monitor>
12 #include <Akonadi/Session>
13 
14 #include <KMime/KMimeMessage>
15 
16 #include <KLocalizedString>
17 
18 #include <KFormat>
19 #include <QLocale>
20 
21 #include <array>
22 #include <ki18n_version.h>
23 #if KI18N_VERSION >= QT_VERSION_CHECK(5, 89, 0)
24 #include <KLazyLocalizedString>
25 #undef I18NC_NOOP
26 #define I18NC_NOOP kli18nc
27 #endif
28 
30 
31 using namespace Akonadi;
32 
33 namespace
34 {
35 struct HeaderName {
36 #if KI18N_VERSION < QT_VERSION_CHECK(5, 89, 0)
37  const char *desc;
38  const char *name;
39 #else
40  const KLazyLocalizedString desc;
41 #endif
42 };
43 constexpr std::array<HeaderName, 5> headers = {{{I18NC_NOOP("@title:column, message (e.g. email) subject", "Subject")},
44  {I18NC_NOOP("@title:column, sender of message (e.g. email)", "Sender")},
45  {I18NC_NOOP("@title:column, receiver of message (e.g. email)", "Receiver")},
46  {I18NC_NOOP("@title:column, message (e.g. email) timestamp", "Date")},
47  {I18NC_NOOP("@title:column, message (e.g. email) size", "Size")}}};
48 
49 }
50 
52  : EntityTreeModel(monitor, parent)
53 {
54  // Make sure we have an envelope
57 }
58 
59 int MessageModel::entityColumnCount(HeaderGroup group) const
60 {
61  if (group == EntityTreeModel::ItemListHeaders) {
62  return headers.size();
63  }
64 
65  return EntityTreeModel::entityColumnCount(group);
66 }
67 
68 QVariant MessageModel::entityData(const Item &item, int column, int role) const
69 {
70  if (!item.hasPayload<KMime::Message::Ptr>()) {
71  return {};
72  }
73 
74  const auto msg = item.payload<KMime::Message::Ptr>();
75  if (role == Qt::DisplayRole) {
76  switch (column) {
77  case Subject:
78  if (msg->subject(false)) {
79  return msg->subject(false)->asUnicodeString();
80  } else {
81  return i18nc("@label Alternative text when email subject is missing", "(No subject)");
82  }
83  case Sender:
84  if (msg->from(false)) {
85  return msg->from(false)->asUnicodeString();
86  } else {
87  return i18nc("@label Alternative text when email sender is missing", "(No sender)");
88  }
89  case Receiver:
90  if (msg->to(false)) {
91  return msg->to(false)->asUnicodeString();
92  } else {
93  return i18nc("@label Alternative text when email recipient is missing", "(No receiver)");
94  }
95  case Date:
96  if (msg->date(false)) {
97  return QLocale().toString(msg->date()->dateTime());
98  } else {
99  return i18nc("@label Alternative text when email date/time is missing", "(No date)");
100  }
101  case Size:
102  if (item.size() == 0) {
103  return i18nc("@label No size available", "-");
104  } else {
105  return KFormat().formatByteSize(item.size());
106  }
107  default:
108  return {};
109  }
110  } else if (role == Qt::EditRole) {
111  switch (column) {
112  case Subject:
113  return msg->subject()->asUnicodeString();
114  case Sender:
115  return msg->from()->asUnicodeString();
116  case Receiver:
117  return msg->to()->asUnicodeString();
118  case Date:
119  return msg->date()->dateTime() /*.dateTime()*/;
120  case Size:
121  return item.size();
122  default:
123  return {};
124  }
125  }
126 
127  return EntityTreeModel::entityData(item, column, role);
128 }
129 
130 QVariant MessageModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
131 {
132  if (section >= 0 && static_cast<std::size_t>(section) < headers.size()) {
133 #if KI18N_VERSION < QT_VERSION_CHECK(5, 89, 0)
134  return i18nc(headers[section].desc, headers[section].name);
135 #else
136  return KLocalizedString(headers[section].desc).toString();
137 #endif
138  }
139 
140  return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
141 }
QString toString() const
virtual QVariant entityData(const Item &item, int column, int role=Qt::DisplayRole) const
QString toString(qlonglong i) const const
qint64 size() const
const QLatin1String name
virtual QVariant entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
T payload() const
ItemFetchScope & itemFetchScope()
void setCollectionFetchStrategy(CollectionFetchStrategy strategy)
AKONADI_MIME_EXPORT const char Envelope[]
The part identifier for envelope parts.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
DisplayRole
QString formatByteSize(double size, int precision=1, KFormat::BinaryUnitDialect dialect=KFormat::DefaultBinaryDialect, KFormat::BinarySizeUnits units=KFormat::DefaultBinaryUnits) const
QSharedPointer< Message > Ptr
Orientation
bool hasPayload() const
MessageModel(Monitor *monitor, QObject *parent=nullptr)
Creates a new message model.
void fetchPayloadPart(const QByteArray &part, bool fetch=true)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:01:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.