OSDN Git Service

Small optimization of lamexp_themes_enabled().
[lamexp/LameXP.git] / src / Global.cpp
index 9733f45..2cc2ff9 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 #include <QRegExp>
 #include <QResource>
 #include <QTranslator>
+#include <QEventLoop>
+#include <QTimer>
+#include <QLibraryInfo>
+#include <QEvent>
 
 //LameXP includes
 #include "Resource.h"
 #include "LockedFile.h"
 
-//Windows includes
-#include <Windows.h>
-
 //CRT includes
-#include <stdio.h>
+#include <iostream>
+#include <fstream>
 #include <io.h>
 #include <fcntl.h>
 #include <intrin.h>
+#include <math.h>
+#include <time.h>
+#include <process.h>
+
+//COM includes
+#include <Objbase.h>
+#include <PowrProf.h>
 
 //Debug only includes
-#ifdef _DEBUG
+#if LAMEXP_DEBUG
 #include <Psapi.h>
-#endif //_DEBUG
+#endif
 
-//Static build only macros
+//Initialize static Qt plugins
 #ifdef QT_NODLL
-#pragma warning(disable:4101)
-#define LAMEXP_INIT_QT_STATIC_PLUGIN(X) Q_IMPORT_PLUGIN(X)
-#else
-#define LAMEXP_INIT_QT_STATIC_PLUGIN(X)
+Q_IMPORT_PLUGIN(qico)
+Q_IMPORT_PLUGIN(qsvg)
 #endif
 
 ///////////////////////////////////////////////////////////////////////////////
 // TYPES
 ///////////////////////////////////////////////////////////////////////////////
 
+static const size_t g_lamexp_ipc_slots = 128;
+
 typedef struct
 {
        unsigned int command;
        unsigned int reserved_1;
        unsigned int reserved_2;
        char parameter[4096];
-} lamexp_ipc_t;
+}
+lamexp_ipc_data_t;
+
+typedef struct
+{
+       unsigned int pos_write;
+       unsigned int pos_read;
+       lamexp_ipc_data_t data[g_lamexp_ipc_slots];
+}
+lamexp_ipc_t;
 
 ///////////////////////////////////////////////////////////////////////////////
 // GLOBAL VARS
 ///////////////////////////////////////////////////////////////////////////////
 
 //Build version
-static const unsigned int g_lamexp_version_major = VER_LAMEXP_MAJOR;
-static const unsigned int g_lamexp_version_minor = VER_LAMEXP_MINOR;
-static const unsigned int g_lamexp_version_build = VER_LAMEXP_BUILD;
-static const char *g_lamexp_version_release = VER_LAMEXP_SUFFIX_STR;
+static const struct
+{
+       unsigned int ver_major;
+       unsigned int ver_minor;
+       unsigned int ver_build;
+       char *ver_release_name;
+}
+g_lamexp_version =
+{
+       VER_LAMEXP_MAJOR,
+       VER_LAMEXP_MINOR,
+       VER_LAMEXP_BUILD,
+       VER_LAMEXP_RNAME
+};
 
 //Build date
 static QDate g_lamexp_version_date;
 static const char *g_lamexp_months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 static const char *g_lamexp_version_raw_date = __DATE__;
-
-//Compiler version
-#if _MSC_VER == 1400
-       static const char *g_lamexp_version_compiler = "MSVC 8.0";
-#else
-       #if _MSC_VER == 1500
-               static const char *g_lamexp_version_compiler = "MSVC 9.0";
+static const char *g_lamexp_version_raw_time = __TIME__;
+
+//Console attached flag
+static bool g_lamexp_console_attached = false;
+
+//Compiler detection
+//The following code was borrowed from MPC-HC project: http://mpc-hc.sf.net/
+#if defined(__INTEL_COMPILER)
+       #if (__INTEL_COMPILER >= 1200)
+               static const char *g_lamexp_version_compiler = "ICL 12.x";
+       #elif (__INTEL_COMPILER >= 1100)
+               static const char *g_lamexp_version_compiler = "ICL 11.x";
+       #elif (__INTEL_COMPILER >= 1000)
+               static const char *g_lamexp_version_compiler = "ICL 10.x";
        #else
-               #if _MSC_VER == 1600
-                       static const char *g_lamexp_version_compiler = "MSVC 10.0";
+               #error Compiler is not supported!
+       #endif
+#elif defined(_MSC_VER)
+       #if (_MSC_VER == 1600)
+               #if (_MSC_FULL_VER >= 160040219)
+                       static const char *g_lamexp_version_compiler = "MSVC 2010-SP1";
                #else
-                       static const char *g_lamexp_version_compiler = "UNKNOWN";
+                       static const char *g_lamexp_version_compiler = "MSVC 2010";
+               #endif
+       #elif (_MSC_VER == 1500)
+               #if (_MSC_FULL_VER >= 150030729)
+                       static const char *g_lamexp_version_compiler = "MSVC 2008-SP1";
+               #else
+                       static const char *g_lamexp_version_compiler = "MSVC 2008";
+               #endif
+       #else
+               #error Compiler is not supported!
+       #endif
+
+       // Note: /arch:SSE and /arch:SSE2 are only available for the x86 platform
+       #if !defined(_M_X64) && defined(_M_IX86_FP)
+               #if (_M_IX86_FP == 1)
+                       LAMEXP_COMPILER_WARNING("SSE instruction set is enabled!")
+               #elif (_M_IX86_FP == 2)
+                       LAMEXP_COMPILER_WARNING("SSE2 instruction set is enabled!")
                #endif
        #endif
+#else
+       #error Compiler is not supported!
 #endif
 
-//Tool versions (expected)
+//Architecture detection
+#if defined(_M_X64)
+       static const char *g_lamexp_version_arch = "x64";
+#elif defined(_M_IX86)
+       static const char *g_lamexp_version_arch = "x86";
+#else
+       #error Architecture is not supported!
+#endif
+
+//Official web-site URL
+static const char *g_lamexp_website_url = "http://lamexp.sourceforge.net/";
+static const char *g_lamexp_support_url = "http://forum.doom9.org/showthread.php?t=157726";
+
+//Tool versions (expected versions!)
 static const unsigned int g_lamexp_toolver_neroaac = VER_LAMEXP_TOOL_NEROAAC;
+static const unsigned int g_lamexp_toolver_fhgaacenc = VER_LAMEXP_TOOL_FHGAACENC;
+static const unsigned int g_lamexp_toolver_qaacenc = VER_LAMEXP_TOOL_QAAC;
+static const unsigned int g_lamexp_toolver_coreaudio = VER_LAMEXP_TOOL_COREAUDIO;
 
 //Special folders
 static QString g_lamexp_temp_folder;
@@ -121,25 +195,60 @@ static QMap<QString, LockedFile*> g_lamexp_tool_registry;
 static QMap<QString, unsigned int> g_lamexp_tool_versions;
 
 //Languages
-static QMap<QString, QString> g_lamexp_translation_files;
-static QMap<QString, QString> g_lamexp_translation_names;
-static QMap<QString, unsigned int> g_lamexp_translation_sysid;
+static struct
+{
+       QMap<QString, QString> files;
+       QMap<QString, QString> names;
+       QMap<QString, unsigned int> sysid;
+       QMap<QString, unsigned int> cntry;
+}
+g_lamexp_translation;
+
+//Translator
 static QTranslator *g_lamexp_currentTranslator = NULL;
 
 //Shared memory
-static const char *g_lamexp_sharedmem_uuid = "{21A68A42-6923-43bb-9CF6-64BF151942EE}";
-static QSharedMemory *g_lamexp_sharedmem_ptr = NULL;
-static const char *g_lamexp_semaphore_read_uuid = "{7A605549-F58C-4d78-B4E5-06EFC34F405B}";
-static QSystemSemaphore *g_lamexp_semaphore_read_ptr = NULL;
-static const char *g_lamexp_semaphore_write_uuid = "{60AA8D04-F6B8-497d-81EB-0F600F4A65B5}";
-static QSystemSemaphore *g_lamexp_semaphore_write_ptr = NULL;
+static const struct
+{
+       char *sharedmem;
+       char *semaphore_read;
+       char *semaphore_read_mutex;
+       char *semaphore_write;
+       char *semaphore_write_mutex;
+}
+g_lamexp_ipc_uuid =
+{
+       "{21A68A42-6923-43bb-9CF6-64BF151942EE}",
+       "{7A605549-F58C-4d78-B4E5-06EFC34F405B}",
+       "{60AA8D04-F6B8-497d-81EB-0F600F4A65B5}",
+       "{726061D5-1615-4B82-871C-75FD93458E46}",
+       "{1A616023-AA6A-4519-8AF3-F7736E899977}"
+};
+static struct
+{
+       QSharedMemory *sharedmem;
+       QSystemSemaphore *semaphore_read;
+       QSystemSemaphore *semaphore_read_mutex;
+       QSystemSemaphore *semaphore_write;
+       QSystemSemaphore *semaphore_write_mutex;
+}
+g_lamexp_ipc_ptr =
+{
+       NULL, NULL, NULL
+};
 
 //Image formats
-static const char *g_lamexp_imageformats[] = {"png", "gif", "ico", "svg", NULL};
+static const char *g_lamexp_imageformats[] = {"png", "jpg", "gif", "ico", "svg", NULL};
 
 //Global locks
 static QMutex g_lamexp_message_mutex;
 
