docsearchdlg.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "docsearchdlg.h"
00015
00016 #include <qcombobox.h>
00017 #include <qfile.h>
00018 #include <qdir.h>
00019 #include <qhbox.h>
00020 #include <qlayout.h>
00021 #include <qlabel.h>
00022 #include <qlineedit.h>
00023 #include <qpushbutton.h>
00024 #include <qregexp.h>
00025 #include <qtextstream.h>
00026 #include <kapplication.h>
00027 #include <kbuttonbox.h>
00028 #include <kconfig.h>
00029 #include <kdebug.h>
00030 #include <kdialog.h>
00031 #include <klocale.h>
00032 #include <kprocess.h>
00033 #include <kstandarddirs.h>
00034 #include <kmessagebox.h>
00035 #include <kstdguiitem.h>
00036 #include <kdeversion.h>
00037
00038
00039 DocSearchDialog::DocSearchDialog(QWidget *parent, const char *name)
00040 : QDialog(parent, name, true)
00041 {
00042 setCaption(i18n("Search in Documentation"));
00043
00044 QBoxLayout *layout = new QVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
00045
00046 QLabel *searchterm_label = new QLabel(i18n("&Words to search:"), this);
00047 layout->addWidget(searchterm_label);
00048
00049 searchterm_edit = new QLineEdit(this);
00050 searchterm_edit->setFocus();
00051 searchterm_label->setBuddy(searchterm_edit);
00052 QFontMetrics fm(searchterm_edit->fontMetrics());
00053 searchterm_edit->setMinimumWidth(fm.width('X')*30);
00054 layout->addWidget(searchterm_edit);
00055
00056 QHBox *method_box = new QHBox(this);
00057 layout->addSpacing(5);
00058 layout->addWidget(method_box);
00059
00060 QLabel *method_label = new QLabel(i18n("&Method:"), method_box);
00061
00062 method_combo = new QComboBox(method_box);
00063 method_label->setBuddy(method_combo);
00064 method_combo->insertItem(i18n("and"));
00065 method_combo->insertItem(i18n("or"));
00066
00067 QHBox *sort_box = new QHBox(this);
00068 layout->addSpacing(5);
00069 layout->addWidget(sort_box);
00070
00071 QLabel *sort_label = new QLabel(i18n("&Sort"), sort_box);
00072
00073 sort_combo = new QComboBox(sort_box);
00074 sort_label->setBuddy(sort_combo);
00075 sort_combo->insertItem(i18n("Score"));
00076 sort_combo->insertItem(i18n("Title"));
00077 sort_combo->insertItem(i18n("Date"));
00078
00079 QFrame *frame = new QFrame(this);
00080 frame->setFrameStyle(QFrame::HLine | QFrame::Sunken);
00081 layout->addWidget(frame, 0);
00082
00083 KButtonBox *buttonbox = new KButtonBox(this);
00084 buttonbox->addStretch();
00085 #if KDE_IS_VERSION( 3, 2, 90 )
00086 QPushButton *ok_button = buttonbox->addButton(KStdGuiItem::ok());
00087 QPushButton *cancel_button = buttonbox->addButton(KStdGuiItem::cancel());
00088 #else
00089 QPushButton *ok_button = buttonbox->addButton(i18n("&OK"));
00090 QPushButton *cancel_button = buttonbox->addButton(i18n("Cancel"));
00091 #endif
00092 ok_button->setDefault(true);
00093 connect( ok_button, SIGNAL(clicked()), this, SLOT(accept()) );
00094 connect( cancel_button, SIGNAL(clicked()), this, SLOT(reject()) );
00095 buttonbox->layout();
00096 layout->addWidget(buttonbox, 0);
00097 }
00098
00099
00100 DocSearchDialog::~DocSearchDialog()
00101 {}
00102
00103
00104 void qt_enter_modal( QWidget *widget );
00105 void qt_leave_modal( QWidget *widget );
00106
00107
00108 bool DocSearchDialog::performSearch()
00109 {
00110 KConfig config("kdevdoctreeviewrc", true);
00111 config.setGroup("htdig");
00112 QString exe = config.readPathEntry("htsearchbin", kapp->dirs()->findExe("htsearch"));
00113 if (exe.isEmpty()) {
00114
00115 exe = "/usr/lib/cgi-bin/htsearch";
00116 QFile f(exe);
00117 if (!f.exists()) {
00118 KMessageBox::error(this, i18n("Cannot find the htsearch executable."));
00119 kdDebug() << "Can not find htsearch" << endl;
00120 return false;
00121 }
00122 }
00123
00124 QString indexdir = kapp->dirs()->saveLocation("data", "kdevdoctreeview/helpindex");
00125 QDir d;
00126 if(indexdir.isEmpty() || !QFile::exists(indexdir + "/htdig.conf")) {
00127 if(QFile::exists("/var/lib/kdevelop3/helpindex/htdig.conf")) {
00128 indexdir = "/var/lib/kdevelop3/helpindex";
00129 }
00130 else if(QFile::exists("/var/lib/kdevelop/helpindex/htdig.conf")) {
00131 indexdir = "/var/lib/kdevelop/helpindex";
00132 } else {
00133 kdDebug() << "Can not find the htdig configuration file" << endl;
00134 return false;
00135 }
00136 }
00137
00138 QString savedir = kapp->dirs()->saveLocation("data", "kdevdoctreeview/helpindex");
00139 if(!d.exists(savedir)) {
00140 d.mkdir(savedir);
00141 }
00142
00143 QString query = QString("words=%1;method=%2;matchesperpage=%3;format=%4;sort=%5")
00144 .arg(searchterm_edit->text())
00145 .arg(method_combo->currentItem()==1? "or" : "and")
00146 .arg(50)
00147 .arg("builtin-short")
00148 .arg(sort_combo->currentItem()==2? "date" : sort_combo->currentItem()==1? "title" : "score");
00149
00150 kdDebug(9002) << "starting kprocess" << endl;
00151 kdDebug(9002) << "htdig line:" << exe << " -c " << (indexdir + "/htdig.conf ") << query << endl;
00152 KProcess *proc = new KProcess;
00153 *proc << exe << "-c" << (indexdir + "/htdig.conf") << query;
00154
00155 connect( proc, SIGNAL(receivedStdout(KProcess *,char*,int)),
00156 this, SLOT(htsearchStdout(KProcess *,char*,int)) );
00157 connect( proc, SIGNAL(processExited(KProcess *)),
00158 this, SLOT(htsearchExited(KProcess *)) );
00159
00160 searchResult = "";
00161
00162 if (!proc->start(KProcess::NotifyOnExit, KProcess::Stdout)) {
00163 KMessageBox::error(this, i18n("Cannot start the htsearch executable."));
00164 kdDebug() << "process start failed" << endl;
00165 delete proc;
00166 return false;
00167 }
00168
00169
00170
00171
00172 kapp->setOverrideCursor(waitCursor);
00173 QWidget blocker(0, 0, WType_Dialog | WShowModal);
00174 qt_enter_modal(&blocker);
00175 kapp->enter_loop();
00176 qt_leave_modal(&blocker);
00177 kapp->restoreOverrideCursor();
00178
00179 if (!proc->normalExit() || proc->exitStatus() != 0) {
00180 kdDebug() << "Error running htsearch... returning now" << endl;
00181 delete proc;
00182 return false;
00183 }
00184
00185 delete proc;
00186
00187
00188 searchResult = searchResult.replace(QRegExp("http://localhost/"), "file:/");
00189 searchResult = searchResult.replace(QRegExp("Content-type: text/html"), "");
00190
00191
00192 QFile f(savedir + "/results.html");
00193 if (f.open(IO_WriteOnly)) {
00194 QTextStream ts(&f);
00195 ts << searchResult << endl;
00196 f.close();
00197 }
00198
00199 return true;
00200 }
00201
00202
00203 void DocSearchDialog::accept()
00204 {
00205 performSearch();
00206 QDialog::accept();
00207 }
00208
00209
00210 void DocSearchDialog::htsearchStdout(KProcess *, char *buffer, int len)
00211 {
00212 searchResult += QString::fromLocal8Bit(buffer, len);
00213 }
00214
00215
00216 void DocSearchDialog::htsearchExited(KProcess *)
00217 {
00218 kapp->exit_loop();
00219 }
00220 #include "docsearchdlg.moc"
This file is part of the documentation for KDevelop Version 3.1.2.