Parent: [543568] (diff)

Download this file

kio_recoll.cpp    377 lines (347 with data), 12.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Copyright (C) 2005 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string>
#include <QCoreApplication>
#include <QObject>
#include <QString>
#include <QUrlQuery>
#include <QStandardPaths>
#include "rclconfig.h"
#include "rcldb.h"
#include "rclinit.h"
#include "pathut.h"
#include "searchdata.h"
#include "rclquery.h"
#include "wasatorcl.h"
#include "kio_recoll.h"
#include "docseqdb.h"
#include "readfile.h"
#include "smallut.h"
#include "textsplit.h"
#include "guiutils.h"
using namespace KIO;
using namespace std;
RclConfig *RecollProtocol::o_rclconfig;
RecollProtocol::RecollProtocol(const QByteArray& pool, const QByteArray& app)
: SlaveBase("recoll", pool, app), m_initok(false), m_rcldb(0),
m_alwaysdir(false)
{
qDebug() << "RecollProtocol::RecollProtocol()";
if (o_rclconfig == 0) {
o_rclconfig = recollinit(0, 0, m_reason);
if (!o_rclconfig || !o_rclconfig->ok()) {
m_reason = string("Configuration problem: ") + m_reason;
return;
}
}
if (o_rclconfig->getDbDir().empty()) {
// Note: this will have to be replaced by a call to a
// configuration building dialog for initial configuration? Or
// do we assume that the QT GUO is always used for this ?
m_reason = "No db directory in configuration ??";
return;
}
rwSettings(false);
m_rcldb = new Rcl::Db(o_rclconfig);
if (!m_rcldb) {
m_reason = "Could not build database object. (out of memory ?)";
return;
}
// Decide if we allow switching between html and file manager
// presentation by using an end slash or not. Can also be done dynamically
// by switching proto names.
const char *cp = getenv("RECOLL_KIO_ALWAYS_DIR");
if (cp) {
m_alwaysdir = stringToBool(cp);
} else {
o_rclconfig->getConfParam("kio_always_dir", &m_alwaysdir);
}
cp = getenv("RECOLL_KIO_STEMLANG");
if (cp) {
m_stemlang = cp;
} else {
m_stemlang = "english";
}
m_pager.setParent(this);
m_initok = true;
return;
}
// There should be an object counter somewhere to delete the config when done.
// Doesn't seem needed in the kio context.
RecollProtocol::~RecollProtocol()
{
qDebug() << "RecollProtocol::~RecollProtocol()";
delete m_rcldb;
}
bool RecollProtocol::maybeOpenDb(string& reason)
{
if (!m_rcldb) {
reason = "Internal error: initialization error";
return false;
}
if (!m_rcldb->isopen() && !m_rcldb->open(Rcl::Db::DbRO)) {
reason = "Could not open database in " + o_rclconfig->getDbDir();
return false;
}
return true;
}
// This is never called afaik
void RecollProtocol::mimetype(const QUrl& url)
{
qDebug() << "RecollProtocol::mimetype: url: " << url;
mimeType("text/html");
finished();
}
UrlIngester::UrlIngester(RecollProtocol *p, const QUrl& url)
: m_parent(p), m_slashend(false), m_alwaysdir(false),
m_retType(UIRET_NONE), m_resnum(0), m_type(UIMT_NONE)
{
qDebug() << "UrlIngester::UrlIngester: Url: " << url;
m_alwaysdir = !url.scheme().compare("recollf");
QString path = url.path();
if (url.host().isEmpty()) {
if (path.isEmpty() || !path.compare("/")) {
m_type = UIMT_ROOTENTRY;
m_retType = UIRET_ROOT;
return;
} else if (!path.compare("/help.html")) {
m_type = UIMT_ROOTENTRY;
m_retType = UIRET_HELP;
return;
} else if (!path.compare("/search.html")) {
m_type = UIMT_ROOTENTRY;
m_retType = UIRET_SEARCH;
QUrlQuery q(url);
// Retrieve the query value for preloading the form
m_query.query = q.queryItemValue("q");
return;
} else if (m_parent->isRecollResult(url, &m_resnum, &m_query.query)) {
m_type = UIMT_QUERYRESULT;
m_query.opt = "l";
m_query.page = 0;
} else {
// Have to think this is some search string
m_type = UIMT_QUERY;
m_query.query = url.path();
m_query.opt = "l";
m_query.page = 0;
}
} else {
// Non empty host, url must be something like :
// //search/query?q=query&param=value...
qDebug() << "UrlIngester::UrlIngester: host " <<
url.host() << " path " << url.path();
if (url.host().compare("search") || url.path().compare("/query")) {
return;
}
m_type = UIMT_QUERY;
// Decode the forms' arguments
// Retrieve the query value for preloading the form
QUrlQuery q(url);
m_query.query = q.queryItemValue("q");
m_query.opt = q.queryItemValue("qtp");
if (m_query.opt.isEmpty()) {
m_query.opt = "l";
}
QString p = q.queryItemValue("p");
if (p.isEmpty()) {
m_query.page = 0;
} else {
sscanf(p.toUtf8(), "%d", &m_query.page);
}
p = q.queryItemValue("det");
m_query.isDetReq = !p.isEmpty();
p = q.queryItemValue("cmd");
if (!p.isEmpty() && !p.compare("pv")) {
p = q.queryItemValue("dn");
if (!p.isEmpty()) {
// Preview and no docnum ??
m_resnum = atoi((const char *)p.toUtf8());
// Result in page is 1+
m_resnum--;
m_type = UIMT_PREVIEW;
}
}
}
if (m_query.query.startsWith("/")) {
m_query.query.remove(0, 1);
}
if (m_query.query.endsWith("/")) {
qDebug() << "UrlIngester::UrlIngester: query Ends with /";
m_slashend = true;
m_query.query.chop(1);
} else {
m_slashend = false;
}
return;
}
bool RecollProtocol::syncSearch(const QueryDesc& qd)
{
qDebug() << "RecollProtocol::syncSearch";
if (!m_initok || !maybeOpenDb(m_reason)) {
string reason = "RecollProtocol::listDir: Init error:" + m_reason;
error(KIO::ERR_SLAVE_DEFINED, u8s2qs(reason));
return false;
}
if (qd.sameQuery(m_query)) {
return true;
}
// doSearch() calls error() if appropriate.
return doSearch(qd);
}
// This is used by the html interface, but also by the directory one
// when doing file copies for exemple. This is the central dispatcher
// for requests, it has to know a little about both models.
void RecollProtocol::get(const QUrl& url)
{
qDebug() << "RecollProtocol::get: " << url;
if (!m_initok || !maybeOpenDb(m_reason)) {
string reason = "Recoll: init error: " + m_reason;
error(KIO::ERR_SLAVE_DEFINED, u8s2qs(reason));
return;
}
UrlIngester ingest(this, url);
UrlIngester::RootEntryType rettp;
QueryDesc qd;
int resnum;
if (ingest.isRootEntry(&rettp)) {
switch (rettp) {
case UrlIngester::UIRET_HELP: {
QString location =
QStandardPaths::locate(QStandardPaths::GenericDataLocation,
"kio_recoll/help.html");
redirection(QUrl::fromLocalFile(location));
}
goto out;
default:
searchPage();
goto out;
}
} else if (ingest.isResult(&qd, &resnum)) {
// Url matched one generated by konqueror/Dolphin out of a
// search directory listing: ie:
// recoll:/some search string/recollResultxx
//
// This happens when the user drags/drop the result to another
// app, or with the "open-with" right-click. Does not happen
// if the entry itself is clicked (the UDS_URL is apparently
// used in this case
//
// Redirect to the result document URL
if (!syncSearch(qd)) {
return;
}
Rcl::Doc doc;
if (resnum >= 0 && m_source && m_source->getDoc(resnum, doc)) {
mimeType(doc.mimetype.c_str());
redirection(QUrl::fromLocalFile((const char *)(doc.url.c_str() + 7)));
goto out;
}
} else if (ingest.isPreview(&qd, &resnum)) {
if (!syncSearch(qd)) {
return;
}
Rcl::Doc doc;
if (resnum >= 0 && m_source && m_source->getDoc(resnum, doc)) {
showPreview(doc);
goto out;
}
} else if (ingest.isQuery(&qd)) {
#if 0
// Do we need this ?
if (host.isEmpty()) {
char cpage[20];
sprintf(cpage, "%d", page);
QString nurl = QString::fromAscii("recoll://search/query?q=") +
query + "&qtp=" + opt + "&p=" + cpage;
redirection(QUrl(nurl));
goto out;
}
#endif
// htmlDoSearch does the search syncing (needs to know about changes).
htmlDoSearch(qd);
goto out;
}
error(KIO::ERR_SLAVE_DEFINED, u8s2qs("Unrecognized URL or internal error"));
out:
finished();
}
// Execute Recoll search, and set the docsource
bool RecollProtocol::doSearch(const QueryDesc& qd)
{
qDebug() << "RecollProtocol::doSearch:query" << qd.query << "opt" << qd.opt;
m_query = qd;
char opt = qd.opt.isEmpty() ? 'l' : qd.opt.toUtf8().at(0);
string qs = (const char *)qd.query.toUtf8();
Rcl::SearchData *sd = 0;
if (opt != 'l') {
Rcl::SearchDataClause *clp = 0;
if (opt == 'f') {
clp = new Rcl::SearchDataClauseFilename(qs);
} else {
clp = new Rcl::SearchDataClauseSimple(opt == 'o' ? Rcl::SCLT_OR :
Rcl::SCLT_AND, qs);
}
sd = new Rcl::SearchData(Rcl::SCLT_OR, m_stemlang);
if (sd && clp) {
sd->addClause(clp);
}
} else {
sd = wasaStringToRcl(o_rclconfig, m_stemlang, qs, m_reason);
}
if (!sd) {
m_reason = "Internal Error: cant build search";
error(KIO::ERR_SLAVE_DEFINED, u8s2qs(m_reason));
return false;
}
std::shared_ptr<Rcl::SearchData> sdata(sd);
std::shared_ptr<Rcl::Query>query(new Rcl::Query(m_rcldb));
query->setCollapseDuplicates(prefs.collapseDuplicates);
if (!query->setQuery(sdata)) {
m_reason = "Query execute failed. Invalid query or syntax error?";
error(KIO::ERR_SLAVE_DEFINED, u8s2qs(m_reason));
return false;
}
DocSequenceDb *src =
new DocSequenceDb(std::shared_ptr<Rcl::Query>(query), "Query results", sdata);
if (src == 0) {
error(KIO::ERR_SLAVE_DEFINED, u8s2qs("Can't build result sequence"));
return false;
}
m_source = std::shared_ptr<DocSequence>(src);
// Reset pager in all cases. Costs nothing, stays at page -1 initially
// htmldosearch will fetch the first page if needed.
m_pager.setDocSource(m_source);
return true;
}
int kdemain(int argc, char **argv)
{
QCoreApplication::setApplicationName("kio_recoll");
qDebug() << "*** starting kio_recoll ";
if (argc != 4) {
qDebug() << "Usage: kio_recoll proto dom-socket1 dom-socket2\n";
exit(-1);
}
RecollProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
qDebug() << "kio_recoll Done";
return 0;
}