Moved translation support into MUtilities library + make clean-up of temporary files work again + various minor fixes.

This commit is contained in:
LoRd_MuldeR 2014-12-20 23:47:06 +01:00
parent 00013f50f0
commit bc3701305d
14 changed files with 350 additions and 50 deletions

View File

@ -33,6 +33,7 @@
<ClCompile Include="src\Startup.cpp" />
<ClCompile Include="src\Taskbar7_Win32.cpp" />
<ClCompile Include="src\Terminal_Win32.cpp" />
<ClCompile Include="src\Translation.cpp" />
<ClCompile Include="src\UpdateChecker.cpp" />
<ClCompile Include="src\Version.cpp" />
</ItemGroup>
@ -50,6 +51,7 @@
<ClInclude Include="include\MUtils\Startup.h" />
<ClInclude Include="include\MUtils\Taskbar7.h" />
<ClInclude Include="include\MUtils\Terminal.h" />
<ClInclude Include="include\MUtils\Translation.h" />
<ClInclude Include="src\3rd_party\adler32\include\adler32.h" />
<ClInclude Include="src\3rd_party\keccak\include\keccak_impl.h" />
<ClInclude Include="src\3rd_party\strnatcmp\include\strnatcmp.h" />
@ -149,6 +151,9 @@
<EnableEnhancedInstructionSet>NoExtensions</EnableEnhancedInstructionSet>
<AdditionalIncludeDirectories>$(ProjectDir)\include;$(QTDIR)\include;$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(SolutionDir)\..\Prerequisites\VisualLeakDetector\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@ -87,6 +87,9 @@
<ClCompile Include="src\Taskbar7_Win32.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Translation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\CriticalSection_Win32.h">
@ -149,6 +152,9 @@
<ClInclude Include="include\MUtils\Taskbar7.h">
<Filter>Public Headers</Filter>
</ClInclude>
<ClInclude Include="include\MUtils\Translation.h">
<Filter>Public Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="include\Mutils\UpdateChecker.h">

View File

@ -24,6 +24,9 @@
//MUtils
#include <MUtils/Global.h>
//Forward Declarations
class QApplication;
///////////////////////////////////////////////////////////////////////////////
namespace MUtils
@ -37,7 +40,7 @@ namespace MUtils
MUTILS_API int startup(int &argc, char **argv, main_function_t *const entry_point, const bool &debugConsole);
//Initialize Qt
MUTILS_API bool init_qt(int &argc, char **argv, const QString &appName);
MUTILS_API QApplication *create_qt(int &argc, char **argv, const QString &appName);
}
}

View File

@ -0,0 +1,56 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// http://www.gnu.org/licenses/lgpl-2.1.txt
//////////////////////////////////////////////////////////////////////////////////
#pragma once
//MUtils
#include <MUtils/Global.h>
//Qt
#include <QtGlobal>
///////////////////////////////////////////////////////////////////////////////
namespace MUtils
{
namespace Translation
{
//Register new translation
MUTILS_API bool insert(const QString &langId, const QString &qmFile, const QString &langName, const quint32 &systemId, const quint32 &country);
//Enumerate translations
MUTILS_API int enumerate(QStringList &list);
//Get translation info
MUTILS_API QString get_name (const QString &langId);
MUTILS_API quint32 get_sysid (const QString &langId);
MUTILS_API quint32 get_country(const QString &langId);
//Install translator
MUTILS_API bool install_translator(const QString &langId);
MUTILS_API bool install_translator_from_file(const QString &qmFile);
//Constant
MUTILS_API extern const char *const DEFAULT_LANGID;
}
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -31,11 +31,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
};
return TRUE;
}

View File

