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 ***************************************************************************/
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>
32 #include <kstandarddirs.h>
34 #include <kfiledialog.h>
35 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <klanguagebutton.h>
39 class DictionaryListItem
: public K3ListViewItem
{
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 () {
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
;
88 QString myLanguageCode
;
91 /***************************************************************************/
93 WordCompletionWidget::WordCompletionWidget(QWidget
*parent
, const char *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
124 WordCompletionWidget::~WordCompletionWidget() {
128 /***************************************************************************/
130 void WordCompletionWidget::load() {
131 dictionaryList
->clear();
133 // Set the group general for the configuration of kttsd itself (no plug ins)
134 const QStringList groups
= config
->groupList();
135 DictionaryListItem
*last
= 0;
136 for (QStringList::const_iterator it
= groups
.constBegin(); it
!= groups
.constEnd(); ++it
)
137 if ((*it
).startsWith (QString("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"),
144 if (!languageButton
->contains(languageTag
))
145 languageButton
->insertLanguage(languageTag
, i18n("without name"));
148 // Clean up disc space
149 for (QStringList::const_iterator it
= newDictionaryFiles
.constBegin(); it
!= newDictionaryFiles
.constEnd(); ++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 const QStringList groups
= config
->groupList();
159 for (QStringList::const_iterator it
= groups
.constBegin(); it
!= groups
.constEnd(); ++it
)
160 if ((*it
).startsWith (QString("Dictionary ")))
161 config
->deleteGroup (*it
);
164 Q3ListViewItemIterator
it(dictionaryList
);
165 while (it
.current()) {
166 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(it
.current());
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());
178 // Clean up disc space
179 for (QStringList::const_iterator it
= removedDictionaryFiles
.constBegin(); it
!= removedDictionaryFiles
.constEnd(); ++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());
197 dictionaryNames
+= item
->text (0);
198 dictionaryFiles
+= item
->filename();
199 dictionaryLanguages
+= item
->languageCode();
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);
218 void WordCompletionWidget::deleteDictionary() {
219 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
222 removedDictionaryFiles
+= item
->filename();
227 void WordCompletionWidget::moveUp() {
228 Q3ListViewItem
*item
= dictionaryList
->selectedItem ();
231 Q3ListViewItem
*above
= item
->itemAbove();
234 above
->moveItem (item
);
239 void WordCompletionWidget::moveDown() {
240 Q3ListViewItem
*item
= dictionaryList
->selectedItem ();
243 Q3ListViewItem
*next
= item
->itemBelow();
246 item
->moveItem (next
);
251 void WordCompletionWidget::exportDictionary() {
252 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
255 KUrl url
= KFileDialog::getSaveUrl(QString(), QString(), this, i18n("Export Dictionary"));
256 if (url
.isEmpty() || !url
.isValid())
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
) {
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 ());
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());
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 ();
307 QString old
= item
->text(0);
310 item
->setText(0, text
);
316 void WordCompletionWidget::languageSelected () {
317 DictionaryListItem
*item
= dynamic_cast<DictionaryListItem
*>(dictionaryList
->selectedItem ());
320 QString old
= item
->text(1);
321 QString text
= languageButton
->current();
324 item
->setLanguage(languageButton
->current(), text
);
330 #include "wordcompletionwidget.moc"