Const'ify
[kdeaccessibility.git] / kmouth / wordcompletion / wordcompletionwidget.cpp
blobe40c280c6b254bccf6a88dc3383f5314ab6fc5d9
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 "wordcompletionwidget.h"
19 #include "wordcompletion.h"
20 #include "dictionarycreationwizard.h"
22 #include <QtGui/QLayout>
23 #include <QtGui/QLabel>
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QLineEdit>
27 #include <k3listview.h>
28 #include <klineedit.h>
29 #include <kurlrequester.h>
30 #include <klocale.h>
31 #include <kglobal.h>
32 #include <kstandarddirs.h>
33 #include <kconfig.h>
34 #include <kfiledialog.h>
35 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <klanguagebutton.h>
39 class DictionaryListItem : public K3ListViewItem {
40 public:
41 DictionaryListItem (Q3ListView *parent, QString filename, QString name, QString language, QString languageCode)
42 : K3ListViewItem (parent, name) {
43 setFilename (filename);
44 setLanguage (language, languageCode);
46 DictionaryListItem (Q3ListView *parent, QString filename, QString name, QString languageCode)
47 : K3ListViewItem (parent, name) {
48 setFilename (filename);
49 setLanguage (languageCode);
51 DictionaryListItem (Q3ListView *parent, Q3ListViewItem *after, QString filename, QString name, QString languageCode)
52 : K3ListViewItem (parent, after, name) {
53 setFilename (filename);
54 setLanguage (languageCode);
56 ~DictionaryListItem () {
59 QString filename() {
60 return myFilename;
63 QString languageCode() {
64 return myLanguageCode;
67 void setFilename(QString filename) {
68 myFilename = filename;
71 void setLanguage (QString languageCode) {
72 QString filename = KGlobal::dirs()->findResource("locale",
73 languageCode + QString::fromLatin1("/entry.desktop"));
75 KConfig entry(filename, KConfig::SimpleConfig);
76 KConfigGroup cg (&entry, "KCM Locale");
77 QString name = cg.readEntry(QString::fromLatin1("Name"), i18n("without name"));
78 setLanguage (name + " (" + languageCode + ')', languageCode);
81 void setLanguage (QString name, QString languageCode) {
82 myLanguageCode = languageCode;
83 setText (1, name);
86 private:
87 QString myFilename;
88 QString myLanguageCode;
91 /***************************************************************************/
93 WordCompletionWidget::WordCompletionWidget(QWidget *parent, const char *name)
94 : QWidget(parent)
96 setupUi(this);
97 setObjectName(name);
98 dictionaryList->setSorting (-1); // no sorted list
100 languageButton->showLanguageCodes(true);
101 languageButton->loadAllLanguages();
103 // Connect the signals from hte KCMKTTSDWidget to this class
104 connect (addButton, SIGNAL (clicked()), this, SLOT(addDictionary()) );
105 connect (deleteButton, SIGNAL (clicked()), this, SLOT (deleteDictionary()) );
106 connect (moveUpButton, SIGNAL (clicked()), this, SLOT (moveUp()) );
107 connect (moveDownButton, SIGNAL (clicked()), this, SLOT (moveDown()) );
108 connect (exportButton, SIGNAL (clicked()), this, SLOT (exportDictionary()) );
110 connect (dictionaryList, SIGNAL (selectionChanged()), this, SLOT (selectionChanged()) );
111 connect (dictionaryName, SIGNAL (textChanged (const QString &)), this, SLOT (nameChanged (const QString &)) );
112 connect (languageButton, SIGNAL (activated (const QString &)), this, SLOT (languageSelected()) );
114 // Object for the KCMKTTSD configuration
115 config = new KConfig("kmouthrc");
117 // Load the configuration from the file
118 load();
122 * Destructor
124 WordCompletionWidget::~WordCompletionWidget() {
125 delete config;
128 /***************************************************************************/
130 void WordCompletionWidget::load() {
131 dictionaryList->clear();
133 // Set the group general for the configuration of kttsd itself (no plug ins)
134 QStringList groups = config->groupList();
135 DictionaryListItem *last = 0;
136 for (QStringList::const_iterator it = groups.begin(); it != groups.end(); ++it)
137 if ((*it).startsWith ("Dictionary ")) {
138 KConfigGroup cg (config, *it);
139 QString languageTag = cg.readEntry("Language");
140 last = new DictionaryListItem (dictionaryList, last,
141 cg.readEntry("Filename"),
142 cg.readEntry("Name"),
143 languageTag);
144 if (!languageButton->contains(languageTag))
145 languageButton->insertLanguage(languageTag, i18n("without name"));
148 // Clean up disc space
149 for (QStringList::const_iterator it = newDictionaryFiles.begin(); it != newDictionaryFiles.end(); ++it) {
150 QString filename = KGlobal::dirs()->findResource ("appdata", *it);
151 if (!filename.isEmpty() && !filename.isNull())
152 QFile::remove (filename);
154 newDictionaryFiles.clear();
157 void WordCompletionWidget::save() {
158 QStringList groups = config->groupList();
159 for (QStringList::const_iterator it = groups.begin(); it != groups.end(); ++it)
160 if ((*it).startsWith ("Dictionary "))
161 config->deleteGroup (*it);
163 int number = 0;
164 Q3ListViewItemIterator it(dictionaryList);
165 while (it.current()) {
166 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
167 if (item != 0) {
168 KConfigGroup cg (config, QString("Dictionary %1").arg(number));
169 cg.writeEntry ("Filename", item->filename());
170 cg.writeEntry ("Name", item->text (0));
171 cg.writeEntry ("Language", item->languageCode());
172 number++;
174 ++it;
176 config->sync();
178 // Clean up disc space
179 for (QStringList::const_iterator it = removedDictionaryFiles.begin(); it != removedDictionaryFiles.end(); ++it) {
180 QString filename = KGlobal::dirs()->findResource ("appdata", *it);
181 if (!filename.isEmpty() && !filename.isNull())
182 QFile::remove (filename);
184 removedDictionaryFiles.clear();
187 /***************************************************************************/
189 void WordCompletionWidget::addDictionary() {
190 QStringList dictionaryNames;
191 QStringList dictionaryFiles;
192 QStringList dictionaryLanguages;
193 Q3ListViewItemIterator it(dictionaryList);
194 while (it.current()) {
195 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(it.current());
196 if (item != 0) {
197 dictionaryNames += item->text (0);
198 dictionaryFiles += item->filename();
199 dictionaryLanguages += item->languageCode();
201 ++it;
203 DictionaryCreationWizard *wizard = new DictionaryCreationWizard (this, "Dictionary creation wizard", dictionaryNames, dictionaryFiles, dictionaryLanguages);
204 if (wizard->exec() == QDialog::Accepted) {
205 QString filename = wizard->createDictionary();
206 newDictionaryFiles += filename;
207 QString languageTag = wizard->language();
208 if (!languageButton->contains(languageTag)) {
209 languageButton->insertLanguage(languageTag, i18n("without name"));
211 K3ListViewItem *item = new DictionaryListItem (dictionaryList,
212 filename, wizard->name(), languageTag);
213 dictionaryList->setSelected(item, true);
215 delete wizard;
218 void WordCompletionWidget::deleteDictionary() {
219 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
221 if (item != 0) {
222 removedDictionaryFiles += item->filename();
223 delete item;
227 void WordCompletionWidget::moveUp() {
228 Q3ListViewItem *item = dictionaryList->selectedItem ();
230 if (item != 0) {
231 Q3ListViewItem *above = item->itemAbove();
233 if (above != 0) {
234 above->moveItem (item);
239 void WordCompletionWidget::moveDown() {
240 Q3ListViewItem *item = dictionaryList->selectedItem ();
242 if (item != 0) {
243 Q3ListViewItem *next = item->itemBelow();
245 if (next != 0) {
246 item->moveItem (next);
251 void WordCompletionWidget::exportDictionary() {
252 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
254 if (item != 0) {
255 KUrl url = KFileDialog::getSaveUrl(QString(), QString(), this, i18n("Export Dictionary"));
256 if (url.isEmpty() || !url.isValid())
257 return;
259 if (KIO::NetAccess::exists(url, KIO::NetAccess::DestinationSide, this)) {
260 if (KMessageBox::warningContinueCancel(0,QString("<qt>%1</qt>").arg(i18n("The file %1 already exists. "
261 "Do you want to overwrite it?", url.url())),i18n("File Exists"),KGuiItem(i18n("&Overwrite")))==KMessageBox::Cancel) {
262 return;
265 KUrl src;
266 src.setPath( KGlobal::dirs()->findResource ("appdata", item->filename()) );
267 KIO::NetAccess::copy (src, url, this);
271 void WordCompletionWidget::selectionChanged() {
272 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
274 if (item != 0) {
275 deleteButton->setEnabled(true);
276 moveUpButton->setEnabled(true);
277 moveDownButton->setEnabled(true);
278 exportButton->setEnabled(true);
279 selectedDictionaryDetails->setEnabled(true);
280 languageLabel->setEnabled(true);
281 dictionaryNameLabel->setEnabled(true);
282 dictionaryName->setEnabled(true);
283 languageButton->setEnabled(true);
285 dictionaryName->setText(item->text(0));
286 languageButton->setCurrentItem(item->languageCode());
288 else {
289 deleteButton->setEnabled(false);
290 moveUpButton->setEnabled(false);
291 moveDownButton->setEnabled(false);
292 exportButton->setEnabled(false);
293 selectedDictionaryDetails->setEnabled(false);
294 languageLabel->setEnabled(false);
295 dictionaryNameLabel->setEnabled(false);
296 dictionaryName->setEnabled(false);
297 languageButton->setEnabled(false);
299 dictionaryName->setText("");
303 void WordCompletionWidget::nameChanged (const QString &text) {
304 Q3ListViewItem *item = dictionaryList->selectedItem ();
306 if (item != 0) {
307 QString old = item->text(0);
309 if (old != text) {
310 item->setText(0, text);
311 emit changed(true);
316 void WordCompletionWidget::languageSelected () {
317 DictionaryListItem *item = dynamic_cast<DictionaryListItem*>(dictionaryList->selectedItem ());
319 if (item != 0) {
320 QString old = item->text(1);
321 QString text = languageButton->current();
323 if (old != text) {
324 item->setLanguage(languageButton->current(), text);
325 emit changed(true);
330 #include "wordcompletionwidget.moc"