File indexing completed on 2025-01-19 04:47:01

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectimapcreatefolderjob.h"
0008 #include "imapfoldercompletionplugin_debug.h"
0009 #include "sessionuiproxy.h"
0010 #include <KIMAP/CreateJob>
0011 #include <KIMAP/LoginJob>
0012 #include <KIMAP/Session>
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 SelectImapCreateFolderJob::SelectImapCreateFolderJob(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 SelectImapCreateFolderJob::~SelectImapCreateFolderJob() = default;
0022 
0023 void SelectImapCreateFolderJob::start()
0024 {
0025     if (mSieveImapAccount.isValid() && !mNewFolderName.isEmpty()) {
0026         mSession = new KIMAP::Session(mSieveImapAccount.serverName(), mSieveImapAccount.port(), this);
0027         mSession->setUiProxy(SessionUiProxy::Ptr(new SessionUiProxy));
0028 
0029         auto login = new KIMAP::LoginJob(mSession);
0030         login->setUserName(mSieveImapAccount.userName());
0031         login->setPassword(mSieveImapAccount.password());
0032         login->setAuthenticationMode(static_cast<KIMAP::LoginJob::AuthenticationMode>(mSieveImapAccount.authenticationType()));
0033         login->setEncryptionMode(static_cast<KIMAP::LoginJob::EncryptionMode>(mSieveImapAccount.encryptionMode()));
0034         connect(login, &KIMAP::LoginJob::result, this, &SelectImapCreateFolderJob::slotLoginDone);
0035         login->start();
0036     } else {
0037         failed();
0038     }
0039 }
0040 
0041 void SelectImapCreateFolderJob::failed()
0042 {
0043     Q_EMIT finished(mSieveImapAccount, false);
0044     deleteLater();
0045 }
0046 
0047 void SelectImapCreateFolderJob::slotLoginDone(KJob *job)
0048 {
0049     if (!job->error()) {
0050         createFolderRequested();
0051     } else {
0052         failed();
0053     }
0054 }
0055 
0056 void SelectImapCreateFolderJob::createFolderRequested()
0057 {
0058     if (!mSession || mSession->state() != KIMAP::Session::Authenticated) {
0059         qCWarning(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "SelectImapCreateFolderJob - got no connection";
0060         failed();
0061         return;
0062     }
0063 
0064     auto job = new KIMAP::CreateJob(mSession);
0065     job->setMailBox(mNewFolderName);
0066     connect(job, &KIMAP::CreateJob::result, this, &SelectImapCreateFolderJob::slotCreateFolderDone);
0067     job->start();
0068 }
0069 
0070 void SelectImapCreateFolderJob::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &account)
0071 {
0072 #if 0
0073     qDebug() << " void SelectImapFolderWidget::setSieveImapAccountSettings(const KSieveUi::SieveImapAccountSettings &account)" << account.serverName()
0074              << " port : " << account.port()
0075              << " password :" << account.password()
0076              << "authentication :" << account.authenticationType()
0077              << "encryption : " << account.encryptionMode();
0078 #endif
0079     mSieveImapAccount = account;
0080 }
0081 
0082 void SelectImapCreateFolderJob::setNewFolderName(const QString &newFolderName)
0083 {
0084     mNewFolderName = newFolderName;
0085 }
0086 
0087 void SelectImapCreateFolderJob::slotCreateFolderDone(KJob *job)
0088 {
0089     if (job->error()) {
0090         KMessageBox::error(nullptr, i18n("Error during creating folder: %1", job->errorString()), i18nc("@title:window", "Create Folder"));
0091         Q_EMIT finished(mSieveImapAccount, false);
0092     } else {
0093         Q_EMIT finished(mSieveImapAccount, true);
0094     }
0095     deleteLater();
0096 }
0097 
0098 #include "moc_selectimapcreatefolderjob.cpp"