2016-08-28 19:16:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-08-28 19:16:23 +00:00
|
|
|
*/
|
|
|
|
#include "boxes/localstoragebox.h"
|
|
|
|
|
2016-11-08 14:43:10 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-08-28 19:16:23 +00:00
|
|
|
#include "lang.h"
|
|
|
|
#include "mainwindow.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "auth_session.h"
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
LocalStorageBox::LocalStorageBox(QWidget *parent)
|
|
|
|
: _clear(this, lang(lng_local_storage_clear), st::boxLinkButton) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::prepare() {
|
|
|
|
setTitle(lang(lng_local_storage_title));
|
|
|
|
|
|
|
|
addButton(lang(lng_box_ok), [this] { closeBox(); });
|
2016-11-19 14:47:28 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
_clear->setClickedCallback([this] { clearStorage(); });
|
2016-08-28 19:16:23 +00:00
|
|
|
|
|
|
|
connect(App::wnd(), SIGNAL(tempDirCleared(int)), this, SLOT(onTempDirCleared(int)));
|
|
|
|
connect(App::wnd(), SIGNAL(tempDirClearFailed(int)), this, SLOT(onTempDirClearFailed(int)));
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
subscribe(AuthSession::CurrentDownloaderTaskFinished(), [this] { update(); });
|
2016-09-26 13:57:08 +00:00
|
|
|
|
2016-11-18 13:34:58 +00:00
|
|
|
updateControls();
|
|
|
|
|
2016-08-28 19:16:23 +00:00
|
|
|
checkLocalStoredCounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::updateControls() {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto rowsHeight = 0;
|
2016-08-28 19:16:23 +00:00
|
|
|
if (_imagesCount > 0 && _audiosCount > 0) {
|
|
|
|
rowsHeight = 2 * (st::linkFont->height + st::localStorageBoxSkip);
|
|
|
|
} else {
|
|
|
|
rowsHeight = st::linkFont->height + st::localStorageBoxSkip;
|
|
|
|
}
|
|
|
|
_clear->setVisible(_imagesCount > 0 || _audiosCount > 0);
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWidth, st::localStorageBoxSkip + rowsHeight + _clear->height());
|
|
|
|
_clear->moveToLeft(st::boxPadding.left(), st::localStorageBoxSkip + rowsHeight);
|
2016-08-28 19:16:23 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::checkLocalStoredCounts() {
|
|
|
|
int imagesCount = Local::hasImages() + Local::hasStickers() + Local::hasWebFiles();
|
|
|
|
int audiosCount = Local::hasAudios();
|
|
|
|
if (imagesCount != _imagesCount || audiosCount != _audiosCount) {
|
|
|
|
_imagesCount = imagesCount;
|
|
|
|
_audiosCount = audiosCount;
|
|
|
|
if (_imagesCount > 0 || _audiosCount > 0) {
|
|
|
|
_state = State::Normal;
|
|
|
|
}
|
|
|
|
updateControls();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::paintEvent(QPaintEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::paintEvent(e);
|
2016-08-28 19:16:23 +00:00
|
|
|
|
2016-11-19 14:47:28 +00:00
|
|
|
Painter p(this);
|
2016-08-28 19:16:23 +00:00
|
|
|
|
|
|
|
p.setFont(st::boxTextFont);
|
2016-11-16 10:44:06 +00:00
|
|
|
p.setPen(st::windowFg);
|
2016-08-28 19:16:23 +00:00
|
|
|
checkLocalStoredCounts();
|
2016-12-13 17:07:56 +00:00
|
|
|
auto top = st::localStorageBoxSkip;
|
2016-08-28 19:16:23 +00:00
|
|
|
if (_imagesCount > 0) {
|
|
|
|
auto text = lng_settings_images_cached(lt_count, _imagesCount, lt_size, formatSizeText(Local::storageImagesSize() + Local::storageStickersSize() + Local::storageWebFilesSize()));
|
|
|
|
p.drawTextLeft(st::boxPadding.left(), top, width(), text);
|
|
|
|
top += st::boxTextFont->height + st::localStorageBoxSkip;
|
|
|
|
}
|
|
|
|
if (_audiosCount > 0) {
|
|
|
|
auto text = lng_settings_audios_cached(lt_count, _audiosCount, lt_size, formatSizeText(Local::storageAudiosSize()));
|
|
|
|
p.drawTextLeft(st::boxPadding.left(), top, width(), text);
|
|
|
|
top += st::boxTextFont->height + st::localStorageBoxSkip;
|
|
|
|
} else if (_imagesCount <= 0) {
|
|
|
|
p.drawTextLeft(st::boxPadding.left(), top, width(), lang(lng_settings_no_data_cached));
|
|
|
|
top += st::boxTextFont->height + st::localStorageBoxSkip;
|
|
|
|
}
|
|
|
|
auto text = ([this]() -> QString {
|
|
|
|
switch (_state) {
|
|
|
|
case State::Clearing: return lang(lng_local_storage_clearing);
|
|
|
|
case State::Cleared: return lang(lng_local_storage_cleared);
|
|
|
|
case State::ClearFailed: return lang(lng_local_storage_clear_failed);
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
})();
|
|
|
|
if (!text.isEmpty()) {
|
|
|
|
p.drawTextLeft(st::boxPadding.left(), top, width(), text);
|
|
|
|
top += st::boxTextFont->height + st::localStorageBoxSkip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void LocalStorageBox::clearStorage() {
|
2016-08-28 19:16:23 +00:00
|
|
|
App::wnd()->tempDirDelete(Local::ClearManagerStorage);
|
|
|
|
_state = State::Clearing;
|
|
|
|
updateControls();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::onTempDirCleared(int task) {
|
|
|
|
if (task & Local::ClearManagerStorage) {
|
|
|
|
_state = State::Cleared;
|
|
|
|
}
|
|
|
|
updateControls();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStorageBox::onTempDirClearFailed(int task) {
|
|
|
|
if (task & Local::ClearManagerStorage) {
|
|
|
|
_state = State::ClearFailed;
|
|
|
|
}
|
|
|
|
updateControls();
|
|
|
|
}
|