collectiontemplateswidget.cpp 3.05 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11
/*
   SPDX-FileCopyrightText: 2020 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "collectiontemplateswidget.h"

#include <KLocalizedString>
#include "templatesconfiguration_kfg.h"

Laurent Montel's avatar
Laurent Montel committed
12
#include <MailCommon/FolderSettings>
Laurent Montel's avatar
Laurent Montel committed
13
#include <QCheckBox>
Laurent Montel's avatar
Laurent Montel committed
14
#include <QSharedPointer>
Laurent Montel's avatar
Laurent Montel committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <QVBoxLayout>
#include <TemplateParser/TemplatesConfiguration>

CollectionTemplatesWidget::CollectionTemplatesWidget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *topLayout = new QVBoxLayout(this);
    QHBoxLayout *topItems = new QHBoxLayout;
    topItems->setContentsMargins(0, 0, 0, 0);
    topLayout->addLayout(topItems);

    mCustom = new QCheckBox(i18n("&Use custom message templates in this folder"), this);
    connect(mCustom, &QCheckBox::clicked, this, &CollectionTemplatesWidget::slotChanged);
    topItems->addWidget(mCustom, Qt::AlignLeft);

    mWidget = new TemplateParser::TemplatesConfiguration(this, QStringLiteral("folder-templates"));
    connect(mWidget, &TemplateParser::TemplatesConfiguration::changed, this, &CollectionTemplatesWidget::slotChanged);
    mWidget->setEnabled(false);

    // Move the help label outside of the templates configuration widget,
    // so that the help can be read even if the widget is not enabled.
    topItems->addStretch(9);
    topItems->addWidget(mWidget->helpLabel(), Qt::AlignRight);

    topLayout->addWidget(mWidget);

    QHBoxLayout *btns = new QHBoxLayout();
    QPushButton *copyGlobal = new QPushButton(i18n("&Copy Global Templates"), this);
    copyGlobal->setEnabled(false);
    btns->addWidget(copyGlobal);
    topLayout->addLayout(btns);

    connect(mCustom, &QCheckBox::toggled, mWidget, &TemplateParser::TemplatesConfiguration::setEnabled);
    connect(mCustom, &QCheckBox::toggled, copyGlobal, &QPushButton::setEnabled);

    connect(copyGlobal, &QPushButton::clicked, this, &CollectionTemplatesWidget::slotCopyGlobal);
}

CollectionTemplatesWidget::~CollectionTemplatesWidget()
{

}

Laurent Montel's avatar
Laurent Montel committed
58
void CollectionTemplatesWidget::save(const Akonadi::Collection &)
Laurent Montel's avatar
Laurent Montel committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
    if (mChanged && !mCollectionId.isEmpty()) {
        TemplateParser::Templates t(mCollectionId);
        //qCDebug(KMAIL_LOG) << "use custom templates for folder" << fid <<":" << mCustom->isChecked();
        t.setUseCustomTemplates(mCustom->isChecked());
        t.save();

        mWidget->saveToFolder(mCollectionId);
    }
}

void CollectionTemplatesWidget::slotCopyGlobal()
{
    if (mIdentity) {
        mWidget->loadFromIdentity(mIdentity);
    } else {
        mWidget->loadFromGlobal();
    }
}

void CollectionTemplatesWidget::slotChanged()
{
    mChanged = true;
}
Laurent Montel's avatar
Laurent Montel committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

void CollectionTemplatesWidget::load(const Akonadi::Collection &col)
{
    const QSharedPointer<MailCommon::FolderSettings> fd = MailCommon::FolderSettings::forCollection(col, false);
    if (!fd) {
        return;
    }

    mCollectionId = QString::number(col.id());

    TemplateParser::Templates t(mCollectionId);

    mCustom->setChecked(t.useCustomTemplates());

    mIdentity = fd->identity();

    mWidget->loadFromFolder(mCollectionId, mIdentity);
    mChanged = false;
}