File indexing completed on 2025-01-05 04:49:51

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectimapfoldermodel.h"
0008 #include "imapfoldercompletionplugin_debug.h"
0009 #include "selectimapcreatefolderjob.h"
0010 #include "selectimaploadfoldersjob.h"
0011 #include <QStandardItemModel>
0012 
0013 SelectImapFolderModel::SelectImapFolderModel(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 SelectImapFolderModel::~SelectImapFolderModel() = default;
0019 
0020 SelectImapFolderModel *SelectImapFolderModel::self()
0021 {
0022     static SelectImapFolderModel s_self;
0023     return &s_self;
0024 }
0025 
0026 void SelectImapFolderModel::createNewFolder(const KSieveCore::SieveImapAccountSettings &account, const QString &folderName)
0027 {
0028     // qCDebug(IMAPFOLDERCOMPLETIONPLUGIN_LOG)  << " folderName " << folderName;
0029     auto job = new SelectImapCreateFolderJob(this);
0030     job->setSieveImapAccountSettings(account);
0031     job->setNewFolderName(folderName);
0032     connect(job, &SelectImapCreateFolderJob::finished, this, &SelectImapFolderModel::slotCreateFolderDone);
0033     job->start();
0034 }
0035 
0036 void SelectImapFolderModel::slotCreateFolderDone(const KSieveCore::SieveImapAccountSettings &account, bool success)
0037 {
0038     if (success) {
0039         reloadFolderModel(account);
0040     } else {
0041         qCDebug(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "Unable to create folder";
0042     }
0043 }
0044 
0045 void SelectImapFolderModel::fillModel(const KSieveCore::SieveImapAccountSettings &account, QStandardItemModel *model)
0046 {
0047     auto job = new SelectImapLoadFoldersJob(model, this);
0048     job->setSieveImapAccountSettings(account);
0049     connect(job, &SelectImapLoadFoldersJob::finished, this, &SelectImapFolderModel::slotLoaded);
0050     job->start();
0051 }
0052 
0053 void SelectImapFolderModel::slotLoaded(bool success, QStandardItemModel *model)
0054 {
0055     Q_EMIT modelLoaded(model, success);
0056     if (!success) {
0057         qCDebug(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "Unable to load list of folder";
0058     }
0059 }
0060 
0061 void SelectImapFolderModel::reloadFolderModel(const KSieveCore::SieveImapAccountSettings &account)
0062 {
0063     if (account.isValid()) {
0064         const QString identifier = account.identifier();
0065         QStandardItemModel *model = mHashFolderModel.value(identifier);
0066         if (model) {
0067             fillModel(account, model);
0068             mHashFolderModel.insert(identifier, model);
0069         } else {
0070             qCWarning(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "Not model defined for account : " << identifier;
0071         }
0072     } else {
0073         qCWarning(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "account is invalid";
0074     }
0075 }
0076 
0077 QStandardItemModel *SelectImapFolderModel::folderModel(const KSieveCore::SieveImapAccountSettings &account, bool &modelIsInitialized)
0078 {
0079     QStandardItemModel *model = nullptr;
0080     if (account.isValid()) {
0081         const QString identifier = account.identifier();
0082         model = mHashFolderModel.value(identifier);
0083         if (!model) {
0084             model = new QStandardItemModel(this);
0085             fillModel(account, model);
0086             mHashFolderModel.insert(identifier, model);
0087             modelIsInitialized = false;
0088         } else {
0089             modelIsInitialized = true;
0090         }
0091     } else {
0092         modelIsInitialized = false;
0093         qCWarning(IMAPFOLDERCOMPLETIONPLUGIN_LOG) << "account is invalid";
0094     }
0095     return model;
0096 }
0097 
0098 #include "moc_selectimapfoldermodel.cpp"