@ -56,16 +56,16 @@ namespace MUtils
:
m_dirPath(dirPath)
{
bool okay = false;
const QByteArray testData = QByteArray(TEST_DATA);
if(m_dirPath.isEmpty())
{
throw DirLockException("Path must not be empty!");
}
const QByteArray testData = QByteArray(TEST_DATA);
bool okay = false;
for(int i = 0; i < 32; i++)
{
m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::rand_str())));
if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
{
if(m_lockFile->write(testData) >= testData.size())
{
@ -83,20 +83,34 @@ namespace MUtils
~DirLock(void)
{
bool okay = false;
if(!m_lockFile.isNull())
{
m_lockFile->remove();
for(int i = 0; i < 8; i++)
{
if(m_lockFile->remove())
{
break;
}
OS::sleep_ms(1);
}
}
for(int i = 0; i < 8; i++)
{
if(MUtils::remove_directory(m_dirPath))
{
okay = true;
break;
}
OS::sleep_ms(1);
}
if(!okay)
{
OS::system_message_wrn(L"Directory Lock", L"Warning: Not all temporary files could be removed!");
}
}
inline const QString &path(void) const
inline const QString &getPath(void) const
{
return m_dirPath;
}

View File

@ -19,8 +19,6 @@
// http://www.gnu.org/licenses/lgpl-2.1.txt
//////////////////////////////////////////////////////////////////////////////////
#pragma once
//MUtils
#include <MUtils/ErrorHandler.h>
#include <MUtils/OSSupport.h>

View File

@ -46,7 +46,7 @@ static QReadWriteLock g_themes_lock;
static bool g_themes_initialized = false;
static bool g_themes_enabled = false;
typedef int (WINAPI *IsAppThemedFunction)(void);
typedef int (WINAPI IsAppThemedFunction)(void);
bool MUtils::GUI::themes_enabled(void)
{
@ -68,13 +68,10 @@ bool MUtils::GUI::themes_enabled(void)
const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
if(osVersion >= MUtils::OS::Version::WINDOWS_WINXP)
{
IsAppThemedFunction IsAppThemedPtr = NULL;
QLibrary uxTheme("UxTheme.dll");
if(uxTheme.load())
{
IsAppThemedPtr = (IsAppThemedFunction) uxTheme.resolve("IsAppThemed");
}
if(IsAppThemedPtr)
if(IsAppThemedFunction *const IsAppThemedPtr = (IsAppThemedFunction*) uxTheme.resolve("IsAppThemed"))
{
g_themes_enabled = IsAppThemedPtr();
if(!g_themes_enabled)
@ -83,6 +80,7 @@ bool MUtils::GUI::themes_enabled(void)
}
}
}
}
g_themes_initialized = true;
return g_themes_enabled;

View File

@ -43,7 +43,9 @@
#include <process.h>
//VLD
#ifdef _MSC_VER
#include <vld.h>
#endif
///////////////////////////////////////////////////////////////////////////////
// Random Support
@ -150,6 +152,17 @@ static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
return NULL;
}
static void temp_folder_cleaup(void)
{
QWriteLocker writeLock(&g_temp_folder_lock);
//Clean the directory
while(!g_temp_folder_file.isNull())
{
g_temp_folder_file.reset(NULL);
}
}
const QString &MUtils::temp_folder(void)
{
QReadLocker readLock(&g_temp_folder_lock);
@ -157,7 +170,7 @@ const QString &MUtils::temp_folder(void)
//Already initialized?
if(!g_temp_folder_file.isNull())
{
return g_temp_folder_file->path();
return g_temp_folder_file->getPath();
}
//Obtain the write lock to initilaize
@ -167,14 +180,15 @@ const QString &MUtils::temp_folder(void)
//Still uninitilaized?
if(!g_temp_folder_file.isNull())
{
return g_temp_folder_file->path();
return g_temp_folder_file->getPath();
}
//Try the %TMP% or %TEMP% directory first
if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
{
g_temp_folder_file.reset(lockFile);
return lockFile->path();
atexit(temp_folder_cleaup);
return lockFile->getPath();
}
qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
@ -190,7 +204,8 @@ const QString &MUtils::temp_folder(void)
if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
{
g_temp_folder_file.reset(lockFile);
return lockFile->path();
atexit(temp_folder_cleaup);
return lockFile->getPath();
}
}
}

View File

@ -19,8 +19,6 @@
// http://www.gnu.org/licenses/lgpl-2.1.txt
//////////////////////////////////////////////////////////////////////////////////
#pragma once
//Win32 API
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>

View File

