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: Created Jan. 19, 2015, 12:50 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)
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(NotificationType type, const std::string& id, const J sValuePtr& jsValue, PrivateCtrArg)
68 : JsValue(jsValue)
69 {
70 SetProperty("type", NotificationTypeToString(type));
71 SetProperty("id", id);
72 }
73
74 std::string Notification::GetId() const
75 {
76 auto jsValue = GetProperty("id");
77 return jsValue ? jsValue->AsString() : "";
78 }
79
80 NotificationType Notification::GetType() const
81 {
82 auto jsValue = GetProperty("type");
83 return StringToNotificationType(jsValue ? GetProperty("type")->AsString() : "" );
84 }
85
86 void Notification::SetTitle(const std::string& value, const std::string& locale)
87 {
88 SetMultilingualProperty("title", value, locale);
89 }
90
91 void Notification::SetMessage(const std::string& value, const std::string& local e)
92 {
93 SetMultilingualProperty("message", value, locale);
94 }
95
96 void Notification::SetMultilingualProperty(const std::string& propertyName,
97 const std::string& value,
98 const std::string& locale)
99 {
100 if (locale.empty())
101 {
102 SetProperty(propertyName, value);
103 }
104 else
105 {
106 auto multilingualPropertyBag = GetProperty(propertyName);
107 if (!multilingualPropertyBag || !multilingualPropertyBag->IsObject())
108 {
109 multilingualPropertyBag = jsEngine->NewObject();
110 multilingualPropertyBag->SetProperty(locale, value);
111 SetProperty(propertyName, multilingualPropertyBag);
112 }
113 else
114 {
115 multilingualPropertyBag->SetProperty(locale, value);
116 }
117 }
118 }
119
120 std::vector<std::string> Notification::GetUrlFilters() const
121 {
122 std::vector<std::string> retValue;
123 auto jsUrlFilters = GetProperty("urlFilters");
124 if (!jsUrlFilters || !jsUrlFilters->IsArray())
125 {
126 return retValue;
127 }
128 auto urlFiltersList = jsUrlFilters->AsList();
129 for (auto ii_urlFilter = urlFiltersList.begin(); ii_urlFilter != urlFiltersLis t.end(); ++ii_urlFilter)
130 {
131 if (!*ii_urlFilter)
132 {
133 continue;
134 }
135 retValue.emplace_back((*ii_urlFilter)->AsString());
136 }
137 return retValue;
138 }
139
140 void Notification::AddUrlFilter(const std::string& value)
141 {
142 if (value.empty())
143 {
144 return;
145 }
146 auto jsFilter = jsEngine->NewValue(value);
147 auto jsUrlFilters = GetProperty("urlFilters");
148 if (jsUrlFilters && jsUrlFilters->IsArray())
149 {
150 jsUrlFilters->Push(jsFilter);
151 return;
152 }
153 jsUrlFilters = jsEngine->NewArray();
154 jsUrlFilters->Push(jsFilter);
155 SetProperty("urlFilters", jsUrlFilters);
156 }
157
158 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg)
159 : JsValue(jsValue)
160 {
161 }
162
163 std::tr1::shared_ptr<Notification> Notification::JsValueToNotification(const JsV aluePtr& jsValue)
164 {
165 if(!jsValue || !jsValue->IsObject())
166 {
167 return std::tr1::shared_ptr<Notification>();
168 }
169 return std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg());
170 }
171
172 NotificationTexts Notification::JsTextsToNotificationTexts(const JsValue& jsText )
173 {
174 NotificationTexts retValue;
175 auto jsTitle = jsText.GetProperty("title");
176 if (jsTitle && jsTitle->IsString())
177 {
178 retValue.title = jsTitle->AsString();
179 }
180 auto jsMessage = jsText.GetProperty("message");
181 if (jsMessage && jsMessage->IsString())
182 {
183 retValue.message = jsMessage->AsString();
184 }
185 return retValue;
186 }
OLDNEW
« include/AdblockPlus/FilterEngine.h ('K') | « src/JsValue.cpp ('k') | test/Notification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld