Parent: [1f943a] (diff)

Child: [ff78e9] (diff)

Download this file

reslist.cpp    640 lines (571 with data), 16.8 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#ifndef lint
static char rcsid[] = "@(#$Id: reslist.cpp,v 1.3 2006-09-29 08:24:16 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
#include <time.h>
#include <qvariant.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qtimer.h>
#include <qmessagebox.h>
#include <qimage.h>
#include <qclipboard.h>
#include "debuglog.h"
#include "recoll.h"
#include "guiutils.h"
#include "pathut.h"
#include "docseq.h"
#include "transcode.h"
#include "pathut.h"
#include "mimehandler.h"
#include "plaintorich.h"
#include "reslist.h"
#include "moc_reslist.cpp"
#ifndef MIN
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#endif
ResList::ResList(QWidget* parent, const char* name)
: QTextBrowser(parent, name)
{
if (!name)
setName("resList");
setTextFormat(Qt::RichText);
setReadOnly(TRUE);
setUndoRedoEnabled(FALSE);
languageChange();
clearWState(WState_Polished);
setTabChangesFocus(true);
// signals and slots connections
connect(this, SIGNAL(clicked(int, int)), this, SLOT(clicked(int,int)));
connect(this, SIGNAL(linkClicked(const QString &)),
this, SLOT(linkWasClicked(const QString &)));
connect(this, SIGNAL(headerClicked()), this, SLOT(showQueryDetails()));
connect(this, SIGNAL(doubleClicked(int,int)),
this, SLOT(doubleClicked(int, int)));
m_winfirst = -1;
m_docsource = 0;
m_curPvDoc = -1;
m_lstClckMod = 0;
}
ResList::~ResList()
{
if (m_docsource)
delete m_docsource;
}
void ResList::languageChange()
{
setCaption(tr("Result list"));
}
// Acquire new docsource
void ResList::setDocSource(DocSequence *docsource, Rcl::AdvSearchData& sdt)
{
if (m_docsource)
delete m_docsource;
m_winfirst = -1;
m_docsource = docsource;
m_queryData = sdt;
m_curPvDoc = -1;
resultPageNext();
}
// Get document number from paragraph number
int ResList::docnumfromparnum(int par)
{
if (m_winfirst == -1)
return -1;
std::map<int,int>::iterator it = m_pageParaToReldocnums.find(par);
int dn;
if (it != m_pageParaToReldocnums.end()) {
dn = m_winfirst + it->second;
} else {
dn = -1;
}
return dn;
}
// Get paragraph number from document number
int ResList::parnumfromdocnum(int docnum)
{
if (m_winfirst == -1 || docnum - m_winfirst < 0)
return -1;
docnum -= m_winfirst;
for (std::map<int,int>::iterator it = m_pageParaToReldocnums.begin();
it != m_pageParaToReldocnums.end(); it++) {
if (docnum == it->second)
return it->first;
}
return -1;
}
// Return doc from current or adjacent result pages
bool ResList::getDoc(int docnum, Rcl::Doc &doc)
{
if (docnum < 0)
return false;
// Is docnum in current page ? Then all Ok
if (docnum >= int(m_winfirst) &&
docnum < int(m_winfirst + m_curDocs.size())) {
doc = m_curDocs[docnum - m_winfirst];
goto found;
}
// Else we accept to page down or up but not further
if (docnum < int(m_winfirst) &&
docnum >= int(m_winfirst) - prefs.respagesize) {
resultPageBack();
} else if (docnum <
int(m_winfirst + m_curDocs.size()) + prefs.respagesize) {
resultPageNext();
}
if (docnum >= int(m_winfirst) &&
docnum < int(m_winfirst + m_curDocs.size())) {
doc = m_curDocs[docnum - m_winfirst];
goto found;
}
return false;
found:
return true;
}
void ResList::keyPressEvent(QKeyEvent * e)
{
if (e->key() == Key_Q && (e->state() & ControlButton)) {
recollNeedsExit = 1;
return;
} else if (e->key() == Key_Prior) {
resPageUpOrBack();
return;
} else if (e->key() == Key_Next) {
resPageDownOrNext();
return;
}
QTextBrowser::keyPressEvent(e);
}
void ResList::contentsMouseReleaseEvent(QMouseEvent *e)
{
m_lstClckMod = 0;
if (e->state() & ControlButton) {
m_lstClckMod |= ControlButton;
}
if (e->state() & ShiftButton) {
m_lstClckMod |= ShiftButton;
}
QTextBrowser::contentsMouseReleaseEvent(e);
}
// Return total result list count
int ResList::getResCnt()
{
if (!m_docsource)
return -1;
return m_docsource->getResCnt();
}
// Page Up/Down: we don't try to check if current paragraph is last or
// first. We just page up/down and check if viewport moved. If it did,
// fair enough, else we go to next/previous result page.
void ResList::resPageUpOrBack()
{
int vpos = contentsY();
moveCursor(QTextEdit::MovePgUp, false);
if (vpos == contentsY())
resultPageBack();
}
void ResList::resPageDownOrNext()
{
int vpos = contentsY();
moveCursor(QTextEdit::MovePgDown, false);
LOGDEB(("ResList::resPageDownOrNext: vpos before %d, after %d\n",
vpos, contentsY()));
if (vpos == contentsY())
resultPageNext();
}
// Show previous page of results. We just set the current number back
// 2 pages and show next page.
void ResList::resultPageBack()
{
if (m_winfirst <= 0)
return;
m_winfirst -= 2 * prefs.respagesize;
resultPageNext();
}
// Convert byte count into unit (KB/MB...) appropriate for display
static string displayableBytes(long size)
{
char sizebuf[30];
const char * unit = " B ";
if (size > 1024 && size < 1024*1024) {
unit = " KB ";
size /= 1024;
} else if (size >= 1024*1204) {
unit = " MB ";
size /= (1024*1024);
}
sprintf(sizebuf, "%ld%s", size, unit);
return string(sizebuf);
}
// Fill up result list window with next screen of hits
void ResList::resultPageNext()
{
if (!m_docsource)
return;
int percent;
Rcl::Doc doc;
int resCnt = m_docsource->getResCnt();
m_pageParaToReldocnums.clear();
LOGDEB(("resultPageNext: rescnt %d, winfirst %d\n", resCnt,
m_winfirst));
// If we are already on the last page, nothing to do:
if (m_winfirst >= 0 &&
(m_winfirst + prefs.respagesize > resCnt)) {
emit nextPageAvailable(false);
return;
}
bool hasPrev = false;
if (m_winfirst < 0) {
m_winfirst = 0;
} else {
hasPrev = true;
m_winfirst += prefs.respagesize;
}
emit prevPageAvailable(hasPrev);
bool gotone = false;
clear();
int last = MIN(resCnt - m_winfirst, prefs.respagesize);
m_curDocs.clear();
// Query term colorization
QStyleSheetItem *item =
new QStyleSheetItem(styleSheet(), "termtag" );
item->setColor("blue");
// item->setFontWeight(QFont::Bold);
list<string> qTerms;
m_docsource->getTerms(qTerms);
// Insert results if any in result list window. We have to send
// the text to the widgets, because we need the paragraph number
// each time we add a result paragraph (its diffult and
// error-prone to compute the paragraph numbers in parallel). We
// would like to disable updates while we're doing this, but
// couldn't find a way to make it work, the widget seems to become
// confused if appended while updates are disabled
// setUpdatesEnabled(false);
for (int i = 0; i < last; i++) {
string sh;
doc.erase();
if (!m_docsource->getDoc(m_winfirst + i, doc, &percent, &sh)) {
// Error or end of docs, stop.
break;
}
if (percent == -1) {
percent = 0;
// Document not available, maybe other further, will go on.
doc.abstract = string(tr("Unavailable document").utf8());
}
if (i == 0) {
// Display list header
// We could use a <title> but the textedit doesnt display
// it prominently
append("<qt><head></head><body>");
// Note: have to append text in chunks that make sense
// html-wise. If we break things up to much, the editor
// gets confused. Hence the use of the 'chunk' text
// accumulator
QString chunk = "<p><font size=+1><b>";
chunk += QString::fromUtf8(m_docsource->title().c_str());
chunk += "</b></font><br>";
chunk += "<a href=\"H-1\">";
chunk += tr("Show query details");
chunk += "</a><br>";
append(chunk);
chunk = tr("<b>Displaying results starting at index"
" %1 (maximum set size %2)</b></p>\n")
.arg(m_winfirst+1)
.arg(resCnt);
append(chunk);
}
gotone = true;
// Determine icon to display if any
string img_name;
if (prefs.showicons) {
string iconpath;
string iconname = rclconfig->getMimeIconName(doc.mimetype,
&iconpath);
LOGDEB1(("Img file; %s\n", iconpath.c_str()));
QImage image(iconpath.c_str());
if (!image.isNull()) {
img_name = string("img_") + iconname;
QMimeSourceFactory::defaultFactory()->
setImage(img_name.c_str(), image);
}
}
// Percentage of 'relevance'
char perbuf[10];
sprintf(perbuf, "%3d%% ", percent);
// Printable url: either utf-8 if transcoding succeeds, or url-encoded
string url;
printableUrl(doc.url, url);
// Make title out of file name if none yet
if (doc.title.empty()) {
doc.title = path_getsimple(url);
}
// Document date: either doc or file modification time
char datebuf[100];
datebuf[0] = 0;
if (!doc.dmtime.empty() || !doc.fmtime.empty()) {
time_t mtime = doc.dmtime.empty() ?
atol(doc.fmtime.c_str()) : atol(doc.dmtime.c_str());
struct tm *tm = localtime(&mtime);
strftime(datebuf, 99, "&nbsp;%Y-%m-%d&nbsp;%H:%M:%S", tm);
}
// Size information. We print both doc and file if they differ a lot
long fsize = -1, dsize = -1;
if (!doc.dbytes.empty())
dsize = atol(doc.dbytes.c_str());
if (!doc.fbytes.empty())
fsize = atol(doc.fbytes.c_str());
string sizebuf;
if (dsize > 0) {
sizebuf = displayableBytes(dsize);
if (fsize > 10 * dsize && fsize - dsize > 1000)
sizebuf += string(" / ") + displayableBytes(fsize);
} else if (fsize >= 0) {
sizebuf = displayableBytes(fsize);
}
// Abstract
string abst;
plaintorich(doc.abstract, abst, qTerms, 0, true);
//string abst = escapeHtml(doc.abstract);
LOGDEB1(("Abstract: {%s}\n", abst.c_str()));
// Concatenate chunks to build the result list paragraph:
string result;
if (!sh.empty())
result += string("<p><b>") + sh + "</p>\n<p>";
else
result += "<p>";
// Percent relevant + size + preview/edit links + title
result += string(perbuf) + sizebuf;
char vlbuf[100];
if (canIntern(doc.mimetype, rclconfig)) {
sprintf(vlbuf, "\"P%d\"", m_winfirst+i);
result += string("<a href=") + vlbuf + ">" + "Preview" + "</a>"
+ "&nbsp;&nbsp;";
}
if (!rclconfig->getMimeViewerDef(doc.mimetype).empty()) {
sprintf(vlbuf, "E%d", m_winfirst+i);
result += string("<a href=") + vlbuf + ">" + "Edit" + "</a>";
}
result += "&nbsp;&nbsp;<b>" + doc.title + "</b><br>";
// Mime type, date modified, url
result += doc.mimetype + "&nbsp;";
result += string(datebuf) + "&nbsp;&nbsp;&nbsp;";
if (!img_name.empty()) {
result += "<img source=\"" + img_name + "\" align=\"left\">";
}
result += "<i>" + url + +"</i><br>";
// Text: abstract and keywords
if (!abst.empty())
result += abst + "<br>";
if (!doc.keywords.empty())
result += doc.keywords + "<br>";
result += "</p>\n";
QString str = QString::fromUtf8(result.c_str(), result.length());
append(str);
setCursorPosition(0,0);
m_pageParaToReldocnums[paragraphs()-1] = i;
m_curDocs.push_back(doc);
}
bool hasNext = false;
if (m_winfirst >= 0 && m_winfirst + prefs.respagesize < resCnt) {
hasNext = true;
}
if (gotone) {
QString chunk = "<p align=\"center\">";
if (hasPrev || hasNext) {
if (hasPrev) {
chunk += "<a href=\"p-1\"><b>";
chunk += tr("Previous");
chunk += "</b></a>&nbsp;&nbsp;&nbsp;";
}
if (hasNext) {
chunk += "<a href=\"n-1\"><b>";
chunk += tr("Next");
chunk += "</b></a>";
}
chunk += "</p>\n";
append(chunk);
}
append("</body></qt>");
ensureCursorVisible();
} else {
// Restore first in win parameter that we shouln't have incremented
QString chunk = "<p><font size=+1><b>";
chunk += QString::fromUtf8(m_docsource->title().c_str());
chunk += "</b></font><br>";
chunk += "<a href=\"H-1\">";
chunk += tr("Show query details");
chunk += "</a><br>";
append(chunk);
append(tr("<p><b>No results found</b><br>"));
m_winfirst -= prefs.respagesize;
if (m_winfirst < 0)
m_winfirst = -1;
hasNext = false;
}
// Possibly color paragraph of current preview if any
previewExposed(m_curPvDoc);
emit nextPageAvailable(hasNext);
}
// Single click in result list use this for document selection, if no
// text selection active:
void ResList::clicked(int par, int)
{
LOGDEB2(("ResList::clicked\n"));
// It's very ennoying, textBrowser always has selected text. This
// is bound to change with qt releases!
if (hasSelectedText()&& selectedText().compare("<!--StartFragment-->")) {
// Give priority to text selection, do nothing
LOGDEB2(("%s\n", (const char *)(selectedText().ascii())));
return;
}
LOGDEB(("click at par %d (with %s %s)\n", par,
(m_lstClckMod & ControlButton) ? "Ctrl" : "",
(m_lstClckMod & ShiftButton) ? "Shft" : ""));
}
// Color paragraph (if any) of currently visible preview
void ResList::previewExposed(int docnum)
{
LOGDEB(("ResList::previewExposed: doc %d\n", docnum));
// Possibly erase old one to white
int par;
if (m_curPvDoc != -1 && (par = parnumfromdocnum(m_curPvDoc)) != -1) {
QColor color("white");
setParagraphBackgroundColor(par, color);
m_curPvDoc = -1;
}
m_curPvDoc = docnum;
par = parnumfromdocnum(docnum);
// Maybe docnum is -1 or not in this window,
if (par < 0)
return;
setCursorPosition(par, 1);
ensureCursorVisible();
// Color the new active paragraph
QColor color("LightBlue");
setParagraphBackgroundColor(par, color);
}
// Double click in res list: add selection to simple search
void ResList::doubleClicked(int, int)
{
if (hasSelectedText())
emit(wordSelect(selectedText()));
}
void ResList::linkWasClicked(const QString &s)
{
LOGDEB(("ResList::linkClicked: [%s]\n", s.ascii()));
int i = atoi(s.ascii()+1);
int what = s.ascii()[0];
switch (what) {
case 'H':
emit headerClicked();
break;
case 'P':
emit docPreviewClicked(i);
break;
case 'E':
emit docEditClicked(i);
break;
case 'n':
resultPageNext();
break;
case 'p':
resultPageBack();
break;
default: break;// ??
}
}
QPopupMenu *ResList::createPopupMenu(const QPoint& pos)
{
int para = paragraphAt(pos);
clicked(para, 0);
m_popDoc = docnumfromparnum(para);
QPopupMenu *popup = new QPopupMenu(this, "qt_edit_menu");
popup->insertItem(tr("&Preview"), this, SLOT(menuPreview()));
popup->insertItem(tr("&Edit"), this, SLOT(menuEdit()));
popup->insertItem(tr("&Copy File Name"), this, SLOT(menuCopyFN()));
popup->insertItem(tr("Copy &Url"), this, SLOT(menuCopyURL()));
popup->insertItem(tr("Find &similar documents"), this, SLOT(menuExpand()));
return popup;
}
void ResList::menuPreview()
{
emit docPreviewClicked(m_popDoc);
}
void ResList::menuEdit()
{
emit docEditClicked(m_popDoc);
}
void ResList::menuCopyFN()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc)) {
// Our urls currently always begin with "file://"
QApplication::clipboard()->setText(doc.url.c_str()+7,
QClipboard::Selection);
}
}
void ResList::menuCopyURL()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc)) {
string url = url_encode(doc.url, 7);
QApplication::clipboard()->setText(url.c_str(),
QClipboard::Selection);
}
}
void ResList::menuExpand()
{
Rcl::Doc doc;
if (rcldb && getDoc(m_popDoc, doc)) {
emit docExpand(m_popDoc);
}
}
QString ResList::getDescription()
{
return QString::fromUtf8(m_queryData.description.c_str());
}
/** Show detailed expansion of a query */
void ResList::showQueryDetails()
{
// Break query into lines of reasonable length, avoid cutting words,
// Also limit the total number of lines.
const unsigned int ll = 100;
const unsigned int maxlines = 50;
string query = m_queryData.description;
string oq;
unsigned int nlines = 0;
while (query.length() > 0) {
string ss = query.substr(0, ll);
if (ss.length() == ll) {
string::size_type pos = ss.find_last_of(" ");
if (pos == string::npos) {
pos = query.find_first_of(" ");
if (pos != string::npos)
ss = query.substr(0, pos+1);
else
ss = query;
} else {
ss = ss.substr(0, pos+1);
}
}
// This cant happen, but anyway. Be very sure to avoid an infinite loop
if (ss.length() == 0) {
LOGDEB(("showQueryDetails: Internal error!\n"));
oq = query;
break;
}
oq += ss + "\n";
if (nlines++ >= maxlines) {
oq += " ... \n";
break;
}
query= query.substr(ss.length());
LOGDEB1(("oq [%s]\n, query [%s]\n, ss [%s]\n",
oq.c_str(), query.c_str(), ss.c_str()));
}
QString desc = tr("Query details") + ": " +
QString::fromUtf8(oq.c_str());
QMessageBox::information(this, tr("Query details"), desc);
}