Fix some error
[kdeaccessibility.git] / kmouth / wordcompletion / wordcompletionwidget.cpp
blob1a2be1892ad78d0606e73a683a064cab5fea17f5
1 /***************************************************************************
2 wordcompletionwidget.cpp - description
3 -------------------
4 begin : Tue Apr 29 2003
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <qlayout.h>
19 #include <qlabel.h>
20 #include <qcheckbox.h>
21 #include <qlineedit.h>
23 #include <klistview.h>
24 #include <klineedit.h>
25 #include <kurlrequester.h>
26 #include <klocale.h>
27 #include <kglobal.h>
28 #include <kstandarddirs.h>
29 #include <kconfig.h>
30 #include <ksimpleconfig.h>
31 #include <kfiledialog.h>
32 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
35 #include "dictionarycreationwizard.h"
36 #include "wordcompletionwidget.h"
37 #include "wordcompletion.h"
38 #include "klanguagebutton.h"
40 class DictionaryListItem : public KListViewItem {
41 public:
42 DictionaryListItem (Q3ListView *parent, QString filename, QString name, QString language, QString languageCode)
43 : KListViewItem (parent, name) {
44 setFilename (filename);
45 setLanguage (language, languageCode);
47 DictionaryListItem (Q3ListView *parent, QString filename, QString name, QString languageCode)
48 : KListViewItem (parent, name) {
49 setFilename (filename);
50 setLanguage (languageCode);
52 DictionaryListItem (Q3ListView *parent, Q3ListViewItem *after, QString filename, QString name, QString languageCode)
53 : KListViewItem (parent, after, name) {
54 setFilename (filename);
55 setLanguage (languageCode);
57 ~DictionaryListItem () {
60 QString filename() {
61 return myFilename;
64 QString languageCode() {
65 return myLanguageCode;
68 void setFilename(QString filename) {
69 myFilename = filename;
72 void setLanguage (QString languageCode) {
73 QString filename = KGlobal::dirs()->findResource("locale",
74 languageCode + QString::fromLatin1("/entry.desktop"));
76 KSimpleConfig entry(filename);
77 entry.setGroup(QString::fromLatin1("KCM Locale"));
78 QString name = entry.readEntry(QString::fromLatin1("Name"), i18n("without name"));
79 setLanguage (name + " (" + languageCode + ")", languageCode);
82 void setLanguage (QString name, QString languageCode) {
83 myLanguageCode = languageCode;
84 setText (1, name);
87 private:
88 QString myFilename;
89 QString myLanguageCode;
92 /***************************************************************************/
94 WordCompletionWidget::WordCompletionWidget(QWidget *parent, const char *name) : WordCompletionUI (parent, name) {
95 dictionaryList->setSorting (-1); // no sorted list
97 // Connect the signals from hte KCMKTTSDWidget to this class
98 connect (addButton, SIGNAL (clicked()), this, SLOT(addDictionary()) );
99 connect (deleteButton, SIGNAL (clicked()), this, SLOT (deleteDictionary()) );
100 connect (moveUpButton, SIGNAL (clicked()), this, SLOT (moveUp()) );
101 connect (moveDownButton, SIGNAL (clicked()), this, SLOT (moveDown()) );
102 connect (exportButton, SIGNAL (clicked()), this, SLOT (exportDictionary()) );
104 connect (dictionaryList, SIGNAL (selectionChanged()), this, SLOT (selectionChanged()) );
105 connect (dictionaryName, SIGNAL (textChanged (const QString &)), this, SLOT (nameChanged (const QString &)) );
106 connect (languageButton, SIGNAL (activated (int)), this, SLOT (languageSelected(int)) );
108 // Object for the KCMKTTSD configuration
109 config = new KConfig("kmouthrc");
111 // Load the configuration from the file
112 load();
116 * Destructor
118 WordCompletionWidget::~WordCompletionWidget() {
119 delete config;
122 /***************************************************************************/
124 void WordCompletionWidget::load() {
125 dictionaryList->clear();
127 // Set the group general for the configuration of kttsd itself (no plug ins)
128 QStringList groups = config->groupList();
129 DictionaryListItem *last = 0;
130 for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
131 if ((*it).startsWith ("Dictionary ")) {
132 config->setGroup(*it);
133 QString languageTag = config->readEntry("Language");
134 last = new DictionaryListItem (dictionaryList, last,
135 config->readEntry("Filename"),
136 config->readEntry("Name"),
137 languageTag);
138 if (!languageButton->containsTag(languageTag))
139 languageButton->insertLanguage(languageTag, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
142 // Clean up disc space
143 for (QStringList::Iterator it = newDictionaryFiles.begin(); it != newDictionaryFiles.end(); ++it) {
144 QString filename = KGlobal::dirs()->findResource ("appdata", *it);
145 if (!filename.isEmpty() && !filename.isNull())
146 QFile::remove (filename);
148 newDictionaryFiles.clear();
151 void WordCompletionWidget::save() {
152 QStringList groups = config->groupList();
153 for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
154 if ((*it).startsWith ("Dictionary "))
155 config->deleteGroup (*it);
157 int number = 0;
158 Q3ListViewItemIterator it(dictionaryList);
159 while (it.current()) {
160 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
161 if (item != 0) {
162 config->setGroup(QString("Dictionary %1").arg(number));
163 config->writeEntry ("Filename", item->filename());
164 config->writeEntry ("Name", item->text (0));
165 config->writeEntry ("Language", item->languageCode());
166 number++;
168 ++it;
170 config->sync();
172 // Clean up disc space
173 for (QStringList::Iterator it = removedDictionaryFiles.begin(); it != removedDictionaryFiles.end(); ++it) {
174 QString filename = KGlobal::dirs()->findResource ("appdata", *it);
175 if (!filename.isEmpty() && !filename.isNull())
176 QFile::remove (filename);
178 removedDictionaryFiles.clear();
181 /***************************************************************************/
183 void WordCompletionWidget::addDictionary() {
184 QStringList dictionaryNames;
185 QStringList dictionaryFiles;
186 QStringList dictionaryLanguages;
187 Q3ListViewItemIterator it(dictionaryList);
188 while (it.current()) {
189 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
190 if (item != 0) {
191 dictionaryNames += item->text (0);
192 dictionaryFiles += item->filename();
193 dictionaryLanguages += item->languageCode();
195 ++it;
197 DictionaryCreationWizard *wizard = new DictionaryCreationWizard (this, "Dictionary creation wizard", dictionaryNames, dictionaryFiles, dictionaryLanguages);
198 if (wizard->exec() == QDialog::Accepted) {
199 QString filename = wizard->createDictionary();
200 newDictionaryFiles += filename;
201 QString languageTag = wizard->language();
202 if (!languageButton->containsTag(languageTag)) {
203 languageButton->insertLanguage(languageTag, i18n("without name"), QString::fromLatin1("l10n/"), QString::null);
205 KListViewItem *item = new DictionaryListItem (dictionaryList,
206 filename, wizard->name(), languageTag);
207 dictionaryList->setSelected(item, true);
209 delete wizard;
212 void WordCompletionWidget::deleteDictionary() {
213 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
215 if (item != 0) {
216 removedDictionaryFiles += item->filename();
217 delete item;
221 void WordCompletionWidget::moveUp() {
222 Q3ListViewItem *item = dictionaryList->selectedItem ();
224 if (item != 0) {
225 Q3ListViewItem *above = item->itemAbove();
227 if (above != 0) {
228 above->moveItem (item);
233 void WordCompletionWidget::moveDown() {
234 Q3ListViewItem *item = dictionaryList->selectedItem ();
236 if (item != 0) {
237 Q3ListViewItem *next = item->itemBelow();
239 if (next != 0) {
240 item->moveItem (next);
245 void WordCompletionWidget::exportDictionary() {
246 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
248 if (item != 0) {
249 KURL url = KFileDialog::getSaveURL(QString::null, QString::null, this, i18n("Export Dictionary"));
250 if (url.isEmpty() || !url.isValid())
251 return;
253 if (KIO::NetAccess::exists(url, false, this)) {
254 if (KMessageBox::warningContinueCancel(0,QString("<qt>%1</qt>").arg(i18n("The file %1 already exists. "
255 "Do you want to overwrite it?").arg(url.url())),i18n("File Exists"),i18n("&Overwrite"))==KMessageBox::Cancel) {
256 return;
259 KURL src;
260 src.setPath( KGlobal::dirs()->findResource ("appdata", item->filename()) );
261 KIO::NetAccess::copy (src, url, this);
265 void WordCompletionWidget::selectionChanged() {
266 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
268 if (item != 0) {
269 deleteButton->setEnabled(true);
270 moveUpButton->setEnabled(true);
271 moveDownButton->setEnabled(true);
272 exportButton->setEnabled(true);
273 selectedDictionaryDetails->setEnabled(true);
274 languageLabel->setEnabled(true);
275 dictionaryNameLabel->setEnabled(true);
276 dictionaryName->setEnabled(true);
277 languageButton->setEnabled(true);
279 dictionaryName->setText(item->text(0));
280 languageButton->setCurrentItem(item->languageCode());
282 else {
283 deleteButton->setEnabled(false);
284 moveUpButton->setEnabled(false);
285 moveDownButton->setEnabled(false);
286 exportButton->setEnabled(false);
287 selectedDictionaryDetails->setEnabled(false);
288 languageLabel->setEnabled(false);
289 dictionaryNameLabel->setEnabled(false);
290 dictionaryName->setEnabled(false);
291 languageButton->setEnabled(false);
293 dictionaryName->setText("");
294 languageButton->setText("");
298 void WordCompletionWidget::nameChanged (const QString &text) {
299 Q3ListViewItem *item = dictionaryList->selectedItem ();
301 if (item != 0) {
302 QString old = item->text(0);
304 if (old != text) {
305 item->setText(0, text);
306 emit changed(true);
311 void WordCompletionWidget::languageSelected (int) {
312 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
314 if (item != 0) {
315 QString old = item->text(1);
316 QString text = languageButton->currentTag();
318 if (old != text) {
319 item->setLanguage(languageButton->text(), text);
320 emit changed(true);
325 #include "wordcompletionwidget.moc"