1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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 ///////////////////////////////////////////////////////////////////////////////
26 #include "LockedFile.h"
29 #include <QApplication>
31 #include <QReadWriteLock>
33 #include <QStringList>
40 #include <MUtils/Global.h>
41 #include <MUtils/Exception.h>
46 ///////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////////
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
;
56 static QScopedPointer
<tool_hash_t
> g_lamexp_tools_data
;
57 static QReadWriteLock g_lamexp_tools_lock
;
60 static const QString g_null_string
;
63 static const quint32 g_max_uint32
= UINT32_MAX
;
67 static QScopedPointer
<LockedFile
> g_lamexp_rcc_lock
;
70 #define MAKE_ENTRY(LOCK_FILE,VER,TAG) \
71 qMakePair((LOCK_FILE),qMakePair((VER),(TAG)))
73 ///////////////////////////////////////////////////////////////////////////////
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();
99 void lamexp_tools_register(const QString
&toolName
, LockedFile
*const file
, const quint32
&version
, const QString
&tag
)
101 QWriteLocker
writeLock(&g_lamexp_tools_lock
);
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
));
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
);
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
;
173 *tagOut
= info
.second
;
187 * Initialize external resources (RCC file)
189 void lamexp_initialize_resources(void)
191 //Load the external RCC file
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
)));
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
]);