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

Side by Side Diff: shell/src/SubscriptionsCommand.cpp

Issue 9987009: Interface for the libadblockplus API (Closed)
Patch Set: Reduce redundant redundancy Created March 27, 2013, 6:45 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <iostream> 1 #include <iostream>
2 #include <sstream> 2 #include <sstream>
3 3
4 #include "SubscriptionsCommand.h" 4 #include "SubscriptionsCommand.h"
5 5
6 SubscriptionsCommand::SubscriptionsCommand(AdblockPlus::JsEngine& jsEngine) 6 namespace
7 : Command("subscriptions"), jsEngine(jsEngine) 7 {
8 typedef std::vector<AdblockPlus::Subscription> SubscriptionList;
9
10 void ShowSubscriptionList(const SubscriptionList& subscriptions)
11 {
12 for (SubscriptionList::const_iterator it = subscriptions.begin();
13 it != subscriptions.end(); it++)
14 std::cout << it->title << " - " << it->url << std::endl;
15 }
16 }
17
18 SubscriptionsCommand::SubscriptionsCommand(
19 AdblockPlus::FilterEngine& filterEngine)
20 : Command("subscriptions"), filterEngine(filterEngine)
8 { 21 {
9 } 22 }
10 23
11 void SubscriptionsCommand::operator()(const std::string& arguments) 24 void SubscriptionsCommand::operator()(const std::string& arguments)
12 { 25 {
13 std::istringstream argumentStream(arguments); 26 std::istringstream argumentStream(arguments);
14 std::string action; 27 std::string action;
15 argumentStream >> action; 28 argumentStream >> action;
16 if (!action.size()) 29 if (!action.size())
17 { 30 {
18 ShowSubscriptions(); 31 ShowSubscriptions();
19 return; 32 return;
20 } 33 }
21 34
22 if (action == "update")
23 {
24 UpdateSubscriptions();
25 return;
26 }
27
28 std::string url;
29 argumentStream >> url;
30
31 if (action == "add") 35 if (action == "add")
32 { 36 {
33 AddSubscription(url); 37 std::string url;
34 return; 38 argumentStream >> url;
39 std::string title;
40 std::getline(argumentStream, title);
41 if (url.size() || title.size())
42 AddSubscription(url, title);
43 else
44 ShowUsage();
35 } 45 }
36 46 else if (action == "remove")
37 if (action == "remove")
38 { 47 {
39 RemoveSubscription(url); 48 std::string url;
40 return; 49 argumentStream >> url;
50 if (url.size())
51 RemoveSubscription(url);
52 else
53 ShowUsage();
41 } 54 }
42 55 else if (action == "update")
43 throw NoSuchCommandError(name + " " + action); 56 UpdateSubscriptions();
57 else if (action == "fetch")
58 FetchSubscriptions();
59 else
60 throw NoSuchCommandError(name + " " + action);
44 } 61 }
45 62
46 std::string SubscriptionsCommand::GetDescription() const 63 std::string SubscriptionsCommand::GetDescription() const
47 { 64 {
48 return "List and manage subscriptions"; 65 return "List and manage subscriptions";
49 } 66 }
50 67
51 std::string SubscriptionsCommand::GetUsage() const 68 std::string SubscriptionsCommand::GetUsage() const
52 { 69 {
53 return name + " [add URL|remove URL|update]"; 70 return name + " [add URL TITLE|remove URL|update|fetch]";
54 } 71 }
55 72
56 void SubscriptionsCommand::ShowSubscriptions() 73 void SubscriptionsCommand::ShowSubscriptions()
57 { 74 {
58 // TODO: List all subscriptions 75 ShowSubscriptionList(filterEngine.GetSubscriptions());
59 } 76 }
60 77
61 void SubscriptionsCommand::AddSubscription(const std::string& url) 78 void SubscriptionsCommand::AddSubscription(const std::string& url,
79 const std::string& title)
62 { 80 {
63 // TODO: Add a subscriptions 81 filterEngine.AddSubscription(AdblockPlus::Subscription(url, title));
64 } 82 }
65 83
66 void SubscriptionsCommand::RemoveSubscription(const std::string& url) 84 void SubscriptionsCommand::RemoveSubscription(const std::string& url)
67 { 85 {
68 // TODO: Remove a subscription 86 const AdblockPlus::Subscription* const subscription =
87 filterEngine.FindSubscription(url);
88 if (!subscription)
89 {
90 std::cout << "No subscription with URL '" << url << "'" << std::endl;
91 return;
92 }
93 filterEngine.RemoveSubscription(*subscription);
69 } 94 }
70 95
71 void SubscriptionsCommand::UpdateSubscriptions() 96 void SubscriptionsCommand::UpdateSubscriptions()
72 { 97 {
73 // TODO: Update all subscriptions 98 const SubscriptionList& subscriptions = filterEngine.GetSubscriptions();
99 for (SubscriptionList::const_iterator it = subscriptions.begin();
100 it != subscriptions.end(); it++)
101 filterEngine.UpdateSubscriptionFilters(*it);
74 } 102 }
103
104 void SubscriptionsCommand::FetchSubscriptions()
105 {
106 ShowSubscriptionList(filterEngine.FetchAvailableSubscriptions());
107 }
OLDNEW
« shell/src/Main.cpp ('K') | « shell/src/SubscriptionsCommand.h ('k') | src/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld