Example #1
0
static void nsgtk_download_store_update_item (struct gui_download_window *dl)
{
	gchar *info = nsgtk_download_info_to_string(dl);
	char *human = human_friendly_bytesize(dl->speed);
	char speed[strlen(human) + SLEN("/s") + 1];
	sprintf(speed, "%s/s", human);
	gchar *time = nsgtk_download_time_to_string(dl->time_remaining);
	gboolean pulse = dl->status == NSGTK_DOWNLOAD_WORKING;

	/* Updates iter (which is needed to set and get data) with the dl row */
	gtk_tree_model_get_iter(GTK_TREE_MODEL(nsgtk_download_store),
				&nsgtk_download_iter,
				gtk_tree_row_reference_get_path(dl->row));

	gtk_list_store_set(nsgtk_download_store, &nsgtk_download_iter,
			   NSGTK_DOWNLOAD_PULSE, pulse ? dl->progress : -1,
			   NSGTK_DOWNLOAD_PROGRESS, pulse ? 0 : dl->progress,
			   NSGTK_DOWNLOAD_INFO, info,
			   NSGTK_DOWNLOAD_SPEED, dl->speed == 0 ? "-" : speed,
			   NSGTK_DOWNLOAD_REMAINING, time,
			   NSGTK_DOWNLOAD, dl,
			   -1);

	g_free(info);
	g_free(time);
}
Example #2
0
NSDownloadWindow::NSDownloadWindow(download_context* ctx)
	: BWindow(BRect(30, 30, 400, 200), "Downloads", B_TITLED_WINDOW,
		B_NOT_RESIZABLE)
	, ctx(ctx)
	, progress(0)
	, success(false)
{
	unsigned long dlsize = download_context_get_total_length(ctx);
	char* buffer = human_friendly_bytesize(dlsize);

	// Create the status bar
	BRect rect = Bounds();
	rect.InsetBy(3, 3);
	bar = new BStatusBar(rect, "progress",
		download_context_get_filename(ctx), buffer);
	bar->SetMaxValue(dlsize);

	// Create the backgroundview (just so that the area around the progress bar
	// is B_PANEL_BACKGROUND_COLOR instead of white)
	BView* back = new BView(Bounds(), "back", B_FOLLOW_ALL_SIDES, B_WILL_DRAW);
	back->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

	// Add the views to the window
	back->AddChild(bar);
	AddChild(back);

	// Resize the window to leave a margin around the progress bar
	BRect size = bar->Bounds();
	ResizeTo(size.Width() + 6, size.Height() + 6);
	Show();
}
Example #3
0
static gchar* nsgtk_download_info_to_string(struct gui_download_window *dl)
{
	gchar *size_info = g_strdup_printf(messages_get("gtkSizeInfo"),
					   human_friendly_bytesize(dl->size_downloaded),
					   dl->size_total == 0 ? messages_get("gtkUnknownSize") :
					   human_friendly_bytesize(dl->size_total));

	gchar *r;

	if (dl->status != NSGTK_DOWNLOAD_ERROR)
		r = g_strdup_printf("%s\n%s", dl->name->str, size_info);
	else
		r = g_strdup_printf("%s\n%s", dl->name->str,
				    dl->error->message);

	g_free(size_info);

	return r;
}
Example #4
0
/**
 * Callback function for fetch progress.
 */
