Some code refactoring.

This commit is contained in:
LoRd_MuldeR 2013-05-21 22:53:19 +02:00
parent d4f7e96578
commit 88315729c6
8 changed files with 374 additions and 184 deletions

View File

@ -120,11 +120,13 @@
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">RCC "$(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp"</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp;%(Outputs)</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)\tmp\Common\rcc\RCC_%(Filename).cpp;%(Outputs)</Outputs>
<SubType>Designer</SubType>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Main.cpp" />
<ClCompile Include="src\MainWindow.cpp" />
<ClCompile Include="src\Utils.cpp" />
<ClCompile Include="tmp\Common\moc\MOC_MainWindow.cpp" />
<ClCompile Include="tmp\Common\rcc\RCC_MediaInfoXP.cpp" />
</ItemGroup>
@ -140,6 +142,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Config.h" />
<ClInclude Include="src\Utils.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="MediaInfoXP.rcx">

View File

@ -27,9 +27,10 @@
<CustomBuild Include="gui\Dialog.ui">
<Filter>Dialogs</Filter>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<None Include="MediaInfoXP.qrc" />
<CustomBuild Include="MediaInfoXP.qrc" />
<CustomBuild Include="src\MainWindow.h">
<Filter>Header Files</Filter>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Main.cpp">
@ -44,12 +45,15 @@
<ClCompile Include="tmp\Common\rcc\RCC_MediaInfoXP.cpp">
<Filter>Source Files\Generated</Filter>
</ClCompile>
<ClCompile Include="src\Utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\MainWindow.h">
<ClInclude Include="src\Config.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Config.h">
<ClInclude Include="src\Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

View File

@ -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<const wchar_t*>(STR.utf16())

View File

@ -29,17 +29,13 @@
//Qt
#include <QApplication>
#include <QMutex>
#include <QUuid>
#include <QDir>
#include <QLibrary>
//Win32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
#include <Objbase.h>
#ifdef QT_NODLL
#include <QtPlugin>
@ -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<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, CSIDL_LOCAL_APPDATA, NULL, NULL, path) == S_OK)
{
//MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST);
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;
}
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 <mulder2@gmx.de>. 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

View File

@ -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); }

204
src/Utils.cpp Normal file
View File

@ -0,0 +1,204 @@
///////////////////////////////////////////////////////////////////////////////
// MediaInfoXP
// Copyright (C) 2004-2013 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
// 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 <cstdio>
#include <iostream>
#include <fstream>
#include <ctime>
#pragma intrinsic(_InterlockedExchange)
//Qt
#include <QLibrary>
#include <QDir>
#include <QFileInfo>
#include <QUuid>
//Win32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
#include <Objbase.h>
/*
* 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<const unsigned short*>(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<const unsigned short*>(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(".");
}

33
src/Utils.h Normal file
View File

@ -0,0 +1,33 @@
///////////////////////////////////////////////////////////////////////////////
// MediaInfoXP
// Copyright (C) 2004-2013 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
// 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 <QString>
#include <QFile>
//Helper macros
#define MIXP_DELETE_OBJ(PTR) do { if((PTR)) { delete ((PTR)); (PTR) = NULL; } } while (0)
#define QWCHAR(STR) reinterpret_cast<const wchar_t*>(STR.utf16())
//Utils
QString mixp_getTempFolder(QFile **lockfile);
void mixp_clean_folder(const QString &folderPath);

116
z_build.bat Normal file
View File

@ -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