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

Side by Side Diff: src/Notification.cpp

Issue 5797488346791936: Issue 1107 - Support notifications (Closed)
Patch Set: fix comment Created Jan. 23, 2015, 3:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/FilterEngine.cpp ('k') | test/Notification.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 notificationTypes = InitNotificationTypes();
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 notificationTypeIterator = std::find_if(
51 notificationTypes.begin(), notificationTypes.end(), findBySecond);
52 if (notificationTypeIterator == notificationTypes.end())
53 {
54 return NotificationType::NOTIFICATION_TYPE_INFORMATION;
55 }
56 return notificationTypeIterator->first;
57 }
58 }
59
60 Notification::Notification(const JsValuePtr& jsValue)
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)
88 {
89 if (!jsValue || !jsValue->IsObject())
90 {
91 return NotificationPtr();
92 }
93
94 NotificationPtr notification(new Notification(jsValue));
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 }
OLDNEW
« no previous file with comments | « src/FilterEngine.cpp ('k') | test/Notification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld