Akonadi

relationremovehandler.cpp
1 /*
2  Copyright (c) 2014 Christian Mollekopf <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "relationremovehandler.h"
21 
22 #include "connection.h"
23 #include "storage/querybuilder.h"
24 #include "storage/selectquerybuilder.h"
25 #include "storage/queryhelper.h"
26 #include "storage/datastore.h"
27 
28 using namespace Akonadi;
29 using namespace Akonadi::Server;
30 
31 RelationRemoveHandler::RelationRemoveHandler(AkonadiServer &akonadi)
32  : Handler(akonadi)
33 {}
34 
35 bool RelationRemoveHandler::parseStream()
36 {
37  const auto &cmd = Protocol::cmdCast<Protocol::RemoveRelationsCommand>(m_command);
38 
39  if (cmd.left() < 0 || cmd.right() < 0) {
40  return failureResponse("Invalid relation id's provided");
41  }
42 
43  RelationType relType;
44  if (!cmd.type().isEmpty()) {
45  relType = RelationType::retrieveByName(QString::fromUtf8(cmd.type()));
46  if (!relType.isValid()) {
47  return failureResponse("Failed to load relation type");
48  }
49  }
50 
51  SelectQueryBuilder<Relation> relationQuery;
52  relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
53  relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
54  if (relType.isValid()) {
55  relationQuery.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
56  }
57 
58  if (!relationQuery.exec()) {
59  return failureResponse("Failed to obtain relations");
60  }
61  const Relation::List relations = relationQuery.result();
62  for (const Relation &relation : relations) {
63  storageBackend()->notificationCollector()->relationRemoved(relation);
64  }
65 
66  // Get all PIM items that are part of the relation
67  SelectQueryBuilder<PimItem> itemsQuery;
68  itemsQuery.setSubQueryMode(Query::Or);
69  itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.left());
70  itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.right());
71 
72  if (!itemsQuery.exec()) {
73  throw failureResponse("Removing relation failed");
74  }
75  const PimItem::List items = itemsQuery.result();
76  if (!items.isEmpty()) {
77  storageBackend()->notificationCollector()->itemsRelationsChanged(items, Relation::List(), relations);
78  }
79 
80  QueryBuilder qb(Relation::tableName(), QueryBuilder::Delete);
81  qb.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
82  qb.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
83  if (relType.isValid()) {
84  qb.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
85  }
86  if (!qb.exec()) {
87  return failureResponse("Failed to remove relations");
88  }
89 
90  return successResponse<Protocol::RemoveRelationsResponse>();
91 }
92 
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.
Relation::List relations() const
Returns the relations.
An Akonadi Relation.
Definition: relation.h:52
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
void setSubQueryMode(Query::LogicOperator op, ConditionType type=WhereCondition)
Define how WHERE or HAVING conditions are combined.
QString fromUtf8(const char *str, int size)
QVector< T > result()
Returns the result of this SELECT query.
Helper class for creating and executing database SELECT queries.
Helper integration between Akonadi and Qt.
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:45
bool exec()
Executes the query, returns true on success.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.