From 88315729c6c7253a90e5516c941f2cb63f52ed3f Mon Sep 17 00:00:00 2001 From: MuldeR Date: Tue, 21 May 2013 22:53:19 +0200 Subject: [PATCH] Some code refactoring. --- MediaInfoXP.vcxproj | 3 + MediaInfoXP.vcxproj.filters | 14 ++- src/Config.h | 4 - src/Main.cpp | 183 ++------------------------------ src/MainWindow.cpp | 1 + src/Utils.cpp | 204 ++++++++++++++++++++++++++++++++++++ src/Utils.h | 33 ++++++ z_build.bat | 116 ++++++++++++++++++++ 8 files changed, 374 insertions(+), 184 deletions(-) create mode 100644 src/Utils.cpp create mode 100644 src/Utils.h create mode 100644 z_build.bat diff --git a/MediaInfoXP.vcxproj b/MediaInfoXP.vcxproj index 33ca773..dffaf6c 100644 --- a/MediaInfoXP.vcxproj +++ b/MediaInfoXP.vcxproj @@ -120,11 +120,13 @@ RCC "$(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp" $(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp;%(Outputs) $(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp;%(Outputs) + Designer + @@ -140,6 +142,7 @@ + diff --git a/MediaInfoXP.vcxproj.filters b/MediaInfoXP.vcxproj.filters index a4ee03d..27bf07e 100644 --- a/MediaInfoXP.vcxproj.filters +++ b/MediaInfoXP.vcxproj.filters @@ -27,9 +27,10 @@ Dialogs - - - + + + Header Files + @@ -44,12 +45,15 @@ Source Files\Generated + + Source Files + - + Header Files - + Header Files diff --git a/src/Config.h b/src/Config.h index 712e529..253f697 100644 --- a/src/Config.h +++ b/src/Config.h @@ -41,7 +41,3 @@ static const char *mixp_buildTime = __TIME__; #else #define MIXP_DEBUG (0) #endif - -//Helper macros -#define MIXP_DELETE_OBJ(PTR) do { if((PTR)) { delete ((PTR)); (PTR) = NULL; } } while (0) -#define QWCHAR(STR) reinterpret_cast(STR.utf16()) diff --git a/src/Main.cpp b/src/Main.cpp index a262b9c..cead289 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -29,17 +29,13 @@ //Qt #include -#include -#include #include -#include //Win32 #define WIN32_LEAN_AND_MEAN #include #include #include -#include #ifdef QT_NODLL #include @@ -50,6 +46,7 @@ #include "Config.h" #include "MainWindow.h" +#include "Utils.h" /////////////////////////////////////////////////////////////////////////////// // Debug Console @@ -83,176 +80,6 @@ static void init_console(void) } } -/////////////////////////////////////////////////////////////////////////////// -// Detect TEMP folder -/////////////////////////////////////////////////////////////////////////////// - -QString mixp_getAppDataFolder(void) -{ - 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 GUID GUID_LOCAL_APPDATA = {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}}; - - static SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL; - static SHGetFolderPathFun SHGetFolderPathPtr = NULL; - - if((!SHGetKnownFolderPathPtr) && (!SHGetFolderPathPtr)) - { - QLibrary kernel32Lib("shell32.dll"); - if(kernel32Lib.load()) - { - SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) kernel32Lib.resolve("SHGetKnownFolderPath"); - SHGetFolderPathPtr = (SHGetFolderPathFun) kernel32Lib.resolve("SHGetFolderPathW"); - } - } - - QString folder; - - if(SHGetKnownFolderPathPtr) - { - WCHAR *path = NULL; - if(SHGetKnownFolderPathPtr(GUID_LOCAL_APPDATA, 0x00008000, NULL, &path) == S_OK) - { - //MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST); - QDir folderTemp = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(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, CSIDL_LOCAL_APPDATA, NULL, NULL, path) == S_OK) - { - //MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST); - QDir folderTemp = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(path)))); - if(!folderTemp.exists()) - { - folderTemp.mkpath("."); - } - if(folderTemp.exists()) - { - folder = folderTemp.canonicalPath(); - } - } - delete [] path; - } - - return folder; -} - -QString mixp_getTempFolder(QFile **lockfile) -{ - *lockfile = NULL; - QString tempFolder; - - static const char *TEMP_STR = "Temp"; - const QByteArray WRITE_TEST_DATA = QByteArray("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."); - const QString SUB_FOLDER = QUuid::createUuid().toString(); - - //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()) - { - QFile *testFile = new QFile(QString("%1/~lock.tmp").arg(temp.canonicalPath())); - if(testFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) - { - if(testFile->write(WRITE_TEST_DATA) >= WRITE_TEST_DATA.size()) - { - *lockfile = testFile; testFile = NULL; - tempFolder = temp.canonicalPath(); - } - if(testFile) testFile->remove(); - MIXP_DELETE_OBJ(testFile); - } - } - if(!tempFolder.isEmpty()) - { - return tempFolder; - } - } - - //Create TEMP folder in %LOCALAPPDATA% - QDir localAppData = QDir(mixp_getAppDataFolder()); - if(!localAppData.path().isEmpty()) - { - if(!localAppData.exists()) - { - localAppData.mkpath("."); - } - 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 = new QFile(QString("%1/~lock.tmp").arg(localAppData.canonicalPath())); - if(testFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) - { - if(testFile->write(WRITE_TEST_DATA) >= WRITE_TEST_DATA.size()) - { - *lockfile = testFile; testFile = NULL; - tempFolder = localAppData.canonicalPath(); - } - if(testFile) testFile->remove(); - MIXP_DELETE_OBJ(testFile); - } - } - } - } - if(!tempFolder.isEmpty()) - { - return tempFolder; - } - } - - qFatal("Failed to determine TEMP folder!"); - return QString(); -} - -void mixp_clean_folder(const QString &folderPath) -{ - QDir tempFolder(folderPath); - QFileInfoList entryList = tempFolder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot); - - for(int i = 0; i < entryList.count(); i++) - { - if(entryList.at(i).isDir()) - { - mixp_clean_folder(entryList.at(i).canonicalFilePath()); - } - else - { - for(int j = 0; j < 5; j++) - { - if(QFile::remove(entryList.at(i).canonicalFilePath())) - { - break; - } - } - } - } - - tempFolder.rmdir("."); -} - /////////////////////////////////////////////////////////////////////////////// // MAIN function /////////////////////////////////////////////////////////////////////////////// @@ -269,7 +96,7 @@ int mixp_main(int argc, char* argv[]) if(bConsole) init_console(); - qDebug("MediaInfoXP [%s]", mixp_buildDate); + qDebug("MediaInfoXP v%u.%02u, built on %s at %s.", mixp_versionMajor, mixp_versionMinor, mixp_buildDate, mixp_buildTime); qDebug("Copyright (c) 2004-%s LoRd_MuldeR . Some rights reserved.", &mixp_buildDate[7]); qDebug("Built with Qt v%s, running with Qt v%s.\n", QT_VERSION_STR, qVersion()); @@ -277,6 +104,12 @@ int mixp_main(int argc, char* argv[]) //Get temp folder const QString tempFolder = mixp_getTempFolder(&lockFile); + if(tempFolder.isEmpty()) + { + qFatal("Failed to determine TEMP folder!"); + return 1; + } + qDebug("TEMP folder is:\n%s\n", QDir::toNativeSeparators(tempFolder).toUtf8().constData()); //Create application diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 43b6b5d..b9195fc 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -42,6 +42,7 @@ //Internal #include "Config.h" +#include "Utils.h" //Macros #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); } diff --git a/src/Utils.cpp b/src/Utils.cpp new file mode 100644 index 0000000..3419822 --- /dev/null +++ b/src/Utils.cpp @@ -0,0 +1,204 @@ +/////////////////////////////////////////////////////////////////////////////// +// MediaInfoXP +// Copyright (C) 2004-2013 LoRd_MuldeR +// +// 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 "Utils.h" + +//StdLib +#include +#include +#include +#include + +#pragma intrinsic(_InterlockedExchange) + +//Qt +#include +#include +#include +#include + +//Win32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include + +/* + * Try to lock folder + */ +QString mixp_tryLockFolder(const QString &folderPath, QFile **lockfile) +{ + const QString SUB_FOLDER = QUuid::createUuid().toString(); + const QByteArray WRITE_TEST_DATA = QByteArray("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."); + + QDir folder(folderPath); + if(!folder.exists()) + { + folder.mkdir("."); + } + + if(folder.exists()) + { + folder.mkdir(SUB_FOLDER); + if(folder.cd(SUB_FOLDER) && folder.exists()) + { + QFile *testFile = new QFile(QString("%1/~lock.tmp").arg(folder.canonicalPath())); + if(testFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) + { + if(testFile->write(WRITE_TEST_DATA) >= WRITE_TEST_DATA.size()) + { + *lockfile = testFile; + return folder.canonicalPath(); + } + testFile->remove(); + } + MIXP_DELETE_OBJ(testFile); + } + } + + return QString(); +} + +/* + * Get AppData folder + */ +QString mixp_getAppDataFolder(void) +{ + 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 GUID GUID_LOCAL_APPDATA = {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}}; + + SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL; + SHGetFolderPathFun SHGetFolderPathPtr = NULL; + + QLibrary kernel32Lib("shell32.dll"); + if(kernel32Lib.load()) + { + SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) kernel32Lib.resolve("SHGetKnownFolderPath"); + SHGetFolderPathPtr = (SHGetFolderPathFun) kernel32Lib.resolve("SHGetFolderPathW"); + } + + QString folder; + + if(SHGetKnownFolderPathPtr) + { + qDebug("SHGetKnownFolderPathPtr()\n"); + WCHAR *path = NULL; + if(SHGetKnownFolderPathPtr(GUID_LOCAL_APPDATA, 0x00008000, NULL, &path) == S_OK) + { + QDir folderTemp = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(path)))); + if(!folderTemp.exists()) + { + folderTemp.mkpath("."); + } + if(folderTemp.exists()) + { + folder = folderTemp.canonicalPath(); + } + CoTaskMemFree(path); + } + } + else if(SHGetFolderPathPtr) + { + qDebug("SHGetFolderPathPtr()\n"); + WCHAR *path = new WCHAR[4096]; + if(SHGetFolderPathPtr(NULL, CSIDL_LOCAL_APPDATA, NULL, NULL, path) == S_OK) + { + QDir folderTemp = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(path)))); + if(!folderTemp.exists()) + { + folderTemp.mkpath("."); + } + if(folderTemp.exists()) + { + folder = folderTemp.canonicalPath(); + } + } + delete [] path; + } + + return folder; +} + +/* + * Detect the TEMP folder + */ +QString mixp_getTempFolder(QFile **lockfile) +{ + *lockfile = NULL; + + //Try the %TMP% or %TEMP% directory first + QString tempPath = mixp_tryLockFolder(QDir::temp().absolutePath(), lockfile); + if(!tempPath.isEmpty()) + { + return tempPath; + } + + qWarning("Failed to init %%TEMP%%, falling back to %%LOCALAPPDATA%%\n"); + + //Create TEMP folder in %LOCALAPPDATA% + QString localAppDataPath = mixp_getAppDataFolder(); + if(!localAppDataPath.isEmpty()) + { + if(QDir(localAppDataPath).exists()) + { + tempPath = mixp_tryLockFolder(QString("%1/Temp").arg(localAppDataPath), lockfile); + if(!tempPath.isEmpty()) + { + return tempPath; + } + } + } + + return QString(); +} + +/* + * Clean folder + */ +void mixp_clean_folder(const QString &folderPath) +{ + QDir tempFolder(folderPath); + QFileInfoList entryList = tempFolder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot); + + for(int i = 0; i < entryList.count(); i++) + { + if(entryList.at(i).isDir()) + { + mixp_clean_folder(entryList.at(i).canonicalFilePath()); + } + else + { + for(int j = 0; j < 5; j++) + { + if(QFile::remove(entryList.at(i).canonicalFilePath())) + { + break; + } + } + } + } + + tempFolder.rmdir("."); +} diff --git a/src/Utils.h b/src/Utils.h new file mode 100644 index 0000000..cf6bb28 --- /dev/null +++ b/src/Utils.h @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////// +// MediaInfoXP +// Copyright (C) 2004-2013 LoRd_MuldeR +// +// 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 +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include +#include + +//Helper macros +#define MIXP_DELETE_OBJ(PTR) do { if((PTR)) { delete ((PTR)); (PTR) = NULL; } } while (0) +#define QWCHAR(STR) reinterpret_cast(STR.utf16()) + +//Utils +QString mixp_getTempFolder(QFile **lockfile); +void mixp_clean_folder(const QString &folderPath); diff --git a/z_build.bat b/z_build.bat new file mode 100644 index 0000000..7bdf7c5 --- /dev/null +++ b/z_build.bat @@ -0,0 +1,116 @@ +@echo off +REM /////////////////////////////////////////////////////////////////////////// +REM // Set Paths +REM /////////////////////////////////////////////////////////////////////////// +set "MSVC_PATH=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC" +set "QTVC_PATH=C:\Qt\4.8.4" +set "UPX3_PATH=C:\UPX" + +REM ############################################### +REM # DO NOT MODIFY ANY LINES BELOW THIS LINE !!! # +REM ############################################### + + +REM /////////////////////////////////////////////////////////////////////////// +REM // Setup environment +REM /////////////////////////////////////////////////////////////////////////// +if exist "%QTVC_PATH%\bin\qtvars.bat" ( call "%QTVC_PATH%\bin\qtvars.bat" ) +if exist "%QTVC_PATH%\bin\qtenv2.bat" ( call "%QTVC_PATH%\bin\qtenv2.bat" ) +call "%MSVC_PATH%\vcvarsall.bat" x86 + +REM /////////////////////////////////////////////////////////////////////////// +REM // Check environment +REM /////////////////////////////////////////////////////////////////////////// +if "%VCINSTALLDIR%"=="" ( + echo %%VCINSTALLDIR%% not specified. Please check your MSVC_PATH var! + goto BuildError +) +if "%QTDIR%"=="" ( + echo %%QTDIR%% not specified. Please check your MSVC_PATH var! + goto BuildError +) +if not exist "%VCINSTALLDIR%\bin\cl.exe" ( + echo C++ compiler not found. Please check your MSVC_PATH var! + goto BuildError +) +if not exist "%QTDIR%\bin\moc.exe" ( + echo Qt meta compiler not found. Please check your QTVC_PATH var! + goto BuildError +) + +REM /////////////////////////////////////////////////////////////////////////// +REM // Get current date and time (in ISO format) +REM /////////////////////////////////////////////////////////////////////////// +set "ISO_DATE=" +set "ISO_TIME=" +if not exist "%~dp0\etc\date.exe" BuildError +for /F "tokens=1,2 delims=:" %%a in ('"%~dp0\etc\date.exe" +ISODATE:%%Y-%%m-%%d') do ( + if "%%a"=="ISODATE" set "ISO_DATE=%%b" +) +for /F "tokens=1,2,3,4 delims=:" %%a in ('"%~dp0\etc\date.exe" +ISOTIME:%%T') do ( + if "%%a"=="ISOTIME" set "ISO_TIME=%%b:%%c:%%d" +) +if "%ISO_DATE%"=="" goto BuildError +if "%ISO_TIME%"=="" goto BuildError + +REM /////////////////////////////////////////////////////////////////////////// +REM // Build the binaries +REM /////////////////////////////////////////////////////////////////////////// +echo --------------------------------------------------------------------- +echo BEGIN BUILD +echo --------------------------------------------------------------------- +MSBuild.exe /property:Configuration=release /target:clean "%~dp0\MediaInfoXP.sln" +if not "%ERRORLEVEL%"=="0" goto BuildError +MSBuild.exe /property:Configuration=release /target:rebuild "%~dp0\MediaInfoXP.sln" +if not "%ERRORLEVEL%"=="0" goto BuildError +MSBuild.exe /property:Configuration=release /target:build "%~dp0\MediaInfoXP.sln" +if not "%ERRORLEVEL%"=="0" goto BuildError + +REM /////////////////////////////////////////////////////////////////////////// +REM // Copy base files +REM /////////////////////////////////////////////////////////////////////////// +echo --------------------------------------------------------------------- +echo BEGIN PACKAGING +echo --------------------------------------------------------------------- +set "PACK_PATH=%TMP%\~%RANDOM%%RANDOM%.tmp" +mkdir "%PACK_PATH%" +copy "%~dp0\bin\Win32\Release\*.exe" "%PACK_PATH%" +copy "%~dp0\Copying.txt" "%PACK_PATH%" +copy "%~dp0\doc\*.txt" "%PACK_PATH%" +copy "%~dp0\doc\*.html" "%PACK_PATH%" + +REM /////////////////////////////////////////////////////////////////////////// +REM // Compress +REM /////////////////////////////////////////////////////////////////////////// +"%UPX3_PATH%\upx.exe" --best "%PACK_PATH%\*.exe" + +REM /////////////////////////////////////////////////////////////////////////// +REM // Attributes +REM /////////////////////////////////////////////////////////////////////////// +attrib +R "%PACK_PATH%\*.exe" +attrib +R "%PACK_PATH%\*.html" +attrib +R "%PACK_PATH%\*.txt" + +REM /////////////////////////////////////////////////////////////////////////// +REM // Build the installer +REM /////////////////////////////////////////////////////////////////////////// +"%~dp0\etc\7za.exe" a -mm=Deflate -mfb=258 -mpass=15 -r "%~dp0\out\MediaInfo-GUI.%ISO_DATE%.zip" "%PACK_PATH%\*.*" +rmdir /Q /S "%PACK_PATH%" + +REM /////////////////////////////////////////////////////////////////////////// +REM // COMPLETE +REM /////////////////////////////////////////////////////////////////////////// +echo. +echo Build completed. +echo. +pause +goto:eof + +REM /////////////////////////////////////////////////////////////////////////// +REM // FAILED +REM /////////////////////////////////////////////////////////////////////////// +:BuildError +echo. +echo Build has failed !!! +echo. +pause