Detect WMA File Decoder component at runtime + suggest download if not installed.
[LameXP.git] / src / Thread_Initialization.cpp
blob17be6b57540c30c10ae733624f778064924f80f9
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Thread_Initialization.h"
24 #include "Global.h"
25 #include "LockedFile.h"
27 #include <QFileInfo>
28 #include <QCoreApplication>
29 #include <QProcess>
30 #include <QMap>
31 #include <QDir>
32 #include <QLibrary>
34 #include <Windows.h>
36 ////////////////////////////////////////////////////////////
37 // TOOLS
38 ////////////////////////////////////////////////////////////
40 struct lamexp_tool_t
42 char *pcHash;
43 char *pcName;
46 static const struct lamexp_tool_t g_lamexp_tools[] =
48 {"153f4274702f3629093b561a31dbf50e2c146305", "alac.exe"},
49 {"4ecc017a66fe43092110f11494f384e57d99280d", "elevator.exe"},
50 {"097dd004f44dbda57dbaeb5f15b34a220724ad60", "faad.exe"},
51 {"070bf98f78e572a97e4703ef5720c682567a6a56", "flac.exe"},
52 {"cf379081035ae6bfb6f7bc22f13bfb7ac6302ac5", "gpgv.exe"},
53 {"d837bf6ee4dab557d8b02d46c75a24e58980fffa", "gpgv.gpg"},
54 {"143fc001a2f6c56fe1b9e6f8a2eb2b53b9e1e504", "lame.exe"},
55 {"775b260b3f64101beaeb317b74746f9bccdab842", "MAC.exe"},
56 {"e8719fbfd7b690b3e518489f7aae3915305711c2", "mediainfo_icl11.exe"},
57 {"55c293a80475f7aeccf449ac9487a4626e5139cb", "mpcdec.exe"},
58 {"8bbf4a3fffe2ff143eb5ba2cf82ca16d676e865d", "mpg123.exe"},
59 {"380c734e3c3948a844b9fae213d53a93ab20beba", "oggdec.exe"},
60 {"ecd15abe103184aca96e406f5f1c82c6fb2e665d", "oggenc2_i386.exe"},
61 {"ffe0fbd73352396dc3752ac9d484dbfc754a226d", "oggenc2_sse2.exe"},
62 {"a8c50872e544a55495a824426e9378984f2ae01d", "oggenc2_x64.exe"},
63 {"cd95369051f96b9ca3a997658771c5ea52bc874d", "selfdelete.exe"},
64 {"ffeaa70bd6321185eafcb067ab2dc441650038bf", "shorten.exe"},
65 {"346ce516281c97e92e1b8957ddeca52edcf2d056", "speexdec.exe"},
66 {"8a74b767cfe88bf88c068fdae0de02d65589d25e", "takc.exe"},
67 {"1c5cedb56358a0e8c4590a863a97c94d7d7e98b2", "ttaenc.exe"},
68 {"7dcf6517aa90ed15737ee8ea50ea00a6dece2d27", "valdec.exe"},
69 {"8159f4e824b3e343ece95ba6dbb5e16da9c4866e", "volumax.exe"},
70 {"62e2805d1b2eb2a4d86a5ca6e6ea58010d05d2a7", "wget.exe"},
71 {"a17011961aa8696bc935e097b3242d33c38a9842", "wupdate.exe"},
72 {"4d018ac7f6a42abd53faacfae5055c2a3c176430", "wvunpack.exe"},
73 {NULL, NULL}
76 ////////////////////////////////////////////////////////////
77 // Constructor
78 ////////////////////////////////////////////////////////////
80 InitializationThread::InitializationThread(void)
82 m_bSuccess = false;
85 ////////////////////////////////////////////////////////////
86 // Thread Main
87 ////////////////////////////////////////////////////////////
89 void InitializationThread::run()
91 m_bSuccess = false;
92 delay();
94 QMap<QString,QString> checksum;
96 //Init checksums
97 for(int i = 0; i < INT_MAX; i++)
99 if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash)
101 checksum.insert(QString::fromLatin1(g_lamexp_tools[i].pcName), QString::fromLatin1(g_lamexp_tools[i].pcHash));
102 continue;
104 break;
107 QDir toolsDir(":/tools/");
108 QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
110 //Extract all files
111 for(int i = 0; i < toolsList.count(); i++)
115 qDebug("Extracting file: %s", g_lamexp_tools[i].pcName);
116 QString toolName = toolsList.at(i).fileName();
117 QByteArray toolHash = checksum.take(toolName).toLatin1();
118 if(toolHash.size() != 40)
120 throw "The required checksum is missing, take care!";
122 LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString(lamexp_temp_folder()).append(QString("/tool_%1").arg(toolName)), toolHash);
123 lamexp_register_tool(toolName, lockedFile);
125 catch(char *errorMsg)
127 qFatal("At least one required tool could not be extracted:\n%s", errorMsg);
128 return;
132 if(!checksum.isEmpty())
134 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(checksum.keys().first()).toLatin1().constData());
135 return;
138 qDebug("All extracted.\n");
140 //Look for Nero encoder
141 initNeroAac();
143 //Look for WMA File decoder
144 initWmaDec();
146 delay();
147 m_bSuccess = true;
150 ////////////////////////////////////////////////////////////
151 // PUBLIC FUNCTIONS
152 ////////////////////////////////////////////////////////////
154 void InitializationThread::delay(void)
156 const char *temp = "|/-\\";
157 printf("Thread is doing something important... ?\b", temp[4]);
159 for(int i = 0; i < 20; i++)
161 printf("%c\b", temp[i%4]);
162 msleep(100);
165 printf("Done\n\n");
168 void InitializationThread::initNeroAac(void)
170 QFileInfo neroFileInfo[3];
171 neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(QCoreApplication::applicationDirPath()));
172 neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(QCoreApplication::applicationDirPath()));
173 neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(QCoreApplication::applicationDirPath()));
175 bool neroFilesFound = true;
176 for(int i = 0; i < 3; i++) { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
178 //Lock the Nero binaries
179 if(!neroFilesFound)
181 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
182 return;
185 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
187 LockedFile *neroBin[3];
188 for(int i = 0; i < 3; i++) neroBin[i] = NULL;
192 for(int i = 0; i < 3; i++)
194 neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
197 catch(...)
199 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
200 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
201 return;
204 QProcess process;
205 process.setProcessChannelMode(QProcess::MergedChannels);
206 process.setReadChannel(QProcess::StandardOutput);
207 process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
209 if(!process.waitForStarted())
211 qWarning("Nero process failed to create!");
212 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
213 process.kill();
214 process.waitForFinished(-1);
215 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
216 return;
219 unsigned int neroVersion = 0;
221 while(process.state() != QProcess::NotRunning)
223 if(!process.waitForReadyRead())
225 if(process.state() == QProcess::Running)
227 qWarning("Nero process time out -> killing!");
228 process.kill();
229 process.waitForFinished(-1);
230 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
231 return;
235 while(process.canReadLine())
237 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
238 QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
239 int index1 = tokens.indexOf("Package");
240 int index2 = tokens.indexOf("version:");
241 if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
243 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
244 if(versionTokens.count() == 4)
246 neroVersion = 0;
247 neroVersion += min(9, max(0, versionTokens.at(3).toInt()));
248 neroVersion += min(9, max(0, versionTokens.at(2).toInt())) * 10;
249 neroVersion += min(9, max(0, versionTokens.at(1).toInt())) * 100;
250 neroVersion += min(9, max(0, versionTokens.at(0).toInt())) * 1000;
256 if(!(neroVersion > 0))
258 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!", neroVersion);
259 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
260 return;
263 for(int i = 0; i < 3; i++)
265 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
270 void InitializationThread::initWmaDec(void)
272 typedef HRESULT (WINAPI *SHGetFolderPathFun)(__in HWND hwndOwner, __in int nFolder, __in HANDLE hToken, __in DWORD dwFlags, __out LPWSTR pszPath);
273 static const char* wmaDecoderComponentPath = "NCH Software/Components/wmawav/wmawav.exe";
274 static const int CSIDL_PROGRAM_FILES = 0x0026;
276 QLibrary Kernel32Lib("shell32.dll");
277 SHGetFolderPathFun SHGetFolderPathPtr = (SHGetFolderPathFun) Kernel32Lib.resolve("SHGetFolderPathW");
278 QDir programFilesDir = QDir::temp();
280 if(SHGetFolderPathPtr)
282 WCHAR *programFilesPath = new WCHAR[4096];
283 if(SHGetFolderPathPtr(NULL, CSIDL_PROGRAM_FILES, NULL, NULL, programFilesPath) == S_OK)
285 programFilesDir.setPath(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const unsigned short*>(programFilesPath))));
289 LockedFile *wmaFileBin = NULL;
290 QFileInfo wmaFileInfo = QFileInfo(QString("%1/%2").arg(programFilesDir.absolutePath(), wmaDecoderComponentPath));
292 //Lock the WMA Decoder binaries
293 if(!wmaFileInfo.exists())
295 qDebug("WMA File Decoder not found -> WMA decoding support will be disabled!\n");
296 return;
299 qDebug("Found WMA File Decoder binary:\n%s\n", wmaFileInfo.canonicalFilePath().toUtf8().constData());
303 wmaFileBin = new LockedFile(wmaFileInfo.canonicalFilePath());
305 catch(...)
307 qWarning("Failed to get excluive lock to WMA File Decoder binary -> WMA decoding support will be disabled!");
308 return;
311 if(wmaFileBin)
313 lamexp_register_tool(wmaFileInfo.fileName(), wmaFileBin);
317 ////////////////////////////////////////////////////////////
318 // EVENTS
319 ////////////////////////////////////////////////////////////
321 /*NONE*/