2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2011-01-01 17:04:25 +01:00
// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
2010-11-06 23:04:47 +01:00
//
// 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
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
# include "Global.h"
//Qt includes
# include <QApplication>
# include <QMessageBox>
# include <QDir>
# include <QUuid>
# include <QMap>
# include <QDate>
# include <QIcon>
# include <QPlastiqueStyle>
# include <QImageReader>
# include <QSharedMemory>
# include <QSysInfo>
# include <QStringList>
2010-11-08 19:29:36 +01:00
# include <QSystemSemaphore>
2010-11-19 13:31:45 +01:00
# include <QMutex>
2010-11-22 21:45:00 +01:00
# include <QTextCodec>
2010-11-25 16:56:32 +01:00
# include <QLibrary>
2010-12-07 22:58:28 +01:00
# include <QRegExp>
2010-12-28 21:26:16 +01:00
# include <QResource>
# include <QTranslator>
2010-11-06 23:04:47 +01:00
//LameXP includes
2010-11-07 23:06:30 +01:00
# include "Resource.h"
2010-11-06 23:04:47 +01:00
# include "LockedFile.h"
2010-12-07 22:58:28 +01:00
//Windows includes
# include <Windows.h>
2010-11-07 23:06:30 +01:00
//CRT includes
# include <stdio.h>
# include <io.h>
# include <fcntl.h>
2010-11-15 04:42:06 +01:00
# include <intrin.h>
2010-11-07 23:06:30 +01:00
2010-11-07 16:32:54 +01:00
//Debug only includes
# ifdef _DEBUG
# include <Psapi.h>
# endif //_DEBUG
2010-11-06 23:04:47 +01:00
2010-11-11 19:37:16 +01:00
//Static build only macros
# 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)
# endif
2010-11-08 19:29:36 +01:00
///////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////
typedef struct
{
unsigned int command ;
unsigned int reserved_1 ;
unsigned int reserved_2 ;
char parameter [ 4096 ] ;
} lamexp_ipc_t ;
2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARS
///////////////////////////////////////////////////////////////////////////////
//Build version
2010-11-07 23:06:30 +01:00
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 ;
2010-11-06 23:04:47 +01:00
//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__ ;
2010-11-20 22:14:29 +01:00
//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 " ;
# else
# if _MSC_VER == 1600
static const char * g_lamexp_version_compiler = " MSVC 10.0 " ;
# else
static const char * g_lamexp_version_compiler = " UNKNOWN " ;
# endif
# endif
# endif
2010-11-12 15:58:53 +01:00
//Tool versions (expected)
static const unsigned int g_lamexp_toolver_neroaac = VER_LAMEXP_TOOL_NEROAAC ;
2010-11-06 23:04:47 +01:00
//Special folders
static QString g_lamexp_temp_folder ;
//Tools
static QMap < QString , LockedFile * > g_lamexp_tool_registry ;
2010-11-12 15:58:53 +01:00
static QMap < QString , unsigned int > g_lamexp_tool_versions ;
2010-11-06 23:04:47 +01:00
2010-12-28 21:26:16 +01:00
//Languages
2010-12-30 16:12:21 +01:00
static QMap < QString , QString > g_lamexp_translation_files ;
static QMap < QString , QString > g_lamexp_translation_names ;
2011-01-04 19:58:18 +01:00
static QMap < QString , unsigned int > g_lamexp_translation_sysid ;
2010-12-28 21:26:16 +01:00
static QTranslator * g_lamexp_currentTranslator = NULL ;
2010-11-06 23:04:47 +01:00
//Shared memory
static const char * g_lamexp_sharedmem_uuid = " {21A68A42-6923-43bb-9CF6-64BF151942EE} " ;
static QSharedMemory * g_lamexp_sharedmem_ptr = NULL ;
2010-11-08 19:29:36 +01:00
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 ;
2010-11-06 23:04:47 +01:00
2010-11-11 19:37:16 +01:00
//Image formats
static const char * g_lamexp_imageformats [ ] = { " png " , " gif " , " ico " , " svg " , NULL } ;
2010-11-19 13:31:45 +01:00
//Global locks
static QMutex g_lamexp_message_mutex ;
2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// GLOBAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////
/*
* 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 ; }
2010-11-20 22:14:29 +01:00
const char * lamexp_version_compiler ( void ) { return g_lamexp_version_compiler ; }
2010-11-12 15:58:53 +01:00
unsigned int lamexp_toolver_neroaac ( void ) { return g_lamexp_toolver_neroaac ; }
2010-11-07 23:06:30 +01:00
bool lamexp_version_demo ( void )
{
2010-11-15 04:42:06 +01:00
return LAMEXP_DEBUG | | ! ( strstr ( g_lamexp_version_release , " Final " ) | | strstr ( g_lamexp_version_release , " Hotfix " ) ) ;
2010-11-07 23:06:30 +01:00
}
2010-11-06 23:04:47 +01:00
2011-01-20 22:06:59 +01:00
QDate lamexp_version_expires ( void )
{
return lamexp_version_date ( ) . addDays ( 30 ) ;
}
2010-11-06 23:04:47 +01:00
/*
* Get build date date
*/
const QDate & lamexp_version_date ( void )
{
if ( ! g_lamexp_version_date . isValid ( ) )
{
char temp [ 32 ] ;
int date [ 3 ] ;
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 + + )
{
date [ i ] = - 1 ;
if ( this_token )
{
for ( int j = 0 ; j < 12 ; j + + )
{
if ( ! _strcmpi ( this_token , g_lamexp_months [ j ] ) )
{
date [ i ] = j + 1 ;
break ;
}
}
if ( date [ i ] < 0 )
{
date [ i ] = atoi ( this_token ) ;
}
this_token = strtok_s ( NULL , " " , & next_token ) ;
}
}
if ( date [ 0 ] > = 0 & & date [ 1 ] > = 0 & & date [ 2 ] > = 0 )
{
g_lamexp_version_date = QDate ( date [ 2 ] , date [ 0 ] , date [ 1 ] ) ;
}
}
return g_lamexp_version_date ;
}
2010-11-19 13:31:45 +01:00
/*
* Qt message handler
*/
void lamexp_message_handler ( QtMsgType type , const char * msg )
{
static HANDLE hConsole = NULL ;
QMutexLocker lock ( & g_lamexp_message_mutex ) ;
if ( ! hConsole )
{
hConsole = CreateFile ( L " CONOUT$ " , GENERIC_WRITE , FILE_SHARE_WRITE | FILE_SHARE_READ , NULL , OPEN_EXISTING , NULL , NULL ) ;
if ( hConsole = = INVALID_HANDLE_VALUE ) hConsole = NULL ;
}
CONSOLE_SCREEN_BUFFER_INFO bufferInfo ;
GetConsoleScreenBufferInfo ( hConsole , & bufferInfo ) ;
2010-11-21 21:51:22 +01:00
SetConsoleOutputCP ( CP_UTF8 ) ;
2010-11-19 13:31:45 +01:00
switch ( type )
{
case QtCriticalMsg :
case QtFatalMsg :
fflush ( stdout ) ;
fflush ( stderr ) ;
SetConsoleTextAttribute ( hConsole , FOREGROUND_RED | FOREGROUND_INTENSITY ) ;
2010-12-22 01:01:01 +01:00
fwprintf ( stderr , L " \n GURU MEDITATION !!! \n %S \n \n " , msg ) ;
MessageBoxW ( NULL , ( wchar_t * ) QString : : fromUtf8 ( msg ) . utf16 ( ) , L " LameXP - GURU MEDITATION " , MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL ) ;
2010-11-19 13:31:45 +01:00
break ;
case QtWarningMsg :
SetConsoleTextAttribute ( hConsole , FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY ) ;
2010-12-22 01:01:01 +01:00
//MessageBoxW(NULL, (wchar_t*) QString::fromUtf8(msg).utf16(), L"LameXP - GURU MEDITATION", MB_ICONWARNING | MB_TOPMOST | MB_TASKMODAL);
2010-11-21 21:51:22 +01:00
fwprintf ( stderr , L " %S \n " , msg ) ;
2010-11-19 13:31:45 +01:00
fflush ( stderr ) ;
break ;
default :
SetConsoleTextAttribute ( hConsole , FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY ) ;
2010-11-21 21:51:22 +01:00
fwprintf ( stderr , L " %S \n " , msg ) ;
2010-11-19 13:31:45 +01:00
fflush ( stderr ) ;
break ;
}
SetConsoleTextAttribute ( hConsole , FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED ) ;
if ( type = = QtCriticalMsg | | type = = QtFatalMsg )
{
2010-11-20 03:11:12 +01:00
lock . unlock ( ) ;
2010-11-19 13:31:45 +01:00
FatalAppExit ( 0 , L " The application has encountered a critical error and will exit now! " ) ;
TerminateProcess ( GetCurrentProcess ( ) , - 1 ) ;
}
}
2010-11-07 23:06:30 +01:00
/*
* Initialize the console
*/
void lamexp_init_console ( int argc , char * argv [ ] )
{
for ( int i = 0 ; i < argc ; i + + )
{
if ( lamexp_version_demo ( ) | | ! _stricmp ( argv [ i ] , " --console " ) )
{
if ( AllocConsole ( ) )
{
//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 ) ;
}
HMENU hMenu = GetSystemMenu ( GetConsoleWindow ( ) , 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 ;
}
}
}
2010-11-15 04:42:06 +01:00
/*
* Detect CPU features
*/
lamexp_cpu_t lamexp_detect_cpu_features ( void )
{
2010-11-25 16:56:32 +01:00
typedef BOOL ( WINAPI * IsWow64ProcessFun ) ( __in HANDLE hProcess , __out PBOOL Wow64Process ) ;
typedef VOID ( WINAPI * GetNativeSystemInfoFun ) ( __out LPSYSTEM_INFO lpSystemInfo ) ;
static IsWow64ProcessFun IsWow64ProcessPtr = NULL ;
static GetNativeSystemInfoFun GetNativeSystemInfoPtr = NULL ;
2010-11-15 04:42:06 +01:00
2010-11-25 16:56:32 +01:00
lamexp_cpu_t features ;
SYSTEM_INFO systemInfo ;
2010-11-15 04:42:06 +01:00
int CPUInfo [ 4 ] = { - 1 } ;
2010-11-26 00:29:53 +01:00
char CPUIdentificationString [ 0x40 ] ;
2010-11-25 16:56:32 +01:00
char CPUBrandString [ 0x40 ] ;
memset ( & features , 0 , sizeof ( lamexp_cpu_t ) ) ;
memset ( & systemInfo , 0 , sizeof ( SYSTEM_INFO ) ) ;
2010-11-26 00:29:53 +01:00
memset ( CPUIdentificationString , 0 , sizeof ( CPUIdentificationString ) ) ;
2010-11-25 16:56:32 +01:00
memset ( CPUBrandString , 0 , sizeof ( CPUBrandString ) ) ;
2010-11-15 04:42:06 +01:00
__cpuid ( CPUInfo , 0 ) ;
2010-11-26 00:29:53 +01:00
memcpy ( CPUIdentificationString , & CPUInfo [ 1 ] , sizeof ( int ) ) ;
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 ) ;
2010-11-15 04:42:06 +01:00
if ( CPUInfo [ 0 ] > = 1 )
{
__cpuid ( CPUInfo , 1 ) ;
features . mmx = ( CPUInfo [ 3 ] & 0x800000 ) | | false ;
features . sse = ( CPUInfo [ 3 ] & 0x2000000 ) | | false ;
features . sse2 = ( CPUInfo [ 3 ] & 0x4000000 ) | | false ;
features . ssse3 = ( CPUInfo [ 2 ] & 0x200 ) | | false ;
features . sse3 = ( CPUInfo [ 2 ] & 0x1 ) | | false ;
features . ssse3 = ( CPUInfo [ 2 ] & 0x200 ) | | false ;
features . stepping = CPUInfo [ 0 ] & 0xf ;
features . model = ( ( CPUInfo [ 0 ] > > 4 ) & 0xf ) + ( ( ( CPUInfo [ 0 ] > > 16 ) & 0xf ) < < 4 ) ;
features . family = ( ( CPUInfo [ 0 ] > > 8 ) & 0xf ) + ( ( CPUInfo [ 0 ] > > 20 ) & 0xff ) ;
}
__cpuid ( CPUInfo , 0x80000000 ) ;
int nExIds = CPUInfo [ 0 ] ;
for ( int i = 0x80000000 ; 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 ) ) ;
}
strcpy_s ( features . brand , 0x40 , CPUBrandString ) ;
2010-11-15 14:02:58 +01:00
2010-11-25 16:56:32 +01:00
# if !defined(_M_X64 ) && !defined(_M_IA64)
if ( ! IsWow64ProcessPtr | | ! GetNativeSystemInfoPtr )
2010-11-15 14:02:58 +01:00
{
2010-11-25 16:56:32 +01:00
QLibrary Kernel32Lib ( " kernel32.dll " ) ;
IsWow64ProcessPtr = ( IsWow64ProcessFun ) Kernel32Lib . resolve ( " IsWow64Process " ) ;
GetNativeSystemInfoPtr = ( GetNativeSystemInfoFun ) Kernel32Lib . resolve ( " GetNativeSystemInfo " ) ;
2010-11-15 14:02:58 +01:00
}
2010-11-25 16:56:32 +01:00
if ( IsWow64ProcessPtr )
{
BOOL x64 = FALSE ;
if ( IsWow64ProcessPtr ( GetCurrentProcess ( ) , & x64 ) )
{
features . x64 = x64 ;
}
}
if ( GetNativeSystemInfoPtr )
{
GetNativeSystemInfoPtr ( & systemInfo ) ;
}
else
{
GetSystemInfo ( & systemInfo ) ;
}
features . count = systemInfo . dwNumberOfProcessors ;
# else
2010-11-15 14:02:58 +01:00
GetNativeSystemInfo ( & systemInfo ) ;
features . count = systemInfo . dwNumberOfProcessors ;
2010-11-25 16:56:32 +01:00
features . x64 = true ;
# endif
2010-11-15 14:02:58 +01:00
2010-11-15 04:42:06 +01:00
return features ;
}
/*
* Check for debugger
*/
2010-11-25 16:56:32 +01:00
void WINAPI debugThreadProc ( __in LPVOID lpParameter )
2010-11-15 04:42:06 +01:00
{
BOOL remoteDebuggerPresent = FALSE ;
2010-11-25 16:56:32 +01:00
//CheckRemoteDebuggerPresent(GetCurrentProcess, &remoteDebuggerPresent);
2010-11-15 04:42:06 +01:00
while ( ! IsDebuggerPresent ( ) & & ! remoteDebuggerPresent )
{
Sleep ( 333 ) ;
2010-11-25 16:56:32 +01:00
//CheckRemoteDebuggerPresent(GetCurrentProcess, &remoteDebuggerPresent);
2010-11-15 04:42:06 +01:00
}
TerminateProcess ( GetCurrentProcess ( ) , - 1 ) ;
}
2010-12-23 03:13:11 +01:00
/*
* Check for compatibility mode
*/
2010-12-27 01:50:22 +01:00
static bool lamexp_check_compatibility_mode ( const char * exportName )
2010-12-23 03:13:11 +01:00
{
QLibrary kernel32 ( " kernel32.dll " ) ;
2010-12-27 01:50:22 +01:00
if ( exportName ! = NULL )
{
if ( kernel32 . resolve ( exportName ) ! = NULL )
{
qFatal ( " Windows compatibility mode detected. Program will exit! " ) ;
return false ;
}
}
return true ;
}
/*
* Check for process elevation
*/
static bool lamexp_check_elevation ( void )
{
2011-01-03 22:24:58 +01:00
typedef enum { lamexp_token_elevationType_class = 18 , lamexp_token_elevation_class = 20 } LAMEXP_TOKEN_INFORMATION_CLASS ;
typedef enum { lamexp_elevationType_default = 1 , lamexp_elevationType_full , lamexp_elevationType_limited } LAMEXP_TOKEN_ELEVATION_TYPE ;
2010-12-27 01:50:22 +01:00
HANDLE hToken = NULL ;
bool bIsProcessElevated = false ;
2010-12-23 03:13:11 +01:00
2010-12-27 01:50:22 +01:00
if ( OpenProcessToken ( GetCurrentProcess ( ) , TOKEN_QUERY , & hToken ) )
2010-12-23 03:13:11 +01:00
{
2011-01-03 22:24:58 +01:00
LAMEXP_TOKEN_ELEVATION_TYPE tokenElevationType ;
2010-12-27 01:50:22 +01:00
DWORD returnLength ;
2011-01-03 22:24:58 +01:00
if ( GetTokenInformation ( hToken , ( TOKEN_INFORMATION_CLASS ) lamexp_token_elevationType_class , & tokenElevationType , sizeof ( LAMEXP_TOKEN_ELEVATION_TYPE ) , & returnLength ) )
2010-12-23 03:13:11 +01:00
{
2011-01-03 22:24:58 +01:00
if ( returnLength = = sizeof ( LAMEXP_TOKEN_ELEVATION_TYPE ) )
2010-12-23 03:13:11 +01:00
{
2011-01-03 22:24:58 +01:00
switch ( tokenElevationType )
{
case lamexp_elevationType_default :
qDebug ( " Process token elevation type: Default -> UAC is disabled. \n " ) ;
break ;
case lamexp_elevationType_full :
qWarning ( " Process token elevation type: Full -> potential security risk! \n " ) ;
bIsProcessElevated = true ;
break ;
case lamexp_elevationType_limited :
qDebug ( " Process token elevation type: Limited -> not elevated. \n " ) ;
break ;
}
2010-12-23 03:13:11 +01:00
}
}
2010-12-27 01:50:22 +01:00
CloseHandle ( hToken ) ;
}
else
{
qWarning ( " Failed to open process token! " ) ;
2010-12-23 03:13:11 +01:00
}
2010-12-27 01:50:22 +01:00
return ! bIsProcessElevated ;
2010-12-23 03:13:11 +01:00
}
2010-11-15 04:42:06 +01:00
2010-11-06 23:04:47 +01:00
/*
* Initialize Qt framework
*/
bool lamexp_init_qt ( int argc , char * argv [ ] )
{
static bool qt_initialized = false ;
//Don't initialized again, if done already
if ( qt_initialized )
{
return true ;
}
//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 ) ;
//Check the Windows version
2010-11-11 19:37:16 +01:00
switch ( QSysInfo : : windowsVersion ( ) & QSysInfo : : WV_NT_based )
2010-11-06 23:04:47 +01:00
{
case QSysInfo : : WV_2000 :
qDebug ( " Running on Windows 2000 (not offically supported!). \n " ) ;
2010-12-27 01:50:22 +01:00
lamexp_check_compatibility_mode ( " GetNativeSystemInfo " ) ;
2010-11-06 23:04:47 +01:00
break ;
case QSysInfo : : WV_XP :
2010-12-27 01:50:22 +01:00
qDebug ( " Running on Windows XP. \n " ) ;
lamexp_check_compatibility_mode ( " GetLargePageMinimum " ) ;
2010-11-06 23:04:47 +01:00
break ;
case QSysInfo : : WV_2003 :
2010-12-27 01:50:22 +01:00
qDebug ( " Running on Windows Server 2003 or Windows XP x64-Edition. \n " ) ;
lamexp_check_compatibility_mode ( " GetLocaleInfoEx " ) ;
2010-11-06 23:04:47 +01:00
break ;
case QSysInfo : : WV_VISTA :
2010-12-27 01:50:22 +01:00
qDebug ( " Running on Windows Vista or Windows Server 2008. \n " ) ;
lamexp_check_compatibility_mode ( " CreateRemoteThreadEx " ) ;
2010-11-06 23:04:47 +01:00
break ;
case QSysInfo : : WV_WINDOWS7 :
qDebug ( " Running on Windows 7 or Windows Server 2008 R2. \n " ) ;
2010-12-27 01:50:22 +01:00
lamexp_check_compatibility_mode ( NULL ) ;
2010-11-06 23:04:47 +01:00
break ;
default :
qFatal ( " Unsupported OS, only Windows 2000 or later is supported! " ) ;
break ;
}
2010-12-23 03:13:11 +01:00
2010-11-06 23:04:47 +01:00
//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 " ) ;
2010-11-07 23:06:30 +01:00
application - > setWindowIcon ( QIcon ( " :/MainIcon.png " ) ) ;
2010-11-06 23:04:47 +01:00
//Load plugins from application directory
QCoreApplication : : setLibraryPaths ( QStringList ( ) < < QApplication : : applicationDirPath ( ) ) ;
2010-11-07 23:06:30 +01:00
qDebug ( " Library Path: \n %s \n " , QApplication : : libraryPaths ( ) . first ( ) . toUtf8 ( ) . constData ( ) ) ;
2010-11-11 19:37:16 +01:00
//Init static Qt plugins
LAMEXP_INIT_QT_STATIC_PLUGIN ( qico ) ;
LAMEXP_INIT_QT_STATIC_PLUGIN ( qsvg ) ;
2010-11-11 00:25:23 +01:00
2010-11-06 23:04:47 +01:00
//Check for supported image formats
QList < QByteArray > supportedFormats = QImageReader : : supportedImageFormats ( ) ;
2010-11-11 19:37:16 +01:00
for ( int i = 0 ; g_lamexp_imageformats [ i ] ; i + + )
2010-11-06 23:04:47 +01:00
{
2010-11-11 19:37:16 +01:00
if ( ! supportedFormats . contains ( g_lamexp_imageformats [ i ] ) )
{
qFatal ( " Qt initialization error: At least one image format plugin is missing! (%s) " , g_lamexp_imageformats [ i ] ) ;
return false ;
}
2010-11-06 23:04:47 +01:00
}
2010-12-30 16:12:21 +01:00
2011-01-04 19:58:18 +01:00
//Add default translations
g_lamexp_translation_files . insert ( LAMEXP_DEFAULT_LANGID , " " ) ;
g_lamexp_translation_names . insert ( LAMEXP_DEFAULT_LANGID , " English " ) ;
2010-12-30 16:12:21 +01:00
//Init language files
2011-01-04 19:58:18 +01:00
//lamexp_init_translations();
2010-12-28 21:26:16 +01:00
2010-12-27 01:50:22 +01:00
//Check for process elevation
if ( ! lamexp_check_elevation ( ) )
{
if ( QMessageBox : : warning ( NULL , " LameXP " , " <nobr>LameXP was started with elevated rights. This is a potential security risk!</nobr> " , " Quit Program (Recommended) " , " Ignore " ) = = 0 )
{
return false ;
}
}
2010-11-06 23:04:47 +01:00
//Done
qt_initialized = true ;
return true ;
}
/*
2010-11-08 21:47:35 +01:00
* Initialize IPC
2010-11-06 23:04:47 +01:00
*/
2010-11-08 21:47:35 +01:00
int lamexp_init_ipc ( void )
2010-11-06 23:04:47 +01:00
{
2010-11-08 19:29:36 +01:00
if ( g_lamexp_sharedmem_ptr & & g_lamexp_semaphore_read_ptr & & g_lamexp_semaphore_write_ptr )
2010-11-06 23:04:47 +01:00
{
2010-11-08 21:47:35 +01:00
return 0 ;
2010-11-06 23:04:47 +01:00
}
2010-11-11 22:58:02 +01:00
2010-11-12 21:02:14 +01:00
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 ) ;
2010-11-08 19:29:36 +01:00
if ( g_lamexp_semaphore_read_ptr - > 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 ) ;
qFatal ( " Failed to create system smaphore: %s " , errorMessage . toUtf8 ( ) . constData ( ) ) ;
2010-11-08 21:47:35 +01:00
return - 1 ;
2010-11-08 19:29:36 +01:00
}
if ( g_lamexp_semaphore_write_ptr - > 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 ) ;
qFatal ( " Failed to create system smaphore: %s " , errorMessage . toUtf8 ( ) . constData ( ) ) ;
2010-11-08 21:47:35 +01:00
return - 1 ;
2010-11-08 19:29:36 +01:00
}
2010-11-12 21:02:14 +01:00
g_lamexp_sharedmem_ptr = new QSharedMemory ( QString ( g_lamexp_sharedmem_uuid ) , NULL ) ;
2010-11-06 23:04:47 +01:00
2010-11-08 19:29:36 +01:00
if ( ! g_lamexp_sharedmem_ptr - > create ( sizeof ( lamexp_ipc_t ) ) )
2010-11-06 23:04:47 +01:00
{
2010-11-08 19:29:36 +01:00
if ( g_lamexp_sharedmem_ptr - > error ( ) = = QSharedMemory : : AlreadyExists )
2010-11-06 23:04:47 +01:00
{
2010-11-08 19:29:36 +01:00
g_lamexp_sharedmem_ptr - > attach ( ) ;
if ( g_lamexp_sharedmem_ptr - > error ( ) = = QSharedMemory : : NoError )
{
2010-11-08 21:47:35 +01:00
return 1 ;
2010-11-08 19:29:36 +01:00
}
else
{
2010-11-08 21:47:35 +01:00
QString errorMessage = g_lamexp_sharedmem_ptr - > errorString ( ) ;
qFatal ( " Failed to attach to shared memory: %s " , errorMessage . toUtf8 ( ) . constData ( ) ) ;
return - 1 ;
2010-11-08 19:29:36 +01:00
}
2010-11-06 23:04:47 +01:00
}
else
{
2010-11-08 19:29:36 +01:00
QString errorMessage = g_lamexp_sharedmem_ptr - > errorString ( ) ;
2010-11-07 23:06:30 +01:00
qFatal ( " Failed to create shared memory: %s " , errorMessage . toUtf8 ( ) . constData ( ) ) ;
2010-11-08 21:47:35 +01:00
return - 1 ;
2010-11-06 23:04:47 +01:00
}
}
2010-11-08 19:29:36 +01:00
memset ( g_lamexp_sharedmem_ptr - > data ( ) , 0 , sizeof ( lamexp_ipc_t ) ) ;
g_lamexp_semaphore_write_ptr - > release ( ) ;
2010-11-08 21:47:35 +01:00
return 0 ;
2010-11-06 23:04:47 +01:00
}
2010-11-08 19:29:36 +01:00
/*
* IPC send message
*/
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 )
{
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 ;
2010-11-09 22:06:11 +01:00
if ( message )
{
strcpy_s ( lamexp_ipc - > parameter , 4096 , message ) ;
}
2010-11-08 19:29:36 +01:00
2010-11-08 21:47:35 +01:00
if ( g_lamexp_semaphore_write_ptr - > acquire ( ) )
{
memcpy ( g_lamexp_sharedmem_ptr - > data ( ) , lamexp_ipc , sizeof ( lamexp_ipc_t ) ) ;
g_lamexp_semaphore_read_ptr - > release ( ) ;
}
2010-11-08 19:29:36 +01:00
LAMEXP_DELETE ( lamexp_ipc ) ;
}
/*
* IPC read message
*/
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 )
{
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 ) ) ;
2010-11-08 21:47:35 +01:00
if ( g_lamexp_semaphore_read_ptr - > acquire ( ) )
2010-11-08 19:29:36 +01:00
{
2010-11-08 21:47:35 +01:00
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
{
qWarning ( " Malformed IPC message, will be ignored " ) ;
}
2010-11-08 19:29:36 +01:00
}
2010-11-08 21:47:35 +01:00
LAMEXP_DELETE ( lamexp_ipc ) ;
}
2010-12-07 22:58:28 +01:00
/*
* Get a random string
*/
QString lamexp_rand_str ( void )
{
QRegExp regExp ( " \\ {( \\ w+) - ( \ \ w + ) - ( \ \ w + ) - ( \ \ w + ) - ( \ \ w + ) \ \ } " ) ;
QString uuid = QUuid : : createUuid ( ) . toString ( ) ;
if ( regExp . indexIn ( uuid ) > = 0 )
{
return QString ( ) . append ( regExp . cap ( 1 ) ) . append ( regExp . cap ( 2 ) ) . append ( regExp . cap ( 3 ) ) . append ( regExp . cap ( 4 ) ) . append ( regExp . cap ( 5 ) ) ;
}
throw " The RegExp didn't match on the UUID string. This shouldn't happen ;-) " ;
}
2010-11-06 23:04:47 +01:00
/*
* Get LameXP temp folder
*/
const QString & lamexp_temp_folder ( void )
{
2010-12-12 13:44:11 +01:00
static const char * TEMP_STR = " Temp " ;
2010-12-07 22:58:28 +01:00
2010-11-06 23:04:47 +01:00
if ( g_lamexp_temp_folder . isEmpty ( ) )
{
2010-11-20 03:11:12 +01:00
QDir temp = QDir : : temp ( ) ;
2010-12-15 18:43:21 +01:00
QDir localAppData = QDir ( lamexp_known_folder ( lamexp_folder_localappdata ) ) ;
2010-11-20 03:11:12 +01:00
2010-12-15 18:43:21 +01:00
if ( ! localAppData . path ( ) . isEmpty ( ) & & localAppData . exists ( ) )
2010-12-07 22:58:28 +01:00
{
2010-12-15 18:43:21 +01:00
if ( ! localAppData . entryList ( QDir : : AllDirs ) . contains ( TEMP_STR , Qt : : CaseInsensitive ) )
2010-12-07 22:58:28 +01:00
{
2010-12-15 18:43:21 +01:00
localAppData . mkdir ( TEMP_STR ) ;
2010-12-07 22:58:28 +01:00
}
2010-12-15 18:43:21 +01:00
if ( localAppData . cd ( TEMP_STR ) )
2010-12-08 20:53:30 +01:00
{
2010-12-15 18:43:21 +01:00
temp . setPath ( localAppData . absolutePath ( ) ) ;
2010-12-08 20:53:30 +01:00
}
}
2010-12-07 22:58:28 +01:00
2010-11-20 03:11:12 +01:00
if ( ! temp . exists ( ) )
{
temp . mkpath ( " . " ) ;
if ( ! temp . exists ( ) )
{
2010-11-20 16:17:51 +01:00
qFatal ( " The system's temporary directory does not exist: \n %s " , temp . absolutePath ( ) . toUtf8 ( ) . constData ( ) ) ;
2010-11-20 03:11:12 +01:00
return g_lamexp_temp_folder ;
}
}
2010-12-07 22:58:28 +01:00
QString subDir = QString ( " %1.tmp " ) . arg ( lamexp_rand_str ( ) ) ;
if ( ! temp . mkdir ( subDir ) )
2010-11-06 23:04:47 +01:00
{
2010-12-07 22:58:28 +01:00
qFatal ( " Temporary directory could not be created: \n %s " , QString ( " %1/%2 " ) . arg ( temp . canonicalPath ( ) , subDir ) . toUtf8 ( ) . constData ( ) ) ;
2010-11-20 03:11:12 +01:00
return g_lamexp_temp_folder ;
2010-11-06 23:04:47 +01:00
}
2010-12-07 22:58:28 +01:00
if ( ! temp . cd ( subDir ) )
2010-11-20 03:11:12 +01:00
{
2010-12-07 22:58:28 +01:00
qFatal ( " Temporary directory could not be entered: \n %s " , QString ( " %1/%2 " ) . arg ( temp . canonicalPath ( ) , subDir ) . toUtf8 ( ) . constData ( ) ) ;
2010-11-20 03:11:12 +01:00
return g_lamexp_temp_folder ;
}
2010-12-07 22:58:28 +01:00
QFile testFile ( QString ( " %1/.%2 " ) . arg ( temp . canonicalPath ( ) , lamexp_rand_str ( ) ) ) ;
2010-11-20 03:11:12 +01:00
if ( ! testFile . open ( QIODevice : : ReadWrite ) | | testFile . write ( " LAMEXP_TEST \n " ) < 12 )
2010-11-06 23:04:47 +01:00
{
2010-11-20 03:11:12 +01:00
qFatal ( " Write access to temporary directory has been denied: \n %s " , temp . canonicalPath ( ) . toUtf8 ( ) . constData ( ) ) ;
return g_lamexp_temp_folder ;
2010-11-06 23:04:47 +01:00
}
2010-11-20 03:11:12 +01:00
testFile . close ( ) ;
QFile : : remove ( testFile . fileName ( ) ) ;
g_lamexp_temp_folder = temp . canonicalPath ( ) ;
2010-11-06 23:04:47 +01:00
}
return g_lamexp_temp_folder ;
}
/*
* Clean folder
*/
bool lamexp_clean_folder ( const QString folderPath )
{
QDir tempFolder ( folderPath ) ;
2010-12-21 01:09:25 +01:00
QFileInfoList entryList = tempFolder . entryInfoList ( QDir : : AllEntries | QDir : : NoDotAndDotDot ) ;
2010-11-06 23:04:47 +01:00
for ( int i = 0 ; i < entryList . count ( ) ; i + + )
{
if ( entryList . at ( i ) . isDir ( ) )
{
2010-11-15 22:07:46 +01:00
lamexp_clean_folder ( entryList . at ( i ) . canonicalFilePath ( ) ) ;
2010-11-06 23:04:47 +01:00
}
else
{
2010-12-21 01:09:25 +01:00
for ( int j = 0 ; j < 3 ; j + + )
{
if ( lamexp_remove_file ( entryList . at ( i ) . canonicalFilePath ( ) ) )
{
break ;
}
}
2010-11-06 23:04:47 +01:00
}
}
tempFolder . rmdir ( " . " ) ;
return ! tempFolder . exists ( ) ;
}
/*
* Register tool
*/
2010-11-12 15:58:53 +01:00
void lamexp_register_tool ( const QString & toolName , LockedFile * file , unsigned int version )
2010-11-06 23:04:47 +01:00
{
2010-11-11 22:58:02 +01:00
if ( g_lamexp_tool_registry . contains ( toolName . toLower ( ) ) )
2010-11-06 23:04:47 +01:00
{
throw " lamexp_register_tool: Tool is already registered! " ;
}
2010-11-11 22:58:02 +01:00
g_lamexp_tool_registry . insert ( toolName . toLower ( ) , file ) ;
2010-11-12 15:58:53 +01:00
g_lamexp_tool_versions . insert ( toolName . toLower ( ) , version ) ;
2010-11-06 23:04:47 +01:00
}
/*
2010-11-11 22:58:02 +01:00
* Check for tool
*/
bool lamexp_check_tool ( const QString & toolName )
{
return g_lamexp_tool_registry . contains ( toolName . toLower ( ) ) ;
}
/*
2010-12-15 22:39:18 +01:00
* Lookup tool path
2010-11-06 23:04:47 +01:00
*/
const QString lamexp_lookup_tool ( const QString & toolName )
{
2010-11-11 22:58:02 +01:00
if ( g_lamexp_tool_registry . contains ( toolName . toLower ( ) ) )
2010-11-06 23:04:47 +01:00
{
2010-11-12 15:58:53 +01:00
return g_lamexp_tool_registry . value ( toolName . toLower ( ) ) - > filePath ( ) ;
2010-11-06 23:04:47 +01:00
}
else
{
return QString ( ) ;
}
2010-11-07 16:32:54 +01:00
}
2010-11-12 15:58:53 +01:00
/*
2010-12-15 22:39:18 +01:00
* Lookup tool version
2010-11-12 15:58:53 +01:00
*/
unsigned int lamexp_tool_version ( const QString & toolName )
{
if ( g_lamexp_tool_versions . contains ( toolName . toLower ( ) ) )
{
return g_lamexp_tool_versions . value ( toolName . toLower ( ) ) ;
}
else
{
return UINT_MAX ;
}
}
/*
* Version number to human - readable string
*/
2011-01-14 23:34:31 +01:00
const QString lamexp_version2string ( const QString & pattern , unsigned int version , const QString & defaultText )
2010-11-12 15:58:53 +01:00
{
2010-12-15 22:39:18 +01:00
if ( version = = UINT_MAX )
{
2011-01-14 23:34:31 +01:00
return defaultText ;
2010-12-15 22:39:18 +01:00
}
2010-11-12 15:58:53 +01:00
QString result = pattern ;
int digits = result . count ( " ? " , Qt : : CaseInsensitive ) ;
if ( digits < 1 )
{
return result ;
}
int pos = 0 ;
QString versionStr = QString ( ) . sprintf ( QString ( ) . sprintf ( " %%0%du " , digits ) . toLatin1 ( ) . constData ( ) , version ) ;
int index = result . indexOf ( " ? " , Qt : : CaseInsensitive ) ;
while ( index > = 0 & & pos < versionStr . length ( ) )
{
result [ index ] = versionStr [ pos + + ] ;
index = result . indexOf ( " ? " , Qt : : CaseInsensitive ) ;
}
return result ;
}
2011-01-04 19:58:18 +01:00
/*
* Register a new translation
*/
bool lamexp_translation_register ( const QString & langId , const QString & qmFile , const QString & langName , unsigned int & systemId )
{
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 ) ;
return true ;
}
2010-12-28 21:26:16 +01:00
/*
2010-12-30 16:12:21 +01:00
* Get list of all translations
2010-12-28 21:26:16 +01:00
*/
QStringList lamexp_query_translations ( void )
{
2010-12-30 16:12:21 +01:00
return g_lamexp_translation_files . keys ( ) ;
}
/*
* Get translation name
*/
QString lamexp_translation_name ( const QString & langId )
{
return g_lamexp_translation_names . value ( langId . toLower ( ) , QString ( ) ) ;
2010-12-28 21:26:16 +01:00
}
2011-01-02 01:09:05 +01:00
/*
* Get translation system id
*/
2011-01-04 19:58:18 +01:00
unsigned int lamexp_translation_sysid ( const QString & langId )
2011-01-02 01:09:05 +01:00
{
return g_lamexp_translation_sysid . value ( langId . toLower ( ) , 0 ) ;
}
2010-12-28 21:26:16 +01:00
/*
* Install a new translator
*/
2010-12-30 16:12:21 +01:00
bool lamexp_install_translator ( const QString & langId )
2010-12-28 21:26:16 +01:00
{
bool success = false ;
2010-12-30 16:12:21 +01:00
if ( langId . isEmpty ( ) | | langId . toLower ( ) . compare ( LAMEXP_DEFAULT_LANGID ) = = 0 )
2010-12-28 21:26:16 +01:00
{
2011-01-06 00:53:52 +01:00
success = lamexp_install_translator_from_file ( QString ( ) ) ;
2010-12-28 21:26:16 +01:00
}
else
{
2010-12-30 16:12:21 +01:00
QString qmFile = g_lamexp_translation_files . value ( langId . toLower ( ) , QString ( ) ) ;
2010-12-28 21:26:16 +01:00
if ( ! qmFile . isEmpty ( ) )
{
2011-01-06 00:53:52 +01:00
success = lamexp_install_translator_from_file ( QString ( " :/localization/%1 " ) . arg ( qmFile ) ) ;
2010-12-28 21:26:16 +01:00
}
else
{
2010-12-30 16:12:21 +01:00
qWarning ( " Translation '%s' not available! " , langId . toLatin1 ( ) . constData ( ) ) ;
2010-12-28 21:26:16 +01:00
}
}
return success ;
}
2011-01-06 00:53:52 +01:00
/*
* Install a new translator from file
*/
bool lamexp_install_translator_from_file ( const QString & qmFile )
{
bool success = false ;
if ( ! g_lamexp_currentTranslator )
{
g_lamexp_currentTranslator = new QTranslator ( ) ;
}
if ( ! qmFile . isEmpty ( ) )
{
QString qmPath = QFileInfo ( qmFile ) . canonicalFilePath ( ) ;
QApplication : : removeTranslator ( g_lamexp_currentTranslator ) ;
success = g_lamexp_currentTranslator - > load ( qmPath ) ;
QApplication : : installTranslator ( g_lamexp_currentTranslator ) ;
if ( ! success )
{
qWarning ( " Failed to load translation: \n \" %s \" " , qmPath . toLatin1 ( ) . constData ( ) ) ;
}
}
else
{
QApplication : : removeTranslator ( g_lamexp_currentTranslator ) ;
success = true ;
}
return success ;
}
2010-12-15 18:43:21 +01:00
/*
* Locate known folder on local system
*/
QString lamexp_known_folder ( lamexp_known_folder_t folder_id )
{
typedef HRESULT ( WINAPI * SHGetKnownFolderPathFun ) ( __in const GUID & rfid , __in DWORD dwFlags , __in HANDLE hToken , __out PWSTR * ppszPath ) ;
typedef HRESULT ( WINAPI * SHGetFolderPathFun ) ( __in HWND hwndOwner , __in int nFolder , __in HANDLE hToken , __in DWORD dwFlags , __out LPWSTR pszPath ) ;
static const int CSIDL_LOCAL_APPDATA = 0x001c ;
static const int CSIDL_PROGRAM_FILES = 0x0026 ;
2010-12-19 21:23:43 +01:00
static const int CSIDL_SYSTEM_FOLDER = 0x0025 ;
2010-12-15 18:43:21 +01:00
static const GUID GUID_LOCAL_APPDATA = { 0xF1B32785 , 0x6FBA , 0x4FCF , { 0x9D , 0x55 , 0x7B , 0x8E , 0x7F , 0x15 , 0x70 , 0x91 } } ;
static const GUID GUID_LOCAL_APPDATA_LOW = { 0xA520A1A4 , 0x1780 , 0x4FF6 , { 0xBD , 0x18 , 0x16 , 0x73 , 0x43 , 0xC5 , 0xAF , 0x16 } } ;
static const GUID GUID_PROGRAM_FILES = { 0x905e63b6 , 0xc1bf , 0x494e , { 0xb2 , 0x9c , 0x65 , 0xb7 , 0x32 , 0xd3 , 0xd2 , 0x1a } } ;
2010-12-19 21:23:43 +01:00
static const GUID GUID_SYSTEM_FOLDER = { 0x1AC14E77 , 0x02E7 , 0x4E5D , { 0xB7 , 0x44 , 0x2E , 0xB1 , 0xAE , 0x51 , 0x98 , 0xB7 } } ;
2010-12-15 18:43:21 +01:00
2010-12-15 19:57:51 +01:00
static QLibrary * Kernel32Lib = NULL ;
2010-12-15 18:43:21 +01:00
static SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL ;
static SHGetFolderPathFun SHGetFolderPathPtr = NULL ;
if ( ( ! SHGetKnownFolderPathPtr ) & & ( ! SHGetFolderPathPtr ) )
{
2010-12-15 19:57:51 +01:00
if ( ! Kernel32Lib ) Kernel32Lib = new QLibrary ( " shell32.dll " ) ;
SHGetKnownFolderPathPtr = ( SHGetKnownFolderPathFun ) Kernel32Lib - > resolve ( " SHGetKnownFolderPath " ) ;
SHGetFolderPathPtr = ( SHGetFolderPathFun ) Kernel32Lib - > resolve ( " SHGetFolderPathW " ) ;
2010-12-15 18:43:21 +01:00
}
int folderCSIDL = - 1 ;
GUID folderGUID = { 0x0000 , 0x0000 , 0x0000 , { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } } ;
switch ( folder_id )
{
case lamexp_folder_localappdata :
folderCSIDL = CSIDL_LOCAL_APPDATA ;
folderGUID = GUID_LOCAL_APPDATA ;
break ;
case lamexp_folder_programfiles :
folderCSIDL = CSIDL_PROGRAM_FILES ;
folderGUID = GUID_PROGRAM_FILES ;
break ;
2010-12-19 21:23:43 +01:00
case lamexp_folder_systemfolder :
folderCSIDL = CSIDL_SYSTEM_FOLDER ;
folderGUID = GUID_SYSTEM_FOLDER ;
break ;
2010-12-15 18:43:21 +01:00
default :
return QString ( ) ;
break ;
}
QString folder ;
if ( SHGetKnownFolderPathPtr )
{
WCHAR * path = NULL ;
if ( SHGetKnownFolderPathPtr ( folderGUID , 0x00008000 , NULL , & path ) = = S_OK )
{
2010-12-15 19:57:51 +01:00
//MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST);
2010-12-15 18:43:21 +01:00
QDir folderTemp = QDir ( QDir : : fromNativeSeparators ( QString : : fromUtf16 ( reinterpret_cast < const unsigned short * > ( path ) ) ) ) ;
if ( ! folderTemp . exists ( ) )
{
folderTemp . mkpath ( " . " ) ;
}
if ( folderTemp . exists ( ) )
{
folder = folderTemp . canonicalPath ( ) ;
}
CoTaskMemFree ( path ) ;
}
}
else if ( SHGetFolderPathPtr )
{
WCHAR * path = new WCHAR [ 4096 ] ;
if ( SHGetFolderPathPtr ( NULL , folderCSIDL , NULL , NULL , path ) = = S_OK )
{
2010-12-15 19:57:51 +01:00
//MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST);
2010-12-15 18:43:21 +01:00
QDir folderTemp = QDir ( QDir : : fromNativeSeparators ( QString : : fromUtf16 ( reinterpret_cast < const unsigned short * > ( path ) ) ) ) ;
if ( ! folderTemp . exists ( ) )
{
folderTemp . mkpath ( " . " ) ;
}
if ( folderTemp . exists ( ) )
{
folder = folderTemp . canonicalPath ( ) ;
}
}
delete [ ] path ;
}
return folder ;
}
2010-11-12 15:58:53 +01:00
2010-12-21 01:09:25 +01:00
/*
* Safely remove a file
*/
bool lamexp_remove_file ( const QString & filename )
{
if ( ! QFileInfo ( filename ) . exists ( ) | | ! QFileInfo ( filename ) . isFile ( ) )
{
return true ;
}
else
{
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 ) ) ) ;
if ( ! QFile : : remove ( filename ) )
{
qWarning ( " Could not delete \" %s \" " , filename . toLatin1 ( ) . constData ( ) ) ;
return false ;
}
else
{
return true ;
}
}
else
{
return true ;
}
}
}
2010-12-19 00:50:22 +01:00
2011-01-02 20:47:26 +01:00
/*
* Check if visual themes are enabled ( WinXP and later )
*/
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 " ) ;
if ( IsAppThemedPtr )
{
isAppThemed = IsAppThemedPtr ( ) ;
if ( ! isAppThemed )
{
qWarning ( " Theme support is disabled for this process! " ) ;
}
}
return isAppThemed ;
}
2010-12-19 00:50:22 +01:00
/*
2010-12-28 21:26:16 +01:00
* Get number of free bytes on disk
2010-12-19 00:50:22 +01:00
*/
__int64 lamexp_free_diskspace ( const QString & path )
{
ULARGE_INTEGER freeBytesAvailable , totalNumberOfBytes , totalNumberOfFreeBytes ;
if ( GetDiskFreeSpaceExW ( reinterpret_cast < const wchar_t * > ( QDir : : toNativeSeparators ( path ) . utf16 ( ) ) , & freeBytesAvailable , & totalNumberOfBytes , & totalNumberOfFreeBytes ) )
{
return freeBytesAvailable . QuadPart ;
}
else
{
return 0 ;
}
}
2010-12-28 21:26:16 +01:00
/*
* Finalization function ( final clean - up )
*/
void lamexp_finalization ( void )
{
//Free all tools
if ( ! g_lamexp_tool_registry . isEmpty ( ) )
{
QStringList keys = g_lamexp_tool_registry . keys ( ) ;
for ( int i = 0 ; i < keys . count ( ) ; i + + )
{
LAMEXP_DELETE ( g_lamexp_tool_registry [ keys . at ( i ) ] ) ;
}
g_lamexp_tool_registry . clear ( ) ;
g_lamexp_tool_versions . clear ( ) ;
}
//Delete temporary files
if ( ! g_lamexp_temp_folder . isEmpty ( ) )
{
for ( int i = 0 ; i < 100 ; i + + )
{
if ( lamexp_clean_folder ( g_lamexp_temp_folder ) )
{
break ;
}
Sleep ( 125 ) ;
}
g_lamexp_temp_folder . clear ( ) ;
}
//Clear languages
2010-12-30 16:12:21 +01:00
if ( g_lamexp_currentTranslator )
{
QApplication : : removeTranslator ( g_lamexp_currentTranslator ) ;
LAMEXP_DELETE ( g_lamexp_currentTranslator ) ;
}
g_lamexp_translation_files . clear ( ) ;
g_lamexp_translation_names . clear ( ) ;
2010-12-28 21:26:16 +01:00
//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 ) ;
}
2010-11-07 16:32:54 +01:00
/*
* Get number private bytes [ debug only ]
*/
SIZE_T lamexp_dbg_private_bytes ( void )
{
# ifdef _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
}