Messagelib

headerstylemenumanager.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "headerstylemenumanager.h"
8#include "headerstyleinterface.h"
9#include "headerstyleplugin.h"
10#include "headerstylepluginmanager.h"
11#include "messageviewer/messageviewersettings.h"
12#include "messageviewer_debug.h"
13#include "utils/messageviewerutil_p.h"
14
15#include <KActionCollection>
16#include <KActionMenu>
17#include <KLocalizedString>
18
19#include <QActionGroup>
20
21using namespace MessageViewer;
22
23class MessageViewer::HeaderStyleMenuManagerPrivate
24{
25public:
26 HeaderStyleMenuManagerPrivate(HeaderStyleMenuManager *qq)
27 : q(qq)
28 {
29 }
30
31 void readSettings();
32 void writeSettings(const QString &pluginName);
33 void initialize(KActionCollection *ac);
34 void setPluginName(const QString &pluginName);
36 QActionGroup *group = nullptr;
37 KActionMenu *headerMenu = nullptr;
38 HeaderStyleMenuManager *const q = nullptr;
39};
40
41void HeaderStyleMenuManagerPrivate::setPluginName(const QString &pluginName)
42{
43 MessageViewer::HeaderStyleInterface *interface = lstInterface.value(pluginName);
44 if (interface) {
45 if (!interface->action().isEmpty()) {
46 interface->activateAction();
47 }
48 } else {
49 if (lstInterface.isEmpty()) {
50 qCCritical(MESSAGEVIEWER_LOG) << "No Header Style plugin found !";
51 return;
52 } else {
53 interface = lstInterface.cbegin().value();
54 if (!interface->action().isEmpty()) {
55 interface->activateAction();
56 }
57 }
58 }
59 Q_EMIT q->styleChanged(interface->headerStylePlugin());
60}
61
62void HeaderStyleMenuManagerPrivate::readSettings()
63{
64 QString headerStyleName = MessageViewer::MessageViewerSettings::self()->headerPluginStyleName();
65 if (headerStyleName.isEmpty()) {
66 const QString headerStyle = MessageViewer::MessageViewerSettings::self()->headerStyle();
67 const QString headerSetDisplayed = MessageViewer::MessageViewerSettings::self()->headerSetDisplayed();
68
69 if ((headerStyle == QLatin1StringView("custom")) && (headerSetDisplayed == QLatin1StringView("custom"))) { // Custom
70 headerStyleName = QStringLiteral("custom");
71 } else if ((headerStyle == QLatin1StringView("plain")) && (headerSetDisplayed == QLatin1StringView("all"))) { // all
72 headerStyleName = QStringLiteral("all-headers");
73 } else if ((headerStyle == QLatin1StringView("brief")) && (headerSetDisplayed == QLatin1StringView("brief"))) { // brief
74 headerStyleName = QStringLiteral("brief");
75 } else if ((headerStyle == QLatin1StringView("enterprise")) && (headerSetDisplayed == QLatin1StringView("rich"))) { // enterprise
76 headerStyleName = QStringLiteral("enterprise");
77 } else if ((headerStyle == QLatin1StringView("fancy")) && (headerSetDisplayed == QLatin1StringView("rich"))) { // fancy
78 headerStyleName = QStringLiteral("fancy");
79 } else if ((headerStyle == QLatin1StringView("grantlee")) && (headerSetDisplayed == QLatin1StringView("grantlee"))) { // grantlee
80 headerStyleName = QStringLiteral("grantlee");
81 } else if ((headerStyle == QLatin1StringView("plain")) && (headerSetDisplayed == QLatin1StringView("rich"))) { // longheader
82 headerStyleName = QStringLiteral("long-header");
83 } else if ((headerStyle == QLatin1StringView("plain")) && (headerSetDisplayed == QLatin1StringView("standard"))) { // Standard
84 headerStyleName = QStringLiteral("standards-header");
85 } else {
86 qCDebug(MESSAGEVIEWER_LOG) << "unknown style : headerstyle " << headerStyle << " headerstrategy :" << headerSetDisplayed;
87 }
88 MessageViewer::MessageViewerSettings::self()->setHeaderPluginStyleName(headerStyleName);
89 }
90 // Fallback
91 if (headerStyleName.isEmpty()) {
92 headerStyleName = QStringLiteral("fancy");
93 }
94 setPluginName(headerStyleName);
95}
96
97void HeaderStyleMenuManagerPrivate::writeSettings(const QString &pluginName)
98{
99 MessageViewer::MessageViewerSettings::self()->setHeaderPluginStyleName(pluginName);
100 MessageViewer::MessageViewerSettings::self()->save();
101}
102
103void HeaderStyleMenuManagerPrivate::initialize(KActionCollection *ac)
104{
105 headerMenu = new KActionMenu(i18nc("View->", "&Headers"), q);
106 if (ac) {
107 ac->addAction(QStringLiteral("view_headers"), headerMenu);
108 }
109 MessageViewer::Util::addHelpTextAction(headerMenu, i18n("Choose display style of message headers"));
110 group = new QActionGroup(q);
111
112 const QList<MessageViewer::HeaderStylePlugin *> lstPlugin = MessageViewer::HeaderStylePluginManager::self()->pluginsList();
113 for (MessageViewer::HeaderStylePlugin *plugin : lstPlugin) {
114 if (plugin->isEnabled()) {
115 MessageViewer::HeaderStyleInterface *interface = plugin->createView(headerMenu, group, ac, q);
116 lstInterface.insert(plugin->name(), interface);
117 q->connect(interface, &HeaderStyleInterface::styleChanged, q, &HeaderStyleMenuManager::slotStyleChanged);
118 q->connect(interface, &HeaderStyleInterface::styleUpdated, q, &HeaderStyleMenuManager::styleUpdated);
119 }
120 }
121}
122
123HeaderStyleMenuManager::HeaderStyleMenuManager(KActionCollection *ac, QObject *parent)
124 : QObject(parent)
125 , d(new MessageViewer::HeaderStyleMenuManagerPrivate(this))
126{
127 d->initialize(ac);
128}
129
130HeaderStyleMenuManager::~HeaderStyleMenuManager() = default;
131
132KActionMenu *HeaderStyleMenuManager::menu() const
133{
134 return d->headerMenu;
135}
136
137void MessageViewer::HeaderStyleMenuManager::setPluginName(const QString &pluginName)
138{
139 d->setPluginName(pluginName);
140}
141
142void HeaderStyleMenuManager::slotStyleChanged(MessageViewer::HeaderStylePlugin *plugin)
143{
144 d->writeSettings(plugin->name());
145 Q_EMIT styleChanged(plugin);
146}
147
148void HeaderStyleMenuManager::readConfig()
149{
150 d->readSettings();
151}
152
153#include "moc_headerstylemenumanager.cpp"
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
The HeaderStyleInterface class.
The HeaderStyleMenuManager class.
The HeaderStylePlugin class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.