Messagelib

headerstylemenumanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "headerstylemenumanager.h"
8 #include "header/headerstrategy.h"
9 #include "header/headerstyle.h"
10 #include "headerstyleinterface.h"
11 #include "headerstyleplugin.h"
12 #include "headerstylepluginmanager.h"
13 #include "messageviewer/messageviewersettings.h"
14 #include "messageviewer_debug.h"
15 #include "utils/messageviewerutil_p.h"
16 
17 #include <KActionCollection>
18 #include <KActionMenu>
19 #include <KLocalizedString>
20 
21 using namespace MessageViewer;
22 
23 class MessageViewer::HeaderStyleMenuManagerPrivate
24 {
25 public:
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 
41 void 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 
62 void 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 == QLatin1String("custom")) && (headerSetDisplayed == QLatin1String("custom"))) { // Custom
70  headerStyleName = QStringLiteral("custom");
71  } else if ((headerStyle == QLatin1String("plain")) && (headerSetDisplayed == QLatin1String("all"))) { // all
72  headerStyleName = QStringLiteral("all-headers");
73  } else if ((headerStyle == QLatin1String("brief")) && (headerSetDisplayed == QLatin1String("brief"))) { // brief
74  headerStyleName = QStringLiteral("brief");
75  } else if ((headerStyle == QLatin1String("enterprise")) && (headerSetDisplayed == QLatin1String("rich"))) { // enterprise
76  headerStyleName = QStringLiteral("enterprise");
77  } else if ((headerStyle == QLatin1String("fancy")) && (headerSetDisplayed == QLatin1String("rich"))) { // fancy
78  headerStyleName = QStringLiteral("fancy");
79  } else if ((headerStyle == QLatin1String("grantlee")) && (headerSetDisplayed == QLatin1String("grantlee"))) { // grantlee
80  headerStyleName = QStringLiteral("grantlee");
81  } else if ((headerStyle == QLatin1String("plain")) && (headerSetDisplayed == QLatin1String("rich"))) { // longheader
82  headerStyleName = QStringLiteral("long-header");
83  } else if ((headerStyle == QLatin1String("plain")) && (headerSetDisplayed == QLatin1String("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 
97 void HeaderStyleMenuManagerPrivate::writeSettings(const QString &pluginName)
98 {
99  MessageViewer::MessageViewerSettings::self()->setHeaderPluginStyleName(pluginName);
100  MessageViewer::MessageViewerSettings::self()->save();
101 }
102 
103 void 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 QVector<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 
123 HeaderStyleMenuManager::HeaderStyleMenuManager(KActionCollection *ac, QObject *parent)
124  : QObject(parent)
125  , d(new MessageViewer::HeaderStyleMenuManagerPrivate(this))
126 {
127  d->initialize(ac);
128 }
129 
130 HeaderStyleMenuManager::~HeaderStyleMenuManager() = default;
131 
132 KActionMenu *HeaderStyleMenuManager::menu() const
133 {
134  return d->headerMenu;
135 }
136 
137 void MessageViewer::HeaderStyleMenuManager::setPluginName(const QString &pluginName)
138 {
139  d->setPluginName(pluginName);
140 }
141 
142 void HeaderStyleMenuManager::slotStyleChanged(MessageViewer::HeaderStylePlugin *plugin)
143 {
144  d->writeSettings(plugin->name());
145  Q_EMIT styleChanged(plugin);
146 }
147 
148 void HeaderStyleMenuManager::readConfig()
149 {
150  d->readSettings();
151 }
KCRASH_EXPORT void initialize()
Q_INVOKABLE QAction * addAction(const QString &name, QAction *action)
The HeaderStyleMenuManager class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
The HeaderStyleInterface class.
bool isEmpty() const const
The HeaderStylePlugin class.
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.