File indexing completed on 2025-01-26 04:45:36

0001 /*
0002     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "messagemodel.h"
0008 #include "messageparts.h"
0009 
0010 #include <Akonadi/ItemFetchScope>
0011 #include <Akonadi/Monitor>
0012 #include <Akonadi/Session>
0013 
0014 #include <KMime/KMimeMessage>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <KIO/Global>
0019 #include <QLocale>
0020 
0021 #include <KLazyLocalizedString>
0022 #include <array>
0023 using MessagePtr = KMime::Message::Ptr;
0024 
0025 using namespace Akonadi;
0026 
0027 namespace
0028 {
0029 struct HeaderName {
0030     const KLazyLocalizedString desc;
0031 };
0032 constexpr std::array<HeaderName, 5> headers = {{{kli18nc("@title:column, message (e.g. email) subject", "Subject")},
0033                                                 {kli18nc("@title:column, sender of message (e.g. email)", "Sender")},
0034                                                 {kli18nc("@title:column, receiver of message (e.g. email)", "Receiver")},
0035                                                 {kli18nc("@title:column, message (e.g. email) timestamp", "Date")},
0036                                                 {kli18nc("@title:column, message (e.g. email) size", "Size")}}};
0037 }
0038 
0039 MessageModel::MessageModel(Monitor *monitor, QObject *parent)
0040     : EntityTreeModel(monitor, parent)
0041 {
0042     // Make sure we have an envelope
0043     monitor->itemFetchScope().fetchPayloadPart(MessagePart::Envelope);
0044     setCollectionFetchStrategy(InvisibleCollectionFetch);
0045 }
0046 
0047 int MessageModel::entityColumnCount(HeaderGroup group) const
0048 {
0049     if (group == EntityTreeModel::ItemListHeaders) {
0050         return headers.size();
0051     }
0052 
0053     return EntityTreeModel::entityColumnCount(group);
0054 }
0055 
0056 QVariant MessageModel::entityData(const Item &item, int column, int role) const
0057 {
0058     if (!item.hasPayload<KMime::Message::Ptr>()) {
0059         return {};
0060     }
0061 
0062     const auto msg = item.payload<KMime::Message::Ptr>();
0063     if (role == Qt::DisplayRole) {
0064         switch (column) {
0065         case Subject:
0066             if (auto s = msg->subject(false)) {
0067                 return s->asUnicodeString();
0068             } else {
0069                 return i18nc("@label Alternative text when email subject is missing", "(No subject)");
0070             }
0071         case Sender:
0072             if (auto s = msg->from(false)) {
0073                 return s->asUnicodeString();
0074             } else {
0075                 return i18nc("@label Alternative text when email sender is missing", "(No sender)");
0076             }
0077         case Receiver:
0078             if (auto s = msg->to(false)) {
0079                 return s->asUnicodeString();
0080             } else {
0081                 return i18nc("@label Alternative text when email recipient is missing", "(No receiver)");
0082             }
0083         case Date:
0084             if (auto s = msg->date(false)) {
0085                 return QLocale().toString(s->dateTime());
0086             } else {
0087                 return i18nc("@label Alternative text when email date/time is missing", "(No date)");
0088             }
0089         case Size:
0090             if (item.size() == 0) {
0091                 return i18nc("@label No size available", "-");
0092             } else {
0093                 return KIO::convertSize(item.size());
0094             }
0095         default:
0096             return {};
0097         }
0098     } else if (role == Qt::EditRole) {
0099         switch (column) {
0100         case Subject:
0101             return msg->subject()->asUnicodeString();
0102         case Sender:
0103             return msg->from()->asUnicodeString();
0104         case Receiver:
0105             return msg->to()->asUnicodeString();
0106         case Date:
0107             return msg->date()->dateTime() /*.dateTime()*/;
0108         case Size:
0109             return item.size();
0110         default:
0111             return {};
0112         }
0113     }
0114 
0115     return EntityTreeModel::entityData(item, column, role);
0116 }
0117 
0118 QVariant MessageModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
0119 {
0120     if (section >= 0 && static_cast<std::size_t>(section) < headers.size()) {
0121         return KLocalizedString(headers[section].desc).toString();
0122     }
0123 
0124     return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
0125 }
0126 
0127 #include "moc_messagemodel.cpp"