Akonadi

collectionpropertiesdialog.cpp
1 /*
2  Copyright (c) 2008 Volker Krause <[email protected]>
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 "akonadiwidgets_debug.h"
29 
30 
31 #include <KSharedConfig>
32 #include <KConfigGroup>
33 #include <QTabWidget>
34 
35 #include <QDialogButtonBox>
36 
37 using namespace Akonadi;
38 
42 class Q_DECL_HIDDEN CollectionPropertiesDialog::Private
43 {
44 public:
45  Private(CollectionPropertiesDialog *parent, const Akonadi::Collection &collection, const QStringList &pageNames);
46 
47  void init();
48 
49  static void registerBuiltinPages();
50 
51  void save()
52  {
53  const int numberOfTab(mTabWidget->count());
54  for (int i = 0; i < numberOfTab; ++i) {
55  CollectionPropertiesPage *page = static_cast<CollectionPropertiesPage *>(mTabWidget->widget(i));
56  page->save(mCollection);
57  }
58 
59  // We use WA_DeleteOnClose => Don't use dialog as parent otherwise we can't save modified collection.
60  CollectionModifyJob *job = new CollectionModifyJob(mCollection);
61  connect(job, &CollectionModifyJob::result, q, [this](KJob *job) { saveResult(job); });
62  Q_EMIT q->settingsSaved();
63  }
64 
65  void saveResult(KJob *job)
66  {
67  if (job->error()) {
68  // TODO
69  qCWarning(AKONADIWIDGETS_LOG) << job->errorString();
70  }
71  }
72 
73  void setCurrentPage(const QString &name)
74  {
75  const int numberOfTab(mTabWidget->count());
76  for (int i = 0; i < numberOfTab; ++i) {
77  QWidget *w = mTabWidget->widget(i);
78  if (w->objectName() == name) {
79  mTabWidget->setCurrentIndex(i);
80  break;
81  }
82  }
83  }
84 
85  CollectionPropertiesDialog *q = nullptr;
86  Collection mCollection;
87  QStringList mPageNames;
88  QTabWidget *mTabWidget = nullptr;
89 };
90 
92 
93 Q_GLOBAL_STATIC(CollectionPropertiesPageFactoryList, s_pages)
94 
95 static bool s_defaultPage = true;
96 
97 CollectionPropertiesDialog::Private::Private(CollectionPropertiesDialog *qq, const Akonadi::Collection &collection, const QStringList &pageNames)
98  : q(qq)
99  , mCollection(collection)
100  , mPageNames(pageNames)
101  , mTabWidget(nullptr)
102 {
103  if (s_defaultPage) {
104  registerBuiltinPages();
105  }
106 }
107 
108 void CollectionPropertiesDialog::Private::registerBuiltinPages()
109 {
110  static bool registered = false;
111 
112  if (registered) {
113  return;
114  }
115 
116  s_pages->append(new CollectionGeneralPropertiesPageFactory());
117  s_pages->append(new CachePolicyPageFactory());
118 
119  registered = true;
120 }
121 
122 void CollectionPropertiesDialog::Private::init()
123 {
124  QVBoxLayout *mainLayout = new QVBoxLayout(q);
125  q->setAttribute(Qt::WA_DeleteOnClose);
126  mTabWidget = new QTabWidget(q);
127  mainLayout->addWidget(mTabWidget);
128 
129  QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
130  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
131  okButton->setDefault(true);
132  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
133  q->connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
134  q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
135  mainLayout->addWidget(buttonBox);
136 
137  if (mPageNames.isEmpty()) { // default loading
138  for (CollectionPropertiesPageFactory *factory : qAsConst(*s_pages)) {
139  CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
140  if (page->canHandle(mCollection)) {
141  mTabWidget->addTab(page, page->pageTitle());
142  page->load(mCollection);
143  } else {
144  delete page;
145  }
146  }
147  } else { // custom loading
149 
150  for (CollectionPropertiesPageFactory *factory : qAsConst(*s_pages)) {
151  CollectionPropertiesPage *page = factory->createWidget(mTabWidget);
152  const QString pageName = page->objectName();
153 
154  if (page->canHandle(mCollection) && mPageNames.contains(pageName) && !pages.contains(pageName)) {
155  pages.insert(page->objectName(), page);
156  } else {
157  delete page;
158  }
159  }
160 
161  for (const QString &pageName : qAsConst(mPageNames)) {
162  CollectionPropertiesPage *page = pages.value(pageName);
163  if (page) {
164  mTabWidget->addTab(page, page->pageTitle());
165  page->load(mCollection);
166  }
167  }
168  }
169 
170  q->connect(buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, q, [this]() { save(); });
171  q->connect(buttonBox->button(QDialogButtonBox::Cancel), &QAbstractButton::clicked, q, &QObject::deleteLater);
172 
173  KConfigGroup group(KSharedConfig::openConfig(), "CollectionPropertiesDialog");
174  const QSize size = group.readEntry("Size", QSize());
175  if (size.isValid()) {
176  q->resize(size);
177  } else {
178  q->resize(q->sizeHint().width(), q->sizeHint().height());
179  }
180 
181 }
182 
184  : QDialog(parent)
185  , d(new Private(this, collection, QStringList()))
186 {
187  d->init();
188 }
189 
191  : QDialog(parent)
192  , d(new Private(this, collection, pages))
193 {
194  d->init();
195 }
196 
198 {
199  KConfigGroup group(KSharedConfig::openConfig(), "CollectionPropertiesDialog");
200  group.writeEntry("Size", size());
201  delete d;
202 }
203 
205 {
206  if (s_pages->isEmpty() && s_defaultPage) {
207  Private::registerBuiltinPages();
208  }
209  s_pages->append(factory);
210 }
211 
213 {
214  s_defaultPage = defaultPage;
215 }
216 
218 {
219  switch (page) {
220  case GeneralPage:
221  return QStringLiteral("Akonadi::CollectionGeneralPropertiesPage");
222  case CachePage:
223  return QStringLiteral("Akonadi::CachePolicyPage");
224  }
225 
226  return QString();
227 }
228 
230 {
231  d->setCurrentPage(name);
232 }
233 
234 #include "moc_collectionpropertiesdialog.cpp"
Job that modifies a collection in the Akonadi storage.
A factory class for collection properties dialog pages.
bool isValid() const const
QHash::iterator insert(const Key &key, const T &value)
virtual void save(Collection &collection)=0
Saves page content to the given collection.
virtual void reject()
virtual QString errorString() const
Represents a collection of PIM items.
Definition: collection.h:76
static QString defaultPageObjectName(DefaultPage page)
Returns the object name of one of the dialog&#39;s registered default pages.
A single page in a collection properties dialog.
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
virtual void load(const Collection &collection)=0
Loads the page content from the given collection.
~CollectionPropertiesDialog()
Destroys the collection properties dialog.
static void registerPage(CollectionPropertiesPageFactory *factory)
Register custom pages for the collection properties dialog.
void setCurrentPage(const QString &name)
Sets the page to be shown in the tab widget.
virtual bool canHandle(const Collection &collection) const
Checks if this page can actually handle the given collection.
QSize size() const const
QString pageTitle() const
Returns the page title.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
static void useDefaultPage(bool use)
Sets whether to use default page or not.
void clicked(bool checked)
DefaultPage
Enumerates the registered default pages which can be displayed.
void deleteLater()
void setShortcut(const QKeySequence &key)
virtual void accept()
const T value(const Key &key) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QCA_EXPORT void init()
KDEGAMES_EXPORT QAction * save(const QObject *recvr, const char *slot, QObject *parent)
Helper integration between Akonadi and Qt.
CollectionPropertiesDialog(const Collection &collection, QWidget *parent=nullptr)
Creates a new collection properties dialog.
QPushButton * button(QDialogButtonBox::StandardButton which) const const
bool contains(const Key &key) const const
A generic and extensible dialog for collection properties.
void result(KJob *job)
QObject * parent() const const
void setDefault(bool)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.