+//Main thread ID
+static const DWORD g_main_thread_id = GetCurrentThreadId();
+
+//Log file
+static FILE *g_lamexp_log_file = NULL;
+
 ///////////////////////////////////////////////////////////////////////////////
 // GLOBAL FUNCTIONS
 ///////////////////////////////////////////////////////////////////////////////
@@ -147,22 +256,48 @@ static QMutex g_lamexp_message_mutex;
 /*
  * Version getters
  */
-unsigned int lamexp_version_major(void) { return g_lamexp_version_major; }
-unsigned int lamexp_version_minor(void) { return g_lamexp_version_minor; }
-unsigned int lamexp_version_build(void) { return g_lamexp_version_build; }
-const char *lamexp_version_release(void) { return g_lamexp_version_release; }
-const char *lamexp_version_compiler(void) {return g_lamexp_version_compiler; }
+unsigned int lamexp_version_major(void) { return g_lamexp_version.ver_major; }
+unsigned int lamexp_version_minor(void) { return g_lamexp_version.ver_minor; }
+unsigned int lamexp_version_build(void) { return g_lamexp_version.ver_build; }
+const char *lamexp_version_release(void) { return g_lamexp_version.ver_release_name; }
+const char *lamexp_version_time(void) { return g_lamexp_version_raw_time; }
+const char *lamexp_version_compiler(void) { return g_lamexp_version_compiler; }
+const char *lamexp_version_arch(void) { return g_lamexp_version_arch; }
 unsigned int lamexp_toolver_neroaac(void) { return g_lamexp_toolver_neroaac; }
+unsigned int lamexp_toolver_fhgaacenc(void) { return g_lamexp_toolver_fhgaacenc; }
+unsigned int lamexp_toolver_qaacenc(void) { return g_lamexp_toolver_qaacenc; }
+unsigned int lamexp_toolver_coreaudio(void) { return g_lamexp_toolver_coreaudio; }
 
-bool lamexp_version_demo(void)
-{ 
+/*
+ * URL getters
+ */
+const char *lamexp_website_url(void) { return g_lamexp_website_url; }
+const char *lamexp_support_url(void) { return g_lamexp_support_url; }
 
-       return LAMEXP_DEBUG || !(strstr(g_lamexp_version_release, "Final") || strstr(g_lamexp_version_release, "Hotfix"));
+/*
+ * Check for Demo (pre-release) version
+ */
+bool lamexp_version_demo(void)
+{
+       char buffer[128];
+       bool releaseVersion = false;
+       if(!strncpy_s(buffer, 128, g_lamexp_version.ver_release_name, _TRUNCATE))
+       {
+               char *context, *prefix = strtok_s(buffer, "-,; ", &context);
+               if(prefix)
+               {
+                       releaseVersion = (!_stricmp(prefix, "Final")) || (!_stricmp(prefix, "Hotfix"));
+               }
+       }
+       return LAMEXP_DEBUG || (!releaseVersion);
 }
 
+/*
+ * Calculate expiration date
+ */
 QDate lamexp_version_expires(void)
 {
-       return lamexp_version_date().addDays(30);
+       return lamexp_version_date().addDays(LAMEXP_DEBUG ? 2 : 30);
 }
 
 /*
@@ -172,39 +307,30 @@ const QDate &lamexp_version_date(void)
 {
        if(!g_lamexp_version_date.isValid())
        {
-               char temp[32];
-               int date[3];
+               int date[3] = {0, 0, 0}; char temp[12] = {'\0'};
+               strncpy_s(temp, 12, g_lamexp_version_raw_date, _TRUNCATE);
 
-               char *this_token = NULL;
-               char *next_token = NULL;
-
-               strcpy_s(temp, 32, g_lamexp_version_raw_date);
-               this_token = strtok_s(temp, " ", &next_token);
-
-               for(int i = 0; i < 3; i++)
+               if(strlen(temp) == 11)
                {
-                       date[i] = -1;
-                       if(this_token)
+                       temp[3] = temp[6] = '\0';
+                       date[2] = atoi(&temp[4]);
+                       date[0] = atoi(&temp[7]);
+                       
+                       for(int j = 0; j < 12; j++)
                        {
-                               for(int j = 0; j < 12; j++)
+                               if(!_strcmpi(&temp[0], g_lamexp_months[j]))
                                {
-                                       if(!_strcmpi(this_token, g_lamexp_months[j]))
-                                       {
-                                               date[i] = j+1;
-                                               break;
-                                       }
-                               }
-                               if(date[i] < 0)
-                               {
-                                       date[i] = atoi(this_token);
+                                       date[1] = j+1;
+                                       break;
                                }
-                               this_token = strtok_s(NULL, " ", &next_token);
                        }
+
+                       g_lamexp_version_date = QDate(date[0], date[1], date[2]);
                }
 
-               if(date[0] >= 0 && date[1] >= 0 && date[2] >= 0)
+               if(!g_lamexp_version_date.isValid())
                {
-                       g_lamexp_version_date = QDate(date[2], date[0], date[1]);
+                       qFatal("Internal error: Date format could not be recognized!");
                }
        }
 
@@ -212,87 +338,265 @@ const QDate &lamexp_version_date(void)
 }
 
 /*
+ * Get the native operating system version
+ */
+DWORD lamexp_get_os_version(void)
+{
+       static DWORD osVersion = 0;
+       
+       if(!osVersion)
+       {
+               OSVERSIONINFO osVerInfo;
+               memset(&osVerInfo, 0, sizeof(OSVERSIONINFO));
+               osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+       
+               if(GetVersionEx(&osVerInfo) == TRUE)
+               {
+                       if(osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
+                       {
+                               throw "Ouuups: Not running under Windows NT. This is not supposed to happen!";
+                       }
+                       osVersion = (DWORD)((osVerInfo.dwMajorVersion << 16) | (osVerInfo.dwMinorVersion & 0xffff));
+               }
+       }
+
+       return osVersion;
+}
+
+/*
+ * Check if we are running under wine
+ */
+bool lamexp_detect_wine(void)
+{
+       static bool isWine = false;
+       static bool isWine_initialized = false;
+
+       if(!isWine_initialized)
+       {
+               QLibrary ntdll("ntdll.dll");
+               if(ntdll.load())
+               {
+                       if(ntdll.resolve("wine_nt_to_unix_file_name") != NULL) isWine = true;
+                       if(ntdll.resolve("wine_get_version") != NULL) isWine = true;
+                       ntdll.unload();
+               }
+               isWine_initialized = true;
+       }
+
+       return isWine;
+}
+
+/*
+ * Global exception handler
+ */
+LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo)
+{
+       if(GetCurrentThreadId() != g_main_thread_id)
+       {
+               HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
+               if(mainThread) TerminateThread(mainThread, ULONG_MAX);
+       }
+       
+       FatalAppExit(0, L"Unhandeled exception handler invoked, application will exit!");
+       TerminateProcess(GetCurrentProcess(), -1);
+       return LONG_MAX;
+}
+
+/*
+ * Invalid parameters handler
+ */
+void lamexp_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t)
+{
+       if(GetCurrentThreadId() != g_main_thread_id)
+       {
+               HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
+               if(mainThread) TerminateThread(mainThread, ULONG_MAX);
+               
+       }
+       
+       FatalAppExit(0, L"Invalid parameter handler invoked, application will exit!");
+       TerminateProcess(GetCurrentProcess(), -1);
+}
+
+/*
+ * Change console text color
+ */
+static void lamexp_console_color(FILE* file, WORD attributes)
+{
+       const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
+       if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
+       {
+               SetConsoleTextAttribute(hConsole, attributes);
+       }
+}
+
+/*
  * Qt message handler
  */
 void lamexp_message_handler(QtMsgType type, const char *msg)
 {
-       static HANDLE hConsole = NULL;
+       static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
+       
        QMutexLocker lock(&g_lamexp_message_mutex);
 
-       if(!hConsole)
+       if(g_lamexp_log_file)
        {
-               hConsole = CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
-               if(hConsole == INVALID_HANDLE_VALUE) hConsole = NULL;
+               static char prefix[] = "DWCF";
+               int index = qBound(0, static_cast<int>(type), 3);
+               unsigned int timestamp = static_cast<unsigned int>(_time64(NULL) % 3600I64);
+               QString str = QString::fromUtf8(msg).trimmed().replace('\n', '\t');
+               fprintf(g_lamexp_log_file, "[%c][%04u] %s\r\n", prefix[index], timestamp, str.toUtf8().constData());
+               fflush(g_lamexp_log_file);
        }
 
-       CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
-       GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
-       SetConsoleOutputCP(CP_UTF8);
-
-       switch(type)
+       if(g_lamexp_console_attached)
        {
-       case QtCriticalMsg:
-       case QtFatalMsg:
-               fflush(stdout);
-               fflush(stderr);
-               SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
-               fwprintf(stderr, L"\nGURU MEDITATION !!!\n%S\n\n", msg);
-               MessageBoxW(NULL, (wchar_t*) QString::fromUtf8(msg).utf16(), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
-               break;
-       case QtWarningMsg:
-               SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
-               //MessageBoxW(NULL, (wchar_t*) QString::fromUtf8(msg).utf16(), L"LameXP - GURU MEDITATION", MB_ICONWARNING | MB_TOPMOST | MB_TASKMODAL);
-               fwprintf(stderr, L"%S\n", msg);
-               fflush(stderr);
-               break;
-       default:
-               SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
-               fwprintf(stderr, L"%S\n", msg);
-               fflush(stderr);
-               break;
+               UINT oldOutputCP = GetConsoleOutputCP();
+               if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
+
+               switch(type)
+               {
+               case QtCriticalMsg:
+               case QtFatalMsg:
+                       fflush(stdout);
+                       fflush(stderr);
+                       lamexp_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY);
+                       fprintf(stderr, GURU_MEDITATION);
+                       fprintf(stderr, "%s\n", msg);
+                       fflush(stderr);
+                       break;
+               case QtWarningMsg:
+                       lamexp_console_color(stderr, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
+                       fprintf(stderr, "%s\n", msg);
+                       fflush(stderr);
+                       break;
+               default:
+                       lamexp_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
+                       fprintf(stderr, "%s\n", msg);
+                       fflush(stderr);
+                       break;
+               }
+       
+               lamexp_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
+               if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(oldOutputCP);
        }
+       else
+       {
+               QString temp("[LameXP][%1] %2");
+               
+               switch(type)
+               {
+               case QtCriticalMsg:
+               case QtFatalMsg:
+                       temp = temp.arg("C", QString::fromUtf8(msg));
+                       break;
+               case QtWarningMsg:
+                       temp = temp.arg("W", QString::fromUtf8(msg));
+                       break;
+               default:
+                       temp = temp.arg("I", QString::fromUtf8(msg));
+                       break;
+               }
 
-       SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
+               temp.replace("\n", "\t").append("\n");
+               OutputDebugStringA(temp.toLatin1().constData());
+       }
 
        if(type == QtCriticalMsg || type == QtFatalMsg)
        {
                lock.unlock();
+
+               if(GetCurrentThreadId() != g_main_thread_id)
+               {
+                       HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
+                       if(mainThread) TerminateThread(mainThread, ULONG_MAX);
+               }
+
+               MessageBoxW(NULL, QWCHAR(QString::fromUtf8(msg)), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
                FatalAppExit(0, L"The application has encountered a critical error and will exit now!");
                TerminateProcess(GetCurrentProcess(), -1);
        }
- }
+}
 
 /*
  * Initialize the console
  */
 void lamexp_init_console(int argc, char* argv[])
 {
-       for(int i = 0; i < argc; i++)
+       bool enableConsole = lamexp_version_demo();
+
+       if(_environ)
+       {
+               wchar_t *logfile = NULL;
+               size_t logfile_len = 0;
+               if(!_wdupenv_s(&logfile, &logfile_len, L"LAMEXP_LOGFILE"))
+               {
+                       if(logfile && (logfile_len > 0))
+                       {
+                               FILE *temp = NULL;
+                               if(!_wfopen_s(&temp, logfile, L"wb"))
+                               {
+                                       fprintf(temp, "%c%c%c", 0xEF, 0xBB, 0xBF);
+                                       g_lamexp_log_file = temp;
+                               }
+                               free(logfile);
+                       }
+               }
+       }
+
+       if(!LAMEXP_DEBUG)
+       {
+               for(int i = 0; i < argc; i++)
+               {
+                       if(!_stricmp(argv[i], "--console"))
+                       {
+                               enableConsole = true;
+                       }
+                       else if(!_stricmp(argv[i], "--no-console"))
+                       {
+                               enableConsole = false;
+                       }
+               }
+       }
+
+       if(enableConsole)
        {
-               if(lamexp_version_demo() || !_stricmp(argv[i], "--console"))
+               if(!g_lamexp_console_attached)
                {
-                       if(AllocConsole())
+                       if(AllocConsole() != FALSE)
                        {
-                               //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
-                               int hCrtStdOut = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
-                               int hCrtStdErr = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
-                               FILE *hfStdOut = _fdopen(hCrtStdOut, "w");
-                               FILE *hfStderr = _fdopen(hCrtStdErr, "w");
-                               *stdout = *hfStdOut;
-                               *stderr = *hfStderr;
-                               setvbuf(stdout, NULL, _IONBF, 0);
-                               setvbuf(stderr, NULL, _IONBF, 0);
+                               SetConsoleCtrlHandler(NULL, TRUE);
+                               SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
+                               SetConsoleOutputCP(CP_UTF8);
+                               g_lamexp_console_attached = true;
                        }
+               }
+               
+               if(g_lamexp_console_attached)
+               {
+                       //-------------------------------------------------------------------
+                       //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
+                       //-------------------------------------------------------------------
+                       const int flags = _O_WRONLY | _O_U8TEXT;
+                       int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
+                       int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE), flags);
+                       FILE *hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
+                       FILE *hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
+                       if(hfStdOut) { *stdout = *hfStdOut; std::cout.rdbuf(new std::filebuf(hfStdOut)); }
+                       if(hfStdErr) { *stderr = *hfStdErr; std::cerr.rdbuf(new std::filebuf(hfStdErr)); }
+               }
 
-                       HMENU hMenu = GetSystemMenu(GetConsoleWindow(), 0);
+               HWND hwndConsole = GetConsoleWindow();
+
+               if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
+               {
+                       HMENU hMenu = GetSystemMenu(hwndConsole, 0);
                        EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
                        RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
 
-                       SetConsoleCtrlHandler(NULL, TRUE);
-                       SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
-                       SetConsoleOutputCP(CP_UTF8);
-                       
-                       break;
+                       SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
+                       SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
+                       SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
                }
        }
 }
