File indexing completed on 2024-12-01 06:31:43
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 <KConfigGroup> 0026 #include <KHelpClient> 0027 #include <KLocalizedString> 0028 #include <KSharedConfig> 0029 0030 #include "phrasebook/initialphrasebookwidget.h" 0031 #include "texttospeechconfigurationwidget.h" 0032 #include "wordcompletion/dictionarycreationwizard.h" 0033 #include "wordcompletion/wordcompletion.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")) 0054 displayCommand = true; 0055 if (!cg.hasKey("Codec")) 0056 displayCommand = true; 0057 0058 if (displayCommand) { 0059 commandWidget = new TextToSpeechConfigurationWidget(this, QStringLiteral("ttsPage")); 0060 commandWidget->readOptions(QStringLiteral("TTS System")); 0061 commandWidget->setTitle(i18n("Text-to-Speech Configuration")); 0062 addPage(commandWidget); 0063 commandWidget->setFinalPage(true); 0064 } else 0065 commandWidget = nullptr; 0066 } 0067 0068 void ConfigWizard::initBookPage() 0069 { 0070 QString standardBook = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("standard.phrasebook")); 0071 bool displayBook = (standardBook.isNull() || standardBook.isEmpty()); 0072 0073 if (displayBook) { 0074 bookWidget = new InitialPhraseBookWidget(this, "pbPage"); 0075 bookWidget->setTitle(i18n("Initial Phrase Book")); 0076 addPage(bookWidget); 0077 bookWidget->setFinalPage(true); 0078 if (commandWidget != nullptr) 0079 commandWidget->setFinalPage(false); 0080 } else 0081 bookWidget = nullptr; 0082 } 0083 0084 void ConfigWizard::initCompletion() 0085 { 0086 if (!WordCompletion::isConfigured()) { 0087 QString dictionaryFile = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dictionary.txt")); 0088 QFile file(dictionaryFile); 0089 if (file.exists()) { 0090 // If there is a word completion dictionary but no entry in the 0091 // configuration file, we need to add it there. 0092 KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("Dictionary 0")); 0093 cg.writeEntry("Filename", "dictionary.txt"); 0094 cg.writeEntry("Name", "Default"); 0095 cg.writeEntry("Language", QString()); 0096 cg.sync(); 0097 } 0098 } 0099 0100 if (KSharedConfig::openConfig()->hasGroup(QLatin1String("Completion"))) { 0101 completionWidget = nullptr; 0102 return; 0103 } 0104 0105 if (!WordCompletion::isConfigured()) { 0106 completionWidget = new CompletionWizardWidget(this, "completionPage"); 0107 completionWidget->setTitle(i18n("Word Completion")); 0108 addPage(completionWidget); 0109 completionWidget->setFinalPage(true); 0110 0111 if (commandWidget != nullptr) 0112 commandWidget->setFinalPage(false); 0113 if (bookWidget != nullptr) 0114 bookWidget->setFinalPage(false); 0115 } else 0116 completionWidget = nullptr; 0117 } 0118 0119 void ConfigWizard::saveConfig() 0120 { 0121 if (commandWidget != nullptr) { 0122 commandWidget->ok(); 0123 commandWidget->saveOptions(QStringLiteral("TTS System")); 0124 } 0125 0126 if (bookWidget != nullptr) 0127 bookWidget->createBook(); 0128 0129 if (completionWidget != nullptr) 0130 completionWidget->ok(); 0131 } 0132 0133 bool ConfigWizard::requestConfiguration() 0134 { 0135 if (commandWidget != nullptr || bookWidget != nullptr || completionWidget != nullptr) 0136 return (exec() == QDialog::Accepted); 0137 else 0138 return false; 0139 } 0140 0141 bool ConfigWizard::configurationNeeded() 0142 { 0143 return (commandWidget != nullptr || bookWidget != nullptr || completionWidget != nullptr); 0144 } 0145 0146 void ConfigWizard::help() 0147 { 0148 KHelpClient::invokeHelp(QStringLiteral("Wizard")); 0149 }