Oops, part 2: I shouldn't remove an icon that is still in use. Also convert some...
[Rockbox.git] / rbutil / rbutilqt / talkfile.cpp
blobdfe948501861ae4340ec7d16b075e29cdb381f5b
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"
22 TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
28 bool TalkFileCreator::initEncoder()
30 QFileInfo enc(m_EncExec);
31 if(enc.exists())
33 return true;
35 else
37 return false;
41 bool TalkFileCreator::initTTS()
43 QFileInfo tts(m_TTSexec);
45 if(tts.exists())
47 return true;
49 else
51 return false;
55 bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
57 m_abort = false;
58 m_logger = logger;
59 m_logger->addItem("Starting Talkfile generation",LOGINFO);
60 if(!initTTS())
62 m_logger->addItem("Init of TTS engine failed",LOGERROR);
63 return false;
65 if(!initEncoder())
67 m_logger->addItem("Init of encoder failed",LOGERROR);
68 return false;
70 QApplication::processEvents();
72 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
73 m_logger->setProgressMax(0);
74 QDirIterator it(m_dir,QDirIterator::Subdirectories);
75 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
76 installlog.beginGroup("talkfiles");
77 // iterate over all entrys
78 while (it.hasNext())
80 if(m_abort)
82 m_logger->addItem("Talkfile creation aborted",LOGERROR);
83 return false;
86 QApplication::processEvents();
87 QFileInfo fileInf = it.fileInfo();
88 QString toSpeak;
89 QString filename;
90 QString wavfilename;
92 if(fileInf.fileName() == "." || fileInf.fileName() == ".." || fileInf.suffix() == "talk")
94 it.next();
95 continue;
97 if(fileInf.isDir()) // if it is a dir
99 toSpeak = fileInf.fileName();
100 filename = fileInf.absolutePath() + "/_dirname.talk";
102 else // if it is a file
104 if(m_stripExtensions)
105 toSpeak = fileInf.baseName();
106 else
107 toSpeak = fileInf.fileName();
108 filename = fileInf.absoluteFilePath() + ".talk";
110 wavfilename = filename + ".wav";
112 QFileInfo filenameInf(filename);
113 QFileInfo wavfilenameInf(wavfilename);
115 if(!filenameInf.exists() || m_overwriteTalk)
117 if(!wavfilenameInf.exists() || m_overwriteWav)
119 m_logger->addItem("Voicing of " + toSpeak,LOGINFO);
120 if(!voice(toSpeak,wavfilename))
122 m_logger->addItem("Voicing of " + toSpeak + " failed",LOGERROR);
123 m_logger->abort();
124 return false;
127 m_logger->addItem("Encoding of " + toSpeak,LOGINFO);
128 if(!encode(wavfilename,filename))
130 m_logger->addItem("Encoding of " + wavfilename + " failed",LOGERROR);
131 m_logger->abort();
132 return false;
136 if(m_removeWav)
138 QFile wavfile(wavfilename);
139 wavfile.remove();
140 installlog.remove(wavfilename);
142 else
143 installlog.setValue(wavfilename.remove(m_mountpoint),installlog.value(wavfilename,0).toInt()+1);
145 installlog.setValue(filename.remove(m_mountpoint),installlog.value(filename,0).toInt()+1);
146 it.next();
149 installlog.endGroup();
150 m_logger->addItem("Finished creating Talkfiles",LOGOK);
151 m_logger->setProgressMax(1);
152 m_logger->setProgressValue(1);
153 m_logger->abort();
155 return true;
159 void TalkFileCreator::abort()
161 m_abort = true;
164 bool TalkFileCreator::voice(QString text,QString wavfile)
167 QString execstring = m_curTTSTemplate;
169 execstring.replace("%exe",m_TTSexec);
170 execstring.replace("%options",m_TTSOpts);
171 execstring.replace("%wavfile",wavfile);
172 execstring.replace("%text",text);
174 QProcess::execute(execstring);
175 return true;
179 bool TalkFileCreator::encode(QString input,QString output)
181 QString execstring = m_curEncTemplate;
183 execstring.replace("%exe",m_EncExec);
184 execstring.replace("%options",m_EncOpts);
185 execstring.replace("%input",input);
186 execstring.replace("%output",output);
188 QProcess::execute(execstring);
189 return true;