From 95516fe3b598c4d970d45e58009fc088ed6f5cab Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 3 Sep 2007 00:59:34 +0000 Subject: [PATCH] * Renamed pex_{enabled,active} to is_pex_*. * Changed the download info screen to only use commands. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@972 e378c898-3ddf-0310-93e7-cc216c733640 --- src/command_download.cc | 15 +++--- src/command_helpers.h | 3 ++ src/command_ui.cc | 78 ++++++++++++++++++++++++++++++ src/display/text_element_helpers.h | 9 ++++ src/display/text_element_string.cc | 55 +++++++++++++-------- src/display/text_element_string.h | 13 +++-- src/ui/download.cc | 48 +++++++++--------- 7 files changed, 167 insertions(+), 54 deletions(-) diff --git a/src/command_download.cc b/src/command_download.cc index 7bad67a2..c6d436da 100644 --- a/src/command_download.cc +++ b/src/command_download.cc @@ -328,6 +328,9 @@ t_multicall(core::Download* download, const torrent::Object& rawArgs) { ADD_CD_SLOT_PUBLIC("d.get_" key, call_unknown, rpc::get_variable_d_fn(firstKey, secondKey), "i:", ""); \ ADD_CD_SLOT_PUBLIC("d.set_" key, call_string, rpc::set_variable_d_fn(firstKey, secondKey), "i:s", ""); +#define ADD_CD_VALUE(key, get) \ + ADD_CD_SLOT_PUBLIC("d." key, call_unknown, rpc::object_void_fn(get), "i:", "") + #define ADD_CD_VALUE_UNI(key, get) \ ADD_CD_SLOT_PUBLIC("d.get_" key, call_unknown, rpc::object_void_fn(get), "i:", "") @@ -383,11 +386,11 @@ initialize_command_download() { ADD_CD_F_VOID("update_priorities", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::update_priorities))); - ADD_CD_VALUE_UNI("is_open", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_open))); - ADD_CD_VALUE_UNI("is_active", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_active))); - ADD_CD_VALUE_UNI("is_hash_checked", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_hash_checked))); - ADD_CD_VALUE_UNI("is_hash_checking", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_hash_checking))); - ADD_CD_VALUE_UNI("is_multi_file", rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::is_multi_file))); + ADD_CD_VALUE("is_open", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_open))); + ADD_CD_VALUE("is_active", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_active))); + ADD_CD_VALUE("is_hash_checked", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_hash_checked))); + ADD_CD_VALUE("is_hash_checking", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_hash_checking))); + ADD_CD_VALUE("is_multi_file", rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::is_multi_file))); ADD_CD_VARIABLE_STRING_PUBLIC("custom1", "rtorrent", "custom1"); ADD_CD_VARIABLE_STRING_PUBLIC("custom2", "rtorrent", "custom2"); @@ -445,7 +448,7 @@ initialize_command_download() { ADD_CD_VALUE_UNI("peers_complete", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::peers_complete))); ADD_CD_VALUE_UNI("peers_accounted", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::peers_accounted))); - ADD_CD_VALUE_MEM_BI("peer_exchange", &core::Download::download, &torrent::Download::set_pex_enabled, &torrent::Download::pex_enabled); + ADD_CD_VALUE_MEM_BI("peer_exchange", &core::Download::download, &torrent::Download::set_pex_enabled, &torrent::Download::is_pex_enabled); ADD_CD_VALUE_UNI("private", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::is_private))); ADD_CD_VALUE_MEM_UNI("up_rate", &torrent::Download::mutable_up_rate, &torrent::Rate::rate); diff --git a/src/command_helpers.h b/src/command_helpers.h index 7b156bba..4a838807 100644 --- a/src/command_helpers.h +++ b/src/command_helpers.h @@ -144,6 +144,9 @@ add_variable(key, NULL, NULL, &rpc::CommandVariable::get_string, NULL, std::stri #define ADD_COMMAND_VALUE(key, slot) \ ADD_COMMAND_SLOT(key, call_value, slot, "i:i", "") +#define ADD_ANY_VALUE(key, slot) \ + ADD_ANY_SLOT(key, call_value, slot, "i:i", "") + #define ADD_COMMAND_VALUE_UN(key, slot) \ ADD_COMMAND_SLOT(key, call_value, rpc::object_value_fn(slot), "i:i", "") diff --git a/src/command_ui.cc b/src/command_ui.cc index 27918a97..d0b630e2 100644 --- a/src/command_ui.cc +++ b/src/command_ui.cc @@ -124,6 +124,77 @@ apply_cat(rpc::target_type target, const torrent::Object& rawArgs) { return result; } +torrent::Object +apply_to_date(const torrent::Object& rawArgs) { + time_t t = (uint64_t)rawArgs.as_value(); + std::tm *u = std::gmtime(&t); + + if (u == NULL) + return torrent::Object(); + + char buffer[11]; + snprintf(buffer, 11, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); + + return std::string(buffer); +} + +torrent::Object +apply_to_time(const torrent::Object& rawArgs) { + time_t t = (uint64_t)rawArgs.as_value(); + std::tm *u = std::gmtime(&t); + + if (u == NULL) + return torrent::Object(); + + char buffer[9]; + snprintf(buffer, 9, "%2d:%02d:%02d", u->tm_hour, u->tm_min, u->tm_sec); + + return std::string(buffer); +} + +torrent::Object +apply_to_elapsed_time(const torrent::Object& rawArgs) { + uint64_t arg = cachedTime.seconds() - rawArgs.as_value(); + + char buffer[48]; + snprintf(buffer, 48, "%2d:%02d:%02d", (int)(arg / 3600), (int)((arg / 60) % 60), (int)(arg % 60)); + + return std::string(buffer); +} + +torrent::Object +apply_to_kb(const torrent::Object& rawArgs) { + char buffer[32]; + snprintf(buffer, 32, "%5.1f", (double)rawArgs.as_value() / (1 << 10)); + + return std::string(buffer); +} + +torrent::Object +apply_to_mb(const torrent::Object& rawArgs) { + char buffer[32]; + snprintf(buffer, 32, "%8.1f", (double)rawArgs.as_value() / (1 << 20)); + + return std::string(buffer); +} + +torrent::Object +apply_to_xb(const torrent::Object& rawArgs) { + char buffer[48]; + int64_t arg = rawArgs.as_value(); + + if (arg < (int64_t(1000) << 10)) + snprintf(buffer, 48, "%5.1f KB", (double)arg / (int64_t(1) << 10)); + else if (arg < (int64_t(1000) << 20)) + snprintf(buffer, 48, "%5.1f MB", (double)arg / (int64_t(1) << 20)); + else if (arg < (int64_t(1000) << 30)) + snprintf(buffer, 48, "%5.1f GB", (double)arg / (int64_t(1) << 30)); + else + snprintf(buffer, 48, "%5.1f TB", (double)arg / (int64_t(1) << 40)); + + return std::string(buffer); +} + // A series of if/else statements. Every even arguments are // conditionals and odd arguments are branches to be executed, except // the last one which is always a branch. @@ -196,4 +267,11 @@ initialize_command_ui() { ADD_ANY_NONE("print", rak::ptr_fn(&apply_print)); ADD_ANY_NONE("cat", rak::ptr_fn(&apply_cat)); ADD_ANY_NONE("if", rak::ptr_fn(&apply_if)); + + ADD_COMMAND_VALUE("to_date", rak::ptr_fn(&apply_to_date)); + ADD_COMMAND_VALUE("to_time", rak::ptr_fn(&apply_to_time)); + ADD_COMMAND_VALUE("to_elapsed_time", rak::ptr_fn(&apply_to_elapsed_time)); + ADD_COMMAND_VALUE("to_kb", rak::ptr_fn(&apply_to_kb)); + ADD_COMMAND_VALUE("to_mb", rak::ptr_fn(&apply_to_mb)); + ADD_COMMAND_VALUE("to_xb", rak::ptr_fn(&apply_to_xb)); } diff --git a/src/display/text_element_helpers.h b/src/display/text_element_helpers.h index fc88ef86..9d3dacb7 100644 --- a/src/display/text_element_helpers.h +++ b/src/display/text_element_helpers.h @@ -52,6 +52,15 @@ namespace display { namespace helpers { +// All we're ever going to need: + +inline TextElementCommand* +te_command(const char* command, int flags = TextElementCommand::flag_normal, int attributes = Attributes::a_invalid) { + return new TextElementCommand(command, flags, attributes, TextElementCommand::extent_full); +} + +// End All we're ever going to need. + typedef TextElementStringBase string_base; typedef TextElementValueBase value_base; diff --git a/src/display/text_element_string.cc b/src/display/text_element_string.cc index 79e8f04c..acdf6e79 100644 --- a/src/display/text_element_string.cc +++ b/src/display/text_element_string.cc @@ -38,6 +38,7 @@ #include +#include "rpc/parse_commands.h" #include "text_element_string.h" namespace display { @@ -89,33 +90,47 @@ TextElementCString::copy_string(char* first, char* last, rpc::target_type target return first + length; } -// char* -// TextElementCommand::print(char* first, char* last, Canvas::attributes_list* attributes, rpc::target_type target) { -// Attributes baseAttribute = attributes->back(); -// push_attribute(attributes, Attributes(first, m_attributes, Attributes::color_invalid)); +char* +TextElementCommand::print(char* first, char* last, Canvas::attributes_list* attributes, rpc::target_type target) { + Attributes baseAttribute = attributes->back(); + push_attribute(attributes, Attributes(first, m_attributes, Attributes::color_invalid)); -// if (first == last) -// return first; + torrent::Object result = rpc::parse_command(target, m_command, m_commandEnd).first; -// if (m_flags & flag_escape_hex) { -// char buffer[last - first]; -// char* bufferLast = copy_string(buffer, buffer + (last - first), target); + if (first == last) + return first; -// first = rak::transform_hex(buffer, bufferLast, first, last); + switch (result.type()) { + case torrent::Object::TYPE_STRING: + { + const std::string& str = result.as_string(); -// } else if (m_flags & flag_escape_html) { -// char buffer[last - first]; -// char* bufferLast = copy_string(buffer, buffer + (last - first), target); + if (m_flags & flag_escape_hex) { + first = rak::transform_hex(str.c_str(), str.c_str() + str.size(), first, last); -// first = rak::copy_escape_html(buffer, bufferLast, first, last); + } else if (m_flags & flag_escape_html) { + first = rak::copy_escape_html(str.c_str(), str.c_str() + str.size(), first, last); -// } else { -// first = copy_string(first, last, target); -// } + } else { + size_t length = std::min(str.size(), std::distance(first, last)); -// push_attribute(attributes, Attributes(first, baseAttribute)); + std::memcpy(first, str.c_str(), length); + first += std::min(str.size(), length); + } -// return first; -// } + break; + } + case torrent::Object::TYPE_VALUE: + { + first += std::max(snprintf(first, last - first + 1, "%lld", result.as_value()), 0); + break; + } + default: + return first; + } + + push_attribute(attributes, Attributes(first, baseAttribute)); + return first; +} } diff --git a/src/display/text_element_string.h b/src/display/text_element_string.h index 601b06f5..e2a83a71 100644 --- a/src/display/text_element_string.h +++ b/src/display/text_element_string.h @@ -167,7 +167,12 @@ public: static const int flag_fixed_width = (1 << 8); - TextElementCommand(const char* command) : m_command(command) {} + TextElementCommand(const char* command, int flags, int attributes, extent_type length) : + m_flags(flags), + m_attributes(attributes), + m_length(length), + m_command(command), + m_commandEnd(command + std::strlen(command)) {} int flags() const { return m_flags; } void set_flags(int flags) { m_flags = flags; } @@ -177,15 +182,17 @@ public: virtual char* print(char* first, char* last, Canvas::attributes_list* attributes, rpc::target_type target); + virtual extent_type max_length() { return m_length; } + protected: int m_flags; int m_attributes; + extent_type m_length; const char* m_command; + const char* m_commandEnd; }; - - } #endif diff --git a/src/ui/download.cc b/src/ui/download.cc index 8a665197..18e60f36 100644 --- a/src/ui/download.cc +++ b/src/ui/download.cc @@ -147,42 +147,40 @@ Download::create_info() { // Get these bindings with some kind of string map. - element->push_column("Name:", te_variable_string("d.get_name")); - element->push_column("Local id:", te_variable_string("d.get_local_id_html")); - element->push_column("Info hash:", te_variable_string("d.get_hash")); - element->push_column("Created:", te_variable_value("d.get_creation_date", value_base::flag_date), " ", te_variable_value("d.get_creation_date", value_base::flag_time)); + element->push_column("Name:", te_command("d.get_name=")); + element->push_column("Local id:", te_command("d.get_local_id_html=")); + element->push_column("Info hash:", te_command("d.get_hash=")); + element->push_column("Created:", te_command("cat=$to_date=$d.get_creation_date=,\" \",$to_time=$d.get_creation_date=")); element->push_back(""); - element->push_column("Directory:", te_variable_string("d.get_base_path")); - element->push_column("Tied to file:", te_variable_string("d.get_tied_to_file")); - element->push_column("File stats:", - te_branch(&core::Download::c_file_list, &torrent::FileList::is_multi_file, te_string("multi"), te_string("single")), - " ", te_variable_value("d.get_size_files"), " files"); + element->push_column("Directory:", te_command("d.get_base_path=")); + element->push_column("Tied to file:", te_command("d.get_tied_to_file=")); + element->push_column("File stats:", te_command("cat=$if=$d.is_multi_file=\\,multi\\,single,\" \",$d.get_size_files=,\" files\"")); element->push_back(""); - element->push_column("Chunks:", te_variable_value("d.get_completed_chunks"), " / ", te_variable_value("d.get_size_chunks"), " * ", te_variable_value("d.get_chunk_size")); - element->push_column("Priority:", te_variable_value("d.get_priority")); - element->push_column("Peer exchange:", display::text_element_branch(std::mem_fun(&torrent::Download::pex_enabled), te_string("enabled"), te_string("disabled"))); + element->push_column("Chunks:", te_command("cat=$d.get_completed_chunks=,\" / \",$d.get_size_chunks=,\" * \",$d.get_chunk_size=")); + element->push_column("Priority:", te_command("d.get_priority=")); + element->push_column("Peer exchange:", te_command("if=$d.get_peer_exchange=,enabled,disabled")); - element->push_column("State changed:", te_variable_value("d.get_state_changed", value_base::flag_timer | value_base::flag_elapsed)); + element->push_column("State changed:", te_command("to_elapsed_time=$d.get_state_changed=")); element->push_back(""); - element->push_column("Memory usage:", te_variable_value("get_memory_usage", value_base::flag_mb), " MB"); - element->push_column("Max memory usage:", te_variable_value("get_max_memory_usage", value_base::flag_mb), " MB"); - element->push_column("Free diskspace:", te_variable_value("d.get_free_diskspace", value_base::flag_mb), " MB"); - element->push_column("Safe diskspace:", te_variable_value("get_safe_free_diskspace", value_base::flag_mb), " MB"); + element->push_column("Memory usage:", te_command("cat=$to_mb=$get_memory_usage=,\" MB\"")); + element->push_column("Max memory usage:", te_command("cat=$to_mb=$get_max_memory_usage=,\" MB\"")); + element->push_column("Free diskspace:", te_command("cat=$to_mb=$d.get_free_diskspace=,\" MB\"")); + element->push_column("Safe diskspace:", te_command("cat=$to_mb=$get_safe_free_diskspace=,\" MB\"")); element->push_back(""); - element->push_column("Connection type:", te_variable_string("d.get_connection_current")); - element->push_column("Safe sync:", te_branch(&torrent::ChunkManager::safe_sync, torrent::chunk_manager(), te_string("yes"), te_string("no"))); - element->push_column("Send buffer:", te_variable_value("get_send_buffer_size", value_base::flag_kb), " KB"); - element->push_column("Receive buffer:", te_variable_value("get_receive_buffer_size", value_base::flag_kb), " KB"); + element->push_column("Connection type:", te_command("d.get_connection_current=")); + element->push_column("Safe sync:", te_command("if=$get_safe_sync=,yes,no")); + element->push_column("Send buffer:", te_command("cat=$to_mb=$get_send_buffer_size=,\" KB\"")); + element->push_column("Receive buffer:", te_command("cat=$to_mb=$get_receive_buffer_size=,\" KB\"")); element->push_back(""); - element->push_column("Upload:", te_variable_value("d.get_up_rate", value_base::flag_kb), " KB / ", te_variable_value("d.get_up_total", value_base::flag_xb)); - element->push_column("Download:", te_variable_value("d.get_down_rate", value_base::flag_kb), " KB / ", te_variable_value("d.get_down_total", value_base::flag_xb)); - element->push_column("Skipped:", te_variable_value("d.get_skip_rate", value_base::flag_kb), " KB / ", te_variable_value("d.get_skip_total", value_base::flag_xb)); - element->push_column("Preload:", te_variable_value("get_preload_type"), " / ", te_variable_value("get_stats_preloaded"), " / ", te_variable_value("get_stats_not_preloaded")); + element->push_column("Upload:", te_command("cat=$to_kb=$d.get_up_rate=,\" KB / \",$to_xb=$d.get_up_total=")); + element->push_column("Download:", te_command("cat=$to_kb=$d.get_down_rate=,\" KB / \",$to_xb=$d.get_down_total=")); + element->push_column("Skipped:", te_command("cat=$to_kb=$d.get_skip_rate=,\" KB / \",$to_xb=$d.get_skip_total=")); + element->push_column("Preload:", te_command("cat=$get_preload_type=,\" / \",$get_stats_preloaded=,\" / \",$get_stats_not_preloaded=")); element->set_column_width(element->column_width() + 1);