Akonadi Mime

messagemodel.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
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/Message>
15
16#include <KLocalizedString>
17
18#include <KFormat>
19#include <QLocale>
20
21#include <KLazyLocalizedString>
22#include <array>
24
25using namespace Akonadi;
26
27namespace
28{
29struct HeaderName {
30 const KLazyLocalizedString desc;
31};
32constexpr std::array<HeaderName, 5> headers = {{{kli18nc("@title:column, message (e.g. email) subject", "Subject")},
33 {kli18nc("@title:column, sender of message (e.g. email)", "Sender")},
34 {kli18nc("@title:column, receiver of message (e.g. email)", "Receiver")},
35 {kli18nc("@title:column, message (e.g. email) timestamp", "Date")},
36 {kli18nc("@title:column, message (e.g. email) size", "Size")}}};
37}
38
40 : EntityTreeModel(monitor, parent)
41{
42 // Make sure we have an envelope
43 monitor->itemFetchScope().fetchPayloadPart(MessagePart::Envelope);
45}
46
47int MessageModel::entityColumnCount(HeaderGroup group) const
48{
50 return headers.size();
51 }
52
53 return EntityTreeModel::entityColumnCount(group);
54}
55
56QVariant MessageModel::entityData(const Item &item, int column, int role) const
57{
58 if (!item.hasPayload<KMime::Message::Ptr>()) {
59 return {};
60 }
61
62 const auto msg = item.payload<KMime::Message::Ptr>();
63 if (role == Qt::DisplayRole) {
64 switch (column) {
65 case Subject:
66 if (auto s = msg->subject(false)) {
67 return s->asUnicodeString();
68 } else {
69 return i18nc("@label Alternative text when email subject is missing", "(No subject)");
70 }
71 case Sender:
72 if (auto s = msg->from(false)) {
73 return s->asUnicodeString();
74 } else {
75 return i18nc("@label Alternative text when email sender is missing", "(No sender)");
76 }
77 case Receiver:
78 if (auto s = msg->to(false)) {
79 return s->asUnicodeString();
80 } else {
81 return i18nc("@label Alternative text when email recipient is missing", "(No receiver)");
82 }
83 case Date:
84 if (auto s = msg->date(false)) {
85 return QLocale().toString(s->dateTime());
86 } else {
87 return i18nc("@label Alternative text when email date/time is missing", "(No date)");
88 }
89 case Size:
90 if (item.size() == 0) {
91 return i18nc("@label No size available", "-");
92 } else {
93 KFormat format;
94 return format.formatByteSize(item.size());
95 }
96 default:
97 return {};
98 }
99 } else if (role == Qt::EditRole) {
100 switch (column) {
101 case Subject:
102 return msg->subject()->asUnicodeString();
103 case Sender:
104 return msg->from()->asUnicodeString();
105 case Receiver:
106 return msg->to()->asUnicodeString();
107 case Date:
108 return msg->date()->dateTime() /*.dateTime()*/;
109 case Size:
110 return item.size();
111 default:
112 return {};
113 }
114 }
115
116 return EntityTreeModel::entityData(item, column, role);
117}
118
119QVariant MessageModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
120{
121 if (section >= 0 && static_cast<std::size_t>(section) < headers.size()) {
122 return KLocalizedString(headers[section].desc).toString();
123 }
124
125 return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
126}
127
128#include "moc_messagemodel.cpp"
void setCollectionFetchStrategy(CollectionFetchStrategy strategy)
virtual QVariant entityData(const Collection &collection, int column, int role=Qt::DisplayRole) const
virtual QVariant entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
MessageModel(Monitor *monitor, QObject *parent=nullptr)
Creates a new message model.
@ Receiver
Receiver column.
@ Sender
Sender column.
@ Subject
Subject column.
ItemFetchScope & itemFetchScope()
QString formatByteSize(double size, int precision=1, KFormat::BinaryUnitDialect dialect=KFormat::DefaultBinaryDialect, KFormat::BinarySizeUnits units=KFormat::DefaultBinaryUnits) const
QString toString() const
QSharedPointer< Message > Ptr
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AKONADI_MIME_EXPORT const char Envelope[]
The part identifier for envelope parts.
QString toString(QDate date, FormatType format) const const
QSizeF size() const const
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:32:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.