OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 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 namespace |
| 26 { |
| 27 std::vector<SubscriptionPtr> subscriptions; |
| 28 } |
| 29 |
| 30 unsigned int FilterStorage::GetSubscriptionCount() |
| 31 { |
| 32 return subscriptions.size(); |
| 33 } |
| 34 |
| 35 Subscription* FilterStorage::SubscriptionAt(unsigned int index) |
| 36 { |
| 37 if (index >= subscriptions.size()) |
| 38 return nullptr; |
| 39 |
| 40 SubscriptionPtr result(subscriptions[index]); |
| 41 return result.release(); |
| 42 } |
| 43 |
| 44 int FilterStorage::IndexOfSubscription(const Subscription* subscription) |
| 45 { |
| 46 for (unsigned int i = 0; i < subscriptions.size(); i++) |
| 47 if (subscriptions[i] == subscription) |
| 48 return i; |
| 49 return -1; |
| 50 } |
| 51 |
| 52 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) |
| 53 { |
| 54 SubscriptionPtr fallback; |
| 55 |
| 56 for (auto& subscription : subscriptions) |
| 57 { |
| 58 UserDefinedSubscription* userDefinedSubscription = |
| 59 subscription->As<UserDefinedSubscription>(); |
| 60 if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && |
| 61 userDefinedSubscription->IsDefaultFor(filter)) |
| 62 { |
| 63 SubscriptionPtr result(subscription); |
| 64 return result.release(); |
| 65 } |
| 66 else if (!fallback && userDefinedSubscription && |
| 67 userDefinedSubscription->IsGeneric()) |
| 68 { |
| 69 fallback = subscription; |
| 70 } |
| 71 } |
| 72 |
| 73 return fallback.release(); |
| 74 } |
| 75 |
| 76 bool FilterStorage::AddSubscription(Subscription* subscription) |
| 77 { |
| 78 assert(subscription, u"Attempt to add a null subscription"_str); |
| 79 |
| 80 if (!subscription || subscription->GetListed()) |
| 81 return false; |
| 82 |
| 83 subscriptions.emplace_back(subscription); |
| 84 subscription->SetListed(true); |
| 85 |
| 86 FilterNotifier::SubscriptionChange( |
| 87 FilterNotifier::Topic::SUBSCRIPTION_ADDED, |
| 88 subscription |
| 89 ); |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool FilterStorage::RemoveSubscription(Subscription* subscription) |
| 94 { |
| 95 assert(subscription, u"Attempt to remove a null subscription"_str); |
| 96 |
| 97 if (!subscription || !subscription->GetListed()) |
| 98 return false; |
| 99 |
| 100 for (auto it = subscriptions.begin(); it != subscriptions.end(); ++it) |
| 101 { |
| 102 if (*it == subscription) |
| 103 { |
| 104 subscriptions.erase(it); |
| 105 break; |
| 106 } |
| 107 } |
| 108 subscription->SetListed(false); |
| 109 |
| 110 FilterNotifier::SubscriptionChange( |
| 111 FilterNotifier::Topic::SUBSCRIPTION_REMOVED, |
| 112 subscription |
| 113 ); |
| 114 return true; |
| 115 } |
| 116 |
| 117 bool FilterStorage::MoveSubscription(Subscription* subscription, |
| 118 const Subscription* insertBefore) |
| 119 { |
| 120 assert(subscription, u"Attempt to move a null subscription"_str); |
| 121 |
| 122 int oldPos = IndexOfSubscription(subscription); |
| 123 if (oldPos == -1) |
| 124 return false; |
| 125 |
| 126 int newPos = -1; |
| 127 if (insertBefore) |
| 128 newPos = IndexOfSubscription(insertBefore); |
| 129 if (newPos == -1) |
| 130 newPos = subscriptions.size(); |
| 131 |
| 132 if (newPos > oldPos) |
| 133 newPos--; |
| 134 |
| 135 if (newPos == oldPos) |
| 136 return false; |
| 137 |
| 138 subscriptions.erase(subscriptions.begin() + oldPos); |
| 139 subscriptions.emplace(subscriptions.begin() + newPos, subscription); |
| 140 |
| 141 FilterNotifier::SubscriptionChange( |
| 142 FilterNotifier::Topic::SUBSCRIPTION_MOVED, |
| 143 subscription |
| 144 ); |
| 145 return true; |
| 146 } |
OLD | NEW |