File indexing completed on 2025-01-05 04:47:11

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "subscriptiondialog.h"
0008 #include "ui_subscriptiondialog.h"
0009 
0010 #include "controlgui.h"
0011 #include "monitor.h"
0012 #include "recursivecollectionfilterproxymodel.h"
0013 #include "subscriptionjob_p.h"
0014 #include "subscriptionmodel_p.h"
0015 
0016 #include "akonadiwidgets_debug.h"
0017 #include <KSharedConfig>
0018 
0019 #include <KConfigGroup>
0020 #include <KLocalizedString>
0021 
0022 #include <KMessageBox>
0023 #include <QCheckBox>
0024 #include <QDialogButtonBox>
0025 #include <QHBoxLayout>
0026 #include <QHeaderView>
0027 #include <QLabel>
0028 #include <QLineEdit>
0029 #include <QPushButton>
0030 #include <QTreeView>
0031 #include <QVBoxLayout>
0032 #include <qnamespace.h>
0033 
0034 using namespace Akonadi;
0035 
0036 namespace
0037 {
0038 static const char mySubscriptionDialogGroupName[] = "SubscriptionDialog";
0039 }
0040 /**
0041  * @internal
0042  */
0043 class Akonadi::SubscriptionDialogPrivate
0044 {
0045 public:
0046     explicit SubscriptionDialogPrivate(SubscriptionDialog *parent)
0047         : q(parent)
0048         , model(&monitor, parent)
0049     {
0050         ui.setupUi(q);
0051 
0052         QObject::connect(&model, &SubscriptionModel::modelLoaded, q, [this]() {
0053             filterRecursiveCollectionFilter.sort(0, Qt::AscendingOrder);
0054             ui.collectionView->setEnabled(true);
0055             ui.collectionView->expandAll();
0056             ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0057         });
0058 
0059         filterRecursiveCollectionFilter.setSourceModel(&model);
0060         filterRecursiveCollectionFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
0061         filterRecursiveCollectionFilter.setSortRole(Qt::DisplayRole);
0062         filterRecursiveCollectionFilter.setSortCaseSensitivity(Qt::CaseSensitive);
0063         filterRecursiveCollectionFilter.setSortLocaleAware(true);
0064         filterRecursiveCollectionFilter.setExcludeUnifiedMailBox(true);
0065 
0066         ui.collectionView->setModel(&filterRecursiveCollectionFilter);
0067         ui.searchLineEdit->setFocus();
0068         q->connect(ui.searchLineEdit, &QLineEdit::textChanged, q, [this](const QString &str) {
0069             filterRecursiveCollectionFilter.setSearchPattern(str);
0070             ui.collectionView->expandAll();
0071         });
0072         q->connect(ui.subscribedOnlyCheckBox, &QCheckBox::toggled, q, [this](bool state) {
0073             filterRecursiveCollectionFilter.setIncludeCheckedOnly(state);
0074         });
0075         q->connect(ui.subscribeButton, &QPushButton::clicked, q, [this]() {
0076             toggleSubscribed(Qt::Checked);
0077         });
0078         q->connect(ui.unsubscribeButton, &QPushButton::clicked, q, [this]() {
0079             toggleSubscribed(Qt::Unchecked);
0080         });
0081 
0082         auto okButton = ui.buttonBox->button(QDialogButtonBox::Ok);
0083         okButton->setEnabled(false);
0084         QObject::connect(okButton, &QPushButton::clicked, q, [this]() {
0085             done();
0086         });
0087     }
0088 
0089     void done()
0090     {
0091         auto job = new SubscriptionJob(q);
0092         job->subscribe(model.subscribed());
0093         job->unsubscribe(model.unsubscribed());
0094         QObject::connect(job, &SubscriptionJob::result, q, [this](KJob *job) {
0095             if (job->error()) {
0096                 qCWarning(AKONADIWIDGETS_LOG) << job->errorString();
0097                 KMessageBox::error(q, i18n("Failed to update subscription: %1", job->errorString()), i18nc("@title:window", "Subscription Error"));
0098                 q->reject();
0099             }
0100             q->accept();
0101         });
0102     }
0103 
0104     void writeConfig() const
0105     {
0106         KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySubscriptionDialogGroupName));
0107         group.writeEntry("Size", q->size());
0108     }
0109 
0110     void readConfig() const
0111     {
0112         KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySubscriptionDialogGroupName));
0113         const QSize sizeDialog = group.readEntry("Size", QSize(500, 400));
0114         if (sizeDialog.isValid()) {
0115             q->resize(sizeDialog);
0116         }
0117     }
0118 
0119     void toggleSubscribed(Qt::CheckState state)
0120     {
0121         const QModelIndexList list = ui.collectionView->selectionModel()->selectedIndexes();
0122         for (const QModelIndex &index : list) {
0123             model.setData(index, state, Qt::CheckStateRole);
0124         }
0125         ui.collectionView->setFocus();
0126     }
0127 
0128     SubscriptionDialog *const q;
0129     Ui::SubscriptionDialog ui;
0130 
0131     Monitor monitor;
0132     SubscriptionModel model;
0133     RecursiveCollectionFilterProxyModel filterRecursiveCollectionFilter;
0134 };
0135 
0136 SubscriptionDialog::SubscriptionDialog(QWidget *parent)
0137     : SubscriptionDialog({}, parent)
0138 {
0139 }
0140 
0141 SubscriptionDialog::SubscriptionDialog(const QStringList &mimetypes, QWidget *parent)
0142     : QDialog(parent)
0143     , d(new SubscriptionDialogPrivate(this))
0144 {
0145     setAttribute(Qt::WA_DeleteOnClose);
0146 
0147     if (!mimetypes.isEmpty()) {
0148         d->filterRecursiveCollectionFilter.addContentMimeTypeInclusionFilters(mimetypes);
0149     }
0150     ControlGui::widgetNeedsAkonadi(this);
0151     d->readConfig();
0152 }
0153 
0154 SubscriptionDialog::~SubscriptionDialog()
0155 {
0156     d->writeConfig();
0157 }
0158 
0159 void SubscriptionDialog::showHiddenCollection(bool showHidden)
0160 {
0161     d->model.setShowHiddenCollections(showHidden);
0162 }
0163 
0164 #include "moc_subscriptiondialog.cpp"