Akonadi

collectionpropertiesdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "collectionpropertiesdialog.h"
8
9#include "cachepolicy.h"
10#include "cachepolicypage.h"
11#include "collection.h"
12#include "collectiongeneralpropertiespage_p.h"
13#include "collectionmodifyjob.h"
14
15#include "akonadiwidgets_debug.h"
16
17#include <KConfigGroup>
18#include <KSharedConfig>
19#include <QTabWidget>
20
21#include <QDialogButtonBox>
22
23using namespace Akonadi;
24
25/**
26 * @internal
27 */
28class Akonadi::CollectionPropertiesDialogPrivate
29{
30public:
31 CollectionPropertiesDialogPrivate(CollectionPropertiesDialog *parent, const Akonadi::Collection &collection, const QStringList &pageNames);
32
33 void init();
34
35 static void registerBuiltinPages();
36
37 void save()
38 {
39 const int numberOfTab(mTabWidget->count());
40 for (int i = 0; i < numberOfTab; ++i) {
41 auto page = static_cast<CollectionPropertiesPage *>(mTabWidget->widget(i));
42 page->save(mCollection);
43 }
44
45 // We use WA_DeleteOnClose => Don't use dialog as parent otherwise we can't save modified collection.
46 auto job = new CollectionModifyJob(mCollection);
48 saveResult(job);
49 });
50 Q_EMIT q->settingsSaved();
51 }
52
53 void saveResult(KJob *job)
54 {
55 if (job->error()) {
56 // TODO
57 qCWarning(AKONADIWIDGETS_LOG) << job->errorString();
58 }
59 }
60
61 void setCurrentPage(const QString &name)
62 {
63 const int numberOfTab(mTabWidget->count());
64 for (int i = 0; i < numberOfTab; ++i) {
65 QWidget *w = mTabWidget->widget(i);
66 if (w->objectName() == name) {
67 mTabWidget->setCurrentIndex(i);
68 break;
69 }
70 }
71 }
72
74 Collection mCollection;
75 QStringList mPageNames;
76 QTabWidget *mTabWidget = nullptr;
77};
78
79class CollectionPropertiesPageFactoryList : public QList<CollectionPropertiesPageFactory *>
80{
81public:
82 explicit CollectionPropertiesPageFactoryList() = default;
83 CollectionPropertiesPageFactoryList(const CollectionPropertiesPageFactoryList &) = delete;
84 CollectionPropertiesPageFactoryList &operator=(const CollectionPropertiesPageFactoryList &) = delete;
85 ~CollectionPropertiesPageFactoryList()
86 {
87 qDeleteAll(*this);
88 }
89};
90
91Q_GLOBAL_STATIC(CollectionPropertiesPageFactoryList, s_pages) // NOLINT(readability-redundant-member-init)
92
93static bool s_defaultPage = true;
94
95CollectionPropertiesDialogPrivate::CollectionPropertiesDialogPrivate(CollectionPropertiesDialog *qq,
96 const Akonadi::Collection &collection,
97 const QStringList &pageNames)
98 : q(qq)
99 , mCollection(collection)
100 , mPageNames(pageNames)
101{
102 if (s_defaultPage) {
103 registerBuiltinPages();
104 }
105}
106
107void CollectionPropertiesDialogPrivate::registerBuiltinPages()
108{
109 static bool registered = false;
110
111 if (registered) {
112 return;
113 }
114
115 s_pages->append(new CollectionGeneralPropertiesPageFactory());
116 s_pages->append(new CachePolicyPageFactory());
117
118 registered = true;
119}
120
121void CollectionPropertiesDialogPrivate::init()
122{
123 auto mainLayout = new QVBoxLayout(q);
125 mTabWidget = new QTabWidget(q);
126 mainLayout->addWidget(mTabWidget);
127
129 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
130 okButton->setDefault(true);
131 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); // NOLINT(bugprone-suspicious-enum-usage)
134 mainLayout->addWidget(buttonBox);
135
136 if (mPageNames.isEmpty()) { // default loading
137 for (CollectionPropertiesPageFactory *factory : std::as_const(*s_pages)) {
138 CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
139 if (page->canHandle(mCollection)) {
140 mTabWidget->addTab(page, page->pageTitle());
141 page->load(mCollection);
142 } else {
143 delete page;
144 }
145 }
146 } else { // custom loading
148
149 for (CollectionPropertiesPageFactory *factory : std::as_const(*s_pages)) {
150 CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
151 const QString pageName = page->objectName();
152
153 if (page->canHandle(mCollection) && mPageNames.contains(pageName) && !pages.contains(pageName)) {
154 pages.insert(page->objectName(), page);
155 } else {
156 delete page;
157 }
158 }
159
160 for (const QString &pageName : std::as_const(mPageNames)) {
161 CollectionPropertiesPage *page = pages.value(pageName);
162 if (page) {
163 mTabWidget->addTab(page, page->pageTitle());
164 page->load(mCollection);
165 }
166 }
167 }
168
169 q->connect(buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, q, [this]() {
170 save();
171 });
173
174 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("CollectionPropertiesDialog"));
175 const QSize size = group.readEntry("Size", QSize());
176 if (size.isValid()) {
177 q->resize(size);
178 } else {
179 q->resize(q->sizeHint().width(), q->sizeHint().height());
180 }
181}
182
184 : QDialog(parent)
185 , d(new CollectionPropertiesDialogPrivate(this, collection, QStringList()))
186{
187 d->init();
188}
189
191 : QDialog(parent)
192 , d(new CollectionPropertiesDialogPrivate(this, collection, pages))
193{
194 d->init();
195}
196
198{
199 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("CollectionPropertiesDialog"));
200 group.writeEntry("Size", size());
201}
202
204{
205 if (s_pages->isEmpty() && s_defaultPage) {
206 CollectionPropertiesDialogPrivate::registerBuiltinPages();
207 }
208 s_pages->append(factory);
209}
210
212{
213 s_defaultPage = defaultPage;
214}
215
217{
218 switch (page) {
219 case GeneralPage:
220 return QStringLiteral("Akonadi::CollectionGeneralPropertiesPage");
221 case CachePage:
222 return QStringLiteral("Akonadi::CachePolicyPage");
223 }
224
225 return QString();
226}
227
229{
230 d->setCurrentPage(name);
231}
232
233#include "moc_collectionpropertiesdialog.cpp"
Job that modifies a collection in the Akonadi storage.
A generic and extensible dialog for collection properties.
CollectionPropertiesDialog(const Collection &collection, QWidget *parent=nullptr)
Creates a new collection properties dialog.
~CollectionPropertiesDialog() override
Destroys the collection properties dialog.
void setCurrentPage(const QString &name)
Sets the page to be shown in the tab widget.
DefaultPage
Enumerates the registered default pages which can be displayed.
static void useDefaultPage(bool use)
Sets whether to use default page or not.
static void registerPage(CollectionPropertiesPageFactory *factory)
Register custom pages for the collection properties dialog.
static QString defaultPageObjectName(DefaultPage page)
Returns the object name of one of the dialog's registered default pages.
A factory class for collection properties dialog pages.
A single page in a collection properties dialog.
virtual void load(const Collection &collection)=0
Loads the page content from the given collection.
virtual void save(Collection &collection)=0
Saves page content to the given collection.
QString pageTitle() const
Returns the page title.
virtual bool canHandle(const Collection &collection) const
Checks if this page can actually handle the given collection.
Represents a collection of PIM items.
Definition collection.h:62
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
virtual QString errorString() const
int error() const
void result(KJob *job)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Helper integration between Akonadi and Qt.
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
virtual void accept()
virtual void reject()
virtual QSize sizeHint() const const override
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void setDefault(bool)
int height() const const
bool isValid() const const
int width() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Key_Return
WA_DeleteOnClose
int addTab(QWidget *page, const QIcon &icon, const QString &label)
void setCurrentIndex(int index)
QWidget * widget(int index) const const
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void resize(const QSize &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.