Welcome to mirror list, hosted at ThFree Co, Russian Federation.

settings.cpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbeccacf1157acba370182238eaa43694fecbbe6 (plain)
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
#include "platform/settings.hpp"
#include "platform/location.hpp"
#include "platform/platform.hpp"

#include "defines.hpp"

#include "coding/reader_streambuf.hpp"
#include "coding/file_writer.hpp"
#include "coding/file_reader.hpp"

#include "geometry/rect2d.hpp"
#include "geometry/any_rect2d.hpp"

#include "base/logging.hpp"

#include "std/cmath.hpp"
#include "std/iostream.hpp"
#include "std/sstream.hpp"

namespace
{
constexpr char kDelimChar = '=';
}  // namespace

namespace settings
{
char const * kLocationStateMode = "LastLocationStateMode";
char const * kMeasurementUnits = "Units";

StringStorage::StringStorage()
{
  try
  {
    string settingsPath = GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME);
    LOG(LINFO, ("Settings path:", settingsPath));
    ReaderStreamBuf buffer(make_unique<FileReader>(settingsPath));
    istream stream(&buffer);

    string line;
    while (getline(stream, line))
    {
      if (line.empty())
        continue;

      size_t const delimPos = line.find(kDelimChar);
      if (delimPos == string::npos)
        continue;

      string const key = line.substr(0, delimPos);
      string const value = line.substr(delimPos + 1);
      if (!key.empty() && !value.empty())
        m_values[key] = value;
    }
  }
  catch (RootException const & ex)
  {
    LOG(LWARNING, ("Loading settings:", ex.Msg()));
  }
}

void StringStorage::Save() const
{
  try
  {
    FileWriter file(GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME));
    for (auto const & value : m_values)
    {
      string line(value.first);
      line += kDelimChar;
      line += value.second;
      line += '\n';
      file.Write(line.data(), line.size());
    }
  }
  catch (RootException const & ex)
  {
    // Ignore all settings saving exceptions.
    LOG(LWARNING, ("Saving settings:", ex.Msg()));
  }
}

StringStorage & StringStorage::Instance()
{
  static StringStorage inst;
  return inst;
}

void StringStorage::Clear()
{
  lock_guard<mutex> guard(m_mutex);
  m_values.clear();
  Save();
}

bool StringStorage::GetValue(string const & key, string & outValue) const
{
  lock_guard<mutex> guard(m_mutex);

  auto const found = m_values.find(key);
  if (found == m_values.end())
    return false;

  outValue = found->second;
  return true;
}

void StringStorage::SetValue(string const & key, string && value)
{
  lock_guard<mutex> guard(m_mutex);

  m_values[key] = move(value);
  Save();
}

void StringStorage::DeleteKeyAndValue(string const & key)
{
  lock_guard<mutex> guard(m_mutex);

  auto const found = m_values.find(key);
  if (found != m_values.end())
  {
    m_values.erase(found);
    Save();
  }
}

////////////////////////////////////////////////////////////////////////////////////////////

template <>
string ToString<string>(string const & str)
{
  return str;
}

template <>
bool FromString<string>(string const & strIn, string & strOut)
{
  strOut = strIn;
  return true;
}

namespace impl
{
template <class T, size_t N>
bool FromStringArray(string const & s, T(&arr)[N])
{
  istringstream in(s);
  size_t count = 0;
  while (count < N && in >> arr[count])
  {
    if (!isfinite(arr[count]))
      return false;
    ++count;
  }

  return (!in.fail() && count == N);
}
}  // namespace impl

template <>
string ToString<m2::AnyRectD>(m2::AnyRectD const & rect)
{
  ostringstream out;
  out.precision(12);
  m2::PointD glbZero(rect.GlobalZero());
  out << glbZero.x << " " << glbZero.y << " ";
  out << rect.Angle().val() << " ";
  m2::RectD const & r = rect.GetLocalRect();
  out << r.minX() << " " << r.minY() << " " << r.maxX() << " " << r.maxY();
  return out.str();
}

template <>
bool FromString<m2::AnyRectD>(string const & str, m2::AnyRectD & rect)
{
  double val[7];
  if (!impl::FromStringArray(str, val))
    return false;

  // Will get an assertion in DEBUG and false return in RELEASE.
  m2::RectD const r(val[3], val[4], val[5], val[6]);
  if (!r.IsValid())
    return false;

  rect = m2::AnyRectD(m2::PointD(val[0], val[1]), ang::AngleD(val[2]), r);
  return true;
}

template <>
string ToString<m2::RectD>(m2::RectD const & rect)
{
  ostringstream stream;
  stream.precision(12);
  stream << rect.minX() << " " << rect.minY() << " " << rect.maxX() << " " << rect.maxY();
  return stream.str();
}
template <>
bool FromString<m2::RectD>(string const & str, m2::RectD & rect)
{
  double val[4];
  if (!impl::FromStringArray(str, val))
    return false;

  // Will get an assertion in DEBUG and false return in RELEASE.
  rect = m2::RectD(val[0], val[1], val[2], val[3]);
  return rect.IsValid();
}

