Akonadi

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

KDE's Doxygen guidelines are available online.