static void test_webkit_download_create(void) { WebKitNetworkRequest* request; WebKitDownload* download; const gchar* uri = "http://example.com"; gchar* tmpDir; request = webkit_network_request_new(uri); download = webkit_download_new(request); g_object_unref(request); g_assert_cmpstr(webkit_download_get_uri(download), ==, uri); g_assert(webkit_download_get_network_request(download) == request); g_assert(g_strrstr(uri, webkit_download_get_suggested_filename(download))); g_assert(webkit_download_get_status(download) == WEBKIT_DOWNLOAD_STATUS_CREATED); g_assert(!webkit_download_get_total_size(download)); g_assert(!webkit_download_get_current_size(download)); g_assert(!webkit_download_get_progress(download)); g_assert(!webkit_download_get_elapsed_time(download)); tmpDir = g_filename_to_uri(g_get_tmp_dir(), NULL, NULL); webkit_download_set_destination_uri(download, tmpDir); g_assert_cmpstr(tmpDir, ==, webkit_download_get_destination_uri(download));; g_free(tmpDir); g_object_unref(download); }
/** * Returns the expected total size of the download. * * May vary during downloading as not all servers send this correctly. * * \param L The Lua VM state. * \param download The \ref download_t of the download. * * \luastack * \lparam A \c download object. * \lreturn The total size of the download in bytes as a number. */ static gint luaH_download_get_total_size(lua_State *L, download_t *download) { gdouble total_size = webkit_download_get_total_size( download->webkit_download); lua_pushnumber(L, total_size); return 1; }
/* download_on_timeout */ static gboolean _download_on_timeout(gpointer data) { gboolean ret = TRUE; Download * d = data; #ifdef WITH_WEBKIT WebKitDownloadStatus status; guint64 received = d->data_received; /* FIXME not very efficient */ status = webkit_download_get_status(d->conn); switch(status) { case WEBKIT_DOWNLOAD_STATUS_ERROR: ret = FALSE; gtk_label_set_text(GTK_LABEL(d->status), _("Error")); break; case WEBKIT_DOWNLOAD_STATUS_FINISHED: /* XXX pasted from _http_data_complete */ ret = FALSE; gtk_label_set_text(GTK_LABEL(d->status), _("Complete")); gtk_widget_set_sensitive(d->check, FALSE); gtk_button_set_label(GTK_BUTTON(d->cancel), GTK_STOCK_CLOSE); gtk_widget_show(d->browse); d->data_received = webkit_download_get_current_size( d->conn); g_object_unref(d->conn); d->conn = NULL; if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( d->check))) { g_idle_add(_download_on_closex, d); break; } break; case WEBKIT_DOWNLOAD_STATUS_STARTED: gtk_label_set_text(GTK_LABEL(d->status), _("Downloading")); d->data_received = webkit_download_get_current_size( d->conn); d->content_length = webkit_download_get_total_size( d->conn); if(d->content_length == d->data_received) { d->pulse = (d->data_received > received) ? 1 : 0; d->content_length = 0; } break; default: /* XXX anything else to handle here? */ break; } #endif _download_refresh(d); if(ret != TRUE) d->timeout = 0; return ret; }
JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1webkit_1download_1get_1total_1size) (JNIEnv *env, jclass that, jintLong arg0) { jlong rc = 0; WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1download_1get_1total_1size_FUNC); rc = (jlong)webkit_download_get_total_size((WebKitDownload *)arg0); WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1download_1get_1total_1size_FUNC); return rc; }
static VALUE Download_total_size(VALUE self) { VALUE __p_retval = Qnil; WebKitDownload *_self = ((WebKitDownload*)RVAL2GOBJ(self)); #line 239 "/home/geoff/Projects/gtk-webkit-ruby/ext/webkit/webkit.cr" do { __p_retval = rb_ull2inum(webkit_download_get_total_size(_self)); goto out; } while(0); out: return __p_retval; }
gboolean download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_data) { (void) web_view; (void) user_data; /* get the URI being downloaded */ const gchar *uri = webkit_download_get_uri(download); /* get the destination path, if specified. * this is only intended to be set when this function is trigger by an * explicit download using uzbl's 'download' action. */ const gchar *destination = user_data; if (uzbl.state.verbose) printf("Download requested -> %s\n", uri); if (!uzbl.behave.download_handler) { webkit_download_cancel(download); return FALSE; /* reject downloads when there's no download handler */ } /* get a reasonable suggestion for a filename */ const gchar *suggested_filename; #ifdef USE_WEBKIT2 WebKitURIResponse *response; g_object_get(download, "network-response", &response, NULL); #if WEBKIT_CHECK_VERSION (1, 9, 90) g_object_get(response, "suggested-filename", &suggested_filename, NULL); #else suggested_filename = webkit_uri_response_get_suggested_filename(respose); #endif #elif WEBKIT_CHECK_VERSION (1, 9, 6) WebKitNetworkResponse *response; g_object_get(download, "network-response", &response, NULL); g_object_get(response, "suggested-filename", &suggested_filename, NULL); #else g_object_get(download, "suggested-filename", &suggested_filename, NULL); #endif /* get the mimetype of the download */ const gchar *content_type = NULL; WebKitNetworkResponse *r = webkit_download_get_network_response(download); /* downloads can be initiated from the context menu, in that case there is no network response yet and trying to get one would crash. */ if(WEBKIT_IS_NETWORK_RESPONSE(r)) { SoupMessage *m = webkit_network_response_get_message(r); SoupMessageHeaders *h = NULL; g_object_get(m, "response-headers", &h, NULL); if(h) /* some versions of libsoup don't have "response-headers" here */ content_type = soup_message_headers_get_one(h, "Content-Type"); } if(!content_type) content_type = "application/octet-stream"; /* get the filesize of the download, as given by the server. (this may be inaccurate, there's nothing we can do about that.) */ unsigned int total_size = webkit_download_get_total_size(download); GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); const CommandInfo *c = parse_command_parts(uzbl.behave.download_handler, a); if(!c) { webkit_download_cancel(download); g_array_free(a, TRUE); return FALSE; } g_array_append_val(a, uri); g_array_append_val(a, suggested_filename); g_array_append_val(a, content_type); gchar *total_size_s = g_strdup_printf("%d", total_size); g_array_append_val(a, total_size_s); if(destination) g_array_append_val(a, destination); GString *result = g_string_new (""); run_parsed_command(c, a, result); g_free(total_size_s); g_array_free(a, TRUE); /* no response, cancel the download */ if(result->len == 0) { webkit_download_cancel(download); return FALSE; } /* we got a response, it's the path we should download the file to */ gchar *destination_path = result->str; g_string_free(result, FALSE); /* presumably people don't need newlines in their filenames. */ char *p = strchr(destination_path, '\n'); if ( p != NULL ) *p = '\0'; /* set up progress callbacks */ g_signal_connect(download, "notify::status", G_CALLBACK(download_status_cb), NULL); g_signal_connect(download, "notify::progress", G_CALLBACK(download_progress_cb), NULL); /* convert relative path to absolute path */ if(destination_path[0] != '/') { gchar *rel_path = destination_path; gchar *cwd = g_get_current_dir(); destination_path = g_strconcat(cwd, "/", destination_path, NULL); g_free(cwd); g_free(rel_path); } send_event(DOWNLOAD_STARTED, NULL, TYPE_STR, destination_path, NULL); /* convert absolute path to file:// URI */ gchar *destination_uri = g_strconcat("file://", destination_path, NULL); g_free(destination_path); webkit_download_set_destination_uri(download, destination_uri); g_free(destination_uri); return TRUE; }
/* download_progress_cb(WebKitDownload *) {{{*/ static void download_progress_cb(WebKitDownload *download, GParamSpec *p, DwbDownloadStatus *status) { /* Update at most four times a second */ gint64 time = g_get_monotonic_time(); static double speed = 0; static guint remaining = 0; if (time - status->time > 250000) { GList *l = download_get_download_label(download); DwbDownload *label = l->data; double elapsed = webkit_download_get_elapsed_time(download); double progress = webkit_download_get_progress(download); double total_size = (double)webkit_download_get_total_size(download) / 0x100000; if (time - status->speedtime > 1000000) { speed = ((progress - status->progress)*total_size) * 1000000 / (time - status->speedtime); status->speedtime = time; status->progress = progress; remaining = (guint)(elapsed / progress - elapsed); } double current_size = (double)webkit_download_get_current_size(download) / 0x100000; char buffer[128] = {0}; const char *format = speed > 1 ? "[%.1fM/s|%d:%02d|%2d%%|%.3f/%.3f]" : "[%3.1fK/s|%d:%02d|%2d%%|%.3f/%.3f]"; snprintf(buffer, sizeof(buffer), format, speed > 1 ? speed : speed*1024, remaining/60, remaining%60, (int)(progress*100), current_size, total_size); gtk_label_set_text(GTK_LABEL(label->rlabel), buffer); #if _HAS_GTK3 gdouble red, green, blue, alpha; #else guint red, green, blue; #endif red = ((progress) * dwb.color.download_end.red + (1-progress) * dwb.color.download_start.red); green = ((progress) * dwb.color.download_end.green + (1-progress) * dwb.color.download_start.green); blue = ((progress) * dwb.color.download_end.blue + (1-progress) * dwb.color.download_start.blue); #if _HAS_GTK3 alpha = ((progress) * dwb.color.download_end.alpha + (1-progress) * dwb.color.download_start.alpha); if (blue != status->blue || red != status->red || green != status->green || alpha != status->alpha) { #else if (blue != status->blue || red != status->red || green != status->green) { #endif DwbColor gradient = { .red = red, .green = green, .blue = blue, #if _HAS_GTK3 .alpha = alpha #endif }; DWB_WIDGET_OVERRIDE_BACKGROUND(label->event, GTK_STATE_NORMAL, &gradient); } status->blue = blue; status->red = red; status->green = green; #if _HAS_GTK3 status->alpha = alpha; #endif status->time = time; }