Warning, file /pim/mailcommon/src/collectionpage/collectiontemplateswidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "collectiontemplateswidget.h"
0008 
0009 #include <KLocalizedString>
0010 #include <templateparser/templatesconfiguration_kfg.h>
0011 
0012 #include <MailCommon/FolderSettings>
0013 #include <QCheckBox>
0014 #include <QSharedPointer>
0015 #include <QVBoxLayout>
0016 #include <TemplateParser/TemplatesConfiguration>
0017 using namespace MailCommon;
0018 CollectionTemplatesWidget::CollectionTemplatesWidget(QWidget *parent)
0019     : QWidget(parent)
0020     , mCustom(new QCheckBox(i18n("&Use custom message templates in this folder"), this))
0021 {
0022     auto topLayout = new QVBoxLayout(this);
0023     auto topItems = new QHBoxLayout;
0024     topItems->setContentsMargins({});
0025     topLayout->addLayout(topItems);
0026 
0027     connect(mCustom, &QCheckBox::clicked, this, &CollectionTemplatesWidget::slotChanged);
0028     topItems->addWidget(mCustom, Qt::AlignLeft);
0029 
0030     mWidget = new TemplateParser::TemplatesConfiguration(this, QStringLiteral("folder-templates"));
0031     connect(mWidget, &TemplateParser::TemplatesConfiguration::changed, this, &CollectionTemplatesWidget::slotChanged);
0032     mWidget->setEnabled(false);
0033 
0034     // Move the help label outside of the templates configuration widget,
0035     // so that the help can be read even if the widget is not enabled.
0036     topItems->addStretch(9);
0037     topItems->addWidget(mWidget->helpLabel(), Qt::AlignRight);
0038 
0039     topLayout->addWidget(mWidget);
0040 
0041     auto btns = new QHBoxLayout();
0042     auto copyGlobal = new QPushButton(i18n("&Copy Global Templates"), this);
0043     copyGlobal->setEnabled(false);
0044     btns->addWidget(copyGlobal);
0045     topLayout->addLayout(btns);
0046 
0047     connect(mCustom, &QCheckBox::toggled, mWidget, &TemplateParser::TemplatesConfiguration::setEnabled);
0048     connect(mCustom, &QCheckBox::toggled, copyGlobal, &QPushButton::setEnabled);
0049 
0050     connect(copyGlobal, &QPushButton::clicked, this, &CollectionTemplatesWidget::slotCopyGlobal);
0051 }
0052 
0053 CollectionTemplatesWidget::~CollectionTemplatesWidget() = default;
0054 
0055 void CollectionTemplatesWidget::save(Akonadi::Collection &col)
0056 {
0057     if (mCollectionId.isEmpty()) {
0058         mCollectionId = QString::number(col.id());
0059     }
0060     if (mChanged) {
0061         TemplateParser::Templates t(mCollectionId);
0062         // qCDebug(KMAIL_LOG) << "use custom templates for folder" << fid <<":" << mCustom->isChecked();
0063         t.setUseCustomTemplates(mCustom->isChecked());
0064         t.save();
0065 
0066         mWidget->saveToFolder(mCollectionId);
0067     }
0068 }
0069 
0070 void CollectionTemplatesWidget::slotCopyGlobal()
0071 {
0072     if (mIdentity) {
0073         mWidget->loadFromIdentity(mIdentity);
0074     } else {
0075         mWidget->loadFromGlobal();
0076     }
0077 }
0078 
0079 void CollectionTemplatesWidget::slotChanged()
0080 {
0081     mChanged = true;
0082 }
0083 
0084 void CollectionTemplatesWidget::load(const Akonadi::Collection &col)
0085 {
0086     const QSharedPointer<MailCommon::FolderSettings> fd = MailCommon::FolderSettings::forCollection(col, false);
0087     if (fd.isNull()) {
0088         return;
0089     }
0090 
0091     mCollectionId = QString::number(col.id());
0092 
0093     TemplateParser::Templates t(mCollectionId);
0094 
0095     mCustom->setChecked(t.useCustomTemplates());
0096 
0097     mIdentity = fd->identity();
0098 
0099     mWidget->loadFromFolder(mCollectionId, mIdentity);
0100     mChanged = false;
0101 }
0102 
0103 #include "moc_collectiontemplateswidget.cpp"