Parent: [c641cb] (diff)

Child: [e1a20a] (diff)

Download this file

kio_recoll.cpp    394 lines (361 with data), 11.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#ifndef lint
static char rcsid[] = "@(#$Id: kio_recoll.cpp,v 1.26 2008-12-17 08:01:40 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
/*
* 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>
using namespace std;
#include <qglobal.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qstring.h>
#include <kdebug.h>
#include <kcomponentdata.h>
#include <kstandarddirs.h>
#include "rclconfig.h"
#include "rcldb.h"
#include "rclinit.h"
#include "pathut.h"
#include "searchdata.h"
#include "rclquery.h"
#include "wasastringtoquery.h"
#include "wasatorcl.h"
#include "kio_recoll.h"
#include "docseqdb.h"
#include "readfile.h"
#include "smallut.h"
#include "textsplit.h"
using namespace KIO;
RclConfig *RecollProtocol::o_rclconfig;
RclConfig *RclConfig::getMainConfig()
{
return RecollProtocol::o_rclconfig;
}
RecollProtocol::RecollProtocol(const QByteArray &pool, const QByteArray &app)
: SlaveBase("recoll", pool, app), m_initok(false), m_rcldb(0),
m_alwaysdir(false)
{
kDebug() << endl;
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;
}
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);
}
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()
{
kDebug();
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;
}
// Not sure this is ever used
void RecollProtocol::mimetype(const KUrl &url)
{
kDebug() << url << endl;
///////////////////////////////REMOVEME REMOVEME REMOVEME when sure !/////
abort();
////////////////////////////////////////////////////////////////////////
mimeType("text/html");
finished();
}
UrlIngester::UrlIngester(RecollProtocol *p, const KUrl& url)
: m_parent(p), m_slashend(false), m_alwaysdir(false),
m_retType(UIRET_NONE), m_resnum(0), m_type(UIMT_NONE)
{
kDebug() << "Url" << url;
m_alwaysdir = !url.protocol().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;
// Retrieve the query value for preloading the form
m_query.query = url.queryItem("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...
kDebug() << "host" << url.host() << "path" << url.path();
if (url.host().compare("search") || url.path().compare("/query")) {
return;
}
m_type = UIMT_QUERY;
// Decode the forms' arguments
m_query.query = url.queryItem("q");
m_query.opt = url.queryItem("qtp");
if (m_query.opt.isEmpty()) {
m_query.opt = "l";
}
QString p = url.queryItem("p");
if (p.isEmpty()) {
m_query.page = 0;
} else {
sscanf(p.toAscii(), "%d", &m_query.page);
}
p = url.queryItem("det");
m_query.isDetReq = !p.isEmpty();
p = url.queryItem("cmd");
if (!p.isEmpty() && !p.compare("pv")) {
p = url.queryItem("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("/")) {
kDebug() << "Ends with /";
m_slashend = true;
m_query.query.chop(1);
} else {
m_slashend = false;
}
return;
}
bool RecollProtocol::syncSearch(const QueryDesc &qd)
{
kDebug();
if (!m_initok || !maybeOpenDb(m_reason)) {
string reason = "RecollProtocol::listDir: Init error:" + m_reason;
error(KIO::ERR_SLAVE_DEFINED, reason.c_str());
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 KUrl& url)
{
kDebug() << url << endl;
if (!m_initok || !maybeOpenDb(m_reason)) {
string reason = "Recoll: init error: " + m_reason;
error(KIO::ERR_SLAVE_DEFINED, reason.c_str());
return;
}
UrlIngester ingest(this, url);
UrlIngester::RootEntryType rettp;
QueryDesc qd;
int resnum;
if (ingest.isRootEntry(&rettp)) {
switch(rettp) {
case UrlIngester::UIRET_HELP:
{
QString location =
KStandardDirs::locate("data", "kio_recoll/help.html");
redirection(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.isNull() && m_source->getDoc(resnum, doc)) {
mimeType(doc.mimetype.c_str());
redirection(KUrl::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.isNull() && 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(KUrl(nurl));
goto out;
}
#endif
// htmlDoSearch does the search syncing (needs to know about changes).
htmlDoSearch(qd);
goto out;
}
error(KIO::ERR_SLAVE_DEFINED, "Unrecognized URL or internal error");
out:
finished();
}
// Execute Recoll search, and set the docsource
bool RecollProtocol::doSearch(const QueryDesc& qd)
{
kDebug() << "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 {
// If there is no white space inside the query, then the user
// certainly means it as a phrase.
bool isreallyaphrase = false;
if (!TextSplit::hasVisibleWhite(qs))
isreallyaphrase = true;
clp = isreallyaphrase ?
new Rcl::SearchDataClauseDist(Rcl::SCLT_PHRASE, qs, 0) :
new Rcl::SearchDataClauseSimple(opt == 'o' ?
Rcl::SCLT_OR : Rcl::SCLT_AND,
qs);
}
sd = new Rcl::SearchData(Rcl::SCLT_OR);
if (sd && clp)
sd->addClause(clp);
} else {
sd = wasaStringToRcl(qs, m_reason);
}
if (!sd) {
m_reason = "Internal Error: cant build search";
error(KIO::ERR_SLAVE_DEFINED, m_reason.c_str());
return false;
}
RefCntr<Rcl::SearchData> sdata(sd);
sdata->setStemlang("english");
RefCntr<Rcl::Query>query(new Rcl::Query(m_rcldb));
if (!query->setQuery(sdata)) {
m_reason = "Query execute failed. Invalid query or syntax error?";
error(KIO::ERR_SLAVE_DEFINED, m_reason.c_str());
return false;
}
DocSequenceDb *src =
new DocSequenceDb(RefCntr<Rcl::Query>(query), "Query results", sdata);
if (src == 0) {
error(KIO::ERR_SLAVE_DEFINED, "Can't build result sequence");
return false;
}
m_source = RefCntr<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;
}
// Note: KDE_EXPORT is actually needed on Unix when building with
// cmake. Says something like __attribute__(visibility(defautl))
// (cmake apparently sets all symbols to not exported)
extern "C" {KDE_EXPORT int kdemain(int argc, char **argv);}
int kdemain(int argc, char **argv)
{
#ifdef KDE_VERSION_3
KInstance instance("kio_recoll");
#else
KComponentData instance("kio_recoll");
#endif
kDebug() << "*** starting kio_recoll " << endl;
if (argc != 4) {
kDebug() << "Usage: kio_recoll proto dom-socket1 dom-socket2\n" << endl;
exit(-1);
}
RecollProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
kDebug() << "kio_recoll Done" << endl;
return 0;
}