File indexing completed on 2023-05-30 10:39:35

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 by Jeremy Whiting <jpwhiting@kde.org>              *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "configwizard.h"
0022 
0023 #include <QStandardPaths>
0024 
0025 #include <KSharedConfig>
0026 #include <KConfigGroup>
0027 #include <KHelpClient>
0028 #include <KLocalizedString>
0029 
0030 #include "texttospeechconfigurationwidget.h"
0031 #include "phrasebook/initialphrasebookwidget.h"
0032 #include "wordcompletion/wordcompletion.h"
0033 #include "wordcompletion/dictionarycreationwizard.h"
0034 
0035 ConfigWizard::ConfigWizard(QWidget *parent)
0036     : QWizard(parent)
0037 {
0038     setWindowTitle(i18n("Initial Configuration - KMouth"));
0039     initCommandPage();
0040     initBookPage();
0041     initCompletion();
0042     connect(this, &QDialog::accepted, this, &ConfigWizard::saveConfig);
0043 }
0044 
0045 ConfigWizard::~ConfigWizard()
0046 {
0047 }
0048 
0049 void ConfigWizard::initCommandPage()
0050 {
0051     KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("TTS System"));
0052     bool displayCommand = false;
0053     if (!cg.hasKey("StdIn"))   displayCommand = true;
0054     if (!cg.hasKey("Codec"))   displayCommand = true;
0055 
0056     if (displayCommand) {
0057         commandWidget = new TextToSpeechConfigurationWidget(this, QStringLiteral("ttsPage"));
0058         commandWidget->readOptions(QStringLiteral("TTS System"));
0059         commandWidget->setTitle(i18n("Text-to-Speech Configuration"));
0060         addPage(commandWidget);
0061         commandWidget->setFinalPage(true);
0062     } else
0063         commandWidget = nullptr;
0064 }
0065 
0066 void ConfigWizard::initBookPage()
0067 {
0068     QString standardBook = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("standard.phrasebook"));
0069     bool displayBook = (standardBook.isNull() || standardBook.isEmpty());
0070 
0071     if (displayBook) {
0072         bookWidget = new InitialPhraseBookWidget(this, "pbPage");
0073         bookWidget->setTitle(i18n("Initial Phrase Book"));
0074         addPage(bookWidget);
0075         bookWidget->setFinalPage(true);
0076         if (commandWidget != nullptr)
0077             commandWidget->setFinalPage(false);
0078     } else
0079         bookWidget = nullptr;
0080 }
0081 
0082 void ConfigWizard::initCompletion()
0083 {
0084     if (!WordCompletion::isConfigured()) {
0085         QString dictionaryFile = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dictionary.txt"));
0086         QFile file(dictionaryFile);
0087         if (file.exists()) {
0088             // If there is a word completion dictionary but no entry in the
0089             // configuration file, we need to add it there.
0090             KConfigGroup cg(KSharedConfig::openConfig() , "Dictionary 0");
0091             cg.writeEntry("Filename", "dictionary.txt");
0092             cg.writeEntry("Name",     "Default");
0093             cg.writeEntry("Language", QString());
0094             cg.sync();
0095         }
0096     }
0097 
0098     if (KSharedConfig::openConfig()->hasGroup("Completion")) {
0099         completionWidget = nullptr;
0100         return;
0101     }
0102 
0103     if (!WordCompletion::isConfigured()) {
0104         completionWidget = new CompletionWizardWidget(this, "completionPage");
0105         completionWidget->setTitle(i18n("Word Completion"));
0106         addPage(completionWidget);
0107         completionWidget->setFinalPage(true);
0108 
0109         if (commandWidget != nullptr)
0110             commandWidget->setFinalPage(false);
0111         if (bookWidget != nullptr)
0112             bookWidget->setFinalPage(false);
0113     } else
0114         completionWidget = nullptr;
0115 }
0116 
0117 void ConfigWizard::saveConfig()
0118 {
0119     if (commandWidget != nullptr) {
0120         commandWidget->ok();
0121         commandWidget->saveOptions(QStringLiteral("TTS System"));
0122     }
0123 
0124     if (bookWidget != nullptr)
0125         bookWidget->createBook();
0126 
0127     if (completionWidget != nullptr)
0128         completionWidget->ok();
0129 }
0130 
0131 bool ConfigWizard::requestConfiguration()
0132 {
0133     if (commandWidget != nullptr || bookWidget != nullptr || completionWidget != nullptr)
0134         return (exec() == QDialog::Accepted);
0135     else
0136         return false;
0137 }
0138 
0139 bool ConfigWizard::configurationNeeded()
0140 {
0141     return (commandWidget != nullptr || bookWidget != nullptr || completionWidget != nullptr);
0142 }
0143 
0144 void ConfigWizard::help()
0145 {
0146     KHelpClient::invokeHelp(QStringLiteral("Wizard"));
0147 }
0148