OSDN Git Service

Small optimization of lamexp_themes_enabled().
[lamexp/LameXP.git] / src / Global.cpp
index 0494a3a..2cc2ff9 100644 (file)
@@ -133,9 +133,9 @@ static bool g_lamexp_console_attached = false;
        #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";
+               static const char *g_lamexp_version_compiler = "ICL 11.x";
        #elif (__INTEL_COMPILER >= 1000)
-               static const char *g_lamexp_version_compiler = "ICL 10.x";
+               static const char *g_lamexp_version_compiler = "ICL 10.x";
        #else
                #error Compiler is not supported!
        #endif
@@ -342,21 +342,48 @@ const QDate &lamexp_version_date(void)
  */
 DWORD lamexp_get_os_version(void)
 {
-       OSVERSIONINFO osVerInfo;
-       memset(&osVerInfo, 0, sizeof(OSVERSIONINFO));
-       osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-       DWORD version = 0;
+       static DWORD osVersion = 0;
        
-       if(GetVersionEx(&osVerInfo) == TRUE)
+       if(!osVersion)
        {
-               if(osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
+               OSVERSIONINFO osVerInfo;
+               memset(&osVerInfo, 0, sizeof(OSVERSIONINFO));
+               osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+       
+               if(GetVersionEx(&osVerInfo) == TRUE)
                {
-                       throw "Ouuups: Not running under Windows NT. This is not supposed to happen!";
+                       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));
                }
-               version = (DWORD)((osVerInfo.dwMajorVersion << 16) | (osVerInfo.dwMinorVersion & 0xffff));
        }
 
-       return version;
+       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;
 }
 
 /*
@@ -978,7 +1005,6 @@ static bool lamexp_check_elevation(void)
 bool lamexp_init_qt(int argc, char* argv[])
 {
        static bool qt_initialized = false;
-       bool isWine = false;
        typedef BOOL (WINAPI *SetDllDirectoryProc)(WCHAR *lpPathName);
 
        //Don't initialized again, if done already
@@ -1053,15 +1079,12 @@ bool lamexp_init_qt(int argc, char* argv[])
        }
 
        //Check for Wine
-       QLibrary ntdll("ntdll.dll");
-       if(ntdll.load())
+       if(lamexp_detect_wine())
        {
-               if(ntdll.resolve("wine_nt_to_unix_file_name") != NULL) isWine = true;
-               if(ntdll.resolve("wine_get_version") != NULL) isWine = true;
-               if(isWine) qWarning("It appears we are running under Wine, unexpected things might happen!\n");
-               ntdll.unload();
+               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");
@@ -1094,16 +1117,19 @@ bool lamexp_init_qt(int argc, char* argv[])
        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 && !isWine)
+       if(g_lamexp_console_attached && (!lamexp_detect_wine()))
        {
                typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
                QLibrary kernel32("kernel32.dll");
@@ -1635,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;
@@ -1749,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;
@@ -2001,6 +2038,23 @@ void lamexp_finalization(void)
        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)