Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/Notification.cpp

Issue 5797488346791936: Issue 1107 - Support notifications (Closed)
Patch Set: C++03 Created Jan. 23, 2015, 10:52 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/Notification.cpp
diff --git a/src/Notification.cpp b/src/Notification.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..48009fca2ea193b5ab1a501737218d35cbfe2c6b
--- /dev/null
+++ b/src/Notification.cpp
@@ -0,0 +1,113 @@
+/*
+ * 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;
+ NotificationTypes InitNotificationTypes()
+ {
+ NotificationTypes retValue;
+ retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUESTION, "question"));
+ retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRITICAL, "critical"));
+ retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INFORMATION, "information"));
+ return retValue;
+ }
+
+ 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
+
+ NotificationType StringToNotificationType(const std::string& value)
+ {
+ struct IsSecondEqualToPredicate
+ {
+ std::string value;
+ bool operator()(const NotificationTypeString& pair) const
+ {
+ return value == pair.second;
+ }
+ } findBySecond = {value};
+ 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
+ g_notificationTypes.begin(), g_notificationTypes.end(), findBySecond);
+ if (ci_notificationType == g_notificationTypes.end())
+ {
+ return NotificationType::NOTIFICATION_TYPE_INFORMATION;
+ }
+ return ci_notificationType->first;
+ }
+}
+
+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()
+{
+ JsValueList params;
+ params.push_back(GetProperty("id"));
+ jsEngine->Evaluate("API.markNotificationAsShown")->Call(params);
+}
+
+NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue)
Felix Dahlke 2015/01/23 13:25:55 Why not simply do this in the constructor, as Filt
+{
+ if(!jsValue || !jsValue->IsObject())
Felix Dahlke 2015/01/23 13:25:55 Single space before ( please.
sergei 2015/01/23 14:48:19 fixed
+ {
+ return NotificationPtr();
+ }
+
+ NotificationPtr notification = std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg());
+ JsValuePtr jsType = notification->GetProperty("type");
+ notification->type = StringToNotificationType(jsType ? jsType->AsString() : "");
+
+ JsValueList params;
+ params.push_back(notification);
+ JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts");
+ JsValuePtr jsTexts = func->Call(params);
+ JsValuePtr jsTitle = jsTexts->GetProperty("title");
+ if (jsTitle->IsString())
+ {
+ notification->title = jsTitle->AsString();
+ }
+ JsValuePtr jsMessage = jsTexts->GetProperty("message");
+ if (jsMessage->IsString())
+ {
+ notification->message = jsMessage->AsString();
+ }
+ return notification;
+}

Powered by Google App Engine
This is Rietveld