Akonadi

subscriptiondialog.cpp
1 /*
2  Copyright (c) 2007 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 "subscriptiondialog.h"
21 #include "ui_subscriptiondialog.h"
22 
23 #include "controlgui.h"
24 #include "recursivecollectionfilterproxymodel.h"
25 #include "subscriptionjob_p.h"
26 #include "subscriptionmodel_p.h"
27 #include "monitor.h"
28 
29 #include "akonadiwidgets_debug.h"
30 #include <KSharedConfig>
31 
32 #include <KLocalizedString>
33 #include <KConfigGroup>
34 
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include <QDialogButtonBox>
38 #include <QLineEdit>
39 #include <QPushButton>
40 #include <QHeaderView>
41 #include <QLabel>
42 #include <QTreeView>
43 #include <QCheckBox>
44 #include <KMessageBox>
45 #include <qnamespace.h>
46 
47 using namespace Akonadi;
48 
52 class Q_DECL_HIDDEN SubscriptionDialog::Private
53 {
54 public:
55  Private(SubscriptionDialog *parent)
56  : q(parent)
57  , model(&monitor, parent)
58  {
59  ui.setupUi(q);
60 
61  connect(&model, &SubscriptionModel::modelLoaded, q, [this]() {
62  filterRecursiveCollectionFilter.sort(0, Qt::AscendingOrder);
63  ui.collectionView->setEnabled(true);
64  ui.collectionView->expandAll();
65  ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
66  });
67 
68  filterRecursiveCollectionFilter.setSourceModel(&model);
69  filterRecursiveCollectionFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
70  filterRecursiveCollectionFilter.setSortRole(Qt::DisplayRole);
71  filterRecursiveCollectionFilter.setSortCaseSensitivity(Qt::CaseSensitive);
72  filterRecursiveCollectionFilter.setSortLocaleAware(true);
73 
74  ui.collectionView->setModel(&filterRecursiveCollectionFilter);
75  ui.searchLineEdit->setFocus();
76  q->connect(ui.searchLineEdit, &QLineEdit::textChanged,
77  q, [this](const QString &str) {
78  filterRecursiveCollectionFilter.setSearchPattern(str);
79  ui.collectionView->expandAll();
80  });
81  q->connect(ui.subscribedOnlyCheckBox, &QCheckBox::toggled,
82  q, [this](bool state) {
83  filterRecursiveCollectionFilter.setIncludeCheckedOnly(state);
84  });
85  q->connect(ui.subscribeButton, &QPushButton::clicked,
86  q, [this]() { toggleSubscribed(Qt::Checked); });
87  q->connect(ui.unsubscribeButton, &QPushButton::clicked,
88  q, [this]() { toggleSubscribed(Qt::Unchecked); });
89 
90  auto okButton = ui.buttonBox->button(QDialogButtonBox::Ok);
91  okButton->setEnabled(false);
92  connect(okButton, &QPushButton::clicked, q, [this] () { done(); });
93  }
94 
95  void done()
96  {
97  SubscriptionJob *job = new SubscriptionJob(q);
98  job->subscribe(model.subscribed());
99  job->unsubscribe(model.unsubscribed());
100  connect(job, &SubscriptionJob::result, q, [this](KJob *job) {
101  if (job->error()) {
102  qCWarning(AKONADIWIDGETS_LOG) << job->errorString();
103  KMessageBox::sorry(q, i18n("Failed to update subscription: %1", job->errorString()),
104  i18nc("@title", "Subscription Error"));
105  q->reject();
106  }
107  q->accept();
108  });
109  }
110 
111  void writeConfig()
112  {
113  KConfigGroup group(KSharedConfig::openConfig(), "SubscriptionDialog");
114  group.writeEntry("Size", q->size());
115  }
116 
117  void readConfig()
118  {
119  KConfigGroup group(KSharedConfig::openConfig(), "SubscriptionDialog");
120  const QSize sizeDialog = group.readEntry("Size", QSize(500, 400));
121  if (sizeDialog.isValid()) {
122  q->resize(sizeDialog);
123  }
124  }
125 
126  void toggleSubscribed(Qt::CheckState state)
127  {
128  const QModelIndexList list = ui.collectionView->selectionModel()->selectedIndexes();
129  for (const QModelIndex &index : list) {
130  model.setData(index, state, Qt::CheckStateRole);
131  }
132  ui.collectionView->setFocus();
133  }
134 
135 
136  SubscriptionDialog * const q;
137  Ui::SubscriptionDialog ui;
138 
139  Monitor monitor;
140  SubscriptionModel model;
141  RecursiveCollectionFilterProxyModel filterRecursiveCollectionFilter;
142 
143 };
144 
145 
148 {
149 }
150 
152  : QDialog(parent)
153  , d(new Private(this))
154 {
155  setAttribute(Qt::WA_DeleteOnClose);
156 
157  if (!mimetypes.isEmpty()) {
158  d->filterRecursiveCollectionFilter.addContentMimeTypeInclusionFilters(mimetypes);
159  }
161  d->readConfig();
162 }
163 
165 {
166  d->writeConfig();
167 }
168 
170 {
171  d->model.setShowHiddenCollections(showHidden);
172 }
173 
174 
175 #include "moc_subscriptiondialog.cpp"
bool isValid() const const
SubscriptionDialog(QWidget *parent=nullptr)
Creates a new subscription dialog.
virtual QString errorString() const
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void textChanged(const QString &text)
Local subscription dialog.
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
Definition: controlgui.cpp:266
~SubscriptionDialog()
Destroys the subscription dialog.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void showHiddenCollection(bool showHidden)
bool isEmpty() const const
void clicked(bool checked)
A model to filter out collections of non-matching content types.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
Monitors an item or collection for changes.
Definition: monitor.h:84
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
Helper integration between Akonadi and Qt.
void toggled(bool checked)
QObject * parent() const const
void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
int error() const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.