Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: shell/src/FiltersCommand.cpp

Issue 10100009: FilterEngine API improvements (Closed)
Patch Set: Added filter access and addressed review comments Created April 5, 2013, 12:10 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: shell/src/FiltersCommand.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/shell/src/FiltersCommand.cpp
@@ -0,0 +1,90 @@
+#include <iostream>
+#include <sstream>
+
+#include "FiltersCommand.h"
+
+namespace
+{
+ typedef std::vector<AdblockPlus::Filter*> FilterList;
+
+ void ShowFilterList(const FilterList& filters)
+ {
+ for (FilterList::const_iterator it = filters.begin();
+ it != filters.end(); it++)
+ {
+ std::cout << (*it)->GetProperty("text", "(no text)") << " - " <<
+ (*it)->GetProperty("type", "(no type)") << std::endl;
+ }
+ }
+}
+
+FiltersCommand::FiltersCommand(
+ AdblockPlus::FilterEngine& filterEngine)
+ : Command("filters"), filterEngine(filterEngine)
+{
+}
+
+void FiltersCommand::operator()(const std::string& arguments)
+{
+ std::istringstream argumentStream(arguments);
+ std::string action;
+ argumentStream >> action;
+ if (!action.size())
+ {
+ ShowFilters();
+ return;
+ }
+
+ if (action == "add")
+ {
+ std::string text;
+ std::getline(argumentStream, text);
+ if (text.size())
+ AddFilter(text);
+ else
+ ShowUsage();
+ }
+ else if (action == "remove")
+ {
+ std::string text;
+ std::getline(argumentStream, text);
+ if (text.size())
+ RemoveFilter(text);
+ else
+ ShowUsage();
+ }
+ else
+ throw NoSuchCommandError(name + " " + action);
+}
+
+std::string FiltersCommand::GetDescription() const
+{
+ return "List and manage custom filters";
+}
+
+std::string FiltersCommand::GetUsage() const
+{
+ return name + " [add FILTER|remove FILTER]";
+}
+
+void FiltersCommand::ShowFilters()
+{
+ ShowFilterList(filterEngine.GetListedFilters());
+}
+
+void FiltersCommand::AddFilter(const std::string& text)
+{
+ AdblockPlus::Filter& filter = filterEngine.GetFilter(text);
+ filter.AddToList();
+}
+
+void FiltersCommand::RemoveFilter(const std::string& text)
+{
+ AdblockPlus::Filter& filter = filterEngine.GetFilter(text);
+ if (!filter.IsListed())
+ {
+ std::cout << "No such filter '" << text << "'" << std::endl;
+ return;
+ }
+ filter.RemoveFromList();
+}

Powered by Google App Engine
This is Rietveld