size_t gaim_xfer_read(GaimXfer *xfer, char **buffer) { size_t s, r; g_return_val_if_fail(xfer != NULL, 0); g_return_val_if_fail(buffer != NULL, 0); if (gaim_xfer_get_size(xfer) == 0) s = 4096; else s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); if (xfer->ops.read != NULL) r = (xfer->ops.read)(buffer, xfer); else { *buffer = g_malloc0(s); r = read(xfer->fd, *buffer, s); if ((gaim_xfer_get_size(xfer) > 0) && ((gaim_xfer_get_bytes_sent(xfer)+r) >= gaim_xfer_get_size(xfer))) gaim_xfer_set_completed(xfer, TRUE); } return r; }
double gaim_xfer_get_progress(const GaimXfer *xfer) { g_return_val_if_fail(xfer != NULL, 0.0); if (gaim_xfer_get_size(xfer) == 0) return 0.0; return ((double)gaim_xfer_get_bytes_sent(xfer) / (double)gaim_xfer_get_size(xfer)); }
void gaim_gtkxfer_dialog_update_xfer(GaimGtkXferDialog *dialog, GaimXfer *xfer) { GaimGtkXferUiData *data; char *size_str, *remaining_str; GtkTreeSelection *selection; g_return_if_fail(dialog != NULL); g_return_if_fail(xfer != NULL); if ((data = GAIM_GTKXFER(xfer)) == NULL) return; if (data->in_list == FALSE) return; size_str = gaim_str_size_to_units(gaim_xfer_get_size(xfer)); remaining_str = gaim_str_size_to_units(gaim_xfer_get_bytes_remaining(xfer)); gtk_list_store_set(xfer_dialog->model, &data->iter, COLUMN_PROGRESS, gaim_xfer_get_progress(xfer), COLUMN_SIZE, size_str, COLUMN_REMAINING, remaining_str, -1); if (gaim_xfer_is_completed(xfer)) { GdkPixbuf *pixbuf; pixbuf = gtk_widget_render_icon(dialog->window, GAIM_STOCK_FILE_DONE, GTK_ICON_SIZE_MENU, NULL); gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter, COLUMN_STATUS, pixbuf, COLUMN_REMAINING, _("Finished"), -1); g_object_unref(pixbuf); } selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(xfer_dialog->tree)); if (xfer == dialog->selected_xfer) update_detailed_info(xfer_dialog, xfer); if (gaim_xfer_is_completed(xfer) && dialog->auto_clear) gaim_gtkxfer_dialog_remove_xfer(dialog, xfer); else update_buttons(dialog, xfer); }
/* just in case you were wondering, this is why DCC is gay */ static void irc_dccsend_send_read(gpointer data, int source, GaimInputCondition cond) { GaimXfer *xfer = data; struct irc_xfer_send_data *xd = xfer->data; char *buffer[16]; int len; len = read(source, buffer, sizeof(buffer)); if (len < 0 && errno == EAGAIN) return; else if (len <= 0) { /* XXX: Shouldn't this be canceling the transfer? */ gaim_input_remove(xd->inpa); xd->inpa = 0; return; } xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); memcpy(xd->rxqueue + xd->rxlen, buffer, len); xd->rxlen += len; while (1) { size_t acked; if (xd->rxlen < 4) break; acked = ntohl(*((gint32 *)xd->rxqueue)); xd->rxlen -= 4; if (xd->rxlen) { unsigned char *tmp = g_memdup(xd->rxqueue + 4, xd->rxlen); g_free(xd->rxqueue); xd->rxqueue = tmp; } else { g_free(xd->rxqueue); xd->rxqueue = NULL; } if (acked >= gaim_xfer_get_size(xfer)) { gaim_input_remove(xd->inpa); xd->inpa = 0; gaim_xfer_set_completed(xfer, TRUE); gaim_xfer_end(xfer); return; } } }
static void transfer_cb(gpointer data, gint source, GaimInputCondition condition) { GaimXferUiOps *ui_ops; GaimXfer *xfer = (GaimXfer *)data; char *buffer = NULL; size_t r; if (condition & GAIM_INPUT_READ) { r = gaim_xfer_read(xfer, &buffer); if (r > 0) fwrite(buffer, 1, r, xfer->dest_fp); } else { size_t s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); buffer = g_malloc0(s); fread(buffer, 1, s, xfer->dest_fp); /* Write as much as we're allowed to. */ r = gaim_xfer_write(xfer, buffer, s); if (r < s) { /* We have to seek back in the file now. */ fseek(xfer->dest_fp, r - s, SEEK_CUR); } } if (gaim_xfer_get_size(xfer) > 0) xfer->bytes_remaining -= r; xfer->bytes_sent += r; if (xfer->ops.ack != NULL) xfer->ops.ack(xfer, buffer, r); g_free(buffer); ui_ops = gaim_xfer_get_ui_ops(xfer); if (ui_ops != NULL && ui_ops->update_progress != NULL) ui_ops->update_progress(xfer, gaim_xfer_get_progress(xfer)); if (gaim_xfer_is_completed(xfer)) gaim_xfer_end(xfer); }
/* just in case you were wondering, this is why DCC is gay */ static void gaym_dccsend_send_read(gpointer data, int source, GaimInputCondition cond) { GaimXfer *xfer = data; struct gaym_xfer_send_data *xd = xfer->data; char *buffer[16]; int len; if ((len = read(source, buffer, sizeof(buffer))) <= 0) { gaim_input_remove(xd->inpa); xd->inpa = 0; return; } xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); memcpy(xd->rxqueue + xd->rxlen, buffer, len); xd->rxlen += len; while (1) { int acked; if (xd->rxlen < 4) break; acked = ntohl(*((gint32 *)xd->rxqueue)); xd->rxlen -= 4; if (xd->rxlen) { char *tmp = g_memdup(xd->rxqueue + 4, xd->rxlen); g_free(xd->rxqueue); xd->rxqueue = tmp; } else { g_free(xd->rxqueue); xd->rxqueue = NULL; } if (acked >= gaim_xfer_get_size(xfer)) { gaim_input_remove(xd->inpa); xd->inpa = 0; gaim_xfer_set_completed(xfer, TRUE); gaim_xfer_end(xfer); return; } } }
void gaim_xfer_start(GaimXfer *xfer, int fd, const char *ip, unsigned int port) { GaimInputCondition cond; GaimXferType type; g_return_if_fail(xfer != NULL); g_return_if_fail(gaim_xfer_get_type(xfer) != GAIM_XFER_UNKNOWN); type = gaim_xfer_get_type(xfer); xfer->bytes_remaining = gaim_xfer_get_size(xfer); xfer->bytes_sent = 0; if (type == GAIM_XFER_RECEIVE) { cond = GAIM_INPUT_READ; if (ip != NULL) { xfer->remote_ip = g_strdup(ip); xfer->remote_port = port; /* Establish a file descriptor. */ gaim_proxy_connect(xfer->account, xfer->remote_ip, xfer->remote_port, connect_cb, xfer); return; } else { xfer->fd = fd; } } else { cond = GAIM_INPUT_WRITE; xfer->fd = fd; } begin_transfer(xfer, cond); }
void gaim_gtkxfer_dialog_add_xfer(GaimGtkXferDialog *dialog, GaimXfer *xfer) { GaimGtkXferUiData *data; GaimXferType type; GdkPixbuf *pixbuf; char *size_str, *remaining_str; char *lfilename, *utf8; g_return_if_fail(dialog != NULL); g_return_if_fail(xfer != NULL); gaim_xfer_ref(xfer); data = GAIM_GTKXFER(xfer); data->in_list = TRUE; gaim_gtkxfer_dialog_show(dialog); data->start_time = time(NULL); data->end_time = -1; type = gaim_xfer_get_type(xfer); size_str = gaim_str_size_to_units(gaim_xfer_get_size(xfer)); remaining_str = gaim_str_size_to_units(gaim_xfer_get_bytes_remaining(xfer)); pixbuf = gtk_widget_render_icon(dialog->window, (type == GAIM_XFER_RECEIVE ? GAIM_STOCK_DOWNLOAD : GAIM_STOCK_UPLOAD), GTK_ICON_SIZE_MENU, NULL); gtk_list_store_append(dialog->model, &data->iter); lfilename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); utf8 = g_filename_to_utf8(lfilename, -1, NULL, NULL, NULL); g_free(lfilename); lfilename = utf8; gtk_list_store_set(dialog->model, &data->iter, COLUMN_STATUS, pixbuf, COLUMN_PROGRESS, 0.0, COLUMN_FILENAME, (type == GAIM_XFER_RECEIVE) ? gaim_xfer_get_filename(xfer) : lfilename, COLUMN_SIZE, size_str, COLUMN_REMAINING, _("Waiting for transfer to begin"), COLUMN_DATA, xfer, -1); g_free(lfilename); gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog->tree)); g_object_unref(pixbuf); g_free(size_str); g_free(remaining_str); dialog->num_transfers++; ensure_row_selected(dialog); }
static void update_detailed_info(GaimGtkXferDialog *dialog, GaimXfer *xfer) { GaimGtkXferUiData *data; char *kbsec, *time_elapsed, *time_remaining; char *status, *utf8; if (dialog == NULL || xfer == NULL) return; data = GAIM_GTKXFER(xfer); get_xfer_info_strings(xfer, &kbsec, &time_elapsed, &time_remaining); status = g_strdup_printf("%ld of %ld", (unsigned long)gaim_xfer_get_bytes_sent(xfer), (unsigned long)gaim_xfer_get_size(xfer)); if (gaim_xfer_get_size(xfer) >= 0 && gaim_xfer_is_completed(xfer)) { GdkPixbuf *pixbuf = NULL; pixbuf = gtk_widget_render_icon(xfer_dialog->window, GAIM_STOCK_FILE_DONE, GTK_ICON_SIZE_MENU, NULL); gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter, COLUMN_STATUS, pixbuf, -1); g_object_unref(pixbuf); } if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label), _("<b>Receiving As:</b>")); gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label), _("<b>Receiving From:</b>")); } else { gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label), _("<b>Sending To:</b>")); gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label), _("<b>Sending As:</b>")); } gtk_label_set_text(GTK_LABEL(dialog->local_user_label), gaim_account_get_username(xfer->account)); gtk_label_set_text(GTK_LABEL(dialog->remote_user_label), xfer->who); gtk_label_set_text(GTK_LABEL(dialog->protocol_label), gaim_account_get_protocol_name(xfer->account)); if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { gtk_label_set_text(GTK_LABEL(dialog->filename_label), gaim_xfer_get_filename(xfer)); } else { char *tmp; tmp = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); utf8 = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL); g_free(tmp); gtk_label_set_text(GTK_LABEL(dialog->filename_label), utf8); g_free(utf8); } utf8 = g_filename_to_utf8((gaim_xfer_get_local_filename(xfer)), -1, NULL, NULL, NULL); gtk_label_set_text(GTK_LABEL(dialog->localfile_label), utf8); g_free(utf8); gtk_label_set_text(GTK_LABEL(dialog->status_label), status); gtk_label_set_text(GTK_LABEL(dialog->speed_label), kbsec); gtk_label_set_text(GTK_LABEL(dialog->time_elapsed_label), time_elapsed); gtk_label_set_text(GTK_LABEL(dialog->time_remaining_label), time_remaining); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog->progress), gaim_xfer_get_progress(xfer)); g_free(kbsec); g_free(time_elapsed); g_free(time_remaining); g_free(status); }
/************************************************************************** * Utility Functions **************************************************************************/ static void get_xfer_info_strings(GaimXfer *xfer, char **kbsec, char **time_elapsed, char **time_remaining) { GaimGtkXferUiData *data; double kb_sent, kb_rem; double kbps = 0.0; time_t elapsed, now; data = GAIM_GTKXFER(xfer); if (data->end_time == -1 && (gaim_xfer_is_canceled(xfer) || gaim_xfer_is_completed(xfer))) data->end_time = time(NULL); if (data->end_time != -1) now = data->end_time; else now = time(NULL); kb_sent = gaim_xfer_get_bytes_sent(xfer) / 1024.0; kb_rem = gaim_xfer_get_bytes_remaining(xfer) / 1024.0; elapsed = (now - data->start_time); kbps = (elapsed > 0 ? (kb_sent / elapsed) : 0); if (kbsec != NULL) { if (gaim_xfer_is_completed(xfer)) *kbsec = g_strdup(""); else *kbsec = g_strdup_printf(_("%.2f KB/s"), kbps); } if (time_elapsed != NULL) { int h, m, s; int secs_elapsed; secs_elapsed = now - data->start_time; h = secs_elapsed / 3600; m = (secs_elapsed % 3600) / 60; s = secs_elapsed % 60; *time_elapsed = g_strdup_printf("%d:%02d:%02d", h, m, s); } if (time_remaining != NULL) { if (gaim_xfer_get_size(xfer) == 0) { *time_remaining = g_strdup(_("Unknown")); } else if (gaim_xfer_is_completed(xfer)) { *time_remaining = g_strdup(_("Finished")); } else if (gaim_xfer_is_canceled(xfer)) { *time_remaining = g_strdup(_("Canceled")); } else if (kb_sent <= 0) { *time_remaining = g_strdup(_("Waiting for transfer to begin")); } else { int h, m, s; int secs_remaining; secs_remaining = (int)(kb_rem / kbps); h = secs_remaining / 3600; m = (secs_remaining % 3600) / 60; s = secs_remaining % 60; *time_remaining = g_strdup_printf("%d:%02d:%02d", h, m, s); } } }