Rearange menu of mpegplayer. Add new menu with "settings" and "quit", and remove...
[kugel-rb.git] / rbutil / rbutilqt / base / voicefile.cpp
blob8edb2406986b340aafb8a391634b3499ece97240
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "voicefile.h"
21 #include "utils.h"
22 #include "rbsettings.h"
24 VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
26 m_wavtrimThreshold=500;
29 void VoiceFileCreator::abort()
31 m_abort = true;
32 emit aborted();
35 bool VoiceFileCreator::createVoiceFile()
37 m_talkList.clear();
38 m_abort = false;
39 emit logItem(tr("Starting Voicefile generation"),LOGINFO);
41 // test if tempdir exists
42 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
44 QDir(QDir::tempPath()).mkdir("rbvoice");
46 m_path = QDir::tempPath() + "/rbvoice/";
48 // read rockbox-info.txt
49 RockboxInfo info(m_mountpoint);
50 if(!info.success())
52 emit logItem(tr("could not find rockbox-info.txt"),LOGERROR);
53 emit done(true);
54 return false;
57 QString target = info.target();
58 QString features = info.features();
59 QString version = info.version();
60 m_targetid = info.targetID().toInt();
61 version = version.left(version.indexOf("-")).remove("r");
63 //prepare download url
64 QUrl genlangUrl = RbSettings::value(RbSettings::GenlangUrl).toString()
65 +"?lang=" + m_lang + "&t=" + target + "&rev=" + version + "&f=" + features;
67 qDebug() << "downloading " << genlangUrl;
69 //download the correct genlang output
70 QTemporaryFile *downloadFile = new QTemporaryFile(this);
71 downloadFile->open();
72 filename = downloadFile->fileName();
73 downloadFile->close();
74 // get the real file.
75 getter = new HttpGet(this);
76 getter->setFile(downloadFile);
78 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
79 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
80 connect(this, SIGNAL(aborted()), getter, SLOT(abort()));
81 emit logItem(tr("Downloading voice info.."),LOGINFO);
82 getter->getFile(genlangUrl);
83 return true;
87 void VoiceFileCreator::downloadDone(bool error)
89 qDebug() << "Voice creator::downloadDone, error:" << error;
91 // update progress bar
92 emit logProgress(1,1);
93 if(getter->httpResponse() != 200 && !getter->isCached()) {
94 emit logItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
95 emit done(true);
96 return;
99 if(getter->isCached())
100 emit logItem(tr("Cached file used."), LOGINFO);
101 if(error)
103 emit logItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
104 emit done(true);
105 return;
107 else
108 emit logItem(tr("Download finished."),LOGINFO);
110 QCoreApplication::processEvents();
112 //open downloaded file
113 QFile genlang(filename);
114 if(!genlang.open(QIODevice::ReadOnly))
116 emit logItem(tr("failed to open downloaded file"),LOGERROR);
117 emit done(true);
118 return;
121 QCoreApplication::processEvents();
123 //read in downloaded file
124 emit logItem(tr("Reading strings..."),LOGINFO);
125 QTextStream in(&genlang);
126 in.setCodec("UTF-8");
127 QString id, voice;
128 bool idfound = false;
129 bool voicefound=false;
130 while (!in.atEnd())
132 QString line = in.readLine();
133 if(line.contains("id:")) //ID found
135 id = line.remove("id:").remove('"').trimmed();
136 idfound = true;
138 else if(line.contains("voice:")) // voice found
140 voice = line.remove("voice:").remove('"').trimmed();
141 voicefound=true;
144 if(idfound && voicefound)
146 TalkGenerator::TalkEntry entry;
147 entry.toSpeak = voice;
148 entry.wavfilename = m_path + "/" + id + ".wav";
149 entry.talkfilename = m_path + "/" + id + ".mp3"; //voicefont wants them with .mp3 extension
150 entry.voiced = false;
151 entry.encoded = false;
152 if(id == "VOICE_PAUSE")
154 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
155 entry.wavfilename = m_path + "/VOICE_PAUSE.wav";
156 entry.voiced = true;
158 m_talkList.append(entry);
159 idfound=false;
160 voicefound=false;
163 genlang.close();
165 // check for empty list
166 if(m_talkList.size() == 0)
168 emit logItem(tr("The downloaded file was empty!"),LOGERROR);
169 emit done(true);
170 return;
173 // generate files
175 TalkGenerator generator(this);
176 connect(&generator,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
177 connect(&generator,SIGNAL(logItem(QString,int)),this,SIGNAL(logItem(QString,int)));
178 connect(&generator,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
179 connect(this,SIGNAL(aborted()),&generator,SLOT(abort()));
181 if(generator.process(&m_talkList) == TalkGenerator::eERROR)
183 cleanup();
184 emit logProgress(0,1);
185 emit done(true);
186 return;
190 //make voicefile
191 emit logItem(tr("Creating voicefiles..."),LOGINFO);
192 FILE* ids2 = fopen(filename.toLocal8Bit(), "r");
193 if (ids2 == NULL)
195 cleanup();
196 emit logItem(tr("Error opening downloaded file"),LOGERROR);
197 emit done(true);
198 return;
201 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toLocal8Bit(), "wb");
202 if (output == NULL)
204 cleanup();
205 emit logItem(tr("Error opening output file"),LOGERROR);
206 emit done(true);
207 return;
210 voicefont(ids2,m_targetid,m_path.toLocal8Bit().data(), output);
212 //cleanup
213 cleanup();
215 // Add Voice file to the install log
216 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
217 installlog.beginGroup("selfcreated Voice");
218 installlog.setValue("/.rockbox/langs/" + m_lang + ".voice",QDate::currentDate().toString("yyyyMMdd"));
219 installlog.endGroup();
220 installlog.sync();
222 emit logProgress(1,1);
223 emit logItem(tr("successfully created."),LOGOK);
225 emit done(false);
228 //! \brief Cleans up Files potentially left in the temp dir
230 void VoiceFileCreator::cleanup()
232 emit logItem(tr("Cleaning up.."),LOGINFO);
234 for(int i=0; i < m_talkList.size(); i++)
236 if(QFile::exists(m_talkList[i].wavfilename))
237 QFile::remove(m_talkList[i].wavfilename);
238 if(QFile::exists(m_talkList[i].talkfilename))
239 QFile::remove(m_talkList[i].talkfilename);
241 QCoreApplication::processEvents();
243 emit logItem(tr("Finished"),LOGINFO);
245 return;