1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 <QApplication>
28 #include <QReadWriteLock>
29 #include <QReadLocker>
30 #include <QWriteLocker>
32 #include <QStringList>
33 #include <QTranslator>
37 #include "LockedFile.h"
39 ///////////////////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////////
46 QMap
<QString
, LockedFile
*> *registry
;
47 QMap
<QString
, unsigned int> *versions
;
48 QMap
<QString
, QString
> *tags
;
56 QMap
<QString
, QString
> *files
;
57 QMap
<QString
, QString
> *names
;
58 QMap
<QString
, unsigned int> *sysid
;
59 QMap
<QString
, unsigned int> *cntry
;
67 QTranslator
*instance
;
70 g_lamexp_currentTranslator
;
72 ///////////////////////////////////////////////////////////////////////////////
74 ///////////////////////////////////////////////////////////////////////////////
79 void lamexp_register_tool(const QString
&toolName
, LockedFile
*file
, unsigned int version
, const QString
*tag
)
81 QWriteLocker
writeLock(&g_lamexp_tools
.lock
);
83 if(!g_lamexp_tools
.registry
) g_lamexp_tools
.registry
= new QMap
<QString
, LockedFile
*>();
84 if(!g_lamexp_tools
.versions
) g_lamexp_tools
.versions
= new QMap
<QString
, unsigned int>();
85 if(!g_lamexp_tools
.tags
) g_lamexp_tools
.tags
= new QMap
<QString
, QString
>();
87 if(g_lamexp_tools
.registry
->contains(toolName
.toLower()))
89 THROW("lamexp_register_tool: Tool is already registered!");
92 g_lamexp_tools
.registry
->insert(toolName
.toLower(), file
);
93 g_lamexp_tools
.versions
->insert(toolName
.toLower(), version
);
94 g_lamexp_tools
.tags
->insert(toolName
.toLower(), (tag
) ? (*tag
) : QString());
100 bool lamexp_check_tool(const QString
&toolName
)
102 QReadLocker
readLock(&g_lamexp_tools
.lock
);
103 return (g_lamexp_tools
.registry
) ? g_lamexp_tools
.registry
->contains(toolName
.toLower()) : false;
109 const QString
lamexp_lookup_tool(const QString
&toolName
)
111 QReadLocker
readLock(&g_lamexp_tools
.lock
);
113 if(g_lamexp_tools
.registry
)
115 if(g_lamexp_tools
.registry
->contains(toolName
.toLower()))
117 return g_lamexp_tools
.registry
->value(toolName
.toLower())->filePath();
131 * Lookup tool version
133 unsigned int lamexp_tool_version(const QString
&toolName
, QString
*tag
)
135 QReadLocker
readLock(&g_lamexp_tools
.lock
);
136 if(tag
) tag
->clear();
138 if(g_lamexp_tools
.versions
)
140 if(g_lamexp_tools
.versions
->contains(toolName
.toLower()))
144 if(g_lamexp_tools
.tags
->contains(toolName
.toLower())) *tag
= g_lamexp_tools
.tags
->value(toolName
.toLower());
146 return g_lamexp_tools
.versions
->value(toolName
.toLower());
160 * Version number to human-readable string
162 const QString
lamexp_version2string(const QString
&pattern
, unsigned int version
, const QString
&defaultText
, const QString
*tag
)
164 if(version
== UINT_MAX
)
169 QString result
= pattern
;
170 const int digits
= result
.count(QChar(L
'?'), Qt::CaseInsensitive
);
177 int pos
= 0, index
= -1;
178 const QString versionStr
= QString().sprintf("%0*u", digits
, version
);
179 Q_ASSERT(versionStr
.length() == digits
);
181 while((index
= result
.indexOf(QChar(L
'?'), Qt::CaseInsensitive
)) >= 0)
183 result
[index
] = versionStr
[pos
++];
188 if((index
= result
.indexOf(QChar(L
'#'), Qt::CaseInsensitive
)) >= 0)
190 result
.remove(index
, 1).insert(index
, (*tag
));
198 * Initialize translations and add default language
200 bool lamexp_translation_init(void)
202 QWriteLocker
writeLockTranslations(&g_lamexp_translation
.lock
);
204 if((!g_lamexp_translation
.files
) && (!g_lamexp_translation
.names
))
206 g_lamexp_translation
.files
= new QMap
<QString
, QString
>();
207 g_lamexp_translation
.names
= new QMap
<QString
, QString
>();
208 g_lamexp_translation
.files
->insert(LAMEXP_DEFAULT_LANGID
, "");
209 g_lamexp_translation
.names
->insert(LAMEXP_DEFAULT_LANGID
, "English");
214 qWarning("[lamexp_translation_init] Error: Already initialized!");
220 * Register a new translation
222 bool lamexp_translation_register(const QString
&langId
, const QString
&qmFile
, const QString
&langName
, unsigned int &systemId
, unsigned int &country
)
224 QWriteLocker
writeLockTranslations(&g_lamexp_translation
.lock
);
226 if(qmFile
.isEmpty() || langName
.isEmpty() || systemId
< 1)
231 if(!g_lamexp_translation
.files
) g_lamexp_translation
.files
= new QMap
<QString
, QString
>();
232 if(!g_lamexp_translation
.names
) g_lamexp_translation
.names
= new QMap
<QString
, QString
>();
233 if(!g_lamexp_translation
.sysid
) g_lamexp_translation
.sysid
= new QMap
<QString
, unsigned int>();
234 if(!g_lamexp_translation
.cntry
) g_lamexp_translation
.cntry
= new QMap
<QString
, unsigned int>();
236 g_lamexp_translation
.files
->insert(langId
, qmFile
);
237 g_lamexp_translation
.names
->insert(langId
, langName
);
238 g_lamexp_translation
.sysid
->insert(langId
, systemId
);
239 g_lamexp_translation
.cntry
->insert(langId
, country
);
245 * Get list of all translations
247 QStringList
lamexp_query_translations(void)
249 QReadLocker
readLockTranslations(&g_lamexp_translation
.lock
);
250 return (g_lamexp_translation
.files
) ? g_lamexp_translation
.files
->keys() : QStringList();
254 * Get translation name
256 QString
lamexp_translation_name(const QString
&langId
)
258 QReadLocker
readLockTranslations(&g_lamexp_translation
.lock
);
259 return (g_lamexp_translation
.names
) ? g_lamexp_translation
.names
->value(langId
.toLower(), QString()) : QString();
263 * Get translation system id
265 unsigned int lamexp_translation_sysid(const QString
&langId
)
267 QReadLocker
readLockTranslations(&g_lamexp_translation
.lock
);
268 return (g_lamexp_translation
.sysid
) ? g_lamexp_translation
.sysid
->value(langId
.toLower(), 0) : 0;
272 * Get translation script id
274 unsigned int lamexp_translation_country(const QString
&langId
)
276 QReadLocker
readLockTranslations(&g_lamexp_translation
.lock
);
277 return (g_lamexp_translation
.cntry
) ? g_lamexp_translation
.cntry
->value(langId
.toLower(), 0) : 0;
281 * Install a new translator
283 bool lamexp_install_translator(const QString
&langId
)
285 bool success
= false;
286 const QString
qmFileToPath(":/localization/%1");
288 if(langId
.isEmpty() || langId
.toLower().compare(LAMEXP_DEFAULT_LANGID
) == 0)
290 success
= lamexp_install_translator_from_file(qmFileToPath
.arg(LAMEXP_DEFAULT_TRANSLATION
));
294 QReadLocker
readLock(&g_lamexp_translation
.lock
);
295 QString qmFile
= (g_lamexp_translation
.files
) ? g_lamexp_translation
.files
->value(langId
.toLower(), QString()) : QString();
298 if(!qmFile
.isEmpty())
300 success
= lamexp_install_translator_from_file(qmFileToPath
.arg(qmFile
));
304 qWarning("Translation '%s' not available!", langId
.toLatin1().constData());
312 * Install a new translator from file
314 bool lamexp_install_translator_from_file(const QString
&qmFile
)
316 QWriteLocker
writeLock(&g_lamexp_currentTranslator
.lock
);
317 bool success
= false;
319 if(!g_lamexp_currentTranslator
.instance
)
321 g_lamexp_currentTranslator
.instance
= new QTranslator();
324 if(!qmFile
.isEmpty())
326 QString qmPath
= QFileInfo(qmFile
).canonicalFilePath();
327 QApplication::removeTranslator(g_lamexp_currentTranslator
.instance
);
328 if(success
= g_lamexp_currentTranslator
.instance
->load(qmPath
))
330 QApplication::installTranslator(g_lamexp_currentTranslator
.instance
);
334 qWarning("Failed to load translation:\n\"%s\"", qmPath
.toLatin1().constData());
339 QApplication::removeTranslator(g_lamexp_currentTranslator
.instance
);
346 ///////////////////////////////////////////////////////////////////////////////
348 ///////////////////////////////////////////////////////////////////////////////
350 extern "C" void _lamexp_global_init_tools(void)
352 LAMEXP_ZERO_MEMORY(g_lamexp_tools
);
353 LAMEXP_ZERO_MEMORY(g_lamexp_currentTranslator
);
354 LAMEXP_ZERO_MEMORY(g_lamexp_translation
);
357 ///////////////////////////////////////////////////////////////////////////////
359 ///////////////////////////////////////////////////////////////////////////////
361 extern "C" void _lamexp_global_free_tools(void)
363 //Free *all* registered translations
364 if(g_lamexp_currentTranslator
.instance
)
366 QApplication::removeTranslator(g_lamexp_currentTranslator
.instance
);
367 LAMEXP_DELETE(g_lamexp_currentTranslator
.instance
);
369 LAMEXP_DELETE(g_lamexp_translation
.files
);
370 LAMEXP_DELETE(g_lamexp_translation
.names
);
371 LAMEXP_DELETE(g_lamexp_translation
.cntry
);
372 LAMEXP_DELETE(g_lamexp_translation
.sysid
);
374 //Free *all* registered tools
375 if(g_lamexp_tools
.registry
)
377 QStringList keys
= g_lamexp_tools
.registry
->keys();
378 for(int i
= 0; i
< keys
.count(); i
++)
380 LockedFile
*lf
= g_lamexp_tools
.registry
->take(keys
.at(i
));
383 g_lamexp_tools
.registry
->clear();
384 g_lamexp_tools
.versions
->clear();
385 g_lamexp_tools
.tags
->clear();
387 LAMEXP_DELETE(g_lamexp_tools
.registry
);
388 LAMEXP_DELETE(g_lamexp_tools
.versions
);
389 LAMEXP_DELETE(g_lamexp_tools
.tags
);