@@ -300,7 +604,7 @@ void lamexp_init_console(int argc, char* argv[])
 /*
  * Detect CPU features
  */
-lamexp_cpu_t lamexp_detect_cpu_features(void)
+lamexp_cpu_t lamexp_detect_cpu_features(int argc, char **argv)
 {
        typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process);
        typedef VOID (WINAPI *GetNativeSystemInfoFun)(__out LPSYSTEM_INFO lpSystemInfo);
@@ -313,7 +617,7 @@ lamexp_cpu_t lamexp_detect_cpu_features(void)
        int CPUInfo[4] = {-1};
        char CPUIdentificationString[0x40];
        char CPUBrandString[0x40];
-       
+
        memset(&features, 0, sizeof(lamexp_cpu_t));
        memset(&systemInfo, 0, sizeof(SYSTEM_INFO));
        memset(CPUIdentificationString, 0, sizeof(CPUIdentificationString));
@@ -324,7 +628,7 @@ lamexp_cpu_t lamexp_detect_cpu_features(void)
        memcpy(CPUIdentificationString + 4, &CPUInfo[3], sizeof(int));
        memcpy(CPUIdentificationString + 8, &CPUInfo[2], sizeof(int));
        features.intel = (_stricmp(CPUIdentificationString, "GenuineIntel") == 0);
-       strcpy_s(features.vendor, 0x40, CPUIdentificationString);
+       strncpy_s(features.vendor, 0x40, CPUIdentificationString, _TRUNCATE);
 
        if(CPUInfo[0] >= 1)
        {
@@ -341,17 +645,29 @@ lamexp_cpu_t lamexp_detect_cpu_features(void)
        }
 
        __cpuid(CPUInfo, 0x80000000);
-       int nExIds = CPUInfo[0];
+       int nExIds = qMax<int>(qMin<int>(CPUInfo[0], 0x80000004), 0x80000000);
 
-       for(int i = 0x80000000; i <= nExIds; ++i)
+       for(int i = 0x80000002; i <= nExIds; ++i)
        {
                __cpuid(CPUInfo, i);
-               if(i == 0x80000002) memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
-               else if(i == 0x80000003) memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
-               else if(i == 0x80000004) memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
+               switch(i)
+               {
+               case 0x80000002:
+                       memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
+                       break;
+               case 0x80000003:
+                       memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
+                       break;
+               case 0x80000004:
+                       memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
+                       break;
+               }
        }
 
-       strcpy_s(features.brand, 0x40, CPUBrandString);
+       strncpy_s(features.brand, 0x40, CPUBrandString, _TRUNCATE);
+
+       if(strlen(features.brand) < 1) strncpy_s(features.brand, 0x40, "Unknown", _TRUNCATE);
+       if(strlen(features.vendor) < 1) strncpy_s(features.vendor, 0x40, "Unknown", _TRUNCATE);
 
 #if !defined(_M_X64 ) && !defined(_M_IA64)
        if(!IsWow64ProcessPtr || !GetNativeSystemInfoPtr)
@@ -376,37 +692,98 @@ lamexp_cpu_t lamexp_detect_cpu_features(void)
        {
                GetSystemInfo(&systemInfo);
        }
-       features.count = systemInfo.dwNumberOfProcessors;
+       features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
 #else
        GetNativeSystemInfo(&systemInfo);
        features.count = systemInfo.dwNumberOfProcessors;
        features.x64 = true;
 #endif
 
