summaryrefslogtreecommitdiffstats
path: root/kmouth/wordcompletion/dictionarycreationwizard.cpp
blob: 7adbb729318873027ae00ee477d3257e1bf5d53a (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
371
372
373
374
375
376
/***************************************************************************
                          wordcompletionwidget.cpp  -  description
                             -------------------
    begin                : Tue Apr 29 2003
    copyright            : (C) 2002 by Gunnar Schmi Dt
    email                : kmouth@schmi-dt.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqlineedit.h>
#include <tqcombobox.h>
#include <tqtextcodec.h>
#include <tqwhatsthis.h>

#include <klistview.h>
#include <klineedit.h>
#include <kurlrequester.h>
#include <klocale.h>
#include <kcombobox.h>
#include <ksconfig.h>
#include <kapplication.h>
#include <kstandarddirs.h>
#include <kprogress.h>
#include <tdeconfig.h>

#include "dictionarycreationwizard.h"
#include "klanguagebutton.h"
#include "creationsourceui.h"
#include "creationsourcedetailsui.h"
#include "kdedocsourceui.h"
#include "wordlist.h"

DictionaryCreationWizard::DictionaryCreationWizard (TQWidget *parent, const char *name,
               TQStringList dictionaryNames, TQStringList dictionaryFiles,
               TQStringList dictionaryLanguages)
   : KWizard (parent, name)
{
   buildCodecList ();
   
   creationSource = new CreationSourceUI (this, "source page");
   addPage (creationSource, i18n("Source of New Dictionary (1)"));
   setHelpEnabled (creationSource, false);
   setFinishEnabled (creationSource, false);

   fileWidget= new CreationSourceDetailsUI (this, "file source page");
   addPage (fileWidget, i18n("Source of New Dictionary (2)"));
   buildCodecCombo (fileWidget->encodingCombo);

   dirWidget= new CreationSourceDetailsUI (this, "directory source page");
   addPage (dirWidget, i18n("Source of New Dictionary (2)"));
   dirWidget->urlLabel->setText (i18n("&Directory:"));
    TQWhatsThis::add (dirWidget->urlLabel, i18n("With this input field you specify which directory you want to load for creating the new dictionary."));
   dirWidget->url->setMode(KFile::Directory);
    TQWhatsThis::add (dirWidget->url, i18n("With this input field you specify which directory you want to load for creating the new dictionary."));
   buildCodecCombo (dirWidget->encodingCombo);

   kdeDocWidget= new KDEDocSourceUI (this, "TDE documentation source page");
   addPage (kdeDocWidget, i18n("Source of New Dictionary (2)"));

   mergeWidget = new MergeWidget (this, "merge source page", dictionaryNames, dictionaryFiles, dictionaryLanguages);
   addPage (mergeWidget, i18n("Source of New Dictionary (2)"));
   
   connect (creationSource->fileButton,    TQT_SIGNAL (toggled(bool)), this, TQT_SLOT(calculateAppropriate(bool)) );
   connect (creationSource->directoryButton,TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(calculateAppropriate(bool)) );
   connect (creationSource->kdeDocButton,  TQT_SIGNAL (toggled(bool)), this, TQT_SLOT(calculateAppropriate(bool)) );
   connect (creationSource->mergeButton,   TQT_SIGNAL (toggled(bool)), this, TQT_SLOT(calculateAppropriate(bool)) );
   connect (creationSource->emptyButton,   TQT_SIGNAL (toggled(bool)), this, TQT_SLOT(calculateAppropriate(bool)) );

   calculateAppropriate (true);
}

DictionaryCreationWizard::~DictionaryCreationWizard () {
   delete codecList;
   removePage (fileWidget);   delete fileWidget;
   removePage (dirWidget);    delete dirWidget;
   removePage (kdeDocWidget); delete kdeDocWidget;
   removePage (mergeWidget);  delete mergeWidget;
}

void DictionaryCreationWizard::buildCodecList () {
   codecList = new TQPtrList<TQTextCodec>;
   TQTextCodec *codec;
   int i;
   for (i = 0; (codec = TQTextCodec::codecForIndex(i)); i++)
      codecList->append (codec);
}

void DictionaryCreationWizard::buildCodecCombo (TQComboBox *combo) {
   TQString local = i18n("Local")+" (";
   local += TQTextCodec::codecForLocale()->name();
   local += ")";
   combo->insertItem (local, 0);
   combo->insertItem (i18n("Latin1"), 1);
   combo->insertItem (i18n("Unicode"), 2);
   for (uint i = 0; i < codecList->count(); i++ )
      combo->insertItem (codecList->at(i)->name(), i+3);
}

void DictionaryCreationWizard::calculateAppropriate (bool) {
   if (creationSource->mergeButton->isChecked()) {
      setFinishEnabled (creationSource, false);
      removePage (fileWidget);
      removePage (dirWidget);
      removePage (kdeDocWidget);
      addPage (mergeWidget, i18n("Source of New Dictionary (2)"));
      setHelpEnabled (mergeWidget, false);
      setFinishEnabled (mergeWidget, true);
   }
   else if (creationSource->emptyButton->isChecked()) {
      removePage (fileWidget);
      removePage (dirWidget);
      removePage (kdeDocWidget);
      removePage (mergeWidget);
      setFinishEnabled (creationSource, true);
   }
   else if (creationSource->fileButton->isChecked()) {
      setFinishEnabled (creationSource, false);
      removePage (dirWidget);
      removePage (kdeDocWidget);
      removePage (mergeWidget);
      addPage (fileWidget, i18n("Source of New Dictionary (2)"));
      setHelpEnabled (fileWidget, false);
      setFinishEnabled (fileWidget, true);
   }
   else if (creationSource->directoryButton->isChecked()) {
      setFinishEnabled (creationSource, false);
      removePage (fileWidget);
      removePage (kdeDocWidget);
      removePage (mergeWidget);
      addPage (dirWidget, i18n("Source of New Dictionary (2)"));
      setHelpEnabled (dirWidget, false);
      setFinishEnabled (dirWidget, true);
   }
   else { // creationSource->kdeDocButton must be checked
      setFinishEnabled (creationSource, false);
      removePage (fileWidget);
      removePage (dirWidget);
      removePage (mergeWidget);
      addPage (kdeDocWidget, i18n("Source of New Dictionary (2)"));
      setHelpEnabled (kdeDocWidget, false);
      setFinishEnabled (kdeDocWidget, true);
   }
}

TQString DictionaryCreationWizard::createDictionary() {
   WordList::WordMap map;
   TQString dicFile = "";
   KProgressDialog *pdlg = WordList::progressDialog();

   if (creationSource->mergeButton->isChecked()) {
      map = WordList::mergeFiles (mergeWidget->mergeParameters(), pdlg);
      dicFile = TQString();
   }
   else if (creationSource->emptyButton->isChecked()) {
      dicFile = TQString();
   }
   else if (creationSource->fileButton->isChecked()) {
      TQString filename = fileWidget->url->url();
      int encoding = fileWidget->encodingCombo->currentItem();
      if (fileWidget->spellCheckBox->isChecked())
         dicFile = fileWidget->ooDictURL->url();
      switch (encoding) {
      case 0:
         map = WordList::parseFile (filename, TQTextStream::Locale, 0, pdlg);
         break;
      case 1:
         map = WordList::parseFile (filename, TQTextStream::Latin1, 0, pdlg);
         break;
      case 2:
         map = WordList::parseFile (filename, TQTextStream::Unicode, 0, pdlg);
         break;
      default:
         map = WordList::parseFile (filename, (TQTextStream::Encoding)0, codecList->at(encoding-3), pdlg);
      }
   }
   else if (creationSource->directoryButton->isChecked()) {
      TQString directory = dirWidget->url->url();
      int encoding = dirWidget->encodingCombo->currentItem();
      if (dirWidget->spellCheckBox->isChecked())
         dicFile = dirWidget->ooDictURL->url();
      switch (encoding) {
      case 0:
         map = WordList::parseDir (directory, TQTextStream::Locale, 0, pdlg);
         break;
      case 1:
         map = WordList::parseDir (directory, TQTextStream::Latin1, 0, pdlg);
         break;
      case 2:
         map = WordList::parseDir (directory, TQTextStream::Unicode, 0, pdlg);
         break;
      default:
         map = WordList::parseDir (directory, (TQTextStream::Encoding)0, codecList->at(encoding-3), pdlg);
      }
   }
   else { // creationSource->kdeDocButton must be checked
      TQString language = kdeDocWidget->languageButton->currentTag();
      if (kdeDocWidget->spellCheckBox->isChecked())
         dicFile = kdeDocWidget->ooDictURL->url();
      map = WordList::parseKDEDoc (language, pdlg);
   }
   
   if (!dicFile.isEmpty() && !dicFile.isNull())
      map = WordList::spellCheck (map, dicFile, pdlg);
   pdlg->close();
   delete pdlg;
   
   int dictnumber = 0;
   TQString filename;
   TQString dictionaryFile;
   do {
      dictnumber++;
      filename = TQString("wordcompletion%1.dict").arg(dictnumber);
      dictionaryFile = TDEApplication::kApplication()->dirs()->findResource("appdata", filename);
   }
   while (TDEStandardDirs::exists(dictionaryFile));
   
   dictionaryFile = TDEApplication::kApplication()->dirs()->saveLocation ("appdata", "/") + filename;
   if (WordList::saveWordList (map, dictionaryFile))
      return filename;
   else
      return "";
}

TQString DictionaryCreationWizard::name() {
   if (creationSource->mergeButton->isChecked()) {
      return i18n("Merge result");
   }
   else if (creationSource->emptyButton->isChecked()) {
      return i18n("In the sense of a blank word list", "Empty list");
   }
   else if (creationSource->fileButton->isChecked()) {
      return fileWidget->url->url();
   }
   else if (creationSource->directoryButton->isChecked()) {
      return dirWidget->url->url();
   }
   else { // creationSource->kdeDocButton must be checked
      return i18n("TDE Documentation");
   }
}

TQString DictionaryCreationWizard::language() {
   if (creationSource->mergeButton->isChecked()) {
      return mergeWidget->language();
   }
   else if (creationSource->emptyButton->isChecked()) {
      if (TDEGlobal::locale())
         return TDEGlobal::locale()->language();
      else
         return TDELocale::defaultLanguage();
   }
   else if (creationSource->fileButton->isChecked()) {
      return fileWidget->languageButton->currentTag();
   }
   else if (creationSource->directoryButton->isChecked()) {
      return dirWidget->languageButton->currentTag();
   }
   else { // creationSource->kdeDocButton must be checked
      return kdeDocWidget->languageButton->currentTag();
   }
}

/***************************************************************************/

MergeWidget::MergeWidget(KWizard *parent, const char *name,
               TQStringList dictionaryNames, TQStringList dictionaryFiles,
               TQStringList dictionaryLanguages)
: TQScrollView (parent, name) {
   dictionaries.setAutoDelete (false);
   weights.setAutoDelete (false);

   TQWidget *contents = new TQWidget(viewport());
   addChild(contents);
   TQGridLayout *layout = new TQGridLayout (contents);
   setResizePolicy (TQScrollView::AutoOneFit);
   layout->setColStretch (0, 0);
   layout->setColStretch (1, 1);

   int row = 0;
   TQStringList::Iterator nIt = dictionaryNames.begin();
   TQStringList::Iterator fIt = dictionaryFiles.begin();
   TQStringList::Iterator lIt = dictionaryLanguages.begin();
   for (; nIt != dictionaryNames.end(); ++nIt, ++fIt, ++lIt) {
      TQCheckBox *checkbox = new TQCheckBox(*nIt, contents);
      KIntNumInput *numInput = new KIntNumInput(contents);
      layout->addWidget (checkbox, row, 0);
      layout->addWidget (numInput, row, 1);
      
      checkbox->setChecked (true);
      numInput->setRange (1, 100, 10, true);
      numInput->setValue (100);
      connect (checkbox, TQT_SIGNAL (toggled(bool)), numInput, TQT_SLOT(setEnabled(bool)));
      
      dictionaries.insert(*fIt, checkbox);
      weights.insert(*fIt, numInput);
      languages [*fIt] = *lIt;
      row++;
   }
}

MergeWidget::~MergeWidget() {
}

TQMap <TQString, int> MergeWidget::mergeParameters () {
   TQMap <TQString, int> files;
   TQDictIterator<TQCheckBox> it(dictionaries);
   for (; it.current(); ++it) {
      if (it.current()->isChecked()) {
         TQString name = it.currentKey();
         TQString dictionaryFile = TDEApplication::kApplication()->dirs()->findResource("appdata", name);
         files[dictionaryFile] = weights[name]->value();
      }
   }

   return files;
}

TQString MergeWidget::language () {
   TQDictIterator<TQCheckBox> it(dictionaries);
   for (; it.current(); ++it) {
      if (it.current()->isChecked()) {
         return languages [it.currentKey()];
      }
   }

   return TQString();
}

/***************************************************************************/

CompletionWizardWidget::CompletionWizardWidget (KWizard *parent, const char *name)
   : KDEDocSourceUI (parent, name) {
}

CompletionWizardWidget::~CompletionWizardWidget() {
}

void CompletionWizardWidget::ok (TDEConfig *config) {
   WordList::WordMap map;
   KProgressDialog *pdlg = WordList::progressDialog();

   TQString language = languageButton->currentTag();
   map = WordList::parseKDEDoc (language, pdlg);

   if (spellCheckBox->isChecked())
      map = WordList::spellCheck (map, ooDictURL->url(), pdlg);

   pdlg->close();
   delete pdlg;
   
   TQString filename;
   TQString dictionaryFile;
   
   dictionaryFile = TDEApplication::kApplication()->dirs()->saveLocation ("appdata", "/") + "wordcompletion1.dict";
   if (WordList::saveWordList (map, dictionaryFile)) {
      config->setGroup("Dictionary 0");
      config->writeEntry ("Filename", "wordcompletion1.dict");
      config->writeEntry ("Name",     i18n("Default"));
      config->writeEntry ("Language", language);
      config->sync();
   }
}

#include "dictionarycreationwizard.moc"