diff --git a/src/core/Makefile.am b/src/core/Makefile.am index f380edf1..aeec64d9 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -11,6 +11,8 @@ libsub_core_a_SOURCES = \ download.h \ download_list.cc \ download_list.h \ + manager.cc \ + manager.h \ poll.cc \ poll.h diff --git a/src/core/manager.cc b/src/core/manager.cc new file mode 100644 index 00000000..c9716943 --- /dev/null +++ b/src/core/manager.cc @@ -0,0 +1,63 @@ +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "manager.h" + + +namespace core { + +void +Manager::insert(const std::string& uri) { + if (std::strncmp(uri.c_str(), "http://", 7)) + create_file(uri); + else + create_http(uri); +} + +void +Manager::create_file(const std::string& uri) { + DownloadList::iterator itr = m_downloadList.end(); + + try { + std::fstream f(uri.c_str(), std::ios::in); + + itr = m_downloadList.insert(&f); + + itr->open(); + itr->hash_check(); + + } catch (torrent::local_error& e) { + // What to do? Keep in list for now. + } +} + +void +Manager::create_http(const std::string& uri) { + core::HttpQueue::iterator itr = m_httpQueue.insert(uri); + + (*itr)->signal_done().slots().push_front(sigc::bind(sigc::mem_fun(*this, &core::Manager::receive_http_done), *itr)); + // Add the failed signal here. +} + +void +Manager::receive_http_done(torrent::Http* http) { + DownloadList::iterator itr = m_downloadList.end(); + + try { + itr = m_downloadList.insert(http->get_stream()); + + itr->open(); + itr->hash_check(); + + } catch (torrent::local_error& e) { + // What to do? Keep in list for now. + } +} + +} diff --git a/src/core/manager.h b/src/core/manager.h new file mode 100644 index 00000000..c483f903 --- /dev/null +++ b/src/core/manager.h @@ -0,0 +1,31 @@ +#ifndef RTORRENT_CORE_MANAGER_H +#define RTORRENT_CORE_MANAGER_H + +#include "download_list.h" +#include "http_queue.h" + +namespace core { + +class Manager { +public: + typedef sigc::slot1 SlotReady; + typedef sigc::slot0 SlotFailed; + + DownloadList& get_download_list() { return m_downloadList; } + HttpQueue& get_http_queue() { return m_httpQueue; } + + void insert(const std::string& uri); + +private: + void receive_http_done(torrent::Http* http); + + void create_file(const std::string& uri); + void create_http(const std::string& uri); + + DownloadList m_downloadList; + HttpQueue m_httpQueue; +}; + +} + +#endif diff --git a/src/main.cc b/src/main.cc index 39f32bc1..d2d7708c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -17,8 +17,7 @@ #include "core/poll.h" #include "core/curl_stack.h" -#include "core/http_queue.h" -#include "core/download_list.h" +#include "core/manager.h" #include "ui/control.h" #include "ui/download_list.h" @@ -34,8 +33,7 @@ bool start_shutdown = false; bool is_shutting_down = false; core::Poll poll; -core::DownloadList downloads; -core::HttpQueue httpQueue; +core::Manager coreManager; bool is_resized() { @@ -65,7 +63,10 @@ do_shutdown() { torrent::listen_close(); - std::for_each(downloads.begin(), downloads.end(), std::mem_fun_ref(&core::Download::stop)); + // TODO: Set display to a safe mode. + + std::for_each(coreManager.get_download_list().begin(), coreManager.get_download_list().end(), + std::mem_fun_ref(&core::Download::stop)); } void @@ -106,7 +107,7 @@ main(int argc, char** argv) { core::CurlStack::init(); ui::Control uiControl; - ui::DownloadList uiDownloadList(&downloads, &uiControl); + ui::DownloadList uiDownloadList(&coreManager.get_download_list(), &uiControl); uiDownloadList.activate(); @@ -120,26 +121,13 @@ main(int argc, char** argv) { poll.slot_select_interrupted(sigc::ptr_fun(display::Canvas::do_update)); torrent::Http::set_factory(poll.get_http_factory()); - httpQueue.slot_factory(poll.get_http_factory()); + coreManager.get_http_queue().slot_factory(poll.get_http_factory()); torrent::initialize(); torrent::listen_open(6880, 6999); - for (int i = 1; i < argc; ++i) { - if (std::strncmp(argv[i], "http://", 7)) { - std::fstream f(argv[i], std::ios::in); - - core::DownloadList::iterator itr = downloads.insert(&f); - - itr->open(); - itr->hash_check(); - - } else { - core::HttpQueue::iterator itr = httpQueue.insert(argv[i]); - - (*itr)->signal_done().slots().push_front(sigc::hide_return(sigc::bind(sigc::mem_fun(downloads, &core::DownloadList::insert), (*itr)->get_stream()))); - } - } + for (int i = 1; i < argc; ++i) + coreManager.insert(argv[i]); uiControl.get_display().adjust_layout();