summaryrefslogtreecommitdiff
path: root/frontends/windows/download.c
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2016-10-30 12:58:43 +0000
committerVincent Sanders <vince@kyllikki.org>2016-10-30 12:58:43 +0000
commita2388a91cf9ceb7efb203a7b4d6d250395cdb744 (patch)
tree12c969f54fd1b1a688dfe92bc94fde13ec6958d0 /frontends/windows/download.c
parent3ecced92f335f80a372cf3fb0ab1c6f7c564cebb (diff)
downloadnetsurf-a2388a91cf9ceb7efb203a7b4d6d250395cdb744.tar.gz
netsurf-a2388a91cf9ceb7efb203a7b4d6d250395cdb744.tar.bz2
Rationalise the use of win32 application instance handle use
The use of the application instance handle global variable was inconsistent throughout the windows frontend. By rationalising the passing of these handles it showed that some of the toolbar and throbber parent handles were also setup wrong giving odd offset behaviour. All these issues have been addressed and the throbber is now in the correct position.
Diffstat (limited to 'frontends/windows/download.c')
-rw-r--r--frontends/windows/download.c276
1 files changed, 141 insertions, 135 deletions
diff --git a/frontends/windows/download.c b/frontends/windows/download.c
index fff47ace8..3a969834e 100644
--- a/frontends/windows/download.c
+++ b/frontends/windows/download.c
@@ -16,6 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+/**
+ * \file
+ * windows frontend download implementation
+ *
+ * \todo The windows download functionality is very buggy this needs redoing
+ */
+
#include <limits.h>
#include "utils/inet.h" /* get correct winsock ordering */
#include <shlobj.h>
@@ -48,30 +55,139 @@ struct gui_download_window {
char *original_total_size;
int size;
int downloaded;
- unsigned int progress;
+ unsigned int progress;
int time_remaining;
struct timeval start_time;
int speed;
int error;
- struct gui_window *window;
+ struct gui_window *window;
FILE *file;
- download_status status;
+ download_status status;
};
static bool downloading = false;
static struct gui_download_window *download1;
-BOOL CALLBACK nsws_download_event_callback(HWND hwnd, UINT msg, WPARAM wparam,
- LPARAM lparam);
-static void nsws_download_update_label(void *p);
-static void nsws_download_update_progress(void *p);
-static void nsws_download_clear_data(struct gui_download_window *w);
+
+static void nsws_download_update_label(void *p)
+{
+ struct gui_download_window *w = p;
+ if (w->hwnd == NULL) {
+ win32_schedule(-1, nsws_download_update_label, p);
+ return;
+ }
+ HWND sub = GetDlgItem(w->hwnd, IDC_DOWNLOAD_LABEL);
+ char *size = human_friendly_bytesize(w->downloaded);
+ int i = 0, temp = w->time_remaining;
+ if (temp == -1) {
+ w->time_left = strdup(messages_get("UnknownSize"));
+ i = strlen(w->time_left);
+ } else {
+ do {
+ temp = temp / 10;
+ i++;
+ } while (temp > 2);
+ w->time_left = malloc(i + SLEN(" s") + 1);
+ if (w->time_left != NULL) {
+ if (w->time_remaining > 3600)
+ sprintf(w->time_left, "%d h",
+ w->time_remaining / 3600);
+ else if (w->time_remaining > 60)
+ sprintf(w->time_left, "%d m",
+ w->time_remaining / 60);
+ else
+ sprintf(w->time_left, "%d s",
+ w->time_remaining);
+ }
+ }
+ char label[strlen(w->title) + strlen(size) + strlen(w->total_size) +
+ + strlen(w->domain) + strlen(w->filename) +
+ SLEN("download from to \n[\t/\t]\n estimate of time"
+ " remaining ") + i + 1];
+ sprintf(label, "download %s from %s to %s\n[%s\t/\t%s] [%d%%]\n"
+ "estimate of time remaining %s", w->title, w->domain,
+ w->filename, size, w->total_size, w->progress / 100,
+ w->time_left);
+ if (w->time_left != NULL) {
+ free(w->time_left);
+ w->time_left = NULL;
+ }
+ SendMessage(sub, WM_SETTEXT, (WPARAM)0, (LPARAM)label);
+ if (w->progress < 10000) {
+ win32_schedule(500, nsws_download_update_label, p);
+ }
+}
+
+
+static void nsws_download_update_progress(void *p)
+{
+ struct gui_download_window *w = p;
+ if (w->hwnd == NULL) {
+ win32_schedule(-1, nsws_download_update_progress, p);
+ return;
+ }
+ HWND sub = GetDlgItem(w->hwnd, IDC_DOWNLOAD_PROGRESS);
+ SendMessage(sub, PBM_SETPOS, (WPARAM)(w->progress / 100), 0);
+ if (w->progress < 10000) {
+ win32_schedule(500, nsws_download_update_progress, p);
+ }
+}
+
+
+static void nsws_download_clear_data(struct gui_download_window *w)
+{
+ if (w == NULL)
+ return;
+ if (w->title != NULL)
+ free(w->title);
+ if (w->filename != NULL)
+ free(w->filename);
+ if (w->domain != NULL)
+ free(w->domain);
+ if (w->time_left != NULL)
+ free(w->time_left);
+ if (w->total_size != NULL)
+ free(w->total_size);
+ if (w->file != NULL)
+ fclose(w->file);
+ win32_schedule(-1, nsws_download_update_progress, (void *)w);
+ win32_schedule(-1, nsws_download_update_label, (void *)w);
+}
+
+
+static BOOL CALLBACK
+nsws_download_event_callback(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
+{
+ switch(msg) {
+ case WM_INITDIALOG:
+ nsws_download_update_label((void *)download1);
+ nsws_download_update_progress((void *)download1);
+ return TRUE;
+
+ case WM_COMMAND:
+ switch(LOWORD(wparam)) {
+ case IDOK:
+ if (download1->downloaded != download1->size)
+ return TRUE;
+
+ case IDCANCEL:
+ nsws_download_clear_data(download1);
+ download1 = NULL;
+ downloading = false;
+ EndDialog(hwnd, IDCANCEL);
+ return FALSE;
+ }
+ }
+ return FALSE;
+}
+
static bool nsws_download_window_up(struct gui_download_window *w)
{
- w->hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DLG_DOWNLOAD),
- gui_window_main_window(w->window),
- nsws_download_event_callback);
+ w->hwnd = CreateDialog(hinst,
+ MAKEINTRESOURCE(IDD_DLG_DOWNLOAD),
+ gui_window_main_window(w->window),
+ nsws_download_event_callback);
if (w->hwnd == NULL) {
return false;
}
@@ -79,6 +195,7 @@ static bool nsws_download_window_up(struct gui_download_window *w)
return true;
}
+
static struct gui_download_window *
gui_download_window_create(download_context *ctx, struct gui_window *gui)
{
@@ -88,8 +205,8 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
return NULL;
}
downloading = true;
- struct gui_download_window *w =
- malloc(sizeof(struct gui_download_window));
+ struct gui_download_window *w =
+ malloc(sizeof(struct gui_download_window));
if (w == NULL) {
win32_warning(messages_get("NoMemory"), 0);
return NULL;
@@ -98,10 +215,10 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
char *domain, *filename, *destination;
nsurl *url = download_context_get_url(ctx);
bool unknown_size = (total_size == 0);
- const char *size = (unknown_size) ?
- messages_get("UnknownSize") :
- human_friendly_bytesize(total_size);
-
+ const char *size = (unknown_size) ?
+ messages_get("UnknownSize") :
+ human_friendly_bytesize(total_size);
+
if (nsurl_nice(url, &filename, false) != NSERROR_OK) {
filename = strdup(messages_get("UnknownFile"));
}
@@ -186,119 +303,9 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
}
-BOOL CALLBACK nsws_download_event_callback(HWND hwnd, UINT msg, WPARAM wparam,
- LPARAM lparam)
-{
- switch(msg) {
- case WM_INITDIALOG:
- nsws_download_update_label((void *)download1);
- nsws_download_update_progress((void *)download1);
- return TRUE;
-
- case WM_COMMAND:
- switch(LOWORD(wparam)) {
- case IDOK:
- if (download1->downloaded != download1->size)
- return TRUE;
-
- case IDCANCEL:
- nsws_download_clear_data(download1);
- download1 = NULL;
- downloading = false;
- EndDialog(hwnd, IDCANCEL);
- return FALSE;
- }
- }
- return FALSE;
-}
-
-void nsws_download_update_label(void *p)
-{
- struct gui_download_window *w = p;
- if (w->hwnd == NULL) {
- win32_schedule(-1, nsws_download_update_label, p);
- return;
- }
- HWND sub = GetDlgItem(w->hwnd, IDC_DOWNLOAD_LABEL);
- char *size = human_friendly_bytesize(w->downloaded);
- int i = 0, temp = w->time_remaining;
- if (temp == -1) {
- w->time_left = strdup(messages_get("UnknownSize"));
- i = strlen(w->time_left);
- } else {
- do {
- temp = temp / 10;
- i++;
- } while (temp > 2);
- w->time_left = malloc(i + SLEN(" s") + 1);
- if (w->time_left != NULL) {
- if (w->time_remaining > 3600)
- sprintf(w->time_left, "%d h",
- w->time_remaining / 3600);
- else if (w->time_remaining > 60)
- sprintf(w->time_left, "%d m",
- w->time_remaining / 60);
- else
- sprintf(w->time_left, "%d s",
- w->time_remaining);
- }
- }
- char label[strlen(w->title) + strlen(size) + strlen(w->total_size) +
- + strlen(w->domain) + strlen(w->filename) +
- SLEN("download from to \n[\t/\t]\n estimate of time"
- " remaining ") + i + 1];
- sprintf(label, "download %s from %s to %s\n[%s\t/\t%s] [%d%%]\n"
- "estimate of time remaining %s", w->title, w->domain,
- w->filename, size, w->total_size, w->progress / 100,
- w->time_left);
- if (w->time_left != NULL) {
- free(w->time_left);
- w->time_left = NULL;
- }
- SendMessage(sub, WM_SETTEXT, (WPARAM)0, (LPARAM)label);
- if (w->progress < 10000) {
- win32_schedule(500, nsws_download_update_label, p);
- }
-}
-
-void nsws_download_update_progress(void *p)
-{
- struct gui_download_window *w = p;
- if (w->hwnd == NULL) {
- win32_schedule(-1, nsws_download_update_progress, p);
- return;
- }
- HWND sub = GetDlgItem(w->hwnd, IDC_DOWNLOAD_PROGRESS);
- SendMessage(sub, PBM_SETPOS, (WPARAM)(w->progress / 100), 0);
- if (w->progress < 10000) {
- win32_schedule(500, nsws_download_update_progress, p);
- }
-}
-
-void nsws_download_clear_data(struct gui_download_window *w)
-{
- if (w == NULL)
- return;
- if (w->title != NULL)
- free(w->title);
- if (w->filename != NULL)
- free(w->filename);
- if (w->domain != NULL)
- free(w->domain);
- if (w->time_left != NULL)
- free(w->time_left);
- if (w->total_size != NULL)
- free(w->total_size);
- if (w->file != NULL)
- fclose(w->file);
- win32_schedule(-1, nsws_download_update_progress, (void *)w);
- win32_schedule(-1, nsws_download_update_label, (void *)w);
-}
-
-
-static nserror
+static nserror
gui_download_window_data(struct gui_download_window *w, const char *data,
- unsigned int size)
+ unsigned int size)
{
if ((w == NULL) || (w->file == NULL))
return NSERROR_SAVE_FAILED;
@@ -309,16 +316,16 @@ gui_download_window_data(struct gui_download_window *w, const char *data,
LOG("file write error %d of %d", size - res, size);
w->downloaded += res;
w->progress = (unsigned int)(((long long)(w->downloaded) * 10000)
- / w->size);
+ / w->size);
gettimeofday(&val, NULL);
- w->time_remaining = (w->progress == 0) ? -1 :
- (int)((val.tv_sec - w->start_time.tv_sec) *
- (10000 - w->progress) / w->progress);
+ w->time_remaining = (w->progress == 0) ? -1 :
+ (int)((val.tv_sec - w->start_time.tv_sec) *
+ (10000 - w->progress) / w->progress);
return NSERROR_OK;
}
static void gui_download_window_error(struct gui_download_window *w,
- const char *error_msg)
+ const char *error_msg)
{
LOG("error %s", error_msg);
}
@@ -341,4 +348,3 @@ static struct gui_download_table download_table = {
};
struct gui_download_table *win32_download_table = &download_table;
-