Debian Codegraph @

Contrib/Makensisw/update.cpp

raw
1 // Unicode support by Jim Park -- 08/20/2007
2
3 #include "makensisw.h"
4 #include "update.h"
5 #include <wininet.h>
6 #include "utils.h" // OpenUrlInDefaultBrowser
7
8 #define mbtitle "NSIS Update"
9
10 static LPSTR InetGetErrorStringAllocA(DWORD ec)
11 {
12 LPSTR buf;
13 DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS;
14 if (FormatMessageA(flags|FORMAT_MESSAGE_FROM_SYSTEM, 0, ec, 0, (LPSTR) &buf, 0, 0))
15 return buf;
16 HMODULE hMod = LoadSysLibrary("WinInet");
17 if (hMod && FormatMessageA(flags|FORMAT_MESSAGE_FROM_HMODULE, hMod, ec, 0, (LPSTR) &buf, 0, 0))
18 return buf;
19 #if 0 // We are not using WinHttp* functions
20 hMod = LoadSysLibrary("WinHTTP");
21 if (hMod && FormatMessageA(flags|FORMAT_MESSAGE_FROM_HMODULE, hMod, ec, 0, (LPSTR) &buf, 0, 0))
22 return buf;
23 #endif
24 return NULL;
25 }
26
27 static void InetWorkOnline(HINTERNET hInet)
28 {
29 //msdn.microsoft.com/en-us/library/Aa768170#Supporting Offline Browsing in Applications and Components
30 DWORD cbio = sizeof(DWORD), op32;
31 if (InternetQueryOption(hInet, INTERNET_OPTION_CONNECTED_STATE, &op32, &cbio))
32 {
33 if (INTERNET_STATE_DISCONNECTED_BY_USER & op32)
34 {
35 INTERNET_CONNECTED_INFO ci = { INTERNET_STATE_CONNECTED, 0 };
36 InternetSetOption(hInet, INTERNET_OPTION_CONNECTED_STATE, &ci, sizeof(ci));
37 }
38 }
39 }
40
41 static DWORD InetSynchronousReadFile(HINTERNET hConn, void*Buffer, DWORD cbBuffer, DWORD*pcbReceived)
42 {
43 *pcbReceived = 0;
44 for (DWORD cbio;;)
45 {
46 if (!cbBuffer)
47 break;
48 if (!InternetReadFile(hConn, Buffer, cbBuffer, &cbio))
49 return GetLastError();
50 if (!cbio)
51 break; // EOF, we are done.
52 Buffer = ((char*)Buffer) + cbio, cbBuffer -= cbio;
53 (*pcbReceived) += cbio;
54 }
55 return ERROR_SUCCESS;
56 }
57
58 static DWORD CALLBACK UpdateCheckWebrequestThread(LPVOID ThreadParam)
59 {
60 char buf[300];
61 wsprintfA(buf, "%s%s", NSIS_UC_URL, g_sdata.brandingv);
62
63 InternetAttemptConnect(0);
64 UINT mbicon = MB_ICONINFORMATION;
65 LPCSTR msg = NULL;
66 HINTERNET hInet = InternetOpenA("MakeNSISw (WinInet)", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
67 DWORD gle = GetLastError();
68 if (hInet)
69 {
70 InetWorkOnline(hInet);
71
72 // Note: InternetOpenUrlW does not handle the http headers in the same way, be careful if you stop forcing the A version
73 static const CHAR httpheaders[] = "Accept:*/*\r\n";
74 DWORD connflags = INTERNET_FLAG_NO_UI|INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS;
75 connflags |= INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES;
76 connflags |= INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_RELOAD;
77 //if ((32|buf[4]) == 's') connflags |= INTERNET_FLAG_SECURE;
78 HINTERNET hConn = InternetOpenUrlA(hInet, buf, httpheaders, -1, connflags, 0);
79 gle = GetLastError();
80 if (hConn)
81 {
82 char response[30+1];
83 DWORD cbRecv;
84 gle = InetSynchronousReadFile(hConn, response, sizeof(response)-1, &cbRecv);
85 if (!gle)
86 {
87 response[cbRecv] = '\0';
88 switch(response[0])
89 {
90 case '0':
91 msg = "There is no update available for NSIS at this time.";
92 break;
93 case '1':
94 case '2':
95 if (cbRecv > 2 && '|' == response[1])
96 {
97 const char *reltypestr = '1' == response[0] ? "it" : "this preview release";
98 wsprintfA(buf, "NSIS %.50s is now available. Would you like to download %s now?", response+2, reltypestr);
99 mbicon = MB_ICONQUESTION, msg = buf;
100 break;
101 }
102 // fall through
103 default:
104 gle = ERROR_INVALID_DATA;
105 }
106 }
107 InternetCloseHandle(hConn);
108 }
109 InternetCloseHandle(hInet);
110 }
111
112 HWND hwndOwner = (HWND) ThreadParam;
113 // Hopefully we don't end up with a race where our main window has been closed
114 // and the HWND has been reused just as this thread completes its work
115 if (IsWindow(hwndOwner))
116 {
117 if (!msg)
118 {
119 LPSTR dynbuf = InetGetErrorStringAllocA(gle);
120 wsprintfA(buf, "There was a problem checking for updates, please try again later.\n\nError: %u %.200s", gle, dynbuf ? dynbuf : "");
121 mbicon = MB_ICONSTOP, msg = buf;
122 LocalFree(dynbuf);
123 }
124
125 UINT mbtype = MB_ICONQUESTION == mbicon ? MB_YESNO : MB_OK;
126 int mbret = MessageBoxA(hwndOwner, msg, mbtitle, mbtype|mbicon);
127 if (mbret == IDYES && mbtype == MB_YESNO)
128 {
129 OpenUrlInDefaultBrowser(hwndOwner, NSIS_DL_URL);
130 }
131
132 EnableMenuItem(g_sdata.menu, IDM_NSISUPDATE, MF_ENABLED);
133 }
134 return 0;
135 }
136
137 void CheckForUpdate()
138 {
139 if (strstr(g_sdata.brandingv, "cvs"))
140 {
141 MessageBoxA(g_sdata.hwnd, "Cannot check for new version of nightly builds. To update, download a new nightly build.", mbtitle, MB_OK|MB_ICONSTOP);
142 return;
143 }
144
145 EnableMenuItem(g_sdata.menu, IDM_NSISUPDATE, MF_GRAYED);
146 DWORD tid;
147 CloseHandle(CreateThread(NULL, 0, UpdateCheckWebrequestThread, (LPVOID) g_sdata.hwnd, 0, &tid));
148 }