OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 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 <AdblockPlus/JsValue.h> |
| 19 #include <AdblockPlus/JsEngine.h> |
| 20 #include <AdblockPlus/Notification.h> |
| 21 #include <algorithm> |
| 22 |
| 23 using namespace AdblockPlus; |
| 24 |
| 25 namespace |
| 26 { |
| 27 typedef std::pair<NotificationType, std::string> NotificationTypeString; |
| 28 typedef std::vector<NotificationTypeString> NotificationTypes; |
| 29 const NotificationTypes g_notificationTypes = []()->NotificationTypes |
| 30 { |
| 31 NotificationTypes retValue; |
| 32 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUE
STION, "question")); |
| 33 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRI
TICAL, "critical")); |
| 34 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INF
ORMATION, "information")); |
| 35 return retValue; |
| 36 }(); |
| 37 |
| 38 NotificationType StringToNotificationType(const std::string& value) |
| 39 { |
| 40 auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notif
icationTypes.end(), |
| 41 [&value](const NotificationTypeString& pair)->bool |
| 42 { |
| 43 return value == pair.second; |
| 44 }); |
| 45 if (ii_notificationType == g_notificationTypes.end()) |
| 46 { |
| 47 return NotificationType::NOTIFICATION_TYPE_INFORMATION; |
| 48 } |
| 49 return ii_notificationType->first; |
| 50 } |
| 51 |
| 52 std::string NotificationTypeToString(NotificationType value) |
| 53 { |
| 54 auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notif
icationTypes.end(), |
| 55 [&value](const NotificationTypeString& pair)->bool |
| 56 { |
| 57 return value == pair.first; |
| 58 }); |
| 59 if (ii_notificationType == g_notificationTypes.end()) |
| 60 { |
| 61 return "information"; |
| 62 } |
| 63 return ii_notificationType->second; |
| 64 } |
| 65 } |
| 66 |
| 67 Notification::Notification(NotificationType type, const std::string& id, const J
sValuePtr& jsValue, PrivateCtrArg) |
| 68 : JsValue(jsValue) |
| 69 { |
| 70 SetProperty("type", NotificationTypeToString(type)); |
| 71 SetProperty("id", id); |
| 72 } |
| 73 |
| 74 std::string Notification::GetId() const |
| 75 { |
| 76 auto jsValue = GetProperty("id"); |
| 77 return jsValue ? jsValue->AsString() : ""; |
| 78 } |
| 79 |
| 80 NotificationType Notification::GetType() const |
| 81 { |
| 82 auto jsValue = GetProperty("type"); |
| 83 return StringToNotificationType(jsValue ? GetProperty("type")->AsString() : ""
); |
| 84 } |
| 85 |
| 86 NotificationTexts Notification::GetTexts(const std::string& locale) |
| 87 { |
| 88 JsValuePtr func = jsEngine->Evaluate("API.getNotificationTexts"); |
| 89 if (!func) |
| 90 { |
| 91 return NotificationTexts(); |
| 92 } |
| 93 JsValueList params; |
| 94 params.push_back(shared_from_this()); |
| 95 if (!locale.empty()) |
| 96 { |
| 97 params.push_back(jsEngine->NewValue(locale)); |
| 98 } |
| 99 auto jsTexts = func->Call(params); |
| 100 if (!jsTexts) |
| 101 { |
| 102 return NotificationTexts(); |
| 103 } |
| 104 return Notification::JsTextsToNotificationTexts(*jsTexts); |
| 105 } |
| 106 |
| 107 void Notification::SetTitle(const std::string& value, const std::string& locale) |
| 108 { |
| 109 SetMultilingualProperty("title", value, locale); |
| 110 } |
| 111 |
| 112 void Notification::SetMessage(const std::string& value, const std::string& local
e) |
| 113 { |
| 114 SetMultilingualProperty("message", value, locale); |
| 115 } |
| 116 |
| 117 void Notification::SetMultilingualProperty(const std::string& propertyName, |
| 118 const std::string& value, |
| 119 const std::string& locale) |
| 120 { |
| 121 if (locale.empty()) |
| 122 { |
| 123 SetProperty(propertyName, value); |
| 124 } |
| 125 else |
| 126 { |
| 127 auto multilingualPropertyBag = GetProperty(propertyName); |
| 128 if (!multilingualPropertyBag || !multilingualPropertyBag->IsObject()) |
| 129 { |
| 130 multilingualPropertyBag = jsEngine->NewObject(); |
| 131 multilingualPropertyBag->SetProperty(locale, value); |
| 132 SetProperty(propertyName, multilingualPropertyBag); |
| 133 } |
| 134 else |
| 135 { |
| 136 multilingualPropertyBag->SetProperty(locale, value); |
| 137 } |
| 138 } |
| 139 } |
| 140 |
| 141 std::vector<std::string> Notification::GetUrlFilters() const |
| 142 { |
| 143 std::vector<std::string> retValue; |
| 144 auto jsUrlFilters = GetProperty("urlFilters"); |
| 145 if (!jsUrlFilters || !jsUrlFilters->IsArray()) |
| 146 { |
| 147 return retValue; |
| 148 } |
| 149 auto urlFiltersList = jsUrlFilters->AsList(); |
| 150 for (auto ii_urlFilter = urlFiltersList.begin(); ii_urlFilter != urlFiltersLis
t.end(); ++ii_urlFilter) |
| 151 { |
| 152 if (!*ii_urlFilter) |
| 153 { |
| 154 continue; |
| 155 } |
| 156 retValue.emplace_back((*ii_urlFilter)->AsString()); |
| 157 } |
| 158 return retValue; |
| 159 } |
| 160 |
| 161 void Notification::AddUrlFilter(const std::string& value) |
| 162 { |
| 163 if (value.empty()) |
| 164 { |
| 165 return; |
| 166 } |
| 167 auto jsFilter = jsEngine->NewValue(value); |
| 168 auto jsUrlFilters = GetProperty("urlFilters"); |
| 169 if (jsUrlFilters && jsUrlFilters->IsArray()) |
| 170 { |
| 171 jsUrlFilters->Push(jsFilter); |
| 172 return; |
| 173 } |
| 174 jsUrlFilters = jsEngine->NewArray(); |
| 175 jsUrlFilters->Push(jsFilter); |
| 176 SetProperty("urlFilters", jsUrlFilters); |
| 177 } |
| 178 |
| 179 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg) |
| 180 : JsValue(jsValue) |
| 181 { |
| 182 } |
| 183 |
| 184 std::tr1::shared_ptr<Notification> Notification::JsValueToNotification(const JsV
aluePtr& jsValue) |
| 185 { |
| 186 if(!jsValue || !jsValue->IsObject()) |
| 187 { |
| 188 return std::tr1::shared_ptr<Notification>(); |
| 189 } |
| 190 return std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg()); |
| 191 } |
| 192 |
| 193 NotificationTexts Notification::JsTextsToNotificationTexts(const JsValue& jsText
) |
| 194 { |
| 195 NotificationTexts retValue; |
| 196 auto jsTitle = jsText.GetProperty("title"); |
| 197 if (jsTitle && jsTitle->IsString()) |
| 198 { |
| 199 retValue.title = jsTitle->AsString(); |
| 200 } |
| 201 auto jsMessage = jsText.GetProperty("message"); |
| 202 if (jsMessage && jsMessage->IsString()) |
| 203 { |
| 204 retValue.message = jsMessage->AsString(); |
| 205 } |
| 206 return retValue; |
| 207 } |
OLD | NEW |