* Added "create_link" and "delete_link" options for creating and

deleting symbolic links to downloads.

* Added "on_*" options for calling commands on state changes.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@849 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-01-20 19:42:22 +00:00
parent b301faf3ca
commit 1d1b5710fa
23 changed files with 416 additions and 200 deletions
+237 -91
View File
@@ -45,6 +45,7 @@
#include <rak/functional.h>
#include <rak/path.h>
#include <rak/string_manip.h>
#include <sigc++/bind.h>
#include <torrent/object.h>
#include <torrent/chunk_manager.h>
#include <torrent/connection_manager.h>
@@ -90,49 +91,49 @@ namespace core {
// }
void
apply_hash_read_ahead(__UNUSED Control* m, int arg) {
apply_hash_read_ahead(int arg) {
torrent::set_hash_read_ahead(arg << 20);
}
void
apply_hash_interval(__UNUSED Control* m, int arg) {
apply_hash_interval(int arg) {
torrent::set_hash_interval(arg * 1000);
}
// The arg string *must* have been checked with validate_port_range
// first.
void
apply_port_range(Control* m, const std::string& arg) {
apply_port_range(const std::string& arg) {
int a = 0, b = 0;
std::sscanf(arg.c_str(), "%i-%i", &a, &b);
m->core()->set_port_range(a, b);
control->core()->set_port_range(a, b);
}
void
apply_load(Control* m, const std::string& arg) {
m->core()->try_create_download_expand(arg, false, false, true);
apply_load(const std::string& arg) {
control->core()->try_create_download_expand(arg, false, false, true);
}
void
apply_load_verbose(Control* m, const std::string& arg) {
m->core()->try_create_download_expand(arg, false, true, true);
apply_load_verbose(const std::string& arg) {
control->core()->try_create_download_expand(arg, false, true, true);
}
void
apply_load_start(Control* m, const std::string& arg) {
m->core()->try_create_download_expand(arg, true, false, true);
apply_load_start(const std::string& arg) {
control->core()->try_create_download_expand(arg, true, false, true);
}
void
apply_load_start_verbose(Control* m, const std::string& arg) {
m->core()->try_create_download_expand(arg, true, true, true);
apply_load_start_verbose(const std::string& arg) {
control->core()->try_create_download_expand(arg, true, true, true);
}
void
apply_start_tied(Control* m) {
for (core::DownloadList::iterator itr = m->core()->download_list()->begin(); itr != m->core()->download_list()->end(); ++itr) {
apply_start_tied() {
for (core::DownloadList::iterator itr = control->core()->download_list()->begin(); itr != control->core()->download_list()->end(); ++itr) {
if ((*itr)->get_value("state") == 1)
continue;
@@ -140,13 +141,13 @@ apply_start_tied(Control* m) {
const std::string& tiedToFile = (*itr)->get_string("tied_to_file");
if (!tiedToFile.empty() && fs.update(rak::path_expand(tiedToFile)))
m->core()->download_list()->start_try(*itr);
control->core()->download_list()->start_try(*itr);
}
}
void
apply_stop_untied(Control* m) {
for (core::DownloadList::iterator itr = m->core()->download_list()->begin(); itr != m->core()->download_list()->end(); ++itr) {
apply_stop_untied() {
for (core::DownloadList::iterator itr = control->core()->download_list()->begin(); itr != control->core()->download_list()->end(); ++itr) {
if ((*itr)->get_value("state") == 0)
continue;
@@ -154,41 +155,41 @@ apply_stop_untied(Control* m) {
const std::string& tiedToFile = (*itr)->get_string("tied_to_file");
if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile)))
m->core()->download_list()->stop_try(*itr);
control->core()->download_list()->stop_try(*itr);
}
}
void
apply_close_untied(Control* m) {
for (core::DownloadList::iterator itr = m->core()->download_list()->begin(); itr != m->core()->download_list()->end(); ++itr) {
apply_close_untied() {
for (core::DownloadList::iterator itr = control->core()->download_list()->begin(); itr != control->core()->download_list()->end(); ++itr) {
rak::file_stat fs;
const std::string& tiedToFile = (*itr)->get_string("tied_to_file");
if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile)) && m->core()->download_list()->stop_try(*itr))
m->core()->download_list()->close(*itr);
if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile)) && control->core()->download_list()->stop_try(*itr))
control->core()->download_list()->close(*itr);
}
}
void
apply_remove_untied(Control* m) {
for (core::DownloadList::iterator itr = m->core()->download_list()->begin(); itr != m->core()->download_list()->end(); ) {
apply_remove_untied() {
for (core::DownloadList::iterator itr = control->core()->download_list()->begin(); itr != control->core()->download_list()->end(); ) {
rak::file_stat fs;
const std::string& tiedToFile = (*itr)->get_string("tied_to_file");
if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile)) && m->core()->download_list()->stop_try(*itr))
itr = m->core()->download_list()->erase(itr);
if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile)) && control->core()->download_list()->stop_try(*itr))
itr = control->core()->download_list()->erase(itr);
else
++itr;
}
}
void
apply_close_low_diskspace(Control* m, int64_t arg) {
core::Manager::DListItr itr = m->core()->download_list()->begin();
apply_close_low_diskspace(int64_t arg) {
core::Manager::DListItr itr = control->core()->download_list()->begin();
while ((itr = std::find_if(itr, m->core()->download_list()->end(), std::mem_fun(&core::Download::is_downloading))) != m->core()->download_list()->end()) {
while ((itr = std::find_if(itr, control->core()->download_list()->end(), std::mem_fun(&core::Download::is_downloading))) != control->core()->download_list()->end()) {
if ((*itr)->file_list()->free_diskspace() < (uint64_t)arg) {
m->core()->download_list()->close(*itr);
control->core()->download_list()->close(*itr);
(*itr)->set_hash_failed(true);
(*itr)->set_message(std::string("Low diskspace."));
@@ -199,7 +200,7 @@ apply_close_low_diskspace(Control* m, int64_t arg) {
}
void
apply_stop_on_ratio(Control* m, const std::string& arg) {
apply_stop_on_ratio(const std::string& arg) {
int64_t minRatio = 0; // first argument: minimum ratio to reach
int64_t minUpload = 0; // second argument: minimum upload amount to reach [optional]
int64_t maxRatio = 0; // third argument: maximum ratio to reach [optional]
@@ -214,14 +215,14 @@ apply_stop_on_ratio(Control* m, const std::string& arg) {
if (++sitr != rak::split_iterator(arg))
utils::Variable::string_to_value_unit(rak::trim(*sitr).c_str(), &maxRatio, 0, 1);
core::Manager::DListItr itr = m->core()->download_list()->begin();
core::Manager::DListItr itr = control->core()->download_list()->begin();
while ((itr = std::find_if(itr, m->core()->download_list()->end(), std::mem_fun(&core::Download::is_seeding))) != m->core()->download_list()->end()) {
while ((itr = std::find_if(itr, control->core()->download_list()->end(), std::mem_fun(&core::Download::is_seeding))) != control->core()->download_list()->end()) {
int64_t totalUpload = (*itr)->download()->up_rate()->total();
int64_t totalDone = (*itr)->download()->bytes_done();
if ((totalUpload >= minUpload && totalUpload * 100 >= totalDone * minRatio) || (maxRatio > 0 && totalUpload * 100 > totalDone * maxRatio)) {
m->core()->download_list()->stop_try(*itr);
control->core()->download_list()->stop_try(*itr);
(*itr)->set("ignore_commands", (int64_t)1);
}
@@ -229,25 +230,24 @@ apply_stop_on_ratio(Control* m, const std::string& arg) {
}
}
// void
// apply_on_finished(const std::string& arg) {
// std::string::const_iterator itr = std::find(arg.begin(), arg.end(), ',');
void
apply_on_state_change(core::DownloadList::slot_map* slotMap, const std::string& arg) {
std::string::const_iterator itr = std::find(arg.begin(), arg.end(), ',');
// std::string key = rak::trim(std::string(arg.begin(), itr));
// std::string value = rak::trim(std::string(itr != arg.end() ? itr + 1 : itr, arg.end()));
std::string key = rak::trim(std::string(arg.begin(), itr));
std::string value = rak::trim(std::string(itr != arg.end() ? itr + 1 : itr, arg.end()));
// if (key.empty())
// throw torrent::input_error("Empty key.");
if (key.empty())
throw torrent::input_error("Empty key.");
// if (value.empty())
// control->core()->download_list()->slot_map_finished().erase("0_finished_" + key);
// else
// control->core()->download_list()->slot_map_finished().insert("0_finished_" + key, );
// ->process_std_single(value);
// }
if (value.empty())
slotMap->erase("1_start_" + key);
else
(*slotMap)["1_start_" + key] = sigc::bind(sigc::mem_fun(control->download_variables(), &utils::VariableMap::process_d_std_single), value);
}
void
apply_encoding_list(__UNUSED Control* m, const std::string& arg) {
apply_encoding_list(const std::string& arg) {
torrent::encoding_list()->push_back(arg);
}
@@ -282,10 +282,10 @@ apply_encryption(const std::string& arg) {
}
void
apply_enable_trackers(Control* m, __UNUSED const std::string& arg) {
apply_enable_trackers(__UNUSED const std::string& arg) {
bool state = (arg != "no");
for (core::Manager::DListItr itr = m->core()->download_list()->begin(), last = m->core()->download_list()->end(); itr != last; ++itr) {
for (core::Manager::DListItr itr = control->core()->download_list()->begin(), last = control->core()->download_list()->end(); itr != last; ++itr) {
torrent::TrackerList tl = (*itr)->download()->tracker_list();
@@ -327,7 +327,7 @@ apply_tos(const std::string& arg) {
}
void
apply_view_filter(Control* control, const std::string& arg) {
apply_view_filter(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
std::string name = rak::trim(*itr);
@@ -348,7 +348,7 @@ apply_view_filter(Control* control, const std::string& arg) {
}
void
apply_view_filter_on(Control* control, const std::string& arg) {
apply_view_filter_on(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
std::string name = rak::trim(*itr);
@@ -369,7 +369,7 @@ apply_view_filter_on(Control* control, const std::string& arg) {
}
void
apply_view_sort(Control* control, const std::string& arg) {
apply_view_sort(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
std::string name = rak::trim(*itr);
@@ -394,7 +394,7 @@ apply_view_sort(Control* control, const std::string& arg) {
}
void
apply_view_sort_current(Control* control, const std::string& arg) {
apply_view_sort_current(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
std::string name = rak::trim(*itr);
@@ -415,7 +415,7 @@ apply_view_sort_current(Control* control, const std::string& arg) {
}
void
apply_view_sort_new(Control* control, const std::string& arg) {
apply_view_sort_new(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
std::string name = rak::trim(*itr);
@@ -448,7 +448,7 @@ apply_try_import(const std::string& path) {
}
void
initialize_option_handler(Control* c) {
initialize_variables() {
utils::VariableMap* variables = control->variable();
variables->insert("check_hash", new utils::VariableBool(true));
@@ -462,14 +462,14 @@ initialize_option_handler(Control* c) {
rak::mem_fn(control->core()->download_store(), &core::DownloadStore::set_path)));
variables->insert("session_lock", new utils::VariableBool(true));
variables->insert("session_on_completion", new utils::VariableBool(true));
variables->insert("session_save", new utils::VariableVoidSlot(rak::mem_fn(c->core()->download_list(), &core::DownloadList::session_save)));
variables->insert("session_save", new utils::VariableVoidSlot(rak::mem_fn(control->core()->download_list(), &core::DownloadList::session_save)));
variables->insert("connection_leech", new utils::VariableAny("leech"));
variables->insert("connection_seed", new utils::VariableAny("seed"));
variables->insert("directory", new utils::VariableAny("./"));
variables->insert("tos", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::ptr_fn(&apply_tos)));
variables->insert("tos", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_tos)));
variables->insert("bind", new utils::VariableStringSlot(rak::mem_fn(control->core(), &core::Manager::bind_address),
rak::mem_fn(control->core(), &core::Manager::set_bind_address)));
@@ -478,8 +478,8 @@ initialize_option_handler(Control* c) {
variables->insert("connection_proxy", new utils::VariableStringSlot(rak::mem_fn(control->core(), &core::Manager::proxy_address),
rak::mem_fn(control->core(), &core::Manager::set_proxy_address)));
variables->insert("http_proxy", new utils::VariableStringSlot(rak::mem_fn(c->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::http_proxy),
rak::mem_fn(c->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::set_http_proxy)));
variables->insert("http_proxy", new utils::VariableStringSlot(rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::http_proxy),
rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::set_http_proxy)));
variables->insert("min_peers", new utils::VariableValue(40));
variables->insert("max_peers", new utils::VariableValue(100));
@@ -499,26 +499,26 @@ initialize_option_handler(Control* c) {
variables->insert("max_open_files", new utils::VariableValueSlot(rak::ptr_fn(&torrent::max_open_files), rak::ptr_fn(&torrent::set_max_open_files)));
variables->insert("max_open_sockets", new utils::VariableValueSlot(rak::mem_fn(torrent::connection_manager(), &torrent::ConnectionManager::max_size),
rak::mem_fn(torrent::connection_manager(), &torrent::ConnectionManager::set_max_size)));
variables->insert("max_open_http", new utils::VariableValueSlot(rak::mem_fn(c->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::max_active),
rak::mem_fn(c->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::set_max_active)));
variables->insert("max_open_http", new utils::VariableValueSlot(rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::max_active),
rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::set_max_active)));
variables->insert("print", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::mem_fn(control->core(), &core::Manager::push_log)));
variables->insert("import", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::ptr_fn(&apply_import)));
variables->insert("try_import", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::ptr_fn(&apply_try_import)));
variables->insert("print", new utils::VariableStringSlot(NULL, rak::mem_fn(control->core(), &core::Manager::push_log)));
variables->insert("import", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_import)));
variables->insert("try_import", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_try_import)));
variables->insert("view_add", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::mem_fn(c->view_manager(), &core::ViewManager::insert_throw)));
variables->insert("view_filter", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_view_filter, c)));
variables->insert("view_filter_on", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_view_filter_on, c)));
variables->insert("view_add", new utils::VariableStringSlot(NULL, rak::mem_fn(control->view_manager(), &core::ViewManager::insert_throw)));
variables->insert("view_filter", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_filter)));
variables->insert("view_filter_on", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_filter_on)));
variables->insert("view_sort", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_view_sort, c)));
variables->insert("view_sort_new", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_view_sort_new, c)));
variables->insert("view_sort_current", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_view_sort_current, c)));
variables->insert("view_sort", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort)));
variables->insert("view_sort_new", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort_new)));
variables->insert("view_sort_current", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort_current)));
variables->insert("key_layout", new utils::VariableAny(std::string("qwerty")));
variables->insert("schedule", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::mem_fn(c->command_scheduler(), &CommandScheduler::parse)));
variables->insert("schedule_remove", new utils::VariableStringSlot(rak::value_fn(std::string()),
rak::mem_fn<const std::string&>(c->command_scheduler(), &CommandScheduler::erase)));
variables->insert("schedule", new utils::VariableStringSlot(NULL, rak::mem_fn(control->command_scheduler(), &CommandScheduler::parse)));
variables->insert("schedule_remove", new utils::VariableStringSlot(NULL,
rak::mem_fn<const std::string&>(control->command_scheduler(), &CommandScheduler::erase)));
variables->insert("download_scheduler", new utils::VariableVoidSlot(rak::mem_fn(control->scheduler(), &core::Scheduler::update)));
@@ -554,33 +554,179 @@ initialize_option_handler(Control* c) {
variables->insert("split_file_size", new utils::VariableValue(-1));
variables->insert("split_suffix", new utils::VariableAny(".part"));
variables->insert("port_range", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_port_range, c)));
variables->insert("port_range", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_port_range)));
variables->insert("hash_read_ahead", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_read_ahead), rak::bind_ptr_fn(&apply_hash_read_ahead, c)));
variables->insert("hash_interval", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_interval), rak::bind_ptr_fn(&apply_hash_interval, c)));
variables->insert("hash_read_ahead", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_read_ahead), rak::ptr_fn(&apply_hash_read_ahead)));
variables->insert("hash_interval", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_interval), rak::ptr_fn(&apply_hash_interval)));
variables->insert("umask", new utils::VariableValueSlot(rak::mem_fn(control, &Control::umask), rak::mem_fn(control, &Control::set_umask), 8));
variables->insert("working_directory", new utils::VariableStringSlot(rak::mem_fn(control, &Control::working_directory),
rak::mem_fn(control, &Control::set_working_directory)));
variables->insert("load", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_load, c)));
variables->insert("load_verbose", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_load_verbose, c)));
variables->insert("load_start", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_load_start, c)));
variables->insert("load_start_verbose", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_load_start_verbose, c)));
variables->insert("load", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_load)));
variables->insert("load_verbose", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_load_verbose)));
variables->insert("load_start", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_load_start)));
variables->insert("load_start_verbose", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_load_start_verbose)));
variables->insert("start_tied", new utils::VariableVoidSlot(rak::bind_ptr_fn(&apply_start_tied, c)));
variables->insert("stop_untied", new utils::VariableVoidSlot(rak::bind_ptr_fn(&apply_stop_untied, c)));
variables->insert("close_untied", new utils::VariableVoidSlot(rak::bind_ptr_fn(&apply_close_untied, c)));
variables->insert("remove_untied", new utils::VariableVoidSlot(rak::bind_ptr_fn(&apply_remove_untied, c)));
variables->insert("start_tied", new utils::VariableVoidSlot(rak::ptr_fn(&apply_start_tied)));
variables->insert("stop_untied", new utils::VariableVoidSlot(rak::ptr_fn(&apply_stop_untied)));
variables->insert("close_untied", new utils::VariableVoidSlot(rak::ptr_fn(&apply_close_untied)));
variables->insert("remove_untied", new utils::VariableVoidSlot(rak::ptr_fn(&apply_remove_untied)));
variables->insert("close_low_diskspace", new utils::VariableValueSlot(rak::value_fn(int64_t()), rak::bind_ptr_fn(&apply_close_low_diskspace, c)));
variables->insert("stop_on_ratio", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_stop_on_ratio, c)));
variables->insert("close_low_diskspace", new utils::VariableValueSlot(rak::value_fn(int64_t()), rak::ptr_fn(&apply_close_low_diskspace)));
variables->insert("stop_on_ratio", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_stop_on_ratio)));
// variables->insert("on_finished", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::ptr_fn(&apply_on_finished)));
variables->insert("on_insert", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_insert())));
variables->insert("on_erase", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_erase())));
variables->insert("on_open", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_open())));
variables->insert("on_close", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_close())));
variables->insert("on_start", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_start())));
variables->insert("on_stop", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_stop())));
variables->insert("on_hash_queued", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_queued())));
variables->insert("on_hash_removed", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_removed())));
variables->insert("on_hash_done", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_done())));
variables->insert("on_finished", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_finished())));
variables->insert("enable_trackers", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_enable_trackers, c)));
variables->insert("encoding_list", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::bind_ptr_fn(&apply_encoding_list, c)));
variables->insert("enable_trackers", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_enable_trackers)));
variables->insert("encoding_list", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_encoding_list)));
variables->insert("encryption", new utils::VariableStringSlot(rak::value_fn(std::string()), rak::ptr_fn(&apply_encryption)));
variables->insert("encryption", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_encryption)));
variables->insert("handshake_log", new utils::VariableBool(false));
}
template <typename Target, typename GetFunc, typename SetFunc>
utils::Variable*
var_d_value(Target target, GetFunc getFunc, SetFunc setFunc) {
return new utils::VariableDownloadValueSlot(rak::ftor_fn1(rak::on(std::mem_fun(target), std::mem_fun(getFunc))),
rak::ftor_fn2(rak::on2(std::mem_fun(target), std::mem_fun(setFunc))));
}
template <typename Target, typename GetFunc, typename SetFunc>
utils::Variable*
var_d2_value(Target target, GetFunc getFunc, SetFunc setFunc) {
return new utils::VariableDownloadValueSlot(rak::ftor_fn1(rak::on(rak::on(std::mem_fun(&core::Download::download), std::mem_fun(target)), std::mem_fun(getFunc))),
rak::ftor_fn2(rak::on2(rak::on(std::mem_fun(&core::Download::download), std::mem_fun(target)), std::mem_fun(setFunc))));
}
template <typename Target, typename GetFunc>
utils::Variable*
var_d2_get_value(Target target, GetFunc getFunc) {
return new utils::VariableDownloadValueSlot(rak::ftor_fn1(rak::on(rak::on(std::mem_fun(&core::Download::download), std::mem_fun(target)), std::mem_fun(getFunc))), NULL);
}
void
apply_d_create_link(core::Download* download, const std::string& args) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(args, ',');
std::string type = rak::trim(*itr); ++itr;
std::string prefix = rak::trim(*itr); ++itr;
std::string postfix = rak::trim(*itr); ++itr;
if (type.empty())
throw torrent::input_error("Invalid arguments.");
std::string target;
std::string link;
if (type == "tied") {
target = rak::path_expand(download->get_string("tied_to_file"));
link = rak::path_expand(prefix + target.substr(target.rfind('/') != std::string::npos ? target.rfind('/') + 1 : 0) + postfix);
} else {
throw torrent::input_error("Unknown type argument.");
}
if (target.empty())
return;
if (symlink(target.c_str(), link.c_str()) == -1)
; // control->core()->push_log("create_link failed: " + std::string(rak::error_number::current().c_str()));
}
void
apply_d_delete_link(core::Download* download, const std::string& args) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(args, ',');
std::string type = rak::trim(*itr); ++itr;
std::string prefix = rak::trim(*itr); ++itr;
std::string postfix = rak::trim(*itr); ++itr;
if (type.empty())
throw torrent::input_error("Invalid arguments.");
std::string target;
std::string link;
if (type == "tied") {
target = rak::path_expand(download->get_string("tied_to_file"));
link = rak::path_expand(prefix + target.substr(target.rfind('/') != std::string::npos ? target.rfind('/') + 1 : 0) + postfix);
} else {
throw torrent::input_error("Unknown type argument.");
}
if (target.empty())
return;
rak::file_stat fileStat;
rak::error_number::clear_global();
if (!fileStat.update_link(link) || !fileStat.is_link() ||
unlink(link.c_str()) == -1)
; // control->core()->push_log("delete_link failed: " + std::string(rak::error_number::current().c_str()));
}
void
initialize_download_variables() {
utils::VariableMap* variables = control->download_variables();
variables->insert("connection_current", new utils::VariableDownloadStringSlot(rak::ftor_fn1(std::mem_fun(&core::Download::connection_current)),
rak::ftor_fn2(std::mem_fun(&core::Download::set_connection_current))));
variables->insert("connection_leech", new utils::VariableAny(core::Download::connection_type_to_string(torrent::Download::CONNECTION_LEECH)));
variables->insert("connection_seed", new utils::VariableAny(core::Download::connection_type_to_string(torrent::Download::CONNECTION_SEED)));
// 0 - stopped
// 1 - started
variables->insert("state", new utils::VariableObject("rtorrent", "state", torrent::Object::TYPE_VALUE));
variables->insert("complete", new utils::VariableObject("rtorrent", "complete", torrent::Object::TYPE_VALUE));
// 0 - Not hashing
// 1 - Normal hashing
// 2 - Download finished, hashing
variables->insert("hashing", new utils::VariableObject("rtorrent", "hashing", torrent::Object::TYPE_VALUE));
variables->insert("tied_to_file", new utils::VariableObject("rtorrent", "tied_to_file", torrent::Object::TYPE_STRING));
// The "state_changed" variable is required to be a valid unix time
// value, it indicates the last time the torrent changed its state,
// resume/pause.
variables->insert("state_changed", new utils::VariableObject("rtorrent", "state_changed", torrent::Object::TYPE_VALUE));
variables->insert("directory", new utils::VariableDownloadStringSlot(rak::ftor_fn1(rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::root_dir))),
rak::ftor_fn2(std::mem_fun(&core::Download::set_root_directory))));
variables->insert("min_peers", var_d_value(&core::Download::download, &torrent::Download::peers_min, &torrent::Download::set_peers_min));
variables->insert("max_peers", var_d_value(&core::Download::download, &torrent::Download::peers_max, &torrent::Download::set_peers_max));
variables->insert("max_uploads", var_d_value(&core::Download::download, &torrent::Download::uploads_max, &torrent::Download::set_uploads_max));
variables->insert("max_file_size", var_d_value(&core::Download::file_list, &torrent::FileList::max_file_size, &torrent::FileList::set_max_file_size));
// variables->insert("split_file_size", new utils::VariableValueSlot(rak::mem_fn(file_list(), &torrent::FileList::split_file_size),
// rak::mem_fn(file_list(), &torrent::FileList::set_split_file_size)));
// variables->insert("split_suffix", new utils::VariableStringSlot(rak::mem_fn(file_list(), &torrent::FileList::split_suffix),
// rak::mem_fn(file_list(), &torrent::FileList::set_split_suffix)));
variables->insert("up_rate", var_d2_get_value(&torrent::Download::mutable_up_rate, &torrent::Rate::rate));
variables->insert("up_total", var_d2_get_value(&torrent::Download::mutable_up_rate, &torrent::Rate::total));
variables->insert("down_rate", var_d2_get_value(&torrent::Download::mutable_down_rate, &torrent::Rate::rate));
variables->insert("down_total", var_d2_get_value(&torrent::Download::mutable_down_rate, &torrent::Rate::total));
variables->insert("skip_rate", var_d2_get_value(&torrent::Download::mutable_skip_rate, &torrent::Rate::rate));
variables->insert("skip_total", var_d2_get_value(&torrent::Download::mutable_skip_rate, &torrent::Rate::total));
variables->insert("priority", new utils::VariableDownloadValueSlot(rak::ftor_fn1(std::mem_fun(&core::Download::priority)), rak::ftor_fn2(std::mem_fun(&core::Download::set_priority))));
variables->insert("tracker_numwant", var_d_value(&core::Download::tracker_list, &torrent::TrackerList::numwant, &torrent::TrackerList::set_numwant));
variables->insert("ignore_commands", new utils::VariableObject("rtorrent", "ignore_commands", torrent::Object::TYPE_VALUE));
// Hmm... do we need dupicates?
variables->insert("print", new utils::VariableStringSlot(NULL, rak::mem_fn(control->core(), &core::Manager::push_log)));
variables->insert("create_link", new utils::VariableDownloadStringSlot(NULL, rak::ptr_fn(&apply_d_create_link)));
variables->insert("delete_link", new utils::VariableDownloadStringSlot(NULL, rak::ptr_fn(&apply_d_delete_link)));
}