Add more simulator stubs. This should get rid of the last reds
[kugel-rb.git] / rbutil / rbutilqt / talkfile.cpp
blob7980e7cbd0add85370cdbc4319f8a02ac669ae42
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 "talkfile.h"
22 TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
27 //! \brief Creates Talkfiles.
28 //!
29 //! \param logger A pointer to a Loggerobject
30 bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
32 m_abort = false;
33 m_logger = logger;
35 QMultiMap<QString,QString> fileList;
36 QMultiMap<QString,QString> dirList;
37 QStringList toSpeakList;
38 QString errStr;
40 m_logger->addItem(tr("Starting Talk file generation"),LOGINFO);
42 //tts
43 m_tts = TTSBase::getTTS(settings->curTTS());
44 m_tts->setCfg(settings);
46 if(!m_tts->start(&errStr))
48 m_logger->addItem(errStr.trimmed(),LOGERROR);
49 m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
50 m_logger->abort();
51 return false;
54 // Encoder
55 m_enc = EncBase::getEncoder(settings->curEncoder());
56 m_enc->setCfg(settings);
58 if(!m_enc->start())
60 m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
61 m_logger->abort();
62 m_tts->stop();
63 return false;
66 QCoreApplication::processEvents();
68 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
69 m_logger->setProgressMax(0);
71 // read in Maps of paths - file/dirnames
72 m_logger->addItem(tr("Reading Filelist..."),LOGINFO);
73 if(createDirAndFileMaps(m_dir,&dirList,&fileList) == false)
75 m_logger->addItem(tr("Talk file creation aborted"),LOGERROR);
76 doAbort(toSpeakList);
77 return false;
80 // create List of all Files/Dirs to speak
81 QMapIterator<QString, QString> dirIt(dirList);
82 while (dirIt.hasNext())
84 dirIt.next();
85 // insert only non dublicate dir entries into list
86 if(!toSpeakList.contains(dirIt.value()))
88 qDebug() << "toSpeaklist dir:" << dirIt.value();
89 toSpeakList.append(dirIt.value());
92 QMapIterator<QString, QString> fileIt(fileList);
93 while (fileIt.hasNext())
95 fileIt.next();
96 // insert only non- dublictae file entries into list
97 if(!toSpeakList.contains(fileIt.value()))
99 if(m_stripExtensions)
100 toSpeakList.append(stripExtension(fileIt.value()));
101 else
102 toSpeakList.append(fileIt.value());
106 // Voice entryies
107 m_logger->addItem(tr("Voicing entries..."),LOGINFO);
108 if(voiceList(toSpeakList,&errStr) == false)
110 m_logger->addItem(errStr,LOGERROR);
111 doAbort(toSpeakList);
112 return false;
115 // Encoding Entries
116 m_logger->addItem(tr("Encoding files..."),LOGINFO);
117 if(encodeList(toSpeakList,&errStr) == false)
119 m_logger->addItem(errStr,LOGERROR);
120 doAbort(toSpeakList);
121 return false;
124 // Copying talk files
125 m_logger->addItem(tr("Copying Talkfile for Dirs..."),LOGINFO);
126 if(copyTalkDirFiles(dirList,&errStr) == false)
128 m_logger->addItem(errStr,LOGERROR);
129 doAbort(toSpeakList);
130 return false;
133 //Copying file talk files
134 m_logger->addItem(tr("Copying Talkfile for Files..."),LOGINFO);
135 if(copyTalkFileFiles(fileList,&errStr) == false)
137 m_logger->addItem(errStr,LOGERROR);
138 doAbort(toSpeakList);
139 return false;
142 // Deleting left overs
143 if( !cleanup(toSpeakList))
144 return false;
146 m_tts->stop();
147 m_enc->stop();
148 m_logger->addItem(tr("Finished creating Talk files"),LOGOK);
149 m_logger->setProgressMax(1);
150 m_logger->setProgressValue(1);
151 m_logger->abort();
153 return true;
156 //! \brief resets the internal progress counter, and sets the Progressbar in the Logger
158 //! \param max The maximum to shich the Progressbar is set.
159 void TalkFileCreator::resetProgress(int max)
161 m_progress = 0;
162 m_logger->setProgressMax(max);
163 m_logger->setProgressValue(m_progress);
166 //! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
168 //! \param filename The filename from which to strip the Extension
169 //! \returns the modified string
170 QString TalkFileCreator::stripExtension(QString filename)
172 if(filename.lastIndexOf(".") != -1)
173 return filename.left(filename.lastIndexOf("."));
174 else
175 return filename;
178 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
180 //! \param cleanupList List of filenames to give to cleanup()
181 void TalkFileCreator::doAbort(QStringList cleanupList)
183 cleanup(cleanupList);
184 m_logger->setProgressMax(1);
185 m_logger->setProgressValue(0);
186 m_logger->abort();
187 m_tts->stop();
188 m_enc->stop();
191 //! \brief Creates MultiMaps (paths -> File/dir names) of all Dirs and Files in a Folder.
192 //! Depending on settings, either Dirs or Files can be ignored.
193 //! Also recursion is controlled by settings
195 //! \param startDir The dir where it beginns scanning
196 //! \param dirMap The MulitMap where the dirs are stored
197 //! \param filMap The MultiMap where Files are stored
198 //! \returns true on Success, false if User aborted.
199 bool TalkFileCreator::createDirAndFileMaps(QDir startDir,QMultiMap<QString,QString> *dirMap,QMultiMap<QString,QString> *fileMap)
201 // create Iterator
202 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
203 if(m_recursive)
204 flags = QDirIterator::Subdirectories;
206 QDirIterator it(startDir,flags);
208 // read in Maps of paths - file/dirnames
209 while (it.hasNext())
211 it.next();
212 if(m_abort)
214 return false;
217 QFileInfo fileInf = it.fileInfo();
219 // its a dir
220 if(fileInf.isDir())
222 QDir dir = fileInf.dir();
224 // insert into List
225 if(!dir.dirName().isEmpty() && m_talkFolders)
227 qDebug() << "Dir: " << dir.dirName() << " - " << dir.path();
228 dirMap->insert(dir.path(),dir.dirName());
231 else // its a File
233 // insert into List
234 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
236 qDebug() << "File: " << fileInf.fileName() << " - " << fileInf.path();
237 fileMap->insert(fileInf.path(),fileInf.fileName());
240 QCoreApplication::processEvents();
242 return true;
245 //! \brief Voices a List of string to the temp dir. Progress is handled inside.
247 //! \param toSpeak QStringList with the Entries to voice.
248 //! \param errString pointer to where the Error cause is written
249 //! \returns true on success, false on error or user abort
250 bool TalkFileCreator::voiceList(QStringList toSpeak,QString* errString)
252 resetProgress(toSpeak.size());
254 for(int i=0; i < toSpeak.size(); i++)
256 if(m_abort)
258 *errString = tr("Talk file creation aborted");
259 return false;
262 QString filename = QDir::tempPath()+ "/"+ toSpeak[i] + ".wav";
264 if(!m_tts->voice(toSpeak[i],filename))
266 *errString =tr("Voicing of %s failed").arg(toSpeak[i]);
267 return false;
269 m_logger->setProgressValue(++m_progress);
270 QCoreApplication::processEvents();
272 return true;
276 //! \brief Encodes a List of strings from/to the temp dir. Progress is handled inside.
277 //! It expects the inputfile in the temp dir with the name in the List appended with ".wav"
279 //! \param toSpeak QStringList with the Entries to encode.
280 //! \param errString pointer to where the Error cause is written
281 //! \returns true on success, false on error or user abort
282 bool TalkFileCreator::encodeList(QStringList toEncode,QString* errString)
284 resetProgress(toEncode.size());
285 for(int i=0; i < toEncode.size(); i++)
287 if(m_abort)
289 *errString = tr("Talk file creation aborted");
290 return false;
293 QString wavfilename = QDir::tempPath()+ "/"+ toEncode[i] + ".wav";
294 QString filename = QDir::tempPath()+ "/"+ toEncode[i] + ".talk";
296 if(!m_enc->encode(wavfilename,filename))
298 *errString =tr("Encoding of %1 failed").arg(filename);
299 return false;
301 m_logger->setProgressValue(++m_progress);
302 QCoreApplication::processEvents();
304 return true;
307 //! \brief copys Talkfile for Dirs from the temp dir to the target. Progress and installlog is handled inside
309 //! \param dirMap a MultiMap of Paths -> Dirnames
310 //! \param errString Pointer to a QString where the error cause is written.
311 //! \returns true on success, false on error or user abort
312 bool TalkFileCreator::copyTalkDirFiles(QMultiMap<QString,QString> dirMap,QString* errString)
314 resetProgress(dirMap.size());
316 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
317 installlog.beginGroup("talkfiles");
319 QMapIterator<QString, QString> it(dirMap);
320 while (it.hasNext())
322 it.next();
323 if(m_abort)
325 *errString = tr("Talk file creation aborted");
326 return false;
329 QString source = QDir::tempPath()+ "/"+ it.value() + ".talk";
330 QString target = it.key() + "/" + "_dirname.talk";
332 // remove target if it exists, and if we should overwrite it
333 if(m_overwriteTalk && QFile::exists(target))
334 QFile::remove(target);
336 // copying
337 if(!QFile::copy(source,target))
339 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
340 return false;
343 // add to installlog
344 QString now = QDate::currentDate().toString("yyyyMMdd");
345 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
347 m_logger->setProgressValue(++m_progress);
348 QCoreApplication::processEvents();
350 installlog.endGroup();
351 installlog.sync();
352 return true;
355 //! \brief copys Talkfile for Files from the temp dir to the target. Progress and installlog is handled inside
357 //! \param fileMap a MultiMap of Paths -> Filenames
358 //! \param errString Pointer to a QString where the error cause is written.
359 //! \returns true on success, false on error or user abort
360 bool TalkFileCreator::copyTalkFileFiles(QMultiMap<QString,QString> fileMap,QString* errString)
362 resetProgress(fileMap.size());
364 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
365 installlog.beginGroup("talkfiles");
367 QMapIterator<QString, QString> it(fileMap);
368 while (it.hasNext())
370 it.next();
371 if(m_abort)
373 *errString = tr("Talk file creation aborted");
374 return false;
377 QString source;
378 QString target = it.key() + "/" + it.value() + ".talk";
380 // correct source if we hav stripExtension enabled
381 if(m_stripExtensions)
382 source = QDir::tempPath()+ "/"+ stripExtension(it.value()) + ".talk";
383 else
384 source = QDir::tempPath()+ "/"+ it.value() + ".talk";
386 // remove target if it exists, and if we should overwrite it
387 if(m_overwriteTalk && QFile::exists(target))
388 QFile::remove(target);
390 // copy file
391 qDebug() << "copying: " << source << " to " << target;
392 if(!QFile::copy(source,target))
394 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
395 return false;
398 // add to Install log
399 QString now = QDate::currentDate().toString("yyyyMMdd");
400 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
402 m_logger->setProgressValue(++m_progress);
403 QCoreApplication::processEvents();
405 installlog.endGroup();
406 installlog.sync();
407 return true;
411 //! \brief Cleans up Files potentially left in the temp dir
412 //!
413 //! \param list List of file to try to delete in the temp dir. Function appends ".wav" and ".talk" to the filenames
414 bool TalkFileCreator::cleanup(QStringList list)
416 m_logger->addItem(tr("Cleaning up.."),LOGINFO);
418 for(int i=0; i < list.size(); i++)
420 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".wav"))
421 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".wav");
422 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".talk"))
423 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".talk");
425 QCoreApplication::processEvents();
427 return true;
430 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
432 void TalkFileCreator::abort()
434 m_abort = true;