diff --git a/src/core/download.cc b/src/core/download.cc index 4dabdc70..98772344 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -120,7 +120,6 @@ Download::set_root_directory(const std::string& path) { !file_stat.update(file_list->front()->frozen_path().str()))) { set_message("Cannot change the directory of an open download after the files have been moved."); - rpc::call_command("d.state.set", (int64_t)0, rpc::make_target(this)); control->core()->download_list()->close_directly(this); throw torrent::input_error("Cannot change the directory of an open download after the files have been moved."); diff --git a/src/core/download_list.cc b/src/core/download_list.cc index 3a4b3214..8dfd86d3 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -275,7 +275,38 @@ void DownloadList::close_directly(Download* download) { lt_log_print_info(torrent::LOG_TORRENT_INFO, download->info(), "download_list", "Closing download directly."); + bool was_active = download->download()->info()->is_active(); + bool was_open = download->download()->info()->is_open(); + close_files(download); + set_state_stopped(download); + + if (was_active) { + DL_TRIGGER_EVENT(download, "event.download.paused"); + update_paused_state(download); + } + + if (was_open) { + DL_TRIGGER_EVENT(download, "event.download.hash_removed"); + DL_TRIGGER_EVENT(download, "event.download.closed"); + } +} + +void +DownloadList::set_state_stopped(Download* download) { + control->view_manager()->find_ptr_throw("stopped")->set_visible(download); + rpc::call_command("d.state.set", (int64_t)0, rpc::make_target(download)); +} + +void +DownloadList::update_paused_state(Download* download) { + rpc::call_command("d.state_changed.set", torrent::this_thread::cached_seconds().count(), rpc::make_target(download)); + rpc::call_command("d.state_counter.set", rpc::call_command_value("d.state_counter", rpc::make_target(download)), rpc::make_target(download)); + + // If initial seeding is complete, don't try it again when restarting. + if (download->is_done() && + rpc::call_command("d.connection_current", torrent::Object(), rpc::make_target(download)).as_string() == "initial_seed") + rpc::call_command("d.connection_seed.set", rpc::call_command("d.connection_current", torrent::Object(), rpc::make_target(download)), rpc::make_target(download)); } void @@ -452,15 +483,7 @@ DownloadList::pause(Download* download, int flags) { // view. DL_TRIGGER_EVENT(download, "event.download.paused"); - auto cached_seconds = torrent::this_thread::cached_seconds().count(); - - rpc::call_command("d.state_changed.set", cached_seconds, rpc::make_target(download)); - rpc::call_command("d.state_counter.set", rpc::call_command_value("d.state_counter", rpc::make_target(download)), rpc::make_target(download)); - - // If initial seeding is complete, don't try it again when restarting. - if (download->is_done() && - rpc::call_command("d.connection_current", torrent::Object(), rpc::make_target(download)).as_string() == "initial_seed") - rpc::call_command("d.connection_seed.set", rpc::call_command("d.connection_current", torrent::Object(), rpc::make_target(download)), rpc::make_target(download)); + update_paused_state(download); // Save the state after all the slots, etc have been called so we // include the modifications they may make. diff --git a/src/core/download_list.h b/src/core/download_list.h index f3fe5b5f..c79bbbae 100644 --- a/src/core/download_list.h +++ b/src/core/download_list.h @@ -133,6 +133,9 @@ private: void hash_done(Download* d); void hash_queue(Download* d, int type); + void set_state_stopped(Download* d); + void update_paused_state(Download* d); + inline void check_contains(Download* d); void received_finished(Download* d);