diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index b2acf706..ed3205cd 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -140,7 +140,7 @@ DownloadFactory::receive_commit() { void DownloadFactory::receive_success() { if (m_stream == NULL) - throw torrent::client_error("DownloadFactory::receive_success() called on an object with m_stream == NULL"); + throw torrent::client_error("DownloadFactory::receive_success() called on an object with m_stream == NULL."); Download* download = m_manager->download_list()->create(m_stream, m_printLog); @@ -167,14 +167,6 @@ DownloadFactory::receive_success() { initialize_rtorrent(download, rtorrent); - if (m_manager->download_list()->insert(download) == m_manager->download_list()->end()) { - // ATM doesn't really ever get here. - delete download; - - m_slotFinished(); - return; - } - // Move to 'rtorrent'. download->variable()->set("connection_leech", m_variables.get("connection_leech")); download->variable()->set("connection_seed", m_variables.get("connection_seed")); @@ -185,12 +177,25 @@ DownloadFactory::receive_success() { if (!control->variable()->get_value("use_udp_trackers")) download->enable_udp_trackers(false); - if (m_session) { - if (!rtorrent->has_key_string("directory")) - download->variable()->set("directory", m_variables.get("directory")); - else - download->variable()->set("directory", rtorrent->get_key("directory")); + if (!rtorrent->has_key_string("directory")) + download->variable()->set("directory", m_variables.get("directory")); + else + download->variable()->set("directory", rtorrent->get_key("directory")); + if (!m_session && m_variables.get("tied_to_file").as_value()) + download->variable()->set("tied_to_file", m_uri); + + // The action of inserting might cause the torrent to be + // opened/started or such. Figure out a nicer way of handling this. + if (m_manager->download_list()->insert(download) == m_manager->download_list()->end()) { + // ATM doesn't really ever get here. + delete download; + + m_slotFinished(); + return; + } + + if (m_session) { // This torrent was queued for hashing or hashing when the session // file was saved. Or it was in a started state. if (download->variable()->get_value("hashing") != Download::variable_hashing_stopped || @@ -198,11 +203,6 @@ DownloadFactory::receive_success() { m_manager->download_list()->resume(download); } else { - download->variable()->set("directory", m_variables.get("directory")); - - if (m_variables.get("tied_to_file").as_value()) - download->variable()->set("tied_to_file", m_uri); - // Use the state thingie here, move below. if (m_start) m_manager->download_list()->start(download); diff --git a/src/core/download_list.cc b/src/core/download_list.cc index c8006c51..6293419d 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -371,6 +371,9 @@ void DownloadList::hash_done(Download* download) { check_contains(download); + if (!download->is_open()) + throw torrent::client_error("DownloadList::hash_done(...) !download->is_open()."); + if (!download->is_hash_checked() || download->is_hash_checking() || download->is_active()) throw torrent::client_error("DownloadList::hash_done(...) download in invalid state."); diff --git a/src/core/view_manager.cc b/src/core/view_manager.cc index bcbe51d4..1665baef 100644 --- a/src/core/view_manager.cc +++ b/src/core/view_manager.cc @@ -83,7 +83,7 @@ private: class ViewSortVariableValue : public ViewSort { public: ViewSortVariableValue(const std::string& name, bool reverse = false) : - m_name(name) {} + m_name(name), m_reverse(reverse) {} virtual bool operator () (Download* d1, Download* d2) const { if (m_reverse) diff --git a/src/option_parser.cc b/src/option_parser.cc index 67c31143..bee6bbd4 100644 --- a/src/option_parser.cc +++ b/src/option_parser.cc @@ -36,7 +36,10 @@ #include "config.h" +#include +#include #include +#include #include #include #include @@ -88,17 +91,9 @@ OptionParser::process(int argc, char** argv) { bool OptionParser::has_flag(char flag, int argc, char** argv) { - int result; - char options[2] = { flag, '\0' }; + char options[3] = { '-', flag, '\0' }; - optind = 0; - opterr = 0; - - while ((result = getopt(argc, argv, options)) != -1) - if (result == flag) - return true; - - return false; + return std::find_if(argv, argv + argc, std::not1(std::bind1st(std::ptr_fun(&std::strcmp), options))) != argv + argc; } std::string