1 /* ScummVM Tools
2 *
3 * ScummVM Tools is the legal property of its developers, whose
4 * names are too numerous to list here. Please refer to the
5 * COPYRIGHT file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 /* List & description of all supported tools */
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #include <algorithm>
35
36 #include "../compress.h"
37 #include "gui_tools.h"
38
39 #include <wx/dir.h>
40 #include <wx/filename.h>
41
42 // Our global tools object, which holds all tools
43 ToolsGUI g_tools;
44
ToolsGUI()45 ToolsGUI::ToolsGUI() {
46 }
47
init()48 void ToolsGUI::init() {
49 for (ToolList::iterator tool = _tools.begin(); tool != _tools.end(); ++tool)
50 _toolmap[wxString((*tool)->getName().c_str(), wxConvUTF8)] = new ToolGUI(*tool);
51 }
52
~ToolsGUI()53 ToolsGUI::~ToolsGUI() {
54 for (std::map<wxString, ToolGUI *>::iterator iter = _toolmap.begin(); iter != _toolmap.end(); ++iter)
55 delete iter->second;
56 }
57
getToolList(ToolType tt) const58 wxArrayString ToolsGUI::getToolList(ToolType tt) const {
59 wxArrayString l;
60 for (std::map<wxString, ToolGUI *>::const_iterator iter = _toolmap.begin(); iter != _toolmap.end(); ++iter)
61 if (tt == TOOLTYPE_ALL || iter->second->getType() == tt)
62 l.Add(iter->first);
63 l.Sort();
64 std::unique(l.begin(), l.end());
65 return l;
66 }
67
getToolList(const Common::Filename & filename,ToolType tt) const68 wxArrayString ToolsGUI::getToolList(const Common::Filename &filename, ToolType tt) const {
69 ToolList choices = inspectInput(filename, tt, true);
70 wxArrayString l;
71
72 for (ToolList::const_iterator tool = choices.begin(); tool != choices.end(); ++tool)
73 l.Add(wxString((*tool)->getName().c_str(), wxConvUTF8));
74
75 l.Sort();
76 std::unique(l.begin(), l.end());
77 return l;
78 }
79
operator [](const wxString & name) const80 const ToolGUI &ToolsGUI::operator[](const wxString& name) const {
81 std::map<wxString, ToolGUI *>::const_iterator iter = _toolmap.find(name);
82
83 wxASSERT_MSG(iter != _toolmap.end(), wxT("All tools should be added, never try to access a tool that does not exist."));
84
85 return *iter->second;
86 }
87
get(const wxString & name) const88 const ToolGUI *ToolsGUI::get(const wxString& name) const {
89 std::map<wxString, ToolGUI *>::const_iterator iter = _toolmap.find(name);
90
91 if (iter == _toolmap.end())
92 return NULL;
93
94 return iter->second;
95 }
96
97 // The Tool class
98
ToolGUI(Tool * tool,ToolType type)99 ToolGUI::ToolGUI(Tool *tool, ToolType type) {
100 _backend = tool;
101 }
102
~ToolGUI()103 ToolGUI::~ToolGUI() {
104 //The parent Tools client deletes the backends
105 //delete _backend;
106 }
107
getInputList() const108 ToolInputs ToolGUI::getInputList() const {
109 return _backend->_inputPaths;
110 }
111
getName() const112 wxString ToolGUI::getName() const {
113 return wxString(_backend->getName().c_str(), wxConvUTF8);
114 }
115
getHelp() const116 wxString ToolGUI::getHelp() const {
117 // Here we want the single line help from the tool, not the extended
118 // help CompressionTool might for example give (and which would not
119 // fit in the window).
120 return wxString(_backend->Tool::getHelp().c_str(), wxConvUTF8);
121 }
122
getShortHelp() const123 wxString ToolGUI::getShortHelp() const {
124 return wxString(_backend->getShortHelp().c_str(), wxConvUTF8);
125 }
126
getType() const127 ToolType ToolGUI::getType() const {
128 return _backend->getType();
129 }
130
supportsAudioFormat(AudioFormat format) const131 bool ToolGUI::supportsAudioFormat(AudioFormat format) const {
132 // FIXME: This is a HACK!
133 CompressionTool *compression = dynamic_cast<CompressionTool *>(_backend);
134 return (compression->_supportedFormats & format) == format;
135 }
136
supportsProgressBar() const137 bool ToolGUI::supportsProgressBar() const {
138 return _backend->_supportsProgressBar;
139 }
140
outputToDirectory() const141 bool ToolGUI::outputToDirectory() const {
142 return _backend->_outputToDirectory;
143 }
144
supportsMultipleRuns() const145 bool ToolGUI::supportsMultipleRuns() const {
146 return _backend->_supportsMultipleRuns;
147 }
148
run(const Configuration & conf) const149 void ToolGUI::run(const Configuration &conf) const {
150 CompressionTool *compression = dynamic_cast<CompressionTool *>(_backend);
151 if (compression) {
152 compression->_format = conf.selectedAudioFormat;
153
154 // mp3
155 compression->setMp3LamePath ( (const char *)conf.mp3LamePath.mb_str() );
156 compression->setMp3CompressionType( (const char *)conf.mp3CompressionType.mb_str() );
157 compression->setMp3MpegQuality ( (const char *)conf.mp3MpegQuality.mb_str() );
158 if (conf.mp3CompressionType == wxT("ABR")) {
159 compression->setMp3TargetBitrate ( (const char *)conf.mp3ABRBitrate.mb_str() );
160 compression->unsetMp3MinBitrate();
161 compression->unsetMp3MaxBitrate();
162 } else {
163 compression->setMp3MinBitrate ( (const char *)conf.mp3VBRMinBitrate.mb_str() );
164 compression->setMp3MaxBitrate ( (const char *)conf.mp3VBRMaxBitrate.mb_str() );
165 }
166 compression->setMp3VBRQuality ( (const char *)conf.mp3VBRQuality.mb_str() );
167
168 // flac
169 compression->setFlacCompressionLevel( (const char *)conf.flacCompressionLevel.mb_str() );
170 compression->setFlacBlockSize ( (const char *)conf.flacBlockSize.mb_str() );
171
172 // vorbis
173 if (conf.useOggQuality)
174 compression->setOggQuality ( (const char *)conf.oggQuality.mb_str() );
175 else
176 compression->setOggAvgBitrate ( (const char *)conf.oggAvgBitrate.mb_str() );
177 if (conf.oggMinBitrate == wxT("None"))
178 compression->unsetOggMinBitrate();
179 else
180 compression->setOggMinBitrate ( (const char *)conf.oggMinBitrate.mb_str() );
181 if (conf.oggMaxBitrate == wxT("None"))
182 compression->unsetOggMaxBitrate();
183 else
184 compression->setOggMaxBitrate ( (const char *)conf.oggMaxBitrate.mb_str() );
185 }
186
187 if (conf.multipleRuns && supportsMultipleRuns() && conf.inputFilePaths.size() == 1) {
188 // Run on all the files with the same extension as the given input file
189 wxFileName inputFile(conf.inputFilePaths[0]);
190 wxArrayString fileList;
191 if (wxDir::GetAllFiles(inputFile.GetPath(), &fileList, wxString::FromAscii("*.") + inputFile.GetExt(), wxDIR_FILES) == 0)
192 _backend->error("No input file found!");
193 for (wxArrayString::const_iterator iter = fileList.begin(); iter != fileList.end(); ++iter) {
194 // Reset the output path for each run in case it is changed by the tool
195 _backend->_outputPath = std::string(conf.outputPath.mb_str());
196
197 // Set the input paths
198 _backend->clearInputPaths();
199 if (!_backend->addInputPath(std::string(iter->mb_str()))) {
200 _backend->warning("Unexpected input file '%s'!", (const char*)iter->mb_str());
201 continue;
202 }
203
204 _backend->run();
205 }
206 } else {
207 // Set the output path
208 _backend->_outputPath = std::string(conf.outputPath.mb_str());
209
210 // Set the input paths
211 _backend->clearInputPaths();
212 if (conf.inputFilePaths.size() < _backend->_inputPaths.size())
213 _backend->error("Too few input files!");
214 for (wxArrayString::const_iterator iter = conf.inputFilePaths.begin(); iter != conf.inputFilePaths.end(); ++iter) {
215 if (!_backend->addInputPath(std::string(iter->mb_str())))
216 _backend->error("Unexpected input file '%s'!", (const char*)iter->mb_str());
217 }
218
219 _backend->run();
220 }
221 }
222