summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/talkfile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/talkfile.cpp')
-rw-r--r--rbutil/rbutilqt/talkfile.cpp458
1 files changed, 0 insertions, 458 deletions
diff --git a/rbutil/rbutilqt/talkfile.cpp b/rbutil/rbutilqt/talkfile.cpp
deleted file mode 100644
index 815c2824fb..0000000000
--- a/rbutil/rbutilqt/talkfile.cpp
+++ /dev/null
@@ -1,458 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "talkfile.h"
21#include "rbsettings.h"
22
23TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
24{
25
26}
27
28//! \brief Creates Talkfiles.
29//!
30//! \param logger A pointer to a Loggerobject
31bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
32{
33 m_abort = false;
34 m_logger = logger;
35
36 QMultiMap<QString,QString> fileList;
37 QMultiMap<QString,QString> dirList;
38 QStringList toSpeakList, voicedEntries, encodedEntries;
39 QString errStr;
40
41 m_logger->addItem(tr("Starting Talk file generation"),LOGINFO);
42
43 //tts
44 m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
45
46 if(!m_tts->start(&errStr))
47 {
48 m_logger->addItem(errStr.trimmed(),LOGERROR);
49 m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
50 m_logger->setFinished();
51 return false;
52 }
53
54 // Encoder
55 m_enc = EncBase::getEncoder(this,RbSettings::value(RbSettings::CurEncoder).toString());
56
57 if(!m_enc->start())
58 {
59 m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
60 m_logger->setFinished();
61 m_tts->stop();
62 return false;
63 }
64
65 QCoreApplication::processEvents();
66
67 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
68 m_logger->setProgressMax(0);
69
70 // read in Maps of paths - file/dirnames
71 m_logger->addItem(tr("Reading Filelist..."),LOGINFO);
72 if(createDirAndFileMaps(m_dir,&dirList,&fileList) == false)
73 {
74 m_logger->addItem(tr("Talk file creation aborted"),LOGERROR);
75 doAbort(toSpeakList);
76 return false;
77 }
78
79 // create List of all Files/Dirs to speak
80 QMapIterator<QString, QString> dirIt(dirList);
81 while (dirIt.hasNext())
82 {
83 dirIt.next();
84 // insert only non dublicate dir entries into list
85 if(!toSpeakList.contains(dirIt.value()))
86 {
87 qDebug() << "toSpeaklist dir:" << dirIt.value();
88 toSpeakList.append(dirIt.value());
89 }
90 }
91 QMapIterator<QString, QString> fileIt(fileList);
92 while (fileIt.hasNext())
93 {
94 fileIt.next();
95 // insert only non- dublictae file entries into list
96 if(!toSpeakList.contains(fileIt.value()))
97 {
98 if(m_stripExtensions)
99 toSpeakList.append(stripExtension(fileIt.value()));
100 else
101 toSpeakList.append(fileIt.value());
102 }
103 }
104
105 // Voice entries
106 m_logger->addItem(tr("Voicing entries..."),LOGINFO);
107 TTSStatus voiceStatus= voiceList(toSpeakList,voicedEntries);
108 if(voiceStatus == FatalError)
109 {
110 doAbort(toSpeakList);
111 return false;
112 }
113
114 // Encoding Entries
115 m_logger->addItem(tr("Encoding files..."),LOGINFO);
116 if(encodeList(voicedEntries,encodedEntries) == false)
117 {
118 doAbort(toSpeakList);
119 return false;
120 }
121
122 // Copying talk files
123 m_logger->addItem(tr("Copying Talkfile for Dirs..."),LOGINFO);
124 if(copyTalkDirFiles(dirList,&errStr) == false)
125 {
126 m_logger->addItem(errStr,LOGERROR);
127 doAbort(toSpeakList);
128 return false;
129 }
130
131 //Copying file talk files
132 m_logger->addItem(tr("Copying Talkfile for Files..."),LOGINFO);
133 if(copyTalkFileFiles(fileList,&errStr) == false)
134 {
135 m_logger->addItem(errStr,LOGERROR);
136 doAbort(toSpeakList);
137 return false;
138 }
139
140 // Deleting left overs
141 if( !cleanup(toSpeakList))
142 return false;
143
144 m_tts->stop();
145 m_enc->stop();
146 m_logger->addItem(tr("Finished creating Talk files"),LOGOK);
147 m_logger->setProgressMax(1);
148 m_logger->setProgressValue(1);
149 m_logger->setFinished();
150
151 return true;
152}
153
154//! \brief resets the internal progress counter, and sets the Progressbar in the Logger
155//!
156//! \param max The maximum to shich the Progressbar is set.
157void TalkFileCreator::resetProgress(int max)
158{
159 m_progress = 0;
160 m_logger->setProgressMax(max);
161 m_logger->setProgressValue(m_progress);
162}
163
164//! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
165//!
166//! \param filename The filename from which to strip the Extension
167//! \returns the modified string
168QString TalkFileCreator::stripExtension(QString filename)
169{
170 if(filename.lastIndexOf(".") != -1)
171 return filename.left(filename.lastIndexOf("."));
172 else
173 return filename;
174}
175
176//! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
177//!
178//! \param cleanupList List of filenames to give to cleanup()
179void TalkFileCreator::doAbort(QStringList cleanupList)
180{
181 cleanup(cleanupList);
182 m_logger->setProgressMax(1);
183 m_logger->setProgressValue(0);
184 m_logger->setFinished();
185 m_tts->stop();
186 m_enc->stop();
187}
188
189//! \brief Creates MultiMaps (paths -> File/dir names) of all Dirs and Files in a Folder.
190//! Depending on settings, either Dirs or Files can be ignored.
191//! Also recursion is controlled by settings
192//!
193//! \param startDir The dir where it beginns scanning
194//! \param dirMap The MulitMap where the dirs are stored
195//! \param filMap The MultiMap where Files are stored
196//! \returns true on Success, false if User aborted.
197bool TalkFileCreator::createDirAndFileMaps(QDir startDir,QMultiMap<QString,QString> *dirMap,QMultiMap<QString,QString> *fileMap)
198{
199 // create Iterator
200 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
201 if(m_recursive)
202 flags = QDirIterator::Subdirectories;
203
204 QDirIterator it(startDir,flags);
205
206 // read in Maps of paths - file/dirnames
207 while (it.hasNext())
208 {
209 it.next();
210 if(m_abort)
211 {
212 return false;
213 }
214
215 QFileInfo fileInf = it.fileInfo();
216
217 // its a dir
218 if(fileInf.isDir())
219 {
220 QDir dir = fileInf.dir();
221
222 // insert into List
223 if(!dir.dirName().isEmpty() && m_talkFolders)
224 {
225 qDebug() << "Dir: " << dir.dirName() << " - " << dir.path();
226 dirMap->insert(dir.path(),dir.dirName());
227 }
228 }
229 else // its a File
230 {
231 // insert into List
232 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
233 {
234 qDebug() << "File: " << fileInf.fileName() << " - " << fileInf.path();
235 fileMap->insert(fileInf.path(),fileInf.fileName());
236 }
237 }
238 QCoreApplication::processEvents();
239 }
240 return true;
241}
242
243//! \brief Voices a List of string to the temp dir. Progress is handled inside.
244//!
245//! \param toSpeak QStringList with the Entries to voice.
246//! \param errString pointer to where the Error cause is written
247//! \returns true on success, false on error or user abort
248TTSStatus TalkFileCreator::voiceList(QStringList toSpeak,QStringList& voicedEntries)
249{
250 resetProgress(toSpeak.size());
251 QStringList errors;
252
253 bool warnings = false;
254 for(int i=0; i < toSpeak.size(); i++)
255 {
256 if(m_abort)
257 {
258 m_logger->addItem(tr("Talk file creation aborted"), LOGERROR);
259 return FatalError;
260 }
261
262 QString filename = QDir::tempPath()+ "/"+ toSpeak[i] + ".wav";
263
264 QString error;
265 TTSStatus status = m_tts->voice(toSpeak[i],filename, &error);
266 if(status == Warning)
267 {
268 warnings = true;
269 m_logger->addItem(tr("Voicing of %1 failed: %2").arg(toSpeak[i]).arg(error),
270 LOGWARNING);
271 }
272 else if (status == FatalError)
273 {
274 m_logger->addItem(tr("Voicing of %1 failed: %2").arg(toSpeak[i]).arg(error),
275 LOGERROR);
276 return FatalError;
277 }
278 else
279 voicedEntries.append(toSpeak[i]);
280 m_logger->setProgressValue(++m_progress);
281 QCoreApplication::processEvents();
282 }
283 if(warnings)
284 return Warning;
285 else
286 return NoError;
287}
288
289
290//! \brief Encodes a List of strings from/to the temp dir. Progress is handled inside.
291//! It expects the inputfile in the temp dir with the name in the List appended with ".wav"
292//!
293//! \param toSpeak QStringList with the Entries to encode.
294//! \param errString pointer to where the Error cause is written
295//! \returns true on success, false on error or user abort
296bool TalkFileCreator::encodeList(QStringList toEncode,QStringList& encodedEntries)
297{
298 resetProgress(toEncode.size());
299 for(int i=0; i < toEncode.size(); i++)
300 {
301 if(m_abort)
302 {
303 m_logger->addItem(tr("Talk file creation aborted"), LOGERROR);
304 return false;
305 }
306
307 QString wavfilename = QDir::tempPath()+ "/"+ toEncode[i] + ".wav";
308 QString filename = QDir::tempPath()+ "/"+ toEncode[i] + ".talk";
309
310 if(!m_enc->encode(wavfilename,filename))
311 {
312 m_logger->addItem(tr("Encoding of %1 failed").arg(filename), LOGERROR);
313 return false;
314 }
315 encodedEntries.append(toEncode[i]);
316 m_logger->setProgressValue(++m_progress);
317 QCoreApplication::processEvents();
318 }
319 return true;
320}
321
322//! \brief copys Talkfile for Dirs from the temp dir to the target. Progress and installlog is handled inside
323//!
324//! \param dirMap a MultiMap of Paths -> Dirnames
325//! \param errString Pointer to a QString where the error cause is written.
326//! \returns true on success, false on error or user abort
327bool TalkFileCreator::copyTalkDirFiles(QMultiMap<QString,QString> dirMap,QString* errString)
328{
329 resetProgress(dirMap.size());
330
331 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
332 installlog.beginGroup("talkfiles");
333
334 QMapIterator<QString, QString> it(dirMap);
335 while (it.hasNext())
336 {
337 it.next();
338 if(m_abort)
339 {
340 *errString = tr("Talk file creation aborted");
341 return false;
342 }
343
344 QString source = QDir::tempPath()+ "/"+ it.value() + ".talk";
345
346 if(!QFileInfo(source).exists())
347 continue; // this file was skipped in one of the previous steps
348
349 QString target = it.key() + "/" + "_dirname.talk";
350
351 // remove target if it exists, and if we should overwrite it
352 if(m_overwriteTalk && QFile::exists(target))
353 QFile::remove(target);
354
355 // copying
356 if(!QFile::copy(source,target))
357 {
358 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
359 return false;
360 }
361
362 // add to installlog
363 QString now = QDate::currentDate().toString("yyyyMMdd");
364 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
365
366 m_logger->setProgressValue(++m_progress);
367 QCoreApplication::processEvents();
368 }
369 installlog.endGroup();
370 installlog.sync();
371 return true;
372}
373
374//! \brief copys Talkfile for Files from the temp dir to the target. Progress and installlog is handled inside
375//!
376//! \param fileMap a MultiMap of Paths -> Filenames
377//! \param errString Pointer to a QString where the error cause is written.
378//! \returns true on success, false on error or user abort
379bool TalkFileCreator::copyTalkFileFiles(QMultiMap<QString,QString> fileMap,QString* errString)
380{
381 resetProgress(fileMap.size());
382
383 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
384 installlog.beginGroup("talkfiles");
385
386 QMapIterator<QString, QString> it(fileMap);
387 while (it.hasNext())
388 {
389 it.next();
390 if(m_abort)
391 {
392 *errString = tr("Talk file creation aborted");
393 return false;
394 }
395
396 QString source;
397 QString target = it.key() + "/" + it.value() + ".talk";
398
399 // correct source if we hav stripExtension enabled
400 if(m_stripExtensions)
401 source = QDir::tempPath()+ "/"+ stripExtension(it.value()) + ".talk";
402 else
403 source = QDir::tempPath()+ "/"+ it.value() + ".talk";
404
405 if(!QFileInfo(source).exists())
406 continue; // this file was skipped in one of the previous steps
407
408 // remove target if it exists, and if we should overwrite it
409 if(m_overwriteTalk && QFile::exists(target))
410 QFile::remove(target);
411
412 // copy file
413 qDebug() << "copying: " << source << " to " << target;
414 if(!QFile::copy(source,target))
415 {
416 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
417 return false;
418 }
419
420 // add to Install log
421 QString now = QDate::currentDate().toString("yyyyMMdd");
422 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
423
424 m_logger->setProgressValue(++m_progress);
425 QCoreApplication::processEvents();
426 }
427 installlog.endGroup();
428 installlog.sync();
429 return true;
430}
431
432
433//! \brief Cleans up Files potentially left in the temp dir
434//!
435//! \param list List of file to try to delete in the temp dir. Function appends ".wav" and ".talk" to the filenames
436bool TalkFileCreator::cleanup(QStringList list)
437{
438 m_logger->addItem(tr("Cleaning up.."),LOGINFO);
439
440 for(int i=0; i < list.size(); i++)
441 {
442 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".wav"))
443 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".wav");
444 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".talk"))
445 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".talk");
446
447 QCoreApplication::processEvents();
448 }
449 return true;
450}
451
452//! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
453//!
454void TalkFileCreator::abort()
455{
456 m_abort = true;
457}
458