Packard Bell Vibe 500: correct the path to a proper one in rbutil (proper directory...
[kugel-rb.git] / rbutil / rbutilqt / base / talkfile.cpp
blob0cc88723517ab8fa25ca865f80d501934dcdd1b8
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 if(filename.lastIndexOf(".") != -1)
92 return filename.left(filename.lastIndexOf("."));
93 else
94 return filename;
97 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
98 //!
99 void TalkFileCreator::doAbort()
101 cleanup();
102 emit logProgress(0,1);
103 emit done(true);
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)
110 m_talkList.clear();
112 // create Iterator
113 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
114 if(m_recursive)
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
125 while (it.hasNext())
127 it.next();
128 if(m_abort)
130 return false;
133 QFileInfo fileInf = it.fileInfo();
135 // its a dir
136 if(fileInf.isDir())
138 QDir dir = fileInf.dir();
140 // insert into List
141 if(!dir.dirName().isEmpty() && m_talkFolders)
143 // check if we should ignore it
144 if(m_generateOnlyNew && QFileInfo(dir.path() + "/_dirname.talk").exists())
146 continue;
149 //generate entry
150 TalkGenerator::TalkEntry entry;
151 entry.toSpeak = dir.dirName();
152 entry.wavfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
153 QCryptographicHash::Md5).toHex() + ".wav";
154 entry.talkfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
155 QCryptographicHash::Md5).toHex() + ".talk";
156 entry.target = dir.path() + "/_dirname.talk";
157 entry.voiced = false;
158 entry.encoded = false;
159 qDebug() << "toSpeak: " << entry.toSpeak << " target: " << entry.target << " intermediates: " <<
160 entry.wavfilename << entry.talkfilename;
161 m_talkList.append(entry);
164 else // its a File
166 // insert into List
167 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
169 //test if we should ignore this file
170 bool match = false;
171 for(int i=0; i < m_ignoreFiles.size();i++)
173 QRegExp rx(m_ignoreFiles[i].trimmed());
174 rx.setPatternSyntax(QRegExp::Wildcard);
175 if(rx.exactMatch(fileInf.fileName()))
176 match = true;
178 if(match)
179 continue;
181 // check if we should ignore it
182 if(m_generateOnlyNew && QFileInfo(fileInf.path() + "/" + fileInf.fileName() + ".talk").exists())
184 continue;
187 //generate entry
188 TalkGenerator::TalkEntry entry;
189 if(m_stripExtensions)
190 entry.toSpeak = stripExtension(fileInf.fileName());
191 else
192 entry.toSpeak = fileInf.fileName();
193 entry.wavfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
194 QCryptographicHash::Md5).toHex() + ".wav";
195 entry.talkfilename = QDir::tempPath()+ "/talkfiles/" + QCryptographicHash::hash(entry.toSpeak.toUtf8(),
196 QCryptographicHash::Md5).toHex() + ".talk";
197 entry.target = fileInf.path() + "/" + fileInf.fileName() + ".talk";
198 entry.voiced = false;
199 entry.encoded = false;
200 qDebug() << "toSpeak: " << entry.toSpeak << " target: " << entry.target << " intermediates: " <<
201 entry.wavfilename << entry.talkfilename;
202 m_talkList.append(entry);
205 QCoreApplication::processEvents();
207 return true;
211 //! \brief copys Talkfiles from the temp dir to the target. Progress and installlog is handled inside
213 //! \param errString Pointer to a QString where the error cause is written.
214 //! \returns true on success, false on error or user abort
215 bool TalkFileCreator::copyTalkFiles(QString* errString)
217 int progressMax = m_talkList.size();
218 int m_progress = 0;
219 emit logProgress(m_progress,progressMax);
221 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
222 installlog.beginGroup("talkfiles");
224 for(int i=0; i < m_talkList.size(); i++)
226 if(m_abort)
228 *errString = tr("File copy aborted");
229 return false;
232 // skip not encoded files
233 if(m_talkList[i].encoded == false)
235 emit logProgress(++m_progress,progressMax);
236 continue; // this file was skipped in one of the previous steps
238 // remove target if it exists, and if we should overwrite it
239 if(QFile::exists(m_talkList[i].target))
240 QFile::remove(m_talkList[i].target);
242 // copying
243 qDebug() << "copying " << m_talkList[i].talkfilename << "to" << m_talkList[i].target;
244 if(!QFile::copy(m_talkList[i].talkfilename,m_talkList[i].target))
246 *errString = tr("Copying of %1 to %2 failed").arg(m_talkList[i].talkfilename).arg(m_talkList[i].target);
247 return false;
250 // add to installlog
251 QString now = QDate::currentDate().toString("yyyyMMdd");
252 installlog.setValue(m_talkList[i].target.remove(0,m_mountpoint.length()),now);
254 emit logProgress(++m_progress,progressMax);
255 QCoreApplication::processEvents();
257 installlog.endGroup();
258 installlog.sync();
259 return true;
263 //! \brief Cleans up Files potentially left in the temp dir
265 bool TalkFileCreator::cleanup()
267 emit logItem(tr("Cleaning up..."),LOGINFO);
269 for(int i=0; i < m_talkList.size(); i++)
271 if(QFile::exists(m_talkList[i].wavfilename))
272 QFile::remove(m_talkList[i].wavfilename);
273 if(QFile::exists(m_talkList[i].talkfilename))
274 QFile::remove(m_talkList[i].talkfilename);
276 QCoreApplication::processEvents();
278 emit logItem(tr("Finished"),LOGINFO);
279 return true;
282 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
284 void TalkFileCreator::abort()
286 m_abort = true;
287 emit aborted();