From 5161dd2a33e0e2147a40865a42461837aee5d7ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robert=20P=C3=B6sel?= <robyer@seznam.cz>
Date: Sun, 20 Jul 2014 13:44:24 +0000
Subject: Plugin Updater: Fixed various issues

- Checking after resume from hibernation finally works
- Checking after having opened update dialog for too long works
- After closing update dialog without updating and checking for updates again now correctly checks for new updates (and not show old unusable window)

git-svn-id: http://svn.miranda-ng.org/main/trunk@9888 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 plugins/PluginUpdater/src/Common.h      |  4 ++--
 plugins/PluginUpdater/src/DlgUpdate.cpp | 14 +++++++++-----
 plugins/PluginUpdater/src/Events.cpp    |  4 ++--
 plugins/PluginUpdater/src/Options.cpp   |  2 +-
 plugins/PluginUpdater/src/Utils.cpp     | 22 ++++++++++++++++------
 plugins/PluginUpdater/src/Version.h     |  2 +-
 6 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/plugins/PluginUpdater/src/Common.h b/plugins/PluginUpdater/src/Common.h
index 52e2732467..c5c192d63f 100644
--- a/plugins/PluginUpdater/src/Common.h
+++ b/plugins/PluginUpdater/src/Common.h
@@ -135,7 +135,7 @@ extern POPUP_OPTIONS PopupOptions;
 extern aPopups PopupsList[POPUPS];
 extern HANDLE Timer, hPipe;
 
-void DoCheck(int iFlag);
+void DoCheck();
 
 void UninitCheck(void);
 void UninitListNew(void);
@@ -206,7 +206,7 @@ void  ShowPopup(HWND hDlg, LPCTSTR Title, LPCTSTR Text, int Number, int ActType,
 void  __stdcall RestartMe(void*);
 void  __stdcall OpenPluginOptions(void*);
 BOOL  AllowUpdateOnStartup();
-void  InitTimer(int type = 0);
+void  InitTimer(void *type);
 
 INT_PTR EmptyFolder(WPARAM,LPARAM);
 
diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp
index c899444eea..c7f02157ce 100644
--- a/plugins/PluginUpdater/src/DlgUpdate.cpp
+++ b/plugins/PluginUpdater/src/DlgUpdate.cpp
@@ -138,6 +138,7 @@ static void ApplyUpdates(void *param)
 	// 5) Prepare Restart
 	int rc = MessageBox(hDlg, TranslateT("Update complete. Press Yes to restart Miranda now or No to postpone a restart until the exit."), TranslateT("Plugin Updater"), MB_YESNO | MB_ICONQUESTION);
 	EndDialog(hDlg, 0);
+	PostMessage(hDlg, WM_DESTROY, 0, 0); // why do we have to call this manually?
 	if (rc == IDYES)
 		CallFunctionAsync(RestartMe, 0);
 }
@@ -319,6 +320,10 @@ static INT_PTR CALLBACK DlgUpdate(HWND hDlg, UINT message, WPARAM wParam, LPARAM
 		opts.bSilent = true;
 		delete (OBJLIST<FILEINFO> *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
 		SetWindowLongPtr(hDlg, GWLP_USERDATA, 0);
+		#if MIRANDA_VER >= 0x0A00
+			db_set_dw(NULL, MODNAME, "LastUpdate", time(NULL));
+		#endif
+		mir_forkthread(InitTimer, (void*)0);
 		break;
 	}
 
@@ -713,13 +718,13 @@ static void CheckUpdates(void *)
 	}
 	else opts.bSilent = true;
 
-	InitTimer(success ? 0 : 2);	
+	mir_forkthread(InitTimer, (void*)(success ? 0 : 2));	
 	
 	hashes.destroy();
 	hCheckThread = NULL;
 }
 
