forked from mail-ru-im/im-desktop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui_settings.cpp
426 lines (333 loc) · 13.5 KB
/
gui_settings.cpp
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
#include "stdafx.h"
#include <cstring>
#include "gui_settings.h"
#include "core_dispatcher.h"
#include "utils/gui_coll_helper.h"
#include "utils/InterConnector.h"
#include "my_info.h"
#include "../common.shared/config/config.h"
namespace
{
QString DefaultDownloadsPath()
{
return QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
}
}
namespace Ui
{
qt_gui_settings::qt_gui_settings()
: shadowWidth_(0)
, isLoaded_(false)
{
}
template<> void qt_gui_settings::set_value<QString>(const QString& _name, const QString& _value)
{
const QByteArray arr = _value.toUtf8();
set_value_simple_data(_name, arr.data(), arr.size() + 1);
}
template<> QString qt_gui_settings::get_value<QString>(QStringView _name, const QString& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
return QString::fromUtf8(&data[0]);
}
template<> void qt_gui_settings::set_value<qt_gui_settings::VoipCallsCountMap>(const QString& _name, const qt_gui_settings::VoipCallsCountMap& _value)
{
QByteArray arr;
QDataStream ds(&arr, QIODevice::WriteOnly);
for (auto [k, v] : _value)
{
ds << k.toStdString().c_str();
ds << v;
}
set_value_simple_data(_name, arr.constData(), arr.size());
}
template<> void qt_gui_settings::set_value<std::map<int64_t, int64_t>>(const QString& _name, const std::map<int64_t, int64_t>& _value)
{
QByteArray arr;
QDataStream ds(&arr, QIODevice::WriteOnly);
for (auto [k, v] : _value)
ds << static_cast<qint64>(k) << static_cast<qint64>(v);
set_value_simple_data(_name, arr.constData(), arr.size());
}
template<> qt_gui_settings::VoipCallsCountMap qt_gui_settings::get_value<qt_gui_settings::VoipCallsCountMap>(QStringView _name, const qt_gui_settings::VoipCallsCountMap& _default) const
{
qt_gui_settings::VoipCallsCountMap result;
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _default;
QByteArray arr(&data[0], data.size());
QDataStream ds(arr);
while (!ds.atEnd())
{
char* str; VoipCallsCountMap::mapped_type v;
ds >> str;
if (!str || !std::strlen(str))
{
assert(!"broken VoipCallsCountMap settings");
break;
}
ds >> v;
result.emplace(QString::fromStdString(std::string(str)), v);
delete str;
}
return result;
}
template<> std::map<int64_t, int64_t> Ui::qt_gui_settings::get_value<std::map<int64_t, int64_t>>(QStringView _name, const std::map<int64_t, int64_t>& _def) const
{
std::map<int64_t, int64_t> result;
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _def;
QByteArray arr(&data[0], data.size());
QDataStream ds(arr);
while (!ds.atEnd())
{
qint64 k, v;
ds >> k >> v;
if (ds.status() != QDataStream::Ok)
{
assert("int - int map read fail in qt_gui_settings");
return _def;
}
result.emplace(k, v);
}
return result;
}
template<> QString qt_gui_settings::get_value<QString>(const char* _name, const QString& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
return QString::fromUtf8(&data[0]);
}
template<> void qt_gui_settings::set_value<std::string>(const QString& _name, const std::string& _value)
{
set_value_simple_data(_name, _value.c_str(), _value.size());
}
template<> std::string qt_gui_settings::get_value<std::string>(const char* _name, const std::string& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
return std::string(data.begin(), data.end());
}
template<> void qt_gui_settings::set_value<int>(const QString& _name, const int& _value)
{
set_value_simple(_name, _value);
}
template <> int qt_gui_settings::get_value<int>(QStringView _name, const int& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template <> int qt_gui_settings::get_value<int>(const char* _name, const int& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template<> void qt_gui_settings::set_value<double>(const QString& _name, const double& _value)
{
set_value_simple(_name, _value);
}
template <> double qt_gui_settings::get_value<double>(QStringView _name, const double& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template <> double qt_gui_settings::get_value<double>(const char* _name, const double& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template<> void qt_gui_settings::set_value<bool>(const QString& _name, const bool& _value)
{
set_value_simple(_name, _value);
}
template <> bool qt_gui_settings::get_value<bool>(QStringView _name, const bool& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template <> bool qt_gui_settings::get_value<bool>(const char* _name, const bool& _defaultValue) const
{
return get_value_simple(_name, _defaultValue);
}
template<> void qt_gui_settings::set_value<std::vector<int32_t>>(const QString& _name, const std::vector<int32_t>& _value)
{
if (_value.empty())
{
set_value_simple_data(_name, 0, 0);
return;
}
set_value_simple_data(_name, (const char*)&_value[0], (int)_value.size() * sizeof(int32_t));
}
template<> std::vector<int32_t> qt_gui_settings::get_value<std::vector<int32_t>>(QStringView _name, const std::vector<int32_t>& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
if (data.empty())
return _defaultValue;
if ((data.size() % sizeof(int32_t)) != 0)
{
assert(false);
return _defaultValue;
}
std::vector<int32_t> out_data(data.size()/sizeof(int32_t));
::memcpy(&out_data[0], &data[0], data.size());
return out_data;
}
template<> std::vector<int32_t> qt_gui_settings::get_value<std::vector<int32_t>>(const char* _name, const std::vector<int32_t>& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
if (data.empty())
return _defaultValue;
if ((data.size() % sizeof(int32_t)) != 0)
{
assert(false);
return _defaultValue;
}
std::vector<int32_t> out_data(data.size() / sizeof(int32_t));
::memcpy(&out_data[0], &data[0], data.size());
return out_data;
}
template<> void qt_gui_settings::set_value<QRect>(const QString& _name, const QRect& _value)
{
int32_t buffer[4] = {_value.left(), _value.top(), _value.width(), _value.height()};
set_value_simple_data(_name, (const char*) buffer, sizeof(buffer));
}
template<> QRect qt_gui_settings::get_value<QRect>(QStringView _name, const QRect& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
if (data.size() != sizeof(int32_t[4]))
{
assert(false);
return _defaultValue;
}
int32_t* buffer = (int32_t*) &data[0];
return QRect(buffer[0], buffer[1], buffer[2], buffer[3]);
}
template<> QRect qt_gui_settings::get_value<QRect>(const char* _name, const QRect& _defaultValue) const
{
std::vector<char> data;
if (!get_value_simple_data(_name, data))
return _defaultValue;
if (data.size() != sizeof(int32_t[4]))
{
assert(false);
return _defaultValue;
}
int32_t* buffer = (int32_t*)&data[0];
return QRect(buffer[0], buffer[1], buffer[2], buffer[3]);
}
void qt_gui_settings::set_shadow_width(int _width)
{
shadowWidth_ = _width;
}
int qt_gui_settings::get_shadow_width() const
{
return shadowWidth_;
}
int qt_gui_settings::get_current_shadow_width() const
{
return (get_value<bool>(settings_window_maximized, false) == true ? 0 : shadowWidth_);
}
void qt_gui_settings::unserialize(core::coll_helper _collection)
{
core::iarray* values_array = _collection.get_value_as_array("values");
if (!values_array)
{
assert(false);
return;
}
for (int i = 0; i < values_array->size(); ++i)
{
const core::ivalue* val = values_array->get_at(i);
gui_coll_helper coll_val(val->get_as_collection(), false);
core::istream* idata = coll_val.get_value_as_stream("value");
int len = idata->size();
set_value_simple_data(QString::fromUtf8(coll_val.get_value_as_string("name")), (const char*) idata->read(len), len, false);
}
Q_EMIT received();
setIsLoaded(true);
}
void qt_gui_settings::clear()
{
values_.clear();
setIsLoaded(false);
}
void qt_gui_settings::post_value_to_core(const QString& _name, const settings_value& _val) const
{
Ui::gui_coll_helper cl_coll(GetDispatcher()->create_collection(), true);
core::ifptr<core::istream> data_stream(cl_coll->create_stream());
if (_val.data_.size())
data_stream->write((const uint8_t*) &_val.data_[0], (uint32_t)_val.data_.size());
cl_coll.set_value_as_qstring("name", _name);
cl_coll.set_value_as_stream("value", data_stream.get());
GetDispatcher()->post_message_to_core("settings/value/set", cl_coll.get());
}
qt_gui_settings* get_gui_settings()
{
static auto settings = std::make_unique<qt_gui_settings>();
return settings.get();
}
std::string get_account_setting_name(const std::string& _settingName)
{
return MyInfo()->aimId().toStdString() + '/' + _settingName;
}
QString getDownloadPath()
{
checkDownloadPath();
QString result = QDir::toNativeSeparators(Ui::get_gui_settings()->get_value(settings_download_directory, QString()));
return QFileInfo(result.isEmpty() ? DefaultDownloadsPath() : result).canonicalFilePath();// we need canonical path to follow symlinks
}
void setDownloadPath(const QString& _path)
{
Ui::get_gui_settings()->set_value(settings_download_directory, _path);
}
void convertOldDownloadPath()
{
const auto& settings = Ui::get_gui_settings();
if (!settings->contains_value(settings_download_directory_old))
return;
auto value = settings->get_value(settings_download_directory_old, QString());
if (value.isEmpty())
return;
auto oldInfo = QFileInfo(QDir::toNativeSeparators(value));
const auto product = config::get().string(config::values::product_name);
auto possibleOldInfo = QFileInfo(QDir::toNativeSeparators(DefaultDownloadsPath() % ql1c('/') % QString::fromUtf8(product.data(), product.size())));
auto newInfo = QFileInfo(DefaultDownloadsPath());
if (oldInfo == newInfo || oldInfo == possibleOldInfo)
{
settings->set_value(settings_download_directory_old, QString());
return;
}
settings->set_value(settings_download_directory, value);
}
void checkDownloadPath()
{
{
auto downloadPath = QDir::toNativeSeparators(Ui::get_gui_settings()->get_value(settings_download_directory, QString()));
if (!downloadPath.isEmpty() && !QFileInfo::exists(downloadPath))
{
Ui::get_gui_settings()->set_value(settings_download_directory, QString());
Q_EMIT Utils::InterConnector::instance().downloadPathUpdated();
}
}
{
auto downloadPath = QDir::toNativeSeparators(Ui::get_gui_settings()->get_value(settings_download_directory_save_as, QString()));
if (!downloadPath.isEmpty() && !QFileInfo::exists(downloadPath))
Ui::get_gui_settings()->set_value(settings_download_directory_save_as, QString());
}
}
QString getSaveAsPath()
{
QString result = QDir::toNativeSeparators(Ui::get_gui_settings()->get_value(settings_download_directory_save_as, QString()));
return (result.isEmpty() ? getDownloadPath() : result);
}
void setSaveAsPath(const QString& _path)
{
Ui::get_gui_settings()->set_value(settings_download_directory_save_as, _path);
}
}