* Changed view_filter to use boolean commands instead of custom

functors.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1042 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2008-03-16 13:55:40 +00:00
parent 8cc7c8430d
commit f9ff1cf5ac
9 changed files with 134 additions and 88 deletions
+22 -8
View File
@@ -39,6 +39,7 @@
#include <algorithm>
#include <functional>
#include <rak/functional.h>
#include <rpc/parse_commands.h>
#include <sigc++/adaptors/bind.h>
#include <torrent/download.h>
#include <torrent/exceptions.h>
@@ -128,19 +129,30 @@ struct view_downloads_compare : std::binary_function<Download*, Download*, bool>
};
struct view_downloads_filter : std::unary_function<Download*, bool> {
view_downloads_filter(const View::filter_list& s) : m_filter(s) {}
view_downloads_filter(const std::string& cmd) : m_command(cmd) {}
bool operator () (Download* d1) const {
for (View::filter_list::const_iterator itr = m_filter.begin(), last = m_filter.end(); itr != last; ++itr)
if (!(**itr)(d1))
return false;
try {
torrent::Object result = rpc::parse_command_single(rpc::make_target(d1), m_command);
// The default filter action is to return true, to not filter the
// download out.
return true;
switch (result.type()) {
case torrent::Object::TYPE_NONE: return false;
case torrent::Object::TYPE_VALUE: return result.as_value();
case torrent::Object::TYPE_STRING: return !result.as_string().empty();
case torrent::Object::TYPE_LIST: return !result.as_list().empty();
case torrent::Object::TYPE_MAP: return !result.as_map().empty();
}
// The default filter action is to return true, to not filter
// the download out.
return true;
} catch (torrent::input_error& e) {
return false;
}
}
const View::filter_list& m_filter;
const std::string& m_command;
};
void
@@ -232,6 +244,8 @@ View::received(core::Download* download, int event) {
// Erase even if it is in visible so that the download is
// re-sorted.
//
// Do we really want to do this?
erase(itr);
insert_visible(download);
+3 -14
View File
@@ -62,14 +62,12 @@ namespace core {
class Download;
class DownloadList;
class ViewSort;
class ViewFilter;
class View : private std::vector<Download*> {
public:
typedef std::vector<Download*> base_type;
typedef sigc::signal0<void> signal_type;
typedef std::vector<const ViewSort*> sort_list;
typedef std::vector<const ViewFilter*> filter_list;
using base_type::iterator;
using base_type::const_iterator;
@@ -117,10 +115,9 @@ public:
// Need to explicity trigger filtering.
void filter();
void set_filter(const filter_list& s) { m_filter = s; }
void set_filter(const std::string& s) { m_filter = s; }
void set_filter_on(int event);
void set_cfilter(const std::string& s) { m_cfilter = s; }
void clear_filter_on();
@@ -162,9 +159,8 @@ private:
sort_list m_sortNew;
sort_list m_sortCurrent;
filter_list m_filter;
std::string m_cfilter;
// This should be replaced by a faster non-string command type.
std::string m_filter;
rak::timer m_lastChanged;
signal_type m_signalChanged;
@@ -177,13 +173,6 @@ public:
virtual bool operator () (Download* d1, Download* d2) const = 0;
};
class ViewFilter {
public:
virtual ~ViewFilter() {}
virtual bool operator () (Download* d1) const = 0;
};
}
#endif
+2 -41
View File
@@ -111,21 +111,6 @@ private:
ViewSort* m_sort;
};
class ViewFilterVariableValue : public ViewFilter {
public:
ViewFilterVariableValue(const char* name, torrent::Object::value_type v, bool inverse = false) :
m_name(name), m_value(v), m_inverse(inverse) {}
virtual bool operator () (Download* d1) const {
return (rpc::call_command_value(m_name, rpc::make_target(d1)) == m_value) != m_inverse;
}
private:
const char* m_name;
torrent::Object::value_type m_value;
bool m_inverse;
};
// Really need to implement a factory and allow options in the sort
// statements.
ViewManager::ViewManager(DownloadList* dl) :
@@ -143,19 +128,12 @@ ViewManager::ViewManager(DownloadList* dl) :
m_sort["state_changed"] = new ViewSortVariableValue("d.get_state_changed");
m_sort["state_changed_reverse"] = new ViewSortVariableValue("d.get_state_changed", true);
m_filter["started"] = new ViewFilterVariableValue("d.get_state", 1);
m_filter["stopped"] = new ViewFilterVariableValue("d.get_state", 0);
m_filter["complete"] = new ViewFilterVariableValue("d.get_complete", 0, true);
m_filter["incomplete"] = new ViewFilterVariableValue("d.get_complete", 0);
m_filter["hashing"] = new ViewFilterVariableValue("d.get_hashing", 0, true);
}
void
ViewManager::clear() {
std::for_each(begin(), end(), rak::call_delete<View>());
std::for_each(m_sort.begin(), m_sort.end(), rak::on(rak::mem_ref(&sort_map::value_type::second), rak::call_delete<ViewSort>()));
std::for_each(m_filter.begin(), m_filter.end(), rak::on(rak::mem_ref(&filter_map::value_type::second), rak::call_delete<ViewFilter>()));
base_type::clear();
}
@@ -230,28 +208,11 @@ ViewManager::set_sort_current(const std::string& name, const sort_args& sort) {
(*viewItr)->set_sort_current(build_sort_list(sort));
}
inline ViewManager::filter_list
ViewManager::build_filter_list(const filter_args& args) {
View::filter_list filterList;
filterList.reserve(args.size());
for (filter_args::const_iterator itr = args.begin(), last = args.end(); itr != last; ++itr) {
filter_map::const_iterator filterItr = m_filter.find(itr->c_str());
if (filterItr == m_filter.end())
throw torrent::input_error("Invalid filtering identifier.");
filterList.push_back(filterItr->second);
}
return filterList;
}
void
ViewManager::set_filter(const std::string& name, const filter_args& args) {
ViewManager::set_filter(const std::string& name, const std::string& cmd) {
iterator viewItr = find_throw(name);
(*viewItr)->set_filter(build_filter_list(args));
(*viewItr)->set_filter(cmd);
(*viewItr)->filter();
}
+1 -5
View File
@@ -60,8 +60,6 @@ public:
typedef View::sort_list sort_list;
typedef std::list<std::string> sort_args;
typedef std::map<const char*, ViewFilter*, view_manager_comp> filter_map;
typedef View::filter_list filter_list;
typedef std::list<std::string> filter_args;
using base_type::iterator;
@@ -105,17 +103,15 @@ public:
void set_sort_new(const std::string& name, const sort_args& sort);
void set_sort_current(const std::string& name, const sort_args& sort);
void set_filter(const std::string& name, const filter_args& args);
void set_filter(const std::string& name, const std::string& cmd);
void set_filter_on(const std::string& name, const filter_args& args);
private:
inline sort_list build_sort_list(const sort_args& args);
inline filter_list build_filter_list(const sort_args& args);
DownloadList* m_list;
sort_map m_sort;
filter_map m_filter;
};
}