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 FilterStorage::Subscriptions::size_type FilterStorage::GetSubscriptionCount() co
nst |
| 28 { |
| 29 return mSubscriptions.size(); |
| 30 } |
| 31 |
| 32 Subscription* FilterStorage::SubscriptionAt(FilterStorage::Subscriptions::size_t
ype 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 (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) |
| 44 if (mSubscriptions[i] == subscription) |
| 45 return i; |
| 46 return -1; |
| 47 } |
| 48 |
| 49 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) cons
t |
| 50 { |
| 51 SubscriptionPtr fallback; |
| 52 |
| 53 for (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) |
| 54 { |
| 55 SubscriptionPtr subscription(mSubscriptions[i]); |
| 56 UserDefinedSubscription* userDefinedSubscription = |
| 57 subscription->As<UserDefinedSubscription>(); |
| 58 if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && |
| 59 userDefinedSubscription->IsDefaultFor(filter)) |
| 60 { |
| 61 SubscriptionPtr result(subscription); |
| 62 return result.release(); |
| 63 } |
| 64 else if (!fallback && userDefinedSubscription && |
| 65 userDefinedSubscription->IsGeneric()) |
| 66 { |
| 67 fallback = subscription; |
| 68 } |
| 69 } |
| 70 |
| 71 return fallback.release(); |
| 72 } |
| 73 |
| 74 bool FilterStorage::AddSubscription(Subscription* subscription) |
| 75 { |
| 76 assert(subscription, u"Attempt to add a null subscription"_str); |
| 77 |
| 78 if (!subscription || subscription->GetListed()) |
| 79 return false; |
| 80 |
| 81 mSubscriptions.emplace_back(subscription); |
| 82 subscription->SetListed(true); |
| 83 |
| 84 FilterNotifier::SubscriptionChange( |
| 85 FilterNotifier::Topic::SUBSCRIPTION_ADDED, |
| 86 subscription |
| 87 ); |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool FilterStorage::RemoveSubscription(Subscription* subscription) |
| 92 { |
| 93 assert(subscription, u"Attempt to remove a null subscription"_str); |
| 94 |
| 95 if (!subscription || !subscription->GetListed()) |
| 96 return false; |
| 97 |
| 98 for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it) |
| 99 { |
| 100 if (*it == subscription) |
| 101 { |
| 102 mSubscriptions.erase(it); |
| 103 break; |
| 104 } |
| 105 } |
| 106 subscription->SetListed(false); |
| 107 |
| 108 FilterNotifier::SubscriptionChange( |
| 109 FilterNotifier::Topic::SUBSCRIPTION_REMOVED, |
| 110 subscription |
| 111 ); |
| 112 return true; |
| 113 } |
| 114 |
| 115 bool FilterStorage::MoveSubscription(Subscription* subscription, |
| 116 const Subscription* insertBefore) |
| 117 { |
| 118 assert(subscription, u"Attempt to move a null subscription"_str); |
| 119 |
| 120 int oldPos = IndexOfSubscription(subscription); |
| 121 assert(oldPos >= 0, u"Attempt to move a subscription that is not in the list"_
str); |
| 122 if (oldPos == -1) |
| 123 return false; |
| 124 |
| 125 int newPos = -1; |
| 126 if (insertBefore) |
| 127 newPos = IndexOfSubscription(insertBefore); |
| 128 if (newPos == -1) |
| 129 newPos = mSubscriptions.size(); |
| 130 |
| 131 if (newPos > oldPos) |
| 132 newPos--; |
| 133 |
| 134 if (newPos == oldPos) |
| 135 return false; |
| 136 |
| 137 mSubscriptions.erase(mSubscriptions.begin() + oldPos); |
| 138 mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription); |
| 139 |
| 140 FilterNotifier::SubscriptionChange( |
| 141 FilterNotifier::Topic::SUBSCRIPTION_MOVED, |
| 142 subscription |
| 143 ); |
| 144 return true; |
| 145 } |
OLD | NEW |