summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/base/talkfile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/base/talkfile.cpp')
-rw-r--r--rbutil/rbutilqt/base/talkfile.cpp263
1 files changed, 263 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/talkfile.cpp b/rbutil/rbutilqt/base/talkfile.cpp
new file mode 100644
index 0000000000..81dcf01ff5
--- /dev/null
+++ b/rbutil/rbutilqt/base/talkfile.cpp
@@ -0,0 +1,263 @@
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()
32{
33 m_abort = false;
34 QString errStr;
35
36 emit logItem(tr("Starting Talk file generation"),LOGINFO);
37 emit logProgress(0,0);
38 QCoreApplication::processEvents();
39
40 // read in Maps of paths - file/dirnames
41 emit logItem(tr("Reading Filelist..."),LOGINFO);
42 if(createTalkList(m_dir) == false)
43 {
44 emit logItem(tr("Talk file creation aborted"),LOGERROR);
45 doAbort();
46 return false;
47 }
48 QCoreApplication::processEvents();
49
50 // generate entries
51 {
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()));
57
58 if(generator.process(&m_talkList) == TalkGenerator::eERROR)
59 {
60 doAbort();
61 return false;
62 }
63 }
64
65 // Copying talk files
66 emit logItem(tr("Copying Talkfiles..."),LOGINFO);
67 if(copyTalkFiles(&errStr) == false)
68 {
69 emit logItem(errStr,LOGERROR);
70 doAbort();
71 return false;
72 }
73
74 // Deleting left overs
75 if( !cleanup())
76 return false;
77
78 emit logItem(tr("Finished creating Talk files"),LOGOK);
79 emit logProgress(1,1);
80 emit done(false);
81
82 return true;
83}
84
85//! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
86//!
87//! \param filename The filename from which to strip the Extension
88//! \returns the modified string
89QString TalkFileCreator::stripExtension(QString filename)
90{
91 if(filename.lastIndexOf(".") != -1)
92 return filename.left(filename.lastIndexOf("."));
93 else
94 return filename;
95}
96
97//! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
98//!
99void TalkFileCreator::doAbort()
100{
101 cleanup();
102 emit logProgress(0,1);
103 emit done(true);
104}
105//! \brief creates a list of what to generate
106//!
107//! \param startDir The directory from which to start scanning
108bool TalkFileCreator::createTalkList(QDir startDir)
109{
110 m_talkList.clear();
111
112 // create Iterator
113 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
114 if(m_recursive)
115 flags = QDirIterator::Subdirectories;
116
117 QDirIterator it(startDir,flags);
118
119 //create temp directory
120 QDir tempDir(QDir::tempPath()+ "/talkfiles/");
121 if(!tempDir.exists())
122 tempDir.mkpath(QDir::tempPath()+ "/talkfiles/");
123
124 // read in Maps of paths - file/dirnames
125 while (it.hasNext())
126 {
127 it.next();
128 if(m_abort)
129 {
130 return false;
131 }
132
133 QFileInfo fileInf = it.fileInfo();
134
135 // its a dir
136 if(fileInf.isDir())
137 {
138 QDir dir = fileInf.dir();
139
140 // insert into List
141 if(!dir.dirName().isEmpty() && m_talkFolders)
142 {
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);
155 }
156 }
157 else // its a File
158 {
159 // insert into List
160 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
161 {
162 TalkGenerator::TalkEntry entry;
163 if(m_stripExtensions)
164 entry.toSpeak = stripExtension(fileInf.fileName());
165 else
166 entry.toSpeak = fileInf.fileName();
167 entry.wavfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
168 QCryptographicHash::Md5).toHex() + ".wav";
169 entry.talkfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
170 QCryptographicHash::Md5).toHex() + ".talk";
171 entry.target = fileInf.path() + "/" + fileInf.fileName() + ".talk";
172 entry.voiced = false;
173 entry.encoded = false;
174 qDebug() << "toSpeak: " << entry.toSpeak << " target: " << entry.target << " intermediates: " <<
175 entry.wavfilename << entry.talkfilename;
176 m_talkList.append(entry);
177 }
178 }
179 QCoreApplication::processEvents();
180 }
181 return true;
182}
183
184
185//! \brief copys Talkfiles from the temp dir to the target. Progress and installlog is handled inside
186//!
187//! \param errString Pointer to a QString where the error cause is written.
188//! \returns true on success, false on error or user abort
189bool TalkFileCreator::copyTalkFiles(QString* errString)
190{
191 int progressMax = m_talkList.size();
192 int m_progress = 0;
193 emit logProgress(m_progress,progressMax);
194
195 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
196 installlog.beginGroup("talkfiles");
197
198 for(int i=0; i < m_talkList.size(); i++)
199 {
200 if(m_abort)
201 {
202 *errString = tr("File copy aborted");
203 return false;
204 }
205
206 // skip not encoded files
207 if(m_talkList[i].encoded == false)
208 {
209 emit logProgress(++m_progress,progressMax);
210 continue; // this file was skipped in one of the previous steps
211 }
212 // remove target if it exists, and if we should overwrite it
213 if(m_overwriteTalk && QFile::exists(m_talkList[i].target))
214 QFile::remove(m_talkList[i].target);
215
216 // copying
217 qDebug() << "copying " << m_talkList[i].talkfilename << "to" << m_talkList[i].target;
218 if(!QFile::copy(m_talkList[i].talkfilename,m_talkList[i].target))
219 {
220 *errString = tr("Copying of %1 to %2 failed").arg(m_talkList[i].talkfilename).arg(m_talkList[i].target);
221 return false;
222 }
223
224 // add to installlog
225 QString now = QDate::currentDate().toString("yyyyMMdd");
226 installlog.setValue(m_talkList[i].target.remove(0,m_mountpoint.length()),now);
227
228 emit logProgress(++m_progress,progressMax);
229 QCoreApplication::processEvents();
230 }
231 installlog.endGroup();
232 installlog.sync();
233 return true;
234}
235
236
237//! \brief Cleans up Files potentially left in the temp dir
238//!
239bool TalkFileCreator::cleanup()
240{
241 emit logItem(tr("Cleaning up.."),LOGINFO);
242
243 for(int i=0; i < m_talkList.size(); i++)
244 {
245 if(QFile::exists(m_talkList[i].wavfilename))
246 QFile::remove(m_talkList[i].wavfilename);
247 if(QFile::exists(m_talkList[i].talkfilename))
248 QFile::remove(m_talkList[i].talkfilename);
249
250 QCoreApplication::processEvents();
251 }
252 emit logItem(tr("Finished"),LOGINFO);
253 return true;
254}
255
256//! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
257//!
258void TalkFileCreator::abort()
259{
260 m_abort = true;
261 emit aborted();
262}
263