summaryrefslogtreecommitdiffstats
path: root/kmouth/wordcompletion
diff options
context:
space:
mode:
Diffstat (limited to 'kmouth/wordcompletion')
-rw-r--r--kmouth/wordcompletion/Makefile.am13
-rw-r--r--kmouth/wordcompletion/creationsourcedetailsui.ui184
-rw-r--r--kmouth/wordcompletion/creationsourcedetailsui.ui.h37
-rw-r--r--kmouth/wordcompletion/creationsourceui.ui179
-rw-r--r--kmouth/wordcompletion/creationsourceui.ui.h26
-rw-r--r--kmouth/wordcompletion/dictionarycreationwizard.cpp376
-rw-r--r--kmouth/wordcompletion/dictionarycreationwizard.h110
-rw-r--r--kmouth/wordcompletion/kdedocsourceui.ui101
-rw-r--r--kmouth/wordcompletion/kdedocsourceui.ui.h19
-rw-r--r--kmouth/wordcompletion/klanguagebutton.cpp238
-rw-r--r--kmouth/wordcompletion/klanguagebutton.h93
-rw-r--r--kmouth/wordcompletion/klanguagebuttonhelper.cpp64
-rw-r--r--kmouth/wordcompletion/klanguagebuttonhelper.h28
-rw-r--r--kmouth/wordcompletion/wordcompletion.cpp224
-rw-r--r--kmouth/wordcompletion/wordcompletion.h77
-rw-r--r--kmouth/wordcompletion/wordcompletionui.ui293
-rw-r--r--kmouth/wordcompletion/wordcompletionui.ui.h35
-rw-r--r--kmouth/wordcompletion/wordcompletionwidget.cpp325
-rw-r--r--kmouth/wordcompletion/wordcompletionwidget.h80
-rw-r--r--kmouth/wordcompletion/wordlist.cpp550
-rw-r--r--kmouth/wordcompletion/wordlist.h92
21 files changed, 3144 insertions, 0 deletions
diff --git a/kmouth/wordcompletion/Makefile.am b/kmouth/wordcompletion/Makefile.am
new file mode 100644
index 0000000..8926f41
--- /dev/null
+++ b/kmouth/wordcompletion/Makefile.am
@@ -0,0 +1,13 @@
+noinst_LIBRARIES = libwordcompletion.a
+
+INCLUDES = $(all_includes)
+
+libwordcompletion_a_METASOURCES = AUTO
+
+libwordcompletion_a_SOURCES = wordcompletionui.ui creationsourceui.ui creationsourcedetailsui.ui kdedocsourceui.ui \
+ wordlist.cpp wordcompletion.cpp wordcompletionwidget.cpp dictionarycreationwizard.cpp klanguagebutton.cpp klanguagebuttonhelper.cpp
+
+EXTRA_DIST = wordcompletionui.ui creationsourceui.ui creationsourcedetailsui.ui kdedocsourceui.ui \
+ creationsourceui.ui.h creationsourcedetailsui.ui.h kdedocsourceui.ui.h \
+ wordlist.cpp wordcompletion.cpp wordcompletionwidget.cpp dictionarycreationwizard.cpp klanguagebutton.cpp klanguagebuttonhelper.cpp \
+ wordlist.h wordcompletion.h wordcompletionwidget.h dictionarycreationwizard.h klanguagebutton.h klanguagebuttonhelper.h
diff --git a/kmouth/wordcompletion/creationsourcedetailsui.ui b/kmouth/wordcompletion/creationsourcedetailsui.ui
new file mode 100644
index 0000000..1a1f61b
--- /dev/null
+++ b/kmouth/wordcompletion/creationsourcedetailsui.ui
@@ -0,0 +1,184 @@
+<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
+<class>CreationSourceDetailsUI</class>
+<widget class="QWidget">
+ <property name="name">
+ <cstring>CreationSourceDetailsUI</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>421</width>
+ <height>189</height>
+ </rect>
+ </property>
+ <grid>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="QCheckBox" row="3" column="0" rowspan="1" colspan="2">
+ <property name="name">
+ <cstring>spellCheckBox</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Compare to OpenOffice.org dictionary:</string>
+ </property>
+ <property name="checked">
+ <bool>false</bool>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this check box the words are spell-checked before they are inserted into the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="QLabel" row="1" column="0">
+ <property name="name">
+ <cstring>encodingLabel</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Character &amp;encoding:</string>
+ </property>
+ <property name="buddy" stdset="0">
+ <cstring>encodingCombo</cstring>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this combo box you select the character encoding used to load text files. This combo box is not used for XML files or for dictionary files.</string>
+ </property>
+ </widget>
+ <widget class="QLabel" row="0" column="0">
+ <property name="name">
+ <cstring>urlLabel</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Filename:</string>
+ </property>
+ <property name="buddy" stdset="0">
+ <cstring>url</cstring>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you specify which file you want to load for creating the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="KURLRequester" row="0" column="1">
+ <property name="name">
+ <cstring>url</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>5</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you specify which file you want to load for creating the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="KComboBox" row="1" column="1">
+ <property name="name">
+ <cstring>encodingCombo</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this combo box you select the character encoding used to load text files. This combo box is not used for XML files or for dictionary files.</string>
+ </property>
+ </widget>
+ <widget class="KURLRequester" row="4" column="1">
+ <property name="name">
+ <cstring>ooDictURL</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you select the OpenOffice.org directory that will be used to spellcheck the words of the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="QLabel" row="2" column="0">
+ <property name="name">
+ <cstring>languageLabel</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Language:</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this combo box you decide which language should be associated with the new dictionary.</string>
+ </property>
+ </widget>
+ </grid>
+</widget>
+<connections>
+ <connection>
+ <sender>spellCheckBox</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>ooDictURL</receiver>
+ <slot>setEnabled(bool)</slot>
+ </connection>
+</connections>
+<includes>
+ <include location="global" impldecl="in implementation">kinputdialog.h</include>
+ <include location="local" impldecl="in implementation">klanguagebutton.h</include>
+ <include location="local" impldecl="in implementation">klanguagebuttonhelper.h</include>
+ <include location="local" impldecl="in implementation">creationsourcedetailsui.ui.h</include>
+</includes>
+<forwards>
+ <forward>class KLanguageButton</forward>
+</forwards>
+<variables>
+ <variable access="public">KLanguageButton *languageButton;</variable>
+</variables>
+<slots>
+ <slot access="protected" specifier="non virtual">languageButton_activated( int index )</slot>
+</slots>
+<functions>
+ <function access="private" specifier="non virtual">init()</function>
+</functions>
+<layoutdefaults spacing="6" margin="11"/>
+<includehints>
+ <includehint>kinputdialog.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>kcombobox.h</includehint>
+ <includehint>kurlrequester.h</includehint>
+ <includehint>kinputdialog.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+</includehints>
+</UI>
diff --git a/kmouth/wordcompletion/creationsourcedetailsui.ui.h b/kmouth/wordcompletion/creationsourcedetailsui.ui.h
new file mode 100644
index 0000000..956f021
--- /dev/null
+++ b/kmouth/wordcompletion/creationsourcedetailsui.ui.h
@@ -0,0 +1,37 @@
+/****************************************************************************
+** ui.h extension file, included from the uic-generated form implementation.
+**
+** If you wish to add, delete or rename functions or slots use
+** Qt Designer which will update this file, preserving your code. Create an
+** init() function in place of a constructor, and a destroy() function in
+** place of a destructor.
+*****************************************************************************/
+
+
+void CreationSourceDetailsUI::init() {
+ languageButton = new KLanguageButton (this, "languageButton");
+ CreationSourceDetailsUILayout->addWidget (languageButton, 2, 1);
+ languageLabel->setBuddy (languageButton);
+ QWhatsThis::add (languageButton, i18n("With this combo box you decide which language should be associated with the new dictionary."));
+
+ loadLanguageList(languageButton);
+ languageButton->insertLanguage("??", i18n("Other"), QString::fromLatin1("l10n/"), QString::null);
+
+ connect (languageButton, SIGNAL(activated(int)), this, SLOT(languageButton_activated(int)));
+}
+
+void CreationSourceDetailsUI::languageButton_activated (int) {
+ if (languageButton->currentTag() == "??") {
+ QString customLanguage = KInputDialog::getText(i18n("Create Custom Language"), i18n("Please enter the code for the custom language:"));
+
+ if (languageButton->containsTag(customLanguage)) {
+ languageButton->setCurrentItem(customLanguage);
+ }
+ else {
+ languageButton->insertLanguage(customLanguage, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
+ languageButton->setCurrentItem(customLanguage);
+ }
+ }
+}
+
+
diff --git a/kmouth/wordcompletion/creationsourceui.ui b/kmouth/wordcompletion/creationsourceui.ui
new file mode 100644
index 0000000..13a7c06
--- /dev/null
+++ b/kmouth/wordcompletion/creationsourceui.ui
@@ -0,0 +1,179 @@
+<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
+<class>CreationSourceUI</class>
+<widget class="QWidget">
+ <property name="name">
+ <cstring>CreationSourceUI</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>318</width>
+ <height>223</height>
+ </rect>
+ </property>
+ <grid>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="QRadioButton" row="0" column="0" rowspan="1" colspan="2">
+ <property name="name">
+ <cstring>createButton</cstring>
+ </property>
+ <property name="text">
+ <string>C&amp;reate new dictionary:</string>
+ </property>
+ <property name="checked">
+ <bool>true</bool>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a new dictionary is created by either loading a dictionary file or by counting the individual words in a text.</string>
+ </property>
+ </widget>
+ <widget class="QRadioButton" row="4" column="0" rowspan="1" colspan="2">
+ <property name="name">
+ <cstring>mergeButton</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Merge dictionaries</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a new dictionary is created by merging existing dictionaries.</string>
+ </property>
+ </widget>
+ <widget class="QRadioButton" row="2" column="1">
+ <property name="name">
+ <cstring>fileButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>true</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>From &amp;file</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a new dictionary is created by loading a file. You may either select an XML file, a standard text file or a file containing a word completion dictionary. If you select a standard text file or an XML file the frequentness of the individual words is detected by simply counting the occurrences of each word.</string>
+ </property>
+ </widget>
+ <widget class="QRadioButton" row="1" column="1">
+ <property name="name">
+ <cstring>kdeDocButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>true</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>From &amp;KDE documentation</string>
+ </property>
+ <property name="checked">
+ <bool>true</bool>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a new dictionary is created by parsing the KDE documentation. The frequentness of the individual words is detect by simply counting the occurrences of each word.</string>
+ </property>
+ </widget>
+ <widget class="QRadioButton" row="3" column="1">
+ <property name="name">
+ <cstring>directoryButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>true</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>From f&amp;older</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a new dictionary is created by loading all files in a folder and its subdirectories.</string>
+ </property>
+ </widget>
+ <spacer row="1" column="0" rowspan="3" colspan="1">
+ <property name="name">
+ <cstring>spacer1</cstring>
+ </property>
+ <property name="orientation">
+ <enum>Vertical</enum>
+ </property>
+ <property name="sizeType">
+ <enum>Fixed</enum>
+ </property>
+ <property name="sizeHint">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ <widget class="QRadioButton" row="5" column="0" rowspan="1" colspan="2">
+ <property name="name">
+ <cstring>emptyButton</cstring>
+ </property>
+ <property name="text">
+ <string>Create an &amp;empty wordlist</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this box a blank dictionary without any entries is created. As KMouth automatically adds newly typed words to the dictionaries it will learn your vocabulary with the time.</string>
+ </property>
+ </widget>
+ </grid>
+</widget>
+<connections>
+ <connection>
+ <sender>createButton</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>kdeDocButton</receiver>
+ <slot>setEnabled(bool)</slot>
+ </connection>
+ <connection>
+ <sender>createButton</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>fileButton</receiver>
+ <slot>setEnabled(bool)</slot>
+ </connection>
+ <connection>
+ <sender>createButton</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>directoryButton</receiver>
+ <slot>setEnabled(bool)</slot>
+ </connection>
+</connections>
+<includes>
+ <include location="global" impldecl="in implementation">qbuttongroup.h</include>
+ <include location="local" impldecl="in implementation">creationsourceui.ui.h</include>
+</includes>
+<forwards>
+ <forward>class QButtonGroup</forward>
+</forwards>
+<variables>
+ <variable access="public">QButtonGroup *group2;</variable>
+ <variable access="public">QButtonGroup *group1;</variable>
+</variables>
+<functions>
+ <function access="private" specifier="non virtual">init()</function>
+ <function access="private" specifier="non virtual">destroy()</function>
+</functions>
+<layoutdefaults spacing="6" margin="11"/>
+</UI>
diff --git a/kmouth/wordcompletion/creationsourceui.ui.h b/kmouth/wordcompletion/creationsourceui.ui.h
new file mode 100644
index 0000000..d78a439
--- /dev/null
+++ b/kmouth/wordcompletion/creationsourceui.ui.h
@@ -0,0 +1,26 @@
+/****************************************************************************
+** ui.h extension file, included from the uic-generated form implementation.
+**
+** If you wish to add, delete or rename functions or slots use
+** Qt Designer which will update this file, preserving your code. Create an
+** init() function in place of a constructor, and a destroy() function in
+** place of a destructor.
+*****************************************************************************/
+
+
+void CreationSourceUI::init() {
+ group1 = new QButtonGroup (0);
+ group1->insert (createButton);
+ group1->insert (mergeButton);
+ group1->insert (emptyButton);
+ group2 = new QButtonGroup (0);
+ group2->insert (kdeDocButton);
+ group2->insert (fileButton);
+ group2->insert (directoryButton);
+}
+
+
+void CreationSourceUI::destroy() {
+ delete group1;
+ delete group2;
+}
diff --git a/kmouth/wordcompletion/dictionarycreationwizard.cpp b/kmouth/wordcompletion/dictionarycreationwizard.cpp
new file mode 100644
index 0000000..a0711ce
--- /dev/null
+++ b/kmouth/wordcompletion/dictionarycreationwizard.cpp
@@ -0,0 +1,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 <qlayout.h>
+#include <qlabel.h>
+#include <qcheckbox.h>
+#include <qradiobutton.h>
+#include <qlineedit.h>
+#include <qcombobox.h>
+#include <qtextcodec.h>
+#include <qwhatsthis.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 <kconfig.h>
+
+#include "dictionarycreationwizard.h"
+#include "klanguagebutton.h"
+#include "creationsourceui.h"
+#include "creationsourcedetailsui.h"
+#include "kdedocsourceui.h"
+#include "wordlist.h"
+
+DictionaryCreationWizard::DictionaryCreationWizard (QWidget *parent, const char *name,
+ QStringList dictionaryNames, QStringList dictionaryFiles,
+ QStringList 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:"));
+ QWhatsThis::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);
+ QWhatsThis::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, "KDE 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, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
+ connect (creationSource->directoryButton,SIGNAL(toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
+ connect (creationSource->kdeDocButton, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
+ connect (creationSource->mergeButton, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
+ connect (creationSource->emptyButton, SIGNAL (toggled(bool)), this, 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 QPtrList<QTextCodec>;
+ QTextCodec *codec;
+ int i;
+ for (i = 0; (codec = QTextCodec::codecForIndex(i)); i++)
+ codecList->append (codec);
+}
+
+void DictionaryCreationWizard::buildCodecCombo (QComboBox *combo) {
+ QString local = i18n("Local")+" (";
+ local += QTextCodec::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);
+ }
+}
+
+QString DictionaryCreationWizard::createDictionary() {
+ WordList::WordMap map;
+ QString dicFile = "";
+ KProgressDialog *pdlg = WordList::progressDialog();
+
+ if (creationSource->mergeButton->isChecked()) {
+ map = WordList::mergeFiles (mergeWidget->mergeParameters(), pdlg);
+ dicFile = QString::null;
+ }
+ else if (creationSource->emptyButton->isChecked()) {
+ dicFile = QString::null;
+ }
+ else if (creationSource->fileButton->isChecked()) {
+ QString 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, QTextStream::Locale, 0, pdlg);
+ break;
+ case 1:
+ map = WordList::parseFile (filename, QTextStream::Latin1, 0, pdlg);
+ break;
+ case 2:
+ map = WordList::parseFile (filename, QTextStream::Unicode, 0, pdlg);
+ break;
+ default:
+ map = WordList::parseFile (filename, (QTextStream::Encoding)0, codecList->at(encoding-3), pdlg);
+ }
+ }
+ else if (creationSource->directoryButton->isChecked()) {
+ QString 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, QTextStream::Locale, 0, pdlg);
+ break;
+ case 1:
+ map = WordList::parseDir (directory, QTextStream::Latin1, 0, pdlg);
+ break;
+ case 2:
+ map = WordList::parseDir (directory, QTextStream::Unicode, 0, pdlg);
+ break;
+ default:
+ map = WordList::parseDir (directory, (QTextStream::Encoding)0, codecList->at(encoding-3), pdlg);
+ }
+ }
+ else { // creationSource->kdeDocButton must be checked
+ QString 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;
+ QString filename;
+ QString dictionaryFile;
+ do {
+ dictnumber++;
+ filename = QString("wordcompletion%1.dict").arg(dictnumber);
+ dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", filename);
+ }
+ while (KStandardDirs::exists(dictionaryFile));
+
+ dictionaryFile = KApplication::kApplication()->dirs()->saveLocation ("appdata", "/") + filename;
+ if (WordList::saveWordList (map, dictionaryFile))
+ return filename;
+ else
+ return "";
+}
+
+QString 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("KDE Documentation");
+ }
+}
+
+QString DictionaryCreationWizard::language() {
+ if (creationSource->mergeButton->isChecked()) {
+ return mergeWidget->language();
+ }
+ else if (creationSource->emptyButton->isChecked()) {
+ if (KGlobal::locale())
+ return KGlobal::locale()->language();
+ else
+ return KLocale::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,
+ QStringList dictionaryNames, QStringList dictionaryFiles,
+ QStringList dictionaryLanguages)
+: QScrollView (parent, name) {
+ dictionaries.setAutoDelete (false);
+ weights.setAutoDelete (false);
+
+ QWidget *contents = new QWidget(viewport());
+ addChild(contents);
+ QGridLayout *layout = new QGridLayout (contents);
+ setResizePolicy (QScrollView::AutoOneFit);
+ layout->setColStretch (0, 0);
+ layout->setColStretch (1, 1);
+
+ int row = 0;
+ QStringList::Iterator nIt = dictionaryNames.begin();
+ QStringList::Iterator fIt = dictionaryFiles.begin();
+ QStringList::Iterator lIt = dictionaryLanguages.begin();
+ for (; nIt != dictionaryNames.end(); ++nIt, ++fIt, ++lIt) {
+ QCheckBox *checkbox = new QCheckBox(*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, SIGNAL (toggled(bool)), numInput, SLOT(setEnabled(bool)));
+
+ dictionaries.insert(*fIt, checkbox);
+ weights.insert(*fIt, numInput);
+ languages [*fIt] = *lIt;
+ row++;
+ }
+}
+
+MergeWidget::~MergeWidget() {
+}
+
+QMap <QString, int> MergeWidget::mergeParameters () {
+ QMap <QString, int> files;
+ QDictIterator<QCheckBox> it(dictionaries);
+ for (; it.current(); ++it) {
+ if (it.current()->isChecked()) {
+ QString name = it.currentKey();
+ QString dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", name);
+ files[dictionaryFile] = weights[name]->value();
+ }
+ }
+
+ return files;
+}
+
+QString MergeWidget::language () {
+ QDictIterator<QCheckBox> it(dictionaries);
+ for (; it.current(); ++it) {
+ if (it.current()->isChecked()) {
+ return languages [it.currentKey()];
+ }
+ }
+
+ return QString::null;
+}
+
+/***************************************************************************/
+
+CompletionWizardWidget::CompletionWizardWidget (KWizard *parent, const char *name)
+ : KDEDocSourceUI (parent, name) {
+}
+
+CompletionWizardWidget::~CompletionWizardWidget() {
+}
+
+void CompletionWizardWidget::ok (KConfig *config) {
+ WordList::WordMap map;
+ KProgressDialog *pdlg = WordList::progressDialog();
+
+ QString language = languageButton->currentTag();
+ map = WordList::parseKDEDoc (language, pdlg);
+
+ if (spellCheckBox->isChecked())
+ map = WordList::spellCheck (map, ooDictURL->url(), pdlg);
+
+ pdlg->close();
+ delete pdlg;
+
+ QString filename;
+ QString dictionaryFile;
+
+ dictionaryFile = KApplication::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"
diff --git a/kmouth/wordcompletion/dictionarycreationwizard.h b/kmouth/wordcompletion/dictionarycreationwizard.h
new file mode 100644
index 0000000..18928e1
--- /dev/null
+++ b/kmouth/wordcompletion/dictionarycreationwizard.h
@@ -0,0 +1,110 @@
+/***************************************************************************
+ dictionarycreationwizard.h - 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. *
+ * *
+ ***************************************************************************/
+
+// $Id$
+
+#ifndef DICTIONARYCREATIONWIZARD_H
+#define DICTIONARYCREATIONWIZARD_H
+
+#include <qptrlist.h>
+#include <qmap.h>
+
+#include <kwizard.h>
+#include <knuminput.h>
+#include "kdedocsourceui.h"
+
+class CreationSourceUI;
+class CompletionWizardWidget;
+class CreationSourceDetailsUI;
+class KDEDocSourceUI;
+class QTextCodec;
+class QComboBox;
+class MergeWidget;
+
+/**
+ * This class represents a wizard that is used in order to gather all
+ * necessary information for creating a new dictionary for the word
+ * completion.
+ */
+class DictionaryCreationWizard : public KWizard {
+ Q_OBJECT
+public:
+ DictionaryCreationWizard (QWidget *parent, const char *name,
+ QStringList dictionaryNames,
+ QStringList dictionaryFiles,
+ QStringList dictionaryLanguages);
+ ~DictionaryCreationWizard();
+
+ QString createDictionary();
+ QString name();
+ QString language();
+
+private slots:
+ void calculateAppropriate (bool);
+
+private:
+ void buildCodecList ();
+ void buildCodecCombo (QComboBox *combo);
+
+ CreationSourceUI *creationSource;
+ CreationSourceDetailsUI *fileWidget;
+ CreationSourceDetailsUI *dirWidget;
+ KDEDocSourceUI *kdeDocWidget;
+ MergeWidget *mergeWidget;
+
+ QPtrList<QTextCodec> *codecList;
+};
+
+/**
+ * This class represents a widget for creating an initial dictionary from the
+ * KDE documentation.
+ * @author Gunnar Schmi Dt
+ */
+class MergeWidget : public QScrollView {
+ Q_OBJECT
+public:
+ MergeWidget(KWizard *parent, const char *name,
+ QStringList dictionaryNames,
+ QStringList dictionaryFiles,
+ QStringList dictionaryLanguages);
+ ~MergeWidget();
+
+ QMap <QString, int> mergeParameters ();
+ QString language ();
+
+private:
+ QDict<QCheckBox> dictionaries;
+ QDict<KIntNumInput> weights;
+ QMap<QString,QString> languages;
+};
+
+/**
+ * This class represents a widget for creating an initial dictionary from the
+ * KDE documentation.
+ * @author Gunnar Schmi Dt
+ */
+class CompletionWizardWidget : public KDEDocSourceUI {
+ Q_OBJECT
+ friend class ConfigWizard;
+public:
+ CompletionWizardWidget(KWizard *parent, const char *name);
+ ~CompletionWizardWidget();
+
+ void ok (KConfig *config);
+};
+
+#endif
diff --git a/kmouth/wordcompletion/kdedocsourceui.ui b/kmouth/wordcompletion/kdedocsourceui.ui
new file mode 100644
index 0000000..21ce02a
--- /dev/null
+++ b/kmouth/wordcompletion/kdedocsourceui.ui
@@ -0,0 +1,101 @@
+<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
+<class>KDEDocSourceUI</class>
+<widget class="QWidget">
+ <property name="name">
+ <cstring>KDEDocSourceUI</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>272</width>
+ <height>100</height>
+ </rect>
+ </property>
+ <grid>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="QCheckBox" row="1" column="0" rowspan="1" colspan="2">
+ <property name="name">
+ <cstring>spellCheckBox</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Compare to OpenOffice.org dictionary:</string>
+ </property>
+ <property name="checked">
+ <bool>false</bool>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>If you select this check box the words from the KDE documentation are spell-checked before they are inserted into the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="KURLRequester" row="2" column="1">
+ <property name="name">
+ <cstring>ooDictURL</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>1</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you select the OpenOffice.org directory that will be used to spellcheck the words of the new dictionary.</string>
+ </property>
+ </widget>
+ <widget class="QLabel" row="0" column="0">
+ <property name="name">
+ <cstring>languageLabel</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Language:</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this combo box you select which of the installed languages is used for creating the new dictionary. KMouth will only parse documentation files of this language.</string>
+ </property>
+ </widget>
+ </grid>
+</widget>
+<connections>
+ <connection>
+ <sender>spellCheckBox</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>ooDictURL</receiver>
+ <slot>setEnabled(bool)</slot>
+ </connection>
+</connections>
+<includes>
+ <include location="local" impldecl="in implementation">klanguagebutton.h</include>
+ <include location="local" impldecl="in implementation">klanguagebuttonhelper.h</include>
+ <include location="local" impldecl="in implementation">kdedocsourceui.ui.h</include>
+</includes>
+<forwards>
+ <forward>class KLanguageButton</forward>
+</forwards>
+<variables>
+ <variable access="public">KLanguageButton *languageButton;</variable>
+</variables>
+<functions>
+ <function access="private" specifier="non virtual">init()</function>
+</functions>
+<layoutdefaults spacing="6" margin="11"/>
+<includehints>
+ <includehint>kurlrequester.h</includehint>
+ <includehint>klineedit.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+</includehints>
+</UI>
diff --git a/kmouth/wordcompletion/kdedocsourceui.ui.h b/kmouth/wordcompletion/kdedocsourceui.ui.h
new file mode 100644
index 0000000..4c42b32
--- /dev/null
+++ b/kmouth/wordcompletion/kdedocsourceui.ui.h
@@ -0,0 +1,19 @@
+/****************************************************************************
+** ui.h extension file, included from the uic-generated form implementation.
+**
+** If you wish to add, delete or rename functions or slots use
+** Qt Designer which will update this file, preserving your code. Create an
+** init() function in place of a constructor, and a destroy() function in
+** place of a destructor.
+*****************************************************************************/
+
+void KDEDocSourceUI::init() {
+ languageButton = new KLanguageButton (this, "languageButton");
+ KDEDocSourceUILayout->addWidget (languageButton, 0, 1);
+ languageLabel->setBuddy (languageButton);
+ QWhatsThis::add (languageButton, i18n("With this combo box you select which of the installed languages is used for creating the new dictionary. KMouth will only parse documentation files of this language."));
+
+ loadLanguageList(languageButton);
+
+ ooDictURL->setFilter ("*.dic");
+}
diff --git a/kmouth/wordcompletion/klanguagebutton.cpp b/kmouth/wordcompletion/klanguagebutton.cpp
new file mode 100644
index 0000000..72182d4
--- /dev/null
+++ b/kmouth/wordcompletion/klanguagebutton.cpp
@@ -0,0 +1,238 @@
+/*
+ * klanguagebutton.cpp - Adds some methods for inserting languages.
+ *
+ * Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
+ *
+ * Requires the Qt widget libraries, available at no cost at
+ * http://www.trolltech.com/
+ *
+ * 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 Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#define INCLUDE_MENUITEM_DEF
+#include <qpopupmenu.h>
+#include <kstandarddirs.h>
+
+#include "klanguagebutton.h"
+
+#include <kdebug.h>
+
+
+static inline void checkInsertPos( QPopupMenu *popup, const QString & str,
+ int &index )
+{
+ if ( index == -2 )
+ index = popup->count();
+ if ( index != -1 )
+ return;
+
+ int a = 0;
+ int b = popup->count();
+ while ( a <= b )
+ {
+ int w = ( a + b ) / 2;
+
+ int id = popup->idAt( w );
+ int j = str.localeAwareCompare( popup->text( id ) );
+
+ if ( j > 0 )
+ a = w + 1;
+ else
+ b = w - 1;
+ }
+
+ index = a; // it doesn't really matter ... a == b here.
+}
+
+static inline QPopupMenu * checkInsertIndex( QPopupMenu *popup,
+ const QStringList *tags, const QString &submenu )
+{
+ int pos = tags->findIndex( submenu );
+
+ QPopupMenu *pi = 0;
+ if ( pos != -1 )
+ {
+ QMenuItem *p = popup->findItem( pos );
+ pi = p ? p->popup() : 0;
+ }
+ if ( !pi )
+ pi = popup;
+
+ return pi;
+}
+
+
+KLanguageButton::~KLanguageButton()
+{
+ delete m_tags;
+}
+
+KLanguageButton::KLanguageButton( QWidget * parent, const char *name )
+: QPushButton( parent, name ),
+ m_popup( 0 ),
+ m_oldPopup( 0 )
+{
+ m_tags = new QStringList;
+
+ clear();
+}
+
+void KLanguageButton::insertItem( const QIconSet& icon, const QString &text,
+ const QString &tag, const QString &submenu, int index )
+{
+ QPopupMenu *pi = checkInsertIndex( m_popup, m_tags, submenu );
+ checkInsertPos( pi, text, index );
+ pi->insertItem( icon, text, count(), index );
+ m_tags->append( tag );
+}
+
+void KLanguageButton::insertItem( const QString &text, const QString &tag,
+ const QString &submenu, int index )
+{
+ QPopupMenu *pi = checkInsertIndex( m_popup, m_tags, submenu );
+ checkInsertPos( pi, text, index );
+ pi->insertItem( text, count(), index );
+ m_tags->append( tag );
+}
+
+void KLanguageButton::insertSeparator( const QString &submenu, int index )
+{
+ QPopupMenu *pi = checkInsertIndex( m_popup, m_tags, submenu );
+ pi->insertSeparator( index );
+ m_tags->append( QString::null );
+}
+
+void KLanguageButton::insertSubmenu( const QString &text, const QString &tag,
+ const QString &submenu, int index )
+{
+ QPopupMenu *pi = checkInsertIndex( m_popup, m_tags, submenu );
+ QPopupMenu *p = new QPopupMenu( pi );
+ checkInsertPos( pi, text, index );
+ pi->insertItem( text, p, count(), index );
+ m_tags->append( tag );
+ connect( p, SIGNAL( activated( int ) ),
+ SLOT( slotActivated( int ) ) );
+ connect( p, SIGNAL( highlighted( int ) ), this,
+ SIGNAL( highlighted( int ) ) );
+}
+
+void KLanguageButton::insertLanguage( const QString& path, const QString& name,
+ const QString& sub, const QString &submenu, int index )
+{
+ QString output = name + QString::fromLatin1( " (" ) + path +
+ QString::fromLatin1( ")" );
+#if 0
+ // Nooooo ! Country != language
+ QPixmap flag( locate( "locale", sub + path +
+ QString::fromLatin1( "/flag.png" ) ) );
+#endif
+ insertItem( output, path, submenu, index );
+}
+
+void KLanguageButton::slotActivated( int index )
+{
+ // Update caption and iconset:
+ if ( m_current == index )
+ return;
+
+ setCurrentItem( index );
+
+ // Forward event from popup menu as if it was emitted from this widget:
+ emit activated( index );
+}
+
+int KLanguageButton::count() const
+{
+ return m_tags->count();
+}
+
+void KLanguageButton::clear()
+{
+ m_tags->clear();
+
+ delete m_oldPopup;
+ m_oldPopup = m_popup;
+ m_popup = new QPopupMenu( this );
+
+ setPopup( m_popup );
+
+ connect( m_popup, SIGNAL( activated( int ) ),
+ SLOT( slotActivated( int ) ) );
+ connect( m_popup, SIGNAL( highlighted( int ) ),
+ SIGNAL( highlighted( int ) ) );
+
+ setText( QString::null );
+ setIconSet( QIconSet() );
+}
+
+/*void KLanguageButton::changeLanguage( const QString& name, int i )
+{
+ if ( i < 0 || i >= count() )
+ return;
+ QString output = name + QString::fromLatin1( " (" ) + tag( i ) +
+ QString::fromLatin1( ")" );
+ changeItem( output, i );
+}*/
+
+bool KLanguageButton::containsTag( const QString &str ) const
+{
+ return m_tags->contains( str ) > 0;
+}
+
+QString KLanguageButton::currentTag() const
+{
+ return *m_tags->at( currentItem() );
+}
+
+QString KLanguageButton::tag( int i ) const
+{
+ if ( i < 0 || i >= count() )
+ {
+ kdDebug() << "KLanguageButton::tag(), unknown tag " << i << endl;
+ return QString::null;
+ }
+ return *m_tags->at( i );
+}
+
+int KLanguageButton::currentItem() const
+{
+ return m_current;
+}
+
+void KLanguageButton::setCurrentItem( int i )
+{
+ if ( i < 0 || i >= count() )
+ return;
+ m_current = i;
+
+ setText( m_popup->text( m_current ) );
+ QIconSet *icon = m_popup->iconSet( m_current );
+ if( icon )
+ setIconSet( *icon );
+ else
+ setIconSet( QPixmap() );
+}
+
+void KLanguageButton::setCurrentItem( const QString &code )
+{
+ int i = m_tags->findIndex( code );
+ if ( code.isNull() )
+ i = 0;
+ if ( i != -1 )
+ setCurrentItem( i );
+}
+
+#include "klanguagebutton.moc"
diff --git a/kmouth/wordcompletion/klanguagebutton.h b/kmouth/wordcompletion/klanguagebutton.h
new file mode 100644
index 0000000..ee1d58f
--- /dev/null
+++ b/kmouth/wordcompletion/klanguagebutton.h
@@ -0,0 +1,93 @@
+/*
+ * klangbutton.h - Button with language selection drop down menu.
+ * Derived from the KLangCombo class by Hans Petter Bieker.
+ *
+ * Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
+ * (c) 2001 Martijn Klingens <mklingens@yahoo.com>
+ *
+ * Requires the Qt widget libraries, available at no cost at
+ * http://www.troll.no/
+ *
+ * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+
+#ifndef __KLANGBUTTON_H__
+#define __KLANGBUTTON_H__
+
+#include "kpushbutton.h"
+
+/*
+ * Extended QPushButton that shows a menu with submenu for language selection.
+ * Essentially just a combo box with a 2-D dataset, but using a real
+ * QComboBox will produce ugly results.
+ *
+ * Combined version of KTagCombo and KLanguageCombo but using a QPushButton
+ * instead.
+ */
+class KLanguageButton : public QPushButton
+{
+ Q_OBJECT
+
+public:
+ KLanguageButton(QWidget *parent=0, const char *name=0);
+ ~KLanguageButton();
+
+ void insertItem( const QIconSet& icon, const QString &text,
+ const QString &tag, const QString &submenu = QString::null,
+ int index = -1 );
+ void insertItem( const QString &text, const QString &tag,
+ const QString &submenu = QString::null, int index = -1 );
+ void insertSeparator( const QString &submenu = QString::null,
+ int index = -1 );
+ void insertSubmenu( const QString &text, const QString &tag,
+ const QString &submenu = QString::null, int index = -1);
+
+ int count() const;
+ void clear();
+
+ void insertLanguage( const QString& path, const QString& name,
+ const QString& sub = QString::null,
+ const QString &submenu = QString::null, int index = -1);
+
+ /*
+ * Tag of the selected item
+ */
+ QString currentTag() const;
+ QString tag( int i ) const;
+ bool containsTag( const QString &str ) const;
+
+ /*
+ * Set the current item
+ */
+ int currentItem() const;
+ void setCurrentItem( int i );
+ void setCurrentItem( const QString &code );
+
+signals:
+ void activated( int index );
+ void highlighted( int index );
+
+private slots:
+ void slotActivated( int );
+
+private:
+ // work space for the new class
+ QStringList *m_tags;
+ QPopupMenu *m_popup, *m_oldPopup;
+ int m_current;
+};
+
+#endif
diff --git a/kmouth/wordcompletion/klanguagebuttonhelper.cpp b/kmouth/wordcompletion/klanguagebuttonhelper.cpp
new file mode 100644
index 0000000..918553e
--- /dev/null
+++ b/kmouth/wordcompletion/klanguagebuttonhelper.cpp
@@ -0,0 +1,64 @@
+/*
+ * klanguagebuttonhelper.cpp - Methods that help filling the KLanguageButton with data.
+ *
+ * Copyright (c) 2003 Gunnar Schmi Dt <gunnar@schmi-dt.de>
+ *
+ * Requires the Qt widget libraries, available at no cost at
+ * http://www.trolltech.com/
+ *
+ * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "klanguagebuttonhelper.h"
+
+#include <kglobal.h>
+#include <klocale.h>
+#include <kstandarddirs.h>
+#include <ksimpleconfig.h>
+#include "klanguagebutton.h"
+
+QString languageName (QString languageCode) {
+ QString filename = KGlobal::dirs()->findResource("locale",
+ languageCode + QString::fromLatin1("/entry.desktop"));
+
+ KSimpleConfig entry(filename);
+ entry.setGroup(QString::fromLatin1("KCM Locale"));
+ return entry.readEntry(QString::fromLatin1("Name"), i18n("without name"));
+}
+
+void loadLanguageList(KLanguageButton *combo)
+// This function was taken from kdebase/kcontrol/kdm/kdm-appear.cpp
+{
+ QStringList langlist = KGlobal::dirs()->findAllResources("locale",
+ QString::fromLatin1("*/entry.desktop"));
+ langlist.sort();
+ for ( QStringList::ConstIterator it = langlist.begin();
+ it != langlist.end(); ++it )
+ {
+ QString fpath = (*it).left((*it).length() - 14);
+ int index = fpath.findRev('/');
+ QString nid = fpath.mid(index + 1);
+
+ KSimpleConfig entry(*it);
+ entry.setGroup(QString::fromLatin1("KCM Locale"));
+ QString name = entry.readEntry(QString::fromLatin1("Name"), i18n("without name"));
+ combo->insertLanguage(nid, name, QString::fromLatin1("l10n/"), QString::null);
+ }
+
+ if (KGlobal::locale())
+ combo->setCurrentItem (KGlobal::locale()->language());
+ else
+ combo->setCurrentItem (KLocale::defaultLanguage());
+}
diff --git a/kmouth/wordcompletion/klanguagebuttonhelper.h b/kmouth/wordcompletion/klanguagebuttonhelper.h
new file mode 100644
index 0000000..9bee5f7
--- /dev/null
+++ b/kmouth/wordcompletion/klanguagebuttonhelper.h
@@ -0,0 +1,28 @@
+/*
+ * klanguagebuttonhelper.h - Methods that help filling the KLanguageButton with data.
+ *
+ * Copyright (c) 2003 Gunnar Schmi Dt <gunnar@schmi-dt.de>
+ *
+ * Requires the Qt widget libraries, available at no cost at
+ * http://www.troll.no/
+ *
+ * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+class QString;
+class KLanguageButton;
+
+QString languageName (QString languageCode);
+void loadLanguageList(KLanguageButton *combo);
diff --git a/kmouth/wordcompletion/wordcompletion.cpp b/kmouth/wordcompletion/wordcompletion.cpp
new file mode 100644
index 0000000..d4ae651
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletion.cpp
@@ -0,0 +1,224 @@
+#include <qregexp.h>
+#include <qfile.h>
+
+#include <kapplication.h>
+#include <kstandarddirs.h>
+#include <kconfig.h>
+
+#include "wordcompletion.h"
+
+class WordCompletion::WordCompletionPrivate {
+friend class WordCompletion;
+private:
+ typedef QMap<QString,int> WordMap;
+ struct DictionaryDetails {
+ QString filename;
+ QString language;
+ };
+
+ QString lastText;
+ QMap<QString,int> map;
+ QMap<QString,int> addedWords;
+ QMap<QString,DictionaryDetails> dictDetails;
+ QStringList dictionaries;
+ QString current;
+ bool blockCurrentListSignal;
+ bool wordsToSave;
+};
+
+WordCompletion::WordCompletion() : KCompletion () {
+ d = new WordCompletionPrivate();
+ d->blockCurrentListSignal = false;
+ d->wordsToSave = false;
+ configure ();
+}
+
+WordCompletion::~WordCompletion() {
+ save ();
+ delete d;
+}
+
+typedef QPair<int,QString> Match;
+typedef QValueList<Match> MatchList;
+
+QString WordCompletion::makeCompletion(const QString &text) {
+ if (d->lastText != text) {
+ d->lastText = text;
+ KCompletion::clear();
+
+ int border = text.findRev(QRegExp("\\W"));
+ QString suffix = text.right (text.length() - border - 1).lower();
+ QString prefix = text.left (border + 1);
+
+ if (suffix.length() > 0) {
+ MatchList matches;
+ QMap<QString,int>::ConstIterator it;
+ for (it = d->map.begin(); it != d->map.end(); ++it)
+ if (it.key().startsWith(suffix))
+ matches += Match (-it.data(), it.key());
+ qHeapSort(matches);
+
+ MatchList::ConstIterator iter = matches.begin();
+ for (int count = 0; (iter != matches.end()) && (count < 10); ++iter, ++count) {
+ int length = (*iter).second.length() + prefix.length() - text.length();
+ KCompletion::addItem(text + (*iter).second.right(length), -(*iter).first);
+ }
+ }
+ }
+
+ // call the KCompletion::makeCompletion(...) method
+ return KCompletion::makeCompletion (text);
+}
+
+QStringList WordCompletion::wordLists() {
+ return d->dictionaries;
+}
+
+QStringList WordCompletion::wordLists(const QString &language) {
+ QStringList result;
+ for (QStringList::Iterator it = d->dictionaries.begin();
+ it != d->dictionaries.end(); ++it)
+ if (d->dictDetails[*it].language == language)
+ result += *it;
+ return result;
+}
+
+QString WordCompletion::languageOfWordList(const QString &wordlist) {
+ if (d->dictDetails.contains(wordlist))
+ return d->dictDetails[wordlist].language;
+ else
+ return QString::null;
+}
+
+QString WordCompletion::currentWordList() {
+ return d->current;
+}
+
+bool WordCompletion::isConfigured() {
+ KConfig *config = new KConfig("kmouthrc");
+ bool result = config->hasGroup("Dictionary 0");
+ delete config;
+
+ return result;
+}
+
+void WordCompletion::configure() {
+ if (d->wordsToSave)
+ save ();
+ d->wordsToSave = false;
+
+ d->dictionaries.clear();
+ d->dictDetails.clear();
+
+ KConfig *config = new KConfig("kmouthrc");
+ QStringList groups = config->groupList();
+ for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
+ if ((*it).startsWith ("Dictionary ")) {
+ config->setGroup(*it);
+ WordCompletionPrivate::DictionaryDetails details;
+ details.filename = config->readEntry("Filename");
+ details.language = config->readEntry("Language");
+ QString name = config->readEntry("Name");
+ d->dictDetails[name] = details;
+ d->dictionaries += name;
+ }
+ delete config;
+
+ d->blockCurrentListSignal = true;
+ setWordList(d->current);
+ d->blockCurrentListSignal = false;
+ emit wordListsChanged (wordLists());
+ emit currentListChanged (d->current);
+}
+
+bool WordCompletion::setWordList(const QString &wordlist) {
+ if (d->wordsToSave)
+ save ();
+ d->wordsToSave = false;
+
+ d->map.clear();
+ bool result = d->dictDetails.contains (wordlist);
+ if (result)
+ d->current = wordlist;
+ else
+ d->current = d->dictionaries[0];
+
+ QString filename = d->dictDetails[d->current].filename;
+ QString dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", filename);
+ QFile file(dictionaryFile);
+ if (file.exists() && file.open(IO_ReadOnly)) {
+ QTextStream stream(&file);
+ stream.setEncoding (QTextStream::UnicodeUTF8);
+ if (!stream.atEnd()) {
+ if (stream.readLine() == "WPDictFile") {
+ while (!stream.atEnd()) {
+ QString s = stream.readLine();
+ if (!(s.isNull() || s.isEmpty())) {
+ QStringList list = QStringList::split("\t", s);
+ bool ok;
+ int weight = list[1].toInt(&ok);
+ if (ok && (weight > 0))
+ d->map [list[0]] = weight;
+ }
+ }
+ }
+ }
+ file.close();
+ }
+ if (!d->blockCurrentListSignal)
+ emit currentListChanged (d->current);
+ d->lastText = "";
+ d->wordsToSave = false;
+ return result;
+}
+
+void WordCompletion::addSentence (const QString &sentence) {
+ QStringList words = QStringList::split(QRegExp("\\W"), sentence);
+
+ QStringList::ConstIterator it;
+ for (it = words.begin(); it != words.end(); ++it) {
+ if (!(*it).contains(QRegExp("\\d|_"))) {
+ QString key = (*it).lower();
+ if (d->map.contains(key))
+ d->map[key] += 1;
+ else
+ d->map[key] = 1;
+ if (d->addedWords.contains(key))
+ d->addedWords[key] += 1;
+ else
+ d->addedWords[key] = 1;
+ }
+ }
+ d->wordsToSave = true;
+}
+
+void WordCompletion::save () {
+ if (d->wordsToSave) {
+ QString filename = d->dictDetails[d->current].filename;
+ QString dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", filename);
+ QFile file(dictionaryFile);
+ if (!file.exists())
+ return;
+ if (!file.open(IO_WriteOnly))
+ return;
+
+ QTextStream stream(&file);
+ stream.setEncoding (QTextStream::UnicodeUTF8);
+
+ stream << "WPDictFile\n";
+ QMap<QString,int>::ConstIterator it;
+ for (it = d->map.begin(); it != d->map.end(); ++it) {
+ if (d->addedWords.contains(it.key())) {
+ stream << it.key() << "\t" << d->addedWords[it.key()] << "\t1\n";
+ stream << it.key() << "\t" << it.data() - d->addedWords[it.key()] << "\t2\n";
+ }
+ else
+ stream << it.key() << "\t" << it.data() << "\t2\n";
+ }
+ file.close();
+ d->wordsToSave = false;
+ }
+}
+
+#include "wordcompletion.moc"
+
diff --git a/kmouth/wordcompletion/wordcompletion.h b/kmouth/wordcompletion/wordcompletion.h
new file mode 100644
index 0000000..f0cb812
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletion.h
@@ -0,0 +1,77 @@
+#ifndef WORDCOMPLETION_H
+#define WORDCOMPLETION_H
+
+#include <kcompletion.h>
+
+/**
+ * This class does completion based on a dictionary of words.
+ */
+class WordCompletion : public KCompletion {
+ friend class WordListWidget;
+ Q_OBJECT
+public:
+ WordCompletion();
+ virtual ~WordCompletion();
+
+ /**
+ * Returns the names for the available word lists
+ */
+ QStringList wordLists();
+
+ /**
+ * Returns the names for those word lists that contain
+ * words of a given language.
+ */
+ QStringList wordLists(const QString &language);
+
+ /**
+ * Returns the language of a given word list.
+ */
+ QString languageOfWordList(const QString &wordlist);
+
+ /**
+ * Returns the name of the currently active word list.
+ */
+ QString currentWordList();
+
+ /**
+ * Finds completions to the given text.
+ */
+ virtual QString makeCompletion(const QString&);
+
+ static bool isConfigured();
+
+ /**
+ * Adds the words from the given sentence to the list of words.
+ */
+ void addSentence (const QString &sentence);
+
+public slots:
+ /**
+ * Re-reads the configuration.
+ */
+ void configure();
+
+ /**
+ * Specify which word list gets used for the actual word completion.
+ * If there is no word list with the given name the first configured
+ * list gets used.
+ * The method returns true if the specified word list was found.
+ */
+ bool setWordList(const QString &wordlist);
+
+ /**
+ * Saves the added words to disk.
+ */
+ void save ();
+
+signals:
+ void wordListsChanged (const QStringList &wordLists);
+ void currentListChanged (const QString &wordList);
+
+private:
+ class WordCompletionPrivate;
+ WordCompletionPrivate *d;
+};
+
+#endif // KURLCOMPLETION_H
diff --git a/kmouth/wordcompletion/wordcompletionui.ui b/kmouth/wordcompletion/wordcompletionui.ui
new file mode 100644
index 0000000..8036272
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletionui.ui
@@ -0,0 +1,293 @@
+<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
+<class>WordCompletionUI</class>
+<widget class="QWidget">
+ <property name="name">
+ <cstring>WordCompletionUI</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>552</width>
+ <height>252</height>
+ </rect>
+ </property>
+ <grid>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="QLayoutWidget" row="0" column="1">
+ <property name="name">
+ <cstring>layout1</cstring>
+ </property>
+ <vbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="KPushButton">
+ <property name="name">
+ <cstring>addButton</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Add D&amp;ictionary...</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this button you can add a new dictionary to the list of available dictionaries.</string>
+ </property>
+ </widget>
+ <widget class="KPushButton">
+ <property name="name">
+ <cstring>deleteButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Delete Dictionary</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this button you delete the selected dictionary.</string>
+ </property>
+ </widget>
+ <widget class="KPushButton">
+ <property name="name">
+ <cstring>moveUpButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Move &amp;Up</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this button you move the selected dictionary up.</string>
+ </property>
+ </widget>
+ <widget class="KPushButton">
+ <property name="name">
+ <cstring>moveDownButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Move Down</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this button you move the selected dictionary down.</string>
+ </property>
+ </widget>
+ <widget class="KPushButton">
+ <property name="name">
+ <cstring>exportButton</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Export Dictionary...</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this button you export the selected dictionary to a file.</string>
+ </property>
+ </widget>
+ <spacer>
+ <property name="name">
+ <cstring>spacer1</cstring>
+ </property>
+ <property name="orientation">
+ <enum>Vertical</enum>
+ </property>
+ <property name="sizeType">
+ <enum>Expanding</enum>
+ </property>
+ <property name="sizeHint">
+ <size>
+ <width>20</width>
+ <height>240</height>
+ </size>
+ </property>
+ </spacer>
+ </vbox>
+ </widget>
+ <widget class="QLayoutWidget" row="0" column="0">
+ <property name="name">
+ <cstring>layout2</cstring>
+ </property>
+ <vbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="KListView">
+ <column>
+ <property name="text">
+ <string>Dictionary</string>
+ </property>
+ <property name="clickable">
+ <bool>true</bool>
+ </property>
+ <property name="resizable">
+ <bool>true</bool>
+ </property>
+ </column>
+ <column>
+ <property name="text">
+ <string>Language</string>
+ </property>
+ <property name="clickable">
+ <bool>true</bool>
+ </property>
+ <property name="resizable">
+ <bool>true</bool>
+ </property>
+ </column>
+ <property name="name">
+ <cstring>dictionaryList</cstring>
+ </property>
+ <property name="allColumnsShowFocus">
+ <bool>true</bool>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>This list contains all available dictionaries for the word completion. KMouth will display a combo box next to the edit field in the main window if this list contains more than one dictionary. You can use this combo box in order to select the dictionary that actually gets used for the word completion.</string>
+ </property>
+ </widget>
+ <widget class="QGroupBox">
+ <property name="name">
+ <cstring>selectedDictionaryDetails</cstring>
+ </property>
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>5</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="title">
+ <string>&amp;Selected Dictionary</string>
+ </property>
+ <grid>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="QLabel" row="1" column="0">
+ <property name="name">
+ <cstring>languageLabel</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Language:</string>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this combo box you select the language associated with the selected dictionary.</string>
+ </property>
+ </widget>
+ <widget class="QLabel" row="0" column="0">
+ <property name="name">
+ <cstring>dictionaryNameLabel</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Name:</string>
+ </property>
+ <property name="buddy" stdset="0">
+ <cstring>dictionaryName</cstring>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you specify the name of the selected dictionary.</string>
+ </property>
+ </widget>
+ <widget class="KLineEdit" row="0" column="1">
+ <property name="name">
+ <cstring>dictionaryName</cstring>
+ </property>
+ <property name="whatsThis" stdset="0">
+ <string>With this input field you specify the name of the selected dictionary.</string>
+ </property>
+ </widget>
+ </grid>
+ </widget>
+ </vbox>
+ </widget>
+ </grid>
+</widget>
+<tabstops>
+ <tabstop>dictionaryList</tabstop>
+ <tabstop>addButton</tabstop>
+ <tabstop>deleteButton</tabstop>
+ <tabstop>moveUpButton</tabstop>
+ <tabstop>moveDownButton</tabstop>
+ <tabstop>exportButton</tabstop>
+ <tabstop>selectedDictionaryDetails</tabstop>
+</tabstops>
+<includes>
+ <include location="global" impldecl="in implementation">kinputdialog.h</include>
+ <include location="local" impldecl="in implementation">klanguagebutton.h</include>
+ <include location="local" impldecl="in implementation">klanguagebuttonhelper.h</include>
+ <include location="local" impldecl="in implementation">wordcompletionui.ui.h</include>
+</includes>
+<forwards>
+ <forward>class KLanguageButton</forward>
+</forwards>
+<variables>
+ <variable access="public">KLanguageButton *languageButton;</variable>
+</variables>
+<slots>
+ <slot access="protected" specifier="non virtual">languageButton_activated( int index )</slot>
+</slots>
+<functions>
+ <function access="private" specifier="non virtual">init()</function>
+</functions>
+<layoutdefaults spacing="6" margin="11"/>
+<includehints>
+ <includehint>kurlrequester.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>kpushbutton.h</includehint>
+ <includehint>klistview.h</includehint>
+ <includehint>kinputdialog.h</includehint>
+</includehints>
+</UI>
diff --git a/kmouth/wordcompletion/wordcompletionui.ui.h b/kmouth/wordcompletion/wordcompletionui.ui.h
new file mode 100644
index 0000000..96878da
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletionui.ui.h
@@ -0,0 +1,35 @@
+/****************************************************************************
+** ui.h extension file, included from the uic-generated form implementation.
+**
+** If you wish to add, delete or rename functions or slots use
+** Qt Designer which will update this file, preserving your code. Create an
+** init() function in place of a constructor, and a destroy() function in
+** place of a destructor.
+*****************************************************************************/
+
+
+void WordCompletionUI::init() {
+ languageButton = new KLanguageButton (selectedDictionaryDetails, "languageButton");
+ selectedDictionaryDetailsLayout->addWidget (languageButton, 1, 1);
+ languageLabel->setBuddy (languageButton);
+ QWhatsThis::add (languageButton, i18n("With this combo box you select the language associated with the selected dictionary."));
+
+ loadLanguageList(languageButton);
+ languageButton->insertLanguage("??", i18n("Other"), QString::fromLatin1("l10n/"), QString::null);
+
+ connect (languageButton, SIGNAL(activated(int)), this, SLOT(languageButton_activated(int)));
+}
+
+void WordCompletionUI::languageButton_activated (int) {
+ if (languageButton->currentTag() == "??") {
+ QString customLanguage = KInputDialog::getText(i18n("Create Custom Language"), i18n("Please enter the code for the custom language:"));
+
+ if (languageButton->containsTag(customLanguage)) {
+ languageButton->setCurrentItem(customLanguage);
+ }
+ else {
+ languageButton->insertLanguage(customLanguage, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
+ languageButton->setCurrentItem(customLanguage);
+ }
+ }
+}
diff --git a/kmouth/wordcompletion/wordcompletionwidget.cpp b/kmouth/wordcompletion/wordcompletionwidget.cpp
new file mode 100644
index 0000000..e5eab23
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletionwidget.cpp
@@ -0,0 +1,325 @@
+/***************************************************************************
+ 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 <qlayout.h>
+#include <qlabel.h>
+#include <qcheckbox.h>
+#include <qlineedit.h>
+
+#include <klistview.h>
+#include <klineedit.h>
+#include <kurlrequester.h>
+#include <klocale.h>
+#include <kglobal.h>
+#include <kstandarddirs.h>
+#include <kconfig.h>
+#include <ksimpleconfig.h>
+#include <kfiledialog.h>
+#include <kio/netaccess.h>
+#include <kmessagebox.h>
+
+#include "dictionarycreationwizard.h"
+#include "wordcompletionwidget.h"
+#include "wordcompletion.h"
+#include "klanguagebutton.h"
+
+class DictionaryListItem : public KListViewItem {
+public:
+ DictionaryListItem (QListView *parent, QString filename, QString name, QString language, QString languageCode)
+ : KListViewItem (parent, name) {
+ setFilename (filename);
+ setLanguage (language, languageCode);
+ };
+ DictionaryListItem (QListView *parent, QString filename, QString name, QString languageCode)
+ : KListViewItem (parent, name) {
+ setFilename (filename);
+ setLanguage (languageCode);
+ };
+ DictionaryListItem (QListView *parent, QListViewItem *after, QString filename, QString name, QString languageCode)
+ : KListViewItem (parent, after, name) {
+ setFilename (filename);
+ setLanguage (languageCode);
+ };
+ ~DictionaryListItem () {
+ };
+
+ QString filename() {
+ return myFilename;
+ }
+
+ QString languageCode() {
+ return myLanguageCode;
+ }
+
+ void setFilename(QString filename) {
+ myFilename = filename;
+ }
+
+ void setLanguage (QString languageCode) {
+ QString filename = KGlobal::dirs()->findResource("locale",
+ languageCode + QString::fromLatin1("/entry.desktop"));
+
+ KSimpleConfig entry(filename);
+ entry.setGroup(QString::fromLatin1("KCM Locale"));
+ QString name = entry.readEntry(QString::fromLatin1("Name"), i18n("without name"));
+ setLanguage (name + " (" + languageCode + ")", languageCode);
+ }
+
+ void setLanguage (QString name, QString languageCode) {
+ myLanguageCode = languageCode;
+ setText (1, name);
+ }
+
+private:
+ QString myFilename;
+ QString myLanguageCode;
+};
+
+/***************************************************************************/
+
+WordCompletionWidget::WordCompletionWidget(QWidget *parent, const char *name) : WordCompletionUI (parent, name) {
+ dictionaryList->setSorting (-1); // no sorted list
+
+ // Connect the signals from hte KCMKTTSDWidget to this class
+ connect (addButton, SIGNAL (clicked()), this, SLOT(addDictionary()) );
+ connect (deleteButton, SIGNAL (clicked()), this, SLOT (deleteDictionary()) );
+ connect (moveUpButton, SIGNAL (clicked()), this, SLOT (moveUp()) );
+ connect (moveDownButton, SIGNAL (clicked()), this, SLOT (moveDown()) );
+ connect (exportButton, SIGNAL (clicked()), this, SLOT (exportDictionary()) );
+
+ connect (dictionaryList, SIGNAL (selectionChanged()), this, SLOT (selectionChanged()) );
+ connect (dictionaryName, SIGNAL (textChanged (const QString &)), this, SLOT (nameChanged (const QString &)) );
+ connect (languageButton, SIGNAL (activated (int)), this, SLOT (languageSelected(int)) );
+
+ // Object for the KCMKTTSD configuration
+ config = new KConfig("kmouthrc");
+
+ // Load the configuration from the file
+ load();
+}
+
+/**
+ * Destructor
+ */
+WordCompletionWidget::~WordCompletionWidget() {
+ delete config;
+}
+
+/***************************************************************************/
+
+void WordCompletionWidget::load() {
+ dictionaryList->clear();
+
+ // Set the group general for the configuration of kttsd itself (no plug ins)
+ QStringList groups = config->groupList();
+ DictionaryListItem *last = 0;
+ for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
+ if ((*it).startsWith ("Dictionary ")) {
+ config->setGroup(*it);
+ QString languageTag = config->readEntry("Language");
+ last = new DictionaryListItem (dictionaryList, last,
+ config->readEntry("Filename"),
+ config->readEntry("Name"),
+ languageTag);
+ if (!languageButton->containsTag(languageTag))
+ languageButton->insertLanguage(languageTag, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
+ }
+
+ // Clean up disc space
+ for (QStringList::Iterator it = newDictionaryFiles.begin(); it != newDictionaryFiles.end(); ++it) {
+ QString filename = KGlobal::dirs()->findResource ("appdata", *it);
+ if (!filename.isEmpty() && !filename.isNull())
+ QFile::remove (filename);
+ }
+ newDictionaryFiles.clear();
+}
+
+void WordCompletionWidget::save() {
+ QStringList groups = config->groupList();
+ for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
+ if ((*it).startsWith ("Dictionary "))
+ config->deleteGroup (*it);
+
+ int number = 0;
+ QListViewItemIterator it(dictionaryList);
+ while (it.current()) {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
+ if (item != 0) {
+ config->setGroup(QString("Dictionary %1").arg(number));
+ config->writeEntry ("Filename", item->filename());
+ config->writeEntry ("Name", item->text (0));
+ config->writeEntry ("Language", item->languageCode());
+ number++;
+ }
+ ++it;
+ }
+ config->sync();
+
+ // Clean up disc space
+ for (QStringList::Iterator it = removedDictionaryFiles.begin(); it != removedDictionaryFiles.end(); ++it) {
+ QString filename = KGlobal::dirs()->findResource ("appdata", *it);
+ if (!filename.isEmpty() && !filename.isNull())
+ QFile::remove (filename);
+ }
+ removedDictionaryFiles.clear();
+}
+
+/***************************************************************************/
+
+void WordCompletionWidget::addDictionary() {
+ QStringList dictionaryNames;
+ QStringList dictionaryFiles;
+ QStringList dictionaryLanguages;
+ QListViewItemIterator it(dictionaryList);
+ while (it.current()) {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
+ if (item != 0) {
+ dictionaryNames += item->text (0);
+ dictionaryFiles += item->filename();
+ dictionaryLanguages += item->languageCode();
+ }
+ ++it;
+ }
+ DictionaryCreationWizard *wizard = new DictionaryCreationWizard (this, "Dictionary creation wizard", dictionaryNames, dictionaryFiles, dictionaryLanguages);
+ if (wizard->exec() == QDialog::Accepted) {
+ QString filename = wizard->createDictionary();
+ newDictionaryFiles += filename;
+ QString languageTag = wizard->language();
+ if (!languageButton->containsTag(languageTag)) {
+ languageButton->insertLanguage(languageTag, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
+ }
+ KListViewItem *item = new DictionaryListItem (dictionaryList,
+ filename, wizard->name(), languageTag);
+ dictionaryList->setSelected(item, true);
+ }
+ delete wizard;
+}
+
+void WordCompletionWidget::deleteDictionary() {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
+
+ if (item != 0) {
+ removedDictionaryFiles += item->filename();
+ delete item;
+ }
+}
+
+void WordCompletionWidget::moveUp() {
+ QListViewItem *item = dictionaryList->selectedItem ();
+
+ if (item != 0) {
+ QListViewItem *above = item->itemAbove();
+
+ if (above != 0) {
+ above->moveItem (item);
+ }
+ }
+}
+
+void WordCompletionWidget::moveDown() {
+ QListViewItem *item = dictionaryList->selectedItem ();
+
+ if (item != 0) {
+ QListViewItem *next = item->itemBelow();
+
+ if (next != 0) {
+ item->moveItem (next);
+ }
+ }
+}
+
+void WordCompletionWidget::exportDictionary() {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
+
+ if (item != 0) {
+ KURL url = KFileDialog::getSaveURL(QString::null, QString::null, this, i18n("Export Dictionary"));
+ if (url.isEmpty() || !url.isValid())
+ return;
+
+ if (KIO::NetAccess::exists(url, false, this)) {
+ if (KMessageBox::warningContinueCancel(0,QString("<qt>%1</qt>").arg(i18n("The file %1 already exists. "
+ "Do you want to overwrite it?").arg(url.url())),i18n("File Exists"),i18n("&Overwrite"))==KMessageBox::Cancel) {
+ return;
+ }
+ }
+ KURL src;
+ src.setPath( KGlobal::dirs()->findResource ("appdata", item->filename()) );
+ KIO::NetAccess::copy (src, url, this);
+ }
+}
+
+void WordCompletionWidget::selectionChanged() {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
+
+ if (item != 0) {
+ deleteButton->setEnabled(true);
+ moveUpButton->setEnabled(true);
+ moveDownButton->setEnabled(true);
+ exportButton->setEnabled(true);
+ selectedDictionaryDetails->setEnabled(true);
+ languageLabel->setEnabled(true);
+ dictionaryNameLabel->setEnabled(true);
+ dictionaryName->setEnabled(true);
+ languageButton->setEnabled(true);
+
+ dictionaryName->setText(item->text(0));
+ languageButton->setCurrentItem(item->languageCode());
+ }
+ else {
+ deleteButton->setEnabled(false);
+ moveUpButton->setEnabled(false);
+ moveDownButton->setEnabled(false);
+ exportButton->setEnabled(false);
+ selectedDictionaryDetails->setEnabled(false);
+ languageLabel->setEnabled(false);
+ dictionaryNameLabel->setEnabled(false);
+ dictionaryName->setEnabled(false);
+ languageButton->setEnabled(false);
+
+ dictionaryName->setText("");
+ languageButton->setText("");
+ }
+}
+
+void WordCompletionWidget::nameChanged (const QString &text) {
+ QListViewItem *item = dictionaryList->selectedItem ();
+
+ if (item != 0) {
+ QString old = item->text(0);
+
+ if (old != text) {
+ item->setText(0, text);
+ emit changed(true);
+ }
+ }
+}
+
+void WordCompletionWidget::languageSelected (int) {
+ DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
+
+ if (item != 0) {
+ QString old = item->text(1);
+ QString text = languageButton->currentTag();
+
+ if (old != text) {
+ item->setLanguage(languageButton->text(), text);
+ emit changed(true);
+ }
+ }
+}
+
+#include "wordcompletionwidget.moc"
diff --git a/kmouth/wordcompletion/wordcompletionwidget.h b/kmouth/wordcompletion/wordcompletionwidget.h
new file mode 100644
index 0000000..574a4e9
--- /dev/null
+++ b/kmouth/wordcompletion/wordcompletionwidget.h
@@ -0,0 +1,80 @@
+/***************************************************************************
+ wordcompletionwidget.h - 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. *
+ * *
+ ***************************************************************************/
+
+// $Id$
+
+#ifndef WORDCOMPLETIONWIDGET_H
+#define WORDCOMPLETIONWIDGET_H
+
+#include "wordcompletionui.h"
+class WordCompletion;
+class KLanguageButton;
+
+/**
+ * This class represents a configuration widget for managing dictionaries.
+ * @author Gunnar Schmi Dt
+ */
+class WordCompletionWidget : public WordCompletionUI {
+ Q_OBJECT
+public:
+ WordCompletionWidget(QWidget *parent, const char *name);
+ ~WordCompletionWidget();
+
+ /**
+ * This method is invoked whenever the widget should read its configuration
+ * from a config file and update the user interface.
+ */
+ void load();
+
+ /**
+ * This function gets called when the user wants to save the settings in
+ * the user interface, updating the config files.
+ */
+ void save();
+
+signals:
+ void changed (bool);
+
+private slots:
+ void addDictionary();
+ void deleteDictionary();
+ void moveUp();
+ void moveDown();
+ void exportDictionary();
+
+ void selectionChanged();
+ void nameChanged (const QString &text);
+ void languageSelected (int);
+
+ /**
+ * This slot is used to emit the signal changed when any widget changes
+ * the configuration
+ */
+ void configChanged() {
+ emit changed(true);
+ };
+
+private:
+ /**
+ * Object holding all the configuration
+ */
+ KConfig *config;
+ QStringList newDictionaryFiles;
+ QStringList removedDictionaryFiles;
+};
+
+#endif
diff --git a/kmouth/wordcompletion/wordlist.cpp b/kmouth/wordcompletion/wordlist.cpp
new file mode 100644
index 0000000..67f05d8
--- /dev/null
+++ b/kmouth/wordcompletion/wordlist.cpp
@@ -0,0 +1,550 @@
+/***************************************************************************
+ wordlist.cpp - description
+ -------------------
+ begin : Don Sep 12 2002
+ 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. *
+ * *
+ ***************************************************************************/
+
+// $Id$
+
+#include <qregexp.h>
+#include <qdir.h>
+
+#include <kapplication.h>
+#include <kstandarddirs.h>
+#include <kprogress.h>
+#include <klocale.h>
+
+#include "wordlist.h"
+
+namespace WordList {
+void addWords (WordMap &map, QString line);
+
+XMLParser::XMLParser() {
+}
+
+XMLParser::~XMLParser() {
+}
+
+bool XMLParser::warning (const QXmlParseException &) {
+ return false;
+}
+
+bool XMLParser::error (const QXmlParseException &) {
+ return false;
+}
+
+bool XMLParser::fatalError (const QXmlParseException &) {
+ return false;
+}
+
+QString XMLParser::errorString() {
+ return "";
+}
+
+bool XMLParser::startDocument() {
+ text = QString::null;
+ return true;
+}
+
+bool XMLParser::startElement (const QString &, const QString &,
+ const QString &,
+ const QXmlAttributes &)
+{
+ if (!text.isNull() && !text.isEmpty()) {
+ addWords (list, text);
+ text = QString::null;
+ }
+ return true;
+}
+
+bool XMLParser::characters (const QString &ch) {
+ text += ch;
+ return true;
+}
+
+bool XMLParser::ignorableWhitespace (const QString &) {
+ if (!text.isNull() && !text.isEmpty()) {
+ addWords (list, text);
+ text = QString::null;
+ }
+ return true;
+}
+
+bool XMLParser::endElement (const QString &, const QString &,
+ const QString &)
+{
+ if (!text.isNull() && !text.isEmpty()) {
+ addWords (list, text);
+ text = QString::null;
+ }
+ return true;
+}
+
+bool XMLParser::endDocument() {
+ if (!text.isNull() && !text.isEmpty()) {
+ addWords (list, text);
+ text = QString::null;
+ }
+ return true;
+}
+
+WordMap XMLParser::getList() {
+ return list;
+}
+
+/***************************************************************************/
+
+KProgressDialog *progressDialog() {
+ KProgressDialog *pdlg = new KProgressDialog(0, "progressDialog", i18n("Creating Word List"), i18n("Parsing the KDE documentation..."), false);
+ pdlg->setAllowCancel (false);
+ pdlg->showCancelButton (false);
+ pdlg->setAutoReset(false);
+ pdlg->setAutoClose(false);
+ pdlg->progressBar()->setTotalSteps(100);
+ pdlg->progressBar()->setProgress(0);
+ return pdlg;
+}
+
+bool saveWordList (WordMap map, QString filename) {
+ QFile file(filename);
+ if(!file.open(IO_WriteOnly))
+ return false;
+
+ QTextStream stream(&file);
+ stream.setEncoding (QTextStream::UnicodeUTF8);
+
+ stream << "WPDictFile\n";
+ WordMap::ConstIterator it;
+ for (it = map.begin(); it != map.end(); ++it)
+ stream << it.key() << "\t" << it.data() << "\t2\n";
+ file.close();
+ return true;
+}
+
+/***************************************************************************/
+
+void addWords (WordMap &map, QString line) {
+ QStringList words = QStringList::split(QRegExp("\\W"), line);
+
+ QStringList::ConstIterator it;
+ for (it = words.begin(); it != words.end(); ++it) {
+ if (!(*it).contains(QRegExp("\\d|_"))) {
+ QString key = (*it).lower();
+ if (map.contains(key))
+ map[key] += 1;
+ else
+ map[key] = 1;
+ }
+ }
+}
+
+void addWords (WordMap &map, WordMap add) {
+ WordList::WordMap::ConstIterator it;
+ for (it = add.begin(); it != add.end(); ++it)
+ if (map.contains(it.key()))
+ map[it.key()] += it.data();
+ else
+ map[it.key()] = it.data();
+}
+
+void addWordsFromFile (WordMap &map, QString filename, QTextStream::Encoding encoding, QTextCodec *codec) {
+ QFile xmlfile(filename);
+ QXmlInputSource source (&xmlfile);
+ XMLParser parser;
+ QXmlSimpleReader reader;
+ reader.setFeature ("http://trolltech.com/xml/features/report-start-end-entity", true);
+ reader.setContentHandler (&parser);
+
+ WordMap words;
+ if (reader.parse(source)) // try to load the file as an xml-file
+ addWords(map, parser.getList());
+ else {
+ QFile wpdfile(filename);
+ if (wpdfile.open(IO_ReadOnly)) {
+ QTextStream stream(&wpdfile);
+ stream.setEncoding (QTextStream::UnicodeUTF8);
+
+ if (!stream.atEnd()) {
+ QString s = stream.readLine();
+ if (s == "WPDictFile") { // Contains the file a weighted word list?
+ // We can assume that weighted word lists are always UTF8 coded.
+ while (!stream.atEnd()) {
+ QString s = stream.readLine();
+ if (!(s.isNull() || s.isEmpty())) {
+ QStringList list = QStringList::split("\t", s);
+ bool ok;
+ int weight = list[1].toInt(&ok);
+ if (ok && (weight > 0)) {
+ if (map.contains(list[0]))
+ map[list[0]] += weight;
+ else
+ map[list[0]] = weight;
+ }
+ }
+ }
+ }
+ else { // Count the words in an ordinary text file
+ QFile file(filename);
+ if (file.open(IO_ReadOnly)) {
+ QTextStream stream(&file);
+ if (codec != 0)
+ stream.setCodec (codec);
+ else
+ stream.setEncoding (encoding);
+ while (!stream.atEnd())
+ addWords (map, stream.readLine());
+ }
+ }
+ }
+ }
+ }
+}
+
+}
+#include <kdebug.h>
+namespace WordList {
+
+WordMap parseFiles (QStringList files, QTextStream::Encoding encoding, QTextCodec *codec, KProgressDialog *pdlg) {
+ int progress = 0;
+ int steps = files.count();
+ int percent = 0;
+
+ WordMap map;
+ QStringList::ConstIterator it;
+ for (progress = 1, it = files.begin(); it != files.end(); ++progress, ++it) {
+ addWordsFromFile (map, *it, encoding, codec);
+
+ if (steps != 0 && progress*100/steps > percent) {
+ percent = progress*100/steps;
+ pdlg->progressBar()->setProgress(percent);
+ qApp->processEvents (20);
+ }
+ }
+ return map;
+}
+
+WordMap mergeFiles (QMap<QString,int> files, KProgressDialog *pdlg) {
+ pdlg->setLabel (i18n("Merging dictionaries..."));
+ pdlg->show();
+ qApp->processEvents (20);
+
+ int progress = 0;
+ int steps = files.count();
+ int percent = 0;
+ float totalWeight = 0;
+ long long maxWeight = 0;
+
+ QMap<QString,float> map;
+ QMap<QString,int>::ConstIterator it;
+ for (progress = 1, it = files.begin(); it != files.end(); ++progress, ++it) {
+ WordMap fileMap;
+ addWordsFromFile (fileMap, it.key(), QTextStream::UnicodeUTF8, 0);
+
+ long long weight = 0;
+ WordMap::ConstIterator iter;
+ for (iter = fileMap.begin(); iter != fileMap.end(); ++iter)
+ weight += iter.data();
+ float factor = 1.0 * it.data() / weight;
+ totalWeight += it.data();
+ if (weight > maxWeight)
+ maxWeight = weight;
+
+ for (iter = fileMap.begin(); iter != fileMap.end(); ++iter)
+ if (map.contains(iter.key()))
+ map[iter.key()] += iter.data() * factor;
+ else
+ map[iter.key()] = iter.data() * factor;
+
+ if (steps != 0 && progress*100/steps > percent) {
+ percent = progress*100/steps;
+ pdlg->progressBar()->setProgress(percent);
+ qApp->processEvents (20);
+ }
+ }
+
+ float factor;
+ if (1.0 * maxWeight * totalWeight > 1000000000)
+ factor = 1000000000 / totalWeight;
+ else
+ factor = 1.0 * maxWeight;
+
+ WordMap resultMap;
+ QMap<QString,float>::ConstIterator iter;
+ for (iter = map.begin(); iter != map.end(); ++iter)
+ resultMap[iter.key()] = (int)(factor * iter.data() + 0.5);
+
+ return resultMap;
+}
+
+WordMap parseKDEDoc (QString language, KProgressDialog *pdlg) {
+ pdlg->setLabel (i18n("Parsing the KDE documentation..."));
+ pdlg->show();
+ qApp->processEvents (20);
+
+ QStringList files = KApplication::kApplication()->dirs()->findAllResources ("html", language + "/*.docbook", true, true);
+ if ((files.count() == 0) && (language.length() == 5)) {
+ language = language.left(2);
+ files = KApplication::kApplication()->dirs()->findAllResources ("html", language + "/*.docbook", true, true);
+ }
+
+ return parseFiles (files, QTextStream::UnicodeUTF8, 0, pdlg);
+}
+
+WordMap parseFile (QString filename, QTextStream::Encoding encoding, QTextCodec *codec, KProgressDialog *pdlg) {
+ pdlg->setLabel (i18n("Parsing file..."));
+ pdlg->show();
+ qApp->processEvents (20);
+
+ QStringList files = filename;
+
+ return parseFiles (files, encoding, codec, pdlg);
+}
+
+WordMap parseDir (QString directory, QTextStream::Encoding encoding, QTextCodec *codec, KProgressDialog *pdlg) {
+ pdlg->setLabel (i18n("Parsing directory..."));
+ pdlg->show();
+ qApp->processEvents (20);
+
+ QStringList directories;
+ directories += directory;
+ QStringList files;
+ for (QStringList::Iterator it = directories.begin(); it != directories.end(); it = directories.remove(it)) {
+ QDir dir(*it);
+ const QFileInfoList *entries = dir.entryInfoList ("*", QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::Readable);
+ if (entries != 0) {
+ QFileInfoListIterator iter (*entries);
+ while ((iter.current()) != 0) {
+ QString name = iter.current()->fileName();
+ if (name != "." && name != "..") {
+ if (iter.current()->isDir())
+ directories += iter.current()->filePath ();
+ else
+ files += iter.current()->filePath ();
+ }
+ ++iter;
+ }
+ }
+ }
+
+ return parseFiles (files, encoding, codec, pdlg);
+}
+
+/***************************************************************************/
+
+/* Structures used for storing *.aff files (part of OpenOffice.org dictionaries)
+ */
+struct AffEntry {
+ bool cross;
+ int charsToRemove;
+ QString add;
+ QStringList condition;
+};
+typedef QValueList<AffEntry> AffList;
+typedef QMap<QChar,AffList> AffMap;
+
+/** Loads an *.aff file (part of OpenOffice.org dictionaries)
+ */
+void loadAffFile(const QString &filename, AffMap &prefixes, AffMap &suffixes) {
+ bool cross = false;
+
+ QFile afile(filename);
+ if (afile.open(IO_ReadOnly)) {
+ QTextStream stream(&afile);
+ while (!stream.atEnd()) {
+ QString s = stream.readLine();
+ QStringList fields = QStringList::split(QRegExp("\\s"), s);
+
+ if (fields.count() == 4) {
+ cross = (fields[2] == "Y");
+ }
+ else {
+ if (fields.count() >= 5) {
+ AffEntry e;
+ e.cross = cross;
+ if (fields[2] == "0")
+ e.charsToRemove = 0;
+ else
+ e.charsToRemove = fields[2].length();
+ e.add = fields[3];
+
+ if (fields[4] != ".") {
+ QString condition = fields[4];
+ for (uint idx = 0; idx < condition.length(); ++idx) {
+ if (condition[idx] == '[') {
+ QString code;
+ for (++idx; (idx < condition.length()) && condition[idx] != ']'; ++idx)
+ code += condition[idx];
+ e.condition << code;
+ }
+ else
+ e.condition << QString(condition[idx]);
+ }
+ }
+
+ if (s.startsWith("PFX")) {
+ AffList list;
+ if (prefixes.contains (fields[1][0]))
+ list = prefixes[fields[1][0]];
+ list << e;
+ prefixes[fields[1][0]] = list;
+ }
+ else if (s.startsWith("SFX")) {
+ AffList list;
+ if (suffixes.contains (fields[1][0]))
+ list = suffixes[fields[1][0]];
+ list << e;
+ suffixes[fields[1][0]] = list;
+ }
+ }
+ }
+ }
+ }
+}
+
+/** Checks if the given word matches the given condition. Each entry of the
+ * QStringList "condition" describes one character of the word. (If the word
+ * has more characters than condition entries only the last characters are
+ * compared).
+ * Each entry contains either all valid characters (if it does _not_ start
+ * with "^") or all invalid characters (if it starts with "^").
+ */
+inline bool checkCondition (const QString &word, const QStringList &condition) {
+ if (condition.count() == 0)
+ return true;
+
+ if (word.length() < condition.count())
+ return false;
+
+ QStringList::ConstIterator it;
+ int idx;
+ for (it = condition.begin(), idx = word.length()-condition.count();
+ it != condition.end();
+ ++it, ++idx)
+ {
+ if ((*it).contains(word[idx]) == ((*it)[0] == '^'))
+ return false;
+ }
+ return true;
+}
+
+/** Constructs words by adding suffixes to the given word, and copies the
+ * resulting words from map to checkedMap.
+ * @param modifiers discribes which suffixes are valid
+ * @param cross true if the word has a prefix
+ */
+inline void checkWord(const QString &word, const QString &modifiers, bool cross, const WordMap &map, WordMap &checkedMap, const AffMap &suffixes) {
+ for (uint i = 0; i < modifiers.length(); i++) {
+ if (suffixes.contains(modifiers[i])) {
+ AffList sList = suffixes[modifiers[i]];
+
+ AffList::ConstIterator sIt;
+ for (sIt = sList.begin(); sIt != sList.end(); ++sIt) {
+ if (((*sIt).cross || !cross)
+ && (checkCondition(word, (*sIt).condition)))
+ {
+ QString sWord = word.left(word.length()-(*sIt).charsToRemove) + (*sIt).add;
+ if (map.contains(sWord))
+ checkedMap[sWord] = map[sWord];
+ }
+ }
+ }
+ }
+}
+
+/** Constructs words by adding pre- and suffixes to the given word, and
+ * copies the resulting words from map to checkedMap.
+ * @param modifiers discribes which pre- and suffixes are valid
+ */
+void checkWord (const QString &word, const QString &modifiers, const WordMap &map, WordMap &checkedMap, const AffMap &prefixes, const AffMap &suffixes) {
+ if (map.contains(word))
+ checkedMap[word] = map[word];
+
+ checkWord(word, modifiers, true, map, checkedMap, suffixes);
+
+ for (uint i = 0; i < modifiers.length(); i++) {
+ if (prefixes.contains(modifiers[i])) {
+ AffList pList = prefixes[modifiers[i]];
+
+ AffList::ConstIterator pIt;
+ for (pIt = pList.begin(); pIt != pList.end(); ++pIt) {
+ QString pWord = (*pIt).add + word;
+ if (map.contains(pWord))
+ checkedMap[pWord] = map[pWord];
+
+ checkWord(pWord, modifiers, false, map, checkedMap, suffixes);
+ }
+ }
+ }
+}
+
+WordMap spellCheck (WordMap map, QString dictionary, KProgressDialog *pdlg) {
+
+ if (dictionary.endsWith(".dic")) {
+ AffMap prefixes;
+ AffMap suffixes;
+ WordMap checkedMap;
+ loadAffFile (dictionary.left(dictionary.length()-4) + ".aff", prefixes, suffixes);
+
+ pdlg->progressBar()->reset();
+ pdlg->setAllowCancel (false);
+ pdlg->showCancelButton (false);
+ pdlg->setAutoReset(false);
+ pdlg->setAutoClose(false);
+ pdlg->setLabel (i18n("Performing spell check..."));
+ pdlg->progressBar()->setTotalSteps(100);
+ pdlg->progressBar()->setProgress(0);
+ qApp->processEvents (20);
+ int progress = 0;
+ int steps = 0;
+ int percent = 0;
+
+ QFile dfile(dictionary);
+ if (dfile.open(IO_ReadOnly)) {
+ QTextStream stream(&dfile);
+
+ if (!stream.atEnd()) {
+ QString s = stream.readLine(); // Number of words
+ steps = s.toInt();
+ }
+
+ while (!stream.atEnd()) {
+ QString s = stream.readLine();
+ if (s.contains("/")) {
+ QString word = s.left(s.find("/")).lower();
+ QString modifiers = s.right(s.length() - s.find("/"));
+
+ checkWord(word, modifiers, map, checkedMap, prefixes, suffixes);
+ }
+ else {
+ if (!s.isEmpty() && !s.isNull() && map.contains(s.lower()))
+ checkedMap[s.lower()] = map[s.lower()];
+ }
+
+ progress++;
+ if (steps != 0 && progress*100/steps > percent) {
+ percent = progress*100/steps;
+ pdlg->progressBar()->setProgress(percent);
+ qApp->processEvents (20);
+ }
+ }
+ }
+
+ return checkedMap;
+ }
+ else
+ return map;
+}
+
+}
+
diff --git a/kmouth/wordcompletion/wordlist.h b/kmouth/wordcompletion/wordlist.h
new file mode 100644
index 0000000..d2f85ac
--- /dev/null
+++ b/kmouth/wordcompletion/wordlist.h
@@ -0,0 +1,92 @@
+/***************************************************************************
+ wordlist.h - description
+ -------------------
+ begin : Don Sep 12 2002
+ 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. *
+ * *
+ ***************************************************************************/
+
+// $Id$
+
+#ifndef WORDLIST_H
+#define WORDLIST_H
+
+#include <qxml.h>
+#include <qvaluestack.h>
+#include <qstring.h>
+#include <qobject.h>
+#include <qmap.h>
+
+class QTextCodec;
+class KProgressDialog;
+
+namespace WordList {
+
+typedef QMap<QString,int> WordMap;
+
+KProgressDialog *progressDialog();
+
+WordMap parseKDEDoc (QString language, KProgressDialog *pdlg);
+WordMap parseFile (QString filename, QTextStream::Encoding encoding, QTextCodec *codec, KProgressDialog *pdlg);
+WordMap parseDir (QString directory, QTextStream::Encoding encoding, QTextCodec *codec, KProgressDialog *pdlg);
+WordMap mergeFiles (QMap<QString,int> files, KProgressDialog *pdlg);
+
+WordMap spellCheck (WordMap wordlist, QString dictionary, KProgressDialog *pdlg);
+
+bool saveWordList (WordMap map, QString filename);
+
+/**
+ * This class implements a parser for reading docbooks and generating word
+ * lists. It is intended to be used together with the Qt SAX2 framework.
+ * @author Gunnar Schmi Dt
+ */
+
+class XMLParser : public QXmlDefaultHandler {
+public:
+ XMLParser();
+ ~XMLParser();
+
+ bool warning (const QXmlParseException &exception);
+ bool error (const QXmlParseException &exception);
+ bool fatalError (const QXmlParseException &exception);
+ QString errorString();
+
+ /** Processes the start of the document. */
+ bool startDocument();
+
+ /** Processes the start tag of an element. */
+ bool startElement (const QString &, const QString &, const QString &name,
+ const QXmlAttributes &attributes);
+
+ /** Processes a chunk of normal character data. */
+ bool characters (const QString &ch);
+
+ /** Processes whitespace. */
+ bool ignorableWhitespace (const QString &ch);
+
+ /** Processes the end tag of an element. */
+ bool endElement (const QString &, const QString &, const QString &name);
+
+ /** Processes the end of the document. */
+ bool endDocument();
+
+ /** returns a list of words */
+ WordMap getList();
+
+private:
+ WordMap list;
+ QString text;
+};
+
+}
+
+#endif