Index: src/Notification.cpp |
diff --git a/src/Notification.cpp b/src/Notification.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0a2e19b0884655bf3cf9a9e2b02b5bdd2f60f4e3 |
--- /dev/null |
+++ b/src/Notification.cpp |
@@ -0,0 +1,135 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 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 <AdblockPlus/JsValue.h> |
+#include <AdblockPlus/JsEngine.h> |
+#include <AdblockPlus/Notification.h> |
+#include <algorithm> |
+ |
+using namespace AdblockPlus; |
+ |
+namespace |
+{ |
+ typedef std::pair<NotificationType, std::string> NotificationTypeString; |
+ typedef std::vector<NotificationTypeString> NotificationTypes; |
+ const NotificationTypes g_notificationTypes = []()->NotificationTypes |
+ { |
+ NotificationTypes retValue; |
+ retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUESTION, "question")); |
+ retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRITICAL, "critical")); |
+ retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INFORMATION, "information")); |
+ return retValue; |
+ }(); |
+ |
+ NotificationType StringToNotificationType(const std::string& value) |
+ { |
+ auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notificationTypes.end(), |
+ [&value](const NotificationTypeString& pair)->bool |
+ { |
+ return value == pair.second; |
+ }); |
+ if (ii_notificationType == g_notificationTypes.end()) |
+ { |
+ return NotificationType::NOTIFICATION_TYPE_INFORMATION; |
+ } |
+ return ii_notificationType->first; |
+ } |
+ |
+ std::string NotificationTypeToString(NotificationType value) |
Wladimir Palant
2015/01/22 15:19:51
This function seems unused.
sergei
2015/01/22 16:15:11
removed
|
+ { |
+ auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notificationTypes.end(), |
+ [&value](const NotificationTypeString& pair)->bool |
+ { |
+ return value == pair.first; |
+ }); |
+ if (ii_notificationType == g_notificationTypes.end()) |
+ { |
+ return "information"; |
+ } |
+ return ii_notificationType->second; |
+ } |
+} |
+ |
+Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg) |
+ : JsValue(jsValue) |
+{ |
+} |
+ |
+NotificationType Notification::GetType() const |
+{ |
+ return type; |
+} |
+ |
+const std::string& Notification::GetTitle() const |
+{ |
+ return title; |
+} |
+ |
+const std::string& Notification::GetMessageString() const |
+{ |
+ return message; |
+} |
+ |
+void Notification::MarkAsShown() |
+{ |
+ JsValuePtr func = jsEngine->Evaluate("API.markNotificationAsShown"); |
+ if (!func) |
+ { |
+ return; |
+ } |
+ JsValueList params; |
+ auto jsId = GetProperty("id"); |
+ params.push_back(jsEngine->NewValue(jsId ? jsId->AsString() : "")); |
Wladimir Palant
2015/01/22 15:19:51
Does it even make sense to call API.markNotificati
sergei
2015/01/22 16:15:11
Does not make sense. What do you think about passi
Wladimir Palant
2015/01/22 19:16:18
Yes, I think we should just pass in the value of t
|
+ func->Call(params); |
+} |
+ |
+NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue) |
+{ |
+ if(!jsValue || !jsValue->IsObject()) |
+ { |
+ return NotificationPtr(); |
+ } |
+ |
+ auto notification = std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg()); |
+ auto jsType = notification->GetProperty("type"); |
+ notification->type = StringToNotificationType(jsType ? jsType->AsString() : ""); |
+ |
+ JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts"); |
+ if (!func) |
Wladimir Palant
2015/01/22 15:19:51
Here and elsewhere, this kind of check is pointles
sergei
2015/01/22 16:15:11
Clear, removed here and elsewhere.
|
+ { |
+ return notification; |
+ } |
+ JsValueList params; |
+ params.push_back(notification); |
+ auto jsTexts = func->Call(params); |
+ if (!jsTexts) |
Wladimir Palant
2015/01/22 15:19:51
Here again a pointless check - JSValue::Call() wil
|
+ { |
+ return notification; |
+ } |
+ |
+ auto jsTitle = jsTexts->GetProperty("title"); |
+ if (jsTitle && jsTitle->IsString()) |
Wladimir Palant
2015/01/22 15:19:51
Checking whether jsTitle is true is pointless (sam
|
+ { |
+ notification->title = jsTitle->AsString(); |
+ } |
+ auto jsMessage = jsTexts->GetProperty("message"); |
+ if (jsMessage && jsMessage->IsString()) |
+ { |
+ notification->message = jsMessage->AsString(); |
+ } |
+ return notification; |
Wladimir Palant
2015/01/22 15:19:51
Shouldn't all this code be inside the constructor?
sergei
2015/01/22 16:15:11
I would say the current variant is better for pres
Wladimir Palant
2015/01/22 19:16:18
Ok, this makes sense then, at least given my knowl
Felix Dahlke
2015/01/23 15:34:31
I see... Don't think we have the time to really di
|
+} |