-void DoCheck(int iFlag)
+void DoCheck()
 {
 	if (hCheckThread)
 		ShowPopup(0, LPGENT("Plugin Updater"), LPGENT("Update checking already started!"), 2, 0);
@@ -727,8 +732,7 @@ void DoCheck(int iFlag)
 		ShowWindow(hwndDialog, SW_SHOW);
 		SetForegroundWindow(hwndDialog);
 		SetFocus(hwndDialog);
-	}
-	else if (iFlag) {
+	} else {
 		#if MIRANDA_VER >= 0x0A00
 			db_set_dw(NULL, MODNAME, "LastUpdate", time(NULL));
 		#endif
@@ -745,7 +749,7 @@ void UninitCheck()
 INT_PTR MenuCommand(WPARAM,LPARAM)
 {
 	opts.bSilent = false;
-	DoCheck(true);
+	DoCheck();
 	return 0;
 }
 
diff --git a/plugins/PluginUpdater/src/Events.cpp b/plugins/PluginUpdater/src/Events.cpp
index e892092094..816f92c603 100644
--- a/plugins/PluginUpdater/src/Events.cpp
+++ b/plugins/PluginUpdater/src/Events.cpp
@@ -49,10 +49,10 @@ int ModulesLoaded(WPARAM, LPARAM)
 		EmptyFolder(0, TRUE); // silently
 
 	if (AllowUpdateOnStartup())
-		DoCheck(opts.bUpdateOnStartup);
+		DoCheck();
 
 	Timer = CreateWaitableTimer(NULL, FALSE, NULL);
-	InitTimer();
+	mir_forkthread(InitTimer, (void*)0);
 
 	return 0;
 }
diff --git a/plugins/PluginUpdater/src/Options.cpp b/plugins/PluginUpdater/src/Options.cpp
index f212a396db..11021ac371 100644
--- a/plugins/PluginUpdater/src/Options.cpp
+++ b/plugins/PluginUpdater/src/Options.cpp
@@ -143,7 +143,7 @@ INT_PTR CALLBACK UpdateNotifyOptsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				db_set_b(NULL, MODNAME, "SilentMode", opts.bSilentMode);
 				db_set_dw(NULL, MODNAME, "Period", opts.Period);
 
-				InitTimer(1);
+				mir_forkthread(InitTimer, (void*)1);
 
 				if ( IsDlgButtonChecked(hwndDlg, IDC_STABLE))
 					db_set_s(NULL, MODNAME, "UpdateURL", DEFAULT_UPDATE_URL);
diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp
index cf3de1c7ec..07e17b2231 100644
--- a/plugins/PluginUpdater/src/Utils.cpp
+++ b/plugins/PluginUpdater/src/Utils.cpp
@@ -362,6 +362,9 @@ bool DownloadFile(FILEURL *pFileURL, HANDLE &nlc)
 
 BOOL AllowUpdateOnStartup()
 {
+	if (!opts.bUpdateOnStartup)
+		return FALSE;
+
 	if (opts.bOnlyOnceADay) {
 		time_t now = time(NULL);
 		time_t was = db_get_dw(NULL, MODNAME, "LastUpdate", 0);
@@ -393,16 +396,15 @@ LONG PeriodToMilliseconds(const int period, BYTE& periodMeasure)
 
 void CALLBACK TimerAPCProc(LPVOID lpArg, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
 {
-	DoCheck(true);
+	DoCheck();
 }
 
-void InitTimer(int type)
+void InitTimer(void *type)
 {
-	CancelWaitableTimer(Timer);
 	if (opts.bUpdateOnPeriod) {
 		LONG interval = PeriodToMilliseconds(opts.Period, opts.bPeriodMeasure);
 
-		switch (type) {
+		switch ((int)type) {
 		case 0: // default, plan next check relative to last check
 		{
 			time_t now = time(NULL);
@@ -420,9 +422,17 @@ void InitTimer(int type)
 			break;
 		}
 
-		LARGE_INTEGER li = {0};
-		li.QuadPart = -1 * (interval * 10000LL);
+		FILETIME ft;
+		GetSystemTimeAsFileTime(&ft);
+
+		LARGE_INTEGER li;
+		li.LowPart = ft.dwLowDateTime;
+		li.HighPart = ft.dwHighDateTime;
+		li.QuadPart += (ULONGLONG)(interval * 10000LL);
 		SetWaitableTimer(Timer, &li, 0, TimerAPCProc, NULL, 0);
+
+		// Wait in an alertable state for the timer to go off.
+		SleepEx(INFINITE, TRUE);
 	}
 }
 
diff --git a/plugins/PluginUpdater/src/Version.h b/plugins/PluginUpdater/src/Version.h
index f12beeebe3..bb4ff2248e 100644
--- a/plugins/PluginUpdater/src/Version.h
+++ b/plugins/PluginUpdater/src/Version.h
@@ -1,7 +1,7 @@
 #define __MAJOR_VERSION           0
 #define __MINOR_VERSION           1
 #define __RELEASE_NUM             2
-#define __BUILD_NUM               1
+#define __BUILD_NUM               2
 
 #include <stdver.h>
 
-- 
cgit v1.2.3