OSDN Git Service

Implemented the current_date() function.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 18:58:36 +0000 (19:58 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 18:58:36 +0000 (19:58 +0100)
include/MUtils/OSSupport.h
src/OSSupport_Win32.cpp
src/UpdateChecker.cpp

index 66e03cf..c7f0f84 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <QString>
+#include <QDate>
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -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);
        }
index 26bb5d6..0cd6a13 100644 (file)
@@ -30,6 +30,7 @@
 #define WIN32_LEAN_AND_MEAN 1
 #include <Windows.h>
 #include <Objbase.h>
+#include <Psapi.h>
 
 //Qt
 #include <QMap>
@@ -159,6 +160,66 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// CURRENT DATA (SAFE)
+///////////////////////////////////////////////////////////////////////////////
+
+QDate MUtils::OS::current_date(void)
+{
+       const DWORD MAX_PROC = 1024;
+       QScopedArrayPointer<DWORD> 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
 ///////////////////////////////////////////////////////////////////////////////
 
index ab6c11c..cff804f 100644 (file)
@@ -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!");