1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
20 #include "rbsettings.h"
22 TalkFileCreator::TalkFileCreator(QObject
* parent
): QObject(parent
)
27 //! \brief Creates Talkfiles.
29 //! \param logger A pointer to a Loggerobject
30 bool TalkFileCreator::createTalkFiles()
35 emit
logItem(tr("Starting Talk file generation for folder %1")
36 .arg(m_dir
.dirName()), LOGINFO
);
37 emit
logProgress(0,0);
38 QCoreApplication::processEvents();
40 // read in Maps of paths - file/dirnames
41 emit
logItem(tr("Reading Filelist..."),LOGINFO
);
42 if(createTalkList(m_dir
) == false)
44 emit
logItem(tr("Talk file creation aborted"),LOGERROR
);
48 QCoreApplication::processEvents();
52 TalkGenerator
generator(this);
53 // no string corrections yet: do not set language for TalkGenerator.
54 connect(&generator
,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
55 connect(&generator
,SIGNAL(logItem(QString
,int)),this,SIGNAL(logItem(QString
,int)));
56 connect(&generator
,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
57 connect(this,SIGNAL(aborted()),&generator
,SLOT(abort()));
59 if(generator
.process(&m_talkList
) == TalkGenerator::eERROR
)
67 emit
logItem(tr("Copying Talkfiles..."),LOGINFO
);
68 if(copyTalkFiles(&errStr
) == false)
70 emit
logItem(errStr
,LOGERROR
);
75 // Deleting left overs
79 emit
logItem(tr("Finished creating Talk files"),LOGOK
);
80 emit
logProgress(1,1);
86 //! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
88 //! \param filename The filename from which to strip the Extension
89 //! \returns the modified string
90 QString
TalkFileCreator::stripExtension(QString filename
)
92 // only strip extension if there is a dot in the filename and there are chars before the dot
93 if(filename
.lastIndexOf(".") != -1 && filename
.left(filename
.lastIndexOf(".")) != "")
94 return filename
.left(filename
.lastIndexOf("."));
99 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
101 void TalkFileCreator::doAbort()
104 emit
logProgress(0,1);
107 //! \brief creates a list of what to generate
109 //! \param startDir The directory from which to start scanning
110 bool TalkFileCreator::createTalkList(QDir startDir
)
115 QDirIterator::IteratorFlags flags
= QDirIterator::NoIteratorFlags
;
117 flags
= QDirIterator::Subdirectories
;
119 QDirIterator
it(startDir
,flags
);
121 //create temp directory
122 QDir
tempDir(QDir::tempPath()+ "/talkfiles/");
123 if(!tempDir
.exists())
124 tempDir
.mkpath(QDir::tempPath()+ "/talkfiles/");
126 // read in Maps of paths - file/dirnames
135 QFileInfo fileInf
= it
.fileInfo();
140 QDir dir
= fileInf
.dir();
143 if(!dir
.dirName().isEmpty() && m_talkFolders
)
145 // check if we should ignore it
146 if(m_generateOnlyNew
&& QFileInfo(dir
.path() + "/_dirname.talk").exists())
152 TalkGenerator::TalkEntry entry
;
153 entry
.toSpeak
= dir
.dirName();
154 entry
.wavfilename
= QDir::tempPath() + "/talkfiles/"
155 + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
156 QCryptographicHash::Md5
).toHex() + ".wav";
157 entry
.talkfilename
= QDir::tempPath() + "/talkfiles/"
158 + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
159 QCryptographicHash::Md5
).toHex() + ".talk";
160 entry
.target
= dir
.path() + "/_dirname.talk";
161 entry
.voiced
= false;
162 entry
.encoded
= false;
163 qDebug() << "[TalkFileCreator] toSpeak:" << entry
.toSpeak
164 << "target:" << entry
.target
165 << "intermediates:" << entry
.wavfilename
<< entry
.talkfilename
;
166 m_talkList
.append(entry
);
172 if( !fileInf
.fileName().isEmpty() && !fileInf
.fileName().endsWith(".talk") && m_talkFiles
)
174 //test if we should ignore this file
176 for(int i
=0; i
< m_ignoreFiles
.size();i
++)
178 QRegExp
rx(m_ignoreFiles
[i
].trimmed());
179 rx
.setPatternSyntax(QRegExp::Wildcard
);
180 if(rx
.exactMatch(fileInf
.fileName()))
186 // check if we should ignore it
187 if(m_generateOnlyNew
&& QFileInfo(fileInf
.path() + "/" + fileInf
.fileName() + ".talk").exists())
193 TalkGenerator::TalkEntry entry
;
194 if(m_stripExtensions
)
195 entry
.toSpeak
= stripExtension(fileInf
.fileName());
197 entry
.toSpeak
= fileInf
.fileName();
198 entry
.wavfilename
= QDir::tempPath() + "/talkfiles/"
199 + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
200 QCryptographicHash::Md5
).toHex() + ".wav";
201 entry
.talkfilename
= QDir::tempPath() + "/talkfiles/"
202 + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
203 QCryptographicHash::Md5
).toHex() + ".talk";
204 entry
.target
= fileInf
.path() + "/" + fileInf
.fileName() + ".talk";
205 entry
.voiced
= false;
206 entry
.encoded
= false;
207 qDebug() << "[TalkFileCreator] toSpeak:" << entry
.toSpeak
208 << "target:" << entry
.target
209 << "intermediates:" <<
210 entry
.wavfilename
<< entry
.talkfilename
;
211 m_talkList
.append(entry
);
214 QCoreApplication::processEvents();
220 //! \brief copys Talkfiles from the temp dir to the target. Progress and installlog is handled inside
222 //! \param errString Pointer to a QString where the error cause is written.
223 //! \returns true on success, false on error or user abort
224 bool TalkFileCreator::copyTalkFiles(QString
* errString
)
226 int progressMax
= m_talkList
.size();
228 emit
logProgress(m_progress
,progressMax
);
230 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
231 installlog
.beginGroup("talkfiles");
233 for(int i
=0; i
< m_talkList
.size(); i
++)
237 *errString
= tr("File copy aborted");
241 // skip not encoded files
242 if(m_talkList
[i
].encoded
== false)
244 emit
logProgress(++m_progress
,progressMax
);
245 continue; // this file was skipped in one of the previous steps
247 // remove target if it exists, and if we should overwrite it
248 if(QFile::exists(m_talkList
[i
].target
))
249 QFile::remove(m_talkList
[i
].target
);
252 qDebug() << "[TalkFileCreator] copying" << m_talkList
[i
].talkfilename
253 << "to" << m_talkList
[i
].target
;
254 if(!QFile::copy(m_talkList
[i
].talkfilename
,m_talkList
[i
].target
))
256 *errString
= tr("Copying of %1 to %2 failed").arg(m_talkList
[i
].talkfilename
).arg(m_talkList
[i
].target
);
261 QString now
= QDate::currentDate().toString("yyyyMMdd");
262 installlog
.setValue(m_talkList
[i
].target
.remove(0,m_mountpoint
.length()),now
);
264 emit
logProgress(++m_progress
,progressMax
);
265 QCoreApplication::processEvents();
267 installlog
.endGroup();
273 //! \brief Cleans up Files potentially left in the temp dir
275 bool TalkFileCreator::cleanup()
277 emit
logItem(tr("Cleaning up..."),LOGINFO
);
279 for(int i
=0; i
< m_talkList
.size(); i
++)
281 if(QFile::exists(m_talkList
[i
].wavfilename
))
282 QFile::remove(m_talkList
[i
].wavfilename
);
283 if(QFile::exists(m_talkList
[i
].talkfilename
))
284 QFile::remove(m_talkList
[i
].talkfilename
);
286 QCoreApplication::processEvents();
288 emit
logItem(tr("Finished"),LOGINFO
);
292 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
294 void TalkFileCreator::abort()