1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
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 ****************************************************************************/
22 TalkFileCreator::TalkFileCreator(QObject
* parent
): QObject(parent
)
27 //! \brief Creates Talkfiles.
29 //! \param logger A pointer to a Loggerobject
30 bool TalkFileCreator::createTalkFiles(ProgressloggerInterface
* logger
)
35 QMultiMap
<QString
,QString
> fileList
;
36 QMultiMap
<QString
,QString
> dirList
;
37 QStringList toSpeakList
;
40 m_logger
->addItem(tr("Starting Talk file generation"),LOGINFO
);
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
);
55 m_enc
= EncBase::getEncoder(settings
->curEncoder());
56 m_enc
->setCfg(settings
);
60 m_logger
->addItem(tr("Init of Encoder engine failed"),LOGERROR
);
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
);
80 // create List of all Files/Dirs to speak
81 QMapIterator
<QString
, QString
> dirIt(dirList
);
82 while (dirIt
.hasNext())
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())
96 // insert only non- dublictae file entries into list
97 if(!toSpeakList
.contains(fileIt
.value()))
100 toSpeakList
.append(stripExtension(fileIt
.value()));
102 toSpeakList
.append(fileIt
.value());
107 m_logger
->addItem(tr("Voicing entries..."),LOGINFO
);
108 if(voiceList(toSpeakList
,&errStr
) == false)
110 m_logger
->addItem(errStr
,LOGERROR
);
111 doAbort(toSpeakList
);
116 m_logger
->addItem(tr("Encoding files..."),LOGINFO
);
117 if(encodeList(toSpeakList
,&errStr
) == false)
119 m_logger
->addItem(errStr
,LOGERROR
);
120 doAbort(toSpeakList
);
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
);
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
);
142 // Deleting left overs
143 if( !cleanup(toSpeakList
))
148 m_logger
->addItem(tr("Finished creating Talk files"),LOGOK
);
149 m_logger
->setProgressMax(1);
150 m_logger
->setProgressValue(1);
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
)
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("."));
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);
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
)
202 QDirIterator::IteratorFlags flags
= QDirIterator::NoIteratorFlags
;
204 flags
= QDirIterator::Subdirectories
;
206 QDirIterator
it(startDir
,flags
);
208 // read in Maps of paths - file/dirnames
217 QFileInfo fileInf
= it
.fileInfo();
222 QDir dir
= fileInf
.dir();
225 if(!dir
.dirName().isEmpty() && m_talkFolders
)
227 qDebug() << "Dir: " << dir
.dirName() << " - " << dir
.path();
228 dirMap
->insert(dir
.path(),dir
.dirName());
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();
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
++)
258 *errString
= tr("Talk file creation aborted");
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
]);
269 m_logger
->setProgressValue(++m_progress
);
270 QCoreApplication::processEvents();
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
++)
289 *errString
= tr("Talk file creation aborted");
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
);
301 m_logger
->setProgressValue(++m_progress
);
302 QCoreApplication::processEvents();
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
);
325 *errString
= tr("Talk file creation aborted");
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
);
337 if(!QFile::copy(source
,target
))
339 *errString
= tr("Copying of %1 to %2 failed").arg(source
).arg(target
);
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();
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
);
373 *errString
= tr("Talk file creation aborted");
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";
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
);
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
);
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();
411 //! \brief Cleans up Files potentially left in the temp dir
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();
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()