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 ***************************************************************************/
23 #include <k3listview.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>
34 #include <klanguagebutton.h>
35 #include <klanguagebuttonhelper.h>
37 #include "dictionarycreationwizard.h"
38 #include "wordcompletionwidget.h"
39 #include "wordcompletion.h"
41 class DictionaryListItem
: public K3ListViewItem
{
43 DictionaryListItem (Q3ListView
*parent
, QString filename
, QString name
, QString language
, QString languageCode
)
44 : K3ListViewItem (parent
, name
) {
45 setFilename (filename
);
46 setLanguage (language
, languageCode
);
48 DictionaryListItem (Q3ListView
*parent
, QString filename
, QString name
, QString languageCode
)
49 : K3ListViewItem (parent
, name
) {
50 setFilename (filename
);
51 setLanguage (languageCode
);
53 DictionaryListItem (Q3ListView
*parent
, Q3ListViewItem
*after
, QString filename
, QString name
, QString languageCode
)
54 : K3ListViewItem (parent
, after
, name
) {
55 setFilename (filename
);
56 setLanguage (languageCode
);
58 ~DictionaryListItem () {
65 QString
languageCode() {
66 return myLanguageCode
;
69 void setFilename(QString filename
) {
70 myFilename
= filename
;
73 void setLanguage (QString languageCode
) {
74 QString filename
= KGlobal::dirs()->findResource("locale",
75 languageCode
+ QString::fromLatin1("/entry.desktop"));
77 KSimpleConfig
entry(filename
);
78 entry
.setGroup(QString::fromLatin1("KCM Locale"));
79 QString name
= entry
.readEntry(QString::fromLatin1("Name"), i18n("without name"));
80 setLanguage (name
+ " (" + languageCode
+ ")", languageCode
);
83 void setLanguage (QString name
, QString languageCode
) {
84 myLanguageCode
= languageCode
;
90 QString myLanguageCode
;
93 /***************************************************************************/
95 WordCompletionWidget::WordCompletionWidget(QWidget
*parent
, const char *name
)
100 dictionaryList
->setSorting (-1); // no sorted list
102 loadLanguageList(languageButton
);
104 // Connect the signals from hte KCMKTTSDWidget to this class
105 connect (addButton
, SIGNAL (clicked()), this, SLOT(addDictionary()) );
106 connect (deleteButton
, SIGNAL (clicked()), this, SLOT (deleteDictionary()) );
107 connect (moveUpButton
, SIGNAL (clicked()), this, SLOT (moveUp()) );
108 connect (moveDownButton
, SIGNAL (clicked()), this, SLOT (moveDown()) );
109 connect (exportButton
, SIGNAL (clicked()), this, SLOT (exportDictionary()) );
111 connect (dictionaryList
, SIGNAL (selectionChanged()), this, SLOT (selectionChanged()) );
112 connect (dictionaryName
, SIGNAL (textChanged (const QString
&)), this, SLOT (nameChanged (const QString
&)) );
113 connect (languageButton
, SIGNAL (activated (const QString
&)), this, SLOT (languageSelected()) );
115 // Object for the KCMKTTSD configuration
116 config
= new KConfig("kmouthrc");
118 // Load the configuration from the file
125 WordCompletionWidget::~WordCompletionWidget() {
129 /***************************************************************************/
131 void WordCompletionWidget::load() {
132 dictionaryList
->clear();
134 // Set the group general for the configuration of kttsd itself (no plug ins)
135 QStringList groups
= config
->groupList();
136 DictionaryListItem
*last
= 0;
137 for (QStringList::Iterator it
= groups
.begin(); it
!= groups
.end(); ++it
)
138 if ((*it
).startsWith ("Dictionary ")) {
139 config
->setGroup(*it
);
140 QString languageTag
= config
->readEntry("Language");
141 last
= new DictionaryListItem (dictionaryList
, last
,
142 config
->readEntry("Filename"),
143 config
->readEntry("Name"),
145 if (!languageButton
->contains(languageTag
))
146 languageButton
->insertLanguage(languageTag
, i18n("without name"), QString::fromLatin1("l10n/"), QString());
149 // Clean up disc space
150 for (QStringList::Iterator it
= newDictionaryFiles
.begin(); it
!= newDictionaryFiles
.end(); ++it
) {
151 QString filename
= KGlobal::dirs()->findResource ("appdata", *it
);
152 if (!filename
.isEmpty() && !filename
.isNull())
153 QFile::remove (filename
);
155 newDictionaryFiles
.clear();
158 void WordCompletionWidget::save() {
159 QStringList groups
= config
->groupList();
160 for (QStringList::Iterator it
= groups
.begin(); it
!= groups
.end(); ++it
)
161 if ((*it
).startsWith ("Dictionary "))
162 config
->deleteGroup (*it
);
165 Q3ListViewItemIterator
it(dictionaryList
);
166 while (it
.current()) {
167 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(it
.current());
169 config
->setGroup(QString("Dictionary %1").arg(number
));
170 config
->writeEntry ("Filename", item
->filename());
171 config
->writeEntry ("Name", item
->text (0));
172 config
->writeEntry ("Language", item
->languageCode());
179 // Clean up disc space
180 for (QStringList::Iterator it
= removedDictionaryFiles
.begin(); it
!= removedDictionaryFiles
.end(); ++it
) {
181 QString filename
= KGlobal::dirs()->findResource ("appdata", *it
);
182 if (!filename
.isEmpty() && !filename
.isNull())
183 QFile::remove (filename
);
185 removedDictionaryFiles
.clear();
188 /***************************************************************************/
190 void WordCompletionWidget::addDictionary() {
191 QStringList dictionaryNames
;
192 QStringList dictionaryFiles
;
193 QStringList dictionaryLanguages
;
194 Q3ListViewItemIterator
it(dictionaryList
);
195 while (it
.current()) {
196 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(it
.current());
198 dictionaryNames
+= item
->text (0);
199 dictionaryFiles
+= item
->filename();
200 dictionaryLanguages
+= item
->languageCode();
204 DictionaryCreationWizard
*wizard
= new DictionaryCreationWizard (this, "Dictionary creation wizard", dictionaryNames
, dictionaryFiles
, dictionaryLanguages
);
205 if (wizard
->exec() == QDialog::Accepted
) {
206 QString filename
= wizard
->createDictionary();
207 newDictionaryFiles
+= filename
;
208 QString languageTag
= wizard
->language();
209 if (!languageButton
->contains(languageTag
)) {
210 languageButton
->insertLanguage(languageTag
, i18n("without name"), QString::fromLatin1("l10n/"), QString());
212 K3ListViewItem
*item
= new DictionaryListItem (dictionaryList
,
213 filename
, wizard
->name(), languageTag
);
214 dictionaryList
->setSelected(item
, true);
219 void WordCompletionWidget::deleteDictionary() {
220 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
223 removedDictionaryFiles
+= item
->filename();
228 void WordCompletionWidget::moveUp() {
229 Q3ListViewItem
*item
= dictionaryList
->selectedItem ();
232 Q3ListViewItem
*above
= item
->itemAbove();
235 above
->moveItem (item
);
240 void WordCompletionWidget::moveDown() {
241 Q3ListViewItem
*item
= dictionaryList
->selectedItem ();
244 Q3ListViewItem
*next
= item
->itemBelow();
247 item
->moveItem (next
);
252 void WordCompletionWidget::exportDictionary() {
253 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
256 KUrl url
= KFileDialog::getSaveURL(QString(), QString(), this, i18n("Export Dictionary"));
257 if (url
.isEmpty() || !url
.isValid())
260 if (KIO::NetAccess::exists(url
, false, this)) {
261 if (KMessageBox::warningContinueCancel(0,QString("<qt>%1</qt>").arg(i18n("The file %1 already exists. "
262 "Do you want to overwrite it?", url
.url())),i18n("File Exists"),i18n("&Overwrite"))==KMessageBox::Cancel
) {
267 src
.setPath( KGlobal::dirs()->findResource ("appdata", item
->filename()) );
268 KIO::NetAccess::copy (src
, url
, this);
272 void WordCompletionWidget::selectionChanged() {
273 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
276 deleteButton
->setEnabled(true);
277 moveUpButton
->setEnabled(true);
278 moveDownButton
->setEnabled(true);
279 exportButton
->setEnabled(true);
280 selectedDictionaryDetails
->setEnabled(true);
281 languageLabel
->setEnabled(true);
282 dictionaryNameLabel
->setEnabled(true);
283 dictionaryName
->setEnabled(true);
284 languageButton
->setEnabled(true);
286 dictionaryName
->setText(item
->text(0));
287 languageButton
->setCurrentItem(item
->languageCode());
290 deleteButton
->setEnabled(false);
291 moveUpButton
->setEnabled(false);
292 moveDownButton
->setEnabled(false);
293 exportButton
->setEnabled(false);
294 selectedDictionaryDetails
->setEnabled(false);
295 languageLabel
->setEnabled(false);
296 dictionaryNameLabel
->setEnabled(false);
297 dictionaryName
->setEnabled(false);
298 languageButton
->setEnabled(false);
300 dictionaryName
->setText("");
301 languageButton
->setText("");
305 void WordCompletionWidget::nameChanged (const QString
&text
) {
306 Q3ListViewItem
*item
= dictionaryList
->selectedItem ();
309 QString old
= item
->text(0);
312 item
->setText(0, text
);
318 void WordCompletionWidget::languageSelected () {
319 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
322 QString old
= item
->text(1);
323 QString text
= languageButton
->current();
326 item
->setLanguage(languageButton
->current(), text
);
332 #include "wordcompletionwidget.moc"