+       if((argv != NULL) && (argc > 0))
+       {
+               bool flag = false;
+               for(int i = 0; i < argc; i++)
+               {
+                       if(!_stricmp("--force-cpu-no-64bit", argv[i])) { flag = true; features.x64 = false; }
+                       if(!_stricmp("--force-cpu-no-sse", argv[i])) { flag = true; features.sse = features.sse2 = features.sse3 = features.ssse3 = false; }
+                       if(!_stricmp("--force-cpu-no-intel", argv[i])) { flag = true; features.intel = false; }
+               }
+               if(flag) qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
+       }
+
        return features;
 }
 
 /*
- * Check for debugger
+ * Check for debugger (detect routine)
  */
-void WINAPI debugThreadProc(__in LPVOID lpParameter)
+static __forceinline bool lamexp_check_for_debugger(void)
 {
-       BOOL remoteDebuggerPresent = FALSE;
-       //CheckRemoteDebuggerPresent(GetCurrentProcess, &remoteDebuggerPresent);
+       if(IsDebuggerPresent())
+       {
+               return true;
+       }
+       
+       __try
+       {
+               CloseHandle((HANDLE) 0x7FFFFFFF);
+       }
+       __except(EXCEPTION_EXECUTE_HANDLER)
+       {
+               return true;
+       }
 
-       while(!IsDebuggerPresent() && !remoteDebuggerPresent)
+       __try 
+       {
+               DebugBreak();
+       }
+       __except(EXCEPTION_EXECUTE_HANDLER) 
        {
-               Sleep(333);
-               //CheckRemoteDebuggerPresent(GetCurrentProcess, &remoteDebuggerPresent);
+               return false;
        }
        
+       return true;
+}
+
+/*
+ * Check for debugger (thread proc)
+ */
+static unsigned int __stdcall lamexp_debug_thread_proc(LPVOID lpParameter)
+{
+       while(!lamexp_check_for_debugger())
+       {
+               Sleep(32);
+       }
+       if(HANDLE thrd = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id))
+       {
+               if(TerminateThread(thrd, -1))
+               {
+                       FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
+               }
+               CloseHandle(thrd);
+       }
        TerminateProcess(GetCurrentProcess(), -1);
+       return 666;
+}
+
+/*
+ * Check for debugger (startup routine)
+ */
+static HANDLE lamexp_debug_thread_init(void)
+{
+       if(lamexp_check_for_debugger())
+       {
+               FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
+               TerminateProcess(GetCurrentProcess(), -1);
+       }
+
+       return (HANDLE) _beginthreadex(NULL, 0, lamexp_debug_thread_proc, NULL, 0, NULL);
 }
 
 /*
  * Check for compatibility mode
  */
-static bool lamexp_check_compatibility_mode(const char *exportName)
+static bool lamexp_check_compatibility_mode(const char *exportName, const char *executableName)
 {
        QLibrary kernel32("kernel32.dll");
 
@@ -414,7 +791,8 @@ static bool lamexp_check_compatibility_mode(const char *exportName)
        {
                if(kernel32.resolve(exportName) != NULL)
                {
-                       qFatal("Windows compatibility mode detected. Program will exit!");
+                       qWarning("Function '%s' exported from 'kernel32.dll' -> Windows compatibility mode!", exportName);
+                       qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
                        return false;
                }
        }
@@ -423,6 +801,161 @@ static bool lamexp_check_compatibility_mode(const char *exportName)
 }
 
 /*
+ * Computus according to H. Lichtenberg
+ */
+static bool lamexp_computus(const QDate &date)
+{
+       int X = date.year();
+       int A = X % 19;
+       int K = X / 100;
+       int M = 15 + (3*K + 3) / 4 - (8*K + 13) / 25;
+       int D = (19*A + M) % 30;
+       int S = 2 - (3*K + 3) / 4;
+       int R = D / 29 + (D / 28 - D / 29) * (A / 11);
+       int OG = 21 + D - R;
+       int SZ = 7 - (X + X / 4 + S) % 7;
+       int OE = 7 - (OG - SZ) % 7;
+       int OS = (OG + OE);
+
+       if(OS > 31)
+       {
+               return (date.month() == 4) && (date.day() == (OS - 31));
+       }
+       else
+       {
+               return (date.month() == 3) && (date.day() == OS);
+       }
+}
+
+/*
+ * Check for Thanksgiving
+ */
+static bool lamexp_thanksgiving(const QDate &date)
+{
+       int day = 0;
+
+       switch(QDate(date.year(), 11, 1).dayOfWeek())
+       {
+               case 1: day = 25; break; 
+               case 2: day = 24; break; 
+               case 3: day = 23; break; 
+               case 4: day = 22; break; 
+               case 5: day = 28; break; 
+               case 6: day = 27; break; 
+               case 7: day = 26; break;
+       }
+
+       return (date.month() == 11) && (date.day() == day);
+}
+
+/*
+ * Initialize app icon
+ */
+QIcon lamexp_app_icon(const QDate *date, const QTime *time)
+{
+       QDate currentDate = (date) ? QDate(*date) : QDate::currentDate();
+       QTime currentTime = (time) ? QTime(*time) : QTime::currentTime();
+       
+       if(lamexp_thanksgiving(currentDate))
+       {
+               return QIcon(":/MainIcon6.png");
+       }
+       else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1)  && (currentTime.hour() <= 19)))
+       {
+               return QIcon(":/MainIcon5.png");
+       }
+       else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1)  && (currentTime.hour() <= 11)))
+       {
+               return QIcon(":/MainIcon4.png");
+       }
+       else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
+       {
+               return QIcon(":/MainIcon3.png");
+       }
+       else if(lamexp_computus(currentDate))
+       {
+               return QIcon(":/MainIcon2.png");
+       }
+       else
+       {
+               return QIcon(":/MainIcon1.png");
+       }
+}
+
+/*
+ * Broadcast event to all windows
+ */
+static bool lamexp_broadcast(int eventType, bool onlyToVisible)
+{
+       if(QApplication *app = dynamic_cast<QApplication*>(QApplication::instance()))
+       {
+               qDebug("Broadcasting %d", eventType);
+               
+               bool allOk = true;
+               QEvent poEvent(static_cast<QEvent::Type>(eventType));
+               QWidgetList list = app->topLevelWidgets();
+
+               while(!list.isEmpty())
+               {
+                       QWidget *widget = list.takeFirst();
+                       if(!onlyToVisible || widget->isVisible())
+                       {
+                               if(!app->sendEvent(widget, &poEvent))
+                               {
+                                       allOk = false;
+                               }
+                       }
+               }
+
+               qDebug("Broadcast %d done (%s)", eventType, (allOk ? "OK" : "Stopped"));
+               return allOk;
+       }
+       else
+       {
+               qWarning("Broadcast failed, could not get QApplication instance!");
+               return false;
+       }
+}
+
+/*
+ * Qt event filter
+ */
+static bool lamexp_event_filter(void *message, long *result)
+{
+       if((!(LAMEXP_DEBUG)) && lamexp_check_for_debugger())
+       {
+               FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
+               TerminateProcess(GetCurrentProcess(), -1);
+       }
+       
+       switch(reinterpret_cast<MSG*>(message)->message)
+       {
+       case WM_QUERYENDSESSION:
+               qWarning("WM_QUERYENDSESSION message received!");
+               *result = lamexp_broadcast(lamexp_event_queryendsession, false) ? TRUE : FALSE;
+               return true;
+       case WM_ENDSESSION:
+               qWarning("WM_ENDSESSION message received!");
+               if(reinterpret_cast<MSG*>(message)->wParam == TRUE)
+               {
+                       lamexp_broadcast(lamexp_event_endsession, false);
+                       if(QApplication *app = reinterpret_cast<QApplication*>(QApplication::instance()))
+                       {
+                               app->closeAllWindows();
+                               app->quit();
+                       }
+                       lamexp_finalization();
+                       exit(1);
+               }
+               *result = 0;
+               return true;
+       default:
+               /*ignore this message and let Qt handle it*/
+               return false;
+       }
+}
+
+/*
  * Check for process elevation
  */
 static bool lamexp_check_elevation(void)
@@ -472,6 +1005,7 @@ static bool lamexp_check_elevation(void)
 bool lamexp_init_qt(int argc, char* argv[])
 {
        static bool qt_initialized = false;
+       typedef BOOL (WINAPI *SetDllDirectoryProc)(WCHAR *lpPathName);
 
        //Don't initialized again, if done already
        if(qt_initialized)
@@ -479,81 +1013,134 @@ bool lamexp_init_qt(int argc, char* argv[])
                return true;
        }
        
+       //Secure DLL loading
+       QLibrary kernel32("kernel32.dll");
+       if(kernel32.load())
+       {
+               SetDllDirectoryProc pSetDllDirectory = (SetDllDirectoryProc) kernel32.resolve("SetDllDirectoryW");
+               if(pSetDllDirectory != NULL) pSetDllDirectory(L"");
+               kernel32.unload();
+       }
+
+       //Extract executable name from argv[] array
+       char *executableName = argv[0];
+       while(char *temp = strpbrk(executableName, "\\/:?"))
+       {
+               executableName = temp + 1;
+       }
+
        //Check Qt version
