Akonadi

relationremovehandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
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/queryhelper.h"
13 #include "storage/selectquerybuilder.h"
14 
15 using namespace Akonadi;
16 using namespace Akonadi::Server;
17 
18 RelationRemoveHandler::RelationRemoveHandler(AkonadiServer &akonadi)
19  : Handler(akonadi)
20 {
21 }
22 
23 bool RelationRemoveHandler::parseStream()
24 {
25  const auto &cmd = Protocol::cmdCast<Protocol::RemoveRelationsCommand>(m_command);
26 
27  if (cmd.left() < 0 || cmd.right() < 0) {
28  return failureResponse("Invalid relation id's provided");
29  }
30 
31  RelationType relType;
32  if (!cmd.type().isEmpty()) {
33  relType = RelationType::retrieveByName(QString::fromUtf8(cmd.type()));
34  if (!relType.isValid()) {
35  return failureResponse("Failed to load relation type");
36  }
37  }
38 
39  SelectQueryBuilder<Relation> relationQuery;
40  relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
41  relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
42  if (relType.isValid()) {
43  relationQuery.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
44  }
45 
46  if (!relationQuery.exec()) {
47  return failureResponse("Failed to obtain relations");
48  }
49  const Relation::List relations = relationQuery.result();
50  for (const Relation &relation : relations) {
51  storageBackend()->notificationCollector()->relationRemoved(relation);
52  }
53 
54  // Get all PIM items that are part of the relation
55  SelectQueryBuilder<PimItem> itemsQuery;
56  itemsQuery.setSubQueryMode(Query::Or);
57  itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.left());
58  itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.right());
59 
60  if (!itemsQuery.exec()) {
61  throw failureResponse("Removing relation failed");
62  }
63  const PimItem::List items = itemsQuery.result();
64  if (!items.isEmpty()) {
65  storageBackend()->notificationCollector()->itemsRelationsChanged(items, Relation::List(), relations);
66  }
67 
68  QueryBuilder qb(Relation::tableName(), QueryBuilder::Delete);
69  qb.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
70  qb.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
71  if (relType.isValid()) {
72  qb.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
73  }
74  if (!qb.exec()) {
75  return failureResponse("Failed to remove relations");
76  }
77 
78  return successResponse<Protocol::RemoveRelationsResponse>();
79 }
QString fromUtf8(const char *str, int size)
An Akonadi Relation.
Definition: relation.h:39
QVector< T > result()
Returns the result of this SELECT query.
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.
Helper class for creating and executing database SELECT queries.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
void setSubQueryMode(Query::LogicOperator op, ConditionType type=WhereCondition)
Define how WHERE or HAVING conditions are combined.
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:31
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.