2016-08-28 19:16:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-08-28 19:16:23 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/local_storage_box.h"
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2018-08-29 13:23:16 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2018-08-31 13:01:42 +00:00
|
|
|
#include "ui/widgets/continuous_sliders.h"
|
|
|
|
#include "ui/effects/radial_animation.h"
|
2018-10-24 13:29:19 +00:00
|
|
|
#include "ui/emoji_config.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2018-08-31 13:01:42 +00:00
|
|
|
#include "storage/cache/storage_cache_database.h"
|
|
|
|
#include "data/data_session.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-08-28 19:16:23 +00:00
|
|
|
#include "mainwindow.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "auth_session.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "layout.h"
|
2018-12-06 08:47:58 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-08-31 13:01:42 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
constexpr auto kMegabyte = int64(1024 * 1024);
|
|
|
|
constexpr auto kTotalSizeLimitsCount = 18;
|
|
|
|
constexpr auto kMediaSizeLimitsCount = 18;
|
|
|
|
constexpr auto kMinimalSizeLimit = 100 * kMegabyte;
|
2018-08-31 13:01:42 +00:00
|
|
|
constexpr auto kTimeLimitsCount = 16;
|
|
|
|
constexpr auto kMaxTimeLimitValue = std::numeric_limits<size_type>::max();
|
2019-03-01 14:20:04 +00:00
|
|
|
constexpr auto kFakeMediaCacheTag = uint16(0xFFFF);
|
2018-08-31 13:01:42 +00:00
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
int64 TotalSizeLimitInMB(int index) {
|
|
|
|
if (index < 8) {
|
|
|
|
return int64(index + 2) * 100;
|
|
|
|
}
|
|
|
|
return int64(index - 7) * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 TotalSizeLimit(int index) {
|
|
|
|
return TotalSizeLimitInMB(index) * kMegabyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 MediaSizeLimitInMB(int index) {
|
|
|
|
if (index < 9) {
|
2018-08-31 13:01:42 +00:00
|
|
|
return int64(index + 1) * 100;
|
|
|
|
}
|
2019-03-01 14:20:04 +00:00
|
|
|
return int64(index - 8) * 1024;
|
2018-08-31 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
int64 MediaSizeLimit(int index) {
|
|
|
|
return MediaSizeLimitInMB(index) * kMegabyte;
|
2018-08-31 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SizeLimitText(int64 limit) {
|
|
|
|
const auto mb = (limit / (1024 * 1024));
|
|
|
|
const auto gb = (mb / 1024);
|
|
|
|
return (gb > 0)
|
|
|
|
? (QString::number(gb) + " GB")
|
|
|
|
: (QString::number(mb) + " MB");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type TimeLimitInDays(int index) {
|
|
|
|
if (index < 3) {
|
|
|
|
const auto weeks = (index + 1);
|
|
|
|
return size_type(weeks) * 7;
|
|
|
|
} else if (index < 15) {
|
|
|
|
const auto month = (index - 2);
|
|
|
|
return (size_type(month) * 30)
|
|
|
|
+ ((month >= 12) ? 5 :
|
|
|
|
(month >= 10) ? 4 :
|
|
|
|
(month >= 8) ? 3 :
|
|
|
|
(month >= 7) ? 2 :
|
|
|
|
(month >= 5) ? 1 :
|
|
|
|
(month >= 3) ? 0 :
|
|
|
|
(month >= 2) ? -1 :
|
|
|
|
(month >= 1) ? 1 : 0);
|
|
|
|
//+ (month >= 1 ? 1 : 0)
|
|
|
|
//- (month >= 2 ? 2 : 0)
|
|
|
|
//+ (month >= 3 ? 1 : 0)
|
|
|
|
//+ (month >= 5 ? 1 : 0)
|
|
|
|
//+ (month >= 7 ? 1 : 0)
|
|
|
|
//+ (month >= 8 ? 1 : 0)
|
|
|
|
//+ (month >= 10 ? 1 : 0)
|
|
|
|
//+ (month >= 12 ? 1 : 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type TimeLimit(int index) {
|
|
|
|
const auto days = TimeLimitInDays(index);
|
|
|
|
return days
|
|
|
|
? (days * 24 * 60 * 60)
|
|
|
|
: kMaxTimeLimitValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TimeLimitText(size_type limit) {
|
|
|
|
const auto days = (limit / (24 * 60 * 60));
|
|
|
|
const auto weeks = (days / 7);
|
|
|
|
const auto months = (days / 29);
|
|
|
|
return (months > 0)
|
2019-06-19 16:39:25 +00:00
|
|
|
? tr::lng_local_storage_limit_months(tr::now, lt_count, months)
|
2018-08-31 13:01:42 +00:00
|
|
|
: (limit > 0)
|
2019-06-19 16:39:25 +00:00
|
|
|
? tr::lng_local_storage_limit_weeks(tr::now, lt_count, weeks)
|
2019-06-19 15:09:03 +00:00
|
|
|
: tr::lng_local_storage_limit_never(tr::now);
|
2018-08-31 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type LimitToValue(size_type timeLimit) {
|
|
|
|
return timeLimit ? timeLimit : kMaxTimeLimitValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type ValueToLimit(size_type timeLimit) {
|
|
|
|
return (timeLimit != kMaxTimeLimitValue) ? timeLimit : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
class LocalStorageBox::Row : public Ui::RpWidget {
|
|
|
|
public:
|
|
|
|
Row(
|
|
|
|
QWidget *parent,
|
|
|
|
Fn<QString(size_type)> title,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::producer<QString> clear,
|
2018-08-29 13:23:16 +00:00
|
|
|
const Database::TaggedSummary &data);
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
void update(const Database::TaggedSummary &data);
|
|
|
|
void toggleProgress(bool shown);
|
2016-12-13 17:07:56 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
rpl::producer<> clearRequests() const;
|
2016-11-19 14:47:28 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
private:
|
|
|
|
QString titleText(const Database::TaggedSummary &data) const;
|
|
|
|
QString sizeText(const Database::TaggedSummary &data) const;
|
2019-04-01 17:44:54 +00:00
|
|
|
void radialAnimationCallback();
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
Fn<QString(size_type)> _titleFactory;
|
|
|
|
object_ptr<Ui::FlatLabel> _title;
|
|
|
|
object_ptr<Ui::FlatLabel> _description;
|
|
|
|
object_ptr<Ui::FlatLabel> _clearing = { nullptr };
|
|
|
|
object_ptr<Ui::RoundButton> _clear;
|
|
|
|
std::unique_ptr<Ui::InfiniteRadialAnimation> _progress;
|
2016-09-26 13:57:08 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
};
|
2016-11-18 13:34:58 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
LocalStorageBox::Row::Row(
|
|
|
|
QWidget *parent,
|
|
|
|
Fn<QString(size_type)> title,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::producer<QString> clear,
|
2018-08-29 13:23:16 +00:00
|
|
|
const Database::TaggedSummary &data)
|
|
|
|
: RpWidget(parent)
|
|
|
|
, _titleFactory(std::move(title))
|
|
|
|
, _title(
|
|
|
|
this,
|
|
|
|
titleText(data),
|
|
|
|
st::localStorageRowTitle)
|
|
|
|
, _description(
|
|
|
|
this,
|
|
|
|
sizeText(data),
|
|
|
|
st::localStorageRowSize)
|
|
|
|
, _clear(this, std::move(clear), st::localStorageClear) {
|
|
|
|
_clear->setVisible(data.count != 0);
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
void LocalStorageBox::Row::update(const Database::TaggedSummary &data) {
|
|
|
|
if (data.count != 0) {
|
|
|
|
_title->setText(titleText(data));
|
|
|
|
}
|
|
|
|
_description->setText(sizeText(data));
|
|
|
|
_clear->setVisible(data.count != 0);
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
void LocalStorageBox::Row::toggleProgress(bool shown) {
|
|
|
|
if (!shown) {
|
|
|
|
_progress = nullptr;
|
|
|
|
_description->show();
|
|
|
|
_clearing.destroy();
|
|
|
|
} else if (!_progress) {
|
|
|
|
_progress = std::make_unique<Ui::InfiniteRadialAnimation>(
|
2019-04-01 17:44:54 +00:00
|
|
|
[=] { radialAnimationCallback(); },
|
2018-08-29 13:23:16 +00:00
|
|
|
st::proxyCheckingAnimation);
|
|
|
|
_progress->start();
|
|
|
|
_clearing = object_ptr<Ui::FlatLabel>(
|
|
|
|
this,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_local_storage_clearing(tr::now),
|
2018-08-29 13:23:16 +00:00
|
|
|
st::localStorageRowSize);
|
|
|
|
_clearing->show();
|
|
|
|
_description->hide();
|
|
|
|
resizeToWidth(width());
|
|
|
|
RpWidget::update();
|
|
|
|
}
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 17:44:54 +00:00
|
|
|
void LocalStorageBox::Row::radialAnimationCallback() {
|
|
|
|
if (!anim::Disabled()) {
|
2018-08-29 13:23:16 +00:00
|
|
|
RpWidget::update();
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
rpl::producer<> LocalStorageBox::Row::clearRequests() const {
|
2018-09-13 20:09:26 +00:00
|
|
|
return _clear->clicks() | rpl::map([] { return rpl::empty_value(); });
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LocalStorageBox::Row::resizeGetHeight(int newWidth) {
|
|
|
|
const auto height = st::localStorageRowHeight;
|
|
|
|
const auto padding = st::localStorageRowPadding;
|
|
|
|
const auto available = newWidth - padding.left() - padding.right();
|
|
|
|
_title->resizeToWidth(available);
|
|
|
|
_description->resizeToWidth(available);
|
|
|
|
_title->moveToLeft(padding.left(), padding.top(), newWidth);
|
|
|
|
_description->moveToLeft(
|
|
|
|
padding.left(),
|
|
|
|
height - padding.bottom() - _description->height(),
|
|
|
|
newWidth);
|
|
|
|
if (_clearing) {
|
|
|
|
const auto progressShift = st::proxyCheckingPosition.x()
|
|
|
|
+ st::proxyCheckingAnimation.size.width()
|
|
|
|
+ st::proxyCheckingSkip;
|
|
|
|
_clearing->resizeToWidth(available - progressShift);
|
|
|
|
_clearing->moveToLeft(
|
|
|
|
padding.left(),// + progressShift,
|
|
|
|
_description->y(),
|
|
|
|
newWidth);
|
|
|
|
}
|
|
|
|
_clear->moveToRight(
|
2018-08-31 13:01:42 +00:00
|
|
|
st::boxLayerButtonPadding.right(),
|
2018-08-29 13:23:16 +00:00
|
|
|
(height - _clear->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::Row::paintEvent(QPaintEvent *e) {
|
|
|
|
if (!_progress || true) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-19 14:47:28 +00:00
|
|
|
Painter p(this);
|
2018-08-29 13:23:16 +00:00
|
|
|
const auto padding = st::localStorageRowPadding;
|
|
|
|
const auto height = st::localStorageRowHeight;
|
|
|
|
const auto bottom = height - padding.bottom() - _description->height();
|
|
|
|
_progress->draw(
|
|
|
|
p,
|
|
|
|
{
|
|
|
|
st::proxyCheckingPosition.x() + padding.left(),
|
|
|
|
st::proxyCheckingPosition.y() + bottom
|
|
|
|
},
|
|
|
|
width());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LocalStorageBox::Row::titleText(const Database::TaggedSummary &data) const {
|
|
|
|
return _titleFactory(data.count);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LocalStorageBox::Row::sizeText(const Database::TaggedSummary &data) const {
|
|
|
|
return data.totalSize
|
|
|
|
? formatSizeText(data.totalSize)
|
2019-06-19 15:09:03 +00:00
|
|
|
: tr::lng_local_storage_empty(tr::now);
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalStorageBox::LocalStorageBox(
|
|
|
|
QWidget*,
|
|
|
|
not_null<Database*> db,
|
2019-03-01 14:20:04 +00:00
|
|
|
not_null<Database*> dbBig,
|
2018-08-29 13:23:16 +00:00
|
|
|
CreateTag)
|
2019-03-01 14:20:04 +00:00
|
|
|
: _db(db)
|
|
|
|
, _dbBig(dbBig) {
|
2018-08-31 13:01:42 +00:00
|
|
|
const auto &settings = Local::cacheSettings();
|
2019-03-01 14:20:04 +00:00
|
|
|
const auto &settingsBig = Local::cacheBigFileSettings();
|
|
|
|
_totalSizeLimit = settings.totalSizeLimit + settingsBig.totalSizeLimit;
|
|
|
|
_mediaSizeLimit = settingsBig.totalSizeLimit;
|
2018-08-31 13:01:42 +00:00
|
|
|
_timeLimit = settings.totalTimeLimit;
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
void LocalStorageBox::Show(
|
|
|
|
not_null<Database*> db,
|
|
|
|
not_null<Database*> dbBig) {
|
2018-08-29 13:23:16 +00:00
|
|
|
auto shared = std::make_shared<object_ptr<LocalStorageBox>>(
|
2019-03-01 14:20:04 +00:00
|
|
|
Box<LocalStorageBox>(db, dbBig, CreateTag()));
|
2018-08-29 13:23:16 +00:00
|
|
|
const auto weak = shared->data();
|
2019-03-01 14:20:04 +00:00
|
|
|
rpl::combine(
|
|
|
|
db->statsOnMain(),
|
|
|
|
dbBig->statsOnMain()
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
Database::Stats &&stats,
|
|
|
|
Database::Stats &&statsBig) {
|
|
|
|
weak->update(std::move(stats), std::move(statsBig));
|
2018-08-29 13:23:16 +00:00
|
|
|
if (auto &strong = *shared) {
|
|
|
|
Ui::show(std::move(strong));
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
2018-08-29 13:23:16 +00:00
|
|
|
}, weak->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::prepare() {
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_local_storage_title());
|
2018-08-29 13:23:16 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_box_ok(), [this] { closeBox(); });
|
2018-08-29 13:23:16 +00:00
|
|
|
|
|
|
|
setupControls();
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 13:23:16 +00:00
|
|
|
void LocalStorageBox::updateRow(
|
|
|
|
not_null<Ui::SlideWrap<Row>*> row,
|
2019-03-01 14:20:04 +00:00
|
|
|
const Database::TaggedSummary *data) {
|
2018-08-29 13:23:16 +00:00
|
|
|
const auto summary = (_rows.find(0)->second == row);
|
|
|
|
const auto shown = (data && data->count && data->totalSize) || summary;
|
|
|
|
if (shown) {
|
|
|
|
row->entity()->update(*data);
|
|
|
|
}
|
|
|
|
row->toggle(shown, anim::type::normal);
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
void LocalStorageBox::update(
|
|
|
|
Database::Stats &&stats,
|
|
|
|
Database::Stats &&statsBig) {
|
2018-08-29 13:23:16 +00:00
|
|
|
_stats = std::move(stats);
|
2019-03-01 14:20:04 +00:00
|
|
|
_statsBig = std::move(statsBig);
|
2018-08-29 13:23:16 +00:00
|
|
|
if (const auto i = _rows.find(0); i != end(_rows)) {
|
2019-03-01 14:20:04 +00:00
|
|
|
i->second->entity()->toggleProgress(
|
|
|
|
_stats.clearing || _statsBig.clearing);
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
for (const auto &entry : _rows) {
|
2019-03-01 14:20:04 +00:00
|
|
|
if (entry.first == kFakeMediaCacheTag) {
|
|
|
|
updateRow(entry.second, &_statsBig.full);
|
|
|
|
} else if (entry.first) {
|
2018-08-29 13:23:16 +00:00
|
|
|
const auto i = _stats.tagged.find(entry.first);
|
|
|
|
updateRow(
|
|
|
|
entry.second,
|
|
|
|
(i != end(_stats.tagged)) ? &i->second : nullptr);
|
|
|
|
} else {
|
2019-03-01 14:20:04 +00:00
|
|
|
const auto full = summary();
|
|
|
|
updateRow(entry.second, &full);
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
auto LocalStorageBox::summary() const -> Database::TaggedSummary {
|
|
|
|
auto result = _stats.full;
|
|
|
|
result.count += _statsBig.full.count;
|
|
|
|
result.totalSize += _statsBig.full.totalSize;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::clearByTag(uint16 tag) {
|
|
|
|
if (tag == kFakeMediaCacheTag) {
|
|
|
|
_dbBig->clear();
|
|
|
|
} else if (tag) {
|
2018-08-29 13:23:16 +00:00
|
|
|
_db->clearByTag(tag);
|
|
|
|
} else {
|
|
|
|
_db->clear();
|
2019-03-01 14:20:04 +00:00
|
|
|
_dbBig->clear();
|
2018-10-24 13:29:19 +00:00
|
|
|
Ui::Emoji::ClearIrrelevantCache();
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::setupControls() {
|
2018-08-31 13:01:42 +00:00
|
|
|
const auto container = setInnerWidget(
|
2018-12-06 08:47:58 +00:00
|
|
|
object_ptr<Ui::VerticalLayout>(this),
|
|
|
|
st::contactsMultiSelect.scroll);
|
2018-08-29 13:23:16 +00:00
|
|
|
const auto createRow = [&](
|
2019-03-01 14:20:04 +00:00
|
|
|
uint16 tag,
|
2018-08-29 13:23:16 +00:00
|
|
|
Fn<QString(size_type)> title,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::producer<QString> clear,
|
2018-08-29 13:23:16 +00:00
|
|
|
const Database::TaggedSummary &data) {
|
2018-08-31 13:01:42 +00:00
|
|
|
auto result = container->add(object_ptr<Ui::SlideWrap<Row>>(
|
|
|
|
container,
|
2018-08-29 13:23:16 +00:00
|
|
|
object_ptr<Row>(
|
2018-08-31 13:01:42 +00:00
|
|
|
container,
|
2018-08-29 13:23:16 +00:00
|
|
|
std::move(title),
|
|
|
|
std::move(clear),
|
|
|
|
data)));
|
|
|
|
const auto shown = (data.count && data.totalSize) || !tag;
|
|
|
|
result->toggle(shown, anim::type::instant);
|
|
|
|
result->entity()->clearRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
clearByTag(tag);
|
|
|
|
}, result->lifetime());
|
|
|
|
_rows.emplace(tag, result);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto tracker = Ui::MultiSlideTracker();
|
|
|
|
const auto createTagRow = [&](uint8 tag, auto &&titleFactory) {
|
|
|
|
static const auto empty = Database::TaggedSummary();
|
|
|
|
const auto i = _stats.tagged.find(tag);
|
|
|
|
const auto &data = (i != end(_stats.tagged)) ? i->second : empty;
|
|
|
|
auto factory = std::forward<decltype(titleFactory)>(titleFactory);
|
|
|
|
auto title = [factory = std::move(factory)](size_type count) {
|
2019-06-19 16:39:25 +00:00
|
|
|
return factory(tr::now, lt_count, count);
|
2018-08-29 13:23:16 +00:00
|
|
|
};
|
|
|
|
tracker.track(createRow(
|
|
|
|
tag,
|
|
|
|
std::move(title),
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_local_storage_clear_some(),
|
2018-08-29 13:23:16 +00:00
|
|
|
data));
|
|
|
|
};
|
|
|
|
auto summaryTitle = [](size_type) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_local_storage_summary(tr::now);
|
2018-08-29 13:23:16 +00:00
|
|
|
};
|
2019-03-01 14:20:04 +00:00
|
|
|
auto mediaCacheTitle = [](size_type) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_local_storage_media(tr::now);
|
2019-03-01 14:20:04 +00:00
|
|
|
};
|
2018-08-29 13:23:16 +00:00
|
|
|
createRow(
|
|
|
|
0,
|
|
|
|
std::move(summaryTitle),
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_local_storage_clear(),
|
2019-03-01 14:20:04 +00:00
|
|
|
summary());
|
2018-08-31 13:01:42 +00:00
|
|
|
setupLimits(container);
|
|
|
|
const auto shadow = container->add(object_ptr<Ui::SlideWrap<>>(
|
|
|
|
container,
|
|
|
|
object_ptr<Ui::PlainShadow>(container),
|
|
|
|
st::localStorageRowPadding));
|
2019-06-19 16:39:25 +00:00
|
|
|
createTagRow(Data::kImageCacheTag, tr::lng_local_storage_image);
|
|
|
|
createTagRow(Data::kStickerCacheTag, tr::lng_local_storage_sticker);
|
|
|
|
createTagRow(Data::kVoiceMessageCacheTag, tr::lng_local_storage_voice);
|
|
|
|
createTagRow(Data::kVideoMessageCacheTag, tr::lng_local_storage_round);
|
|
|
|
createTagRow(Data::kAnimationCacheTag, tr::lng_local_storage_animation);
|
2019-03-01 14:20:04 +00:00
|
|
|
tracker.track(createRow(
|
|
|
|
kFakeMediaCacheTag,
|
|
|
|
std::move(mediaCacheTitle),
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_local_storage_clear_some(),
|
2019-03-01 14:20:04 +00:00
|
|
|
_statsBig.full));
|
2018-08-29 13:23:16 +00:00
|
|
|
shadow->toggleOn(
|
|
|
|
std::move(tracker).atLeastOneShownValue()
|
|
|
|
);
|
2018-08-31 13:01:42 +00:00
|
|
|
container->resizeToWidth(st::boxWidth);
|
|
|
|
container->heightValue(
|
2018-08-29 13:23:16 +00:00
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWidth, height);
|
2018-08-31 13:01:42 +00:00
|
|
|
}, container->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Convert,
|
|
|
|
typename Callback,
|
|
|
|
typename>
|
2019-03-01 14:20:04 +00:00
|
|
|
not_null<Ui::MediaSlider*> LocalStorageBox::createLimitsSlider(
|
2018-08-31 13:01:42 +00:00
|
|
|
not_null<Ui::VerticalLayout*> container,
|
|
|
|
int valuesCount,
|
|
|
|
Convert &&convert,
|
|
|
|
Value currentValue,
|
|
|
|
Callback &&callback) {
|
|
|
|
const auto label = container->add(
|
|
|
|
object_ptr<Ui::LabelSimple>(container, st::localStorageLimitLabel),
|
|
|
|
st::localStorageLimitLabelMargin);
|
|
|
|
callback(label, currentValue);
|
|
|
|
const auto slider = container->add(
|
|
|
|
object_ptr<Ui::MediaSlider>(container, st::localStorageLimitSlider),
|
|
|
|
st::localStorageLimitMargin);
|
|
|
|
slider->resize(st::localStorageLimitSlider.seekSize);
|
|
|
|
slider->setPseudoDiscrete(
|
|
|
|
valuesCount,
|
|
|
|
std::forward<Convert>(convert),
|
|
|
|
currentValue,
|
|
|
|
[=, callback = std::forward<Callback>(callback)](Value value) {
|
|
|
|
callback(label, value);
|
|
|
|
});
|
2019-03-01 14:20:04 +00:00
|
|
|
return slider;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::updateMediaLimit() {
|
|
|
|
const auto good = [&](int64 mediaLimit) {
|
|
|
|
return (_totalSizeLimit - mediaLimit >= kMinimalSizeLimit);
|
|
|
|
};
|
|
|
|
if (good(_mediaSizeLimit) || !_mediaSlider || !_mediaLabel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto index = 1;
|
|
|
|
while ((index < kMediaSizeLimitsCount)
|
|
|
|
&& (MediaSizeLimit(index) * 2 <= _totalSizeLimit)) {
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
--index;
|
|
|
|
_mediaSizeLimit = MediaSizeLimit(index);
|
|
|
|
_mediaSlider->setValue(index / float64(kMediaSizeLimitsCount - 1));
|
|
|
|
updateMediaLabel();
|
|
|
|
|
|
|
|
Ensures(good(_mediaSizeLimit));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::updateTotalLimit() {
|
|
|
|
const auto good = [&](int64 totalLimit) {
|
|
|
|
return (totalLimit - _mediaSizeLimit >= kMinimalSizeLimit);
|
|
|
|
};
|
|
|
|
if (good(_totalSizeLimit) || !_totalSlider || !_totalLabel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto index = kTotalSizeLimitsCount - 1;
|
|
|
|
while ((index > 0)
|
|
|
|
&& (TotalSizeLimit(index - 1) >= 2 * _mediaSizeLimit)) {
|
|
|
|
--index;
|
|
|
|
}
|
|
|
|
_totalSizeLimit = TotalSizeLimit(index);
|
|
|
|
_totalSlider->setValue(index / float64(kTotalSizeLimitsCount - 1));
|
|
|
|
updateTotalLabel();
|
|
|
|
|
|
|
|
Ensures(good(_totalSizeLimit));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::updateTotalLabel() {
|
|
|
|
Expects(_totalLabel != nullptr);
|
|
|
|
|
|
|
|
const auto text = SizeLimitText(_totalSizeLimit);
|
2019-06-19 16:39:25 +00:00
|
|
|
_totalLabel->setText(tr::lng_local_storage_size_limit(tr::now, lt_size, text));
|
2019-03-01 14:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::updateMediaLabel() {
|
|
|
|
Expects(_mediaLabel != nullptr);
|
|
|
|
|
|
|
|
const auto text = SizeLimitText(_mediaSizeLimit);
|
2019-06-19 16:39:25 +00:00
|
|
|
_mediaLabel->setText(tr::lng_local_storage_media_limit(tr::now, lt_size, text));
|
2018-08-31 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::setupLimits(not_null<Ui::VerticalLayout*> container) {
|
|
|
|
const auto shadow = container->add(
|
|
|
|
object_ptr<Ui::PlainShadow>(container),
|
|
|
|
st::localStorageRowPadding);
|
|
|
|
|
2019-03-01 14:20:04 +00:00
|
|
|
_totalSlider = createLimitsSlider(
|
|
|
|
container,
|
|
|
|
kTotalSizeLimitsCount,
|
|
|
|
TotalSizeLimit,
|
|
|
|
_totalSizeLimit,
|
|
|
|
[=](not_null<Ui::LabelSimple*> label, int64 limit) {
|
|
|
|
_totalSizeLimit = limit;
|
|
|
|
_totalLabel = label;
|
|
|
|
updateTotalLabel();
|
|
|
|
updateMediaLimit();
|
|
|
|
limitsChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
_mediaSlider = createLimitsSlider(
|
2018-08-31 13:01:42 +00:00
|
|
|
container,
|
2019-03-01 14:20:04 +00:00
|
|
|
kMediaSizeLimitsCount,
|
|
|
|
MediaSizeLimit,
|
|
|
|
_mediaSizeLimit,
|
2018-08-31 13:01:42 +00:00
|
|
|
[=](not_null<Ui::LabelSimple*> label, int64 limit) {
|
2019-03-01 14:20:04 +00:00
|
|
|
_mediaSizeLimit = limit;
|
|
|
|
_mediaLabel = label;
|
|
|
|
updateMediaLabel();
|
|
|
|
updateTotalLimit();
|
2018-08-31 13:01:42 +00:00
|
|
|
limitsChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
createLimitsSlider(
|
|
|
|
container,
|
|
|
|
kTimeLimitsCount,
|
|
|
|
TimeLimit,
|
|
|
|
LimitToValue(_timeLimit),
|
|
|
|
[=](not_null<Ui::LabelSimple*> label, size_type limit) {
|
2019-03-14 09:20:52 +00:00
|
|
|
_timeLimit = ValueToLimit(limit);
|
|
|
|
const auto text = TimeLimitText(_timeLimit);
|
2019-06-19 16:39:25 +00:00
|
|
|
label->setText(tr::lng_local_storage_time_limit(tr::now, lt_limit, text));
|
2018-08-31 13:01:42 +00:00
|
|
|
limitsChanged();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::limitsChanged() {
|
|
|
|
const auto &settings = Local::cacheSettings();
|
2019-03-01 14:20:04 +00:00
|
|
|
const auto &settingsBig = Local::cacheBigFileSettings();
|
|
|
|
const auto sizeLimit = _totalSizeLimit - _mediaSizeLimit;
|
|
|
|
const auto changed = (settings.totalSizeLimit != sizeLimit)
|
|
|
|
|| (settingsBig.totalSizeLimit != _mediaSizeLimit)
|
|
|
|
|| (settings.totalTimeLimit != _timeLimit)
|
|
|
|
|| (settingsBig.totalTimeLimit != _timeLimit);
|
2018-08-31 13:01:42 +00:00
|
|
|
if (_limitsChanged != changed) {
|
|
|
|
_limitsChanged = changed;
|
|
|
|
clearButtons();
|
|
|
|
if (_limitsChanged) {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_settings_save(), [=] { save(); });
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-08-31 13:01:42 +00:00
|
|
|
} else {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_box_ok(), [=] { closeBox(); });
|
2018-08-31 13:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::save() {
|
|
|
|
if (!_limitsChanged) {
|
|
|
|
closeBox();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto update = Storage::Cache::Database::SettingsUpdate();
|
2019-03-01 14:20:04 +00:00
|
|
|
update.totalSizeLimit = _totalSizeLimit - _mediaSizeLimit;
|
|
|
|
update.totalTimeLimit = _timeLimit;
|
|
|
|
auto updateBig = Storage::Cache::Database::SettingsUpdate();
|
|
|
|
updateBig.totalSizeLimit = _mediaSizeLimit;
|
2019-03-14 09:20:52 +00:00
|
|
|
updateBig.totalTimeLimit = _timeLimit;
|
2019-03-01 14:20:04 +00:00
|
|
|
Local::updateCacheSettings(update, updateBig);
|
2018-08-31 13:01:42 +00:00
|
|
|
Auth().data().cache().updateSettings(update);
|
|
|
|
closeBox();
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::paintEvent(QPaintEvent *e) {
|
|
|
|
BoxContent::paintEvent(e);
|
|
|
|
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.setFont(st::boxTextFont);
|
|
|
|
p.setPen(st::windowFg);
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|