diff --git a/include/MUtils/OSSupport.h b/include/MUtils/OSSupport.h index 66e03cf..c7f0f84 100644 --- a/include/MUtils/OSSupport.h +++ b/include/MUtils/OSSupport.h @@ -22,6 +22,7 @@ #pragma once #include +#include /////////////////////////////////////////////////////////////////////////////// @@ -42,6 +43,9 @@ namespace MUtils //Get known Folder const QString &known_folder(known_folder_t folder_id); + //Current Date + QDate current_date(void); + //Error handling void fatal_exit(const char* const errorMessage); } diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index 26bb5d6..0cd6a13 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -30,6 +30,7 @@ #define WIN32_LEAN_AND_MEAN 1 #include #include +#include //Qt #include @@ -158,6 +159,66 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id) return g_known_folders_map->operator[](folderId); } +/////////////////////////////////////////////////////////////////////////////// +// CURRENT DATA (SAFE) +/////////////////////////////////////////////////////////////////////////////// + +QDate MUtils::OS::current_date(void) +{ + const DWORD MAX_PROC = 1024; + QScopedArrayPointer processes(new DWORD[MAX_PROC]); + DWORD bytesReturned = 0; + + if(!EnumProcesses(processes.data(), sizeof(DWORD) * MAX_PROC, &bytesReturned)) + { + return QDate::currentDate(); + } + + const DWORD procCount = bytesReturned / sizeof(DWORD); + ULARGE_INTEGER lastStartTime; + memset(&lastStartTime, 0, sizeof(ULARGE_INTEGER)); + + for(DWORD i = 0; i < procCount; i++) + { + if(HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processes[i])) + { + FILETIME processTime[4]; + if(GetProcessTimes(hProc, &processTime[0], &processTime[1], &processTime[2], &processTime[3])) + { + ULARGE_INTEGER timeCreation; + timeCreation.LowPart = processTime[0].dwLowDateTime; + timeCreation.HighPart = processTime[0].dwHighDateTime; + if(timeCreation.QuadPart > lastStartTime.QuadPart) + { + lastStartTime.QuadPart = timeCreation.QuadPart; + } + } + CloseHandle(hProc); + } + } + + FILETIME lastStartTime_fileTime; + lastStartTime_fileTime.dwHighDateTime = lastStartTime.HighPart; + lastStartTime_fileTime.dwLowDateTime = lastStartTime.LowPart; + + FILETIME lastStartTime_localTime; + if(!FileTimeToLocalFileTime(&lastStartTime_fileTime, &lastStartTime_localTime)) + { + memcpy(&lastStartTime_localTime, &lastStartTime_fileTime, sizeof(FILETIME)); + } + + SYSTEMTIME lastStartTime_system; + if(!FileTimeToSystemTime(&lastStartTime_localTime, &lastStartTime_system)) + { + memset(&lastStartTime_system, 0, sizeof(SYSTEMTIME)); + lastStartTime_system.wYear = 1970; lastStartTime_system.wMonth = lastStartTime_system.wDay = 1; + } + + const QDate currentDate = QDate::currentDate(); + const QDate processDate = QDate(lastStartTime_system.wYear, lastStartTime_system.wMonth, lastStartTime_system.wDay); + return (currentDate >= processDate) ? currentDate : processDate; +} + /////////////////////////////////////////////////////////////////////////////// // FATAL EXIT /////////////////////////////////////////////////////////////////////////////// diff --git a/src/UpdateChecker.cpp b/src/UpdateChecker.cpp index ab6c11c..cff804f 100644 --- a/src/UpdateChecker.cpp +++ b/src/UpdateChecker.cpp @@ -688,20 +688,22 @@ bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *upd } } } - + if(!updateInfoDate.isValid()) { updateInfo->resetInfo(); log("WARNING: Version info timestamp is missing!"); return false; } - else if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < current_date_safe()) + + const QDate currentDate = OS::current_date(); + if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < currentDate) { updateInfo->resetInfo(); log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS).toString(Qt::ISODate))); return false; } - else if(current_date_safe() < updateInfoDate) + else if(currentDate < updateInfoDate) { log("Version info is from the future, take care!"); qWarning("Version info is from the future, take care!");