Index: compiled/storage/FilterStorage.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/compiled/storage/FilterStorage.cpp |
@@ -0,0 +1,146 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2017 eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#include <vector> |
+ |
+#include "FilterStorage.h" |
+#include "../filter/Filter.h" |
+#include "../subscription/UserDefinedSubscription.h" |
+#include "../FilterNotifier.h" |
+ |
+namespace |
+{ |
+ std::vector<SubscriptionPtr> subscriptions; |
+} |
+ |
+unsigned int FilterStorage::GetSubscriptionCount() |
+{ |
+ return subscriptions.size(); |
+} |
+ |
+Subscription* FilterStorage::SubscriptionAt(unsigned int index) |
+{ |
+ if (index >= subscriptions.size()) |
+ return nullptr; |
+ |
+ SubscriptionPtr result(subscriptions[index]); |
+ return result.release(); |
+} |
+ |
+int FilterStorage::IndexOfSubscription(const Subscription* subscription) |
+{ |
+ for (unsigned int i = 0; i < subscriptions.size(); i++) |
+ if (subscriptions[i] == subscription) |
+ return i; |
+ return -1; |
+} |
+ |
+Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) |
+{ |
+ SubscriptionPtr fallback; |
+ |
+ for (auto& subscription : subscriptions) |
+ { |
+ UserDefinedSubscription* userDefinedSubscription = |
+ subscription->As<UserDefinedSubscription>(); |
+ if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && |
+ userDefinedSubscription->IsDefaultFor(filter)) |
+ { |
+ SubscriptionPtr result(subscription); |
+ return result.release(); |
+ } |
+ else if (!fallback && userDefinedSubscription && |
+ userDefinedSubscription->IsGeneric()) |
+ { |
+ fallback = subscription; |
+ } |
+ } |
+ |
+ return fallback.release(); |
+} |
+ |
+bool FilterStorage::AddSubscription(Subscription* subscription) |
+{ |
+ assert(subscription, u"Attempt to add a null subscription"_str); |
+ |
+ if (!subscription || subscription->GetListed()) |
+ return false; |
+ |
+ subscriptions.emplace_back(subscription); |
+ subscription->SetListed(true); |
+ |
+ FilterNotifier::SubscriptionChange( |
+ FilterNotifier::Topic::SUBSCRIPTION_ADDED, |
+ subscription |
+ ); |
+ return true; |
+} |
+ |
+bool FilterStorage::RemoveSubscription(Subscription* subscription) |
+{ |
+ assert(subscription, u"Attempt to remove a null subscription"_str); |
+ |
+ if (!subscription || !subscription->GetListed()) |
+ return false; |
+ |
+ for (auto it = subscriptions.begin(); it != subscriptions.end(); ++it) |
+ { |
+ if (*it == subscription) |
+ { |
+ subscriptions.erase(it); |
+ break; |
+ } |
+ } |
+ subscription->SetListed(false); |
+ |
+ FilterNotifier::SubscriptionChange( |
+ FilterNotifier::Topic::SUBSCRIPTION_REMOVED, |
+ subscription |
+ ); |
+ return true; |
+} |
+ |
+bool FilterStorage::MoveSubscription(Subscription* subscription, |
+ const Subscription* insertBefore) |
+{ |
+ assert(subscription, u"Attempt to move a null subscription"_str); |
+ |
+ int oldPos = IndexOfSubscription(subscription); |
+ if (oldPos == -1) |
+ return false; |
+ |
+ int newPos = -1; |
+ if (insertBefore) |
+ newPos = IndexOfSubscription(insertBefore); |
+ if (newPos == -1) |
+ newPos = subscriptions.size(); |
+ |
+ if (newPos > oldPos) |
+ newPos--; |
+ |
+ if (newPos == oldPos) |
+ return false; |
+ |
+ subscriptions.erase(subscriptions.begin() + oldPos); |
+ subscriptions.emplace(subscriptions.begin() + newPos, subscription); |
+ |
+ FilterNotifier::SubscriptionChange( |
+ FilterNotifier::Topic::SUBSCRIPTION_MOVED, |
+ subscription |
+ ); |
+ return true; |
+} |