@ -125,20 +125,19 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr
// QT INITIALIZATION
///////////////////////////////////////////////////////////////////////////////
static QMutex g_qt_lock;
static QScopedPointer<QApplication> g_application;
static QMutex g_init_lock;
static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL};
bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName)
{
QMutexLocker lock(&g_qt_lock);
QMutexLocker lock(&g_init_lock);
const QStringList &arguments = MUtils::OS::arguments();
//Don't initialized again, if done already
if(!g_application.isNull())
if(QApplication::instance() != NULL)
{
return true;
qWarning("Qt is already initialized!");
return NULL;
}
//Extract executable name from argv[] array
@ -205,7 +204,7 @@ bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
if(!arguments.contains("--ignore-compat-mode", Qt::CaseInsensitive))
{
qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData());
return false;
return NULL;
}
}
@ -219,17 +218,17 @@ bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
//Create Qt application instance
g_application.reset(new QApplication(argc, argv));
QApplication *application = new QApplication(argc, argv);
//Load plugins from application directory
QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
qDebug("Library Path:\n%s\n", MUTILS_UTF8(QApplication::libraryPaths().first()));
//Set application properties
g_application->setApplicationName(appName);
g_application->setOrganizationName("LoRd_MuldeR");
g_application->setOrganizationDomain("mulder.at.gg");
g_application->setEventFilter(qt_event_filter);
application->setApplicationName(appName);
application->setOrganizationName("LoRd_MuldeR");
application->setOrganizationDomain("mulder.at.gg");
application->setEventFilter(qt_event_filter);
//Check for supported image formats
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
@ -238,7 +237,8 @@ bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
if(!supportedFormats.contains(g_imageformats[i]))
{
qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats[i]);
return false;
MUTILS_DELETE(application);
return NULL;
}
}
@ -254,9 +254,9 @@ bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
if(!qFuzzyCompare(fontScaleFactor, 1.0))
{
qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor);
QFont appFont = g_application->font();
QFont appFont = application->font();
appFont.setPointSizeF(appFont.pointSizeF() * fontScaleFactor);
g_application->setFont(appFont);
application->setFont(appFont);
}
//Check for process elevation
@ -267,12 +267,14 @@ bool MUtils::Startup::init_qt(int &argc, char **argv, const QString &appName)
messageBox.addButton("Ignore", QMessageBox::NoRole);
if(messageBox.exec() == 0)
{
MUTILS_DELETE(application);
return NULL;
}
}
//Successful
return g_application.data();
//Qt created successfully
return application;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -25,7 +25,6 @@
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
//Internal
#include <MUtils/Terminal.h>
#include <MUtils/Global.h>
@ -184,6 +183,7 @@ void MUtils::Terminal::setup(int &argc, char **argv, const bool forceEnabled)
*stderr = *hfStdErr;
g_filebufStdErr.reset(new std::filebuf(hfStdErr));
std::cerr.rdbuf(g_filebufStdErr.data());
std::cerr.rdbuf(new std::filebuf(hfStdErr));
}
const HWND hwndConsole = GetConsoleWindow();

205
src/Translation.cpp Normal file
View File

