1/*
2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "collectionpropertiesdialog.h"
21
22#include "cachepolicy.h"
23#include "cachepolicypage.h"
24#include "collection.h"
25#include "collectiongeneralpropertiespage_p.h"
26#include "collectionmodifyjob.h"
27
28#include <kdebug.h>
29#include <kglobal.h>
30#include <ksharedconfig.h>
31#include <ktabwidget.h>
32
33#include <QBoxLayout>
34
35using namespace Akonadi;
36
37/**
38 * @internal
39 */
40class CollectionPropertiesDialog::Private
41{
42public:
43 Private(CollectionPropertiesDialog *parent, const Akonadi::Collection &collection, const QStringList &pageNames);
44
45 void init();
46
47 static void registerBuiltinPages();
48
49 void save()
50 {
51 for (int i = 0; i < mTabWidget->count(); ++i) {
52 CollectionPropertiesPage *page = static_cast<CollectionPropertiesPage *>(mTabWidget->widget(i));
53 page->save(mCollection);
54 }
55
56 CollectionModifyJob *job = new CollectionModifyJob(mCollection, q);
57 connect(job, SIGNAL(result(KJob*)), q, SLOT(saveResult(KJob*)));
58 }
59
60 void saveResult(KJob *job)
61 {
62 if (job->error()) {
63 // TODO
64 kWarning() << job->errorString();
65 }
66 q->deleteLater();
67 }
68
69 void setCurrentPage(const QString &name)
70 {
71 for (int i = 0; i < mTabWidget->count(); ++i) {
72 QWidget *w = mTabWidget->widget(i);
73 if (w->objectName() == name) {
74 mTabWidget->setCurrentIndex(i);
75 break;
76 }
77 }
78 }
79
80 CollectionPropertiesDialog *q;
81 Collection mCollection;
82 QStringList mPageNames;
83 KTabWidget *mTabWidget;
84};
85
86typedef QList<CollectionPropertiesPageFactory *> CollectionPropertiesPageFactoryList;
87
88K_GLOBAL_STATIC(CollectionPropertiesPageFactoryList, s_pages)
89
90static bool s_defaultPage = true;
91
92CollectionPropertiesDialog::Private::Private(CollectionPropertiesDialog *qq, const Akonadi::Collection &collection, const QStringList &pageNames)
93 : q(qq)
94 , mCollection(collection)
95 , mPageNames(pageNames)
96 , mTabWidget(0)
97{
98 if (s_defaultPage) {
99 registerBuiltinPages();
100 }
101}
102
103void CollectionPropertiesDialog::Private::registerBuiltinPages()
104{
105 static bool registered = false;
106
107 if (registered) {
108 return;
109 }
110
111 s_pages->append(new CollectionGeneralPropertiesPageFactory());
112 s_pages->append(new CachePolicyPageFactory());
113
114 registered = true;
115}
116
117void CollectionPropertiesDialog::Private::init()
118{
119 QBoxLayout *layout = new QHBoxLayout(q->mainWidget());
120 layout->setMargin(0);
121 mTabWidget = new KTabWidget(q->mainWidget());
122 layout->addWidget(mTabWidget);
123
124 if (mPageNames.isEmpty()) { // default loading
125 foreach (CollectionPropertiesPageFactory *factory, *s_pages) {
126 CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
127 if (page->canHandle(mCollection)) {
128 mTabWidget->addTab(page, page->pageTitle());
129 page->load(mCollection);
130 } else {
131 delete page;
132 }
133 }
134 } else { // custom loading
135 QHash<QString, CollectionPropertiesPage *> pages;
136
137 foreach (CollectionPropertiesPageFactory *factory, *s_pages) {
138 CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
139 const QString pageName = page->objectName();
140
141 if (page->canHandle(mCollection) && mPageNames.contains(pageName) && !pages.contains(pageName)) {
142 pages.insert(page->objectName(), page);
143 } else {
144 delete page;
145 }
146 }
147
148 foreach (const QString &pageName, mPageNames) {
149 CollectionPropertiesPage *page = pages.value(pageName);
150 if (page) {
151 mTabWidget->addTab(page, page->pageTitle());
152 page->load(mCollection);
153 }
154 }
155 }
156
157 q->connect(q, SIGNAL(okClicked()), SLOT(save()));
158 q->connect(q, SIGNAL(cancelClicked()), SLOT(deleteLater()));
159
160 KConfigGroup group(KGlobal::config(), "CollectionPropertiesDialog");
161 const QSize size = group.readEntry("Size", QSize());
162 if (size.isValid()) {
163 q->resize(size);
164 } else {
165 q->resize(q->sizeHint().width(), q->sizeHint().height());
166 }
167
168}
169
170CollectionPropertiesDialog::CollectionPropertiesDialog(const Collection &collection, QWidget *parent)
171 : KDialog(parent)
172 , d(new Private(this, collection, QStringList()))
173{
174 d->init();
175}
176
177CollectionPropertiesDialog::CollectionPropertiesDialog(const Collection &collection, const QStringList &pages, QWidget *parent)
178 : KDialog(parent)
179 , d(new Private(this, collection, pages))
180{
181 d->init();
182}
183
184CollectionPropertiesDialog::~CollectionPropertiesDialog()
185{
186 KConfigGroup group(KGlobal::config(), "CollectionPropertiesDialog");
187 group.writeEntry("Size", size());
188 delete d;
189}
190
191void CollectionPropertiesDialog::registerPage(CollectionPropertiesPageFactory *factory)
192{
193 if (s_pages->isEmpty() && s_defaultPage) {
194 Private::registerBuiltinPages();
195 }
196 s_pages->append(factory);
197}
198
199void CollectionPropertiesDialog::useDefaultPage(bool defaultPage)
200{
201 s_defaultPage = defaultPage;
202}
203
204QString CollectionPropertiesDialog::defaultPageObjectName(DefaultPage page)
205{
206 switch (page) {
207 case GeneralPage:
208 return QLatin1String("Akonadi::CollectionGeneralPropertiesPage");
209 case CachePage:
210 return QLatin1String("Akonadi::CachePolicyPage");
211 }
212
213 return QString();
214}
215
216void CollectionPropertiesDialog::setCurrentPage(const QString &name)
217{
218 d->setCurrentPage(name);
219}
220
221#include "moc_collectionpropertiesdialog.cpp"
222