Messagelib

customtemplatesmenu.cpp
1 /*
2  * SPDX-FileCopyrightText: 2006 Dmitry Morozhnikov <[email protected]>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 
7 #include "customtemplatesmenu.h"
8 #include "customtemplates.h"
9 #include "customtemplates_kfg.h"
10 #include "globalsettings_templateparser.h"
11 
12 #include <KActionCollection>
13 #include <KActionMenu>
14 #include <KLocalizedString>
15 #include <QIcon>
16 #include <QMenu>
17 #include <QVector>
18 
19 using namespace TemplateParser;
20 class TemplateParser::CustomTemplatesMenuPrivate
21 {
22 public:
23  CustomTemplatesMenuPrivate() = default;
24 
25  ~CustomTemplatesMenuPrivate()
26  {
27  delete mCustomReplyActionMenu;
28  delete mCustomReplyAllActionMenu;
29  delete mCustomForwardActionMenu;
30  }
31 
32  KActionCollection *mOwnerActionCollection = nullptr;
33 
34  QStringList mCustomTemplates;
35  QVector<QAction *> mCustomTemplateActions;
36 
37  // Custom template actions menu
38  KActionMenu *mCustomReplyActionMenu = nullptr;
39  KActionMenu *mCustomReplyAllActionMenu = nullptr;
40  KActionMenu *mCustomForwardActionMenu = nullptr;
41 };
42 
43 CustomTemplatesMenu::CustomTemplatesMenu(QWidget *owner, KActionCollection *ac)
44  : d(new TemplateParser::CustomTemplatesMenuPrivate)
45 {
46  d->mOwnerActionCollection = ac;
47 
48  d->mCustomForwardActionMenu = new KActionMenu(QIcon::fromTheme(QStringLiteral("mail-forward-custom")), i18n("With Custom Template"), owner);
49  d->mOwnerActionCollection->addAction(QStringLiteral("custom_forward"), d->mCustomForwardActionMenu);
50 
51  d->mCustomReplyActionMenu = new KActionMenu(QIcon::fromTheme(QStringLiteral("mail-reply-custom")), i18n("Reply With Custom Template"), owner);
52  d->mOwnerActionCollection->addAction(QStringLiteral("custom_reply"), d->mCustomReplyActionMenu);
53 
54  d->mCustomReplyAllActionMenu = new KActionMenu(QIcon::fromTheme(QStringLiteral("mail-reply-all-custom")), i18n("Reply to All With Custom Template"), owner);
55  d->mOwnerActionCollection->addAction(QStringLiteral("custom_reply_all"), d->mCustomReplyAllActionMenu);
56 
57  update();
58 }
59 
60 CustomTemplatesMenu::~CustomTemplatesMenu()
61 {
62  clear();
63 }
64 
65 KActionMenu *CustomTemplatesMenu::replyActionMenu() const
66 {
67  return d->mCustomReplyActionMenu;
68 }
69 
70 KActionMenu *CustomTemplatesMenu::replyAllActionMenu() const
71 {
72  return d->mCustomReplyAllActionMenu;
73 }
74 
75 KActionMenu *CustomTemplatesMenu::forwardActionMenu() const
76 {
77  return d->mCustomForwardActionMenu;
78 }
79 
80 void CustomTemplatesMenu::clear()
81 {
82  qDeleteAll(d->mCustomTemplateActions);
83  d->mCustomTemplateActions.clear();
84 
85  d->mCustomReplyActionMenu->menu()->clear();
86  d->mCustomReplyAllActionMenu->menu()->clear();
87  d->mCustomForwardActionMenu->menu()->clear();
88  d->mCustomTemplates.clear();
89 }
90 
91 void CustomTemplatesMenu::update()
92 {
93  clear();
94 
95  const QStringList list = TemplateParserSettings::self()->customTemplates();
98  int idx = 0;
99  int replyc = 0;
100  int replyallc = 0;
101  int forwardc = 0;
102  for (; it != end; ++it) {
103  CTemplates t(*it);
104  d->mCustomTemplates.append(*it);
105  QString nameAction(*it);
106  nameAction.replace(QLatin1Char('&'), QStringLiteral("&&"));
107 
108  const QString nameActionName = nameAction.replace(QLatin1Char(' '), QLatin1Char('_'));
109 
110  QAction *action = nullptr;
111  switch (t.type()) {
112  case CustomTemplates::TReply:
113  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
114  d->mOwnerActionCollection->setDefaultShortcut(action, t.shortcut());
115  d->mOwnerActionCollection->addAction(nameActionName, action);
116  connect(action, &QAction::triggered, this, [this, idx] {
117  slotReplySelected(idx);
118  });
119  d->mCustomReplyActionMenu->addAction(action);
120  d->mCustomTemplateActions.append(action);
121  ++replyc;
122  break;
123 
124  case CustomTemplates::TReplyAll:
125  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
126  d->mOwnerActionCollection->setDefaultShortcut(action, t.shortcut());
127  d->mOwnerActionCollection->addAction(nameActionName, action);
128  connect(action, &QAction::triggered, this, [this, idx] {
129  slotReplyAllSelected(idx);
130  });
131  d->mCustomReplyAllActionMenu->addAction(action);
132  d->mCustomTemplateActions.append(action);
133  ++replyallc;
134  break;
135 
136  case CustomTemplates::TForward:
137  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
138  d->mOwnerActionCollection->addAction(nameActionName, action);
139  d->mOwnerActionCollection->setDefaultShortcut(action, t.shortcut());
140  connect(action, &QAction::triggered, this, [this, idx] {
141  slotForwardSelected(idx);
142  });
143  d->mCustomForwardActionMenu->addAction(action);
144  d->mCustomTemplateActions.append(action);
145  ++forwardc;
146  break;
147 
148  case CustomTemplates::TUniversal:
149  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
150  d->mOwnerActionCollection->addAction(nameActionName, action);
151 
152  connect(action, &QAction::triggered, this, [this, idx] {
153  slotReplySelected(idx);
154  });
155  d->mCustomReplyActionMenu->addAction(action);
156  d->mCustomTemplateActions.append(action);
157  ++replyc;
158  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
159  connect(action, &QAction::triggered, this, [this, idx] {
160  slotReplyAllSelected(idx);
161  });
162  d->mCustomReplyAllActionMenu->addAction(action);
163  d->mCustomTemplateActions.append(action);
164  ++replyallc;
165  action = new QAction(nameAction, d->mOwnerActionCollection); // krazy:exclude=tipsandthis
166  connect(action, &QAction::triggered, this, [this, idx] {
167  slotForwardSelected(idx);
168  });
169  d->mCustomForwardActionMenu->addAction(action);
170  d->mCustomTemplateActions.append(action);
171  ++forwardc;
172  break;
173  }
174 
175  ++idx;
176  }
177 
178  if (!replyc) {
179  QAction *noAction = d->mCustomReplyActionMenu->menu()->addAction(i18n("(no custom templates)"));
180  noAction->setEnabled(false);
181  d->mCustomReplyActionMenu->setEnabled(false);
182  }
183  if (!replyallc) {
184  QAction *noAction = d->mCustomReplyAllActionMenu->menu()->addAction(i18n("(no custom templates)"));
185  noAction->setEnabled(false);
186  d->mCustomReplyAllActionMenu->setEnabled(false);
187  }
188  if (!forwardc) {
189  QAction *noAction = d->mCustomForwardActionMenu->menu()->addAction(i18n("(no custom templates)"));
190  noAction->setEnabled(false);
191  d->mCustomForwardActionMenu->setEnabled(false);
192  }
193 }
194 
195 void CustomTemplatesMenu::slotReplySelected(int idx)
196 {
197  Q_EMIT replyTemplateSelected(d->mCustomTemplates.at(idx));
198 }
199 
200 void CustomTemplatesMenu::slotReplyAllSelected(int idx)
201 {
202  Q_EMIT replyAllTemplateSelected(d->mCustomTemplates.at(idx));
203 }
204 
205 void CustomTemplatesMenu::slotForwardSelected(int idx)
206 {
207  Q_EMIT forwardTemplateSelected(d->mCustomTemplates.at(idx));
208 }
void triggered(bool checked)
QAction * addAction(const QString &text)
void update()
QString i18n(const char *text, const TYPE &arg...)
QString & replace(int position, int n, QChar after)
const QList< QKeySequence > & end()
QIcon fromTheme(const QString &name)
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMenu * menu() const const
void setEnabled(bool)
Q_EMITQ_EMIT
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.