File indexing completed on 2025-01-05 04:47:09
0001 /* 0002 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "manageaccountwidget.h" 0008 0009 #include "agentconfigurationdialog.h" 0010 #include "agentfilterproxymodel.h" 0011 #include "agentinstance.h" 0012 #include "agentinstancecreatejob.h" 0013 #include "agentmanager.h" 0014 #include "agenttypedialog.h" 0015 0016 #include "ui_manageaccountwidget.h" 0017 0018 #include <KLocalizedString> 0019 #include <KMessageBox> 0020 #include <QAbstractItemDelegate> 0021 #include <QAbstractItemView> 0022 #include <QKeyEvent> 0023 #include <QPointer> 0024 0025 using namespace Akonadi; 0026 0027 class Akonadi::ManageAccountWidgetPrivate 0028 { 0029 public: 0030 QString mSpecialCollectionIdentifier; 0031 0032 QStringList mMimeTypeFilter; 0033 QStringList mCapabilityFilter; 0034 QStringList mExcludeCapabilities; 0035 0036 Ui::ManageAccountWidget ui; 0037 }; 0038 0039 ManageAccountWidget::ManageAccountWidget(QWidget *parent) 0040 : QWidget(parent) 0041 , d(new Akonadi::ManageAccountWidgetPrivate) 0042 { 0043 d->ui.setupUi(this); 0044 connect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount); 0045 0046 connect(d->ui.mModifyAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotModifySelectedAccount); 0047 0048 connect(d->ui.mRemoveAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRemoveSelectedAccount); 0049 connect(d->ui.mRestartAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRestartSelectedAccount); 0050 0051 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::clicked, this, &ManageAccountWidget::slotAccountSelected); 0052 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::doubleClicked, this, &ManageAccountWidget::slotModifySelectedAccount); 0053 0054 d->ui.mAccountList->view()->setSelectionMode(QAbstractItemView::SingleSelection); 0055 0056 connect(d->ui.mFilterAccount, &QLineEdit::textChanged, this, &ManageAccountWidget::slotSearchAgentType); 0057 0058 d->ui.mFilterAccount->installEventFilter(this); 0059 slotAccountSelected(d->ui.mAccountList->currentAgentInstance()); 0060 } 0061 0062 ManageAccountWidget::~ManageAccountWidget() = default; 0063 0064 void ManageAccountWidget::slotSearchAgentType(const QString &str) 0065 { 0066 d->ui.mAccountList->agentFilterProxyModel()->setFilterRegularExpression(str); 0067 } 0068 0069 void ManageAccountWidget::disconnectAddAccountButton() 0070 { 0071 disconnect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount); 0072 } 0073 0074 QPushButton *ManageAccountWidget::addAccountButton() const 0075 { 0076 return d->ui.mAddAccountButton; 0077 } 0078 0079 void ManageAccountWidget::setDescriptionLabelText(const QString &text) 0080 { 0081 d->ui.label->setText(text); 0082 } 0083 0084 bool ManageAccountWidget::eventFilter(QObject *obj, QEvent *event) 0085 { 0086 if (event->type() == QEvent::KeyPress && obj == d->ui.mFilterAccount) { 0087 auto key = static_cast<QKeyEvent *>(event); 0088 if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) { 0089 event->accept(); 0090 return true; 0091 } 0092 } 0093 return QWidget::eventFilter(obj, event); 0094 } 0095 0096 QAbstractItemView *ManageAccountWidget::view() const 0097 { 0098 return d->ui.mAccountList->view(); 0099 } 0100 0101 void ManageAccountWidget::setSpecialCollectionIdentifier(const QString &identifier) 0102 { 0103 d->mSpecialCollectionIdentifier = identifier; 0104 } 0105 0106 void ManageAccountWidget::slotAddAccount() 0107 { 0108 Akonadi::AgentTypeDialog dlg(this); 0109 0110 Akonadi::AgentFilterProxyModel *filter = dlg.agentFilterProxyModel(); 0111 for (const QString &filterStr : std::as_const(d->mMimeTypeFilter)) { 0112 filter->addMimeTypeFilter(filterStr); 0113 } 0114 for (const QString &capa : std::as_const(d->mCapabilityFilter)) { 0115 filter->addCapabilityFilter(capa); 0116 } 0117 for (const QString &capa : std::as_const(d->mExcludeCapabilities)) { 0118 filter->excludeCapabilities(capa); 0119 } 0120 if (dlg.exec()) { 0121 const Akonadi::AgentType agentType = dlg.agentType(); 0122 0123 if (agentType.isValid()) { 0124 auto job = new Akonadi::AgentInstanceCreateJob(agentType, this); 0125 job->configure(this); 0126 job->start(); 0127 } 0128 } 0129 } 0130 0131 QStringList ManageAccountWidget::excludeCapabilities() const 0132 { 0133 return d->mExcludeCapabilities; 0134 } 0135 0136 void ManageAccountWidget::setExcludeCapabilities(const QStringList &excludeCapabilities) 0137 { 0138 d->mExcludeCapabilities = excludeCapabilities; 0139 for (const QString &capability : std::as_const(d->mExcludeCapabilities)) { 0140 d->ui.mAccountList->agentFilterProxyModel()->excludeCapabilities(capability); 0141 } 0142 } 0143 0144 void ManageAccountWidget::setItemDelegate(QAbstractItemDelegate *delegate) 0145 { 0146 d->ui.mAccountList->view()->setItemDelegate(delegate); 0147 } 0148 0149 QStringList ManageAccountWidget::capabilityFilter() const 0150 { 0151 return d->mCapabilityFilter; 0152 } 0153 0154 void ManageAccountWidget::setCapabilityFilter(const QStringList &capabilityFilter) 0155 { 0156 d->mCapabilityFilter = capabilityFilter; 0157 for (const QString &capability : std::as_const(d->mCapabilityFilter)) { 0158 d->ui.mAccountList->agentFilterProxyModel()->addCapabilityFilter(capability); 0159 } 0160 } 0161 0162 QStringList ManageAccountWidget::mimeTypeFilter() const 0163 { 0164 return d->mMimeTypeFilter; 0165 } 0166 0167 void ManageAccountWidget::setMimeTypeFilter(const QStringList &mimeTypeFilter) 0168 { 0169 d->mMimeTypeFilter = mimeTypeFilter; 0170 for (const QString &mimeType : std::as_const(d->mMimeTypeFilter)) { 0171 d->ui.mAccountList->agentFilterProxyModel()->addMimeTypeFilter(mimeType); 0172 } 0173 } 0174 0175 void ManageAccountWidget::slotModifySelectedAccount() 0176 { 0177 Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance(); 0178 if (instance.isValid()) { 0179 QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, this)); 0180 dlg->exec(); 0181 delete dlg; 0182 } 0183 } 0184 0185 void ManageAccountWidget::slotRestartSelectedAccount() 0186 { 0187 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance(); 0188 if (instance.isValid()) { 0189 instance.restart(); 0190 } 0191 } 0192 0193 void ManageAccountWidget::slotRemoveSelectedAccount() 0194 { 0195 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance(); 0196 0197 const int rc = KMessageBox::questionTwoActions(this, 0198 i18n("Do you want to remove account '%1'?", instance.name()), 0199 i18nc("@title:window", "Remove account?"), 0200 KStandardGuiItem::remove(), 0201 KStandardGuiItem::cancel()); 0202 if (rc == KMessageBox::ButtonCode::SecondaryAction) { 0203 return; 0204 } 0205 0206 if (instance.isValid()) { 0207 Akonadi::AgentManager::self()->removeInstance(instance); 0208 } 0209 0210 slotAccountSelected(d->ui.mAccountList->currentAgentInstance()); 0211 } 0212 0213 void ManageAccountWidget::slotAccountSelected(const Akonadi::AgentInstance ¤t) 0214 { 0215 if (current.isValid()) { 0216 d->ui.mModifyAccountButton->setEnabled(!current.type().capabilities().contains(QLatin1StringView("NoConfig"))); 0217 d->ui.mRemoveAccountButton->setEnabled(d->mSpecialCollectionIdentifier != current.identifier()); 0218 // Restarting an agent is not possible if it's in Running status... (see AgentProcessInstance::restartWhenIdle) 0219 d->ui.mRestartAccountButton->setEnabled((current.status() != 1)); 0220 } else { 0221 d->ui.mModifyAccountButton->setEnabled(false); 0222 d->ui.mRemoveAccountButton->setEnabled(false); 0223 d->ui.mRestartAccountButton->setEnabled(false); 0224 } 0225 } 0226 0227 #include "moc_manageaccountwidget.cpp"