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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "itemmovejob.h"
0008 
0009 #include "collection.h"
0010 #include "job_p.h"
0011 #include "private/protocol_p.h"
0012 #include "protocolhelper_p.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 using namespace Akonadi;
0017 
0018 class Akonadi::ItemMoveJobPrivate : public Akonadi::JobPrivate
0019 {
0020 public:
0021     explicit ItemMoveJobPrivate(ItemMoveJob *parent)
0022         : JobPrivate(parent)
0023     {
0024     }
0025 
0026     QString jobDebuggingString() const override
0027     {
0028         QString str = QStringLiteral("Move item");
0029         if (source.isValid()) {
0030             str += QStringLiteral("from collection %1").arg(source.id());
0031         }
0032         str += QStringLiteral(" to collection %1. ").arg(destination.id());
0033         if (items.isEmpty()) {
0034             str += QStringLiteral("No Items defined.");
0035         } else {
0036             str += QStringLiteral("Items: ");
0037             const int nbItems = items.count();
0038             for (int i = 0; i < nbItems; ++i) {
0039                 if (i != 0) {
0040                     str += QStringLiteral(", ");
0041                 }
0042                 str += QString::number(items.at(i).id());
0043             }
0044         }
0045         return str;
0046     }
0047 
0048     Item::List items;
0049     Collection destination;
0050     Collection source;
0051 
0052     Q_DECLARE_PUBLIC(ItemMoveJob)
0053 };
0054 
0055 ItemMoveJob::ItemMoveJob(const Item &item, const Collection &destination, QObject *parent)
0056     : Job(new ItemMoveJobPrivate(this), parent)
0057 {
0058     Q_D(ItemMoveJob);
0059     d->destination = destination;
0060     d->items.append(item);
0061 }
0062 
0063 ItemMoveJob::ItemMoveJob(const Item::List &items, const Collection &destination, QObject *parent)
0064     : Job(new ItemMoveJobPrivate(this), parent)
0065 {
0066     Q_D(ItemMoveJob);
0067     d->destination = destination;
0068     d->items = items;
0069 }
0070 
0071 ItemMoveJob::ItemMoveJob(const Item::List &items, const Collection &source, const Collection &destination, QObject *parent)
0072     : Job(new ItemMoveJobPrivate(this), parent)
0073 {
0074     Q_D(ItemMoveJob);
0075     d->source = source;
0076     d->destination = destination;
0077     d->items = items;
0078 }
0079 
0080 ItemMoveJob::~ItemMoveJob()
0081 {
0082 }
0083 
0084 void ItemMoveJob::doStart()
0085 {
0086     Q_D(ItemMoveJob);
0087 
0088     if (d->items.isEmpty()) {
0089         setError(Job::Unknown);
0090         setErrorText(i18n("No objects specified for moving"));
0091         emitResult();
0092         return;
0093     }
0094 
0095     if (!d->destination.isValid() && d->destination.remoteId().isEmpty()) {
0096         setError(Job::Unknown);
0097         setErrorText(i18n("No valid destination specified"));
0098         emitResult();
0099         return;
0100     }
0101 
0102     try {
0103         d->sendCommand(Protocol::MoveItemsCommandPtr::create(ProtocolHelper::entitySetToScope(d->items),
0104                                                              ProtocolHelper::commandContextToProtocol(d->source, Tag(), d->items),
0105                                                              ProtocolHelper::entityToScope(d->destination)));
0106     } catch (const Akonadi::Exception &e) {
0107         setError(Job::Unknown);
0108         setErrorText(QString::fromUtf8(e.what()));
0109         emitResult();
0110         return;
0111     }
0112 }
0113 
0114 bool ItemMoveJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
0115 {
0116     if (!response->isResponse() || response->type() != Protocol::Command::MoveItems) {
0117         return Job::doHandleResponse(tag, response);
0118     }
0119 
0120     return true;
0121 }
0122 
0123 Collection ItemMoveJob::destinationCollection() const
0124 {
0125     Q_D(const ItemMoveJob);
0126     return d->destination;
0127 }
0128 
0129 Item::List ItemMoveJob::items() const
0130 {
0131     Q_D(const ItemMoveJob);
0132     return d->items;
0133 }
0134 
0135 #include "moc_itemmovejob.cpp"