Windows Launcher: Download and install dependencies
[gpodder.git] / tools / win32-launcher / downloader.c
blobc0dfd8fe30c469196077bb0e01f5ea7d50cd2917
2 #include <stdio.h>
3 #include <wininet.h>
4 #include <commctrl.h>
6 unsigned long
7 DownloadFile(const char *filename, const char *url, unsigned long size)
9 HINTERNET inet;
10 HINTERNET connection;
11 FILE *out;
12 char buf[1024*10];
13 char line[1024];
14 long readBytes;
15 unsigned long totalBytes = 0;
16 int error = 0;
17 HWND dlg;
18 HWND progress;
19 HWND label;
20 MSG msg;
22 #if defined(GPODDER_GUI)
23 dlg = CreateDialog(NULL, "PROGRESS", NULL, NULL);
25 progress = GetDlgItem(dlg, 1);
26 SendMessage(progress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
28 label = GetDlgItem(dlg, 3);
29 SendMessage(label, WM_SETTEXT, 0, (LPARAM)filename);
31 label = GetDlgItem(dlg, 4);
32 #endif
34 inet = InternetOpen("gpodder-dependency-downloader",
35 INTERNET_OPEN_TYPE_PRECONFIG,
36 NULL,
37 NULL,
38 0);
40 connection = InternetOpenUrl(inet,
41 url,
42 NULL,
44 INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_CACHE_WRITE |
45 INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI,
46 0);
48 out = fopen(filename, "wb");
49 if (out == NULL) {
50 error = 1;
53 while (out != NULL) {
54 if (!InternetReadFile(connection,
55 buf,
56 sizeof(buf),
57 &readBytes)) {
58 error = 1;
59 break;
62 if (readBytes == 0) {
63 break;
66 fwrite(buf, readBytes, 1, out);
68 totalBytes += readBytes;
70 snprintf(line, sizeof(line), "%.2f / %.2f MB",
71 (float)totalBytes / (float)(1024*1024),
72 (float)size / (float)(1024*1024));
74 #if defined(GPODDER_CLI)
75 fprintf(stderr, "Downloading: %s\r", line);
76 #endif
78 #if defined(GPODDER_GUI)
79 SendMessage(label, WM_SETTEXT,
80 0, (LPARAM)TEXT(line));
82 SendMessage(progress, PBM_SETPOS,
83 (int)(100*(float)totalBytes/(float)size), 0);
85 while (PeekMessage(&msg, dlg, 0, 0, PM_NOREMOVE)) {
86 if (GetMessage(&msg, NULL, 0, 0) > 0) {
87 TranslateMessage(&msg);
88 DispatchMessage(&msg);
91 #endif
94 #if defined(GPODDER_GUI)
95 DestroyWindow(dlg);
96 #endif
98 fclose(out);
99 InternetCloseHandle(connection);
100 InternetCloseHandle(inet);
101 if (error) {
102 return 0;
103 } else {
104 return totalBytes;