Akonadi

itemcopyhandler.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "itemcopyhandler.h"
8
9#include "akonadi.h"
10#include "cachecleaner.h"
11#include "connection.h"
12#include "handlerhelper.h"
13#include "storage/datastore.h"
14#include "storage/itemqueryhelper.h"
15#include "storage/itemretriever.h"
16#include "storage/parthelper.h"
17#include "storage/selectquerybuilder.h"
18#include "storage/transaction.h"
19
20using namespace Akonadi;
21using namespace Akonadi::Server;
22
23ItemCopyHandler::ItemCopyHandler(AkonadiServer &akonadi)
24 : Handler(akonadi)
25{
26}
27
28bool ItemCopyHandler::copyItem(const PimItem &item, const Collection &target)
29{
30 PimItem newItem = item;
31 newItem.setId(-1);
32 newItem.setRev(0);
33 newItem.setDatetime(QDateTime::currentDateTimeUtc());
34 newItem.setAtime(QDateTime::currentDateTimeUtc());
35 newItem.setRemoteId(QString());
36 newItem.setRemoteRevision(QString());
37 newItem.setCollectionId(target.id());
38 Part::List newParts;
39 const auto parts = item.parts();
40 newParts.reserve(parts.size());
41 for (const Part &part : parts) {
42 Part newPart(part);
43 newPart.setData(PartHelper::translateData(newPart.data(), part.storage()));
44 newPart.setPimItemId(-1);
45 newPart.setStorage(Part::Internal);
46 newParts << newPart;
47 }
48
49 DataStore *store = connection()->storageBackend();
50 return store->appendPimItem(newParts, item.flags(), item.mimeType(), target, QDateTime::currentDateTimeUtc(), QString(), QString(), item.gid(), newItem);
51}
52
53void ItemCopyHandler::processItems(const QList<qint64> &ids)
54{
57 if (!qb.exec()) {
58 failureResponse(QStringLiteral("Unable to retrieve items"));
59 return;
60 }
61 const PimItem::List items = qb.result();
62 qb.query().finish();
63
64 DataStore *store = connection()->storageBackend();
65 Transaction transaction(store, QStringLiteral("COPY"));
66
67 for (const PimItem &item : items) {
68 if (!copyItem(item, mTargetCollection)) {
69 failureResponse(QStringLiteral("Unable to copy item"));
70 return;
71 }
72 }
73
74 if (!transaction.commit()) {
75 failureResponse(QStringLiteral("Cannot commit transaction."));
76 return;
77 }
78}
79
81{
82 const auto &cmd = Protocol::cmdCast<Protocol::CopyItemsCommand>(m_command);
83
84 if (!checkScopeConstraints(cmd.items(), {Scope::Uid})) {
85 return failureResponse(QStringLiteral("Only UID copy is allowed"));
86 }
87
88 if (cmd.items().isEmpty()) {
89 return failureResponse(QStringLiteral("No items specified"));
90 }
91
92 mTargetCollection = HandlerHelper::collectionFromScope(cmd.destination(), connection()->context());
93 if (!mTargetCollection.isValid()) {
94 return failureResponse(QStringLiteral("No valid target specified"));
95 }
96 if (mTargetCollection.isVirtual()) {
97 return failureResponse(QStringLiteral("Copying items into virtual collections is not allowed"));
98 }
99
100 CacheCleanerInhibitor inhibitor(akonadi());
101
102 ItemRetriever retriever(akonadi().itemRetrievalManager(), connection(), connection()->context());
103 retriever.setItemSet(cmd.items().uidSet());
104 retriever.setRetrieveFullPayload(true);
105 QObject::connect(&retriever, &ItemRetriever::itemsRetrieved, &retriever, [this](const QList<qint64> &ids) {
106 processItems(ids);
107 });
108 if (!retriever.exec()) {
109 return failureResponse(retriever.lastError());
110 }
111
112 return successResponse<Protocol::CopyItemsResponse>();
113}
Represents a collection of PIM items.
Definition collection.h:62
A RAII helper class to temporarily stop the CacheCleaner.
This class handles all the database access.
Definition datastore.h:95
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition handler.h:32
bool copyItem(const PimItem &item, const Collection &target)
Copy the given item and all its parts into the target.
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
Helper class for retrieving missing items parts from remote resources.
bool exec()
Executes the query, returns true on success.
QSqlQuery & query()
Returns the query, only valid after exec().
Helper class for creating and executing database SELECT queries.
QList< T > result()
Returns the result of this SELECT query.
Helper class for DataStore transaction handling.
Definition transaction.h:23
void itemSetToQuery(const QList< PimItem::Id > &set, QueryBuilder &qb, const Collection &collection=Collection())
Add conditions to qb for the given item set set.
QByteArray translateData(const QByteArray &data, Part::Storage storageType)
Returns the payload data.
Helper integration between Akonadi and Qt.
QDateTime currentDateTimeUtc()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void finish()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 11:57:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.