OLD | NEW |
(Empty) | |
| 1 #include <iostream> |
| 2 #include <sstream> |
| 3 |
| 4 #include "FiltersCommand.h" |
| 5 |
| 6 namespace |
| 7 { |
| 8 typedef std::vector<AdblockPlus::Filter*> FilterList; |
| 9 |
| 10 void ShowFilterList(const FilterList& filters) |
| 11 { |
| 12 for (FilterList::const_iterator it = filters.begin(); |
| 13 it != filters.end(); it++) |
| 14 { |
| 15 std::cout << (*it)->GetProperty("text", "(no text)") << " - " << |
| 16 (*it)->GetProperty("type", "(no type)") << std::endl; |
| 17 } |
| 18 } |
| 19 } |
| 20 |
| 21 FiltersCommand::FiltersCommand( |
| 22 AdblockPlus::FilterEngine& filterEngine) |
| 23 : Command("filters"), filterEngine(filterEngine) |
| 24 { |
| 25 } |
| 26 |
| 27 void FiltersCommand::operator()(const std::string& arguments) |
| 28 { |
| 29 std::istringstream argumentStream(arguments); |
| 30 std::string action; |
| 31 argumentStream >> action; |
| 32 if (!action.size()) |
| 33 { |
| 34 ShowFilters(); |
| 35 return; |
| 36 } |
| 37 |
| 38 if (action == "add") |
| 39 { |
| 40 std::string text; |
| 41 std::getline(argumentStream, text); |
| 42 if (text.size()) |
| 43 AddFilter(text); |
| 44 else |
| 45 ShowUsage(); |
| 46 } |
| 47 else if (action == "remove") |
| 48 { |
| 49 std::string text; |
| 50 std::getline(argumentStream, text); |
| 51 if (text.size()) |
| 52 RemoveFilter(text); |
| 53 else |
| 54 ShowUsage(); |
| 55 } |
| 56 else |
| 57 throw NoSuchCommandError(name + " " + action); |
| 58 } |
| 59 |
| 60 std::string FiltersCommand::GetDescription() const |
| 61 { |
| 62 return "List and manage custom filters"; |
| 63 } |
| 64 |
| 65 std::string FiltersCommand::GetUsage() const |
| 66 { |
| 67 return name + " [add FILTER|remove FILTER]"; |
| 68 } |
| 69 |
| 70 void FiltersCommand::ShowFilters() |
| 71 { |
| 72 ShowFilterList(filterEngine.GetListedFilters()); |
| 73 } |
| 74 |
| 75 void FiltersCommand::AddFilter(const std::string& text) |
| 76 { |
| 77 AdblockPlus::Filter& filter = filterEngine.GetFilter(text); |
| 78 filter.AddToList(); |
| 79 } |
| 80 |
| 81 void FiltersCommand::RemoveFilter(const std::string& text) |
| 82 { |
| 83 AdblockPlus::Filter& filter = filterEngine.GetFilter(text); |
| 84 if (!filter.IsListed()) |
| 85 { |
| 86 std::cout << "No such filter '" << text << "'" << std::endl; |
| 87 return; |
| 88 } |
| 89 filter.RemoveFromList(); |
| 90 } |
OLD | NEW |