Parent: [504705] (diff)

Download this file

reslist.cpp    1258 lines (1148 with data), 37.6 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
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
/* 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 "autoconfig.h"
#include <time.h>
#include <stdlib.h>
#include <memory>
#include <qapplication.h>
#include <qvariant.h>
#include <qevent.h>
#include <qmenu.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qtimer.h>
#include <qmessagebox.h>
#include <qimage.h>
#include <qscrollbar.h>
#include <QTextBlock>
#include <QShortcut>
#include "log.h"
#include "smallut.h"
#include "recoll.h"
#include "guiutils.h"
#include "pathut.h"
#include "docseq.h"
#include "pathut.h"
#include "mimehandler.h"
#include "plaintorich.h"
#include "internfile.h"
#include "indexer.h"
#include "snippets_w.h"
#include "listdialog.h"
#include "reslist.h"
#include "moc_reslist.cpp"
#include "rclhelp.h"
#include "appformime.h"
#include "respopup.h"
#include "reslistpager.h"
static const QKeySequence quitKeySeq("Ctrl+q");
static const QKeySequence closeKeySeq("Ctrl+w");
#if defined(USING_WEBKIT)
# include <QWebFrame>
# include <QWebElement>
# include <QWebSettings>
# define QWEBSETTINGS QWebSettings
#elif defined(USING_WEBENGINE)
// Notes for WebEngine
// - All links must begin with http:// for acceptNavigationRequest to be
// called.
// - The links passed to acceptNav.. have the host part
// lowercased -> we change S0 to http://localhost/S0, not http://S0
# include <QWebEnginePage>
# include <QWebEngineSettings>
# include <QtWebEngineWidgets>
# define QWEBSETTINGS QWebEngineSettings
#endif
#ifdef USING_WEBENGINE
// This script saves the location details when a mouse button is
// clicked. This is for replacing data provided by Webkit QWebElement
// on a right-click as QT WebEngine does not have an equivalent service.
static const string locdetailscript(R"raw(
var locDetails = '';
function saveLoc(ev)
{
el = ev.target;
locDetails = '';
while (el && el.attributes && !el.attributes.getNamedItem("rcldocnum")) {
el = el.parentNode;
}
rcldocnum = el.attributes.getNamedItem("rcldocnum");
if (rcldocnum) {
rcldocnumvalue = rcldocnum.value;
} else {
rcldocnumvalue = "";
}
if (el && el.attributes) {
locDetails = 'rcldocnum = ' + rcldocnumvalue
}
}
)raw");
#endif // webengine
// Decide if we set font family and style with a css section in the
// html <head> or with qwebsettings setfont... calls. We currently do
// it with websettings because this gives an instant redisplay, and
// the css has a tendancy to not find some system fonts. Otoh,
// SetFontSize() needs a strange offset of 3, not needed with css.
#undef SETFONT_WITH_HEADSTYLE
class QtGuiResListPager : public ResListPager {
public:
QtGuiResListPager(ResList *p, int ps)
: ResListPager(ps), m_reslist(p)
{}
virtual bool append(const string& data);
virtual bool append(const string& data, int idx, const Rcl::Doc& doc);
virtual string trans(const string& in);
virtual string detailsLink();
virtual const string &parFormat();
virtual const string &dateFormat();
virtual string nextUrl();
virtual string prevUrl();
virtual string headerContent();
virtual void suggest(const vector<string>uterms,
map<string, vector<string> >& sugg);
virtual string absSep() {return (const char *)(prefs.abssep.toUtf8());}
virtual string iconUrl(RclConfig *, Rcl::Doc& doc);
#ifdef USING_WEBENGINE
virtual string linkPrefix() override {return "http://localhost/";}
#endif
private:
ResList *m_reslist;
};
#if 0
FILE *fp;
void logdata(const char *data)
{
if (fp == 0)
fp = fopen("/tmp/recolltoto.html", "a");
if (fp)
fprintf(fp, "%s", data);
}
#else
#define logdata(X)
#endif
//////////////////////////////
// /// QtGuiResListPager methods:
bool QtGuiResListPager::append(const string& data)
{
LOGDEB2("QtGuiReslistPager::appendString : " << data << "\n");
logdata(data.c_str());
m_reslist->append(QString::fromUtf8(data.c_str()));
return true;
}
bool QtGuiResListPager::append(const string& data, int docnum,
const Rcl::Doc&)
{
LOGDEB2("QtGuiReslistPager::appendDoc: blockCount " <<
m_reslist->document()->blockCount() << ", " << data << "\n");
logdata(data.c_str());
#if defined(USING_WEBKIT) || defined(USING_WEBENGINE)
QString sdoc = QString(
"<div class=\"rclresult\" id=\"%1\" rcldocnum=\"%1\">").arg(docnum);
m_reslist->append(sdoc);
m_reslist->append(QString::fromUtf8(data.c_str()));
m_reslist->append("</div>");
#else
int blkcnt0 = m_reslist->document()->blockCount();
m_reslist->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
m_reslist->textCursor().insertBlock();
m_reslist->insertHtml(QString::fromUtf8(data.c_str()));
m_reslist->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
m_reslist->ensureCursorVisible();
int blkcnt1 = m_reslist->document()->blockCount();
for (int block = blkcnt0; block < blkcnt1; block++) {
m_reslist->m_pageParaToReldocnums[block] = docnum;
}
#endif
return true;
}
string QtGuiResListPager::trans(const string& in)
{
return string((const char*)ResList::tr(in.c_str()).toUtf8());
}
string QtGuiResListPager::detailsLink()
{
string chunk = string("<a href=\"") + linkPrefix() + "H-1\">";
chunk += trans("(show query)");
chunk += "</a>";
return chunk;
}
const string& QtGuiResListPager::parFormat()
{
return prefs.creslistformat;
}
const string& QtGuiResListPager::dateFormat()
{
return prefs.creslistdateformat;
}
string QtGuiResListPager::nextUrl()
{
return "n-1";
}
string QtGuiResListPager::prevUrl()
{
return "p-1";
}
string QtGuiResListPager::headerContent()
{
string out;
out = "<style type=\"text/css\">\nbody,table,select,input {\n";
#ifdef SETFONT_WITH_HEADSTYLE
char ftsz[30];
sprintf(ftsz, "%d", prefs.reslistfontsize);
out += string("font-family: \"") + qs2utf8s(prefs.reslistfontfamily)
+ "\";\n";
out += string("font-size: ") + ftsz + "pt;\n";
#endif
out += string("color: ") + qs2utf8s(prefs.fontcolor) + ";\n";
out += string("}\n</style>\n");
#if defined(USING_WEBENGINE)
out += "<script type=\"text/javascript\">\n";
out += locdetailscript;
out += "</script>\n";
#endif
out += qs2utf8s(prefs.reslistheadertext);
return out;
}
void QtGuiResListPager::suggest(const vector<string>uterms,
map<string, vector<string> >& sugg)
{
sugg.clear();
bool issimple = m_reslist && m_reslist->m_rclmain &&
m_reslist->m_rclmain->lastSearchSimple();
for (const auto& uit : uterms) {
vector<string> tsuggs;
// If the term is in the dictionary, Aspell::suggest won't
// list alternatives. In fact we may want to check the
// frequencies and propose something anyway if a possible
// variation is much more common (as google does) ?
if (!rcldb->getSpellingSuggestions(uit, tsuggs)) {
continue;
}
// We should check that the term stems differently from the
// base word (else it's not useful to expand the search). Or
// is it ? This should depend if stemming is turned on or not
if (!tsuggs.empty()) {
sugg[uit] = vector<string>(tsuggs.begin(), tsuggs.end());
if (sugg[uit].size() > 5)
sugg[uit].resize(5);
// Set up the links as a <href="Sold|new">.
for (auto& it : sugg[uit]) {
if (issimple) {
it = string("<a href=\"S") + uit + "|" + it + "\">" +
it + "</a>";
}
}
}
}
}
string QtGuiResListPager::iconUrl(RclConfig *config, Rcl::Doc& doc)
{
if (doc.ipath.empty()) {
vector<Rcl::Doc> docs;
docs.push_back(doc);
vector<string> paths;
Rcl::docsToPaths(docs, paths);
if (!paths.empty()) {
string path;
LOGDEB2("ResList::iconUrl: source path [" << paths[0] << "]\n");
if (thumbPathForUrl(cstr_fileu + paths[0], 128, path)) {
LOGDEB2("ResList::iconUrl: icon path [" << path << "]\n");
return cstr_fileu + path;
} else {
LOGDEB2("ResList::iconUrl: no icon: path [" << path << "]\n");
}
} else {
LOGDEB("ResList::iconUrl: docsToPaths failed\n");
}
}
return ResListPager::iconUrl(config, doc);
}
/////// /////// End reslistpager methods
class PlainToRichQtReslist : public PlainToRich {
public:
virtual string startMatch(unsigned int idx) {
if (0 && m_hdata) {
string s1, s2;
stringsToString<vector<string> >(m_hdata->groups[idx], s1);
stringsToString<vector<string> >(
m_hdata->ugroups[m_hdata->grpsugidx[idx]], s2);
LOGDEB2("Reslist startmatch: group " << s1 << " user group " <<
s2 << "\n");
}
return string("<span class='rclmatch' style='")
+ qs2utf8s(prefs.qtermstyle) + string("'>");
}
virtual string endMatch() {
return string("</span>");
}
};
static PlainToRichQtReslist g_hiliter;
/////////////////////////////////////
ResList::ResList(QWidget* parent, const char* name)
: RESLIST_PARENTCLASS(parent)
{
if (!name)
setObjectName("resList");
else
setObjectName(name);
#if defined(USING_WEBKIT) || defined(USING_WEBENGINE)
setPage(new RclWebPage(this));
#ifdef USING_WEBKIT
LOGDEB("Reslist: using Webkit\n");
page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
// signals and slots connections
connect(this, SIGNAL(linkClicked(const QUrl &)),
this, SLOT(onLinkClicked(const QUrl &)));
#else
LOGDEB("Reslist: using Webengine\n");
#endif
settings()->setAttribute(QWEBSETTINGS::JavascriptEnabled, true);
#else
LOGDEB("Reslist: using QTextBrowser\n");
setReadOnly(true);
setUndoRedoEnabled(false);
setOpenLinks(false);
setTabChangesFocus(true);
// signals and slots connections
connect(this, SIGNAL(anchorClicked(const QUrl &)),
this, SLOT(onLinkClicked(const QUrl &)));
#endif
setFont();
languageChange();
(void)new HelpClient(this);
HelpClient::installMap(qs2utf8s(this->objectName()),
"RCL.SEARCH.GUI.RESLIST");
#if 0
// See comments in "highlighted
connect(this, SIGNAL(highlighted(const QString &)),
this, SLOT(highlighted(const QString &)));
#endif
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(createPopupMenu(const QPoint&)));
m_pager = new QtGuiResListPager(this, prefs.respagesize);
m_pager->setHighLighter(&g_hiliter);
}
ResList::~ResList()
{
// These have to exist somewhere for translations to work
#ifdef __GNUC__
__attribute__((unused))
#endif
static const char* strings[] = {
QT_TR_NOOP("<p><b>No results found</b><br>"),
QT_TR_NOOP("Documents"),
QT_TR_NOOP("out of at least"),
QT_TR_NOOP("for"),
QT_TR_NOOP("Previous"),
QT_TR_NOOP("Next"),
QT_TR_NOOP("Unavailable document"),
QT_TR_NOOP("Preview"),
QT_TR_NOOP("Open"),
QT_TR_NOOP("Snippets"),
QT_TR_NOOP("(show query)"),
QT_TR_NOOP("<p><i>Alternate spellings (accents suppressed): </i>"),
QT_TR_NOOP("<p><i>Alternate spellings: </i>"),
};
}
void ResList::setRclMain(RclMain *m, bool ismain)
{
m_rclmain = m;
m_ismainres = ismain;
if (!m_ismainres) {
connect(new QShortcut(closeKeySeq, this), SIGNAL (activated()),
this, SLOT (close()));
connect(new QShortcut(quitKeySeq, this), SIGNAL (activated()),
m_rclmain, SLOT (fileExit()));
connect(this, SIGNAL(previewRequested(Rcl::Doc)),
m_rclmain, SLOT(startPreview(Rcl::Doc)));
connect(this, SIGNAL(docSaveToFileClicked(Rcl::Doc)),
m_rclmain, SLOT(saveDocToFile(Rcl::Doc)));
connect(this, SIGNAL(editRequested(Rcl::Doc)),
m_rclmain, SLOT(startNativeViewer(Rcl::Doc)));
}
}
void ResList::runStoredJS()
{
runJS(m_js);
m_js.clear();
}
void ResList::runJS(const QString& js)
{
#if defined(USING_WEBKIT)
page()->mainFrame()->evaluateJavaScript(js);
#elif defined(USING_WEBENGINE)
page()->runJavaScript(js);
#else
Q_UNUSED(js);
#endif
}
void ResList::setFont()
{
#if defined(USING_WEBKIT) || defined(USING_WEBENGINE)
# ifndef SETFONT_WITH_HEADSTYLE
if (prefs.reslistfontfamily.length()) {
// For some reason there is (12-2014) an offset of 3 between what
// we request from webkit and what we get.
settings()->setFontSize(QWEBSETTINGS::DefaultFontSize,
prefs.reslistfontsize + 3);
settings()->setFontFamily(QWEBSETTINGS::StandardFont,
prefs.reslistfontfamily);
} else {
settings()->resetFontSize(QWEBSETTINGS::DefaultFontSize);
settings()->resetFontFamily(QWEBSETTINGS::StandardFont);
}
# endif
#else
if (prefs.reslistfontfamily.length()) {
QFont nfont(prefs.reslistfontfamily, prefs.reslistfontsize);
QTextBrowser::setFont(nfont);
} else {
QTextBrowser::setFont(QFont());
}
#endif
}
int ResList::newListId()
{
static int id;
return ++id;
}
void ResList::setDocSource(std::shared_ptr<DocSequence> nsource)
{
LOGDEB("ResList::setDocSource()\n");
m_source = std::shared_ptr<DocSequence>(new DocSource(theconfig, nsource));
if (m_pager)
m_pager->setDocSource(m_source);
}
// A query was executed, or the filtering/sorting parameters changed,
// re-read the results.
void ResList::readDocSource()
{
LOGDEB("ResList::readDocSource()\n");
resetView();
if (!m_source)
return;
m_listId = newListId();
// Reset the page size in case the preference was changed
m_pager->setPageSize(prefs.respagesize);
m_pager->setDocSource(m_source);
resultPageNext();
emit hasResults(m_source->getResCnt());
}
void ResList::resetList()
{
LOGDEB("ResList::resetList()\n");
setDocSource(std::shared_ptr<DocSequence>());
resetView();
}
void ResList::resetView()
{
m_curPvDoc = -1;
// There should be a progress bar for long searches but there isn't
// We really want the old result list to go away, otherwise, for a
// slow search, the user will wonder if anything happened. The
// following helps making sure that the textedit is really
// blank. Else, there are often icons or text left around
#if defined(USING_WEBKIT) || defined(USING_WEBENGINE)
m_text = "";
setHtml("<html><body></body></html>");
#else
m_pageParaToReldocnums.clear();
clear();
QTextBrowser::append(".");
clear();
#endif
}
bool ResList::displayingHistory()
{
// We want to reset the displayed history if it is currently
// shown. Using the title value is an ugly hack
string htstring = string((const char *)tr("Document history").toUtf8());
if (!m_source || m_source->title().empty())
return false;
return m_source->title().find(htstring) == 0;
}
void ResList::languageChange()
{
setWindowTitle(tr("Result list"));
}
#if !defined(USING_WEBKIT) && !defined(USING_WEBENGINE)
// Get document number from text block number
int ResList::docnumfromparnum(int block)
{
if (m_pager->pageNumber() < 0)
return -1;
// Try to find the first number < input and actually in the map
// (result blocks can be made of several text blocks)
std::map<int,int>::iterator it;
do {
it = m_pageParaToReldocnums.find(block);
if (it != m_pageParaToReldocnums.end())
return pageFirstDocNum() + it->second;
} while (--block >= 0);
return -1;
}
// Get range of paragraph numbers which make up the result for document number
pair<int,int> ResList::parnumfromdocnum(int docnum)
{
LOGDEB("parnumfromdocnum: docnum " << docnum << "\n");
if (m_pager->pageNumber() < 0) {
LOGDEB("parnumfromdocnum: no page return -1,-1\n");
return pair<int,int>(-1,-1);
}
int winfirst = pageFirstDocNum();
if (docnum - winfirst < 0) {
LOGDEB("parnumfromdocnum: docnum " << docnum << " < winfirst " <<
winfirst << " return -1,-1\n");
return pair<int,int>(-1,-1);
}
docnum -= winfirst;
for (std::map<int,int>::iterator it = m_pageParaToReldocnums.begin();
it != m_pageParaToReldocnums.end(); it++) {
if (docnum == it->second) {
int first = it->first;
int last = first+1;
std::map<int,int>::iterator it1;
while ((it1 = m_pageParaToReldocnums.find(last)) !=
m_pageParaToReldocnums.end() && it1->second == docnum) {
last++;
}
LOGDEB("parnumfromdocnum: return " << first << "," << last << "\n");
return pair<int,int>(first, last);
}
}
LOGDEB("parnumfromdocnum: not found return -1,-1\n");
return pair<int,int>(-1,-1);
}
#endif // TEXTBROWSER
// Return doc from current or adjacent result pages. We can get called
// for a document not in the current page if the user browses through
// results inside a result window (with shift-arrow). This can only
// result in a one-page change.
bool ResList::getDoc(int docnum, Rcl::Doc &doc)
{
LOGDEB("ResList::getDoc: docnum " << docnum << " winfirst " <<
pageFirstDocNum() << "\n");
int winfirst = pageFirstDocNum();
int winlast = m_pager->pageLastDocNum();
if (docnum < 0 || winfirst < 0 || winlast < 0)
return false;
// Is docnum in current page ? Then all Ok
if (docnum >= winfirst && docnum <= winlast) {
return m_pager->getDoc(docnum, doc);
}
// Else we accept to page down or up but not further
if (docnum < winfirst && docnum >= winfirst - prefs.respagesize) {
resultPageBack();
} else if (docnum < winlast + 1 + prefs.respagesize) {
resultPageNext();
}
winfirst = pageFirstDocNum();
winlast = m_pager->pageLastDocNum();
if (docnum >= winfirst && docnum <= winlast) {
return m_pager->getDoc(docnum, doc);
}
return false;
}
void ResList::keyPressEvent(QKeyEvent * e)
{
if ((e->modifiers() & Qt::ShiftModifier)) {
if (e->key() == Qt::Key_PageUp) {
// Shift-PageUp -> first page of results
resultPageFirst();
return;
}
} else {
if (e->key() == Qt::Key_PageUp || e->key() == Qt::Key_Backspace) {
resPageUpOrBack();
return;
} else if (e->key() == Qt::Key_PageDown || e->key() == Qt::Key_Space) {
resPageDownOrNext();
return;
}
}
RESLIST_PARENTCLASS::keyPressEvent(e);
}
void ResList::mouseReleaseEvent(QMouseEvent *e)
{
m_lstClckMod = 0;
if (e->modifiers() & Qt::ControlModifier) {
m_lstClckMod |= Qt::ControlModifier;
}
if (e->modifiers() & Qt::ShiftModifier) {
m_lstClckMod |= Qt::ShiftModifier;
}
RESLIST_PARENTCLASS::mouseReleaseEvent(e);
}
void ResList::highlighted(const QString& )
{
// This is supposedly called when a link is preactivated (hover or tab
// traversal, but is not actually called for tabs. We would have liked to
// give some kind of visual feedback for tab traversal
}
// 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()
{
#if defined(USING_WEBKIT)
if (scrollIsAtTop()) {
resultPageBack();
} else {
page()->mainFrame()->scroll(0, -int(0.9*geometry().height()));
}
setupArrows();
#elif defined(USING_WEBENGINE)
if (scrollIsAtTop()) {
resultPageBack();
} else {
QString js = "window.scrollBy(" +
QString::number(0) + ", " +
QString::number(-int(0.9*geometry().height())) + ");";
runJS(js);
}
setupArrows();
#else
int vpos = verticalScrollBar()->value();
verticalScrollBar()->triggerAction(QAbstractSlider::SliderPageStepSub);
if (vpos == verticalScrollBar()->value())
resultPageBack();
#endif
}
void ResList::resPageDownOrNext()
{
#if defined(USING_WEBKIT)
if (scrollIsAtBottom()) {
resultPageNext();
} else {
page()->mainFrame()->scroll(0, int(0.9*geometry().height()));
}
setupArrows();
#elif defined(USING_WEBENGINE)
if (scrollIsAtBottom()) {
resultPageNext();
} else {
QString js = "window.scrollBy(" +
QString::number(0) + ", " +
QString::number(int(0.9*geometry().height())) + ");";
runJS(js);
}
setupArrows();
#else
int vpos = verticalScrollBar()->value();
verticalScrollBar()->triggerAction(QAbstractSlider::SliderPageStepAdd);
LOGDEB("ResList::resPageDownOrNext: vpos before " << vpos << ", after "
<< verticalScrollBar()->value() << "\n");
if (vpos == verticalScrollBar()->value())
resultPageNext();
#endif
}
void ResList::setupArrows()
{
emit prevPageAvailable(m_pager->hasPrev() || !scrollIsAtTop());
emit nextPageAvailable(m_pager->hasNext() || !scrollIsAtBottom());
}
bool ResList::scrollIsAtBottom()
{
#if defined(USING_WEBKIT)
QWebFrame *frame = page()->mainFrame();
bool ret;
if (!frame || frame->scrollBarGeometry(Qt::Vertical).isEmpty()) {
ret = true;
} else {
int max = frame->scrollBarMaximum(Qt::Vertical);
int cur = frame->scrollBarValue(Qt::Vertical);
ret = (max != 0) && (cur == max);
LOGDEB2("Scrollatbottom: cur " << cur << " max " << max << "\n");
}
LOGDEB2("scrollIsAtBottom: returning " << ret << "\n");
return ret;
#elif defined(USING_WEBENGINE)
QSize css = page()->contentsSize().toSize();
QSize wss = size();
QPoint sp = page()->scrollPosition().toPoint();
LOGDEB1("atBottom: contents W " << css.width() << " H " << css.height() <<
" widget W " << wss.width() << " Y " << wss.height() <<
" scroll X " << sp.x() << " Y " << sp.y() << "\n");
// This seems to work but it's mysterious as points and pixels
// should not be the same
return wss.height() + sp.y() >= css.height() - 10;
#else
return false;
#endif
}
bool ResList::scrollIsAtTop()
{
#if defined(USING_WEBKIT)
QWebFrame *frame = page()->mainFrame();
bool ret;
if (!frame || frame->scrollBarGeometry(Qt::Vertical).isEmpty()) {
ret = true;
} else {
int cur = frame->scrollBarValue(Qt::Vertical);
int min = frame->scrollBarMinimum(Qt::Vertical);
LOGDEB("Scrollattop: cur " << cur << " min " << min << "\n");
ret = (cur == min);
}
LOGDEB2("scrollIsAtTop: returning " << ret << "\n");
return ret;
#elif defined(USING_WEBENGINE)
return page()->scrollPosition().toPoint().ry() == 0;
#else
return false;
#endif
}
// Show previous page of results. We just set the current number back
// 2 pages and show next page.
void ResList::resultPageBack()
{
if (m_pager->hasPrev()) {
m_pager->resultPageBack();
displayPage();
}
}
// Go to the first page
void ResList::resultPageFirst()
{
// In case the preference was changed
m_pager->setPageSize(prefs.respagesize);
m_pager->resultPageFirst();
displayPage();
}
// Fill up result list window with next screen of hits
void ResList::resultPageNext()
{
if (m_pager->hasNext()) {
m_pager->resultPageNext();
displayPage();
}
}
void ResList::resultPageFor(int docnum)
{
m_pager->resultPageFor(docnum);
displayPage();
}
void ResList::append(const QString &text)
{
LOGDEB2("QtGuiReslistPager::appendQString : " << qs2utf8s(text) << "\n");
#if defined(USING_WEBKIT) || defined(USING_WEBENGINE)
m_text += text;
#else
QTextBrowser::append(text);
#endif
}
void ResList::displayPage()
{
resetView();
m_pager->displayPage(theconfig);
#if defined(USING_WEBENGINE) || defined(USING_WEBKIT)
setHtml(m_text);
#endif
#if defined(USING_WEBENGINE)
// Have to delay running this. Alternative would be to set it as
// onload on the body element in the html, like upplay does, but
// this would need an ennoying reslistpager modification.
m_js = "elt=document.getElementsByTagName('body')[0];"
"elt.addEventListener('contextmenu', saveLoc);";
QTimer::singleShot(200, this, SLOT(runStoredJS()));
#endif
LOGDEB0("ResList::displayPg: hasNext " << m_pager->hasNext() <<
" atBot " << scrollIsAtBottom() << " hasPrev " <<
m_pager->hasPrev() << " at Top " << scrollIsAtTop() << " \n");
setupArrows();
// Possibly color paragraph of current preview if any
previewExposed(m_curPvDoc);
}
// Color paragraph (if any) of currently visible preview
void ResList::previewExposed(int docnum)
{
LOGDEB("ResList::previewExposed: doc " << docnum << "\n");
// Possibly erase old one to white
if (m_curPvDoc != -1) {
#if defined(USING_WEBKIT)
QString sel =
QString("div[rcldocnum=\"%1\"]").arg(m_curPvDoc - pageFirstDocNum());
LOGDEB2("Searching for element, selector: [" << qs2utf8s(sel) << "]\n");
QWebElement elt = page()->mainFrame()->findFirstElement(sel);
if (!elt.isNull()) {
LOGDEB2("Found\n");
elt.removeAttribute("style");
} else {
LOGDEB2("Not Found\n");
}
#elif defined(USING_WEBENGINE)
QString js = QString(
"elt=document.getElementById('%1');"
"if (elt){elt.removeAttribute('style');}"
).arg(m_curPvDoc - pageFirstDocNum());
runJS(js);
#else
pair<int,int> blockrange = parnumfromdocnum(m_curPvDoc);
if (blockrange.first != -1) {
for (int blockn = blockrange.first;
blockn < blockrange.second; blockn++) {
QTextBlock block = document()->findBlockByNumber(blockn);
QTextCursor cursor(block);
QTextBlockFormat format = cursor.blockFormat();
format.clearBackground();
cursor.setBlockFormat(format);
}
}
#endif
m_curPvDoc = -1;
}
// Set background for active preview's doc entry
m_curPvDoc = docnum;
#if defined(USING_WEBKIT)
QString sel =
QString("div[rcldocnum=\"%1\"]").arg(docnum - pageFirstDocNum());
LOGDEB2("Searching for element, selector: [" << qs2utf8s(sel) << "]\n");
QWebElement elt = page()->mainFrame()->findFirstElement(sel);
if (!elt.isNull()) {
LOGDEB2("Found\n");
elt.setAttribute("style", "background: LightBlue;}");
} else {
LOGDEB2("Not Found\n");
}
#elif defined(USING_WEBENGINE)
QString js = QString(
"elt=document.getElementById('%1');"
"if(elt){elt.setAttribute('style', 'background: LightBlue');}"
).arg(docnum - pageFirstDocNum());
runJS(js);
#else
pair<int,int> blockrange = parnumfromdocnum(docnum);
// Maybe docnum is -1 or not in this window,
if (blockrange.first < 0)
return;
// Color the new active paragraph
QColor color("LightBlue");
for (int blockn = blockrange.first+1;
blockn < blockrange.second; blockn++) {
QTextBlock block = document()->findBlockByNumber(blockn);
QTextCursor cursor(block);
QTextBlockFormat format;
format.setBackground(QBrush(color));
cursor.mergeBlockFormat(format);
setTextCursor(cursor);
ensureCursorVisible();
}
#endif
}
// Double click in res list: add selection to simple search
void ResList::mouseDoubleClickEvent(QMouseEvent *event)
{
RESLIST_PARENTCLASS::mouseDoubleClickEvent(event);
#if defined(USING_WEBKIT)
emit(wordSelect(selectedText()));
#elif defined(USING_WEBENGINE)
// webengineview does not have such an event function, and
// reimplementing event() itself is not useful (tried) as it does
// not get mouse clicks. We'd need javascript to do this, but it's
// not that useful, so left aside for now.
#else
if (textCursor().hasSelection())
emit(wordSelect(textCursor().selectedText()));
#endif
}
void ResList::showQueryDetails()
{
if (!m_source)
return;
string oq = breakIntoLines(m_source->getDescription(), 100, 50);
QString str;
QString desc = tr("Result count (est.)") + ": " +
str.setNum(m_source->getResCnt()) + "<br>";
desc += tr("Query details") + ": " + QString::fromUtf8(oq.c_str());
QMessageBox::information(this, tr("Query details"), desc);
}
void ResList::onLinkClicked(const QUrl &qurl)
{
// qt5: url.toString() does not accept FullyDecoded, but that's what we
// want. e.g. Suggestions links are like Sterm|spelling which we
// receive as Sterm%7CSpelling
string strurl = url_decode(qs2utf8s(qurl.toString()));
LOGDEB1("ResList::onLinkClicked: [" << strurl << "] prefix " <<
m_pager->linkPrefix() << "\n");
strurl = strurl.substr(m_pager->linkPrefix().size());
int what = strurl[0];
switch (what) {
// Open abstract/snippets window
case 'A':
{
if (!m_source)
return;
int i = atoi(strurl.c_str()+1) - 1;
Rcl::Doc doc;
if (!getDoc(i, doc)) {
LOGERR("ResList::onLinkClicked: can't get doc for " << i << "\n");
return;
}
emit(showSnippets(doc));
}
break;
// Show duplicates
case 'D':
{
if (!m_source)
return;
int i = atoi(strurl.c_str()+1) - 1;
Rcl::Doc doc;
if (!getDoc(i, doc)) {
LOGERR("ResList::onLinkClicked: can't get doc for " << i << "\n");
return;
}
vector<Rcl::Doc> dups;
if (m_source->docDups(doc, dups) && m_rclmain) {
m_rclmain->newDupsW(doc, dups);
}
}
break;
// Open parent folder
case 'F':
{
int i = atoi(strurl.c_str()+1) - 1;
Rcl::Doc doc;
if (!getDoc(i, doc)) {
LOGERR("ResList::onLinkClicked: can't get doc for " << i << "\n");
return;
}
emit editRequested(ResultPopup::getParent(std::shared_ptr<DocSequence>(),
doc));
}
break;
// Show query details
case 'h':
case 'H':
{
showQueryDetails();
break;
}
// Preview and edit
case 'P':
case 'E':
{
int i = atoi(strurl.c_str()+1) - 1;
Rcl::Doc doc;
if (!getDoc(i, doc)) {
LOGERR("ResList::onLinkClicked: can't get doc for " << i << "\n");
return;
}
if (what == 'P') {
if (m_ismainres) {
emit docPreviewClicked(i, doc, m_lstClckMod);
} else {
emit previewRequested(doc);
}
} else {
emit editRequested(doc);
}
}
break;
// Next/prev page
case 'n':
resultPageNext();
break;
case 'p':
resultPageBack();
break;
// Run script. Link format Rnn|Script Name
case 'R':
{
int i = atoi(strurl.c_str() + 1) - 1;
QString s = qurl.toString();
int bar = s.indexOf("|");
if (bar == -1 || bar >= s.size()-1)
break;
string cmdname = qs2utf8s(s.right(s.size() - (bar + 1)));
DesktopDb ddb(path_cat(theconfig->getConfDir(), "scripts"));
DesktopDb::AppDef app;
if (ddb.appByName(cmdname, app)) {
QAction act(QString::fromUtf8(app.name.c_str()), this);
QVariant v(QString::fromUtf8(app.command.c_str()));
act.setData(v);
m_popDoc = i;
menuOpenWith(&act);
}
}
break;
// Spelling: replacement suggestion clicked
case 'S':
{
string s;
if (!strurl.empty())
s = strurl.substr(1);
string::size_type bar = s.find_first_of("|");
if (bar != string::npos && bar < s.size() - 1) {
string o = s.substr(0, bar);
string n = s.substr(bar+1);
LOGDEB2("Emitting wordreplace " << o << " -> " << n << std::endl);
emit wordReplace(u8s2qs(o), u8s2qs(n));
}
}
break;
default:
LOGERR("ResList::onLinkClicked: bad link [" << strurl << "]\n");
break;// ??
}
}
void ResList::onPopupJsDone(const QVariant &jr)
{
QString qs(jr.toString());
LOGDEB("onPopupJsDone: parameter: " << qs2utf8s(qs) << "\n");
QStringList qsl = qs.split("\n", QString::SkipEmptyParts);
for (int i = 0 ; i < qsl.size(); i++) {
int eq = qsl[i].indexOf("=");
if (eq > 0) {
QString nm = qsl[i].left(eq).trimmed();
QString value = qsl[i].right(qsl[i].size() - (eq+1)).trimmed();
if (!nm.compare("rcldocnum")) {
m_popDoc = atoi(qs2utf8s(value).c_str());
} else {
LOGERR("onPopupJsDone: unknown key: " << qs2utf8s(nm) << "\n");
}
}
}
doCreatePopupMenu();
}
void ResList::createPopupMenu(const QPoint& pos)
{
LOGDEB("ResList::createPopupMenu(" << pos.x() << ", " << pos.y() << ")\n");
m_popDoc = -1;
m_popPos = pos;
#if defined(USING_WEBKIT)
QWebHitTestResult htr = page()->mainFrame()->hitTestContent(pos);
if (htr.isNull())
return;
QWebElement el = htr.enclosingBlockElement();
while (!el.isNull() && !el.hasAttribute("rcldocnum"))
el = el.parent();
if (el.isNull())
return;
QString snum = el.attribute("rcldocnum");
m_popDoc = pageFirstDocNum() + snum.toInt();
#elif defined(USING_WEBENGINE)
QString js("window.locDetails;");
RclWebPage *mypage = dynamic_cast<RclWebPage*>(page());
mypage->runJavaScript(js, [this](const QVariant &v) {onPopupJsDone(v);});
#else
QTextCursor cursor = cursorForPosition(pos);
int blocknum = cursor.blockNumber();
LOGDEB("ResList::createPopupMenu(): block " << blocknum << "\n");
m_popDoc = docnumfromparnum(blocknum);
#endif
doCreatePopupMenu();
}
void ResList::doCreatePopupMenu()
{
if (m_popDoc < 0)
return;
Rcl::Doc doc;
if (!getDoc(m_popDoc, doc))
return;
int options = ResultPopup::showSaveOne;
if (m_ismainres)
options |= ResultPopup::isMain;
QMenu *popup = ResultPopup::create(this, options, m_source, doc);
popup->popup(mapToGlobal(m_popPos));
}
void ResList::menuPreview()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc)) {
if (m_ismainres) {
emit docPreviewClicked(m_popDoc, doc, 0);
} else {
emit previewRequested(doc);
}
}
}
void ResList::menuSaveToFile()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit docSaveToFileClicked(doc);
}
void ResList::menuPreviewParent()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc) && m_source) {
Rcl::Doc pdoc = ResultPopup::getParent(m_source, doc);
if (pdoc.mimetype == "inode/directory") {
emit editRequested(pdoc);
} else {
emit previewRequested(pdoc);
}
}
}
void ResList::menuOpenParent()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc) && m_source)
emit editRequested(ResultPopup::getParent(m_source, doc));
}
void ResList::menuShowSnippets()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit showSnippets(doc);
}
void ResList::menuShowSubDocs()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit showSubDocs(doc);
}
void ResList::menuEdit()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit editRequested(doc);
}
void ResList::menuOpenWith(QAction *act)
{
if (act == 0)
return;
string cmd = qs2utf8s(act->data().toString());
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit openWithRequested(doc, cmd);
}
void ResList::menuCopyFN()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
ResultPopup::copyFN(doc);
}
void ResList::menuCopyURL()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
ResultPopup::copyURL(doc);
}
void ResList::menuExpand()
{
Rcl::Doc doc;
if (getDoc(m_popDoc, doc))
emit docExpand(doc);
}
int ResList::pageFirstDocNum()
{
return m_pager->pageFirstDocNum();
}