1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
#include "mwm_url.hpp"
#include "map/api_mark_point.hpp"
#include "map/bookmark_manager.hpp"
#include "geometry/mercator.hpp"
#include "indexer/scales.hpp"
#include "drape_frontend/visual_params.hpp"
#include "platform/marketing_service.hpp"
#include "platform/settings.hpp"
#include "coding/uri.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include "std/algorithm.hpp"
#include "std/bind.hpp"
#include <array>
namespace url_scheme
{
namespace lead
{
char const * kFrom = marketing::kFrom;
char const * kType = marketing::kType;
char const * kName = marketing::kName;
char const * kContent = marketing::kContent;
char const * kKeyword = marketing::kKeyword;
struct CampaignDescription
{
void Write() const
{
if (!IsValid())
{
LOG(LERROR, ("Invalid campaign description"));
return;
}
marketing::Settings::Set(kFrom, m_from);
marketing::Settings::Set(kType, m_type);
marketing::Settings::Set(kName, m_name);
if (!m_content.empty())
marketing::Settings::Set(kContent, m_content);
if (!m_keyword.empty())
marketing::Settings::Set(kKeyword, m_keyword);
}
bool IsValid() const { return !m_from.empty() && !m_type.empty() && !m_name.empty(); }
string m_from;
string m_type;
string m_name;
string m_content;
string m_keyword;
};
} // namespace lead
namespace map
{
char const * kLatLon = "ll";
char const * kZoomLevel = "z";
char const * kName = "n";
char const * kId = "id";
char const * kStyle = "s";
char const * kBackUrl = "backurl";
char const * kVersion = "v";
char const * kAppName = "appname";
char const * kBalloonAction = "balloonaction";
} // namespace map
namespace route
{
char const * kSourceLatLon = "sll";
char const * kDestLatLon = "dll";
char const * kSourceName = "saddr";
char const * kDestName = "daddr";
char const * kRouteType = "type";
char const * kRouteTypeVehicle = "vehicle";
char const * kRouteTypePedestrian = "pedestrian";
char const * kRouteTypeBicycle = "bicycle";
char const * kRouteTypeTransit = "transit";
} // namespace route
namespace search
{
char const * kQuery = "query";
char const * kCenterLatLon = "cll";
char const * kLocale = "locale";
char const * kSearchOnMap = "map";
} // namespace search
namespace catalogue
{
char const * kId = "id";
char const * kName = "name";
}
namespace cataloguePath
{
char const * kUrl = "url";
}
namespace
{
enum class ApiURLType
{
Incorrect,
Map,
Route,
Search,
Lead,
Catalogue,
CataloguePath
};
std::array<std::string, 3> const kAvailableSchemes = {{"mapswithme", "mwm", "mapsme"}};
ApiURLType URLType(Uri const & uri)
{
if (std::find(kAvailableSchemes.begin(), kAvailableSchemes.end(), uri.GetScheme()) == kAvailableSchemes.end())
return ApiURLType::Incorrect;
auto const path = uri.GetPath();
if (path == "map")
return ApiURLType::Map;
if (path == "route")
return ApiURLType::Route;
if (path == "search")
return ApiURLType::Search;
if (path == "lead")
return ApiURLType::Lead;
if (path == "catalogue")
return ApiURLType::Catalogue;
if (path == "guides_page")
return ApiURLType::CataloguePath;
return ApiURLType::Incorrect;
}
bool ParseLatLon(string const & key, string const & value, double & lat, double & lon)
{
size_t const firstComma = value.find(',');
if (firstComma == string::npos)
{
LOG(LWARNING, ("Map API: no comma between lat and lon for key:", key, " value:", value));
return false;
}
if (!strings::to_double(value.substr(0, firstComma), lat) ||
!strings::to_double(value.substr(firstComma + 1), lon))
{
LOG(LWARNING, ("Map API: can't parse lat,lon for key:", key, " value:", value));
return false;
}
if (!MercatorBounds::ValidLat(lat) || !MercatorBounds::ValidLon(lon))
{
LOG(LWARNING, ("Map API: incorrect value for lat and/or lon", key, value, lat, lon));
return false;
}
return true;
}
} // namespace
void ParsedMapApi::SetBookmarkManager(BookmarkManager * manager)
{
m_bmManager = manager;
}
ParsedMapApi::ParsingResult ParsedMapApi::SetUriAndParse(string const & url)
{
Reset();
if (!strings::StartsWith(url, "mapswithme://") && !strings::StartsWith(url, "mwm://") &&
!strings::StartsWith(url, "mapsme://"))
{
return ParsingResult::Incorrect;
}
ParsingResult const res = Parse(url_scheme::Uri(url));
m_isValid = res != ParsingResult::Incorrect;
return res;
}
ParsedMapApi::ParsingResult ParsedMapApi::Parse(Uri const & uri)
{
switch (URLType(uri))
{
case ApiURLType::Incorrect:
return ParsingResult::Incorrect;
case ApiURLType::Map:
{
vector<ApiPoint> points;
auto const result = uri.ForEachKeyValue([&points, this](string const & key, string const & value)
{
return AddKeyValue(key, value, points);
});
if (!result)
return ParsingResult::Incorrect;
if (points.empty())
return ParsingResult::Incorrect;
ASSERT(m_bmManager != nullptr, ());
auto editSession = m_bmManager->GetEditSession();
for (auto const & p : points)
{
m2::PointD glPoint(MercatorBounds::FromLatLon(p.m_lat, p.m_lon));
auto * mark = editSession.CreateUserMark<ApiMarkPoint>(glPoint);
mark->SetName(p.m_name);
mark->SetApiID(p.m_id);
mark->SetStyle(style::GetSupportedStyle(p.m_style));
}
return ParsingResult::Map;
}
case ApiURLType::Route:
{
m_routePoints.clear();
using namespace route;
vector<string> pattern{kSourceLatLon, kSourceName, kDestLatLon, kDestName, kRouteType};
auto const result = uri.ForEachKeyValue([&pattern, this](string const & key, string const & value)
{
return RouteKeyValue(key, value, pattern);
});
if (!result)
return ParsingResult::Incorrect;
if (pattern.size() != 0)
return ParsingResult::Incorrect;
if (m_routePoints.size() != 2)
{
ASSERT(false, ());
return ParsingResult::Incorrect;
}
return ParsingResult::Route;
}
case ApiURLType::Search:
{
SearchRequest request;
auto const result = uri.ForEachKeyValue([&request, this](string const & key, string const & value)
{
return SearchKeyValue(key, value, request);
});
if (!result)
return ParsingResult::Incorrect;
m_request = request;
return request.m_query.empty() ? ParsingResult::Incorrect : ParsingResult::Search;
}
case ApiURLType::Lead:
{
lead::CampaignDescription description;
auto result = uri.ForEachKeyValue([&description, this](string const & key, string const & value)
{
return LeadKeyValue(key, value, description);
});
if (!result)
return ParsingResult::Incorrect;
if (!description.IsValid())
return ParsingResult::Incorrect;
description.Write();
return ParsingResult::Lead;
}
case ApiURLType::Catalogue:
{
CatalogItem item;
auto const result = uri.ForEachKeyValue([&item, this](string const & key, string const & value)
{
return CatalogKeyValue(key, value, item);
});
if (!result)
return ParsingResult::Incorrect;
if (item.m_id.empty())
return ParsingResult::Incorrect;
m_catalogItem = item;
return ParsingResult::Catalogue;
}
case ApiURLType::CataloguePath:
{
CatalogPathItem item;
auto const result = uri.ForEachKeyValue([&item, this](string const & key, string const & value)
{
return CatalogPathKeyValue(key, value, item);
});
if (!result)
return ParsingResult::Incorrect;
if (item.m_url.empty())
return ParsingResult::Incorrect;
m_catalogPathItem = item;
return ParsingResult::CataloguePath;
}
}
UNREACHABLE();
}
bool ParsedMapApi::RouteKeyValue(string const & key, string const & value, vector<string> & pattern)
{
using namespace route;
if (pattern.empty() || key != pattern.front())
return false;
if (key == kSourceLatLon || key == kDestLatLon)
{
double lat = 0.0;
double lon = 0.0;
if (!ParseLatLon(key, value, lat, lon))
return false;
RoutePoint p;
p.m_org = MercatorBounds::FromLatLon(lat, lon);
m_routePoints.push_back(p);
}
else if (key == kSourceName || key == kDestName)
{
m_routePoints.back().m_name = value;
}
else if (key == kRouteType)
{
string const lowerValue = strings::MakeLowerCase(value);
if (lowerValue == kRouteTypePedestrian || lowerValue == kRouteTypeVehicle ||
lowerValue == kRouteTypeBicycle || lowerValue == kRouteTypeTransit)
{
m_routingType = lowerValue;
}
else
{
LOG(LWARNING, ("Incorrect routing type:", value));
return false;
}
}
pattern.erase(pattern.begin());
return true;
}
bool ParsedMapApi::AddKeyValue(string const & key, string const & value, vector<ApiPoint> & points)
{
using namespace map;
if (key == kLatLon)
{
double lat = 0.0;
double lon = 0.0;
if (!ParseLatLon(key, value, lat, lon))
return false;
ApiPoint pt{.m_lat = lat, .m_lon = lon};
points.push_back(pt);
}
else if (key == kZoomLevel)
{
if (!strings::to_double(value, m_zoomLevel))
m_zoomLevel = 0.0;
}
else if (key == kName)
{
if (!points.empty())
{
points.back().m_name = value;
}
else
{
LOG(LWARNING, ("Map API: Point name with no point. 'll' should come first!"));
return false;
}
}
else if (key == kId)
{
if (!points.empty())
{
points.back().m_id = value;
}
else
{
LOG(LWARNING, ("Map API: Point url with no point. 'll' should come first!"));
return false;
}
}
else if (key == kStyle)
{
if (!points.empty())
{
points.back().m_style = value;
}
else
{
LOG(LWARNING, ("Map API: Point style with no point. 'll' should come first!"));
return false;
}
}
else if (key == kBackUrl)
{
// Fix missing :// in back url, it's important for iOS
if (value.find("://") == string::npos)
m_globalBackUrl = value + "://";
else
m_globalBackUrl = value;
}
else if (key == kVersion)
{
if (!strings::to_int(value, m_version))
m_version = 0;
}
else if (key == kAppName)
{
m_appTitle = value;
}
else if (key == kBalloonAction)
{
m_goBackOnBalloonClick = true;
}
return true;
}
bool ParsedMapApi::SearchKeyValue(string const & key, string const & value, SearchRequest & request) const
{
using namespace search;
if (key == kQuery)
{
if (value.empty())
return false;
request.m_query = value;
}
else if (key == kCenterLatLon)
{
double lat = 0.0;
double lon = 0.0;
if (ParseLatLon(key, value, lat, lon))
{
request.m_centerLat = lat;
request.m_centerLon = lon;
}
}
else if (key == kLocale)
{
request.m_locale = value;
}
else if (key == kSearchOnMap)
{
request.m_isSearchOnMap = true;
}
return true;
}
bool ParsedMapApi::LeadKeyValue(string const & key, string const & value, lead::CampaignDescription & description) const
{
using namespace lead;
if (key == kFrom)
description.m_from = value;
else if (key == kType)
description.m_type = value;
else if (key == kName)
description.m_name = value;
else if (key == kContent)
description.m_content = value;
else if (key == kKeyword)
description.m_keyword = value;
/*
We have to support parsing the uri which contains unregistred parameters.
*/
return true;
}
bool ParsedMapApi::CatalogKeyValue(string const & key, string const & value, CatalogItem & item) const
{
using namespace catalogue;
if (key == kName)
item.m_name = value;
else if (key == kId)
item.m_id = value;
return true;
}
bool ParsedMapApi::CatalogPathKeyValue(string const & key, string const & value, CatalogPathItem & item) const
{
using namespace cataloguePath;
if (key == kUrl)
item.m_url = value;
return true;
}
void ParsedMapApi::Reset()
{
m_globalBackUrl.clear();
m_appTitle.clear();
m_version = 0;
m_zoomLevel = 0.0;
m_goBackOnBalloonClick = false;
}
bool ParsedMapApi::GetViewportRect(m2::RectD & rect) const
{
ASSERT(m_bmManager != nullptr, ());
auto const & markIds = m_bmManager->GetUserMarkIds(UserMark::Type::API);
if (markIds.size() == 1 && m_zoomLevel >= 1)
{
double zoom = min(static_cast<double>(scales::GetUpperComfortScale()), m_zoomLevel);
rect = df::GetRectForDrawScale(zoom, m_bmManager->GetUserMark(*markIds.begin())->GetPivot());
return true;
}
else
{
m2::RectD result;
for (auto markId : markIds)
result.Add(m_bmManager->GetUserMark(markId)->GetPivot());
if (result.IsValid())
{
rect = result;
return true;
}
return false;
}
}
ApiMarkPoint const * ParsedMapApi::GetSinglePoint() const
{
ASSERT(m_bmManager != nullptr, ());
auto const & markIds = m_bmManager->GetUserMarkIds(UserMark::Type::API);
if (markIds.size() != 1)
return nullptr;
return static_cast<ApiMarkPoint const *>(m_bmManager->GetUserMark(*markIds.begin()));
}
}
|