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 elapsed time since starting the download. * * \param L The Lua VM state. * \param download The \ref download_t of the download. * * \luastack * \lparam A \c download object. * \lreturn The elapsed time since starting the download in seconds as a number. */ static gint luaH_download_get_elapsed_time(lua_State *L, download_t *download) { gdouble elapsed_time = webkit_download_get_elapsed_time( download->webkit_download); lua_pushnumber(L, elapsed_time); return 1; }
/* 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; }