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: remove local Notifications Created Jan. 22, 2015, 2:01 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
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 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 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
53 {
54 auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notif icationTypes.end(),
55 [&value](const NotificationTypeString& pair)->bool
56 {
57 return value == pair.first;
58 });
59 if (ii_notificationType == g_notificationTypes.end())
60 {
61 return "information";
62 }
63 return ii_notificationType->second;
64 }
65 }
66
67 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg)
68 : JsValue(jsValue)
69 {
70 }
71
72 NotificationType Notification::GetType() const
73 {
74 return type;
75 }
76
77 const std::string& Notification::GetTitle() const
78 {
79 return title;
80 }
81
82 const std::string& Notification::GetMessageString() const
83 {
84 return message;
85 }
86
87 void Notification::MarkAsShown()
88 {
89 JsValuePtr func = jsEngine->Evaluate("API.markNotificationAsShown");
90 if (!func)
91 {
92 return;
93 }
94 JsValueList params;
95 auto jsId = GetProperty("id");
96 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
97 func->Call(params);
98 }
99
100 NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue)
101 {
102 if(!jsValue || !jsValue->IsObject())
103 {
104 return NotificationPtr();
105 }
106
107 auto notification = std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg ());
108 auto jsType = notification->GetProperty("type");
109 notification->type = StringToNotificationType(jsType ? jsType->AsString() : "" );
110
111 JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts") ;
112 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.
113 {
114 return notification;
115 }
116 JsValueList params;
117 params.push_back(notification);
118 auto jsTexts = func->Call(params);
119 if (!jsTexts)
Wladimir Palant 2015/01/22 15:19:51 Here again a pointless check - JSValue::Call() wil
120 {
121 return notification;
122 }
123
124 auto jsTitle = jsTexts->GetProperty("title");
125 if (jsTitle && jsTitle->IsString())
Wladimir Palant 2015/01/22 15:19:51 Checking whether jsTitle is true is pointless (sam
126 {
127 notification->title = jsTitle->AsString();
128 }
129 auto jsMessage = jsTexts->GetProperty("message");
130 if (jsMessage && jsMessage->IsString())
131 {
132 notification->message = jsMessage->AsString();
133 }
134 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
135 }
OLDNEW
« src/JsValue.cpp ('K') | « src/JsValue.cpp ('k') | test/Notification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld