summaryrefslogtreecommitdiffstats
path: root/khelpcenter/htmlsearch/kcmhtmlsearch.cpp
blob: e0ef62dd444c08c5ca80d06406ac37faa2fb5a7a (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
 *  kcmhtmlsearch.cpp
 *
 *  Copyright (c) 2000 Matthias H�lzer-Kl�pfel <[email protected]>
 *
 *  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 <tqlayout.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kurllabel.h>
#include <kapplication.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tdefiledialog.h>
#include <kprocess.h>
#include "klangcombo.h"
#include <kurlrequester.h>
#include <klineedit.h>

#include "kcmhtmlsearch.moc"


TDEHTMLSearchConfig::TDEHTMLSearchConfig(TQWidget *parent, const char *name)
  : TDECModule(parent, name), indexProc(0)
{
  TQVBoxLayout *vbox = new TQVBoxLayout(this, 5);


  TQGroupBox *gb = new TQGroupBox(i18n("ht://dig"), this);
  vbox->addWidget(gb);

  TQGridLayout *grid = new TQGridLayout(gb, 3,2, 6,6);

  grid->addRowSpacing(0, gb->fontMetrics().lineSpacing());

  TQLabel *l = new TQLabel(i18n("The fulltext search feature makes use of the "
                  "ht://dig HTML search engine. "
                  "You can get ht://dig at the"), gb);
  l->setAlignment(TQLabel::WordBreak);
  l->setMinimumSize(l->sizeHint());
  grid->addMultiCellWidget(l, 1, 1, 0, 1);
  TQWhatsThis::add( gb, i18n( "Information about where to get the ht://dig package." ) );

  KURLLabel *url = new KURLLabel(gb);
  url->setURL("http://www.htdig.org");
  url->setText(i18n("ht://dig home page"));
  url->setAlignment(TQLabel::AlignHCenter);
  grid->addMultiCellWidget(url, 2,2, 0, 1);
  connect(url, TQT_SIGNAL(leftClickedURL(const TQString&)),
      this, TQT_SLOT(urlClicked(const TQString&)));

  gb = new TQGroupBox(i18n("Program Locations"), this);

  vbox->addWidget(gb);
  grid = new TQGridLayout(gb, 4,2, 6,6);
  grid->addRowSpacing(0, gb->fontMetrics().lineSpacing());

  htdigBin = new KURLRequester(gb);
  l = new TQLabel(htdigBin, i18n("ht&dig"), gb);
  l->setBuddy( htdigBin );
  grid->addWidget(l, 1,0);
  grid->addWidget(htdigBin, 1,1);
  connect(htdigBin->lineEdit(), TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(configChanged()));
  TQString wtstr = i18n( "Enter the path to your htdig program here, e.g. /usr/local/bin/htdig" );
  TQWhatsThis::add( htdigBin, wtstr );
  TQWhatsThis::add( l, wtstr );

  htsearchBin = new KURLRequester(gb);
  l = new TQLabel(htsearchBin, i18n("ht&search"), gb);
  l->setBuddy( htsearchBin );
  grid->addWidget(l, 2,0);
  grid->addWidget(htsearchBin, 2,1);
  connect(htsearchBin->lineEdit(), TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(configChanged()));
  wtstr = i18n( "Enter the path to your htsearch program here, e.g. /usr/local/bin/htsearch" );
  TQWhatsThis::add( htsearchBin, wtstr );
  TQWhatsThis::add( l, wtstr );

  htmergeBin = new KURLRequester(gb);
  l = new TQLabel(htmergeBin, i18n("ht&merge"), gb);
  l->setBuddy( htmergeBin );
  grid->addWidget(l, 3,0);
  grid->addWidget(htmergeBin, 3,1);
  connect(htmergeBin->lineEdit(), TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(configChanged()));
  wtstr = i18n( "Enter the path to your htmerge program here, e.g. /usr/local/bin/htmerge" );
  TQWhatsThis::add( htmergeBin, wtstr );
  TQWhatsThis::add( l, wtstr );

  TQHBoxLayout *hbox = new TQHBoxLayout(vbox);

  gb = new TQGroupBox(i18n("Scope"), this);
  hbox->addWidget(gb);
  TQWhatsThis::add( gb, i18n( "Here you can select which parts of the documentation should be included in the fulltext search index. Available options are the TDE Help pages, the installed man pages, and the installed info pages. You can select any number of these." ) );

  TQVBoxLayout *vvbox = new TQVBoxLayout(gb, 6,2);
  vvbox->addSpacing(gb->fontMetrics().lineSpacing());

  indexKDE = new TQCheckBox(i18n("&TDE help"), gb);
  vvbox->addWidget(indexKDE);
  connect(indexKDE, TQT_SIGNAL(clicked()), this, TQT_SLOT(configChanged()));

  indexMan = new TQCheckBox(i18n("&Man pages"), gb);
  vvbox->addWidget(indexMan);
  indexMan->setEnabled(false),
  connect(indexMan, TQT_SIGNAL(clicked()), this, TQT_SLOT(configChanged()));

  indexInfo = new TQCheckBox(i18n("&Info pages"), gb);
  vvbox->addWidget(indexInfo);
  indexInfo->setEnabled(false);
  connect(indexInfo, TQT_SIGNAL(clicked()), this, TQT_SLOT(configChanged()));

  gb = new TQGroupBox(i18n("Additional Search Paths"), this);
  hbox->addWidget(gb);
  TQWhatsThis::add( gb, i18n( "Here you can add additional paths to search for documentation. To add a path, click on the <em>Add...</em> button and select the folder from where additional documentation should be searched. You can remove folders by clicking on the <em>Delete</em> button." ) );

  grid = new TQGridLayout(gb, 4,3, 6,2);
  grid->addRowSpacing(0, gb->fontMetrics().lineSpacing());

  addButton = new TQPushButton(i18n("Add..."), gb);
  grid->addWidget(addButton, 1,0);

  delButton = new TQPushButton(i18n("Delete"), gb);
  grid->addWidget(delButton, 2,0);

  searchPaths = new KListBox(gb);
  grid->addMultiCellWidget(searchPaths, 1,3, 1,1);
  grid->setRowStretch(2,2);

  gb = new TQGroupBox(i18n("Language Settings"), this);
  vbox->addWidget(gb);
  TQWhatsThis::add(gb, i18n("Here you can select the language you want to create the index for."));
  language = new KLanguageCombo(gb);
  l = new TQLabel(language, i18n("&Language"), gb);
  vvbox = new TQVBoxLayout(gb, 6,2);
  vvbox->addSpacing(gb->fontMetrics().lineSpacing());
  hbox = new TQHBoxLayout(vvbox, 6);
  hbox->addWidget(l);
  hbox->addWidget(language,1);
  hbox->addStretch(1);

  loadLanguages();

  vbox->addStretch(1);

  runButton = new TQPushButton(i18n("Generate Index..."), this);
  TQWhatsThis::add( runButton, i18n( "Click this button to generate the index for the fulltext search." ) );
  runButton->setFixedSize(runButton->sizeHint());
  vbox->addWidget(runButton, AlignRight);
  connect(runButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(generateIndex()));

  connect(addButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addClicked()));
  connect(delButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(delClicked()));
  connect(searchPaths, TQT_SIGNAL(highlighted(const TQString &)),
      this, TQT_SLOT(pathSelected(const TQString &)));

  checkButtons();

  load();
}


void TDEHTMLSearchConfig::loadLanguages()
{
  // clear the list
  language->clear();

  // add all languages to the list
  TQStringList langs = TDEGlobal::dirs()->findAllResources("locale",
							TQString::fromLatin1("*/entry.desktop"));
  langs.sort();

  for (TQStringList::ConstIterator it = langs.begin(); it != langs.end(); ++it)
    {
      KSimpleConfig entry(*it);
      entry.setGroup(TQString::fromLatin1("KCM Locale"));
      TQString name = entry.readEntry(TQString::fromLatin1("Name"), TDEGlobal::locale()->translate("without name"));

      TQString path = *it;
      int index = path.findRev('/');
      path = path.left(index);
      index = path.findRev('/');
      path = path.mid(index+1);
      language->insertLanguage(path, name);
    }
}


TQString TDEHTMLSearchConfig::quickHelp() const
{
    return i18n( "<h1>Help Index</h1> This configuration module lets you configure the ht://dig engine which can be used for fulltext search in the TDE documentation as well as other system documentation like man and info pages." );
}


void TDEHTMLSearchConfig::pathSelected(const TQString &)
{
  checkButtons();
}


void TDEHTMLSearchConfig::checkButtons()
{

  delButton->setEnabled(searchPaths->currentItem() >= 0);
}


