File indexing completed on 2024-11-10 04:40:29

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "itemdeletejob.h"
0008 
0009 #include "collection.h"
0010 #include "job_p.h"
0011 #include "private/protocol_p.h"
0012 #include "protocolhelper_p.h"
0013 
0014 using namespace Akonadi;
0015 
0016 class Akonadi::ItemDeleteJobPrivate : public JobPrivate
0017 {
0018 public:
0019     explicit ItemDeleteJobPrivate(ItemDeleteJob *parent)
0020         : JobPrivate(parent)
0021     {
0022     }
0023 
0024     Q_DECLARE_PUBLIC(ItemDeleteJob)
0025     QString jobDebuggingString() const override;
0026 
0027     Item::List mItems;
0028     Collection mCollection;
0029     Tag mCurrentTag;
0030 };
0031 
0032 QString Akonadi::ItemDeleteJobPrivate::jobDebuggingString() const
0033 {
0034     QString itemStr = QStringLiteral("items id: ");
0035     bool firstItem = true;
0036     for (const Akonadi::Item &item : std::as_const(mItems)) {
0037         if (firstItem) {
0038             firstItem = false;
0039         } else {
0040             itemStr += QStringLiteral(", ");
0041         }
0042         itemStr += QString::number(item.id());
0043     }
0044 
0045     return QStringLiteral("Remove %1 from collection id %2").arg(itemStr).arg(mCollection.id());
0046 }
0047 
0048 ItemDeleteJob::ItemDeleteJob(const Item &item, QObject *parent)
0049     : Job(new ItemDeleteJobPrivate(this), parent)
0050 {
0051     Q_D(ItemDeleteJob);
0052 
0053     d->mItems << item;
0054 }
0055 
0056 ItemDeleteJob::ItemDeleteJob(const Item::List &items, QObject *parent)
0057     : Job(new ItemDeleteJobPrivate(this), parent)
0058 {
0059     Q_D(ItemDeleteJob);
0060 
0061     d->mItems = items;
0062 }
0063 
0064 ItemDeleteJob::ItemDeleteJob(const Collection &collection, QObject *parent)
0065     : Job(new ItemDeleteJobPrivate(this), parent)
0066 {
0067     Q_D(ItemDeleteJob);
0068 
0069     d->mCollection = collection;
0070 }
0071 
0072 ItemDeleteJob::ItemDeleteJob(const Tag &tag, QObject *parent)
0073     : Job(new ItemDeleteJobPrivate(this), parent)
0074 {
0075     Q_D(ItemDeleteJob);
0076 
0077     d->mCurrentTag = tag;
0078 }
0079 
0080 ItemDeleteJob::~ItemDeleteJob()
0081 {
0082 }
0083 
0084 Item::List ItemDeleteJob::deletedItems() const
0085 {
0086     Q_D(const ItemDeleteJob);
0087 
0088     return d->mItems;
0089 }
0090 
0091 void ItemDeleteJob::doStart()
0092 {
0093     Q_D(ItemDeleteJob);
0094 
0095     try {
0096         d->sendCommand(Protocol::DeleteItemsCommandPtr::create(d->mItems.isEmpty() ? Scope() : ProtocolHelper::entitySetToScope(d->mItems),
0097                                                                ProtocolHelper::commandContextToProtocol(d->mCollection, d->mCurrentTag, d->mItems)));
0098     } catch (const Akonadi::Exception &e) {
0099         setError(Job::Unknown);
0100         setErrorText(QString::fromUtf8(e.what()));
0101         emitResult();
0102         return;
0103     }
0104 }
0105 
0106 bool ItemDeleteJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
0107 {
0108     if (!response->isResponse() || response->type() != Protocol::Command::DeleteItems) {
0109         return Job::doHandleResponse(tag, response);
0110     }
0111 
0112     return true;
0113 }
0114 
0115 #include "moc_itemdeletejob.cpp"