Messagelib

aggregationcombobox.cpp
1/* SPDX-FileCopyrightText: 2009 James Bendig <james@imptalk.com>
2
3 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4*/
5#include "aggregationcombobox.h"
6#include "aggregationcombobox_p.h"
7
8#include "core/aggregation.h"
9#include "core/manager.h"
10#include "messagelistsettings.h"
11#include "storagemodel.h"
12
13using namespace MessageList::Core;
14using namespace MessageList::Utils;
15
16AggregationComboBox::AggregationComboBox(QWidget *parent)
17 : QComboBox(parent)
18 , d(new AggregationComboBoxPrivate(this))
19{
20 if (Manager::instance()) {
21 d->slotLoadAggregations();
22 } else {
23 setEnabled(false);
24 }
25}
26
27AggregationComboBox::~AggregationComboBox() = default;
28
29QString AggregationComboBox::currentAggregation() const
30{
31 return itemData(currentIndex()).toString();
32}
33
34void AggregationComboBox::writeDefaultConfig() const
35{
36 KConfigGroup group(MessageListSettings::self()->config(), QStringLiteral("MessageListView::StorageModelAggregations"));
37
38 const QString aggregationID = currentAggregation();
39 group.writeEntry(QStringLiteral("DefaultSet"), aggregationID);
40
41 if (Manager::instance()) {
42 Manager::instance()->aggregationsConfigurationCompleted();
43 }
44}
45
46void AggregationComboBox::writeStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool isPrivateSetting) const
47{
48 writeStorageModelConfig(storageModel->id(), isPrivateSetting);
49}
50
51void AggregationComboBox::writeStorageModelConfig(const QString &id, bool isPrivateSetting) const
52{
53 if (Manager::instance()) {
54 // message list aggregation
55 QString aggregationID;
56 if (isPrivateSetting) {
57 aggregationID = currentAggregation();
58 } else { // explicitly use default aggregation id when using default aggregation.
59 aggregationID = Manager::instance()->defaultAggregation()->id();
60 }
61 Manager::instance()->saveAggregationForStorageModel(id, aggregationID, isPrivateSetting);
62 Manager::instance()->aggregationsConfigurationCompleted();
63 }
64}
65
66void AggregationComboBox::readStorageModelConfig(const QString &id, bool &isPrivateSetting)
67{
68 if (Manager::instance()) {
69 const Aggregation *aggregation = Manager::instance()->aggregationForStorageModel(id, &isPrivateSetting);
70 d->setCurrentAggregation(aggregation);
71 }
72}
73
74void AggregationComboBox::readStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool &isPrivateSetting)
75{
76 readStorageModelConfig(storageModel->id(), isPrivateSetting);
77}
78
79void AggregationComboBox::readStorageModelConfig(const Akonadi::Collection &col, bool &isPrivateSetting)
80{
81 if (col.isValid()) {
82 readStorageModelConfig(QString::number(col.id()), isPrivateSetting);
83 }
84}
85
86void AggregationComboBox::selectDefault()
87{
88 if (Manager::instance()) {
89 const Aggregation *defaultAggregation = Manager::instance()->defaultAggregation();
90 d->setCurrentAggregation(defaultAggregation);
91 }
92}
93
94void AggregationComboBox::slotLoadAggregations()
95{
96 d->slotLoadAggregations();
97}
98
99void AggregationComboBoxPrivate::slotLoadAggregations()
100{
101 if (!Manager::instance()) {
102 return;
103 }
104 q->clear();
105
106 // Get all message list aggregations and sort them into alphabetical order.
107 QList<Aggregation *> aggregations = Manager::instance()->aggregations().values();
108 std::sort(aggregations.begin(), aggregations.end(), MessageList::Core::Aggregation::compareName);
109
110 for (const Aggregation *aggregation : std::as_const(aggregations)) {
111 q->addItem(aggregation->name(), QVariant(aggregation->id()));
112 }
113}
114
115void AggregationComboBoxPrivate::setCurrentAggregation(const Aggregation *aggregation)
116{
117 Q_ASSERT(aggregation != nullptr);
118
119 const QString aggregationID = aggregation->id();
120 const int aggregationIndex = q->findData(QVariant(aggregationID));
121 q->setCurrentIndex(aggregationIndex);
122}
123
124#include "moc_aggregationcombobox.cpp"
bool isValid() const
A set of aggregation options that can be applied to the MessageList::Model in a single shot.
Definition aggregation.h:29
const QString & id() const
Returns the unique id of this OptionSet.
Definition optionset.h:51
const QString & name() const
Returns the name of this OptionSet.
Definition optionset.h:59
The QAbstractItemModel based interface that you need to provide for your storage to work with Message...
virtual QString id() const =0
Returns an unique id for this Storage collection.
The implementation independent part of the MessageList library.
Definition aggregation.h:22
QVariant itemData(int index, int role) const const
iterator begin()
iterator end()
QString number(double n, char format, int precision)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.