| Left: | ||
| Right: |
| 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 NotificationTypes InitNotificationTypes() | |
| 30 { | |
| 31 NotificationTypes retValue; | |
| 32 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUESTI ON, "question")); | |
| 33 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRITIC AL, "critical")); | |
| 34 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INFORM ATION, "information")); | |
| 35 return retValue; | |
| 36 } | |
| 37 | |
| 38 const NotificationTypes g_notificationTypes = InitNotificationTypes(); | |
|
Felix Dahlke
2015/01/23 13:25:55
No hungarian notation please :) https://adblockplu
sergei
2015/01/23 14:48:19
fixed, although it's not hungarian notation
| |
| 39 | |
| 40 NotificationType StringToNotificationType(const std::string& value) | |
| 41 { | |
| 42 struct IsSecondEqualToPredicate | |
| 43 { | |
| 44 std::string value; | |
| 45 bool operator()(const NotificationTypeString& pair) const | |
| 46 { | |
| 47 return value == pair.second; | |
| 48 } | |
| 49 } findBySecond = {value}; | |
| 50 NotificationTypes::const_iterator ci_notificationType = std::find_if( | |
|
Felix Dahlke
2015/01/23 13:25:55
Hungarian again :)
sergei
2015/01/23 14:48:19
fixed
| |
| 51 g_notificationTypes.begin(), g_notificationTypes.end(), findBySecond); | |
| 52 if (ci_notificationType == g_notificationTypes.end()) | |
| 53 { | |
| 54 return NotificationType::NOTIFICATION_TYPE_INFORMATION; | |
| 55 } | |
| 56 return ci_notificationType->first; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg) | |
| 61 : JsValue(jsValue) | |
| 62 { | |
| 63 } | |
| 64 | |
| 65 NotificationType Notification::GetType() const | |
| 66 { | |
| 67 return type; | |
| 68 } | |
| 69 | |
| 70 const std::string& Notification::GetTitle() const | |
| 71 { | |
| 72 return title; | |
| 73 } | |
| 74 | |
| 75 const std::string& Notification::GetMessageString() const | |
| 76 { | |
| 77 return message; | |
| 78 } | |
| 79 | |
| 80 void Notification::MarkAsShown() | |
| 81 { | |
| 82 JsValueList params; | |
| 83 params.push_back(GetProperty("id")); | |
| 84 jsEngine->Evaluate("API.markNotificationAsShown")->Call(params); | |
| 85 } | |
| 86 | |
| 87 NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue) | |
|
Felix Dahlke
2015/01/23 13:25:55
Why not simply do this in the constructor, as Filt
| |
| 88 { | |
| 89 if(!jsValue || !jsValue->IsObject()) | |
|
Felix Dahlke
2015/01/23 13:25:55
Single space before ( please.
sergei
2015/01/23 14:48:19
fixed
| |
| 90 { | |
| 91 return NotificationPtr(); | |
| 92 } | |
| 93 | |
| 94 NotificationPtr notification = std::tr1::make_shared<Notification>(jsValue, Pr ivateCtrArg()); | |
| 95 JsValuePtr jsType = notification->GetProperty("type"); | |
| 96 notification->type = StringToNotificationType(jsType ? jsType->AsString() : "" ); | |
| 97 | |
| 98 JsValueList params; | |
| 99 params.push_back(notification); | |
| 100 JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts") ; | |
| 101 JsValuePtr jsTexts = func->Call(params); | |
| 102 JsValuePtr jsTitle = jsTexts->GetProperty("title"); | |
| 103 if (jsTitle->IsString()) | |
| 104 { | |
| 105 notification->title = jsTitle->AsString(); | |
| 106 } | |
| 107 JsValuePtr jsMessage = jsTexts->GetProperty("message"); | |
| 108 if (jsMessage->IsString()) | |
| 109 { | |
| 110 notification->message = jsMessage->AsString(); | |
| 111 } | |
| 112 return notification; | |
| 113 } | |
| OLD | NEW |