1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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, 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 ///////////////////////////////////////////////////////////////////////////////
26 #include "LockedFile.h"
29 #include <QApplication>
31 #include <QReadWriteLock>
33 #include <QStringList>
38 #include <MUtils/Global.h>
39 #include <MUtils/Exception.h>
44 ///////////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////////
49 typedef QPair
<quint32
,QString
> tool_info_t
;
50 typedef QPair
<LockedFile
*, tool_info_t
> tool_data_t
;
51 typedef QHash
<QString
, tool_data_t
> tool_hash_t
;
54 static QScopedPointer
<tool_hash_t
> g_lamexp_tools_data
;
55 static QReadWriteLock g_lamexp_tools_lock
;
58 static const QString g_null_string
;
61 static const quint32 g_max_uint32
= UINT32_MAX
;
64 #define MAKE_ENTRY(LOCK_FILE,VER,TAG) \
65 qMakePair((LOCK_FILE),qMakePair((VER),(TAG)))
67 ///////////////////////////////////////////////////////////////////////////////
69 ///////////////////////////////////////////////////////////////////////////////
72 * Clean-up *all* registered tools
74 static void lamexp_tools_clean_up(void)
76 QWriteLocker
writeLock(&g_lamexp_tools_lock
);
78 if(!g_lamexp_tools_data
.isNull())
80 const QStringList keys
= g_lamexp_tools_data
->keys();
81 for(QStringList::ConstIterator iter
= keys
.constBegin(); iter
!= keys
.constEnd(); iter
++)
83 tool_data_t currentTool
= (*g_lamexp_tools_data
)[*iter
];
84 MUTILS_DELETE(currentTool
.first
);
86 g_lamexp_tools_data
->clear();
93 void lamexp_tools_register(const QString
&toolName
, LockedFile
*const file
, const quint32
&version
, const QString
&tag
)
95 QWriteLocker
writeLock(&g_lamexp_tools_lock
);
99 MUTILS_THROW("lamexp_register_tool: Tool file must not be NULL!");
102 if(g_lamexp_tools_data
.isNull())
104 g_lamexp_tools_data
.reset(new tool_hash_t());
105 atexit(lamexp_tools_clean_up
);
108 const QString key
= toolName
.simplified().toLower();
109 if(g_lamexp_tools_data
->contains(key
))
111 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
114 g_lamexp_tools_data
->insert(key
, MAKE_ENTRY(file
, version
, tag
));
120 bool lamexp_tools_check(const QString
&toolName
)
122 QReadLocker
readLock(&g_lamexp_tools_lock
);
124 if(!g_lamexp_tools_data
.isNull())
126 const QString key
= toolName
.simplified().toLower();
127 return g_lamexp_tools_data
->contains(key
);
136 const QString
lamexp_tools_lookup(const QString
&toolName
)
138 QReadLocker
readLock(&g_lamexp_tools_lock
);
140 if(!g_lamexp_tools_data
.isNull())
142 const QString key
= toolName
.simplified().toLower();
143 if(g_lamexp_tools_data
->contains(key
))
145 return (*g_lamexp_tools_data
)[key
].first
->filePath();
149 return g_null_string
;
153 * Lookup tool version
155 const quint32
&lamexp_tools_version(const QString
&toolName
, QString
*const tagOut
)
157 QReadLocker
readLock(&g_lamexp_tools_lock
);
159 if(!g_lamexp_tools_data
.isNull())
161 const QString key
= toolName
.simplified().toLower();
162 if(g_lamexp_tools_data
->contains(key
))
164 const tool_info_t
&info
= (*g_lamexp_tools_data
)[key
].second
;
167 *tagOut
= info
.second
;