Akonadi

relationfetchhandler.cpp
1 /***************************************************************************
2  * Copyright (C) 2014 by Christian Mollekopf <[email protected]> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License as *
6  * published by the Free Software Foundation; either version 2 of the *
7  * License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public *
15  * License along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #include "relationfetchhandler.h"
21 #include "handlerhelper.h"
22 #include "connection.h"
23 #include "storage/selectquerybuilder.h"
24 
25 #include <private/imapset_p.h>
26 
27 using namespace Akonadi;
28 using namespace Akonadi::Server;
29 
30 RelationFetchHandler::RelationFetchHandler(AkonadiServer &akonadi)
31  : Handler(akonadi)
32 {}
33 
34 bool RelationFetchHandler::parseStream()
35 {
36  const auto &cmd = Protocol::cmdCast<Protocol::FetchRelationsCommand>(m_command);
37 
38  SelectQueryBuilder<Relation> relationQuery;
39  if (cmd.side() > 0) {
41  c.setSubQueryMode(Query::Or);
42  c.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.side());
43  c.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.side());
44  relationQuery.addCondition(c);
45  } else {
46  if (cmd.left() > 0) {
47  relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
48  }
49  if (cmd.right() > 0) {
50  relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
51  }
52  }
53  if (!cmd.types().isEmpty()) {
54  relationQuery.addJoin(QueryBuilder::InnerJoin, RelationType::tableName(), Relation::typeIdFullColumnName(), RelationType::idFullColumnName());
55  QStringList types;
56  types.reserve(cmd.types().size());
57  Q_FOREACH (const QByteArray &type, cmd.types()) {
58  types << QString::fromUtf8(type);
59  }
60  relationQuery.addValueCondition(RelationType::nameFullColumnName(), Query::In, types);
61  }
62  if (!cmd.resource().isEmpty()) {
63  Resource res = Resource::retrieveByName(cmd.resource());
64  if (!res.isValid()) {
65  return failureResponse("Invalid resource");
66  }
67  Query::Condition condition;
68  condition.setSubQueryMode(Query::Or);
69  condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::leftIdFullColumnName());
70  condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::rightIdFullColumnName());
71  relationQuery.addJoin(QueryBuilder::InnerJoin, PimItem::tableName(), condition);
72 
73  relationQuery.addJoin(QueryBuilder::InnerJoin, Collection::tableName(), PimItem::collectionIdFullColumnName(), Collection::idFullColumnName());
74 
75  relationQuery.addValueCondition(Collection::resourceIdFullColumnName(), Query::Equals, res.id());
76  relationQuery.addGroupColumns(QStringList() << Relation::leftIdFullColumnName() << Relation::rightIdFullColumnName() << Relation::typeIdFullColumnName());
77  }
78 
79  if (!relationQuery.exec()) {
80  return failureResponse("Failed to query for existing relation");
81  }
82  const Relation::List existingRelations = relationQuery.result();
83  for (const Relation &relation : existingRelations) {
84  sendResponse(HandlerHelper::fetchRelationsResponse(relation));
85  }
86 
87  return successResponse<Protocol::FetchRelationsResponse>();
88 }
void reserve(int alloc)
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
QString fromUtf8(const char *str, int size)
Relation relation() const
Returns the relation.
void addColumnCondition(const QString &column, CompareOperator op, const QString &column2)
Add a WHERE condition which compares a column with another column.
Definition: query.cpp:35
Helper class for creating and executing database SELECT queries.
void setSubQueryMode(LogicOperator op)
Set how sub-conditions should be combined, default is And.
Definition: query.cpp:62
Helper integration between Akonadi and Qt.
Representation of a record in the Relation table.
Definition: entities.h:3492
Represents a WHERE condition tree.
Definition: query.h:77
Representation of a record in the Resource table.
Definition: entities.h:246
void addValueCondition(const QString &column, CompareOperator op, const QVariant &value)
Add a WHERE condition which compares a column with a given value.
Definition: query.cpp:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:46:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.