2016-10-03 08:56:03 +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-10-03 08:56:03 +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-10-03 08:56:03 +00:00
|
|
|
*/
|
|
|
|
#include "window/notifications_utilities.h"
|
|
|
|
|
2021-11-02 13:10:40 +00:00
|
|
|
#include "window/main_window.h"
|
2020-10-29 19:56:13 +00:00
|
|
|
#include "base/platform/base_platform_file_utilities.h"
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_peer.h"
|
2019-08-28 18:20:49 +00:00
|
|
|
#include "ui/empty_userpic.h"
|
2016-10-04 18:18:08 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
namespace Window::Notifications {
|
2016-10-03 08:56:03 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Delete notify photo file after 1 minute of not using.
|
|
|
|
constexpr int kNotifyDeletePhotoAfterMs = 60000;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
QImage GenerateUserpic(not_null<PeerData*> peer, Ui::PeerUserpicView &view) {
|
|
|
|
return peer->isSelf()
|
|
|
|
? Ui::EmptyUserpic::GenerateSavedMessages(st::notifyMacPhotoSize)
|
|
|
|
: peer->isRepliesChat()
|
|
|
|
? Ui::EmptyUserpic::GenerateRepliesMessages(st::notifyMacPhotoSize)
|
|
|
|
: peer->generateUserpicImage(view, st::notifyMacPhotoSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedUserpics::CachedUserpics()
|
|
|
|
: _clearTimer([=] { clear(); }) {
|
2022-11-29 21:46:36 +00:00
|
|
|
QDir().mkpath(cWorkingDir() + u"tdata/temp"_q);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 15:10:25 +00:00
|
|
|
CachedUserpics::~CachedUserpics() {
|
|
|
|
if (_someSavedFlag) {
|
|
|
|
for (const auto &item : std::as_const(_images)) {
|
|
|
|
QFile(item.path).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This works about 1200ms on Windows for a folder with one image O_o
|
2022-12-05 12:18:10 +00:00
|
|
|
//base::Platform::DeleteDirectory(cWorkingDir() + u"tdata/temp"_q);
|
2020-05-29 15:10:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CachedUserpics::get(
|
|
|
|
const InMemoryKey &key,
|
|
|
|
not_null<PeerData*> peer,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &view) {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-10-03 08:56:03 +00:00
|
|
|
auto i = _images.find(key);
|
|
|
|
if (i != _images.cend()) {
|
|
|
|
if (i->until) {
|
|
|
|
i->until = ms + kNotifyDeletePhotoAfterMs;
|
|
|
|
clearInMs(-kNotifyDeletePhotoAfterMs);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Image v;
|
|
|
|
if (key.first) {
|
|
|
|
v.until = ms + kNotifyDeletePhotoAfterMs;
|
|
|
|
clearInMs(-kNotifyDeletePhotoAfterMs);
|
|
|
|
} else {
|
|
|
|
v.until = 0;
|
|
|
|
}
|
2021-03-13 11:50:34 +00:00
|
|
|
v.path = u"%1tdata/temp/%2.png"_q.arg(
|
|
|
|
cWorkingDir(),
|
2021-09-15 10:21:45 +00:00
|
|
|
QString::number(base::RandomValue<uint64>(), 16));
|
2016-10-03 08:56:03 +00:00
|
|
|
if (key.first || key.second) {
|
2022-12-05 12:18:10 +00:00
|
|
|
GenerateUserpic(peer, view).save(v.path, "PNG");
|
2016-10-03 08:56:03 +00:00
|
|
|
} else {
|
2021-11-02 13:10:40 +00:00
|
|
|
LogoNoMargin().save(v.path, "PNG");
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
i = _images.insert(key, v);
|
|
|
|
_someSavedFlag = true;
|
|
|
|
}
|
|
|
|
return i->path;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time CachedUserpics::clear(crl::time ms) {
|
|
|
|
crl::time result = 0;
|
2016-10-03 08:56:03 +00:00
|
|
|
for (auto i = _images.begin(); i != _images.end();) {
|
|
|
|
if (!i->until) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i->until <= ms) {
|
|
|
|
QFile(i->path).remove();
|
|
|
|
i = _images.erase(i);
|
|
|
|
} else {
|
|
|
|
if (!result) {
|
|
|
|
result = i->until;
|
|
|
|
} else {
|
|
|
|
accumulate_min(result, i->until);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CachedUserpics::clearInMs(int ms) {
|
|
|
|
if (ms < 0) {
|
|
|
|
ms = -ms;
|
|
|
|
if (_clearTimer.isActive() && _clearTimer.remainingTime() <= ms) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 11:26:43 +00:00
|
|
|
_clearTimer.callOnce(ms);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 15:10:25 +00:00
|
|
|
void CachedUserpics::clear() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-10-03 08:56:03 +00:00
|
|
|
auto minuntil = clear(ms);
|
|
|
|
if (minuntil) {
|
|
|
|
clearInMs(int(minuntil - ms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
} // namespace Window::Notifications
|