Messagelib

messagelistutil.cpp
1/*
2 This file is part of KMail, the KDE mail client.
3 SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7#include "messagelistutil.h"
8#include "messagelistsettings.h"
9#include "messagelistutil_p.h"
10
11#include <Akonadi/Item>
12#include <KColorScheme>
13#include <KConfigGroup>
14#include <KLocalizedString>
15#include <KMime/Message>
16#include <QIcon>
17#include <QMenu>
18
19QString MessageList::Util::messageSortingConfigName()
20{
21 return QStringLiteral("MessageSorting");
22}
23
24QString MessageList::Util::messageSortDirectionConfigName()
25{
26 return QStringLiteral("MessageSortDirection");
27}
28
29QString MessageList::Util::groupSortingConfigName()
30{
31 return QStringLiteral("GroupSorting");
32}
33
34QString MessageList::Util::groupSortDirectionConfigName()
35{
36 return QStringLiteral("GroupSortDirection");
37}
38
39QString MessageList::Util::messageUniqueIdConfigName()
40{
41 return QStringLiteral("MessageUniqueIdForStorageModel%1");
42}
43
44QString MessageList::Util::storageModelSortOrderGroup()
45{
46 return QStringLiteral("MessageListView::StorageModelSortOrder");
47}
48
49QString MessageList::Util::storageModelThemesGroup()
50{
51 return QStringLiteral("MessageListView::StorageModelThemes");
52}
53
54QString MessageList::Util::storageModelAggregationsGroup()
55{
56 return QStringLiteral("MessageListView::StorageModelAggregations");
57}
58
59QString MessageList::Util::setForStorageModelConfigName()
60{
61 return QStringLiteral("SetForStorageModel%1");
62}
63
64QString MessageList::Util::storageModelSelectedMessageGroup()
65{
66 return QStringLiteral("MessageListView::StorageModelSelectedMessages");
67}
68
69void MessageList::Util::deleteConfig(const QString &collectionId)
70{
71 KConfigGroup confselectedMessage(MessageListSettings::self()->config(), MessageList::Util::storageModelSelectedMessageGroup());
72 confselectedMessage.deleteEntry(MessageList::Util::messageUniqueIdConfigName().arg(collectionId));
73
74 KConfigGroup storageModelOrder(MessageListSettings::self()->config(), MessageList::Util::storageModelSortOrderGroup());
75 storageModelOrder.deleteEntry(collectionId + groupSortDirectionConfigName());
76 storageModelOrder.deleteEntry(collectionId + groupSortingConfigName());
77 storageModelOrder.deleteEntry(collectionId + messageSortDirectionConfigName());
78 storageModelOrder.deleteEntry(collectionId + messageSortingConfigName());
79
80 KConfigGroup storageModelTheme(MessageListSettings::self()->config(), MessageList::Util::storageModelThemesGroup());
81 storageModelTheme.deleteEntry(collectionId + setForStorageModelConfigName());
82
83 KConfigGroup storageModelAggregation(MessageListSettings::self()->config(), MessageList::Util::storageModelAggregationsGroup());
84 storageModelAggregation.deleteEntry(collectionId + setForStorageModelConfigName());
85}
86
87QColor MessageList::Util::unreadDefaultMessageColor()
88{
90}
91
92QColor MessageList::Util::importantDefaultMessageColor()
93{
95}
96
97QColor MessageList::Util::todoDefaultMessageColor()
98{
100}
101
102void MessageList::Util::fillViewMenu(QMenu *menu, QObject *receiver)
103{
104 auto sortingMenu = new QMenu(i18n("Sorting"), menu);
105 sortingMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort-ascending")));
106 menu->addMenu(sortingMenu);
107 QObject::connect(sortingMenu, SIGNAL(aboutToShow()), receiver, SLOT(sortOrderMenuAboutToShow()));
108
109 auto aggregationMenu = new QMenu(i18n("Aggregation"), menu);
110 aggregationMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-process-tree")));
111 menu->addMenu(aggregationMenu);
112 QObject::connect(aggregationMenu, SIGNAL(aboutToShow()), receiver, SLOT(aggregationMenuAboutToShow()));
113
114 auto themeMenu = new QMenu(i18n("Theme"), menu);
115 themeMenu->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-theme")));
116 menu->addMenu(themeMenu);
117 QObject::connect(themeMenu, SIGNAL(aboutToShow()), receiver, SLOT(themeMenuAboutToShow()));
118}
119
120QString MessageList::Util::contentSummary(const Akonadi::Item &item)
121{
122 if (!item.hasPayload<KMime::Message::Ptr>()) {
123 return {};
124 }
125
126 auto message = item.payload<KMime::Message::Ptr>();
127 KMime::Content *textContent = message->textContent();
128 if (!textContent) {
129 return {};
130 }
131 const QString content = textContent->decodedText(KMime::Content::TrimSpaces);
132 if (content.isEmpty()) {
133 return {};
134 }
135
136 // Extract the first 5 non-empty, non-quoted lines from the content and return it
137 int numLines = 0;
138 const int maxLines = 5;
139 const QStringList lines = content.split(QLatin1Char('\n'));
140 if (lines.isEmpty()) {
141 return {};
142 }
143 if (lines.count() == 1 && content.length() > 100) {
144 return content.left(100);
145 }
146 QString ret;
147 for (const QString &line : lines) {
148 const QString lineTrimmed = line.trimmed();
149 const bool isQuoted = lineTrimmed.startsWith(QLatin1Char('>')) || lineTrimmed.startsWith(QLatin1Char('|'));
150 if (!isQuoted && !lineTrimmed.isEmpty()) {
151 ret += line + QLatin1Char('\n');
152 numLines++;
153 if (numLines >= maxLines) {
154 break;
155 }
156 }
157 }
158 return ret.toHtmlEscaped();
159}
T payload() const
bool hasPayload() const
QBrush foreground(ForegroundRole=NormalText) const
QString i18n(const char *text, const TYPE &arg...)
const QColor & color() const const
QIcon fromTheme(const QString &name)
qsizetype count() const const
bool isEmpty() const const
QAction * addMenu(QMenu *menu)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString left(qsizetype n) const const
qsizetype length() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toHtmlEscaped() const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.