Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 #include <vector> | |
19 | |
20 #include "FilterStorage.h" | |
21 #include "../filter/Filter.h" | |
22 #include "../subscription/UserDefinedSubscription.h" | |
23 #include "../FilterNotifier.h" | |
24 | |
25 FilterStorage* FilterStorage::mInstance = new FilterStorage(); | |
26 | |
27 unsigned int FilterStorage::GetSubscriptionCount() const | |
28 { | |
29 return mSubscriptions.size(); | |
30 } | |
31 | |
32 Subscription* FilterStorage::SubscriptionAt(unsigned int index) const | |
33 { | |
34 if (index >= mSubscriptions.size()) | |
35 return nullptr; | |
36 | |
37 SubscriptionPtr result(mSubscriptions[index]); | |
38 return result.release(); | |
39 } | |
40 | |
41 int FilterStorage::IndexOfSubscription(const Subscription* subscription) const | |
42 { | |
43 for (unsigned int i = 0; i < mSubscriptions.size(); i++) | |
sergei
2017/08/24 13:32:06
Despite there are strangely no warnings from compi
Wladimir Palant
2017/08/31 11:32:35
Done.
| |
44 if (mSubscriptions[i] == subscription) | |
45 return i; | |
46 return -1; | |
47 } | |
48 | |
49 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) | |
sergei
2017/08/24 13:32:05
It seems that this method should be const.
Wladimir Palant
2017/08/31 11:32:36
Managed to do this with somewhat counterintuitive
sergei
2017/08/31 12:43:38
Yeah, sorry, maybe it was a bad idea to turn it in
| |
50 { | |
51 SubscriptionPtr fallback; | |
52 | |
53 for (auto& subscription : mSubscriptions) | |
54 { | |
55 UserDefinedSubscription* userDefinedSubscription = | |
56 subscription->As<UserDefinedSubscription>(); | |
57 if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && | |
58 userDefinedSubscription->IsDefaultFor(filter)) | |
59 { | |
60 SubscriptionPtr result(subscription); | |
61 return result.release(); | |
62 } | |
63 else if (!fallback && userDefinedSubscription && | |
64 userDefinedSubscription->IsGeneric()) | |
65 { | |
66 fallback = subscription; | |
67 } | |
68 } | |
69 | |
70 return fallback.release(); | |
71 } | |
72 | |
73 bool FilterStorage::AddSubscription(Subscription* subscription) | |
74 { | |
75 assert(subscription, u"Attempt to add a null subscription"_str); | |
76 | |
77 if (!subscription || subscription->GetListed()) | |
78 return false; | |
79 | |
80 mSubscriptions.emplace_back(subscription); | |
81 subscription->SetListed(true); | |
82 | |
83 FilterNotifier::SubscriptionChange( | |
84 FilterNotifier::Topic::SUBSCRIPTION_ADDED, | |
85 subscription | |
86 ); | |
87 return true; | |
88 } | |
89 | |
90 bool FilterStorage::RemoveSubscription(Subscription* subscription) | |
sergei
2017/08/24 13:32:06
It seems so far there is no way to make the argume
Wladimir Palant
2017/08/31 11:32:35
For AddSubscription() the argument shouldn't be co
sergei
2017/08/31 12:43:38
Acknowledged.
| |
91 { | |
92 assert(subscription, u"Attempt to remove a null subscription"_str); | |
93 | |
94 if (!subscription || !subscription->GetListed()) | |
95 return false; | |
96 | |
97 for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it) | |
98 { | |
99 if (*it == subscription) | |
100 { | |
101 mSubscriptions.erase(it); | |
102 break; | |
103 } | |
104 } | |
105 subscription->SetListed(false); | |
106 | |
107 FilterNotifier::SubscriptionChange( | |
108 FilterNotifier::Topic::SUBSCRIPTION_REMOVED, | |
109 subscription | |
110 ); | |
111 return true; | |
112 } | |
113 | |
114 bool FilterStorage::MoveSubscription(Subscription* subscription, | |
sergei
2017/08/24 13:32:05
should `subscription` argument be a const pointer?
Wladimir Palant
2017/08/31 11:32:35
It cannot be. Even if we make changes to the refer
sergei
2017/08/31 12:43:38
Acknowledged.
| |
115 const Subscription* insertBefore) | |
116 { | |
117 assert(subscription, u"Attempt to move a null subscription"_str); | |
118 | |
119 int oldPos = IndexOfSubscription(subscription); | |
120 if (oldPos == -1) | |
121 return false; | |
sergei
2017/08/24 13:32:05
should there be an assert? it's seems undesired wh
Wladimir Palant
2017/08/31 11:32:34
Done.
| |
122 | |
123 int newPos = -1; | |
124 if (insertBefore) | |
125 newPos = IndexOfSubscription(insertBefore); | |
126 if (newPos == -1) | |
127 newPos = mSubscriptions.size(); | |
128 | |
129 if (newPos > oldPos) | |
130 newPos--; | |
131 | |
132 if (newPos == oldPos) | |
133 return false; | |
134 | |
135 mSubscriptions.erase(mSubscriptions.begin() + oldPos); | |
136 mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription); | |
137 | |
138 FilterNotifier::SubscriptionChange( | |
139 FilterNotifier::Topic::SUBSCRIPTION_MOVED, | |
140 subscription | |
141 ); | |
142 return true; | |
143 } | |
OLD | NEW |