Make sure to include audiohw.h in settings.h or the definition of struct user_setting...
[kugel-rb.git] / rbutil / rbutilqt / base / talkfile.cpp
blob3813912347f4f43fd0cfc26cc53ca623850a3720
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"
21 #include "rbsettings.h"
23 TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
28 //! \brief Creates Talkfiles.
29 //!
30 //! \param logger A pointer to a Loggerobject
31 bool TalkFileCreator::createTalkFiles()
33 m_abort = false;
34 QString errStr;
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);
45 doAbort();
46 return false;
48 QCoreApplication::processEvents();
50 // generate entries
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)
60 doAbort();
61 return false;
65 // Copying talk files
66 emit logItem(tr("Copying Talkfiles..."),LOGINFO);
67 if(copyTalkFiles(&errStr) == false)
69 emit logItem(errStr,LOGERROR);
70 doAbort();
71 return false;
74 // Deleting left overs
75 if( !cleanup())
76 return false;
78 emit logItem(tr("Finished creating Talk files"),LOGOK);
79 emit logProgress(1,1);
80 emit done(false);
82 return true;
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
89 QString TalkFileCreator::stripExtension(QString filename)
91 // only strip extension if there is a dot in the filename and there are chars before the dot
92 if(filename.lastIndexOf(".") != -1 && filename.left(filename.lastIndexOf(".")) != "")
93 return filename.left(filename.lastIndexOf("."));
94 else
95 return filename;
98 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
99 //!
100 void TalkFileCreator::doAbort()
102 cleanup();
103 emit logProgress(0,1);
104 emit done(true);
106 //! \brief creates a list of what to generate
108 //! \param startDir The directory from which to start scanning
109 bool TalkFileCreator::createTalkList(QDir startDir)
111 m_talkList.clear();
113 // create Iterator
114 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
115 if(m_recursive)
116 flags = QDirIterator::Subdirectories;
118 QDirIterator it(startDir,flags);
120 //create temp directory
121 QDir tempDir(QDir::tempPath()+ "/talkfiles/");
122 if(!tempDir.exists())
123 tempDir.mkpath(QDir::tempPath()+ "/talkfiles/");
125 // read in Maps of paths - file/dirnames
126 while (it.hasNext())
128 it.next();
129 if(m_abort)
131 return false;
134 QFileInfo fileInf = it.fileInfo();
136 // its a dir
137 if(fileInf.isDir())
139 QDir dir = fileInf.dir();
141 // insert into List
142 if(!dir.dirName().isEmpty() && m_talkFolders)
144 // check if we should ignore it
145 if(m_generateOnlyNew && QFileInfo(dir.path() + "/_dirname.talk").exists())
147 continue;
150 //generate entry
151 TalkGenerator::TalkEntry entry;
152 entry.toSpeak = dir.dirName();
153 entry.wavfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
154 QCryptographicHash::Md5).toHex() + ".wav";
155 entry.talkfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
156 QCryptographicHash::Md5).toHex() + ".talk";
157 entry.target = dir.path() + "/_dirname.talk";
158 entry.voiced = false;
159 entry.encoded = false;
160 qDebug() << "toSpeak: " << entry.toSpeak << " target: " << entry.target << " intermediates: " <<
161 entry.wavfilename << entry.talkfilename;
162 m_talkList.append(entry);
165 else // its a File
167 // insert into List
168 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
170 //test if we should ignore this file
171 bool match = false;
172 for(int i=0; i < m_ignoreFiles.size();i++)
174 QRegExp rx(m_ignoreFiles[i].trimmed());
175 rx.setPatternSyntax(QRegExp::Wildcard);
176 if(rx.exactMatch(fileInf.fileName()))
177 match = true;
179 if(match)
180 continue;
182 // check if we should ignore it
183 if(m_generateOnlyNew && QFileInfo(fileInf.path() + "/" + fileInf.fileName() + ".talk").exists())
185 continue;
188 //generate entry
189 TalkGenerator::TalkEntry entry;
190 if(m_stripExtensions)
191 entry.toSpeak = stripExtension(fileInf.fileName());
192 else
193 entry.toSpeak = fileInf.fileName();
194 entry.wavfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
195 QCryptographicHash::Md5).toHex() + ".wav";
196 entry.talkfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
197 QCryptographicHash::Md5).toHex() + ".talk";
198 entry.target = fileInf.path() + "/" + fileInf.fileName() + ".talk";
199 entry.voiced = false;
200 entry.encoded = false;
201 qDebug() << "toSpeak: " << entry.toSpeak << " target: " << entry.target << " intermediates: " <<
202 entry.wavfilename << entry.talkfilename;
203 m_talkList.append(entry);
206 QCoreApplication::processEvents();
208 return true;
212 //! \brief copys Talkfiles from the temp dir to the target. Progress and installlog is handled inside
214 //! \param errString Pointer to a QString where the error cause is written.
215 //! \returns true on success, false on error or user abort
216 bool TalkFileCreator::copyTalkFiles(QString* errString)
218 int progressMax = m_talkList.size();
219 int m_progress = 0;
220 emit logProgress(m_progress,progressMax);
222 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
223 installlog.beginGroup("talkfiles");
225 for(int i=0; i < m_talkList.size(); i++)
227 if(m_abort)
229 *errString = tr("File copy aborted");
230 return false;
233 // skip not encoded files
234 if(m_talkList[i].encoded == false)
236 emit logProgress(++m_progress,progressMax);
237 continue; // this file was skipped in one of the previous steps
239 // remove target if it exists, and if we should overwrite it
240 if(QFile::exists(m_talkList[i].target))
241 QFile::remove(m_talkList[i].target);
243 // copying
244 qDebug() << "copying " << m_talkList[i].talkfilename << "to" << m_talkList[i].target;
245 if(!QFile::copy(m_talkList[i].talkfilename,m_talkList[i].target))
247 *errString = tr("Copying of %1 to %2 failed").arg(m_talkList[i].talkfilename).arg(m_talkList[i].target);
248 return false;
251 // add to installlog
252 QString now = QDate::currentDate().toString("yyyyMMdd");
253 installlog.setValue(m_talkList[i].target.remove(0,m_mountpoint.length()),now);
255 emit logProgress(++m_progress,progressMax);
256 QCoreApplication::processEvents();
258 installlog.endGroup();
259 installlog.sync();
260 return true;
264 //! \brief Cleans up Files potentially left in the temp dir
266 bool TalkFileCreator::cleanup()
268 emit logItem(tr("Cleaning up..."),LOGINFO);
270 for(int i=0; i < m_talkList.size(); i++)
272 if(QFile::exists(m_talkList[i].wavfilename))
273 QFile::remove(m_talkList[i].wavfilename);
274 if(QFile::exists(m_talkList[i].talkfilename))
275 QFile::remove(m_talkList[i].talkfilename);
277 QCoreApplication::processEvents();
279 emit logItem(tr("Finished"),LOGINFO);
280 return true;
283 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
285 void TalkFileCreator::abort()
287 m_abort = true;
288 emit aborted();