template <>
string ToString<bool>(bool const & v)
{
  return v ? "true" : "false";
}

template <>
bool FromString<bool>(string const & str, bool & v)
{
  if (str == "true")
    v = true;
  else if (str == "false")
    v = false;
  else
    return false;
  return true;
}

namespace impl
{
template <typename T>
string ToStringScalar(T const & v)
{
  ostringstream stream;
  stream.precision(12);
  stream << v;
  return stream.str();
}

template <typename T>
bool FromStringScalar(string const & str, T & v)
{
  istringstream stream(str);
  if (stream)
  {
    stream >> v;
    return !stream.fail();
  }
  else
    return false;
}
}  // namespace impl

template <>
string ToString<double>(double const & v)
{
  return impl::ToStringScalar<double>(v);
}

template <>
bool FromString<double>(string const & str, double & v)
{
  return impl::FromStringScalar<double>(str, v);
}

template <>
string ToString<int32_t>(int32_t const & v)
{
  return impl::ToStringScalar<int32_t>(v);
}

template <>
bool FromString<int32_t>(string const & str, int32_t & v)
{
  return impl::FromStringScalar<int32_t>(str, v);
}

template <>
string ToString<int64_t>(int64_t const & v)
{
  return impl::ToStringScalar<int64_t>(v);
}

template <>
bool FromString<int64_t>(string const & str, int64_t & v)
{
  return impl::FromStringScalar<int64_t>(str, v);
}

template <>
string ToString<uint32_t>(uint32_t const & v)
{
  return impl::ToStringScalar<uint32_t>(v);
}

template <>
string ToString<uint64_t>(uint64_t const & v)
{
  return impl::ToStringScalar<uint64_t>(v);
}

template <>
bool FromString<uint32_t>(string const & str, uint32_t & v)
{
  return impl::FromStringScalar<uint32_t>(str, v);
}

template <>
bool FromString<uint64_t>(string const & str, uint64_t & v)
{
  return impl::FromStringScalar<uint64_t>(str, v);
}

namespace impl
{
template <class TPair>
string ToStringPair(TPair const & value)
{
  ostringstream stream;
  stream.precision(12);
  stream << value.first << " " << value.second;
  return stream.str();
}

template <class TPair>
bool FromStringPair(string const & str, TPair & value)
{
  istringstream stream(str);
  if (stream)
  {
    stream >> value.first;
    if (stream)
    {
      stream >> value.second;
      return !stream.fail();
    }
  }
  return false;
}
}  // namespace impl

typedef pair<int, int> IPairT;
typedef pair<double, double> DPairT;

template <>
string ToString<IPairT>(IPairT const & v)
{
  return impl::ToStringPair(v);
}

template <>
bool FromString<IPairT>(string const & s, IPairT & v)
{
  return impl::FromStringPair(s, v);
}

template <>
string ToString<DPairT>(DPairT const & v)
{
  return impl::ToStringPair(v);
}

template <>
bool FromString<DPairT>(string const & s, DPairT & v)
{
  return impl::FromStringPair(s, v);
}

template <>
string ToString<measurement_utils::Units>(measurement_utils::Units const & v)
{
  switch (v)
  {
  // The value "Foot" is left here for compatibility with old settings.ini files.
  case measurement_utils::Units::Imperial: return "Foot";
  case measurement_utils::Units::Metric: return "Metric";
  }
}

template <>
bool FromString<measurement_utils::Units>(string const & s, measurement_utils::Units & v)
{
  if (s == "Metric")
    v = measurement_utils::Units::Metric;
  else if (s == "Foot")
    v = measurement_utils::Units::Imperial;
  else
    return false;

  return true;
}

template <>
string ToString<location::EMyPositionMode>(location::EMyPositionMode const & v)
{
  switch (v)
  {
  case location::PendingPosition: return "PendingPosition";
  case location::NotFollow: return "NotFollow";
  case location::NotFollowNoPosition: return "NotFollowNoPosition";
  case location::Follow: return "Follow";
  case location::FollowAndRotate: return "FollowAndRotate";
  default: return "Pending";
  }
}

template <>
bool FromString<location::EMyPositionMode>(string const & s, location::EMyPositionMode & v)
{
  if (s == "PendingPosition")
    v = location::PendingPosition;
  else if (s == "NotFollow")
    v = location::NotFollow;
  else if (s == "NotFollowNoPosition")
    v = location::NotFollowNoPosition;
  else if (s == "Follow")
    v = location::Follow;
  else if (s == "FollowAndRotate")
    v = location::FollowAndRotate;
  else
    return false;

  return true;
}

bool IsFirstLaunchForDate(int date)
{
  constexpr char const * kFirstLaunchKey = "FirstLaunchOnDate";
  int savedDate;
  if (!Get(kFirstLaunchKey, savedDate) || savedDate < date)
  {
    Set(kFirstLaunchKey, date);
    return true;
  }
  else
    return false;
}
}  // namespace settings