OLD | NEW |
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 namespace | 6 namespace |
7 { | 7 { |
8 typedef std::vector<AdblockPlus::Subscription> SubscriptionList; | 8 typedef std::vector<AdblockPlus::Subscription*> SubscriptionList; |
9 | 9 |
10 void ShowSubscriptionList(const SubscriptionList& subscriptions) | 10 void ShowSubscriptionList(const SubscriptionList& subscriptions) |
11 { | 11 { |
12 for (SubscriptionList::const_iterator it = subscriptions.begin(); | 12 for (SubscriptionList::const_iterator it = subscriptions.begin(); |
13 it != subscriptions.end(); it++) | 13 it != subscriptions.end(); it++) |
14 std::cout << it->title << " - " << it->url << std::endl; | 14 std::cout << (*it)->GetProperty("title", "(no title)") << " - " << (*it)->
GetProperty("url", "") << std::endl; |
15 } | 15 } |
16 } | 16 } |
17 | 17 |
18 SubscriptionsCommand::SubscriptionsCommand( | 18 SubscriptionsCommand::SubscriptionsCommand( |
19 AdblockPlus::FilterEngine& filterEngine) | 19 AdblockPlus::FilterEngine& filterEngine) |
20 : Command("subscriptions"), filterEngine(filterEngine) | 20 : Command("subscriptions"), filterEngine(filterEngine) |
21 { | 21 { |
22 } | 22 } |
23 | 23 |
24 void SubscriptionsCommand::operator()(const std::string& arguments) | 24 void SubscriptionsCommand::operator()(const std::string& arguments) |
25 { | 25 { |
26 std::istringstream argumentStream(arguments); | 26 std::istringstream argumentStream(arguments); |
27 std::string action; | 27 std::string action; |
28 argumentStream >> action; | 28 argumentStream >> action; |
29 if (!action.size()) | 29 if (!action.size()) |
30 { | 30 { |
31 ShowSubscriptions(); | 31 ShowSubscriptions(); |
32 return; | 32 return; |
33 } | 33 } |
34 | 34 |
35 if (action == "add") | 35 if (action == "add") |
36 { | 36 { |
37 std::string url; | 37 std::string url; |
38 argumentStream >> url; | 38 argumentStream >> url; |
39 std::string title; | 39 std::string title; |
40 std::getline(argumentStream, title); | 40 std::getline(argumentStream, title); |
41 if (url.size() || title.size()) | 41 if (url.size()) |
42 AddSubscription(url, title); | 42 AddSubscription(url, title); |
43 else | 43 else |
44 ShowUsage(); | 44 ShowUsage(); |
45 } | 45 } |
46 else if (action == "remove") | 46 else if (action == "remove") |
47 { | 47 { |
48 std::string url; | 48 std::string url; |
49 argumentStream >> url; | 49 argumentStream >> url; |
50 if (url.size()) | 50 if (url.size()) |
51 RemoveSubscription(url); | 51 RemoveSubscription(url); |
52 else | 52 else |
53 ShowUsage(); | 53 ShowUsage(); |
54 } | 54 } |
55 else if (action == "update") | 55 else if (action == "update") |
56 UpdateSubscriptions(); | 56 UpdateSubscriptions(); |
57 else if (action == "fetch") | 57 else if (action == "fetch") |
58 FetchSubscriptions(); | 58 FetchSubscriptions(); |
59 else | 59 else |
60 throw NoSuchCommandError(name + " " + action); | 60 throw NoSuchCommandError(name + " " + action); |
61 } | 61 } |
62 | 62 |
63 std::string SubscriptionsCommand::GetDescription() const | 63 std::string SubscriptionsCommand::GetDescription() const |
64 { | 64 { |
65 return "List and manage subscriptions"; | 65 return "List and manage subscriptions"; |
66 } | 66 } |
67 | 67 |
68 std::string SubscriptionsCommand::GetUsage() const | 68 std::string SubscriptionsCommand::GetUsage() const |
69 { | 69 { |
70 return name + " [add URL TITLE|remove URL|update|fetch]"; | 70 return name + " [add URL [TITLE]|remove URL|update|fetch]"; |
71 } | 71 } |
72 | 72 |
73 void SubscriptionsCommand::ShowSubscriptions() | 73 void SubscriptionsCommand::ShowSubscriptions() |
74 { | 74 { |
75 ShowSubscriptionList(filterEngine.GetSubscriptions()); | 75 ShowSubscriptionList(filterEngine.GetListedSubscriptions()); |
76 } | 76 } |
77 | 77 |
78 void SubscriptionsCommand::AddSubscription(const std::string& url, | 78 void SubscriptionsCommand::AddSubscription(const std::string& url, |
79 const std::string& title) | 79 const std::string& title) |
80 { | 80 { |
81 filterEngine.AddSubscription(AdblockPlus::Subscription(url, title)); | 81 AdblockPlus::Subscription& subscription = filterEngine.GetSubscription(url); |
| 82 if (title.size()) |
| 83 subscription.SetProperty("title", title); |
| 84 subscription.AddToList(); |
82 } | 85 } |
83 | 86 |
84 void SubscriptionsCommand::RemoveSubscription(const std::string& url) | 87 void SubscriptionsCommand::RemoveSubscription(const std::string& url) |
85 { | 88 { |
86 const AdblockPlus::Subscription* const subscription = | 89 AdblockPlus::Subscription& subscription = filterEngine.GetSubscription(url); |
87 filterEngine.FindSubscription(url); | 90 if (!subscription.IsListed()) |
88 if (!subscription) | |
89 { | 91 { |
90 std::cout << "No subscription with URL '" << url << "'" << std::endl; | 92 std::cout << "No subscription with URL '" << url << "'" << std::endl; |
91 return; | 93 return; |
92 } | 94 } |
93 filterEngine.RemoveSubscription(*subscription); | 95 subscription.RemoveFromList(); |
94 } | 96 } |
95 | 97 |
96 void SubscriptionsCommand::UpdateSubscriptions() | 98 void SubscriptionsCommand::UpdateSubscriptions() |
97 { | 99 { |
98 const SubscriptionList& subscriptions = filterEngine.GetSubscriptions(); | 100 const SubscriptionList& subscriptions = filterEngine.GetListedSubscriptions(); |
99 for (SubscriptionList::const_iterator it = subscriptions.begin(); | 101 for (SubscriptionList::const_iterator it = subscriptions.begin(); |
100 it != subscriptions.end(); it++) | 102 it != subscriptions.end(); it++) |
101 filterEngine.UpdateSubscriptionFilters(*it); | 103 (*it)->UpdateFilters(); |
102 } | 104 } |
103 | 105 |
104 void SubscriptionsCommand::FetchSubscriptions() | 106 void SubscriptionsCommand::FetchSubscriptions() |
105 { | 107 { |
106 ShowSubscriptionList(filterEngine.FetchAvailableSubscriptions()); | 108 filterEngine.FetchAvailableSubscriptions(&ShowSubscriptionList); |
107 } | 109 } |
OLD | NEW |