File indexing completed on 2024-11-24 04:39:31

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "messageformattingwidget.h"
0010 #include "editor/utils/utils.h"
0011 #include <KLocalizedString>
0012 
0013 #include <QCheckBox>
0014 #include <QComboBox>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 using namespace Akonadi;
0018 
0019 MessageFormattingWidget::MessageFormattingWidget(QWidget *parent)
0020     : QWidget(parent)
0021     , mMailPreferFormatting(new QComboBox(this))
0022     , mAllowRemoteContent(new QCheckBox(i18n("Allow remote content in received HTML messages"), this))
0023 {
0024     auto topLayout = new QVBoxLayout(this);
0025     topLayout->setContentsMargins({});
0026     auto label = new QLabel(i18n("Show messages received from this contact as:"), this);
0027     label->setObjectName(QLatin1StringView("label"));
0028     topLayout->addWidget(label);
0029 
0030     mMailPreferFormatting->setObjectName(QLatin1StringView("mMailPreferFormatting"));
0031     topLayout->addWidget(mMailPreferFormatting);
0032     label->setBuddy(mMailPreferFormatting);
0033     const QStringList listFormat{i18n("Default"), i18n("Plain Text"), i18n("HTML")};
0034     mMailPreferFormatting->addItems(listFormat);
0035 
0036     mAllowRemoteContent->setObjectName(QLatin1StringView("mAllowRemoteContent"));
0037     topLayout->addWidget(mAllowRemoteContent);
0038 }
0039 
0040 MessageFormattingWidget::~MessageFormattingWidget() = default;
0041 
0042 void MessageFormattingWidget::loadContact(const KContacts::Addressee &contact)
0043 {
0044     const QString mailAllowToRemoteContent = Akonadi::Utils::loadCustom(contact, QStringLiteral("MailAllowToRemoteContent"));
0045     mAllowRemoteContent->setChecked(mailAllowToRemoteContent == QLatin1StringView("TRUE"));
0046 
0047     const QString mailPreferedFormatting = Akonadi::Utils::loadCustom(contact, QStringLiteral("MailPreferedFormatting"));
0048     if (mailPreferedFormatting.isEmpty()) {
0049         mMailPreferFormatting->setCurrentIndex(0);
0050     } else if (mailPreferedFormatting == QLatin1StringView("TEXT")) {
0051         mMailPreferFormatting->setCurrentIndex(1);
0052     } else if (mailPreferedFormatting == QLatin1StringView("HTML")) {
0053         mMailPreferFormatting->setCurrentIndex(2);
0054     } else {
0055         mMailPreferFormatting->setCurrentIndex(0);
0056     }
0057 }
0058 
0059 void MessageFormattingWidget::storeContact(KContacts::Addressee &contact) const
0060 {
0061     QString mailPreferedFormatting;
0062     const int index = mMailPreferFormatting->currentIndex();
0063     if (index == 0) {
0064         // Nothing => remove custom variable
0065     } else if (index == 1) {
0066         mailPreferedFormatting = QStringLiteral("TEXT");
0067     } else if (index == 2) {
0068         mailPreferedFormatting = QStringLiteral("HTML");
0069     }
0070     Akonadi::Utils::storeCustom(contact, QStringLiteral("MailPreferedFormatting"), mailPreferedFormatting);
0071 
0072     QString mailAllowToRemoteContent;
0073     if (mAllowRemoteContent->isChecked()) {
0074         mailAllowToRemoteContent = QStringLiteral("TRUE");
0075     }
0076     Akonadi::Utils::storeCustom(contact, QStringLiteral("MailAllowToRemoteContent"), mailAllowToRemoteContent);
0077 }
0078 
0079 void MessageFormattingWidget::setReadOnly(bool readOnly)
0080 {
0081     mMailPreferFormatting->setEnabled(!readOnly);
0082     mAllowRemoteContent->setEnabled(!readOnly);
0083 }
0084 
0085 #include "moc_messageformattingwidget.cpp"