Akonadi

relationremovehandler.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "relationremovehandler.h"
8
9#include "connection.h"
10#include "storage/datastore.h"
11#include "storage/querybuilder.h"
12#include "storage/selectquerybuilder.h"
13
14using namespace Akonadi;
15using namespace Akonadi::Server;
16
17RelationRemoveHandler::RelationRemoveHandler(AkonadiServer &akonadi)
18 : Handler(akonadi)
19{
20}
21
22bool RelationRemoveHandler::parseStream()
23{
24 const auto &cmd = Protocol::cmdCast<Protocol::RemoveRelationsCommand>(m_command);
25
26 if (cmd.left() < 0 || cmd.right() < 0) {
27 return failureResponse("Invalid relation id's provided");
28 }
29
30 RelationType relType;
31 if (!cmd.type().isEmpty()) {
32 relType = RelationType::retrieveByName(QString::fromUtf8(cmd.type()));
33 if (!relType.isValid()) {
34 return failureResponse("Failed to load relation type");
35 }
36 }
37
38 SelectQueryBuilder<Relation> relationQuery;
39 relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
40 relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
41 if (relType.isValid()) {
42 relationQuery.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
43 }
44
45 if (!relationQuery.exec()) {
46 return failureResponse("Failed to obtain relations");
47 }
48 const Relation::List relations = relationQuery.result();
49 for (const Relation &relation : relations) {
50 storageBackend()->notificationCollector()->relationRemoved(relation);
51 }
52
53 // Get all PIM items that are part of the relation
55 itemsQuery.setSubQueryMode(Query::Or);
56 itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.left());
57 itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.right());
58
59 if (!itemsQuery.exec()) {
60 throw failureResponse("Removing relation failed");
61 }
62 const PimItem::List items = itemsQuery.result();
63 if (!items.isEmpty()) {
64 storageBackend()->notificationCollector()->itemsRelationsChanged(items, Relation::List(), relations);
65 }
66
67 QueryBuilder qb(Relation::tableName(), QueryBuilder::Delete);
68 qb.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
69 qb.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
70 if (relType.isValid()) {
71 qb.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
72 }
73 if (!qb.exec()) {
74 return failureResponse("Failed to remove relations");
75 }
76
77 return successResponse<Protocol::RemoveRelationsResponse>();
78}
An Akonadi Relation.
Definition relation.h:41
NotificationCollector * notificationCollector()
Returns the notification collector of this DataStore object.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition handler.h:32
void itemsRelationsChanged(const PimItem::List &items, const Relation::List &addedRelations, const Relation::List &removedRelations, const Collection &collection=Collection(), const QByteArray &resource=QByteArray())
Notify about changed items relations.
void relationRemoved(const Relation &relation)
Notify about a removed relation.
Helper class to construct arbitrary SQL queries.
void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type=WhereCondition)
Add a WHERE or HAVING condition which compares a column with a given value.
bool exec()
Executes the query, returns true on success.
void setSubQueryMode(Query::LogicOperator op, ConditionType type=WhereCondition)
Define how WHERE or HAVING conditions are combined.
Helper class for creating and executing database SELECT queries.
QList< T > result()
Returns the result of this SELECT query.
Helper integration between Akonadi and Qt.
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.