static int
fetch_curl_progress(void *clientp,
		    double dltotal,
		    double dlnow,
		    double ultotal,
		    double ulnow)
{
	static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */
	struct curl_fetch_info *f = (struct curl_fetch_info *) clientp;
	uint64_t time_now_ms;
	fetch_msg msg;

	if (f->abort) {
		return 0;
        }

	msg.type = FETCH_PROGRESS;
	msg.data.progress = fetch_progress_buffer;

	/* Rate limit each fetch's progress notifications */
        nsu_getmonotonic_ms(&time_now_ms);
#define UPDATE_DELAY_MS (1000 / UPDATES_PER_SECOND)
	if (time_now_ms - f->last_progress_update < UPDATE_DELAY_MS) {
		return 0;
        }
#undef UPDATE_DELAY_MS
	f->last_progress_update = time_now_ms;

	if (dltotal > 0) {
		snprintf(fetch_progress_buffer, 255,
				messages_get("Progress"),
				human_friendly_bytesize(dlnow),
				human_friendly_bytesize(dltotal));
		fetch_send_callback(&msg, f->fetch_handle);
	} else {
		snprintf(fetch_progress_buffer, 255,
				messages_get("ProgressU"),
				human_friendly_bytesize(dlnow));
		fetch_send_callback(&msg, f->fetch_handle);
	}

	return 0;
}
Example #5
0
void
NSDownloadWindow::Progress(int size)
{
	progress += size;

	char* buffer = human_friendly_bytesize(progress);
	strcat(buffer, "/");

	bar->LockLooper();
	bar->Update(size, NULL, buffer);
	bar->Invalidate();
	bar->UnlockLooper();
}
Example #6
0
BOOL ami_download_check_overwrite(const char *file, struct Window *win, ULONG size)
{
	/* Return TRUE if file can be (over-)written */
	int32 res = 0;
	BPTR lock = 0;
	char *overwritetext;

	if(nsoption_bool(ask_overwrite) == false) return TRUE;

	lock = Lock(file, ACCESS_READ);

	if(lock)
	{
		if(size) {
			BPTR fh;
			int64 oldsize = 0;

			if((fh = OpenFromLock(lock))) {
				oldsize = GetFileSize(fh);
				Close(fh);
			}
			overwritetext = ASPrintf("%s\n\n%s %s\n%s %s",
				messages_get("OverwriteFile"),
				messages_get("amiSizeExisting"), human_friendly_bytesize((ULONG)oldsize),
				messages_get("amiSizeNew"), human_friendly_bytesize(size));
		} else {
			UnLock(lock);
			overwritetext = ASPrintf(messages_get("OverwriteFile"));
		}

		res = amiga_warn_user_multi(overwritetext, "Replace", "DontReplace", win);
		FreeVec(overwritetext);
	}
	else return TRUE;

	if(res == 1) return TRUE;
		else return FALSE;
}
Example #7
0
void ro_gui_download_update_status(struct gui_download_window *dw)
{
	char *received;
	char *total_size;
	char *speed;
	char time[20] = "?";
	struct timeval t;
	float dt;
	unsigned int left;
	float rate;
	os_error *error;
	int width;
	char *local_status;
	utf8_convert_ret err;

	gettimeofday(&t, 0);
	dt = (t.tv_sec + 0.000001 * t.tv_usec) - (dw->last_time.tv_sec +
			0.000001 * dw->last_time.tv_usec);
	if (dt == 0)
		dt = 0.001;

	total_size = human_friendly_bytesize(max(dw->received, dw->total_size));

	if (dw->ctx) {
		rate = (dw->received - dw->last_received) / dt;
		received = human_friendly_bytesize(dw->received);
		/* A simple 'modified moving average' download rate calculation
		 * to smooth out rate fluctuations: chosen for simplicity.
		 */
		dw->average_points++;
		dw->average_rate =
				((dw->average_points - 1) *
				dw->average_rate + rate) /
				dw->average_points;
		speed = human_friendly_bytesize(dw->average_rate);
		if (dw->total_size) {
			float f;

			if (dw->average_rate > 0) {
				left = (dw->total_size - dw->received) /
						dw->average_rate;
				sprintf(time, "%u:%.2u", left / 60, left % 60);
			}

			/* convert to local encoding */
			err = utf8_to_local_encoding(
				messages_get("Download"), 0, &local_status);
			if (err != UTF8_CONVERT_OK) {
				/* badenc should never happen */
				assert(err != UTF8_CONVERT_BADENC);
				/* hide nomem error */
				snprintf(dw->status, sizeof dw->status,
					messages_get("Download"),
					received, total_size, speed, time);
			}
			else {
				snprintf(dw->status, sizeof dw->status,
					local_status,
					received, total_size, speed, time);
				free(local_status);
			}

			f = (float) dw->received / (float) dw->total_size;
			width = download_progress_width * f;
		} else {
			left = t.tv_sec - dw->start_time.tv_sec;
			sprintf(time, "%u:%.2u", left / 60, left % 60);

			err = utf8_to_local_encoding(
				messages_get("DownloadU"), 0, &local_status);
			if (err != UTF8_CONVERT_OK) {
				/* badenc should never happen */
				assert(err != UTF8_CONVERT_BADENC);
				/* hide nomem error */
				snprintf(dw->status, sizeof dw->status,
					messages_get("DownloadU"),
					received, speed, time);
			}
			else {
				snprintf(dw->status, sizeof dw->status,
					local_status,
					received, speed, time);
				free(local_status);
			}

			/* length unknown, stay at 0 til finished */
			width = 0;
		}
	} else {
		left = dw->last_time.tv_sec - dw->start_time.tv_sec;
		if (left == 0)
			left = 1;
		rate = (float) dw->received / (float) left;
		sprintf(time, "%u:%.2u", left / 60, left % 60);
		speed = human_friendly_bytesize(rate);

		err = utf8_to_local_encoding(messages_get("Downloaded"), 0,
				&local_status);
		if (err != UTF8_CONVERT_OK) {
			/* badenc should never happen */
			assert(err != UTF8_CONVERT_BADENC);
			/* hide nomem error */
			snprintf(dw->status, sizeof dw->status,
				messages_get("Downloaded"),
				total_size, speed, time);
		}
		else {
			snprintf(dw->status, sizeof dw->status, local_status,
					total_size, speed, time);
			free(local_status);
		}

		/* all done */
		width = download_progress_width;
	}

	dw->last_time = t;
	dw->last_received = dw->received;

	error = xwimp_resize_icon(dw->window, ICON_DOWNLOAD_PROGRESS,
			download_progress_x0,
			download_progress_y0,
			download_progress_x0 + width,
			download_progress_y1);
	if (error) {
		LOG(("xwimp_resize_icon: 0x%x: %s",
				error->errnum, error->errmess));
		warn_user("WimpError", error->errmess);
	}

	error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_STATUS, 0, 0);
	if (error) {
		LOG(("xwimp_set_icon_state: 0x%x: %s",
				error->errnum, error->errmess));
		warn_user("WimpError", error->errmess);
	}

	if (dw->ctx)
		schedule(100, ro_gui_download_update_status_wrapper, dw);
	else
		schedule_remove(ro_gui_download_update_status_wrapper, dw);
}
Example #8
0
static struct gui_download_window *
gui_download_window_create(download_context *ctx, struct gui_window *gui)
{
	nsurl *url = download_context_get_url(ctx);
	unsigned long total_size = download_context_get_total_length(ctx);
	gchar *domain;
	gchar *destination;
	gboolean unknown_size = total_size == 0;
	const char *size = (total_size == 0 ?
			    messages_get("gtkUnknownSize") :
			    human_friendly_bytesize(total_size));

	nsgtk_download_parent =
		nsgtk_scaffolding_window(nsgtk_get_scaffold(gui));

	struct gui_download_window *download = malloc(sizeof *download);
	if (download == NULL) {
		return NULL;
	}

	/* set the domain to the host component of the url if it exists */
	if (nsurl_has_component(url, NSURL_HOST)) {
		domain = g_strdup(lwc_string_data(nsurl_get_component(url, NSURL_HOST)));
	} else {
		domain = g_strdup(messages_get("gtkUnknownHost"));
	}
	if (domain == NULL) {
		free(download);
		return NULL;
	}

	/* show the dialog */
	destination = nsgtk_download_dialog_show(
		download_context_get_filename(ctx), domain, size);
	if (destination == NULL) {
		g_free(domain);
		free(download);
		return NULL;
	}

	/* Add the new row and store the reference to it (which keeps track of
	 * the tree changes) */
	gtk_list_store_prepend(nsgtk_download_store, &nsgtk_download_iter);
	download->row = gtk_tree_row_reference_new(
		GTK_TREE_MODEL(nsgtk_download_store),
		gtk_tree_model_get_path(
			GTK_TREE_MODEL(nsgtk_download_store),
			&nsgtk_download_iter));

	download->ctx = ctx;
	download->name = g_string_new(download_context_get_filename(ctx));
	download->time_left = g_string_new("");
	download->size_total = total_size;
	download->size_downloaded = 0;
	download->speed = 0;
	download->start_time = g_timer_elapsed(nsgtk_downloads_timer, NULL);
	download->time_remaining = -1;
	download->status = NSGTK_DOWNLOAD_NONE;
	download->progress = 0;
	download->error = NULL;
	download->write =
		g_io_channel_new_file(destination, "w", &download->error);

	if (nsgtk_download_handle_error(download->error)) {
		g_string_free(download->name, TRUE);
		g_string_free(download->time_left, TRUE);
		free(download);
		return NULL;
	}
	g_io_channel_set_encoding(download->write, NULL, &download->error);

	nsgtk_download_change_sensitivity(download, NSGTK_DOWNLOAD_CANCEL);

	nsgtk_download_store_create_item(download);
	nsgtk_download_show(nsgtk_download_parent);

	if (unknown_size)
		nsgtk_download_change_status(download, NSGTK_DOWNLOAD_WORKING);

	if (nsgtk_downloads_num_active == 0) {
		g_timeout_add(UPDATE_RATE,
			      (GSourceFunc) nsgtk_download_update, FALSE);
	}

	nsgtk_downloads_list = g_list_prepend(nsgtk_downloads_list, download);

	return download;
}