static gssize yahoo_xfer_write(const guchar *buffer, size_t size, PurpleXfer *xfer) { gssize len; struct yahoo_xfer_data *xd = xfer->data; if (!xd) return -1; if (purple_xfer_get_type(xfer) != PURPLE_XFER_SEND) { return -1; } len = write(xfer->fd, buffer, size); if (len == -1) { if (purple_xfer_get_bytes_sent(xfer) >= purple_xfer_get_size(xfer)) purple_xfer_set_completed(xfer, TRUE); if ((errno != EAGAIN) && (errno != EINTR)) return -1; return 0; } if ((purple_xfer_get_bytes_sent(xfer) + len) >= purple_xfer_get_size(xfer)) purple_xfer_set_completed(xfer, TRUE); return len; }
void purple_xfer_set_size(PurpleXfer *xfer, size_t size) { g_return_if_fail(xfer != NULL); xfer->size = size; xfer->bytes_remaining = xfer->size - purple_xfer_get_bytes_sent(xfer); }
static void _qq_update_send_progess(PurpleConnection *gc, guint32 fragment_index) { guint32 mask; guint8 *buffer; gint readbytes; qq_data *qd = (qq_data *) gc->proto_data; PurpleXfer *xfer = qd->xfer; ft_info *info = (ft_info *) xfer->data; purple_debug_info("QQ", "receiving %dth fragment ack, slide window status %o, max_fragment_index %d\n", fragment_index, info->window, info->max_fragment_index); if (fragment_index < info->max_fragment_index || fragment_index >= info->max_fragment_index + sizeof(info->window)) { purple_debug_info("QQ", "duplicate %dth fragment, drop it!\n", fragment_index+1); return; } mask = 0x1 << (fragment_index % sizeof(info->window)); if ((info->window & mask) == 0) { info->window |= mask; if (fragment_index + 1 != info->fragment_num) { xfer->bytes_sent += info->fragment_len; } else { xfer->bytes_sent += purple_xfer_get_size(xfer) % info->fragment_len; } xfer->bytes_remaining = purple_xfer_get_size(xfer) - purple_xfer_get_bytes_sent(xfer); purple_xfer_update_progress(xfer); if (purple_xfer_get_bytes_remaining(xfer) <= 0) { /* We have finished sending the file */ purple_xfer_set_completed(xfer, TRUE); return; } mask = 0x1 << (info->max_fragment_index % sizeof(info->window)); while (info->window & mask) { /* move the slide window */ info->window &= ~mask; buffer = g_newa(guint8, info->fragment_len); readbytes = _qq_xfer_read_file(buffer, info->max_fragment_index + sizeof(info->window), info->fragment_len, xfer); if (readbytes > 0) _qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_DATA_INFO, info->max_fragment_index + sizeof(info->window) + 1, 0, buffer, readbytes); info->max_fragment_index ++; if (mask & 0x8000) mask = 0x0001; else mask = mask << 1; } } purple_debug_info("QQ", "procceed %dth fragment ack, slide window status %o, max_fragment_index %d\n", fragment_index, info->window, info->max_fragment_index); }
double purple_xfer_get_progress(const PurpleXfer *xfer) { g_return_val_if_fail(xfer != NULL, 0.0); if (purple_xfer_get_size(xfer) == 0) return 0.0; return ((double)purple_xfer_get_bytes_sent(xfer) / (double)purple_xfer_get_size(xfer)); }
/* * This function is called whenever data is received. * It sends the acknowledgement (in the form of a total byte count as an * unsigned 4 byte integer in network byte order) */ static void irc_dccsend_recv_ack(PurpleXfer *xfer, const guchar *data, size_t size) { guint32 l; gssize result; l = htonl(purple_xfer_get_bytes_sent(xfer)); result = purple_xfer_write(xfer, (guchar *)&l, sizeof(l)); if (result != sizeof(l)) { purple_debug_error("irc", "unable to send acknowledgement: %s\n", g_strerror(errno)); /* TODO: We should probably close the connection here or something. */ } }
static void update_title_progress(PidginXferDialog *dialog) { gboolean valid; GtkTreeIter iter; int num_active_xfers = 0; guint64 total_bytes_xferred = 0; guint64 total_file_size = 0; if (dialog->window == NULL) return; valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter); /* Find all active transfers */ while (valid) { GValue val; PurpleXfer *xfer = NULL; val.g_type = 0; gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model), &iter, COLUMN_DATA, &val); xfer = g_value_get_pointer(&val); if (purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_STARTED) { num_active_xfers++; total_bytes_xferred += purple_xfer_get_bytes_sent(xfer); total_file_size += purple_xfer_get_size(xfer); } valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter); } /* Update the title */ if (num_active_xfers > 0) { gchar *title; int total_pct = 0; if (total_file_size > 0) { total_pct = 100 * total_bytes_xferred / total_file_size; } title = g_strdup_printf(ngettext("File Transfers - %d%% of %d file", "File Transfers - %d%% of %d files", num_active_xfers), total_pct, num_active_xfers); gtk_window_set_title(GTK_WINDOW(dialog->window), title); g_free(title); } else { gtk_window_set_title(GTK_WINDOW(dialog->window), _("File Transfers")); } }
static void yahoo_process_filetrans_15_reader(PurpleHttpConnection *hc, gchar *buffer, size_t offset, size_t length, gpointer _xfer, PurpleHttpContentReaderCb cb) { PurpleXfer *xfer = _xfer; gssize stored; if ((goffset)offset != purple_xfer_get_bytes_sent(xfer)) { purple_debug_warning("yahoo", "offset != purple_xfer_get_bytes_sent(xfer)\n"); } stored = purple_xfer_read_file(xfer, (guchar*)buffer, length); cb(hc, (stored >= 0), (purple_xfer_get_bytes_remaining(xfer) == 0), stored); }
static void update_title_progress(void) { GList *list; int num_active_xfers = 0; guint64 total_bytes_xferred = 0; guint64 total_file_size = 0; if (xfer_dialog == NULL || xfer_dialog->window == NULL) return; /* Find all active transfers */ for (list = gnt_tree_get_rows(GNT_TREE(xfer_dialog->tree)); list; list = list->next) { PurpleXfer *xfer = (PurpleXfer *)list->data; if (purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_STARTED) { num_active_xfers++; total_bytes_xferred += purple_xfer_get_bytes_sent(xfer); total_file_size += purple_xfer_get_size(xfer); } } /* Update the title */ if (num_active_xfers > 0) { gchar *title; int total_pct = 0; if (total_file_size > 0) { total_pct = 100 * total_bytes_xferred / total_file_size; } title = g_strdup_printf(ngettext("File Transfers - %d%% of %d file", "File Transfers - %d%% of %d files", num_active_xfers), total_pct, num_active_xfers); gnt_screen_rename_widget((xfer_dialog->window), title); g_free(title); } else { gnt_screen_rename_widget((xfer_dialog->window), _("File Transfers")); } }
gssize purple_xfer_write(PurpleXfer *xfer, const guchar *buffer, gsize size) { gssize r, s; g_return_val_if_fail(xfer != NULL, 0); g_return_val_if_fail(buffer != NULL, 0); g_return_val_if_fail(size != 0, 0); s = MIN(purple_xfer_get_bytes_remaining(xfer), size); if (xfer->ops.write != NULL) { r = (xfer->ops.write)(buffer, s, xfer); } else { r = write(xfer->fd, buffer, s); if (r < 0 && errno == EAGAIN) r = 0; if ((purple_xfer_get_bytes_sent(xfer)+r) >= purple_xfer_get_size(xfer)) purple_xfer_set_completed(xfer, TRUE); } return r; }
static gboolean ggp_edisc_xfer_recv_writer(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, const gchar *buffer, size_t offset, size_t length, gpointer _xfer) { PurpleXfer *xfer = _xfer; ggp_edisc_xfer *edisc_xfer; gssize stored; g_return_val_if_fail(xfer != NULL, FALSE); edisc_xfer = purple_xfer_get_protocol_data(xfer); g_return_val_if_fail(edisc_xfer != NULL, FALSE); stored = purple_xfer_write_file(xfer, (guchar *)buffer, length) ? (gssize)length : -1; if (stored < 0 || (gsize)stored != length) { purple_debug_error("gg", "ggp_edisc_xfer_recv_writer: " "saved too less\n"); return FALSE; } if (stored > purple_xfer_get_bytes_remaining(xfer)) { purple_debug_error("gg", "ggp_edisc_xfer_recv_writer: " "saved too much (%" G_GSSIZE_FORMAT " > %" G_GOFFSET_FORMAT ")\n", stored, purple_xfer_get_bytes_remaining(xfer)); return FALSE; } /* May look redundant with ggp_edisc_xfer_progress_watcher, * but it isn't! */ purple_xfer_set_bytes_sent(xfer, purple_xfer_get_bytes_sent(xfer) + stored); return TRUE; }
static void do_transfer(PurpleXfer *xfer) { PurpleXferUiOps *ui_ops; guchar *buffer = NULL; gssize r = 0; ui_ops = purple_xfer_get_ui_ops(xfer); if (xfer->type == PURPLE_XFER_RECEIVE) { r = purple_xfer_read(xfer, &buffer); if (r > 0) { size_t wc; if (ui_ops && ui_ops->ui_write) wc = ui_ops->ui_write(xfer, buffer, r); else wc = fwrite(buffer, 1, r, xfer->dest_fp); if (wc != r) { purple_debug_error("filetransfer", "Unable to write whole buffer.\n"); purple_xfer_cancel_local(xfer); g_free(buffer); return; } if ((purple_xfer_get_size(xfer) > 0) && ((purple_xfer_get_bytes_sent(xfer)+r) >= purple_xfer_get_size(xfer))) purple_xfer_set_completed(xfer, TRUE); } else if(r < 0) { purple_xfer_cancel_remote(xfer); g_free(buffer); return; } } else if (xfer->type == PURPLE_XFER_SEND) { size_t result = 0; size_t s = MIN(purple_xfer_get_bytes_remaining(xfer), xfer->current_buffer_size); PurpleXferPrivData *priv = g_hash_table_lookup(xfers_data, xfer); gboolean read = TRUE; /* this is so the prpl can keep the connection open if it needs to for some odd reason. */ if (s == 0) { if (xfer->watcher) { purple_input_remove(xfer->watcher); xfer->watcher = 0; } return; } if (priv->buffer) { if (priv->buffer->len < s) { s -= priv->buffer->len; read = TRUE; } else { read = FALSE; } } if (read) { if (ui_ops && ui_ops->ui_read) { gssize tmp = ui_ops->ui_read(xfer, &buffer, s); if (tmp == 0) { /* * The UI claimed it was ready, but didn't have any data for * us... It will call purple_xfer_ui_ready when ready, which * sets back up this watcher. */ if (xfer->watcher != 0) { purple_input_remove(xfer->watcher); xfer->watcher = 0; } /* Need to indicate the prpl is still ready... */ priv->ready |= PURPLE_XFER_READY_PRPL; g_return_if_reached(); } else if (tmp < 0) { purple_debug_error("filetransfer", "Unable to read whole buffer.\n"); purple_xfer_cancel_local(xfer); return; } result = tmp; } else { buffer = g_malloc(s); result = fread(buffer, 1, s, xfer->dest_fp); if (result != s) { purple_debug_error("filetransfer", "Unable to read whole buffer.\n"); purple_xfer_cancel_local(xfer); g_free(buffer); return; } } } if (priv->buffer) { g_byte_array_append(priv->buffer, buffer, result); g_free(buffer); buffer = priv->buffer->data; result = priv->buffer->len; } r = purple_xfer_write(xfer, buffer, result); if (r == -1) { purple_xfer_cancel_remote(xfer); if (!priv->buffer) /* We don't free buffer if priv->buffer is set, because in that case buffer doesn't belong to us. */ g_free(buffer); return; } else if (r == result) { /* * We managed to write the entire buffer. This means our * network is fast and our buffer is too small, so make it * bigger. */ purple_xfer_increase_buffer_size(xfer); } else { if (ui_ops && ui_ops->data_not_sent) ui_ops->data_not_sent(xfer, buffer + r, result - r); } if (priv->buffer) { /* * Remove what we wrote * If we wrote the whole buffer the byte array will be empty * Otherwise we'll keep what wasn't sent for next time. */ buffer = NULL; g_byte_array_remove_range(priv->buffer, 0, r); } } if (r > 0) { if (purple_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); if (ui_ops != NULL && ui_ops->update_progress != NULL) ui_ops->update_progress(xfer, purple_xfer_get_progress(xfer)); } if (purple_xfer_is_completed(xfer)) purple_xfer_end(xfer); }
static void do_transfer(PurpleXfer *xfer) { PurpleXferUiOps *ui_ops; guchar *buffer = NULL; gssize r = 0; ui_ops = purple_xfer_get_ui_ops(xfer); if (xfer->type == PURPLE_XFER_RECEIVE) { r = purple_xfer_read(xfer, &buffer); if (r > 0) { size_t wc; if (ui_ops && ui_ops->ui_write) wc = ui_ops->ui_write(xfer, buffer, r); else wc = fwrite(buffer, 1, r, xfer->dest_fp); if (wc != r) { purple_debug_error("filetransfer", "Unable to write whole buffer.\n"); purple_xfer_cancel_local(xfer); g_free(buffer); return; } if ((purple_xfer_get_size(xfer) > 0) && ((purple_xfer_get_bytes_sent(xfer)+r) >= purple_xfer_get_size(xfer))) purple_xfer_set_completed(xfer, TRUE); } else if(r < 0) { purple_xfer_cancel_remote(xfer); g_free(buffer); return; } } else if (xfer->type == PURPLE_XFER_SEND) { size_t result; size_t s = MIN(purple_xfer_get_bytes_remaining(xfer), xfer->current_buffer_size); /* this is so the prpl can keep the connection open if it needs to for some odd reason. */ if (s == 0) { if (xfer->watcher) { purple_input_remove(xfer->watcher); xfer->watcher = 0; } return; } if (ui_ops && ui_ops->ui_read) { gssize tmp = ui_ops->ui_read(xfer, &buffer, s); if (tmp == 0) { /* * UI isn't ready to send data. It will call * purple_xfer_ui_ready when ready, which sets back up this * watcher. */ if (xfer->watcher != 0) { purple_timeout_remove(xfer->watcher); xfer->watcher = 0; } return; } else if (tmp < 0) { purple_debug_error("filetransfer", "Unable to read whole buffer.\n"); purple_xfer_cancel_local(xfer); return; } result = tmp; } else { buffer = g_malloc0(s); result = fread(buffer, 1, s, xfer->dest_fp); if (result != s) { purple_debug_error("filetransfer", "Unable to read whole buffer.\n"); purple_xfer_cancel_local(xfer); g_free(buffer); return; } } /* Write as much as we're allowed to. */ r = purple_xfer_write(xfer, buffer, result); if (r == -1) { purple_xfer_cancel_remote(xfer); g_free(buffer); return; } else if (r < result) { if (ui_ops == NULL || (ui_ops->ui_read == NULL && ui_ops->ui_write == NULL)) { /* We have to seek back in the file now. */ fseek(xfer->dest_fp, r - s, SEEK_CUR); } else { ui_ops->data_not_sent(xfer, buffer + r, result - r); } } else { /* * We managed to write the entire buffer. This means our * network is fast and our buffer is too small, so make it * bigger. */ purple_xfer_increase_buffer_size(xfer); } } if (r > 0) { if (purple_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); if (ui_ops != NULL && ui_ops->update_progress != NULL) ui_ops->update_progress(xfer, purple_xfer_get_progress(xfer)); } if (purple_xfer_is_completed(xfer)) purple_xfer_end(xfer); }
void finch_xfer_dialog_update_xfer(PurpleXfer *xfer) { PurpleGntXferUiData *data; char *size_str, *remaining_str; time_t current_time; char prog_str[5]; double kb_sent; double kbps = 0.0; time_t elapsed, now; char *kbsec; gboolean send; if ((now = purple_xfer_get_end_time(xfer)) == 0) now = time(NULL); kb_sent = purple_xfer_get_bytes_sent(xfer) / 1024.0; elapsed = (purple_xfer_get_start_time(xfer) > 0 ? now - purple_xfer_get_start_time(xfer) : 0); kbps = (elapsed > 0 ? (kb_sent / elapsed) : 0); g_return_if_fail(xfer_dialog != NULL); g_return_if_fail(xfer != NULL); if ((data = FINCHXFER(xfer)) == NULL) return; if (data->in_list == FALSE || data->notified) return; current_time = time(NULL); if (((current_time - data->last_updated_time) == 0) && (!purple_xfer_is_completed(xfer))) { /* Don't update the window more than once per second */ return; } data->last_updated_time = current_time; send = (purple_xfer_get_type(xfer) == PURPLE_XFER_SEND); size_str = purple_str_size_to_units(purple_xfer_get_size(xfer)); remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer)); kbsec = g_strdup_printf(_("%.2f KiB/s"), kbps); gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_PROGRESS, g_ascii_dtostr(prog_str, sizeof(prog_str), purple_xfer_get_progress(xfer) * 100.)); gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_SIZE, size_str); gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_REMAINING, remaining_str); gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_SPEED, kbsec); g_free(size_str); g_free(remaining_str); g_free(kbsec); if (purple_xfer_is_completed(xfer)) { gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_STATUS, send ? _("Sent") : _("Received")); gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_REMAINING, _("Finished")); if (!send) { char *msg = g_strdup_printf(_("The file was saved as %s."), purple_xfer_get_local_filename(xfer)); purple_xfer_conversation_write(xfer, msg, FALSE); g_free(msg); } data->notified = TRUE; } else { gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_STATUS, send ? _("Sending") : _("Receiving")); } update_title_progress(); if (purple_xfer_is_completed(xfer) && xfer_dialog->auto_clear) finch_xfer_dialog_remove_xfer(xfer); }
static gssize yahoo_xfer_read(guchar **buffer, PurpleXfer *xfer) { gchar buf[4096]; gssize len; gchar *start = NULL; gchar *length; gchar *end; int filelen; struct yahoo_xfer_data *xd = xfer->data; if (purple_xfer_get_type(xfer) != PURPLE_XFER_RECEIVE) { return 0; } len = read(xfer->fd, buf, sizeof(buf)); if (len <= 0) { if ((purple_xfer_get_size(xfer) > 0) && (purple_xfer_get_bytes_sent(xfer) >= purple_xfer_get_size(xfer))) { purple_xfer_set_completed(xfer, TRUE); return 0; } else return -1; } if (!xd->started) { xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); memcpy(xd->rxqueue + xd->rxlen, buf, len); xd->rxlen += len; length = g_strstr_len(xd->rxqueue, len, "Content-length:"); /* some proxies re-write this header, changing the capitalization :( * technically that's allowed since headers are case-insensitive * [RFC 2616, section 4.2] */ if (length == NULL) length = g_strstr_len(xd->rxqueue, len, "Content-Length:"); if (length) { end = g_strstr_len(length, length - xd->rxqueue, "\r\n"); if (!end) return 0; if ((filelen = calculate_length(length, len - (length - xd->rxqueue)))) purple_xfer_set_size(xfer, filelen); } start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n"); if (start) start += 4; if (!start || start > (xd->rxqueue + len)) return 0; xd->started = TRUE; len -= (start - xd->rxqueue); *buffer = g_malloc(len); memcpy(*buffer, start, len); g_free(xd->rxqueue); xd->rxqueue = NULL; xd->rxlen = 0; } else { *buffer = g_malloc(len); memcpy(*buffer, buf, len); } return len; }
static void update_detailed_info(PidginXferDialog *dialog, PurpleXfer *xfer) { PidginXferUiData *data; char *kbsec, *time_elapsed, *time_remaining; char *status, *utf8; if (dialog == NULL || xfer == NULL) return; data = PIDGINXFER(xfer); get_xfer_info_strings(xfer, &kbsec, &time_elapsed, &time_remaining); status = g_strdup_printf("%d%% (%" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT " bytes)", (int)(purple_xfer_get_progress(xfer)*100), purple_xfer_get_bytes_sent(xfer), purple_xfer_get_size(xfer)); if (purple_xfer_is_completed(xfer)) { GdkPixbuf *pixbuf = NULL; pixbuf = gtk_widget_render_icon(xfer_dialog->window, PIDGIN_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 (purple_xfer_get_type(xfer) == PURPLE_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), purple_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), purple_account_get_protocol_name(xfer->account)); if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) { gtk_label_set_text(GTK_LABEL(dialog->filename_label), purple_xfer_get_filename(xfer)); } else { char *tmp; tmp = g_path_get_basename(purple_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((purple_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), purple_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(PurpleXfer *xfer, char **kbsec, char **time_elapsed, char **time_remaining) { PidginXferUiData *data; double kb_sent, kb_rem; double kbps = 0.0; time_t elapsed, now; data = PIDGINXFER(xfer); if (xfer->end_time != 0) now = xfer->end_time; else now = time(NULL); kb_sent = purple_xfer_get_bytes_sent(xfer) / 1024.0; kb_rem = purple_xfer_get_bytes_remaining(xfer) / 1024.0; elapsed = (xfer->start_time > 0 ? now - xfer->start_time : 0); kbps = (elapsed > 0 ? (kb_sent / elapsed) : 0); if (kbsec != NULL) { *kbsec = g_strdup_printf(_("%.2f KiB/s"), kbps); } if (time_elapsed != NULL) { int h, m, s; int secs_elapsed; if (xfer->start_time > 0) { secs_elapsed = now - xfer->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); } else { *time_elapsed = g_strdup(_("Not started")); } } if (time_remaining != NULL) { if (purple_xfer_is_completed(xfer)) { *time_remaining = g_strdup(_("Finished")); } else if (purple_xfer_is_canceled(xfer)) { *time_remaining = g_strdup(_("Canceled")); } else if (purple_xfer_get_size(xfer) == 0 || (kb_sent > 0 && kbps == 0)) { *time_remaining = g_strdup(_("Unknown")); } 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); } } }