Messagelib

aggregationcombobox.cpp
1 /* SPDX-FileCopyrightText: 2009 James Bendig <[email protected]>
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 
13 using namespace MessageList::Core;
14 using namespace MessageList::Utils;
15 
16 AggregationComboBox::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 
27 AggregationComboBox::~AggregationComboBox() = default;
28 
29 QString AggregationComboBox::currentAggregation() const
30 {
31  return itemData(currentIndex()).toString();
32 }
33 
34 void AggregationComboBox::writeDefaultConfig() const
35 {
36  KConfigGroup group(MessageListSettings::self()->config(), "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 
46 void AggregationComboBox::writeStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool isPrivateSetting) const
47 {
48  writeStorageModelConfig(storageModel->id(), isPrivateSetting);
49 }
50 
51 void 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 
66 void 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 
74 void AggregationComboBox::readStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool &isPrivateSetting)
75 {
76  readStorageModelConfig(storageModel->id(), isPrivateSetting);
77 }
78 
79 void AggregationComboBox::readStorageModelConfig(const Akonadi::Collection &col, bool &isPrivateSetting)
80 {
81  if (col.isValid()) {
82  readStorageModelConfig(QString::number(col.id()), isPrivateSetting);
83  }
84 }
85 
86 void AggregationComboBox::selectDefault()
87 {
88  if (Manager::instance()) {
89  const Aggregation *defaultAggregation = Manager::instance()->defaultAggregation();
90  d->setCurrentAggregation(defaultAggregation);
91  }
92 }
93 
94 void AggregationComboBox::slotLoadAggregations()
95 {
96  d->slotLoadAggregations();
97 }
98 
99 void 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 
115 void 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"
A set of aggregation options that can be applied to the MessageList::Model in a single shot...
Definition: aggregation.h:28
const QString & id() const
Returns the unique id of this OptionSet.
Definition: optionset.h:51
bool isValid() const
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
KSharedConfigPtr config()
const QString & name() const
Returns the name of this OptionSet.
Definition: optionset.h:59
QString number(int n, int base)
QList::iterator end()
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.
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.