diff --git a/src/core/download.h b/src/core/download.h index 2229efa2..051541ba 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -57,6 +57,7 @@ public: void release_download(); torrent::Download& get_download() { return m_download; } + const torrent::Download& get_download() const { return m_download; } std::string get_hash() { return m_download.get_hash(); } torrent::Bencode& get_bencode() { return torrent::download_bencode(m_download.get_hash()); } diff --git a/src/display/utils.cc b/src/display/utils.cc index 2f4231c1..e7f9148f 100644 --- a/src/display/utils.cc +++ b/src/display/utils.cc @@ -38,6 +38,7 @@ #include #include +#include #include "core/download.h" #include "utils/timer.h" @@ -46,23 +47,54 @@ namespace display { -std::string -print_download_status(core::Download* d) { - if (d->get_download().is_hash_checking()) { - return "Checking hash"; +char* +print_download_title(char* buf, unsigned int length, core::Download* d) { + return buf + std::max(0, snprintf(buf, length, "%s", + d->get_download().get_name().c_str())); +} - } else if (d->get_download().is_tracker_busy()) { - return "Tracker: Connecting"; +char* +print_download_info(char* buf, unsigned int length, core::Download* d) { + char* last = buf + length; - } else if (!d->get_download().is_active()) { - return "Inactive"; + buf += std::max(0, snprintf(buf, last - buf, "Torrent: ")); - } else if (!d->get_message().empty()) { - return d->get_message(); + if (!d->get_download().is_open()) + buf += std::max(0, snprintf(buf, last - buf, "closed ")); + else if (d->is_done()) + buf += std::max(0, snprintf(buf, last - buf, "done %10.1f MB", + (double)d->get_download().get_bytes_total() / (double)(1 << 20))); + else + buf += std::max(0, snprintf(buf, last - buf, "%6.1f / %6.1f MB", + (double)d->get_download().get_bytes_done() / (double)(1 << 20), + (double)d->get_download().get_bytes_total() / (double)(1 << 20))); + + buf += std::max(0, snprintf(buf, last - buf, " Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", + (double)d->get_download().get_write_rate().rate() / (1 << 10), + (double)d->get_download().get_read_rate().rate() / (1 << 10), + (double)d->get_download().get_write_rate().total() / (1 << 20))); - } else { - return ""; - } + return buf; +} + +char* +print_download_status(char* buf, unsigned int length, core::Download* d) { + if (d->get_download().is_hash_checking()) + buf += std::max(0, snprintf(buf, length, "Checking hash")); + + else if (d->get_download().is_tracker_busy()) + buf += std::max(0, snprintf(buf, length, "Tracker: Connecting")); + + else if (!d->get_download().is_active()) + buf += std::max(0, snprintf(buf, length, "Inactive")); + + else if (!d->get_message().empty()) + buf += std::max(0, snprintf(buf, length, "%s", d->get_message().c_str())); + + else + buf[0] = '\0'; + + return buf; } std::string diff --git a/src/display/utils.h b/src/display/utils.h index 8933a6e0..0293a682 100644 --- a/src/display/utils.h +++ b/src/display/utils.h @@ -50,7 +50,9 @@ namespace utils { namespace display { -std::string print_download_status(core::Download* d); +char* print_download_title(char* buf, unsigned int length, core::Download* d); +char* print_download_info(char* buf, unsigned int length, core::Download* d); +char* print_download_status(char* buf, unsigned int length, core::Download* d); std::string print_hhmmss(utils::Timer t); std::string print_ddmmyyyy(time_t t); diff --git a/src/display/window_download_list.cc b/src/display/window_download_list.cc index 79aafe30..c4704f07 100644 --- a/src/display/window_download_list.cc +++ b/src/display/window_download_list.cc @@ -36,8 +36,6 @@ #include "config.h" -#include - #include "core/download.h" #include "rak/algorithm.h" @@ -64,7 +62,7 @@ WindowDownloadList::redraw() { m_canvas->erase(); - if (m_list->base().empty()) + if (m_list->base().empty() || m_canvas->get_width() < 5) return; typedef std::pair Range; @@ -83,31 +81,18 @@ WindowDownloadList::redraw() { int pos = 0; while (range.first != range.second) { - torrent::Download& d = (*range.first)->get_download(); + char buffer[m_canvas->get_width() - 2]; + char* position; + char* last = buffer + m_canvas->get_width() - 2; - m_canvas->print(0, pos++, "%c %s", - range.first == m_list->get_focus() ? '*' : ' ', - d.get_name().c_str()); - - if ((*range.first)->is_open() && (*range.first)->is_done()) - m_canvas->print(0, pos++, "%c Torrent: Done %10.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", - range.first == m_list->get_focus() ? '*' : ' ', - (double)d.get_bytes_total() / (double)(1 << 20), - (double)d.get_write_rate().rate() / 1024.0, - (double)d.get_read_rate().rate() / 1024.0, - (double)d.get_write_rate().total() / (double)(1 << 20)); - else - m_canvas->print(0, pos++, "%c Torrent: %6.1f / %6.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", - range.first == m_list->get_focus() ? '*' : ' ', - (double)d.get_bytes_done() / (double)(1 << 20), - (double)d.get_bytes_total() / (double)(1 << 20), - (double)d.get_write_rate().rate() / 1024.0, - (double)d.get_read_rate().rate() / 1024.0, - (double)d.get_write_rate().total() / (double)(1 << 20)); + position = print_download_title(buffer, last - buffer, *range.first); + m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer); - m_canvas->print(0, pos++, "%c %s", - range.first == m_list->get_focus() ? '*' : ' ', - print_download_status(*range.first).c_str()); + position = print_download_info(buffer, last - buffer, *range.first); + m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer); + + position = print_download_status(buffer, last - buffer, *range.first); + m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer); ++range.first; } diff --git a/src/display/window_download_statusbar.cc b/src/display/window_download_statusbar.cc index 686f2926..e51b5746 100644 --- a/src/display/window_download_statusbar.cc +++ b/src/display/window_download_statusbar.cc @@ -57,20 +57,27 @@ WindowDownloadStatusbar::redraw() { m_canvas->erase(); - if (m_download->get_download().get_chunks_done() != m_download->get_download().get_chunks_total() || !m_download->get_download().is_open()) - m_canvas->print(0, 0, "Torrent: %.1f / %.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", - (double)m_download->get_download().get_bytes_done() / (double)(1 << 20), - (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), - (double)m_download->get_download().get_write_rate().rate() / 1024.0, - (double)m_download->get_download().get_read_rate().rate() / 1024.0, - (double)m_download->get_download().get_write_rate().total() / (double)(1 << 20)); + char buffer[m_canvas->get_width() - 2]; + char* position; + char* last = buffer + m_canvas->get_width() - 2; + + position = print_download_info(buffer, last - buffer, m_download); + m_canvas->print(0, 0, "%s", buffer); + +// if (m_download->get_download().get_chunks_done() != m_download->get_download().get_chunks_total() || !m_download->get_download().is_open()) +// m_canvas->print(0, 0, "Torrent: %.1f / %.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", +// (double)m_download->get_download().get_bytes_done() / (double)(1 << 20), +// (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), +// (double)m_download->get_download().get_write_rate().rate() / 1024.0, +// (double)m_download->get_download().get_read_rate().rate() / 1024.0, +// (double)m_download->get_download().get_write_rate().total() / (double)(1 << 20)); - else - m_canvas->print(0, 0, "Torrent: Done %.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", - (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), - (double)m_download->get_download().get_write_rate().rate() / 1024.0, - (double)m_download->get_download().get_read_rate().rate() / 1024.0, - (double)m_download->get_download().get_write_rate().total() / (double)(1 << 20)); +// else +// m_canvas->print(0, 0, "Torrent: Done %.1f MiB Rate: %5.1f / %5.1f KiB Uploaded: %.1f MiB", +// (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), +// (double)m_download->get_download().get_write_rate().rate() / 1024.0, +// (double)m_download->get_download().get_read_rate().rate() / 1024.0, +// (double)m_download->get_download().get_write_rate().total() / (double)(1 << 20)); m_canvas->print(0, 1, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i", (int)m_download->get_download().get_peers_connected(), @@ -79,10 +86,11 @@ WindowDownloadStatusbar::redraw() { (int)m_download->get_download().get_peers_max(), (int)m_download->get_download().get_uploads_max()); + position = print_download_status(buffer, last - buffer, m_download); m_canvas->print(0, 2, "[%c:%i] %s", m_download->get_download().is_tracker_busy() ? 'C' : ' ', (int)(m_download->get_download().get_tracker_timeout() / 1000000), - print_download_status(m_download).c_str()); + buffer); } }