Mailcommon

collectiontemplateswidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2020-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "collectiontemplateswidget.h"
8 
9 #include <KLocalizedString>
10 #include <templateparser/templatesconfiguration_kfg.h>
11 
12 #include <MailCommon/FolderSettings>
13 #include <QCheckBox>
14 #include <QSharedPointer>
15 #include <QVBoxLayout>
16 #include <TemplateParser/TemplatesConfiguration>
17 using namespace MailCommon;
18 CollectionTemplatesWidget::CollectionTemplatesWidget(QWidget *parent)
19  : QWidget(parent)
20 {
21  auto topLayout = new QVBoxLayout(this);
22  auto topItems = new QHBoxLayout;
23  topItems->setContentsMargins({});
24  topLayout->addLayout(topItems);
25 
26  mCustom = new QCheckBox(i18n("&Use custom message templates in this folder"), this);
27  connect(mCustom, &QCheckBox::clicked, this, &CollectionTemplatesWidget::slotChanged);
28  topItems->addWidget(mCustom, Qt::AlignLeft);
29 
30  mWidget = new TemplateParser::TemplatesConfiguration(this, QStringLiteral("folder-templates"));
31  connect(mWidget, &TemplateParser::TemplatesConfiguration::changed, this, &CollectionTemplatesWidget::slotChanged);
32  mWidget->setEnabled(false);
33 
34  // Move the help label outside of the templates configuration widget,
35  // so that the help can be read even if the widget is not enabled.
36  topItems->addStretch(9);
37  topItems->addWidget(mWidget->helpLabel(), Qt::AlignRight);
38 
39  topLayout->addWidget(mWidget);
40 
41  auto btns = new QHBoxLayout();
42  auto copyGlobal = new QPushButton(i18n("&Copy Global Templates"), this);
43  copyGlobal->setEnabled(false);
44  btns->addWidget(copyGlobal);
45  topLayout->addLayout(btns);
46 
48  connect(mCustom, &QCheckBox::toggled, copyGlobal, &QPushButton::setEnabled);
49 
50  connect(copyGlobal, &QPushButton::clicked, this, &CollectionTemplatesWidget::slotCopyGlobal);
51 }
52 
53 CollectionTemplatesWidget::~CollectionTemplatesWidget() = default;
54 
55 void CollectionTemplatesWidget::save(Akonadi::Collection &col)
56 {
57  if (mCollectionId.isEmpty()) {
58  mCollectionId = QString::number(col.id());
59  }
60  if (mChanged) {
61  TemplateParser::Templates t(mCollectionId);
62  // qCDebug(KMAIL_LOG) << "use custom templates for folder" << fid <<":" << mCustom->isChecked();
63  t.setUseCustomTemplates(mCustom->isChecked());
64  t.save();
65 
66  mWidget->saveToFolder(mCollectionId);
67  }
68 }
69 
70 void CollectionTemplatesWidget::slotCopyGlobal()
71 {
72  if (mIdentity) {
73  mWidget->loadFromIdentity(mIdentity);
74  } else {
75  mWidget->loadFromGlobal();
76  }
77 }
78 
79 void CollectionTemplatesWidget::slotChanged()
80 {
81  mChanged = true;
82 }
83 
84 void CollectionTemplatesWidget::load(const Akonadi::Collection &col)
85 {
86  const QSharedPointer<MailCommon::FolderSettings> fd = MailCommon::FolderSettings::forCollection(col, false);
87  if (fd.isNull()) {
88  return;
89  }
90 
91  mCollectionId = QString::number(col.id());
92 
93  TemplateParser::Templates t(mCollectionId);
94 
95  mCustom->setChecked(t.useCustomTemplates());
96 
97  mIdentity = fd->identity();
98 
99  mWidget->loadFromFolder(mCollectionId, mIdentity);
100  mChanged = false;
101 }
AlignLeft
QString number(int n, int base)
bool isNull() const const
void clicked(bool checked)
bool isChecked() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void toggled(bool checked)
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
void setEnabled(bool)
void setContentsMargins(int left, int top, int right, int bottom)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.