@ -0,0 +1,205 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// http://www.gnu.org/licenses/lgpl-2.1.txt
//////////////////////////////////////////////////////////////////////////////////
//MUtils
#include <MUtils/Translation.h>
//Qt
#include <QPair>
#include <QReadWriteLock>
#include <QMap>
#include <QStringList>
#include <QTranslator>
#include <QFileInfo>
#include <QApplication>
//////////////////////////////////////////////////////////////////////////////////
// TYPES
//////////////////////////////////////////////////////////////////////////////////
typedef QPair<QString,QString> translation_info_t;
typedef QPair<quint32,quint32> translation_data_t;
typedef QPair<translation_info_t, translation_data_t> translation_entry_t;
typedef QMap<QString, translation_entry_t> translation_store_t;
#define MAKE_ENTRY(NAME,PATH,SYSID,CNTRY) \
qMakePair(qMakePair((NAME),(PATH)),qMakePair((SYSID),(CNTRY)))
//////////////////////////////////////////////////////////////////////////////////
// TRANSLATIONS STORE
//////////////////////////////////////////////////////////////////////////////////
static QReadWriteLock g_translation_lock;
static QScopedPointer<translation_store_t> g_translation_data;
static QScopedPointer<QTranslator> g_translation_inst;
//////////////////////////////////////////////////////////////////////////////////
// CONSTANT
//////////////////////////////////////////////////////////////////////////////////
namespace MUtils
{
namespace Translation
{
const char *const DEFAULT_LANGID = "en";
}
}
//////////////////////////////////////////////////////////////////////////////////
// REGISTER TRANSLATION
//////////////////////////////////////////////////////////////////////////////////
bool MUtils::Translation::insert(const QString &langId, const QString &qmFile, const QString &langName, const quint32 &systemId, const quint32 &country)
{
QWriteLocker writeLockTranslations(&g_translation_lock);
const QString key = langId.simplified().toLower();
if(key.isEmpty() || qmFile.isEmpty() || langName.isEmpty() || (systemId < 1))
{
return false;
}
if(g_translation_data.isNull())
{
g_translation_data.reset(new translation_store_t());
}
if(g_translation_data->contains(key))
{
qWarning("Translation store already contains entry for '%s', going to replace!", MUTILS_UTF8(key));
}
g_translation_data->insert(key, MAKE_ENTRY(langName, qmFile, systemId, country));
return true;
}
//////////////////////////////////////////////////////////////////////////////////
// GET TRANSLATION INFO
//////////////////////////////////////////////////////////////////////////////////
int MUtils::Translation::enumerate(QStringList &list)
{
QReadLocker readLockTranslations(&g_translation_lock);
if(g_translation_data.isNull())
{
list.clear();
return -1;
}
list.swap(g_translation_data->keys());
return list.count();
}
QString MUtils::Translation::get_name(const QString &langId)
{
QReadLocker readLockTranslations(&g_translation_lock);
const QString key = langId.simplified().toLower();
if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
{
return QString();
}
return (*g_translation_data)[key].first.first;
}
quint32 MUtils::Translation::get_sysid(const QString &langId)
{
QReadLocker readLockTranslations(&g_translation_lock);
const QString key = langId.simplified().toLower();
if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
{
return 0;
}
return (*g_translation_data)[key].second.first;
}
quint32 MUtils::Translation::get_country(const QString &langId)
{
QReadLocker readLockTranslations(&g_translation_lock);
const QString key = langId.simplified().toLower();
if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
{
return 0;
}
return (*g_translation_data)[key].second.second;
}
//////////////////////////////////////////////////////////////////////////////////
// INSTALL TRANSLATION
//////////////////////////////////////////////////////////////////////////////////
bool MUtils::Translation::install_translator(const QString &langId)
{
QReadLocker readLockTranslations(&g_translation_lock);
const QString key = langId.simplified().toLower();
if(key.isEmpty() || g_translation_data.isNull() || (!g_translation_data->contains(key)))
{
return false;
}
const QString qmFile = (*g_translation_data)[key].first.second;
readLockTranslations.unlock();
return install_translator_from_file(qmFile);
}
bool MUtils::Translation::install_translator_from_file(const QString &qmFile)
{
QWriteLocker writeLock(&g_translation_lock);
if(g_translation_inst.isNull())
{
g_translation_inst.reset(new QTranslator());
}
if(qmFile.isEmpty())
{
QApplication::removeTranslator(g_translation_inst.data());
return true;
}
const QFileInfo qmFileInfo(qmFile);
if(!(qmFileInfo.exists() && qmFileInfo.isFile()))
{
qWarning("Translation file not found:\n\"%s\"", MUTILS_UTF8(qmFile));
return false;
}
const QString qmPath = QFileInfo(qmFile).canonicalFilePath();
if(!qmPath.isEmpty())
{
QApplication::removeTranslator(g_translation_inst.data());
if(g_translation_inst->load(qmPath))
{
QApplication::installTranslator(g_translation_inst.data());
return true;
}
}
qWarning("Failed to load translation:\n\"%s\"", MUTILS_UTF8(qmFile));
return false;
}

View File

@ -19,14 +19,14 @@
// http://www.gnu.org/licenses/lgpl-2.1.txt
//////////////////////////////////////////////////////////////////////////////////
#define MUTILS_INC_CONFIG 1
//MUtils
#include <MUtils/Version.h>
//Internal
#include <MUtils/Global.h>
#include <MUtils/Exception.h>
#include <MUtils/OSSupport.h>
//Internal
#define MUTILS_INC_CONFIG 1
#include "Config.h"
///////////////////////////////////////////////////////////////////////////////