Akonadi

subscriptiondialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "subscriptiondialog.h"
8 #include "ui_subscriptiondialog.h"
9 
10 #include "controlgui.h"
11 #include "monitor.h"
12 #include "recursivecollectionfilterproxymodel.h"
13 #include "subscriptionjob_p.h"
14 #include "subscriptionmodel_p.h"
15 
16 #include "akonadiwidgets_debug.h"
17 #include <KSharedConfig>
18 
19 #include <KConfigGroup>
20 #include <KLocalizedString>
21 
22 #include <KMessageBox>
23 #include <QCheckBox>
24 #include <QDialogButtonBox>
25 #include <QHBoxLayout>
26 #include <QHeaderView>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QPushButton>
30 #include <QTreeView>
31 #include <QVBoxLayout>
32 #include <qnamespace.h>
33 
34 using namespace Akonadi;
35 
36 /**
37  * @internal
38  */
39 class Akonadi::SubscriptionDialogPrivate
40 {
41 public:
42  explicit SubscriptionDialogPrivate(SubscriptionDialog *parent)
43  : q(parent)
44  , model(&monitor, parent)
45  {
46  ui.setupUi(q);
47 
48  QObject::connect(&model, &SubscriptionModel::modelLoaded, q, [this]() {
49  filterRecursiveCollectionFilter.sort(0, Qt::AscendingOrder);
50  ui.collectionView->setEnabled(true);
51  ui.collectionView->expandAll();
52  ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
53  });
54 
55  filterRecursiveCollectionFilter.setSourceModel(&model);
56  filterRecursiveCollectionFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
57  filterRecursiveCollectionFilter.setSortRole(Qt::DisplayRole);
58  filterRecursiveCollectionFilter.setSortCaseSensitivity(Qt::CaseSensitive);
59  filterRecursiveCollectionFilter.setSortLocaleAware(true);
60  filterRecursiveCollectionFilter.setExcludeUnifiedMailBox(true);
61 
62  ui.collectionView->setModel(&filterRecursiveCollectionFilter);
63  ui.searchLineEdit->setFocus();
64  q->connect(ui.searchLineEdit, &QLineEdit::textChanged, q, [this](const QString &str) {
65  filterRecursiveCollectionFilter.setSearchPattern(str);
66  ui.collectionView->expandAll();
67  });
68  q->connect(ui.subscribedOnlyCheckBox, &QCheckBox::toggled, q, [this](bool state) {
69  filterRecursiveCollectionFilter.setIncludeCheckedOnly(state);
70  });
71  q->connect(ui.subscribeButton, &QPushButton::clicked, q, [this]() {
72  toggleSubscribed(Qt::Checked);
73  });
74  q->connect(ui.unsubscribeButton, &QPushButton::clicked, q, [this]() {
75  toggleSubscribed(Qt::Unchecked);
76  });
77 
78  auto okButton = ui.buttonBox->button(QDialogButtonBox::Ok);
79  okButton->setEnabled(false);
80  QObject::connect(okButton, &QPushButton::clicked, q, [this]() {
81  done();
82  });
83  }
84 
85  void done()
86  {
87  auto job = new SubscriptionJob(q);
88  job->subscribe(model.subscribed());
89  job->unsubscribe(model.unsubscribed());
90  QObject::connect(job, &SubscriptionJob::result, q, [this](KJob *job) {
91  if (job->error()) {
92  qCWarning(AKONADIWIDGETS_LOG) << job->errorString();
93  KMessageBox::sorry(q, i18n("Failed to update subscription: %1", job->errorString()), i18nc("@title", "Subscription Error"));
94  q->reject();
95  }
96  q->accept();
97  });
98  }
99 
100  void writeConfig() const
101  {
102  KConfigGroup group(KSharedConfig::openStateConfig(), "SubscriptionDialog");
103  group.writeEntry("Size", q->size());
104  }
105 
106  void readConfig() const
107  {
108  KConfigGroup group(KSharedConfig::openStateConfig(), "SubscriptionDialog");
109  const QSize sizeDialog = group.readEntry("Size", QSize(500, 400));
110  if (sizeDialog.isValid()) {
111  q->resize(sizeDialog);
112  }
113  }
114 
115  void toggleSubscribed(Qt::CheckState state)
116  {
117  const QModelIndexList list = ui.collectionView->selectionModel()->selectedIndexes();
118  for (const QModelIndex &index : list) {
119  model.setData(index, state, Qt::CheckStateRole);
120  }
121  ui.collectionView->setFocus();
122  }
123 
124  SubscriptionDialog *const q;
125  Ui::SubscriptionDialog ui;
126 
127  Monitor monitor;
128  SubscriptionModel model;
129  RecursiveCollectionFilterProxyModel filterRecursiveCollectionFilter;
130 };
131 
133  : SubscriptionDialog({}, parent)
134 {
135 }
136 
138  : QDialog(parent)
139  , d(new SubscriptionDialogPrivate(this))
140 {
142 
143  if (!mimetypes.isEmpty()) {
144  d->filterRecursiveCollectionFilter.addContentMimeTypeInclusionFilters(mimetypes);
145  }
147  d->readConfig();
148 }
149 
151 {
152  d->writeConfig();
153 }
154 
156 {
157  d->model.setShowHiddenCollections(showHidden);
158 }
159 
160 #include "moc_subscriptiondialog.cpp"
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:243
SubscriptionDialog(QWidget *parent=nullptr)
Creates a new subscription dialog.
A model to filter out collections of non-matching content types.
DisplayRole
bool isValid() const const
~SubscriptionDialog() override
Destroys the subscription dialog.
CaseInsensitive
void clicked(bool checked)
AscendingOrder
void readConfig()
void setAttribute(Qt::WidgetAttribute attribute, bool on)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
Monitors an item or collection for changes.
Definition: monitor.h:71
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void toggled(bool checked)
Local subscription dialog.
void showHiddenCollection(bool showHidden)
void textChanged(const QString &text)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
bool isEmpty() const const
CheckState
int error() const
WA_DeleteOnClose
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.