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"
|
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "platform/platform_specific.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"
|
2016-10-04 18:18:08 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-10-03 08:56:03 +00:00
|
|
|
|
|
|
|
namespace Window {
|
|
|
|
namespace Notifications {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Delete notify photo file after 1 minute of not using.
|
|
|
|
constexpr int kNotifyDeletePhotoAfterMs = 60000;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
CachedUserpics::CachedUserpics(Type type)
|
|
|
|
: _type(type)
|
|
|
|
, _clearTimer([=] { onClear(); }) {
|
2016-10-03 08:56:03 +00:00
|
|
|
QDir().mkpath(cWorkingDir() + qsl("tdata/temp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CachedUserpics::get(const StorageKey &key, PeerData *peer) {
|
2016-12-01 19:20:33 +00:00
|
|
|
auto ms = getms(true);
|
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;
|
|
|
|
}
|
|
|
|
v.path = cWorkingDir() + qsl("tdata/temp/") + QString::number(rand_value<uint64>(), 16) + qsl(".png");
|
|
|
|
if (key.first || key.second) {
|
2017-01-07 11:55:05 +00:00
|
|
|
if (_type == Type::Rounded) {
|
|
|
|
peer->saveUserpicRounded(v.path, st::notifyMacPhotoSize);
|
|
|
|
} else {
|
|
|
|
peer->saveUserpic(v.path, st::notifyMacPhotoSize);
|
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
} else {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().logoNoMargin().save(v.path, "PNG");
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
i = _images.insert(key, v);
|
|
|
|
_someSavedFlag = true;
|
|
|
|
}
|
|
|
|
return i->path;
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs CachedUserpics::clear(TimeMs ms) {
|
|
|
|
TimeMs 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
|
|
|
}
|
|
|
|
|
|
|
|
void CachedUserpics::onClear() {
|
|
|
|
auto ms = getms(true);
|
|
|
|
auto minuntil = clear(ms);
|
|
|
|
if (minuntil) {
|
|
|
|
clearInMs(int(minuntil - ms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedUserpics::~CachedUserpics() {
|
|
|
|
if (_someSavedFlag) {
|
2017-03-04 19:36:59 +00:00
|
|
|
TimeMs result = 0;
|
|
|
|
for_const (auto &item, _images) {
|
|
|
|
QFile(item.path).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This works about 1200ms on Windows for a folder with one image O_o
|
|
|
|
// psDeleteDir(cWorkingDir() + qsl("tdata/temp"));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Window
|