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
|
/***************************************************************************
* Copyright (C) 1999-2001 by Matthias Hoelzer-Kluepfel *
* hoelzer@kde.org *
* Copyright (C) 2001 by Bernd Gehrmann *
* bernd@kdevelop.org *
* Copyright (C) 2004 by Alexander Dymo *
* cloudtemple@mksat.net *
* *
* 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "searchview.h"
#include <qlayout.h>
#include <qlabel.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qdir.h>
#include <qregexp.h>
#include <kpushbutton.h>
#include <klistview.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kdialog.h>
#include <kprocess.h>
#include <kapplication.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kdevpartcontroller.h>
#include <kdevdocumentationplugin.h>
#include "documentation_part.h"
#include "docutils.h"
SearchView::SearchView(DocumentationPart *part, QWidget *parent, const char *name)
:QWidget(parent, name), m_part(part)
{
QVBoxLayout *l = new QVBoxLayout(this, 0, KDialog::spacingHint());
QVBoxLayout *l2 = new QVBoxLayout(l, 0);
QLabel *editLabel = new QLabel(i18n("Wor&ds to search:"), this);
l2->addWidget(editLabel);
QHBoxLayout *l21 = new QHBoxLayout(l2, 0);
m_edit = new KLineEdit(this);
editLabel->setBuddy(m_edit);
m_goSearchButton = new KPushButton(i18n("Se&arch"), this);
l21->addWidget(m_edit);
l21->addWidget(m_goSearchButton);
QGridLayout *l3 = new QGridLayout(l, 2, 2, 0);
m_searchMethodBox = new KComboBox(this);
m_searchMethodBox->insertItem(i18n("and"));
m_searchMethodBox->insertItem(i18n("or"));
QLabel *smLabel = new QLabel(m_searchMethodBox, i18n("&Method:"), this);
m_sortMethodBox = new KComboBox(this);
m_sortMethodBox->insertItem(i18n("Score"));
m_sortMethodBox->insertItem(i18n("Title"));
m_sortMethodBox->insertItem(i18n("Date"));
QLabel *rmLabel = new QLabel(m_sortMethodBox, i18n("S&ort by:"), this);
l3->addWidget(smLabel, 0, 0);
l3->addWidget(m_searchMethodBox, 0, 1);
l3->addWidget(rmLabel, 1, 0);
l3->addWidget(m_sortMethodBox, 1, 1);
QVBoxLayout *l4 = new QVBoxLayout(l, 0);
m_view = new KListView(this);
QLabel *vLabel = new QLabel(m_view, i18n("Search &results:"), this);
l4->addWidget(vLabel);
l4->addWidget(m_view);
QHBoxLayout *l5 = new QHBoxLayout(l, KDialog::spacingHint());
m_configButton = new KPushButton(i18n("Update Config"), this);
m_indexButton = new KPushButton(i18n("Update Index"), this);
l5->addWidget(m_configButton);
l5->addWidget(m_indexButton);
l5->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed));
l->addSpacing(2);
m_view->setSorting(-1);
m_view->addColumn(i18n("Relevance"));
m_view->addColumn(i18n("Title"));
m_view->setColumnWidthMode(0, QListView::Maximum);
m_view->setColumnWidthMode(1, QListView::Maximum);
m_view->setAllColumnsShowFocus(true);
m_view->setResizeMode( QListView::LastColumn );
connect(m_configButton, SIGNAL(clicked()), this, SLOT(updateConfig()));
connect(m_indexButton, SIGNAL(clicked()), this, SLOT(updateIndex()));
connect(m_edit, SIGNAL(returnPressed()), this, SLOT(search()));
connect(m_goSearchButton, SIGNAL(clicked()), this, SLOT(search()));
connect(m_view, SIGNAL(executed(QListViewItem*)), this, SLOT(executed(QListViewItem*)));
connect(m_view, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int )),
this, SLOT(itemMouseButtonPressed(int, QListViewItem*, const QPoint&, int )));
}
SearchView::~SearchView()
{
}
void SearchView::updateConfig()
{
runHtdig("-c");
}
void SearchView::updateIndex()
{
runHtdig("-i");
KConfig *config = m_part->config();
config->setGroup("htdig");
config->writeEntry("IsSetup", true);
config->sync();
}
void SearchView::runHtdig(const QString &arg)
{
KProcess proc;
proc << "kdevelop-htdig" << arg;
proc.start(KProcess::DontCare);
}
void qt_enter_modal(QWidget *widget);
void qt_leave_modal(QWidget *widget);
void SearchView::search()
{
KConfig *config = m_part->config();
config->setGroup("htdig");
if (config->readBoolEntry("IsSetup", false) == false)
{
KMessageBox::information(this, i18n("Full text search has to be set up before usage."));
if (!m_part->configure(1))
return;
KMessageBox::information(this, i18n("Now the full text search database will be created.\nWait for database creation to finish and then repeat search."));
updateIndex();
return;
}
QString exe = config->readPathEntry("htsearchbin", kapp->dirs()->findExe("htsearch"));
if (exe.isEmpty())
{
KMessageBox::error(this, i18n("Cannot find the htsearch executable.\nIt is part of the ht://Dig package that is used by KDevelop to perform full text search. Please install ht://Dig and use Documentation page in Configure KDevelop dialog to set the htsearch location."));
kdDebug() << "Can not find htsearch" << endl;
return;
}
QString indexdir = kapp->dirs()->saveLocation("data", "kdevdocumentation/search");
QDir d;
if (indexdir.isEmpty() || !QFile::exists(indexdir + "/htdig.conf"))
{
if (QFile::exists("/var/lib/kdevelop3/helpindex/htdig.conf"))
indexdir = "/var/lib/kdevelop3/helpindex";
else if (QFile::exists("/var/lib/kdevelop/helpindex/htdig.conf"))
indexdir = "/var/lib/kdevelop/helpindex";
if (!QFile::exists(indexdir + "/htdig.conf"))
{
KMessageBox::error(this, i18n("Cannot find the htdig configuration file."));
kdDebug() << "Cannot find the htdig configuration file" << endl;
return;
}
}
QString savedir = kapp->dirs()->saveLocation("data", "kdevdocumentation/search");
if (!d.exists(savedir))
d.mkdir(savedir);
QString query = QString("words=%1;method=%2;matchesperpage=%3;format=%4;sort=%5")
.arg(m_edit->text())
.arg(m_searchMethodBox->currentItem()==1? "or" : "and")
.arg(50)
.arg("builtin-short")
.arg(m_sortMethodBox->currentItem()==2? "date" : m_sortMethodBox->currentItem()==1? "title" : "score");
kdDebug(9002) << "starting kprocess" << endl;
kdDebug(9002) << "htdig line:" << exe << " -c " << (indexdir + "/htdig.conf ") << query << endl;
KProcess *proc = new KProcess;
QString picdir = kapp->dirs()->findResourceDir("data", "kdevdocumentation/pics/htdig.png");
proc->setEnvironment("PICDIR", picdir);
*proc << exe << "-c" << (indexdir + "/htdig.conf") << query;
connect( proc, SIGNAL(receivedStdout(KProcess *,char*,int)),
this, SLOT(htsearchStdout(KProcess *,char*,int)) );
connect( proc, SIGNAL(processExited(KProcess *)),
this, SLOT(htsearchExited(KProcess *)) );
searchResult = "";
if (!proc->start(KProcess::NotifyOnExit, KProcess::Stdout))
{
KMessageBox::error(this, i18n("Cannot start the htsearch executable."));
kdDebug() << "process start failed" << endl;
delete proc;
return;
}
// While receiving data from the subprocess, we want
// to block the user interface, but still get repaint
// events. Hack taken from NetAccess...
kapp->setOverrideCursor(waitCursor);
QWidget blocker(0, 0, WType_Dialog | WShowModal);
qt_enter_modal(&blocker);
kapp->enter_loop();
qt_leave_modal(&blocker);
kapp->restoreOverrideCursor();
if (!proc->normalExit() || proc->exitStatus() != 0)
{
kdDebug() << "Error running htsearch... returning now" << endl;
delete proc;
return;
}
delete proc;
// modify the search result
searchResult = searchResult.replace(QRegExp("http://localhost/"), "file:/");
searchResult = searchResult.replace(QRegExp("Content-type: text/html"), "");
// dump the search result
QFile f(savedir + "/results.html");
if (f.open(IO_WriteOnly))
{
QTextStream ts(&f);
ts << searchResult << endl;
f.close();
}
//show results
analyseSearchResults();
// m_part->partController()->showDocument(KURL("file://" + indexdir + "/results.html"));
}
void SearchView::htsearchStdout(KProcess *, char *buffer, int len)
{
searchResult += QString::fromLocal8Bit(buffer, len);
}
void SearchView::htsearchExited(KProcess *)
{
kapp->exit_loop();
}
void SearchView::analyseSearchResults()
{
m_view->clear();
QTextStream str(searchResult, IO_ReadOnly);
DocumentationItem *former = 0;
while (!str.eof())
{
QString line = str.readLine();
QRegExp starsExp("alt=\"\\*\"");
starsExp.setMinimal(true);
int stars = line.contains(starsExp);
QRegExp urlExp("<strong><a href=\"(.*)\">(.*)</a></strong>");
if (urlExp.search(line)==-1)
continue;
QString url = urlExp.cap(1);
QString title = urlExp.cap(2);
QString starsStr;
for (int i = 0; i < stars; ++i)
starsStr += "*";
if (former)
former = new DocumentationItem(DocumentationItem::Document, m_view, former, starsStr);
else
former = new DocumentationItem(DocumentationItem::Document, m_view, starsStr);
former->setText(1, title);
former->setURL(KURL(url));
}
executed( m_view->firstChild() );
}
void SearchView::executed(QListViewItem *item)
{
DocumentationItem *d = dynamic_cast<DocumentationItem*>(item);
if (!d)
return;
m_part->partController()->showDocument(d->url());
}
void SearchView::itemMouseButtonPressed(int button, QListViewItem *item, const QPoint &pos, int // c
)
{
if ((button != Qt::RightButton) || (!item))
return;
DocumentationItem *docItem = dynamic_cast<DocumentationItem*>(item);
if (!docItem)
return;
DocUtils::docItemPopup(m_part, docItem, pos, true, false, 1);
}
void SearchView::setSearchTerm(const QString &term)
{
m_edit->setText(term);
}
void SearchView::askSearchTerm()
{
m_edit->setFocus();
}
void SearchView::focusInEvent(QFocusEvent */*e*/)
{
m_edit->setFocus();
}
#include "searchview.moc"
|