Skip to content

Commit

Permalink
Make update checker code check multiple urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Aug 2, 2015
1 parent d0e3d80 commit 86c61df
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 53 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Thumbs.db
.DS_Store
*.swp

build
*.dll
*.exe
/Translation.ini
.DS_Store
6 changes: 2 additions & 4 deletions altdrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

// App
#define APP_NAME L"AltDrag"
#define APP_VERSION "1.0"
#define APP_URL L"http://code.google.com/p/altdrag/"
#define APP_UPDATE_STABLE L"http://altdrag.googlecode.com/svn/wiki/latest-stable.txt"
#define APP_UPDATE_UNSTABLE L"http://altdrag.googlecode.com/svn/wiki/latest-unstable.txt"
#define APP_VERSION "1.1b1"
#define APP_URL L"https://stefansundin.github.io/altdrag/"

// Messages
#define WM_TRAY WM_USER+1
Expand Down
2 changes: 1 addition & 1 deletion config/window.rc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ STYLE DS_3DLOOK | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_CH
CAPTION "About"
FONT 8, "Ms Shell Dlg", 400, 0, 1
{
CONTROL "<a href=""http://stefansundin.com/"">http://stefansundin.com/</a>", IDC_STATIC, "SysLink", 0x50020000, 75, 50, 130, 10
CONTROL "<a href=""https://stefansundin.com/"">https://stefansundin.com/</a>", IDC_STATIC, "SysLink", 0x50020000, 75, 50, 130, 10
GROUPBOX "About", IDC_ABOUT_BOX, 3, 1, 212, 105
ICON IDI_BIGICON, IDC_STATIC, 15, 15, 45, 42, SS_ICON | SS_REALSIZEIMAGE
LTEXT "Version", IDC_VERSION, 75, 15, 130, 8, SS_LEFT
Expand Down
1 change: 0 additions & 1 deletion hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <endpointvolume.h>

// Stuff missing in MinGW
#define WM_MOUSEHWHEEL 0x020E // WM_MOUSEHWHEEL is only defined for >= Vista
CLSID my_CLSID_MMDeviceEnumerator = {0xBCDE0395,0xE52F,0x467C,{0x8E,0x3D,0xC4,0x57,0x92,0x91,0x69,0x2E}};
GUID my_IID_IMMDeviceEnumerator = {0xA95664D2,0x9614,0x4F35,{0xA7,0x46,0xDE,0x8D,0xB6,0x36,0x17,0xE6}};
GUID my_IID_IAudioEndpointVolume = {0x5CDF2C82,0x841E,0x4546,{0x97,0x22,0x0C,0xF7,0x40,0x78,0x22,0x9A}};
Expand Down
133 changes: 87 additions & 46 deletions include/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

#include <wininet.h>

const wchar_t const *update_urls[] = {
L"https://update.stefansundin.com/altdrag/latest-stable.txt",
L"https://stefansundin.github.io/altdrag/latest-stable.txt",
L"https://stefansundin.com/altdrag-latest-stable.txt",
L""
};

const wchar_t const *beta_update_urls[] = {
L"https://update.stefansundin.com/altdrag/latest-beta.txt",
L"https://stefansundin.github.io/altdrag/latest-beta.txt",
L"https://stefansundin.com/altdrag-latest-beta.txt",
L""
};

int update = 0;

int OpenUrl(wchar_t *url) {
Expand Down Expand Up @@ -54,7 +68,7 @@ DWORD WINAPI _CheckForUpdate(LPVOID arg) {
}
}

// Open connection
// Configure client
HINTERNET http = InternetOpen(APP_NAME"/"APP_VERSION, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (http == NULL) {
if (verbose) {
Expand All @@ -64,64 +78,91 @@ DWORD WINAPI _CheckForUpdate(LPVOID arg) {
}
unsigned long timeout = 5000;
InternetSetOption(http, INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof(timeout));
HINTERNET file = InternetOpenUrl(http, (beta?APP_UPDATE_UNSTABLE:APP_UPDATE_STABLE), NULL, 0, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_AUTH|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_NO_UI, 0);
if (file == NULL) {
if (verbose) {
Error(L"InternetOpenUrl()", L"Could not establish connection.\n\nPlease check for update manually on the website.", GetLastError());

short success = 0;
int i = 0;
const wchar_t const **urls = (beta ? beta_update_urls : update_urls);

for (i=0; urls[i][0] != '\0'; i++) {
const wchar_t const *url = urls[i];

// Open connection
// DBG("Checking %s", url);
HINTERNET file = InternetOpenUrl(http, url, NULL, 0, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_AUTH|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_NO_UI, 0);
if (file == NULL) {
// Error(L"InternetOpenUrl(http) == NULL", L"", GetLastError());
continue;
}
InternetCloseHandle(http);
return 1;
}
// Read file
char data[20];
DWORD numread;
if (InternetReadFile(file,data,sizeof(data),&numread) == FALSE) {
if (verbose) {
Error(L"InternetReadFile()", L"Could not read response.\n\nPlease check for update manually on the website.", GetLastError());

// Read response
char data[20];
DWORD numread;
if (InternetReadFile(file,data,sizeof(data),&numread) == FALSE) {
// Error(L"InternetReadFile(file) == NULL", L"", GetLastError());
InternetCloseHandle(file);
continue;
}
data[numread] = '\0';
// Get response code
wchar_t code[4];
DWORD len = sizeof(code);
HttpQueryInfo(file, HTTP_QUERY_STATUS_CODE, &code, &len, NULL);
// Close handle
InternetCloseHandle(file);
InternetCloseHandle(http);
return 1;
}
data[numread] = '\0';
// Get response code
wchar_t code[4];
DWORD len = sizeof(code);
HttpQueryInfo(file, HTTP_QUERY_STATUS_CODE, &code, &len, NULL);
// Close connection
InternetCloseHandle(file);
InternetCloseHandle(http);

// Make sure the response is valid
char header[] = "Version: ";
if (wcscmp(code,L"200") || strstr(data,header) != data) {
if (verbose) {
MessageBox(NULL, L"Could not determine if an update is available.\n\nPlease check for update manually on the website.", APP_NAME, MB_ICONWARNING|MB_OK);
// Make sure the response is valid
char header[] = "Version: ";
if (wcscmp(code,L"200") || strstr(data,header) != data) {
continue;
}
return 2;
}

// New version available?
char *latest = data+strlen(header);
int cmp = strcmp(latest, APP_VERSION);
if (cmp > 0 || (beta && cmp != 0)) {
update = 1;
if (verbose) {
SendMessage(g_hwnd, WM_COMMAND, SWM_UPDATE, 0);
// We have found the version number
success = 1;

// New version available?
char *latest = data+strlen(header);

// Terminate newline, if any
char *p;
for (p=latest; *p != '\0'; p++) {
if (*p == '\r' || *p == '\n') {
*p = '\0';
break;
}
}

// Compare version numbers
int cmp = strcmp(latest, APP_VERSION);
if (cmp > 0 || (beta && cmp != 0)) {
update = 1;
if (verbose) {
SendMessage(g_hwnd, WM_COMMAND, SWM_UPDATE, 0);
}
else {
wcsncpy(tray.szInfo, l10n->update_balloon, ARRAY_SIZE(tray.szInfo));
tray.uFlags |= NIF_INFO;
UpdateTray();
tray.uFlags ^= NIF_INFO;
}
}
else {
wcsncpy(tray.szInfo, l10n->update_balloon, ARRAY_SIZE(tray.szInfo));
tray.uFlags |= NIF_INFO;
UpdateTray();
tray.uFlags ^= NIF_INFO;
update = 0;
if (verbose) {
MessageBox(NULL, l10n->update_nonew, APP_NAME, MB_ICONINFORMATION|MB_OK);
}
}
break;
}
else {
update = 0;

InternetCloseHandle(http);

if (!success) {
if (verbose) {
MessageBox(NULL, l10n->update_nonew, APP_NAME, MB_ICONINFORMATION|MB_OK);
MessageBox(NULL, L"Could not determine if an update is available.\n\nPlease check for update manually on the website.", APP_NAME, MB_ICONWARNING|MB_OK);
}
return 3;
}

return 0;
}

Expand Down

0 comments on commit 86c61df

Please sign in to comment.