Skip to content

Commit

Permalink
Merge branch 'pyroscope-cmd-d_multicall_filtered'
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa committed Oct 20, 2018
2 parents 4080134 + 656d735 commit c62fa6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/command_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,42 @@ d_multicall(const torrent::Object::list_type& args) {
return resultRaw;
}

torrent::Object
d_multicall_filtered(const torrent::Object::list_type& args) {
if (args.size() < 2)
throw torrent::input_error("d.multicall.filtered requires at least 2 arguments.");
torrent::Object::list_const_iterator arg = args.begin();

// Find the given view
core::ViewManager* viewManager = control->view_manager();
core::ViewManager::iterator viewItr = viewManager->find(arg->as_string().empty() ? "default" : arg->as_string());

if (viewItr == viewManager->end())
throw torrent::input_error("Could not find view '" + arg->as_string() + "'.");

// Make a filtered copy of the current item list
core::View::base_type dlist;
(*viewItr)->filter_by(*++arg, dlist);

// Generate result by iterating over all items
torrent::Object resultRaw = torrent::Object::create_list();
torrent::Object::list_type& result = resultRaw.as_list();
++arg; // skip to first command

for (core::View::iterator item = dlist.begin(); item != dlist.end(); ++item) {
// Add empty row to result
torrent::Object::list_type& row = result.insert(result.end(), torrent::Object::create_list())->as_list();

// Call the provided commands and assemble their results
for (torrent::Object::list_const_iterator command = arg; command != args.end(); command++) {
const std::string& cmdstr = command->as_string();
row.push_back(rpc::parse_command(rpc::make_target(*item), cmdstr.c_str(), cmdstr.c_str() + cmdstr.size()).first);
}
}

return resultRaw;
}

static void
call_watch_command(const std::string& command, const std::string& path) {
rpc::commands.call_catch(command.c_str(), rpc::make_target(), path);
Expand Down Expand Up @@ -359,6 +395,7 @@ initialize_command_events() {

CMD2_ANY_LIST ("download_list", std::bind(&apply_download_list, std::placeholders::_2));
CMD2_ANY_LIST ("d.multicall2", std::bind(&d_multicall, std::placeholders::_2));
CMD2_ANY_LIST ("d.multicall.filtered", std::bind(&d_multicall_filtered, std::placeholders::_2));

CMD2_ANY_LIST ("directory.watch.added", std::bind(&directory_watch_added, std::placeholders::_2));
}
10 changes: 10 additions & 0 deletions src/core/view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ View::filter() {
emit_changed();
}

void
View::filter_by(const torrent::Object& condition, View::base_type& result)
{
// std::copy_if(begin_visible(), end_visible(), result.begin(), view_downloads_filter(condition));
view_downloads_filter matches = view_downloads_filter(condition);
for (iterator itr = begin_visible(); itr != end_visible(); ++itr)
if (matches(*itr))
result.push_back(*itr);
}

void
View::filter_download(core::Download* download) {
iterator itr = std::find(base_type::begin(), base_type::end(), download);
Expand Down
1 change: 1 addition & 0 deletions src/core/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class View : private std::vector<Download*> {

// Need to explicity trigger filtering.
void filter();
void filter_by(const torrent::Object& condition, base_type& result);
void filter_download(core::Download* download);

const torrent::Object& get_filter() const { return m_filter; }
Expand Down

0 comments on commit c62fa6a

Please sign in to comment.