some debug messages removed
[kdevelop:kdevelop.git] / kdevelop / setup / ccreatedocdatabasedlg.cpp
1 /***************************************************************************
2                           ccreatedocdatabasedlg.cpp  -  description
3                              -------------------
4
5     begin                : Sat Jan 9 1999
6     copyright            : (C) 1999 by Sandy Meier
7     email                : smeier@rz.uni-potsdam.de
8  ***************************************************************************/
9
10 /***************************************************************************
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  ***************************************************************************/
18
19 #include <qdir.h>
20 #include <qwhatsthis.h>
21 #include <qwidget.h>
22 #include <qcheckbox.h>
23 #include <qpushbutton.h>
24 #include <qlabel.h>
25 #include <qlineedit.h>
26 #include <qlistbox.h>
27 #include <qradiobutton.h>
28 #include <qbuttongroup.h>
29 #include <qlayout.h>
30 #include <qgrid.h>
31 #include <qmultilineedit.h>
32 #include <qapplication.h>
33
34 #include <kmessagebox.h>
35 #include <kfiledialog.h>
36 //#include <kapp.h>
37 #include <klocale.h>
38 #include <kstddirs.h>
39 #include <kprocess.h>
40 #include <kbuttonbox.h>
41 //#include <kconfig.h>
42
43 #include "ccreatedocdatabasedlg.h"
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 //#include <iostream.h>
50
51 CCreateDocDatabaseDlg::CCreateDocDatabaseDlg(QWidget *parent, const char *name,KShellProcess* proc,const QString& kdeDocDir, const QString& qtDocDir, bool foundGlimpse,bool foundHtDig, bool bShowIndexingButton) : QWidget(parent,name)
52         ,start_button(0L)
53   ,m_proc(proc)
54   ,m_pShellProcessOutput(0L)
55   ,m_pShellProcessOutputLines(0L)
56   ,m_pShellProcessOutputOKButton(0L)
57 {
58   m_kdeDocDir = kdeDocDir;
59   m_qtDocDir = qtDocDir;
60
61   QGridLayout *grid2 = new QGridLayout(this,2,3,0,7);
62
63   //-----search engine group-----
64   QButtonGroup* searcheng_ButtonGroup;
65   searcheng_ButtonGroup = new QButtonGroup( this, "ButtonGroup_0" );
66   searcheng_ButtonGroup->setTitle( i18n("Index engine") );
67
68   useGlimpse = new QRadioButton( i18n("Glimpse"), searcheng_ButtonGroup );
69   if (foundGlimpse)
70     useGlimpse->setChecked( true );
71   else
72     useGlimpse->setEnabled( false );
73
74   useHtDig = new QRadioButton( i18n("ht://Dig"), searcheng_ButtonGroup );
75   if (foundGlimpse)
76     useHtDig->setChecked( false );
77   else if (foundHtDig)
78     useHtDig->setChecked( true );
79
80   QGridLayout *grid1 = new QGridLayout(searcheng_ButtonGroup,3,1,15,7);
81   grid1->addWidget(useGlimpse,0,0);
82   grid1->addWidget(useHtDig,1,0);
83   grid2->addWidget(searcheng_ButtonGroup, 0, 0);
84
85   // ------- index size group --------
86   QButtonGroup* qtarch_ButtonGroup_1;
87   qtarch_ButtonGroup_1 = new QButtonGroup( this, "ButtonGroup_1" );
88   qtarch_ButtonGroup_1->setTitle( i18n("Index Size") );
89
90   tiny_radio_button = new QRadioButton(  qtarch_ButtonGroup_1, "RadioButton_2" );
91   tiny_radio_button->setText(i18n("tiny size") );
92   tiny_radio_button->setChecked( TRUE );
93
94   small_radio_button = new QRadioButton( qtarch_ButtonGroup_1, "RadioButton_3" );
95   small_radio_button->setText(i18n("small size") );
96
97   medium_radio_button = new QRadioButton( qtarch_ButtonGroup_1, "RadioButton_4" );
98   medium_radio_button->setText(i18n("medium size") );
99
100   grid1 = new QGridLayout(qtarch_ButtonGroup_1,3,1,15,7);
101   grid1->addWidget(tiny_radio_button,0,0);
102   grid1->addWidget(small_radio_button,1,0);
103   grid1->addWidget(medium_radio_button,2,0);
104   grid2->addWidget(qtarch_ButtonGroup_1, 0, 1);
105
106   // ------- index options group --------
107   QWidget* wdg = new QWidget(this);
108   QButtonGroup* qtarch_ButtonGroup_3;
109   qtarch_ButtonGroup_3 = new QButtonGroup( wdg, "ButtonGroup_3" );
110   qtarch_ButtonGroup_3->setTitle( i18n("Index Options") );
111
112   qt_checkbox = new QCheckBox( qtarch_ButtonGroup_3, "CheckBox_3" );
113   qt_checkbox->setText( i18n("index the QT documentation") );
114   qt_checkbox->setChecked( TRUE );
115
116   kde_checkbox = new QCheckBox( qtarch_ButtonGroup_3, "CheckBox_1" );
117   kde_checkbox->setText(i18n("index the KDE documentation") );
118   kde_checkbox->setChecked( TRUE );
119
120         if (bShowIndexingButton) {
121           start_button  = new QPushButton(i18n("Start indexing"), wdg);
122         start_button->setDefault(true);
123         }
124
125   grid1 = new QGridLayout(qtarch_ButtonGroup_3,2,1,15,7);
126   grid1->addWidget(qt_checkbox,0,0);
127   grid1->addWidget(kde_checkbox,1,0);
128   QVBoxLayout* vl = new QVBoxLayout(wdg,0,7);
129   vl->addWidget(qtarch_ButtonGroup_3);
130         if (bShowIndexingButton)
131           vl->addWidget(start_button);
132   grid2->addWidget(wdg, 0, 2);
133
134   // ------- additional dirs group --------------
135   QButtonGroup* qtarch_ButtonGroup_2;
136   qtarch_ButtonGroup_2 = new QButtonGroup( this, "ButtonGroup_2" );
137   qtarch_ButtonGroup_2->setTitle(i18n("additional directories to index"));
138
139   dir_edit = new QLineEdit( qtarch_ButtonGroup_2, "LineEdit_1" );
140   dir_edit->setText( "" );
141   dir_edit->setEchoMode( QLineEdit::Normal );
142   dir_edit->setFrame( TRUE );
143
144   dir_button = new QPushButton( qtarch_ButtonGroup_2, "PushButton_3" );
145   QPixmap pix = SmallIcon("fileopen");
146   dir_button->setPixmap(pix);
147
148   dir_listbox = new QListBox(  qtarch_ButtonGroup_2, "ListBox_1" );
149   dir_listbox->setMultiSelection( FALSE );
150
151   add_button = new QPushButton(i18n("add"), qtarch_ButtonGroup_2);
152   remove_button = new QPushButton(i18n("remove"), qtarch_ButtonGroup_2);
153
154   grid1 = new QGridLayout(qtarch_ButtonGroup_2,3,2,15,7);
155   grid1->addWidget(dir_edit,0,0);
156   grid1->addWidget(dir_button,0,1);
157   grid1->addWidget(add_button,1,1);
158   grid1->addWidget(remove_button,2,1);
159   grid1->addMultiCellWidget(dir_listbox,1,2,0,0);
160   grid2->addMultiCellWidget(qtarch_ButtonGroup_2,1,1,0,2);
161
162   resize(sizeHint());
163
164  /*****************Connections******************/
165  if (bShowIndexingButton)
166          connect(start_button,SIGNAL(clicked()),SLOT(slotOkClicked()));
167  connect(add_button,SIGNAL(clicked()),SLOT(slotAddButtonClicked()));
168  connect(remove_button,SIGNAL(clicked()),SLOT(slotRemoveButtonClicked()));
169  connect(dir_button,SIGNAL(clicked()),SLOT(slotDirButtonClicked()));
170
171  dir_edit->setFocus();
172
173  /*doc*/
174  QWhatsThis::add(medium_radio_button,
175      i18n("builds a medium-size index (20-30% of the size\n"
176           "of all files), allowing faster search."));
177  QWhatsThis::add(small_radio_button,
178      i18n("Build a small index rather than tiny (meaning 7-9%\n"
179           "of the sizes of all files - your mileage may vary)\n"
180           "allowing faster search."));
181  QWhatsThis::add(tiny_radio_button,
182      i18n("a tiny index (2-3% of the total size of all files)"));
183
184   createShellProcessOutputWidget();
185 }
186
187 CCreateDocDatabaseDlg::~CCreateDocDatabaseDlg()
188 {
189 }
190
191 void CCreateDocDatabaseDlg::slotOkClicked()
192 {
193   if(!QFile::exists(m_kdeDocDir + "/kdeui/KDialog.html") && kde_checkbox->isChecked()){
194     KMessageBox::error(0,i18n("The KDE-Documentation-Path isn't set correctly."),i18n("No Database created!"));
195     return;
196   }
197   if(!QFile::exists(m_qtDocDir + "/qtabbar.html") && qt_checkbox->isChecked()){
198     KMessageBox::error(0,i18n("The Qt-Documentation-Path isn't set correctly."),i18n("No Database created!"));
199     return;
200   }
201
202   QDir dir(locateLocal("data", ""));
203   dir.mkdir("kdevelop");
204
205   QString dirs;
206   if(kde_checkbox->isChecked()){
207     dirs = dirs + m_kdeDocDir;
208   }
209   if(qt_checkbox->isChecked()){
210     dirs = dirs + " " +  m_qtDocDir;
211   }
212   // added for documentation search in the kdevelop html directory
213   dirs= dirs + " "+ locate("html", "default/kdevelop");
214
215   uint count = dir_listbox->count();
216   uint i;
217   for(i=0;i<count;i++){
218     dirs = dirs + " ";
219     dirs = dirs + dir_listbox->text(i);
220   }
221   
222   QString size_str;
223   if (small_radio_button->isChecked()){
224     size_str = " -o ";
225   }
226   else if (medium_radio_button->isChecked()){
227     size_str = " -b ";
228   }
229   
230   m_proc->clearArguments();
231   if (useGlimpse->isChecked())
232   {
233     *m_proc <<  "find "+ dirs +" -name '*.html' | glimpseindex " +
234                     size_str +" -F -X -H "+ locateLocal("appdata","");
235     m_proc->start(KShellProcess::NotifyOnExit,KShellProcess::AllOutput);
236   }
237   else if (useHtDig->isChecked())
238   {
239     QString htdigConf = locate("appdata", "tools/htdig.conf");
240     if (!htdigConf.isEmpty()) {
241       *m_proc <<  "find " +
242                 dirs +
243                 " -name '*.html' | awk 'OFS=\"\"; {print \"file://localhost\", $0}' | htdig -v -s -c " +
244                 htdigConf + " - ; htmerge -v -s -c " + htdigConf;
245       m_proc->start(KShellProcess::NotifyOnExit,KShellProcess::AllOutput);
246       slotShowToolProcessOutputDlg();
247     }
248     else {
249       KMessageBox::sorry(this, i18n("Couldn't find a htdig.conf"), i18n("htdig Execution Error"));
250     }
251   }
252 }
253
254 void CCreateDocDatabaseDlg::slotAddButtonClicked()
255 {
256   QString str = dir_edit->text();
257
258   if(str != "" ){
259     dir_listbox->insertItem(str,0);
260     dir_edit->clear();
261   }
262   
263 }
264
265 void CCreateDocDatabaseDlg::slotRemoveButtonClicked()
266 {
267   dir_listbox->removeItem(dir_listbox->currentItem());
268   
269 }
270
271 void CCreateDocDatabaseDlg::slotDirButtonClicked()
272 {
273   QString name=KFileDialog::getExistingDirectory(dir_edit->text(),0,i18n("Select Directory..."));
274   if(!name.isEmpty()){
275     dir_edit->setText(name);
276   }
277 }
278
279 void CCreateDocDatabaseDlg::createShellProcessOutputWidget()
280 {
281   m_pShellProcessOutput = new QDialog(this, "shell_process_output_dlg", true);
282   m_pShellProcessOutput->setCaption(i18n("Creating the KDE Documentation"));
283   QVBoxLayout* pVL = new QVBoxLayout(m_pShellProcessOutput, 15, 7);
284   QLabel* pLabel = new QLabel(i18n("Wait until the process has finished:"), m_pShellProcessOutput);
285   m_pShellProcessOutputLines = new QMultiLineEdit(m_pShellProcessOutput);
286   KButtonBox *bb = new KButtonBox( m_pShellProcessOutput );
287   bb->addStretch();
288   m_pShellProcessOutputOKButton =bb->addButton( i18n("&OK") );
289   m_pShellProcessOutputOKButton->setDefault( true );
290   bb->addStretch();
291
292   pVL->addWidget(pLabel);
293   pVL->addWidget(m_pShellProcessOutputLines);
294   pVL->addWidget(bb);
295
296   m_pShellProcessOutput->resize(500, 400);
297
298   QObject::connect(m_pShellProcessOutputOKButton, SIGNAL(clicked()), m_pShellProcessOutput, SLOT(accept()));
299   QObject::connect(m_proc,SIGNAL(receivedStdout(KProcess*,char*,int)), this, SLOT(slotReceivedStdout(KProcess*,char*,int)) );
300   QObject::connect(m_proc,SIGNAL(receivedStderr(KProcess*,char*,int)), this, SLOT(slotReceivedStderr(KProcess*,char*,int)) );
301   QObject::connect(m_proc,SIGNAL(processExited(KProcess*)), this, SLOT(slotProcessExited(KProcess*) )) ;
302 }
303
304 void CCreateDocDatabaseDlg::slotShowToolProcessOutputDlg()
305 {
306   m_pShellProcessOutputOKButton->setEnabled(false);
307   m_pShellProcessOutputLines->clear();
308   QApplication::setOverrideCursor(WaitCursor);
309   emit indexingStartedNow();
310         if (start_button)
311           start_button->setEnabled(false);
312   m_pShellProcessOutput->exec();
313 }
314
315 void CCreateDocDatabaseDlg::slotReceivedStdout(KProcess*,char* buffer,int count)
316 {
317   QCString test(buffer, count);
318   m_pShellProcessOutputLines->insertLine(test);
319   m_pShellProcessOutputLines->setCursorPosition(m_pShellProcessOutputLines->numLines(), 0);
320 }
321
322 void CCreateDocDatabaseDlg::slotReceivedStderr(KProcess*,char* buffer, int count)
323 {
324   QCString test(buffer, count);
325   m_pShellProcessOutputLines->insertLine(test);
326   m_pShellProcessOutputLines->setCursorPosition(m_pShellProcessOutputLines->numLines(), 0);
327 }
328
329 void CCreateDocDatabaseDlg::slotProcessExited(KProcess*)
330 {
331   m_pShellProcessOutputLines->insertLine("");
332   m_pShellProcessOutputLines->insertLine("Finished!");
333   m_pShellProcessOutputLines->setCursorPosition(m_pShellProcessOutputLines->numLines(), 0);
334   m_pShellProcessOutputOKButton->setEnabled(true);
335         if (start_button)
336           start_button->setEnabled(true);
337   QApplication::restoreOverrideCursor();
338
339   if (useGlimpse->isChecked())
340     emit indexingFinished("glimpse");
341   else
342     emit indexingFinished("htdig");
343 }
344
345 #include "ccreatedocdatabasedlg.moc"