00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "find_documentation.h"
00021
00022 #include <stdlib.h>
00023 #include <qprocess.h>
00024 #include <qcheckbox.h>
00025 #include <qheader.h>
00026 #include <qlineedit.h>
00027 #include <qapplication.h>
00028
00029 #include <klistbox.h>
00030
00031 #include <kdevpartcontroller.h>
00032 #include <kdevdocumentationplugin.h>
00033
00034 #include "documentation_widget.h"
00035 #include "documentation_part.h"
00036 #include "indexview.h"
00037 #include "find_documentation_options.h"
00038 #include "docutils.h"
00039 #include "contentsview.h"
00040
00041 FindDocumentation::FindDocumentation(DocumentationWidget* parent, const char* name)
00042 :FindDocumentationBase(parent, name),
00043 man_item(0), info_item(0), index_item(0), google_item(0), contents_item(0),
00044 last_item(0), m_widget(parent)
00045 {
00046 QWidget* tmp = QApplication::desktop();
00047 setGeometry(tmp->width()/2 - width()/2, tmp->height()/2 - height()/2, width(), height());
00048 proc_man = new QProcess( this );
00049 proc_info = new QProcess( this );
00050
00051 connect( proc_man, SIGNAL(processExited()),
00052 this, SLOT(procManReadFromStdout()) );
00053
00054 connect( proc_info, SIGNAL(processExited()),
00055 this, SLOT(procInfoReadFromStdout()) );
00056
00057 result_list->header()->hide();
00058 result_list->setSorting(-1);
00059
00060 m_options = new FindDocumentationOptions(this);
00061 }
00062
00063 FindDocumentation::~FindDocumentation()
00064 {
00065 }
00066
00067
00068 void FindDocumentation::buttonPressedOnItem( int button, QListViewItem * item, const QPoint & pos, int c )
00069 {
00070 if ((button != Qt::RightButton) || (!item))
00071 return;
00072 DocumentationItem *docItem = dynamic_cast<DocumentationItem*>(item);
00073 if (!docItem)
00074 return;
00075
00076 DocUtils::docItemPopup(m_widget->part(), docItem->text(0), docItem->url(), pos, true, false);
00077 }
00078
00079 void FindDocumentation::clickOnItem( QListViewItem * item )
00080 {
00081 if(!item)
00082 return;
00083
00084 if(item == man_item || item == info_item || item == index_item || item == google_item)
00085 return;
00086
00087 DocumentationItem* doc_item = dynamic_cast<DocumentationItem*>(item);
00088
00089 if(item->parent() == man_item ||
00090 item->parent() == info_item ||
00091 item->parent() == google_item ||
00092 item->parent() == index_item ||
00093 item->parent() == contents_item)
00094 m_widget->part()->partController()->showDocument(doc_item->url());
00095 }
00096
00097 void FindDocumentation::procInfoReadFromStdout()
00098 {
00099
00100
00101 if(proc_info->exitStatus() != 0 || !proc_info->normalExit())
00102 {
00103 proc_info->readStdout();
00104 proc_info->readStderr();
00105 }
00106 else
00107 {
00108 QString tmp;
00109 while(!(tmp = proc_info->readLineStdout()).isNull())
00110 {
00111 if(tmp[0] == '*')
00112 break;
00113
00114 DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document, info_item, tmp);
00115 newitem->setURL(KURL("info:/" + search_term->text()));
00116 }
00117 }
00118
00119 if(info_item->firstChild() && m_options->goto_first_match->isOn())
00120 {
00121 m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(info_item->firstChild())->url());
00122 first_match_found = true;
00123 }
00124 }
00125
00126 void FindDocumentation::procManReadFromStdout()
00127 {
00128
00129
00130 if (proc_man->exitStatus() != 0 || !proc_man->normalExit())
00131 {
00132 proc_man->readStdout();
00133 proc_man->readStderr();
00134 }
00135 else
00136 {
00137 QString tmp;
00138 while(!(tmp = proc_man->readLineStdout()).isNull())
00139 {
00140 DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document, man_item, search_term->text());
00141 newitem->setURL(KURL("man://" + tmp));
00142 }
00143 }
00144
00145 if(man_item->firstChild() && m_options->goto_first_match->isOn())
00146 {
00147 m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(man_item->firstChild())->url());
00148 first_match_found = true;
00149 }
00150 }
00151
00152 void FindDocumentation::searchInInfo()
00153 {
00154 info_item = new KListViewItem(result_list, last_item, "Info");
00155 info_item->setOpen(true);
00156 last_item = info_item;
00157
00158 proc_info->clearArguments();
00159
00160 proc_info->addArgument( "info" );
00161 proc_info->addArgument( "-w" );
00162 proc_info->addArgument( search_term->text() );
00163
00164 if ( !proc_info->start() )
00165 {
00166
00167 }
00168 }
00169
00170 void FindDocumentation::searchInMan()
00171 {
00172 man_item = new KListViewItem(result_list, last_item , "Manual");
00173 man_item->setOpen(true);
00174 last_item = man_item;
00175
00176 proc_man->clearArguments();
00177
00178 proc_man->addArgument( "man" );
00179 proc_man->addArgument( "-w" );
00180 proc_man->addArgument( search_term->text() );
00181
00182 if ( !proc_man->start() )
00183 {
00184
00185 }
00186
00187 }
00188
00189 void FindDocumentation::searchInGoogle()
00190 {
00191 google_item = new KListViewItem(result_list, last_item, "Google");
00192 google_item->setOpen(true);
00193 last_item = google_item;
00194
00195 DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document,
00196 google_item, "First result for: " + search_term->text());
00197 newitem->setURL(KURL("http://www.google.com/search?q=" + search_term->text() + "&btnI"));
00198 newitem = new DocumentationItem(DocumentationItem::Document, google_item, "All results for: " + search_term->text());
00199 newitem->setURL(KURL("http://www.google.com/search?q=" + search_term->text()));
00200
00201 if (google_item->firstChild() && m_options->goto_first_match->isOn())
00202 {
00203 m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(google_item->firstChild())->url());
00204 first_match_found = true;
00205 }
00206 }
00207
00208 void FindDocumentation::searchInContents()
00209 {
00210 contents_item = new KListViewItem(result_list, last_item , "Contents");
00211 contents_item->setOpen(true);
00212 last_item = contents_item;
00213
00214 QListViewItemIterator it( m_widget->m_contents->m_view );
00215 while ( it.current() )
00216 {
00217 DocumentationItem *docItem = dynamic_cast<DocumentationItem*>(it.current());
00218
00219 if (docItem->type() == DocumentationItem::Catalog)
00220 {
00221 DocumentationCatalogItem *catalogItem = dynamic_cast<DocumentationCatalogItem*>(it.current());
00222 catalogItem->load();
00223 catalogItem->plugin()->setCatalogURL(catalogItem);
00224 }
00225 if (it.current()->text(0).contains(search_term->text(),false))
00226 {
00227 DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document,
00228 contents_item, it.current()->text(0) );
00229 newitem->setURL(docItem->url());
00230 }
00231 ++it;
00232 }
00233
00234 if (contents_item->firstChild() && m_options->goto_first_match->isOn())
00235 {
00236 m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(contents_item->firstChild())->url());
00237 first_match_found = true;
00238 }
00239 }
00240
00241 void FindDocumentation::searchInIndex()
00242 {
00243 index_item =new KListViewItem(result_list, last_item , "Index");
00244 index_item->setOpen(true);
00245 last_item = index_item;
00246
00247 m_widget->part()->emitIndexSelected(m_widget->m_index->indexBox());
00248 m_widget->m_index->setSearchTerm(search_term->text());
00249 m_widget->m_index->showIndex(search_term->text());
00250
00251 if(m_widget->m_index->m_index->selectedItem())
00252 {
00253 IndexItem* item = dynamic_cast<IndexItem*>(m_widget->m_index->m_index->selectedItem());
00254 DocumentationItem* newitem = 0;
00255 while(item)
00256 {
00257 if(!item->text().contains(search_term->text(),false))
00258 break;
00259
00260 IndexItem::List urls = item->urls();
00261 for (IndexItem::List::const_iterator it = urls.begin(); it != urls.end(); ++it)
00262 {
00263 QString text = item->text();
00264 if (urls.count() > 1)
00265 text = (*it).first;
00266 if(newitem)
00267 newitem = new DocumentationItem(DocumentationItem::Document, index_item,
00268 newitem, text);
00269 else
00270 newitem = new DocumentationItem(DocumentationItem::Document,
00271 index_item, text);
00272
00273 newitem->setURL((*it).second);
00274 }
00275
00276 item = dynamic_cast<IndexItem*>(item->next());
00277 }
00278 }
00279
00280 if(index_item->firstChild() && m_options->goto_first_match->isOn())
00281 {
00282 m_widget->part()->partController()->showDocument(
00283 dynamic_cast<DocumentationItem*>(index_item->firstChild())->url());
00284 first_match_found = true;
00285 }
00286 }
00287
00288 void FindDocumentation::startSearch()
00289 {
00290
00291 result_list->clear();
00292
00293 last_item = NULL;
00294 first_match_found = false;
00295
00296 QListViewItem* item = m_options->source_list->firstChild();
00297
00298 while ( item && ( !m_options->goto_first_match->isOn() || !first_match_found ))
00299 {
00300 if ( m_options->isMan(dynamic_cast<QCheckListItem*>(item)) )
00301 searchInMan();
00302 else if ( m_options->isInfo(dynamic_cast<QCheckListItem*>(item)) )
00303 searchInInfo();
00304 else if ( m_options->isIndex(dynamic_cast<QCheckListItem*>(item)) )
00305 searchInIndex();
00306 else if ( m_options->isGoogle(dynamic_cast<QCheckListItem*>(item)) )
00307 searchInGoogle();
00308 else if ( m_options->isContents(dynamic_cast<QCheckListItem*>(item)) )
00309 searchInContents();
00310
00311 item = item->itemBelow();
00312 }
00313
00314
00315
00316 result_list->setFocus();
00317 if(result_list->firstChild())
00318 result_list->setCurrentItem(result_list->firstChild());
00319
00320 }
00321
00322 void FindDocumentation::setSearchTerm( const QString & term )
00323 {
00324 search_term->setText(term);
00325 }
00326
00327 void FindDocumentation::clickOptions()
00328 {
00329 if( m_options->exec() == QDialog::Accepted )
00330 m_options->writeOptions();
00331 else
00332 m_options->readOptions();
00333 }
00334
00335 void FindDocumentation::focusInEvent( QFocusEvent * e )
00336 {
00337 search_term->setFocus();
00338 }
00339
00340 #include "find_documentation.moc"