diff --git a/src/core/download_store.cc b/src/core/download_store.cc index b9248309..cd31d037 100644 --- a/src/core/download_store.cc +++ b/src/core/download_store.cc @@ -53,6 +53,32 @@ DownloadStore::remove(Download* d) { unlink(create_filename(d).c_str()); } +utils::Directory +DownloadStore::get_formated_entries() { + if (!is_active()) + return utils::Directory(); + + utils::Directory d(m_path); + d.update(); + + d.erase(std::remove_if(d.begin(), d.end(), std::not1(std::ptr_fun(&DownloadStore::is_correct_format))), d.end()); + + return d; +} + +bool +DownloadStore::is_correct_format(std::string f) { + if (f.size() != 48 || f.substr(40) != ".torrent") + return false; + + for (std::string::const_iterator itr = f.begin(); itr != f.end() - 8; ++itr) + if (!(*itr >= '0' && *itr <= '9') && + !(*itr >= 'A' && *itr <= 'F')) + return false; + + return true; +} + std::string DownloadStore::create_filename(Download* d) { return m_path + utils::string_to_hex(d->get_hash()) + ".torrent"; diff --git a/src/core/download_store.h b/src/core/download_store.h index f8a01e5e..08ecdc8a 100644 --- a/src/core/download_store.h +++ b/src/core/download_store.h @@ -3,25 +3,31 @@ #include +#include "utils/directory.h" + namespace core { class Download; class DownloadStore { public: - - void activate(const std::string& path); - void disable(); - bool is_active() { return !m_path.empty(); } + void activate(const std::string& path); + void disable(); - void save(Download* d); - void remove(Download* d); + bool is_active() { return !m_path.empty(); } + + void save(Download* d); + void remove(Download* d); + + // Currently shows all entries in the correct format. + utils::Directory get_formated_entries(); private: - std::string create_filename(Download* d); + static bool is_correct_format(std::string f); + std::string create_filename(Download* d); - std::string m_path; + std::string m_path; }; } diff --git a/src/core/manager.cc b/src/core/manager.cc index faae5be7..8bcd8350 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -32,7 +32,7 @@ Manager::cleanup() { } void -Manager::insert(const std::string& uri) { +Manager::insert(std::string uri) { if (std::strncmp(uri.c_str(), "http://", 7)) create_file(uri); else diff --git a/src/core/manager.h b/src/core/manager.h index 1c5b9198..d7c4e9a5 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -27,7 +27,7 @@ public: void initialize(); void cleanup(); - void insert(const std::string& uri); + void insert(std::string uri); iterator erase(DownloadList::iterator itr); void start(Download* d); diff --git a/src/main.cc b/src/main.cc index 537de0cf..04323229 100644 --- a/src/main.cc +++ b/src/main.cc @@ -85,6 +85,19 @@ test_dir(const std::string& s) { exit(0); } +void +load_session_torrents(ui::Control* c) { + // Load session torrents. + std::list l = c->get_core().get_download_store().get_formated_entries().make_list(); + + std::for_each(l.begin(), l.end(), std::bind1st(std::mem_fun(&core::Manager::insert), &c->get_core())); +} + +void +load_arg_torrents(ui::Control* c, char** begin, char** end) { + std::for_each(begin, end, std::bind1st(std::mem_fun(&core::Manager::insert), &c->get_core())); +} + int main(int argc, char** argv) { ui::Control uiControl; @@ -122,8 +135,8 @@ main(int argc, char** argv) { uiControl.get_core().initialize(); - while (firstArg < argc) - uiControl.get_core().insert(argv[firstArg++]); + load_session_torrents(&uiControl); + load_arg_torrents(&uiControl, argv + firstArg, argv + argc); uiControl.get_display().adjust_layout(); diff --git a/src/utils/directory.cc b/src/utils/directory.cc index 58720677..eb5ca593 100644 --- a/src/utils/directory.cc +++ b/src/utils/directory.cc @@ -5,6 +5,7 @@ #include #include +#include "functional.h" #include "directory.h" namespace utils { @@ -28,7 +29,17 @@ Directory::update() { Base::push_back(ent->d_name); } - std::sort(begin(), end(), std::less()); + Base::sort(std::less()); +} + +Directory::Base +Directory::make_list() { + Base l; + + for (Base::iterator itr = begin(); itr != end(); ++itr) + l.push_back(m_path + *itr); + + return l; } } diff --git a/src/utils/directory.h b/src/utils/directory.h index c5ea2f22..1ce6ee63 100644 --- a/src/utils/directory.h +++ b/src/utils/directory.h @@ -2,13 +2,13 @@ #define RTORRENT_UTILS_DIRECTORY_H #include -#include +#include namespace utils { -class Directory : private std::vector { +class Directory : private std::list { public: - typedef std::vector Base; + typedef std::list Base; using Base::iterator; using Base::const_iterator; @@ -23,6 +23,8 @@ public: using Base::empty; using Base::size; + using Base::erase; + Directory() {} Directory(const std::string& path) : m_path(path) {} @@ -30,6 +32,9 @@ public: const std::string& get_path() { return m_path; } + // Make a list with full path names. + Base make_list(); + private: std::string m_path; }; diff --git a/src/utils/functional.h b/src/utils/functional.h index 6dd7986e..e44f4f75 100644 --- a/src/utils/functional.h +++ b/src/utils/functional.h @@ -52,9 +52,9 @@ equal(Type t, Ftor f) { return _equal(t, f); } -template +template struct _on : public std::unary_function { - _on(Dest d, Src s) : m_dest(d), m_src(s) {} + _on(Src s, Dest d) : m_dest(d), m_src(s) {} typename Dest::result_type operator () (typename Src::argument_type arg) { return m_dest(m_src(arg)); @@ -64,10 +64,10 @@ struct _on : public std::unary_function -inline _on -on(Dest d, Src s) { - return _on(d, s); +template +inline _on +on(Src s, Dest d) { + return _on(s, d); } template