KDevelop API Documentation

parts/doctreeview/docsearchdlg.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 1999-2001 by Matthias Hoelzer-Kluepfel * 00003 * hoelzer@kde.org * 00004 * Copyright (C) 2001 by Bernd Gehrmann * 00005 * bernd@kdevelop.org * 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 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 00036 00037 DocSearchDialog::DocSearchDialog(QWidget *parent, const char *name) 00038 : QDialog(parent, name, true) 00039 { 00040 setCaption(i18n("Search in Documentation")); 00041 00042 QBoxLayout *layout = new QVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint()); 00043 00044 QLabel *searchterm_label = new QLabel(i18n("&Words to search:"), this); 00045 layout->addWidget(searchterm_label); 00046 00047 searchterm_edit = new QLineEdit(this); 00048 searchterm_edit->setFocus(); 00049 searchterm_label->setBuddy(searchterm_edit); 00050 QFontMetrics fm(searchterm_edit->fontMetrics()); 00051 searchterm_edit->setMinimumWidth(fm.width('X')*30); 00052 layout->addWidget(searchterm_edit); 00053 00054 QHBox *method_box = new QHBox(this); 00055 layout->addSpacing(5); 00056 layout->addWidget(method_box); 00057 00058 QLabel *method_label = new QLabel(i18n("&Method:"), method_box); 00059 00060 method_combo = new QComboBox(method_box); 00061 method_label->setBuddy(method_combo); 00062 method_combo->insertItem(i18n("and")); 00063 method_combo->insertItem(i18n("or")); 00064 00065 QHBox *sort_box = new QHBox(this); 00066 layout->addSpacing(5); 00067 layout->addWidget(sort_box); 00068 00069 QLabel *sort_label = new QLabel(i18n("&Sort"), sort_box); 00070 00071 sort_combo = new QComboBox(sort_box); 00072 sort_label->setBuddy(sort_combo); 00073 sort_combo->insertItem(i18n("Score")); 00074 sort_combo->insertItem(i18n("Title")); 00075 sort_combo->insertItem(i18n("Date")); 00076 00077 QFrame *frame = new QFrame(this); 00078 frame->setFrameStyle(QFrame::HLine | QFrame::Sunken); 00079 layout->addWidget(frame, 0); 00080 00081 KButtonBox *buttonbox = new KButtonBox(this); 00082 buttonbox->addStretch(); 00083 QPushButton *ok_button = buttonbox->addButton(i18n("&OK")); 00084 QPushButton *cancel_button = buttonbox->addButton(i18n("Cancel")); 00085 ok_button->setDefault(true); 00086 connect( ok_button, SIGNAL(clicked()), this, SLOT(accept()) ); 00087 connect( cancel_button, SIGNAL(clicked()), this, SLOT(reject()) ); 00088 buttonbox->layout(); 00089 layout->addWidget(buttonbox, 0); 00090 } 00091 00092 00093 DocSearchDialog::~DocSearchDialog() 00094 {} 00095 00096 00097 void qt_enter_modal( QWidget *widget ); 00098 void qt_leave_modal( QWidget *widget ); 00099 00100 00101 bool DocSearchDialog::performSearch() 00102 { 00103 KConfig config("kdevdoctreeviewrc", true); 00104 config.setGroup("htdig"); 00105 QString exe = config.readPathEntry("htsearchbin", kapp->dirs()->findExe("htsearch")); 00106 if (exe.isEmpty()) { 00107 // Check for htsearch in /usr/lib/cgi-bin (for Debian systems) 00108 exe = "/usr/lib/cgi-bin/htsearch"; 00109 QFile f(exe); 00110 if (!f.exists()) { 00111 KMessageBox::error(this, i18n("Cannot find the htsearch executable!")); 00112 kdDebug() << "Can not find htsearch" << endl; 00113 return false; 00114 } 00115 } 00116 00117 QString indexdir = kapp->dirs()->saveLocation("data", "kdevdoctreeview/helpindex"); 00118 QDir d; 00119 if(indexdir.isEmpty() || !QFile::exists(indexdir + "/htdig.conf")) { 00120 if(QFile::exists("/var/lib/kdevelop3/helpindex/htdig.conf")) { 00121 indexdir = "/var/lib/kdevelop3/helpindex"; 00122 } 00123 else if(QFile::exists("/var/lib/kdevelop/helpindex/htdig.conf")) { 00124 indexdir = "/var/lib/kdevelop/helpindex"; 00125 } else { 00126 kdDebug() << "Can not find the htdig configuration file" << endl; 00127 return false; 00128 } 00129 } 00130 00131 QString savedir = kapp->dirs()->saveLocation("data", "kdevdoctreeview/helpindex"); 00132 if(!d.exists(savedir)) { 00133 d.mkdir(savedir); 00134 } 00135 00136 QString query = QString("words=%1;method=%2;matchesperpage=%3;format=%4;sort=%5") 00137 .arg(searchterm_edit->text()) 00138 .arg(method_combo->currentItem()==1? "or" : "and") 00139 .arg(50) 00140 .arg("builtin-short") 00141 .arg(sort_combo->currentItem()==2? "date" : sort_combo->currentItem()==1? "title" : "score"); 00142 00143 kdDebug(9002) << "starting kprocess" << endl; 00144 kdDebug(9002) << "htdig line:" << exe << " -c " << (indexdir + "/htdig.conf ") << query << endl; 00145 KProcess *proc = new KProcess; 00146 *proc << exe << "-c" << (indexdir + "/htdig.conf") << query; 00147 00148 connect( proc, SIGNAL(receivedStdout(KProcess *,char*,int)), 00149 this, SLOT(htsearchStdout(KProcess *,char*,int)) ); 00150 connect( proc, SIGNAL(processExited(KProcess *)), 00151 this, SLOT(htsearchExited(KProcess *)) ); 00152 00153 searchResult = ""; 00154 00155 if (!proc->start(KProcess::NotifyOnExit, KProcess::Stdout)) { 00156 KMessageBox::error(this, i18n("Cannot start the htsearch executable!")); 00157 kdDebug() << "process start failed" << endl; 00158 delete proc; 00159 return false; 00160 } 00161 00162 // While receiving data from the subprocess, we want 00163 // to block the user interface, but still get repaint 00164 // events. Hack taken from NetAccess... 00165 kapp->setOverrideCursor(waitCursor); 00166 QWidget blocker(0, 0, WType_Dialog | WShowModal); 00167 qt_enter_modal(&blocker); 00168 kapp->enter_loop(); 00169 qt_leave_modal(&blocker); 00170 kapp->restoreOverrideCursor(); 00171 00172 if (!proc->normalExit() || proc->exitStatus() != 0) { 00173 kdDebug() << "Error running htsearch... returning now" << endl; 00174 delete proc; 00175 return false; 00176 } 00177 00178 delete proc; 00179 00180 // modify the search result 00181 searchResult = searchResult.replace(QRegExp("http://localhost/"), "file:/"); 00182 searchResult = searchResult.replace(QRegExp("Content-type: text/html"), ""); 00183 00184 // dump the search result 00185 QFile f(savedir + "/results.html"); 00186 if (f.open(IO_WriteOnly)) { 00187 QTextStream ts(&f); 00188 ts << searchResult << endl; 00189 f.close(); 00190 } 00191 00192 return true; 00193 } 00194 00195 00196 void DocSearchDialog::accept() 00197 { 00198 performSearch(); 00199 QDialog::accept(); 00200 } 00201 00202 00203 void DocSearchDialog::htsearchStdout(KProcess *, char *buffer, int len) 00204 { 00205 searchResult += QString::fromLocal8Bit(buffer, len); 00206 } 00207 00208 00209 void DocSearchDialog::htsearchExited(KProcess *) 00210 { 00211 kapp->exit_loop(); 00212 } 00213 #include "docsearchdlg.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Oct 19 08:01:50 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003