-       qDebug("Using Qt Framework v%s, compiled with Qt v%s", qVersion(), QT_VERSION_STR);
-       QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR);
-       
+       qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
+       qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR, QT_PACKAGEDATE_STR, QT_BUILD_KEY);
+       if(_stricmp(qVersion(), QT_VERSION_STR))
+       {
+               qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(QString::fromLatin1(executableName), QString::fromLatin1(QT_VERSION_STR), QString::fromLatin1(qVersion())).toLatin1().constData());
+               return false;
+       }
+       if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY), Qt::CaseInsensitive))
+       {
+               qFatal("%s", QApplication::tr("Executable '%1' was built for Qt '%2', but found Qt '%3'.").arg(QString::fromLatin1(executableName), QString::fromLatin1(QT_BUILD_KEY), QLibraryInfo::buildKey()).toLatin1().constData());
+               return false;
+       }
+
        //Check the Windows version
        switch(QSysInfo::windowsVersion() & QSysInfo::WV_NT_based)
        {
+       case 0:
+       case QSysInfo::WV_NT:
+               qFatal("%s", QApplication::tr("Executable '%1' requires Windows 2000 or later.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
+               break;
        case QSysInfo::WV_2000:
-               qDebug("Running on Windows 2000 (not offically supported!).\n");
-               lamexp_check_compatibility_mode("GetNativeSystemInfo");
+               qDebug("Running on Windows 2000 (not officially supported!).\n");
+               lamexp_check_compatibility_mode("GetNativeSystemInfo", executableName);
                break;
        case QSysInfo::WV_XP:
                qDebug("Running on Windows XP.\n");
-               lamexp_check_compatibility_mode("GetLargePageMinimum");
+               lamexp_check_compatibility_mode("GetLargePageMinimum", executableName);
                break;
        case QSysInfo::WV_2003:
                qDebug("Running on Windows Server 2003 or Windows XP x64-Edition.\n");
-               lamexp_check_compatibility_mode("GetLocaleInfoEx");
+               lamexp_check_compatibility_mode("GetLocaleInfoEx", executableName);
                break;
        case QSysInfo::WV_VISTA:
                qDebug("Running on Windows Vista or Windows Server 2008.\n");
-               lamexp_check_compatibility_mode("CreateRemoteThreadEx");
+               lamexp_check_compatibility_mode("CreateRemoteThreadEx", executableName);
                break;
        case QSysInfo::WV_WINDOWS7:
                qDebug("Running on Windows 7 or Windows Server 2008 R2.\n");
-               lamexp_check_compatibility_mode(NULL);
+               lamexp_check_compatibility_mode(NULL, executableName);
                break;
        default:
-               qFatal("Unsupported OS, only Windows 2000 or later is supported!");
+               {
+                       DWORD osVersionNo = lamexp_get_os_version();
+                       qWarning("Running on an unknown/untested WinNT-based OS (v%u.%u).\n", HIWORD(osVersionNo), LOWORD(osVersionNo));
+               }
                break;
        }
 
+       //Check for Wine
+       if(lamexp_detect_wine())
+       {
+               qWarning("It appears we are running under Wine, unexpected things might happen!\n");
+       }
+
+
        //Create Qt application instance and setup version info
        QApplication *application = new QApplication(argc, argv);
        application->setApplicationName("LameXP - Audio Encoder Front-End");
        application->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build())); 
        application->setOrganizationName("LoRd_MuldeR");
-       application->setOrganizationDomain("mulder.dummwiedeutsch.de");
-       application->setWindowIcon(QIcon(":/MainIcon.png"));
-       
+       application->setOrganizationDomain("mulder.at.gg");
+       application->setWindowIcon(lamexp_app_icon());
+       application->setEventFilter(lamexp_event_filter);
+
+       //Set text Codec for locale
+       QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
+
        //Load plugins from application directory
        QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
        qDebug("Library Path:\n%s\n", QApplication::libraryPaths().first().toUtf8().constData());
 
-       //Init static Qt plugins
-       LAMEXP_INIT_QT_STATIC_PLUGIN(qico);
-       LAMEXP_INIT_QT_STATIC_PLUGIN(qsvg);
-
        //Check for supported image formats
        QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
        for(int i = 0; g_lamexp_imageformats[i]; i++)
        {
                if(!supportedFormats.contains(g_lamexp_imageformats[i]))
                {
-                       qFatal("Qt initialization error: At least one image format plugin is missing! (%s)", g_lamexp_imageformats[i]);
+                       qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_lamexp_imageformats[i]);
                        return false;
                }
        }
 
        //Add default translations
-       g_lamexp_translation_files.insert(LAMEXP_DEFAULT_LANGID, "");
-       g_lamexp_translation_names.insert(LAMEXP_DEFAULT_LANGID, "English");
-
-       //Init language files
-       //lamexp_init_translations();
+       g_lamexp_translation.files.insert(LAMEXP_DEFAULT_LANGID, "");
+       g_lamexp_translation.names.insert(LAMEXP_DEFAULT_LANGID, "English");
 
        //Check for process elevation
-       if(!lamexp_check_elevation())
+       if((!lamexp_check_elevation()) && (!lamexp_detect_wine()))
        {
-               if(QMessageBox::warning(NULL, "LameXP", "<nobr>LameXP was started with elevated rights. This is a potential security risk!</nobr>", "Quit Program (Recommended)", "Ignore") == 0)
+               QMessageBox messageBox(QMessageBox::Warning, "LameXP", "<nobr>LameXP was started with 'elevated' rights, altough LameXP does not need these rights.<br>Running an applications with unnecessary rights is a potential security risk!</nobr>", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
+               messageBox.addButton("Quit Program (Recommended)", QMessageBox::NoRole);
+               messageBox.addButton("Ignore", QMessageBox::NoRole);
+               if(messageBox.exec() == 0)
                {
                        return false;
                }
        }
 
+       //Update console icon, if a console is attached
+       if(g_lamexp_console_attached && (!lamexp_detect_wine()))
+       {
+               typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
+               QLibrary kernel32("kernel32.dll");
+               if(kernel32.load())
+               {
+                       SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon");
+                       if(SetConsoleIconPtr != NULL) SetConsoleIconPtr(QIcon(":/icons/sound.png").pixmap(16, 16).toWinHICON());
+                       kernel32.unload();
+               }
+       }
+
        //Done
        qt_initialized = true;
        return true;
@@ -564,59 +1151,89 @@ bool lamexp_init_qt(int argc, char* argv[])
  */
 int lamexp_init_ipc(void)
 {
-       if(g_lamexp_sharedmem_ptr && g_lamexp_semaphore_read_ptr && g_lamexp_semaphore_write_ptr)
+       if(g_lamexp_ipc_ptr.sharedmem && g_lamexp_ipc_ptr.semaphore_read && g_lamexp_ipc_ptr.semaphore_write && g_lamexp_ipc_ptr.semaphore_read_mutex && g_lamexp_ipc_ptr.semaphore_write_mutex)
        {
                return 0;
        }
 
-       g_lamexp_semaphore_read_ptr = new QSystemSemaphore(QString(g_lamexp_semaphore_read_uuid), 0);
-       g_lamexp_semaphore_write_ptr = new QSystemSemaphore(QString(g_lamexp_semaphore_write_uuid), 0);
+       g_lamexp_ipc_ptr.semaphore_read = new QSystemSemaphore(QString(g_lamexp_ipc_uuid.semaphore_read), 0);
+       g_lamexp_ipc_ptr.semaphore_write = new QSystemSemaphore(QString(g_lamexp_ipc_uuid.semaphore_write), 0);
+       g_lamexp_ipc_ptr.semaphore_read_mutex = new QSystemSemaphore(QString(g_lamexp_ipc_uuid.semaphore_read_mutex), 0);
+       g_lamexp_ipc_ptr.semaphore_write_mutex = new QSystemSemaphore(QString(g_lamexp_ipc_uuid.semaphore_write_mutex), 0);
 
-       if(g_lamexp_semaphore_read_ptr->error() != QSystemSemaphore::NoError)
+       if(g_lamexp_ipc_ptr.semaphore_read->error() != QSystemSemaphore::NoError)
+       {
+               QString errorMessage = g_lamexp_ipc_ptr.semaphore_read->errorString();
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex);
+               qFatal("Failed to create system smaphore: %s", errorMessage.toUtf8().constData());
+               return -1;
+       }
+       if(g_lamexp_ipc_ptr.semaphore_write->error() != QSystemSemaphore::NoError)
        {
-               QString errorMessage = g_lamexp_semaphore_read_ptr->errorString();
-               LAMEXP_DELETE(g_lamexp_semaphore_read_ptr);
-               LAMEXP_DELETE(g_lamexp_semaphore_write_ptr);
+               QString errorMessage = g_lamexp_ipc_ptr.semaphore_write->errorString();
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex);
                qFatal("Failed to create system smaphore: %s", errorMessage.toUtf8().constData());
                return -1;
        }
-       if(g_lamexp_semaphore_write_ptr->error() != QSystemSemaphore::NoError)
+       if(g_lamexp_ipc_ptr.semaphore_read_mutex->error() != QSystemSemaphore::NoError)
        {
-               QString errorMessage = g_lamexp_semaphore_write_ptr->errorString();
-               LAMEXP_DELETE(g_lamexp_semaphore_read_ptr);
-               LAMEXP_DELETE(g_lamexp_semaphore_write_ptr);
+               QString errorMessage = g_lamexp_ipc_ptr.semaphore_read_mutex->errorString();
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex);
+               qFatal("Failed to create system smaphore: %s", errorMessage.toUtf8().constData());
+               return -1;
+       }
+       if(g_lamexp_ipc_ptr.semaphore_write_mutex->error() != QSystemSemaphore::NoError)
+       {
+               QString errorMessage = g_lamexp_ipc_ptr.semaphore_write_mutex->errorString();
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex);
+               LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex);
                qFatal("Failed to create system smaphore: %s", errorMessage.toUtf8().constData());
                return -1;
        }
 
