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 |
| 53 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg) |
| 54 : JsValue(jsValue) |
| 55 { |
| 56 } |
| 57 |
| 58 NotificationType Notification::GetType() const |
| 59 { |
| 60 return type; |
| 61 } |
| 62 |
| 63 const std::string& Notification::GetTitle() const |
| 64 { |
| 65 return title; |
| 66 } |
| 67 |
| 68 const std::string& Notification::GetMessageString() const |
| 69 { |
| 70 return message; |
| 71 } |
| 72 |
| 73 void Notification::MarkAsShown() |
| 74 { |
| 75 JsValueList params; |
| 76 params.push_back(jsEngine->NewValue(GetProperty("id")->AsString())); |
| 77 jsEngine->Evaluate("API.markNotificationAsShown")->Call(params); |
| 78 } |
| 79 |
| 80 NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue) |
| 81 { |
| 82 if(!jsValue || !jsValue->IsObject()) |
| 83 { |
| 84 return NotificationPtr(); |
| 85 } |
| 86 |
| 87 auto notification = std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg
()); |
| 88 auto jsType = notification->GetProperty("type"); |
| 89 notification->type = StringToNotificationType(jsType ? jsType->AsString() : ""
); |
| 90 |
| 91 JsValueList params; |
| 92 params.push_back(notification); |
| 93 JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts")
; |
| 94 auto jsTexts = func->Call(params); |
| 95 auto jsTitle = jsTexts->GetProperty("title"); |
| 96 if (jsTitle->IsString()) |
| 97 { |
| 98 notification->title = jsTitle->AsString(); |
| 99 } |
| 100 auto jsMessage = jsTexts->GetProperty("message"); |
| 101 if (jsMessage->IsString()) |
| 102 { |
| 103 notification->message = jsMessage->AsString(); |
| 104 } |
| 105 return notification; |
| 106 } |
OLD | NEW |