File indexing completed on 2025-02-16 04:50:19

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "resourcestate.h"
0009 
0010 #include "imapaccount.h"
0011 #include "imapresource.h"
0012 #include "sessionpool.h"
0013 #include "settings.h"
0014 
0015 #include "imapresource_debug.h"
0016 #include <Akonadi/AgentSearchInterface>
0017 #include <Akonadi/CollectionModifyJob>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 
0021 ResourceState::ResourceState(ImapResourceBase *resource, const TaskArguments &args)
0022     : m_resource(resource)
0023     , m_arguments(args)
0024 {
0025 }
0026 
0027 ResourceState::~ResourceState() = default;
0028 
0029 QString ResourceState::userName() const
0030 {
0031     return m_resource->m_pool->account()->userName();
0032 }
0033 
0034 QString ResourceState::resourceName() const
0035 {
0036     return m_resource->name();
0037 }
0038 
0039 QString ResourceState::resourceIdentifier() const
0040 {
0041     return m_resource->identifier();
0042 }
0043 
0044 QStringList ResourceState::serverCapabilities() const
0045 {
0046     return m_resource->m_pool->serverCapabilities();
0047 }
0048 
0049 QList<KIMAP::MailBoxDescriptor> ResourceState::serverNamespaces() const
0050 {
0051     return m_resource->m_pool->serverNamespaces();
0052 }
0053 
0054 QList<KIMAP::MailBoxDescriptor> ResourceState::personalNamespaces() const
0055 {
0056     return m_resource->m_pool->serverNamespaces(SessionPool::Personal);
0057 }
0058 
0059 QList<KIMAP::MailBoxDescriptor> ResourceState::userNamespaces() const
0060 {
0061     return m_resource->m_pool->serverNamespaces(SessionPool::User);
0062 }
0063 
0064 QList<KIMAP::MailBoxDescriptor> ResourceState::sharedNamespaces() const
0065 {
0066     return m_resource->m_pool->serverNamespaces(SessionPool::Shared);
0067 }
0068 
0069 bool ResourceState::isAutomaticExpungeEnabled() const
0070 {
0071     return m_resource->settings()->automaticExpungeEnabled();
0072 }
0073 
0074 bool ResourceState::isSubscriptionEnabled() const
0075 {
0076     return m_resource->settings()->subscriptionEnabled();
0077 }
0078 
0079 bool ResourceState::isDisconnectedModeEnabled() const
0080 {
0081     return m_resource->settings()->disconnectedModeEnabled();
0082 }
0083 
0084 int ResourceState::intervalCheckTime() const
0085 {
0086     if (m_resource->settings()->intervalCheckEnabled()) {
0087         return m_resource->settings()->intervalCheckTime();
0088     } else {
0089         return -1; // -1 for never
0090     }
0091 }
0092 
0093 Akonadi::Collection ResourceState::collection() const
0094 {
0095     return m_arguments.collection;
0096 }
0097 
0098 Akonadi::Item ResourceState::item() const
0099 {
0100     if (m_arguments.items.count() > 1) {
0101         qCWarning(IMAPRESOURCE_LOG) << "Called item() while state holds multiple items!";
0102     }
0103 
0104     return m_arguments.items.first();
0105 }
0106 
0107 Akonadi::Item::List ResourceState::items() const
0108 {
0109     return m_arguments.items;
0110 }
0111 
0112 Akonadi::Collection ResourceState::parentCollection() const
0113 {
0114     return m_arguments.parentCollection;
0115 }
0116 
0117 Akonadi::Collection ResourceState::sourceCollection() const
0118 {
0119     return m_arguments.sourceCollection;
0120 }
0121 
0122 Akonadi::Collection ResourceState::targetCollection() const
0123 {
0124     return m_arguments.targetCollection;
0125 }
0126 
0127 QSet<QByteArray> ResourceState::parts() const
0128 {
0129     return m_arguments.parts;
0130 }
0131 
0132 QSet<QByteArray> ResourceState::addedFlags() const
0133 {
0134     return m_arguments.addedFlags;
0135 }
0136 
0137 QSet<QByteArray> ResourceState::removedFlags() const
0138 {
0139     return m_arguments.removedFlags;
0140 }
0141 
0142 Akonadi::Tag ResourceState::tag() const
0143 {
0144     return m_arguments.tag;
0145 }
0146 
0147 QSet<Akonadi::Tag> ResourceState::addedTags() const
0148 {
0149     return m_arguments.addedTags;
0150 }
0151 
0152 QSet<Akonadi::Tag> ResourceState::removedTags() const
0153 {
0154     return m_arguments.removedTags;
0155 }
0156 
0157 Akonadi::Relation::List ResourceState::addedRelations() const
0158 {
0159     return m_arguments.addedRelations;
0160 }
0161 
0162 Akonadi::Relation::List ResourceState::removedRelations() const
0163 {
0164     return m_arguments.removedRelations;
0165 }
0166 
0167 QString ResourceState::rootRemoteId() const
0168 {
0169     return m_resource->settings()->rootRemoteId();
0170 }
0171 
0172 void ResourceState::setIdleCollection(const Akonadi::Collection &collection)
0173 {
0174     QStringList ridPath;
0175 
0176     Akonadi::Collection curCol = collection;
0177     while (curCol != Akonadi::Collection::root() && !curCol.remoteId().isEmpty()) {
0178         ridPath.append(curCol.remoteId());
0179         curCol = curCol.parentCollection();
0180     }
0181 
0182     m_resource->settings()->setIdleRidPath(ridPath);
0183     m_resource->settings()->save();
0184 }
0185 
0186 void ResourceState::applyCollectionChanges(const Akonadi::Collection &collection)
0187 {
0188     m_resource->modifyCollection(collection);
0189 }
0190 
0191 void ResourceState::collectionAttributesRetrieved(const Akonadi::Collection &collection)
0192 {
0193     m_resource->collectionAttributesRetrieved(collection);
0194 }
0195 
0196 void ResourceState::itemRetrieved(const Akonadi::Item &item)
0197 {
0198     m_resource->itemRetrieved(item);
0199 }
0200 
0201 void ResourceState::itemsRetrieved(const Akonadi::Item::List &items)
0202 {
0203     m_resource->itemsRetrieved(items);
0204 }
0205 
0206 void ResourceState::itemsRetrievedIncremental(const Akonadi::Item::List &changed, const Akonadi::Item::List &removed)
0207 {
0208     m_resource->itemsRetrievedIncremental(changed, removed);
0209 }
0210 
0211 void ResourceState::itemsRetrievalDone()
0212 {
0213     m_resource->itemsRetrievalDone();
0214     emitPercent(100);
0215 }
0216 
0217 void ResourceState::setTotalItems(int items)
0218 {
0219     m_resource->setTotalItems(items);
0220 }
0221 
0222 void ResourceState::itemChangeCommitted(const Akonadi::Item &item)
0223 {
0224     m_resource->changeCommitted(item);
0225 }
0226 
0227 void ResourceState::itemsChangesCommitted(const Akonadi::Item::List &items)
0228 {
0229     m_resource->changesCommitted(items);
0230 }
0231 
0232 void ResourceState::collectionsRetrieved(const Akonadi::Collection::List &collections)
0233 {
0234     m_resource->collectionsRetrieved(collections);
0235     m_resource->startIdleIfNeeded();
0236 }
0237 
0238 void ResourceState::collectionChangeCommitted(const Akonadi::Collection &collection)
0239 {
0240     m_resource->changeCommitted(collection);
0241 }
0242 
0243 void ResourceState::tagChangeCommitted(const Akonadi::Tag &tag)
0244 {
0245     m_resource->changeCommitted(tag);
0246 }
0247 
0248 void ResourceState::changeProcessed()
0249 {
0250     m_resource->changeProcessed();
0251 }
0252 
0253 void ResourceState::searchFinished(const QList<qint64> &result, bool isRid)
0254 {
0255     m_resource->searchFinished(result, isRid ? Akonadi::AgentSearchInterface::Rid : Akonadi::AgentSearchInterface::Uid);
0256 }
0257 
0258 void ResourceState::cancelTask(const QString &errorString)
0259 {
0260     m_resource->cancelTask(errorString);
0261 }
0262 
0263 void ResourceState::deferTask()
0264 {
0265     m_resource->deferTask();
0266 }
0267 
0268 void ResourceState::restartItemRetrieval(Akonadi::Collection::Id col)
0269 {
0270     // This ensures the collection fetch job is rerun (it isn't when using deferTask)
0271     // The task will be appended
0272     // TODO: deferTask should rerun the collectionfetchjob
0273     m_resource->synchronizeCollection(col);
0274     cancelTask(i18n("Restarting item retrieval."));
0275 }
0276 
0277 void ResourceState::taskDone()
0278 {
0279     m_resource->taskDone();
0280 }
0281 
0282 void ResourceState::emitError(const QString &message)
0283 {
0284     Q_EMIT m_resource->error(message);
0285 }
0286 
0287 void ResourceState::emitWarning(const QString &message)
0288 {
0289     Q_EMIT m_resource->warning(message);
0290 }
0291 
0292 void ResourceState::emitPercent(int percent)
0293 {
0294     Q_EMIT m_resource->percent(percent);
0295 }
0296 
0297 void ResourceState::synchronizeCollection(Akonadi::Collection::Id id)
0298 {
0299     m_resource->synchronizeCollection(id);
0300 }
0301 
0302 void ResourceState::synchronizeCollectionTree()
0303 {
0304     m_resource->synchronizeCollectionTree();
0305 }
0306 
0307 void ResourceState::scheduleConnectionAttempt()
0308 {
0309     m_resource->scheduleConnectionAttempt();
0310 }
0311 
0312 QChar ResourceState::separatorCharacter() const
0313 {
0314     return m_resource->separatorCharacter();
0315 }
0316 
0317 void ResourceState::setSeparatorCharacter(QChar separator)
0318 {
0319     m_resource->setSeparatorCharacter(separator);
0320 }
0321 
0322 void ResourceState::showInformationDialog(const QString &message, const QString &title, const QString &dontShowAgainName)
0323 {
0324     KMessageBox::information(nullptr, message, title, dontShowAgainName);
0325 }
0326 
0327 int ResourceState::batchSize() const
0328 {
0329     return m_resource->itemSyncBatchSize();
0330 }
0331 
0332 MessageHelper::Ptr ResourceState::messageHelper() const
0333 {
0334     return MessageHelper::Ptr(new MessageHelper());
0335 }
0336 
0337 void ResourceState::tagsRetrieved(const Akonadi::Tag::List &tags, const QHash<QString, Akonadi::Item::List> &tagMembers)
0338 {
0339     m_resource->tagsRetrieved(tags, tagMembers);
0340 }
0341 
0342 void ResourceState::relationsRetrieved(const Akonadi::Relation::List &relations)
0343 {
0344     m_resource->relationsRetrieved(relations);
0345 }
0346 
0347 void ResourceState::setItemMergingMode(Akonadi::ItemSync::MergeMode mode)
0348 {
0349     m_resource->setItemMergingMode(mode);
0350 }