-       g_lamexp_sharedmem_ptr = new QSharedMemory(QString(g_lamexp_sharedmem_uuid), NULL);
+       g_lamexp_ipc_ptr.sharedmem = new QSharedMemory(QString(g_lamexp_ipc_uuid.sharedmem), NULL);
        
-       if(!g_lamexp_sharedmem_ptr->create(sizeof(lamexp_ipc_t)))
+       if(!g_lamexp_ipc_ptr.sharedmem->create(sizeof(lamexp_ipc_t)))
        {
-               if(g_lamexp_sharedmem_ptr->error() == QSharedMemory::AlreadyExists)
+               if(g_lamexp_ipc_ptr.sharedmem->error() == QSharedMemory::AlreadyExists)
                {
-                       g_lamexp_sharedmem_ptr->attach();
-                       if(g_lamexp_sharedmem_ptr->error() == QSharedMemory::NoError)
+                       g_lamexp_ipc_ptr.sharedmem->attach();
+                       if(g_lamexp_ipc_ptr.sharedmem->error() == QSharedMemory::NoError)
                        {
                                return 1;
                        }
                        else
                        {
-                               QString errorMessage = g_lamexp_sharedmem_ptr->errorString();
+                               QString errorMessage = g_lamexp_ipc_ptr.sharedmem->errorString();
+                               LAMEXP_DELETE(g_lamexp_ipc_ptr.sharedmem);
                                qFatal("Failed to attach to shared memory: %s", errorMessage.toUtf8().constData());
                                return -1;
                        }
                }
                else
                {
-                       QString errorMessage = g_lamexp_sharedmem_ptr->errorString();
+                       QString errorMessage = g_lamexp_ipc_ptr.sharedmem->errorString();
+                       LAMEXP_DELETE(g_lamexp_ipc_ptr.sharedmem);
                        qFatal("Failed to create shared memory: %s", errorMessage.toUtf8().constData());
                        return -1;
                }
        }
 
-       memset(g_lamexp_sharedmem_ptr->data(), 0, sizeof(lamexp_ipc_t));
-       g_lamexp_semaphore_write_ptr->release();
+       memset(g_lamexp_ipc_ptr.sharedmem->data(), 0, sizeof(lamexp_ipc_t));
+       g_lamexp_ipc_ptr.semaphore_write->release(g_lamexp_ipc_slots);
+       g_lamexp_ipc_ptr.semaphore_read_mutex->release();
+       g_lamexp_ipc_ptr.semaphore_write_mutex->release();
 
        return 0;
 }
@@ -626,26 +1243,31 @@ int lamexp_init_ipc(void)
  */
 void lamexp_ipc_send(unsigned int command, const char* message)
 {
-       if(!g_lamexp_sharedmem_ptr || !g_lamexp_semaphore_read_ptr || !g_lamexp_semaphore_write_ptr)
+       if(!g_lamexp_ipc_ptr.sharedmem || !g_lamexp_ipc_ptr.semaphore_read || !g_lamexp_ipc_ptr.semaphore_write || !g_lamexp_ipc_ptr.semaphore_read_mutex || !g_lamexp_ipc_ptr.semaphore_write_mutex)
        {
                throw "Shared memory for IPC not initialized yet.";
        }
 
-       lamexp_ipc_t *lamexp_ipc = new lamexp_ipc_t;
-       memset(lamexp_ipc, 0, sizeof(lamexp_ipc_t));
-       lamexp_ipc->command = command;
+       lamexp_ipc_data_t ipc_data;
+       memset(&ipc_data, 0, sizeof(lamexp_ipc_data_t));
+       ipc_data.command = command;
+       
        if(message)
        {
-               strcpy_s(lamexp_ipc->parameter, 4096, message);
+               strncpy_s(ipc_data.parameter, 4096, message, _TRUNCATE);
        }
 
-       if(g_lamexp_semaphore_write_ptr->acquire())
+       if(g_lamexp_ipc_ptr.semaphore_write->acquire())
        {
-               memcpy(g_lamexp_sharedmem_ptr->data(), lamexp_ipc, sizeof(lamexp_ipc_t));
-               g_lamexp_semaphore_read_ptr->release();
+               if(g_lamexp_ipc_ptr.semaphore_write_mutex->acquire())
+               {
+                       lamexp_ipc_t *ptr = reinterpret_cast<lamexp_ipc_t*>(g_lamexp_ipc_ptr.sharedmem->data());
+                       memcpy(&ptr->data[ptr->pos_write], &ipc_data, sizeof(lamexp_ipc_data_t));
+                       ptr->pos_write = (ptr->pos_write + 1) % g_lamexp_ipc_slots;
+                       g_lamexp_ipc_ptr.semaphore_read->release();
+                       g_lamexp_ipc_ptr.semaphore_write_mutex->release();
+               }
        }
-
-       LAMEXP_DELETE(lamexp_ipc);
 }
 
 /*
@@ -656,31 +1278,46 @@ void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize)
        *command = 0;
        message[0] = '\0';
        
-       if(!g_lamexp_sharedmem_ptr || !g_lamexp_semaphore_read_ptr || !g_lamexp_semaphore_write_ptr)
+       if(!g_lamexp_ipc_ptr.sharedmem || !g_lamexp_ipc_ptr.semaphore_read || !g_lamexp_ipc_ptr.semaphore_write || !g_lamexp_ipc_ptr.semaphore_read_mutex || !g_lamexp_ipc_ptr.semaphore_write_mutex)
        {
                throw "Shared memory for IPC not initialized yet.";
        }
 
-       lamexp_ipc_t *lamexp_ipc = new lamexp_ipc_t;
-       memset(lamexp_ipc, 0, sizeof(lamexp_ipc_t));
+       lamexp_ipc_data_t ipc_data;
+       memset(&ipc_data, 0, sizeof(lamexp_ipc_data_t));
 
-       if(g_lamexp_semaphore_read_ptr->acquire())
+       if(g_lamexp_ipc_ptr.semaphore_read->acquire())
        {
-               memcpy(lamexp_ipc, g_lamexp_sharedmem_ptr->data(), sizeof(lamexp_ipc_t));
-               g_lamexp_semaphore_write_ptr->release();
-
-               if(!(lamexp_ipc->reserved_1 || lamexp_ipc->reserved_2))
-               {
-                       *command = lamexp_ipc->command;
-                       strcpy_s(message, buffSize, lamexp_ipc->parameter);
-               }
-               else
+               if(g_lamexp_ipc_ptr.semaphore_read_mutex->acquire())
                {
-                       qWarning("Malformed IPC message, will be ignored");
+                       lamexp_ipc_t *ptr = reinterpret_cast<lamexp_ipc_t*>(g_lamexp_ipc_ptr.sharedmem->data());
+                       memcpy(&ipc_data, &ptr->data[ptr->pos_read], sizeof(lamexp_ipc_data_t));
+                       ptr->pos_read = (ptr->pos_read + 1) % g_lamexp_ipc_slots;
+                       g_lamexp_ipc_ptr.semaphore_write->release();
+                       g_lamexp_ipc_ptr.semaphore_read_mutex->release();
+
+                       if(!(ipc_data.reserved_1 || ipc_data.reserved_2))
+                       {
+                               *command = ipc_data.command;
+                               strncpy_s(message, buffSize, ipc_data.parameter, _TRUNCATE);
+                       }
+                       else
+                       {
+                               qWarning("Malformed IPC message, will be ignored");
+                       }
                }
        }
+}
 
-       LAMEXP_DELETE(lamexp_ipc);
+/*
+ * Check for LameXP "portable" mode
+ */
+bool lamexp_portable_mode(void)
+{
+       QString baseName = QFileInfo(QApplication::applicationFilePath()).completeBaseName();
+       int idx1 = baseName.indexOf("lamexp", 0, Qt::CaseInsensitive);
+       int idx2 = baseName.lastIndexOf("portable", -1, Qt::CaseInsensitive);
+       return (idx1 >= 0) && (idx2 >= 0) && (idx1 < idx2);
 }
 
 /*
@@ -702,69 +1339,94 @@ QString lamexp_rand_str(void)
 /*
  * Get LameXP temp folder
  */
-const QString &lamexp_temp_folder(void)
+const QString &lamexp_temp_folder2(void)
 {
        static const char *TEMP_STR = "Temp";
+       const QString WRITE_TEST_DATA = lamexp_rand_str();
+       const QString SUB_FOLDER = lamexp_rand_str();
 
-       if(g_lamexp_temp_folder.isEmpty())
+       //Already initialized?
+       if(!g_lamexp_temp_folder.isEmpty())
        {
-               QDir temp = QDir::temp();
-               QDir localAppData = QDir(lamexp_known_folder(lamexp_folder_localappdata));
-
-               if(!localAppData.path().isEmpty() && localAppData.exists())
+               if(QDir(g_lamexp_temp_folder).exists())
                {
-                       if(!localAppData.entryList(QDir::AllDirs).contains(TEMP_STR, Qt::CaseInsensitive))
-                       {
-                               localAppData.mkdir(TEMP_STR);
-                       }
-                       if(localAppData.cd(TEMP_STR))
-                       {
-                               temp.setPath(localAppData.absolutePath());
-                       }
+                       return g_lamexp_temp_folder;
                }
-
-               if(!temp.exists())
+               else
+               {
+                       g_lamexp_temp_folder.clear();
+               }
+       }
+       
+       //Try the %TMP% or %TEMP% directory first
+       QDir temp = QDir::temp();
+       if(temp.exists())
+       {
+               temp.mkdir(SUB_FOLDER);
+               if(temp.cd(SUB_FOLDER) && temp.exists())
                {
-                       temp.mkpath(".");
-                       if(!temp.exists())
+                       QFile testFile(QString("%1/~%2.tmp").arg(temp.canonicalPath(), lamexp_rand_str()));
+                       if(testFile.open(QIODevice::ReadWrite))
                        {
-                               qFatal("The system's temporary directory does not exist:\n%s", temp.absolutePath().toUtf8().constData());
-                               return g_lamexp_temp_folder;
+                               if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData()))
+                               {
+                                       g_lamexp_temp_folder = temp.canonicalPath();
+                               }
+                               testFile.remove();
                        }
                }
-
-               QString subDir = QString("%1.tmp").arg(lamexp_rand_str());
-               if(!temp.mkdir(subDir))
+               if(!g_lamexp_temp_folder.isEmpty())
                {
-                       qFatal("Temporary directory could not be created:\n%s", QString("%1/%2").arg(temp.canonicalPath(), subDir).toUtf8().constData());
                        return g_lamexp_temp_folder;
                }
-               if(!temp.cd(subDir))
+       }
+
+       //Create TEMP folder in %LOCALAPPDATA%
+       QDir localAppData = QDir(lamexp_known_folder(lamexp_folder_localappdata));
+       if(!localAppData.path().isEmpty())
+       {
+               if(!localAppData.exists())
                {
-                       qFatal("Temporary directory could not be entered:\n%s", QString("%1/%2").arg(temp.canonicalPath(), subDir).toUtf8().constData());
-                       return g_lamexp_temp_folder;
+                       localAppData.mkpath(".");
                }
-               
-               QFile testFile(QString("%1/.%2").arg(temp.canonicalPath(), lamexp_rand_str()));
-               if(!testFile.open(QIODevice::ReadWrite) || testFile.write("LAMEXP_TEST\n") < 12)
+               if(localAppData.exists())
+               {
+                       if(!localAppData.entryList(QDir::AllDirs).contains(TEMP_STR, Qt::CaseInsensitive))
+                       {
+                               localAppData.mkdir(TEMP_STR);
+                       }
+                       if(localAppData.cd(TEMP_STR) && localAppData.exists())
+                       {
+                               localAppData.mkdir(SUB_FOLDER);
+                               if(localAppData.cd(SUB_FOLDER) && localAppData.exists())
+                               {
+                                       QFile testFile(QString("%1/~%2.tmp").arg(localAppData.canonicalPath(), lamexp_rand_str()));
+                                       if(testFile.open(QIODevice::ReadWrite))
+                                       {
+                                               if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData()))
+                                               {
+                                                       g_lamexp_temp_folder = localAppData.canonicalPath();
+                                               }
+                                               testFile.remove();
+                                       }
+                               }
+                       }
+               }
+               if(!g_lamexp_temp_folder.isEmpty())
                {
-                       qFatal("Write access to temporary directory has been denied:\n%s", temp.canonicalPath().toUtf8().constData());
                        return g_lamexp_temp_folder;
                }
-
-               testFile.close();
-               QFile::remove(testFile.fileName());
-               
-               g_lamexp_temp_folder = temp.canonicalPath();
        }
-       
+
+       //Failed to create TEMP folder!
+       qFatal("Temporary directory could not be initialized!\n\nFirst attempt:\n%s\n\nSecond attempt:\n%s", temp.canonicalPath().toUtf8().constData(), localAppData.canonicalPath().toUtf8().constData());
        return g_lamexp_temp_folder;
 }
 
 /*
  * Clean folder
  */
-bool lamexp_clean_folder(const QString folderPath)
+bool lamexp_clean_folder(const QString &folderPath)
 {
        QDir tempFolder(folderPath);
        QFileInfoList entryList = tempFolder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
@@ -877,16 +1539,17 @@ const QString lamexp_version2string(const QString &pattern, unsigned int version
 /*
  * Register a new translation
  */
-bool lamexp_translation_register(const QString &langId, const QString &qmFile, const QString &langName, unsigned int &systemId)
+bool lamexp_translation_register(const QString &langId, const QString &qmFile, const QString &langName, unsigned int &systemId, unsigned int &country)
 {
        if(qmFile.isEmpty() || langName.isEmpty() || systemId < 1)
        {
                return false;
        }
 
-       g_lamexp_translation_files.insert(langId, qmFile);
-       g_lamexp_translation_names.insert(langId, langName);
-       g_lamexp_translation_sysid.insert(langId, systemId);
+       g_lamexp_translation.files.insert(langId, qmFile);
+       g_lamexp_translation.names.insert(langId, langName);
+       g_lamexp_translation.sysid.insert(langId, systemId);
+       g_lamexp_translation.cntry.insert(langId, country);
 
        return true;
 }
@@ -896,7 +1559,7 @@ bool lamexp_translation_register(const QString &langId, const QString &qmFile, c
  */
 QStringList lamexp_query_translations(void)
 {
-       return g_lamexp_translation_files.keys();
+       return g_lamexp_translation.files.keys();
 }
 
 /*
@@ -904,7 +1567,7 @@ QStringList lamexp_query_translations(void)
  */
 QString lamexp_translation_name(const QString &langId)
 {
-       return g_lamexp_translation_names.value(langId.toLower(), QString());
+       return g_lamexp_translation.names.value(langId.toLower(), QString());
 }
 
 /*
@@ -912,7 +1575,15 @@ QString lamexp_translation_name(const QString &langId)
  */
 unsigned int lamexp_translation_sysid(const QString &langId)
 {
-       return g_lamexp_translation_sysid.value(langId.toLower(), 0);
+       return g_lamexp_translation.sysid.value(langId.toLower(), 0);
+}
+
+/*
+ * Get translation script id
+ */
+unsigned int lamexp_translation_country(const QString &langId)
+{
+       return g_lamexp_translation.cntry.value(langId.toLower(), 0);
 }
 
 /*
@@ -928,7 +1599,7 @@ bool lamexp_install_translator(const QString &langId)
        }
        else
        {
-               QString qmFile = g_lamexp_translation_files.value(langId.toLower(), QString());
+               QString qmFile = g_lamexp_translation.files.value(langId.toLower(), QString());
                if(!qmFile.isEmpty())
                {
                        success = lamexp_install_translator_from_file(QString(":/localization/%1").arg(qmFile));
@@ -990,15 +1661,17 @@ QString lamexp_known_folder(lamexp_known_folder_t folder_id)
        static const GUID GUID_PROGRAM_FILES = {0x905e63b6,0xc1bf,0x494e,{0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a}};
        static const GUID GUID_SYSTEM_FOLDER = {0x1AC14E77,0x02E7,0x4E5D,{0xB7,0x44,0x2E,0xB1,0xAE,0x51,0x98,0xB7}};
 
-       static QLibrary *Kernel32Lib = NULL;
        static SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL;
        static SHGetFolderPathFun SHGetFolderPathPtr = NULL;
 
        if((!SHGetKnownFolderPathPtr) && (!SHGetFolderPathPtr))
        {
-               if(!Kernel32Lib) Kernel32Lib = new QLibrary("shell32.dll");
-               SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) Kernel32Lib->resolve("SHGetKnownFolderPath");
-               SHGetFolderPathPtr = (SHGetFolderPathFun) Kernel32Lib->resolve("SHGetFolderPathW");
+               QLibrary kernel32Lib("shell32.dll");
+               if(kernel32Lib.load())
+               {
+                       SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) kernel32Lib.resolve("SHGetKnownFolderPath");
+                       SHGetFolderPathPtr = (SHGetFolderPathFun) kernel32Lib.resolve("SHGetFolderPathW");
+               }
        }
 
        int folderCSIDL = -1;
@@ -1078,8 +1751,8 @@ bool lamexp_remove_file(const QString &filename)
        {
                if(!QFile::remove(filename))
                {
-                       DWORD attributes = GetFileAttributesW(reinterpret_cast<const wchar_t*>(filename.utf16()));
-                       SetFileAttributesW(reinterpret_cast<const wchar_t*>(filename.utf16()), (attributes & (~FILE_ATTRIBUTE_READONLY)));
+                       DWORD attributes = GetFileAttributesW(QWCHAR(filename));
+                       SetFileAttributesW(QWCHAR(filename), (attributes & (~FILE_ATTRIBUTE_READONLY)));
                        if(!QFile::remove(filename))
                        {
                                qWarning("Could not delete \"%s\"", filename.toLatin1().constData());
@@ -1104,17 +1777,26 @@ bool lamexp_themes_enabled(void)
 {
        typedef int (WINAPI *IsAppThemedFun)(void);
        
-       bool isAppThemed = false;
-       QLibrary uxTheme(QString("%1/UxTheme.dll").arg(lamexp_known_folder(lamexp_folder_systemfolder)));
-       IsAppThemedFun IsAppThemedPtr = (IsAppThemedFun) uxTheme.resolve("IsAppThemed");
+       static bool isAppThemed = false;
+       static bool isAppThemed_initialized = false;
 
-       if(IsAppThemedPtr)
+       if(!isAppThemed_initialized)
        {
-               isAppThemed = IsAppThemedPtr();
-               if(!isAppThemed)
+               IsAppThemedFun IsAppThemedPtr = NULL;
+               QLibrary uxTheme(QString("%1/UxTheme.dll").arg(lamexp_known_folder(lamexp_folder_systemfolder)));
+               if(uxTheme.load())
                {
-                       qWarning("Theme support is disabled for this process!");
+                       IsAppThemedPtr = (IsAppThemedFun) uxTheme.resolve("IsAppThemed");
                }
+               if(IsAppThemedPtr)
+               {
+                       isAppThemed = IsAppThemedPtr();
+                       if(!isAppThemed)
+                       {
+                               qWarning("Theme support is disabled for this process!");
+                       }
+               }
+               isAppThemed_initialized = true;
        }
 
        return isAppThemed;
@@ -1123,24 +1805,195 @@ bool lamexp_themes_enabled(void)
 /*
  * Get number of free bytes on disk
  */
-__int64 lamexp_free_diskspace(const QString &path)
+unsigned __int64 lamexp_free_diskspace(const QString &path, bool *ok)
 {
        ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;
        if(GetDiskFreeSpaceExW(reinterpret_cast<const wchar_t*>(QDir::toNativeSeparators(path).utf16()), &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes))
        {
+               if(ok) *ok = true;
                return freeBytesAvailable.QuadPart;
        }
        else
        {
+               if(ok) *ok = false;
                return 0;
        }
 }
 
 /*
+ * Check if computer does support hibernation
+ */
+bool lamexp_is_hibernation_supported(void)
+{
+       bool hibernationSupported = false;
+
+       SYSTEM_POWER_CAPABILITIES pwrCaps;
+       SecureZeroMemory(&pwrCaps, sizeof(SYSTEM_POWER_CAPABILITIES));
+       
+       if(GetPwrCapabilities(&pwrCaps))
+       {
+               hibernationSupported = pwrCaps.SystemS4 && pwrCaps.HiberFilePresent;
+       }
+
+       return hibernationSupported;
+}
+
+/*
+ * Shutdown the computer
+ */
+bool lamexp_shutdown_computer(const QString &message, const unsigned long timeout, const bool forceShutdown, const bool hibernate)
+{
+       HANDLE hToken = NULL;
+
+       if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
+       {
+               TOKEN_PRIVILEGES privileges;
+               memset(&privileges, 0, sizeof(TOKEN_PRIVILEGES));
+               privileges.PrivilegeCount = 1;
+               privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+               
+               if(LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &privileges.Privileges[0].Luid))
+               {
+                       if(AdjustTokenPrivileges(hToken, FALSE, &privileges, NULL, NULL, NULL))
+                       {
+                               if(hibernate)
+                               {
+                                       if(SetSuspendState(TRUE, TRUE, TRUE))
+                                       {
+                                               return true;
+                                       }
+                               }
+                               const DWORD reason = SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_FLAG_PLANNED;
+                               return InitiateSystemShutdownEx(NULL, const_cast<wchar_t*>(QWCHAR(message)), timeout, forceShutdown ? TRUE : FALSE, FALSE, reason);
+                       }
+               }
+       }
+       
+       return false;
+}
+
+/*
+ * Make a window blink (to draw user's attention)
+ */
+void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
+{
+       static QMutex blinkMutex;
+
+       const double maxOpac = 1.0;
+       const double minOpac = 0.3;
+       const double delOpac = 0.1;
+
+       if(!blinkMutex.tryLock())
+       {
+               qWarning("Blinking is already in progress, skipping!");
+               return;
+       }
+       
+       try
+       {
+               const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
+               const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
+               const double opacity = poWindow->windowOpacity();
+       
+               for(unsigned int i = 0; i < count; i++)
+               {
+                       for(double x = maxOpac; x >= minOpac; x -= delOpac)
+                       {
+                               poWindow->setWindowOpacity(x);
+                               QApplication::processEvents();
+                               Sleep(sleep);
+                       }
+
+                       for(double x = minOpac; x <= maxOpac; x += delOpac)
+                       {
+                               poWindow->setWindowOpacity(x);
+                               QApplication::processEvents();
+                               Sleep(sleep);
+                       }
+               }
+
+               poWindow->setWindowOpacity(opacity);
+               QApplication::processEvents();
+               blinkMutex.unlock();
+       }
+       catch (...)
+       {
+               blinkMutex.unlock();
+               qWarning("Exception error while blinking!");
+       }
+}
+
+/*
+ * Remove forbidden characters from a filename
+ */
+const QString lamexp_clean_filename(const QString &str)
+{
+       QString newStr(str);
+       
+       newStr.replace("\\", "-");
+       newStr.replace(" / ", ", ");
+       newStr.replace("/", ",");
+       newStr.replace(":", "-");
+       newStr.replace("*", "x");
+       newStr.replace("?", "");
+       newStr.replace("<", "[");
+       newStr.replace(">", "]");
+       newStr.replace("|", "!");
+       
+       return newStr.simplified();
+}
+
+/*
+ * Remove forbidden characters from a file path
+ */
+const QString lamexp_clean_filepath(const QString &str)
+{
+       QStringList parts = QString(str).replace("\\", "/").split("/");
+
+       for(int i = 0; i < parts.count(); i++)
+       {
+               parts[i] = lamexp_clean_filename(parts[i]);
+       }
+
+       return parts.join("/");
+}
+
+/*
+ * Get a list of all available Qt Text Codecs
+ */
+QStringList lamexp_available_codepages(bool noAliases)
+{
+       QStringList codecList;
+       
+       QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
+       while(!availableCodecs.isEmpty())
+       {
+               QByteArray current = availableCodecs.takeFirst();
+               if(!(current.startsWith("system") || current.startsWith("System")))
+               {
+                       codecList << QString::fromLatin1(current.constData(), current.size());
+                       if(noAliases)
+                       {
+                               if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
+                               {
+                                       
+                                       QList<QByteArray> aliases = currentCodec->aliases();
+                                       while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
+                               }
+                       }
+               }
+       }
+
+       return codecList;
+}
+
+/*
  * Finalization function (final clean-up)
  */
 void lamexp_finalization(void)
 {
+       qDebug("lamexp_finalization()");
+       
        //Free all tools
        if(!g_lamexp_tool_registry.isEmpty())
        {
@@ -1173,31 +2026,60 @@ void lamexp_finalization(void)
                QApplication::removeTranslator(g_lamexp_currentTranslator);
                LAMEXP_DELETE(g_lamexp_currentTranslator);
        }
-       g_lamexp_translation_files.clear();
-       g_lamexp_translation_names.clear();
+       g_lamexp_translation.files.clear();
+       g_lamexp_translation.names.clear();
 
        //Destroy Qt application object
        QApplication *application = dynamic_cast<QApplication*>(QApplication::instance());
        LAMEXP_DELETE(application);
 
        //Detach from shared memory
-       if(g_lamexp_sharedmem_ptr) g_lamexp_sharedmem_ptr->detach();
-       LAMEXP_DELETE(g_lamexp_sharedmem_ptr);
-       LAMEXP_DELETE(g_lamexp_semaphore_read_ptr);
-       LAMEXP_DELETE(g_lamexp_semaphore_write_ptr);
+       if(g_lamexp_ipc_ptr.sharedmem) g_lamexp_ipc_ptr.sharedmem->detach();
+       LAMEXP_DELETE(g_lamexp_ipc_ptr.sharedmem);
+       LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read);
+       LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write);
+       LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex);
+       LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex);
+
+       //Free STDOUT and STDERR buffers
+       if(g_lamexp_console_attached)
+       {
+               if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cout.rdbuf()))
+               {
+                       std::cout.rdbuf(NULL);
+                       LAMEXP_DELETE(tmp);
+               }
+               if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cerr.rdbuf()))
+               {
+                       std::cerr.rdbuf(NULL);
+                       LAMEXP_DELETE(tmp);
+               }
+       }
+
+       //Close log file
+       if(g_lamexp_log_file)
+       {
+               fclose(g_lamexp_log_file);
+               g_lamexp_log_file = NULL;
+       }
 }
 
 /*
+ * Initialize debug thread
+ */
+static const HANDLE g_debug_thread = LAMEXP_DEBUG ? NULL : lamexp_debug_thread_init();
+
+/*
  * Get number private bytes [debug only]
  */
 SIZE_T lamexp_dbg_private_bytes(void)
 {
-#ifdef _DEBUG
+#if LAMEXP_DEBUG
        PROCESS_MEMORY_COUNTERS_EX memoryCounters;
        memoryCounters.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
        GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS) &memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX));
        return memoryCounters.PrivateUsage;
 #else
        throw "Cannot call this function in a non-debug build!";
-#endif //_DEBUG
+#endif //LAMEXP_DEBUG
 }