1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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
);
97 if(g_lamexp_tools_data
.isNull())
99 g_lamexp_tools_data
.reset(new tool_hash_t());
100 atexit(lamexp_tools_clean_up
);
103 const QString key
= toolName
.simplified().toLower();
104 if(g_lamexp_tools_data
->contains(key
))
106 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
109 g_lamexp_tools_data
->insert(key
, MAKE_ENTRY(file
, version
, tag
));
115 bool lamexp_tools_check(const QString
&toolName
)
117 QReadLocker
readLock(&g_lamexp_tools_lock
);
119 if(!g_lamexp_tools_data
.isNull())
121 const QString key
= toolName
.simplified().toLower();
122 return g_lamexp_tools_data
->contains(key
);
131 const QString
&lamexp_tools_lookup(const QString
&toolName
)
133 QReadLocker
readLock(&g_lamexp_tools_lock
);
135 if(!g_lamexp_tools_data
.isNull())
137 const QString key
= toolName
.simplified().toLower();
138 if(g_lamexp_tools_data
->contains(key
))
140 return (*g_lamexp_tools_data
)[key
].first
->filePath();
144 return g_null_string
;
148 * Lookup tool version
150 const quint32
&lamexp_tools_version(const QString
&toolName
, QString
*const tagOut
)
152 QReadLocker
readLock(&g_lamexp_tools_lock
);
154 if(!g_lamexp_tools_data
.isNull())
156 const QString key
= toolName
.simplified().toLower();
157 if(g_lamexp_tools_data
->contains(key
))
159 const tool_info_t
&info
= (*g_lamexp_tools_data
)[key
].second
;
162 *tagOut
= info
.second
;