1
2/*
3 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
4 Copyright (c) 2014 Daniel Vrátil <dvratil@redhat.com>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "searchcreatejob.h"
23
24#include "collection.h"
25#include "imapparser_p.h"
26#include "protocolhelper_p.h"
27#include "job_p.h"
28#include "searchquery.h"
29#include <akonadi/private/protocol_p.h>
30
31using namespace Akonadi;
32
33class Akonadi::SearchCreateJobPrivate : public JobPrivate
34{
35public:
36 SearchCreateJobPrivate(const QString &name, const SearchQuery &query, SearchCreateJob *parent)
37 : JobPrivate(parent)
38 , mName(name)
39 , mQuery(query)
40 , mRecursive(false)
41 , mRemote(false)
42 {
43 }
44
45 QString mName;
46 SearchQuery mQuery;
47 QStringList mMimeTypes;
48 Collection::List mCollections;
49 bool mRecursive;
50 bool mRemote;
51 Collection mCreatedCollection;
52};
53
54SearchCreateJob::SearchCreateJob(const QString &name, const QString &query, QObject *parent)
55 : Job(new SearchCreateJobPrivate(name, SearchQuery::fromJSON(query.toLatin1()), this), parent)
56{
57}
58
59SearchCreateJob::SearchCreateJob(const QString &name, const SearchQuery &searchQuery, QObject *parent)
60 : Job(new SearchCreateJobPrivate(name, searchQuery, this), parent)
61{
62}
63
64SearchCreateJob::~SearchCreateJob()
65{
66}
67
68void SearchCreateJob::setQueryLanguage(const QString &queryLanguage)
69{
70 Q_UNUSED(queryLanguage);
71}
72
73void SearchCreateJob::setSearchCollections(const Collection::List &collections)
74{
75 Q_D(SearchCreateJob);
76
77 d->mCollections = collections;
78}
79
80Collection::List SearchCreateJob::searchCollections() const
81{
82 return d_func()->mCollections;
83}
84
85void SearchCreateJob::setSearchMimeTypes(const QStringList &mimeTypes)
86{
87 Q_D(SearchCreateJob);
88
89 d->mMimeTypes = mimeTypes;
90}
91
92QStringList SearchCreateJob::searchMimeTypes() const
93{
94 return d_func()->mMimeTypes;
95}
96
97void SearchCreateJob::setRecursive(bool recursive)
98{
99 Q_D(SearchCreateJob);
100
101 d->mRecursive = recursive;
102}
103
104bool SearchCreateJob::isRecursive() const
105{
106 return d_func()->mRecursive;
107}
108
109void SearchCreateJob::setRemoteSearchEnabled(bool enabled)
110{
111 Q_D(SearchCreateJob);
112
113 d->mRemote = enabled;
114}
115
116bool SearchCreateJob::isRemoteSearchEnabled() const
117{
118 return d_func()->mRemote;
119}
120
121void SearchCreateJob::doStart()
122{
123 Q_D(SearchCreateJob);
124
125 QByteArray command = d->newTag() + " SEARCH_STORE ";
126 command += ImapParser::quote(d->mName.toUtf8());
127 command += ' ';
128 command += ImapParser::quote(d->mQuery.toJSON());
129 command += " (";
130 command += QByteArray(AKONADI_PARAM_PERSISTENTSEARCH_QUERYLANG) + " \"ASQL\" "; // Akonadi Search Query Language ;-)
131 if (!d->mCollections.isEmpty()) {
132 command += QByteArray(AKONADI_PARAM_PERSISTENTSEARCH_QUERYCOLLECTIONS) + " (";
133 QList<QByteArray> ids;
134 Q_FOREACH (const Collection &col, d->mCollections) {
135 ids << QByteArray::number(col.id());
136 }
137 command += ImapParser::join(ids, " ");
138 command += ") ";
139 }
140 if (d->mRecursive) {
141 command += QByteArray(AKONADI_PARAM_RECURSIVE) + " ";
142 }
143 if (d->mRemote) {
144 command += QByteArray(AKONADI_PARAM_REMOTE) + " ";
145 }
146 command += QByteArray(AKONADI_PARAM_MIMETYPE) + " (";
147 command += d->mMimeTypes.join(QLatin1String(" ")).toLatin1();
148 command += ") )";
149 command += '\n';
150 d->writeData(command);
151}
152
153Akonadi::Collection SearchCreateJob::createdCollection() const
154{
155 Q_D(const SearchCreateJob);
156 return d->mCreatedCollection;
157}
158
159void SearchCreateJob::doHandleResponse(const QByteArray &tag, const QByteArray &data)
160{
161 Q_D(SearchCreateJob);
162 if (tag == "*") {
163 ProtocolHelper::parseCollection(data, d->mCreatedCollection);
164 return;
165 }
166 kDebug() << "Unhandled response: " << tag << data;
167}
168