File indexing completed on 2024-12-15 04:54:44

0001 /* SPDX-FileCopyrightText: 2009 James Bendig <james@imptalk.com>
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 #include "aggregationcombobox.h"
0006 #include "aggregationcombobox_p.h"
0007 
0008 #include "core/aggregation.h"
0009 #include "core/manager.h"
0010 #include "messagelistsettings.h"
0011 #include "storagemodel.h"
0012 
0013 using namespace MessageList::Core;
0014 using namespace MessageList::Utils;
0015 
0016 AggregationComboBox::AggregationComboBox(QWidget *parent)
0017     : QComboBox(parent)
0018     , d(new AggregationComboBoxPrivate(this))
0019 {
0020     if (Manager::instance()) {
0021         d->slotLoadAggregations();
0022     } else {
0023         setEnabled(false);
0024     }
0025 }
0026 
0027 AggregationComboBox::~AggregationComboBox() = default;
0028 
0029 QString AggregationComboBox::currentAggregation() const
0030 {
0031     return itemData(currentIndex()).toString();
0032 }
0033 
0034 void AggregationComboBox::writeDefaultConfig() const
0035 {
0036     KConfigGroup group(MessageListSettings::self()->config(), QStringLiteral("MessageListView::StorageModelAggregations"));
0037 
0038     const QString aggregationID = currentAggregation();
0039     group.writeEntry(QStringLiteral("DefaultSet"), aggregationID);
0040 
0041     if (Manager::instance()) {
0042         Manager::instance()->aggregationsConfigurationCompleted();
0043     }
0044 }
0045 
0046 void AggregationComboBox::writeStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool isPrivateSetting) const
0047 {
0048     writeStorageModelConfig(storageModel->id(), isPrivateSetting);
0049 }
0050 
0051 void AggregationComboBox::writeStorageModelConfig(const QString &id, bool isPrivateSetting) const
0052 {
0053     if (Manager::instance()) {
0054         // message list aggregation
0055         QString aggregationID;
0056         if (isPrivateSetting) {
0057             aggregationID = currentAggregation();
0058         } else { // explicitly use default aggregation id when using default aggregation.
0059             aggregationID = Manager::instance()->defaultAggregation()->id();
0060         }
0061         Manager::instance()->saveAggregationForStorageModel(id, aggregationID, isPrivateSetting);
0062         Manager::instance()->aggregationsConfigurationCompleted();
0063     }
0064 }
0065 
0066 void AggregationComboBox::readStorageModelConfig(const QString &id, bool &isPrivateSetting)
0067 {
0068     if (Manager::instance()) {
0069         const Aggregation *aggregation = Manager::instance()->aggregationForStorageModel(id, &isPrivateSetting);
0070         d->setCurrentAggregation(aggregation);
0071     }
0072 }
0073 
0074 void AggregationComboBox::readStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool &isPrivateSetting)
0075 {
0076     readStorageModelConfig(storageModel->id(), isPrivateSetting);
0077 }
0078 
0079 void AggregationComboBox::readStorageModelConfig(const Akonadi::Collection &col, bool &isPrivateSetting)
0080 {
0081     if (col.isValid()) {
0082         readStorageModelConfig(QString::number(col.id()), isPrivateSetting);
0083     }
0084 }
0085 
0086 void AggregationComboBox::selectDefault()
0087 {
0088     if (Manager::instance()) {
0089         const Aggregation *defaultAggregation = Manager::instance()->defaultAggregation();
0090         d->setCurrentAggregation(defaultAggregation);
0091     }
0092 }
0093 
0094 void AggregationComboBox::slotLoadAggregations()
0095 {
0096     d->slotLoadAggregations();
0097 }
0098 
0099 void AggregationComboBoxPrivate::slotLoadAggregations()
0100 {
0101     if (!Manager::instance()) {
0102         return;
0103     }
0104     q->clear();
0105 
0106     // Get all message list aggregations and sort them into alphabetical order.
0107     QList<Aggregation *> aggregations = Manager::instance()->aggregations().values();
0108     std::sort(aggregations.begin(), aggregations.end(), MessageList::Core::Aggregation::compareName);
0109 
0110     for (const Aggregation *aggregation : std::as_const(aggregations)) {
0111         q->addItem(aggregation->name(), QVariant(aggregation->id()));
0112     }
0113 }
0114 
0115 void AggregationComboBoxPrivate::setCurrentAggregation(const Aggregation *aggregation)
0116 {
0117     Q_ASSERT(aggregation != nullptr);
0118 
0119     const QString aggregationID = aggregation->id();
0120     const int aggregationIndex = q->findData(QVariant(aggregationID));
0121     q->setCurrentIndex(aggregationIndex);
0122 }
0123 
0124 #include "moc_aggregationcombobox.cpp"