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 ****************************************************************************/
21 #include "rbsettings.h"
23 TalkFileCreator::TalkFileCreator(QObject
* parent
): QObject(parent
)
28 //! \brief Creates Talkfiles.
30 //! \param logger A pointer to a Loggerobject
31 bool TalkFileCreator::createTalkFiles()
36 emit
logItem(tr("Starting Talk file generation"),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 connect(&generator
,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
54 connect(&generator
,SIGNAL(logItem(QString
,int)),this,SIGNAL(logItem(QString
,int)));
55 connect(&generator
,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
56 connect(this,SIGNAL(aborted()),&generator
,SLOT(abort()));
58 if(generator
.process(&m_talkList
) == TalkGenerator::eERROR
)
66 emit
logItem(tr("Copying Talkfiles..."),LOGINFO
);
67 if(copyTalkFiles(&errStr
) == false)
69 emit
logItem(errStr
,LOGERROR
);
74 // Deleting left overs
78 emit
logItem(tr("Finished creating Talk files"),LOGOK
);
79 emit
logProgress(1,1);
85 //! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
87 //! \param filename The filename from which to strip the Extension
88 //! \returns the modified string
89 QString
TalkFileCreator::stripExtension(QString filename
)
91 if(filename
.lastIndexOf(".") != -1)
92 return filename
.left(filename
.lastIndexOf("."));
97 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
99 void TalkFileCreator::doAbort()
102 emit
logProgress(0,1);
105 //! \brief creates a list of what to generate
107 //! \param startDir The directory from which to start scanning
108 bool TalkFileCreator::createTalkList(QDir startDir
)
113 QDirIterator::IteratorFlags flags
= QDirIterator::NoIteratorFlags
;
115 flags
= QDirIterator::Subdirectories
;
117 QDirIterator
it(startDir
,flags
);
119 //create temp directory
120 QDir
tempDir(QDir::tempPath()+ "/talkfiles/");
121 if(!tempDir
.exists())
122 tempDir
.mkpath(QDir::tempPath()+ "/talkfiles/");
124 // read in Maps of paths - file/dirnames
133 QFileInfo fileInf
= it
.fileInfo();
138 QDir dir
= fileInf
.dir();
141 if(!dir
.dirName().isEmpty() && m_talkFolders
)
143 TalkGenerator::TalkEntry entry
;
144 entry
.toSpeak
= dir
.dirName();
145 entry
.wavfilename
= QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
146 QCryptographicHash::Md5
).toHex() + ".wav";
147 entry
.talkfilename
= QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
148 QCryptographicHash::Md5
).toHex() + ".talk";
149 entry
.target
= dir
.path() + "/_dirname.talk";
150 entry
.voiced
= false;
151 entry
.encoded
= false;
152 qDebug() << "toSpeak: " << entry
.toSpeak
<< " target: " << entry
.target
<< " intermediates: " <<
153 entry
.wavfilename
<< entry
.talkfilename
;
154 m_talkList
.append(entry
);
160 if( !fileInf
.fileName().isEmpty() && !fileInf
.fileName().endsWith(".talk") && m_talkFiles
)
162 //test if we should ignore this file
164 for(int i
=0; i
< m_ignoreFiles
.size();i
++)
166 QRegExp
rx(m_ignoreFiles
[i
].trimmed());
167 rx
.setPatternSyntax(QRegExp::Wildcard
);
168 if(rx
.exactMatch(fileInf
.fileName()))
175 TalkGenerator::TalkEntry entry
;
176 if(m_stripExtensions
)
177 entry
.toSpeak
= stripExtension(fileInf
.fileName());
179 entry
.toSpeak
= fileInf
.fileName();
180 entry
.wavfilename
= QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
181 QCryptographicHash::Md5
).toHex() + ".wav";
182 entry
.talkfilename
= QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry
.toSpeak
.toUtf8(),
183 QCryptographicHash::Md5
).toHex() + ".talk";
184 entry
.target
= fileInf
.path() + "/" + fileInf
.fileName() + ".talk";
185 entry
.voiced
= false;
186 entry
.encoded
= false;
187 qDebug() << "toSpeak: " << entry
.toSpeak
<< " target: " << entry
.target
<< " intermediates: " <<
188 entry
.wavfilename
<< entry
.talkfilename
;
189 m_talkList
.append(entry
);
192 QCoreApplication::processEvents();
198 //! \brief copys Talkfiles from the temp dir to the target. Progress and installlog is handled inside
200 //! \param errString Pointer to a QString where the error cause is written.
201 //! \returns true on success, false on error or user abort
202 bool TalkFileCreator::copyTalkFiles(QString
* errString
)
204 int progressMax
= m_talkList
.size();
206 emit
logProgress(m_progress
,progressMax
);
208 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
209 installlog
.beginGroup("talkfiles");
211 for(int i
=0; i
< m_talkList
.size(); i
++)
215 *errString
= tr("File copy aborted");
219 // skip not encoded files
220 if(m_talkList
[i
].encoded
== false)
222 emit
logProgress(++m_progress
,progressMax
);
223 continue; // this file was skipped in one of the previous steps
225 // remove target if it exists, and if we should overwrite it
226 if(m_overwriteTalk
&& QFile::exists(m_talkList
[i
].target
))
227 QFile::remove(m_talkList
[i
].target
);
230 qDebug() << "copying " << m_talkList
[i
].talkfilename
<< "to" << m_talkList
[i
].target
;
231 if(!QFile::copy(m_talkList
[i
].talkfilename
,m_talkList
[i
].target
))
233 *errString
= tr("Copying of %1 to %2 failed").arg(m_talkList
[i
].talkfilename
).arg(m_talkList
[i
].target
);
238 QString now
= QDate::currentDate().toString("yyyyMMdd");
239 installlog
.setValue(m_talkList
[i
].target
.remove(0,m_mountpoint
.length()),now
);
241 emit
logProgress(++m_progress
,progressMax
);
242 QCoreApplication::processEvents();
244 installlog
.endGroup();
250 //! \brief Cleans up Files potentially left in the temp dir
252 bool TalkFileCreator::cleanup()
254 emit
logItem(tr("Cleaning up.."),LOGINFO
);
256 for(int i
=0; i
< m_talkList
.size(); i
++)
258 if(QFile::exists(m_talkList
[i
].wavfilename
))
259 QFile::remove(m_talkList
[i
].wavfilename
);
260 if(QFile::exists(m_talkList
[i
].talkfilename
))
261 QFile::remove(m_talkList
[i
].talkfilename
);
263 QCoreApplication::processEvents();
265 emit
logItem(tr("Finished"),LOGINFO
);
269 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
271 void TalkFileCreator::abort()