Warning, file /pim/mailcommon/src/collectionpage/collectionviewwidget.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 "collectionviewwidget.h"
0008 #include "mailcommon_debug.h"
0009 #include <KLocalizedString>
0010 #include <QFormLayout>
0011 
0012 #include <Akonadi/MessageFolderAttribute>
0013 #include <MessageList/AggregationComboBox>
0014 #include <MessageList/AggregationConfigButton>
0015 #include <MessageList/ThemeComboBox>
0016 #include <MessageList/ThemeConfigButton>
0017 
0018 #include <MessageViewer/Viewer>
0019 
0020 #include <QCheckBox>
0021 #include <QComboBox>
0022 #include <QRadioButton>
0023 using namespace MailCommon;
0024 CollectionViewWidget::CollectionViewWidget(QWidget *parent)
0025     : QWidget(parent)
0026 {
0027     auto topLayout = new QFormLayout(this);
0028     topLayout->setObjectName(QLatin1StringView("topLayout"));
0029     topLayout->setContentsMargins({});
0030 
0031     // sender or receiver column
0032     const QString senderReceiverColumnTip = i18n("Show Sender/Receiver Column in List of Messages");
0033 
0034     mShowSenderReceiverComboBox = new QComboBox(this);
0035     mShowSenderReceiverComboBox->setToolTip(senderReceiverColumnTip);
0036     mShowSenderReceiverComboBox->insertItem(0, i18nc("@item:inlistbox Show default value.", "Default"));
0037     mShowSenderReceiverComboBox->insertItem(1, i18nc("@item:inlistbox Show sender.", "Sender"));
0038     mShowSenderReceiverComboBox->insertItem(2, i18nc("@item:inlistbox Show receiver.", "Receiver"));
0039     topLayout->addRow(i18n("Sho&w column:"), mShowSenderReceiverComboBox);
0040 
0041     // message list aggregation
0042     mUseDefaultAggregationCheckBox = new QCheckBox(i18n("Use default message list aggregation:"), this);
0043     connect(mUseDefaultAggregationCheckBox, &QCheckBox::stateChanged, this, &CollectionViewWidget::slotAggregationCheckboxChanged);
0044     topLayout->addRow(QString(), mUseDefaultAggregationCheckBox);
0045 
0046     mAggregationComboBox = new MessageList::Utils::AggregationComboBox(this);
0047 
0048     using MessageList::Utils::AggregationConfigButton;
0049     auto aggregationConfigButton = new AggregationConfigButton(this, mAggregationComboBox);
0050     // Make sure any changes made in the aggregations configure dialog are reflected in the combo.
0051     connect(aggregationConfigButton, &AggregationConfigButton::configureDialogCompleted, this, &CollectionViewWidget::slotSelectFolderAggregation);
0052 
0053     auto aggregationLayout = new QHBoxLayout;
0054     aggregationLayout->addWidget(mAggregationComboBox, 1);
0055     aggregationLayout->addWidget(aggregationConfigButton, 0);
0056     topLayout->addRow(QString(), aggregationLayout);
0057 
0058     // message list theme
0059     mUseDefaultThemeCheckBox = new QCheckBox(i18n("Use default message list theme"), this);
0060     connect(mUseDefaultThemeCheckBox, &QCheckBox::stateChanged, this, &CollectionViewWidget::slotThemeCheckboxChanged);
0061     topLayout->addRow(QString(), mUseDefaultThemeCheckBox);
0062 
0063     mThemeComboBox = new MessageList::Utils::ThemeComboBox(this);
0064 
0065     using MessageList::Utils::ThemeConfigButton;
0066     auto themeConfigButton = new ThemeConfigButton(this, mThemeComboBox);
0067     // Make sure any changes made in the themes configure dialog are reflected in the combo.
0068     connect(themeConfigButton, &ThemeConfigButton::configureDialogCompleted, this, &CollectionViewWidget::slotSelectFolderTheme);
0069 
0070     auto themeLayout = new QHBoxLayout;
0071     themeLayout->addWidget(mThemeComboBox, 1);
0072     themeLayout->addWidget(themeConfigButton, 0);
0073     topLayout->addRow(QString(), themeLayout);
0074 
0075     // Message Default Format
0076     mPreferHtmlToText = new QRadioButton(i18n("Prefer HTML to text"), this);
0077     topLayout->addRow(i18n("Message format:"), mPreferHtmlToText);
0078     mPreferTextToHtml = new QRadioButton(i18n("Prefer text to HTML"), this);
0079     topLayout->addRow(QString(), mPreferTextToHtml);
0080     mUseGlobalSettings = new QRadioButton(i18n("Use Global Settings"), this);
0081     topLayout->addRow(QString(), mUseGlobalSettings);
0082 }
0083 
0084 CollectionViewWidget::~CollectionViewWidget() = default;
0085 
0086 void CollectionViewWidget::load(const Akonadi::Collection &col)
0087 {
0088     mCurrentCollection = col;
0089     mFolderCollection = MailCommon::FolderSettings::forCollection(col);
0090     if (col.hasAttribute<Akonadi::MessageFolderAttribute>()) {
0091         const bool outboundFolder = col.attribute<Akonadi::MessageFolderAttribute>()->isOutboundFolder();
0092         if (outboundFolder) {
0093             mShowSenderReceiverComboBox->setCurrentIndex(2);
0094         } else {
0095             mShowSenderReceiverComboBox->setCurrentIndex(1);
0096         }
0097     } else {
0098         mShowSenderReceiverComboBox->setCurrentIndex(0);
0099     }
0100     mShowSenderReceiverValue = mShowSenderReceiverComboBox->currentIndex();
0101 
0102     // message list aggregation
0103     slotSelectFolderAggregation();
0104 
0105     // message list theme
0106     slotSelectFolderTheme();
0107 
0108     const MessageViewer::Viewer::DisplayFormatMessage formatMessage = mFolderCollection->formatMessage();
0109     switch (formatMessage) {
0110     case MessageViewer::Viewer::Html:
0111         mPreferHtmlToText->setChecked(true);
0112         break;
0113     case MessageViewer::Viewer::Text:
0114         mPreferTextToHtml->setChecked(true);
0115         break;
0116     case MessageViewer::Viewer::UseGlobalSetting:
0117         mUseGlobalSettings->setChecked(true);
0118         break;
0119     default:
0120         qCDebug(MAILCOMMON_LOG) << "No settings defined";
0121         break;
0122     }
0123 }
0124 
0125 void CollectionViewWidget::save(Akonadi::Collection &col)
0126 {
0127     if (!mFolderCollection) {
0128         mFolderCollection = MailCommon::FolderSettings::forCollection(col);
0129     }
0130     const int currentIndex = mShowSenderReceiverComboBox->currentIndex();
0131     if (mShowSenderReceiverValue != currentIndex) {
0132         if (currentIndex == 1) {
0133             auto *messageFolder = col.attribute<Akonadi::MessageFolderAttribute>(Akonadi::Collection::AddIfMissing);
0134             messageFolder->setOutboundFolder(false);
0135         } else if (currentIndex == 2) {
0136             auto *messageFolder = col.attribute<Akonadi::MessageFolderAttribute>(Akonadi::Collection::AddIfMissing);
0137             messageFolder->setOutboundFolder(true);
0138         } else {
0139             col.removeAttribute<Akonadi::MessageFolderAttribute>();
0140         }
0141     }
0142     // message list theme
0143     const bool usePrivateTheme = !mUseDefaultThemeCheckBox->isChecked();
0144     mThemeComboBox->writeStorageModelConfig(QString::number(mCurrentCollection.id()), usePrivateTheme);
0145     // message list aggregation
0146     const bool usePrivateAggregation = !mUseDefaultAggregationCheckBox->isChecked();
0147     mAggregationComboBox->writeStorageModelConfig(QString::number(mCurrentCollection.id()), usePrivateAggregation);
0148 
0149     MessageViewer::Viewer::DisplayFormatMessage formatMessage = MessageViewer::Viewer::Unknown;
0150     if (mPreferHtmlToText->isChecked()) {
0151         formatMessage = MessageViewer::Viewer::Html;
0152     } else if (mPreferTextToHtml->isChecked()) {
0153         formatMessage = MessageViewer::Viewer::Text;
0154     } else if (mUseGlobalSettings->isChecked()) {
0155         formatMessage = MessageViewer::Viewer::UseGlobalSetting;
0156     } else {
0157         qCDebug(MAILCOMMON_LOG) << "No settings defined";
0158     }
0159     if (formatMessage != MessageViewer::Viewer::Unknown) {
0160         mFolderCollection->setFormatMessage(formatMessage);
0161         mFolderCollection->writeConfig();
0162     }
0163     mFolderCollection.reset();
0164 }
0165 
0166 void CollectionViewWidget::slotSelectFolderAggregation()
0167 {
0168     bool usesPrivateAggregation = false;
0169     mAggregationComboBox->readStorageModelConfig(mCurrentCollection, usesPrivateAggregation);
0170     mUseDefaultAggregationCheckBox->setChecked(!usesPrivateAggregation);
0171 }
0172 
0173 void CollectionViewWidget::slotSelectFolderTheme()
0174 {
0175     bool usesPrivateTheme = false;
0176     mThemeComboBox->readStorageModelConfig(mCurrentCollection, usesPrivateTheme);
0177     mUseDefaultThemeCheckBox->setChecked(!usesPrivateTheme);
0178 }
0179 
0180 void CollectionViewWidget::slotAggregationCheckboxChanged()
0181 {
0182     mAggregationComboBox->setEnabled(!mUseDefaultAggregationCheckBox->isChecked());
0183 }
0184 
0185 void CollectionViewWidget::slotThemeCheckboxChanged()
0186 {
0187     mThemeComboBox->setEnabled(!mUseDefaultThemeCheckBox->isChecked());
0188 }
0189 
0190 #include "moc_collectionviewwidget.cpp"