1 /***************************************************************************
2 wordcompletionwidget.cpp - description
4 begin : Tue Apr 29 2003
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
20 #include <qcheckbox.h>
21 #include <qlineedit.h>
23 #include <klistview.h>
24 #include <klineedit.h>
25 #include <kurlrequester.h>
28 #include <kstandarddirs.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
{
42 DictionaryListItem (QListView
*parent
, QString filename
, QString name
, QString language
, QString languageCode
)
43 : KListViewItem (parent
, name
) {
44 setFilename (filename
);
45 setLanguage (language
, languageCode
);
47 DictionaryListItem (QListView
*parent
, QString filename
, QString name
, QString languageCode
)
48 : KListViewItem (parent
, name
) {
49 setFilename (filename
);
50 setLanguage (languageCode
);
52 DictionaryListItem (QListView
*parent
, QListViewItem
*after
, QString filename
, QString name
, QString languageCode
)
53 : KListViewItem (parent
, after
, name
) {
54 setFilename (filename
);
55 setLanguage (languageCode
);
57 ~DictionaryListItem () {
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
;
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
118 WordCompletionWidget::~WordCompletionWidget() {
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"),
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
);
158 QListViewItemIterator
it(dictionaryList
);
159 while (it
.current()) {
160 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(it
.current());
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());
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 QListViewItemIterator
it(dictionaryList
);
188 while (it
.current()) {
189 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(it
.current());
191 dictionaryNames
+= item
->text (0);
192 dictionaryFiles
+= item
->filename();
193 dictionaryLanguages
+= item
->languageCode();
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);
212 void WordCompletionWidget::deleteDictionary() {
213 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
216 removedDictionaryFiles
+= item
->filename();
221 void WordCompletionWidget::moveUp() {
222 QListViewItem
*item
= dictionaryList
->selectedItem ();
225 QListViewItem
*above
= item
->itemAbove();
228 above
->moveItem (item
);
233 void WordCompletionWidget::moveDown() {
234 QListViewItem
*item
= dictionaryList
->selectedItem ();
237 QListViewItem
*next
= item
->itemBelow();
240 item
->moveItem (next
);
245 void WordCompletionWidget::exportDictionary() {
246 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
249 KURL url
= KFileDialog::getSaveURL(QString::null
, QString::null
, this, i18n("Export Dictionary"));
250 if (url
.isEmpty() || !url
.isValid())
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
) {
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 ());
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());
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 QListViewItem
*item
= dictionaryList
->selectedItem ();
302 QString old
= item
->text(0);
305 item
->setText(0, text
);
311 void WordCompletionWidget::languageSelected (int) {
312 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
315 QString old
= item
->text(1);
316 QString text
= languageButton
->currentTag();
319 item
->setLanguage(languageButton
->text(), text
);
325 #include "wordcompletionwidget.moc"