1/***************************************************************************
2 optionsdialog.cpp - description
3 -------------------
4 begin : Don Nov 21 2002
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
8
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 ***************************************************************************/
17
18#include "optionsdialog.h"
19#include "wordcompletion/wordcompletionwidget.h"
20
21#include "texttospeechconfigurationwidget.h"
22#include "speech.h"
23
24#include <QtGui/QLayout>
25#include <QtGui/QLabel>
26#include <QtGui/QPixmap>
27#include <QtCore/QFile>
28
29#include <kcombobox.h>
30#include <ktabwidget.h>
31#include <klocale.h>
32#include <kconfig.h>
33#include <kglobal.h>
34#include <kcmodule.h>
35#include <klibloader.h>
36#include <kicon.h>
37#include <kpagewidgetmodel.h>
38#include <kparts/componentfactory.h>
39
40PreferencesWidget::PreferencesWidget (QWidget *parent, const char *name)
41 : QWidget (parent)
42{
43 setObjectName( QLatin1String( name ) );
44 setupUi(this);
45 speakCombo->setCurrentIndex (1);
46 speak = false;
47
48 closeCombo->setCurrentIndex (2);
49 save = 2;
50}
51
52PreferencesWidget::~PreferencesWidget() {
53}
54
55void PreferencesWidget::cancel() {
56 if (speak)
57 speakCombo->setCurrentIndex (0);
58 else
59 speakCombo->setCurrentIndex (1);
60 closeCombo->setCurrentIndex (save);
61}
62
63void PreferencesWidget::ok() {
64 speak = speakCombo->currentIndex () == 0;
65 save = closeCombo->currentIndex ();
66}
67
68void PreferencesWidget::readOptions (KConfig *config) {
69 KConfigGroup cg ( config,"Preferences");
70 if (cg.hasKey("AutomaticSpeak"))
71 if (cg.readEntry ("AutomaticSpeak") == QLatin1String( "Yes" ))
72 speak = true;
73 else
74 speak = false;
75 else
76 speak = false;
77
78 KConfigGroup cg2 ( config ,"Notification Messages");
79 if (cg2.hasKey("AutomaticSave"))
80 if (cg2.readEntry ("AutomaticSave") == QLatin1String( "Yes" ))
81 save = 0;
82 else
83 save = 1;
84 else
85 save = 2;
86
87 if (speak)
88 speakCombo->setCurrentIndex (0);
89 else
90 speakCombo->setCurrentIndex (1);
91 closeCombo->setCurrentIndex (save);
92}
93
94void PreferencesWidget::saveOptions (KConfig *config) {
95 KConfigGroup cg (config, "Preferences");
96 if (speak)
97 cg.writeEntry ("AutomaticSpeak", "Yes");
98 else
99 cg.writeEntry ("AutomaticSpeak", "No");
100
101 KConfigGroup cg2 (config, "Notification Messages");
102 if (save == 0)
103 cg2.writeEntry ("AutomaticSave", "Yes");
104 else if (save == 1)
105 cg2.writeEntry ("AutomaticSave", "No");
106 else
107 cg2.deleteEntry ("AutomaticSave");
108}
109
110bool PreferencesWidget::isSpeakImmediately () {
111 return speak;
112}
113
114/***************************************************************************/
115
116OptionsDialog::OptionsDialog (QWidget *parent)
117 : KPageDialog(parent)
118{
119 setCaption(i18n("Configuration"));
120 setButtons(KDialog::Ok|KDialog::Apply|KDialog::Cancel|KDialog::Help);
121 setFaceType(KPageDialog::List);
122 setHelp (QLatin1String( "config-dialog" ));
123
124
125 //addGridPage (1, Qt::Horizontal, i18n("General Options"), QString(), iconGeneral);
126
127 tabCtl = new KTabWidget();
128 tabCtl->setObjectName( QLatin1String("general" ));
129
130 behaviourWidget = new PreferencesWidget (tabCtl, "prefPage");
131 behaviourWidget->layout()->setMargin(KDialog::marginHint());
132 tabCtl->addTab (behaviourWidget, i18n("&Preferences"));
133
134 commandWidget = new TextToSpeechConfigurationWidget (tabCtl, "ttsTab");
135 commandWidget->layout()->setMargin(KDialog::marginHint());
136 tabCtl->addTab (commandWidget, i18n("&Text-to-Speech"));
137
138 KPageWidgetItem *pageGeneral = new KPageWidgetItem(tabCtl, i18n("General Options"));
139 pageGeneral->setHeader(i18n("General Options"));
140 pageGeneral->setIcon(KIcon( QLatin1String( "configure" )));
141 addPage(pageGeneral);
142
143 completionWidget = new WordCompletionWidget(0, "Word Completion widget");
144 KPageWidgetItem *pageCompletion = new KPageWidgetItem(completionWidget, i18n("Word Completion"));
145 pageCompletion->setHeader(i18n("Word Completion"));
146 pageCompletion->setIcon(KIcon( QLatin1String( "keyboard" )));
147 addPage(pageCompletion);
148
149 kttsd = loadKttsd();
150 if (kttsd != 0) {
151 KPageWidgetItem *pageKttsd = new KPageWidgetItem(kttsd, i18n("Jovie Speech Service"));
152 pageKttsd->setIcon(KIcon( QLatin1String( "multimedia" )));
153 pageKttsd->setHeader(i18n("KDE Text-to-Speech Daemon Configuration"));
154 addPage(pageKttsd);
155 }
156
157 setDefaultButton(KDialog::Cancel);
158
159 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
160 connect(this, SIGNAL(cancelClicked()), this, SLOT(slotCancel()));
161 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
162}
163
164OptionsDialog::~OptionsDialog() {
165 unloadKttsd();
166}
167
168void OptionsDialog::slotCancel() {
169// KDialog::slotCancel();
170 commandWidget->cancel();
171 behaviourWidget->cancel();
172 completionWidget->load();
173 if (kttsd != 0)
174 kttsd->load ();
175}
176
177void OptionsDialog::slotOk() {
178// KDialog::slotOk();
179 commandWidget->ok();
180 behaviourWidget->ok();
181 completionWidget->save();
182 emit configurationChanged();
183 if (kttsd != 0)
184 kttsd->save ();
185
186}
187
188void OptionsDialog::slotApply() {
189// KDialog::slotApply();
190 commandWidget->ok();
191 behaviourWidget->ok();
192 completionWidget->save();
193 emit configurationChanged();
194 if (kttsd != 0)
195 kttsd->save ();
196}
197
198TextToSpeechSystem *OptionsDialog::getTTSSystem() const {
199 return commandWidget->getTTSSystem();
200}
201
202void OptionsDialog::readOptions (KConfig *config) {
203 commandWidget->readOptions (config, QLatin1String( "TTS System" ));
204 behaviourWidget->readOptions (config);
205}
206
207void OptionsDialog::saveOptions (KConfig *config) {
208 commandWidget->saveOptions (config, QLatin1String( "TTS System" ));
209 behaviourWidget->saveOptions (config);
210 config->sync();
211}
212
213bool OptionsDialog::isSpeakImmediately () {
214 return behaviourWidget->isSpeakImmediately ();
215}
216
217KCModule *OptionsDialog::loadKttsd () {
218 KLibLoader *loader = KLibLoader::self();
219
220 QString libname = QLatin1String( "kcm_kttsd" );
221 KLibrary *lib = loader->library(QLatin1String( QFile::encodeName(libname) ));
222
223 if (lib == 0) {
224 libname = QLatin1String( "libkcm_kttsd" );
225 lib = loader->library(QLatin1String( QFile::encodeName(QLatin1String( "libkcm_kttsd" )) ));
226 }
227
228 if (lib != 0) {
229 QString initSym(QLatin1String( "init_" ));
230 initSym += libname;
231
232 if (lib->resolveFunction(QFile::encodeName(initSym))) {
233 // Reuse "lib" instead of letting createInstanceFromLibrary recreate it
234 KLibFactory *factory = lib->factory();
235 if (factory != 0) {
236 KCModule *module = factory->create<KCModule> (factory);
237 if (module)
238 return module;
239 }
240 }
241
242 lib->unload();
243 }
244 return 0;
245}
246
247void OptionsDialog::unloadKttsd () {
248 KLibLoader *loader = KLibLoader::self();
249 loader->unloadLibrary(QLatin1String( QFile::encodeName(QLatin1String( "libkcm_kttsd" )) ));
250 loader->unloadLibrary(QLatin1String( QFile::encodeName(QLatin1String( "kcm_kttsd" )) ));
251}
252
253#include "optionsdialog.moc"
254