Updated FileAnalyzer_Task to extract cover artwork with latest MediaInfo version.
[LameXP.git] / src / Global_Tools.cpp
blobd6af883ef5a007918c655d681548940c1a1b2f78
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2018 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Global.h"
25 //LameXP includes
26 #include "LockedFile.h"
28 //Qt includes
29 #include <QApplication>
30 #include <QHash>
31 #include <QReadWriteLock>
32 #include <QString>
33 #include <QStringList>
34 #include <QFileInfo>
35 #include <QPair>
36 #include <QResource>
37 #include <QDir>
39 //MUtils
40 #include <MUtils/Global.h>
41 #include <MUtils/Exception.h>
43 //CRT
44 #include <stdint.h>
46 ///////////////////////////////////////////////////////////////////////////////
47 // GLOBAL VARS
48 ///////////////////////////////////////////////////////////////////////////////
50 //Typedef
51 typedef QPair<quint32,QString> tool_info_t;
52 typedef QPair<LockedFile*, tool_info_t> tool_data_t;
53 typedef QHash<QString, tool_data_t> tool_hash_t;
55 //Tool registry
56 static QScopedPointer<tool_hash_t> g_lamexp_tools_data;
57 static QReadWriteLock g_lamexp_tools_lock;
59 //Null String
60 static const QString g_null_string;
62 //UINT_MAX
63 static const quint32 g_max_uint32 = UINT32_MAX;
66 //Resource file lock
67 static QScopedPointer<LockedFile> g_lamexp_rcc_lock;
69 //Helper Macro
70 #define MAKE_ENTRY(LOCK_FILE,VER,TAG) \
71 qMakePair((LOCK_FILE),qMakePair((VER),(TAG)))
73 ///////////////////////////////////////////////////////////////////////////////
74 // GLOBAL FUNCTIONS
75 ///////////////////////////////////////////////////////////////////////////////
78 * Clean-up *all* registered tools
80 static void lamexp_tools_clean_up(void)
82 QWriteLocker writeLock(&g_lamexp_tools_lock);
84 if(!g_lamexp_tools_data.isNull())
86 const QStringList keys = g_lamexp_tools_data->keys();
87 for(QStringList::ConstIterator iter = keys.constBegin(); iter != keys.constEnd(); iter++)
89 tool_data_t currentTool = (*g_lamexp_tools_data)[*iter];
90 MUTILS_DELETE(currentTool.first);
92 g_lamexp_tools_data->clear();
97 * Register tool
99 void lamexp_tools_register(const QString &toolName, LockedFile *const file, const quint32 &version, const QString &tag)
101 QWriteLocker writeLock(&g_lamexp_tools_lock);
103 if(!file)
105 MUTILS_THROW("lamexp_register_tool: Tool file must not be NULL!");
108 if(g_lamexp_tools_data.isNull())
110 g_lamexp_tools_data.reset(new tool_hash_t());
111 atexit(lamexp_tools_clean_up);
114 const QString key = toolName.simplified().toLower();
115 if(g_lamexp_tools_data->contains(key))
117 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
120 g_lamexp_tools_data->insert(key, MAKE_ENTRY(file, version, tag));
124 * Check for tool
126 bool lamexp_tools_check(const QString &toolName)
128 QReadLocker readLock(&g_lamexp_tools_lock);
130 if(!g_lamexp_tools_data.isNull())
132 const QString key = toolName.simplified().toLower();
133 return g_lamexp_tools_data->contains(key);
136 return false;
140 * Lookup tool path
142 const QString lamexp_tools_lookup(const QString &toolName)
144 QReadLocker readLock(&g_lamexp_tools_lock);
146 if(!g_lamexp_tools_data.isNull())
148 const QString key = toolName.simplified().toLower();
149 if(g_lamexp_tools_data->contains(key))
151 return (*g_lamexp_tools_data)[key].first->filePath();
155 return g_null_string;
159 * Lookup tool version
161 const quint32 &lamexp_tools_version(const QString &toolName, QString *const tagOut)
163 QReadLocker readLock(&g_lamexp_tools_lock);
165 if(!g_lamexp_tools_data.isNull())
167 const QString key = toolName.simplified().toLower();
168 if(g_lamexp_tools_data->contains(key))
170 const tool_info_t &info = (*g_lamexp_tools_data)[key].second;
171 if(tagOut)
173 *tagOut = info.second;
175 return info.first;
179 if(tagOut)
181 tagOut->clear();
183 return g_max_uint32;
187 * Initialize external resources (RCC file)
189 void lamexp_initialize_resources(void)
191 //Load the external RCC file
192 #ifndef QT_NODLL
193 if (g_lamexp_rcc_lock.isNull())
195 const QFileInfo appPath(QCoreApplication::applicationFilePath());
196 const QString rccPath = QString("%1/%2.rcc").arg(appPath.canonicalPath(), appPath.completeBaseName());
199 qDebug("Using resource file:\n%s\n", MUTILS_UTF8(rccPath));
200 g_lamexp_rcc_lock.reset(new LockedFile(rccPath));
201 QResource::registerResource(g_lamexp_rcc_lock->filePath());
203 catch (std::runtime_error&)
205 qFatal("Failed to load the resource file:\n%s\n", MUTILS_UTF8(QDir::toNativeSeparators(rccPath)));
208 #endif //!QT_NODLL
210 //Make sure resources are accessible!
211 static const char *const RESOURCES[] = { ":/images/Logo.png", ":/tools/lame.x86-i686.exe", NULL };
212 for (size_t i = 0U; RESOURCES[i]; ++i)
214 QResource resourceCheck(QString::fromLatin1(RESOURCES[i]));
215 if ((!resourceCheck.isValid()) || (resourceCheck.size() <= 0))
217 qFatal("Qt resource system initialization has failed:\nResource \"%s\" not found!", RESOURCES[i]);