void TDEHTMLSearchConfig::addClicked()
{
  TQString dir = KFileDialog::getExistingDirectory();

  if (!dir.isEmpty())
    {
      for (uint i=0; i<searchPaths->count(); ++i)
    if (searchPaths->text(i) == dir)
      return;
      searchPaths->insertItem(dir);
      configChanged();
    }
}


void TDEHTMLSearchConfig::delClicked()
{
  searchPaths->removeItem(searchPaths->currentItem());
  checkButtons();
  configChanged();
}


TDEHTMLSearchConfig::~TDEHTMLSearchConfig()
{
}


void TDEHTMLSearchConfig::configChanged()
{
  emit changed(true);
}


void TDEHTMLSearchConfig::load()
{
  TDEConfig *config = new TDEConfig("khelpcenterrc", true);

  config->setGroup("htdig");
  htdigBin->lineEdit()->setText(config->readPathEntry("htdig", kapp->dirs()->findExe("htdig")));
  htsearchBin->lineEdit()->setText(config->readPathEntry("htsearch", kapp->dirs()->findExe("htsearch")));
  htmergeBin->lineEdit()->setText(config->readPathEntry("htmerge", kapp->dirs()->findExe("htmerge")));

  config->setGroup("Scope");
  indexKDE->setChecked(config->readBoolEntry("KDE", true));
  indexMan->setChecked(config->readBoolEntry("Man", false));
  indexInfo->setChecked(config->readBoolEntry("Info", false));

  TQStringList l = config->readPathListEntry("Paths");
  searchPaths->clear();
  TQStringList::Iterator it;
  for (it=l.begin(); it != l.end(); ++it)
    searchPaths->insertItem(*it);

  config->setGroup("Locale");
  TQString lang = config->readEntry("Search Language", TDEGlobal::locale()->language());
  language->setCurrentItem(lang);

  emit changed(false);
}


void TDEHTMLSearchConfig::save()
{
  TDEConfig *config= new TDEConfig("khelpcenterrc", false);

  config->setGroup("htdig");
  config->writePathEntry("htdig", htdigBin->lineEdit()->text());
  config->writePathEntry("htsearch", htsearchBin->lineEdit()->text());
  config->writePathEntry("htmerge", htmergeBin->lineEdit()->text());

  config->setGroup("Scope");
  config->writeEntry("KDE", indexKDE->isChecked());
  config->writeEntry("Man", indexMan->isChecked());
  config->writeEntry("Info", indexInfo->isChecked());

  TQStringList l;
  for (uint i=0; i<searchPaths->count(); ++i)
    l.append(searchPaths->text(i));
  config->writePathEntry("Paths", l);

  config->setGroup("Locale");
  config->writeEntry("Search Language", language->currentTag());

  config->sync();
  delete config;

  emit changed(false);
}


void TDEHTMLSearchConfig::defaults()
{
  htdigBin->lineEdit()->setText(kapp->dirs()->findExe("htdig"));
  htsearchBin->lineEdit()->setText(kapp->dirs()->findExe("htsearch"));
  htmergeBin->lineEdit()->setText(kapp->dirs()->findExe("htmerge"));

  indexKDE->setChecked(true);
  indexMan->setChecked(false);
  indexInfo->setChecked(false);

  searchPaths->clear();

  language->setCurrentItem(TDEGlobal::locale()->language());

  emit changed(true);
}


void TDEHTMLSearchConfig::urlClicked(const TQString &url)
{
  kapp->invokeBrowser(url);
}


void TDEHTMLSearchConfig::generateIndex()
{
  save();

  TQString exe = kapp->dirs()->findExe("tdehtmlindex");
  if (exe.isEmpty())
    return;

  delete indexProc;

  indexProc = new TDEProcess;
  *indexProc << exe << "--lang" << language->currentTag();

  connect(indexProc, TQT_SIGNAL(processExited(TDEProcess *)),
      this, TQT_SLOT(indexTerminated(TDEProcess *)));

  runButton->setEnabled(false);

  indexProc->start();
}


void TDEHTMLSearchConfig::indexTerminated(TDEProcess *)
{
  runButton->setEnabled(true);
}


extern "C"
{
  KDE_EXPORT TDECModule *create_htmlsearch(TQWidget *parent, const char *name)
  {
    TDEGlobal::locale()->insertCatalogue("kcmhtmlsearch");
    return new TDEHTMLSearchConfig(parent, name);
  };
}