Handle language change events in widgets.
[maemo-rb.git] / rbutil / rbutilqt / base / talkfile.cpp
blobb7d080de012a5d92006eb39a141d3929957a1e92
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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 ****************************************************************************/
19 #include "talkfile.h"
20 #include "rbsettings.h"
22 TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
27 //! \brief Creates Talkfiles.
28 //!
29 //! \param logger A pointer to a Loggerobject
30 bool TalkFileCreator::createTalkFiles()
32 m_abort = false;
33 QString errStr;
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);
45 doAbort();
46 return false;
48 QCoreApplication::processEvents();
50 // generate entries
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)
61 doAbort();
62 return false;
66 // Copying talk files
67 emit logItem(tr("Copying Talkfiles..."),LOGINFO);
68 if(copyTalkFiles(&errStr) == false)
70 emit logItem(errStr,LOGERROR);
71 doAbort();
72 return false;
75 // Deleting left overs
76 if( !cleanup())
77 return false;
79 emit logItem(tr("Finished creating Talk files"),LOGOK);
80 emit logProgress(1,1);
81 emit done(false);
83 return true;
86 //! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
87 //!
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("."));
95 else
96 return filename;
99 //! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
101 void TalkFileCreator::doAbort()
103 cleanup();
104 emit logProgress(0,1);
105 emit done(true);
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)
112 m_talkList.clear();
114 // create Iterator
115 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
116 if(m_recursive)
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
127 while (it.hasNext())
129 it.next();
130 if(m_abort)
132 return false;
135 QFileInfo fileInf = it.fileInfo();
137 // its a dir
138 if(fileInf.isDir())
140 QDir dir = fileInf.dir();
142 // insert into List
143 if(!dir.dirName().isEmpty() && m_talkFolders)
145 // check if we should ignore it
146 if(m_generateOnlyNew && QFileInfo(dir.path() + "/_dirname.talk").exists())
148 continue;
151 //generate entry
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);
169 else // its a File
171 // insert into List
172 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
174 //test if we should ignore this file
175 bool match = false;
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()))
181 match = true;
183 if(match)
184 continue;
186 // check if we should ignore it
187 if(m_generateOnlyNew && QFileInfo(fileInf.path() + "/" + fileInf.fileName() + ".talk").exists())
189 continue;
192 //generate entry
193 TalkGenerator::TalkEntry entry;
194 if(m_stripExtensions)
195 entry.toSpeak = stripExtension(fileInf.fileName());
196 else
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();
216 return true;
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();
227 int m_progress = 0;
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++)
235 if(m_abort)
237 *errString = tr("File copy aborted");
238 return false;
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);
251 // copying
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);
257 return false;
260 // add to installlog
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();
268 installlog.sync();
269 return true;
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);
289 return true;
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()
296 m_abort = true;
297 emit aborted();