Messagelib

messagelistutil.cpp
1 /*
2  This file is part of KMail, the KDE mail client.
3  SPDX-FileCopyrightText: 2011-2021 Laurent Montel <[email protected]>
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 
19 QString MessageList::Util::messageSortingConfigName()
20 {
21  return QStringLiteral("MessageSorting");
22 }
23 
24 QString MessageList::Util::messageSortDirectionConfigName()
25 {
26  return QStringLiteral("MessageSortDirection");
27 }
28 
29 QString MessageList::Util::groupSortingConfigName()
30 {
31  return QStringLiteral("GroupSorting");
32 }
33 
34 QString MessageList::Util::groupSortDirectionConfigName()
35 {
36  return QStringLiteral("GroupSortDirection");
37 }
38 
39 QString MessageList::Util::messageUniqueIdConfigName()
40 {
41  return QStringLiteral("MessageUniqueIdForStorageModel%1");
42 }
43 
44 QString MessageList::Util::storageModelSortOrderGroup()
45 {
46  return QStringLiteral("MessageListView::StorageModelSortOrder");
47 }
48 
49 QString MessageList::Util::storageModelThemesGroup()
50 {
51  return QStringLiteral("MessageListView::StorageModelThemes");
52 }
53 
54 QString MessageList::Util::storageModelAggregationsGroup()
55 {
56  return QStringLiteral("MessageListView::StorageModelAggregations");
57 }
58 
59 QString MessageList::Util::setForStorageModelConfigName()
60 {
61  return QStringLiteral("SetForStorageModel%1");
62 }
63 
64 QString MessageList::Util::storageModelSelectedMessageGroup()
65 {
66  return QStringLiteral("MessageListView::StorageModelSelectedMessages");
67 }
68 
69 void 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 
87 QColor MessageList::Util::unreadDefaultMessageColor()
88 {
90 }
91 
92 QColor MessageList::Util::importantDefaultMessageColor()
93 {
95 }
96 
97 QColor MessageList::Util::todoDefaultMessageColor()
98 {
100 }
101 
102 void 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 
120 QString 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(true, true);
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 }
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
T payload() const
KSharedConfigPtr config()
const QColor & color() const const
int count(const T &value) const const
QBrush foreground(ForegroundRole=NormalText) const
bool isEmpty() const const
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toHtmlEscaped() const const
QString i18n(const char *text, const TYPE &arg...)
QAction * addMenu(QMenu *menu)
int length() const const
QString left(int n) const const
QIcon fromTheme(const QString &name)
bool hasPayload() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.