tdesktop/Telegram/SourceFiles/data/data_document.cpp

1723 lines
46 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_document.h"
#include "data/data_session.h"
#include "data/data_document_good_thumbnail.h"
#include "lang/lang_keys.h"
#include "inline_bots/inline_bot_layout_item.h"
#include "mainwidget.h"
#include "core/file_utilities.h"
2018-10-23 12:28:36 +00:00
#include "core/media_active_cache.h"
#include "core/mime_type.h"
#include "media/audio/media_audio.h"
2019-02-28 21:03:25 +00:00
#include "media/player/media_player_instance.h"
#include "media/streaming/media_streaming_loader_mtproto.h"
#include "media/streaming/media_streaming_loader_local.h"
#include "storage/localstorage.h"
#include "storage/streamed_file_downloader.h"
#include "platform/platform_specific.h"
#include "history/history.h"
#include "history/history_item.h"
#include "window/window_session_controller.h"
#include "storage/cache/storage_cache_database.h"
2018-12-05 08:07:17 +00:00
#include "boxes/confirm_box.h"
#include "ui/image/image.h"
#include "ui/image/image_source.h"
#include "ui/text/text_utilities.h"
#include "mainwindow.h"
#include "core/application.h"
#include "lottie/lottie_animation.h"
namespace {
2018-10-23 12:28:36 +00:00
constexpr auto kMemoryForCache = 32 * 1024 * 1024;
const auto kAnimatedStickerDimensions = QSize(512, 512);
2018-10-23 12:28:36 +00:00
using FilePathResolve = DocumentData::FilePathResolve;
2018-10-23 12:28:36 +00:00
Core::MediaActiveCache<DocumentData> &ActiveCache() {
static auto Instance = Core::MediaActiveCache<DocumentData>(
kMemoryForCache,
[](DocumentData *document) { document->unload(); });
2018-10-23 12:28:36 +00:00
return Instance;
}
int64 ComputeUsage(StickerData *sticker) {
return (sticker != nullptr && sticker->image != nullptr)
? sticker->image->width() * sticker->image->height() * 4
2018-10-23 12:28:36 +00:00
: 0;
}
QString JoinStringList(const QStringList &list, const QString &separator) {
const auto count = list.size();
if (!count) {
return QString();
}
auto result = QString();
auto fullsize = separator.size() * (count - 1);
for (const auto &string : list) {
fullsize += string.size();
}
result.reserve(fullsize);
result.append(list[0]);
for (auto i = 1; i != count; ++i) {
result.append(separator).append(list[i]);
}
return result;
}
2018-12-07 16:15:58 +00:00
void LaunchWithWarning(const QString &name, HistoryItem *item) {
const auto warn = [&] {
if (!Data::IsExecutableName(name)) {
return false;
} else if (!Auth().settings().exeLaunchWarning()) {
return false;
} else if (item && item->history()->peer->isVerified()) {
return false;
}
return true;
}();
if (!warn) {
2018-12-05 08:07:17 +00:00
File::Launch(name);
return;
}
const auto extension = '.' + Data::FileExtension(name);
const auto callback = [=](bool checked) {
if (checked) {
Auth().settings().setExeLaunchWarning(false);
Auth().saveSettingsDelayed();
}
File::Launch(name);
};
Ui::show(Box<ConfirmDontWarnBox>(
tr::lng_launch_exe_warning(
2018-12-05 08:07:17 +00:00
lt_extension,
rpl::single(Ui::Text::Bold(extension)),
Ui::Text::WithEntities),
2019-06-19 15:09:03 +00:00
tr::lng_launch_exe_dont_ask(tr::now),
tr::lng_launch_exe_sure(),
2018-12-05 08:07:17 +00:00
callback));
}
} // namespace
bool fileIsImage(const QString &name, const QString &mime) {
QString lowermime = mime.toLower(), namelower = name.toLower();
if (lowermime.startsWith(qstr("image/"))) {
return true;
} else if (namelower.endsWith(qstr(".bmp"))
|| namelower.endsWith(qstr(".jpg"))
|| namelower.endsWith(qstr(".jpeg"))
|| namelower.endsWith(qstr(".gif"))
|| namelower.endsWith(qstr(".webp"))
|| namelower.endsWith(qstr(".tga"))
|| namelower.endsWith(qstr(".tiff"))
|| namelower.endsWith(qstr(".tif"))
|| namelower.endsWith(qstr(".psd"))
|| namelower.endsWith(qstr(".png"))) {
return true;
}
return false;
}
QString FileNameUnsafe(
const QString &title,
const QString &filter,
const QString &prefix,
QString name,
bool savingAs,
const QDir &dir) {
#ifdef Q_OS_WIN
name = name.replace(QRegularExpression(qsl("[\\\\\\/\\:\\*\\?\\\"\\<\\>\\|]")), qsl("_"));
#elif defined Q_OS_MAC
name = name.replace(QRegularExpression(qsl("[\\:]")), qsl("_"));
#elif defined Q_OS_LINUX
name = name.replace(QRegularExpression(qsl("[\\/]")), qsl("_"));
#endif
if (Global::AskDownloadPath() || savingAs) {
if (!name.isEmpty() && name.at(0) == QChar::fromLatin1('.')) {
name = filedialogDefaultName(prefix, name);
} else if (dir.path() != qsl(".")) {
QString path = dir.absolutePath();
if (path != cDialogLastPath()) {
cSetDialogLastPath(path);
Local::writeUserSettings();
}
}
// check if extension of filename is present in filter
// it should be in first filter section on the first place
// place it there, if it is not
QString ext = QFileInfo(name).suffix(), fil = filter, sep = qsl(";;");
if (!ext.isEmpty()) {
if (QRegularExpression(qsl("^[a-zA-Z_0-9]+$")).match(ext).hasMatch()) {
QStringList filters = filter.split(sep);
if (filters.size() > 1) {
2019-02-27 11:36:19 +00:00
const auto &first = filters.at(0);
int32 start = first.indexOf(qsl("(*."));
if (start >= 0) {
if (!QRegularExpression(qsl("\\(\\*\\.") + ext + qsl("[\\)\\s]"), QRegularExpression::CaseInsensitiveOption).match(first).hasMatch()) {
QRegularExpressionMatch m = QRegularExpression(qsl(" \\*\\.") + ext + qsl("[\\)\\s]"), QRegularExpression::CaseInsensitiveOption).match(first);
if (m.hasMatch() && m.capturedStart() > start + 3) {
int32 oldpos = m.capturedStart(), oldend = m.capturedEnd();
fil = first.mid(0, start + 3) + ext + qsl(" *.") + first.mid(start + 3, oldpos - start - 3) + first.mid(oldend - 1) + sep + JoinStringList(filters.mid(1), sep);
} else {
fil = first.mid(0, start + 3) + ext + qsl(" *.") + first.mid(start + 3) + sep + JoinStringList(filters.mid(1), sep);
}
}
} else {
fil = QString();
}
} else {
fil = QString();
}
} else {
fil = QString();
}
}
return filedialogGetSaveFile(name, title, fil, name) ? name : QString();
}
QString path;
if (Global::DownloadPath().isEmpty()) {
path = File::DefaultDownloadPath();
} else if (Global::DownloadPath() == qsl("tmp")) {
path = cTempDir();
} else {
path = Global::DownloadPath();
}
if (name.isEmpty()) name = qsl(".unknown");
if (name.at(0) == QChar::fromLatin1('.')) {
if (!QDir().exists(path)) QDir().mkpath(path);
return filedialogDefaultName(prefix, name, path);
}
if (dir.path() != qsl(".")) {
path = dir.absolutePath() + '/';
}
QString nameStart, extension;
int32 extPos = name.lastIndexOf('.');
if (extPos >= 0) {
nameStart = name.mid(0, extPos);
extension = name.mid(extPos);
} else {
nameStart = name;
}
QString nameBase = path + nameStart;
name = nameBase + extension;
for (int i = 0; QFileInfo(name).exists(); ++i) {
name = nameBase + QString(" (%1)").arg(i + 2) + extension;
}
if (!QDir().exists(path)) QDir().mkpath(path);
return name;
}
QString FileNameForSave(
const QString &title,
const QString &filter,
const QString &prefix,
QString name,
bool savingAs,
const QDir &dir) {
const auto result = FileNameUnsafe(
title,
filter,
prefix,
name,
savingAs,
dir);
#ifdef Q_OS_WIN
const auto lower = result.trimmed().toLower();
const auto kBadExtensions = { qstr(".lnk"), qstr(".scf") };
const auto kMaskExtension = qsl(".download");
for (const auto extension : kBadExtensions) {
if (lower.endsWith(extension)) {
return result + kMaskExtension;
}
}
#endif // Q_OS_WIN
return result;
}
QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = false, const QString already = QString(), const QDir &dir = QDir()) {
auto alreadySavingFilename = data->loadingFilePath();
if (!alreadySavingFilename.isEmpty()) {
return alreadySavingFilename;
}
QString name, filter, caption, prefix;
const auto mimeType = Core::MimeTypeForName(data->mimeString());
QStringList p = mimeType.globPatterns();
QString pattern = p.isEmpty() ? QString() : p.front();
if (data->isVoiceMessage()) {
auto mp3 = data->hasMimeType(qstr("audio/mp3"));
name = already.isEmpty() ? (mp3 ? qsl(".mp3") : qsl(".ogg")) : already;
filter = mp3 ? qsl("MP3 Audio (*.mp3);;") : qsl("OGG Opus Audio (*.ogg);;");
filter += FileDialog::AllFilesFilter();
2019-06-19 15:09:03 +00:00
caption = tr::lng_save_audio(tr::now);
prefix = qsl("audio");
} else if (data->isVideoFile()) {
name = already.isEmpty() ? data->filename() : already;
if (name.isEmpty()) {
name = pattern.isEmpty() ? qsl(".mov") : pattern.replace('*', QString());
}
if (pattern.isEmpty()) {
filter = qsl("MOV Video (*.mov);;") + FileDialog::AllFilesFilter();
} else {
filter = mimeType.filterString() + qsl(";;") + FileDialog::AllFilesFilter();
}
2019-06-19 15:09:03 +00:00
caption = tr::lng_save_video(tr::now);
prefix = qsl("video");
} else {
name = already.isEmpty() ? data->filename() : already;
if (name.isEmpty()) {
name = pattern.isEmpty() ? qsl(".unknown") : pattern.replace('*', QString());
}
if (pattern.isEmpty()) {
filter = QString();
} else {
filter = mimeType.filterString() + qsl(";;") + FileDialog::AllFilesFilter();
}
2019-06-19 15:09:03 +00:00
caption = data->isAudioFile()
? tr::lng_save_audio_file(tr::now)
: tr::lng_save_file(tr::now);
prefix = qsl("doc");
}
return FileNameForSave(caption, filter, prefix, name, forceSavingAs, dir);
}
2018-07-13 21:25:47 +00:00
void DocumentOpenClickHandler::Open(
Data::FileOrigin origin,
not_null<DocumentData*> data,
HistoryItem *context) {
if (!data->date) {
return;
}
const auto openFile = [&] {
const auto &location = data->location(true);
if (data->size < App::kImageSizeLimit && location.accessEnable()) {
const auto guard = gsl::finally([&] {
location.accessDisable();
});
const auto path = location.name();
if (QImageReader(path).canRead()) {
Core::App().showDocument(data, context);
return;
}
}
LaunchWithWarning(location.name(), context);
};
2019-02-28 21:03:25 +00:00
const auto &location = data->location(true);
if (data->isTheme() && !location.isEmpty() && location.accessEnable()) {
Core::App().showDocument(data, context);
location.accessDisable();
} else if (data->canBePlayed()) {
2019-03-04 11:38:48 +00:00
if (data->isAudioFile()
|| data->isVoiceMessage()
|| data->isVideoMessage()) {
const auto msgId = context ? context->fullId() : FullMsgId();
2019-02-28 21:03:25 +00:00
Media::Player::instance()->playPause({ data, msgId });
} else if (context && data->isAnimation()) {
data->owner().requestAnimationPlayInline(context);
2019-02-28 21:03:25 +00:00
} else {
Core::App().showDocument(data, context);
}
} else if (!location.isEmpty()
|| (data->loaded()
&& !data->filepath(
FilePathResolve::SaveFromDataSilent).isEmpty())) {
openFile();
} else if (data->status == FileReady
|| data->status == FileDownloadFailed) {
DocumentSaveClickHandler::Save(origin, data);
}
}
void DocumentOpenClickHandler::onClickImpl() const {
Open(context(), document(), getActionItem());
}
2018-07-13 21:25:47 +00:00
void DocumentSaveClickHandler::Save(
Data::FileOrigin origin,
not_null<DocumentData*> data,
Mode mode) {
if (!data->date) {
return;
}
auto savename = QString();
if (mode != Mode::ToCacheOrFile || !data->saveToCache()) {
const auto filepath = data->filepath(FilePathResolve::Checked);
if (mode != Mode::ToNewFile
&& (!filepath.isEmpty()
|| !data->filepath(
FilePathResolve::SaveFromData).isEmpty())) {
return;
}
const auto fileinfo = QFileInfo(filepath);
const auto filedir = filepath.isEmpty()
? QDir()
: fileinfo.dir();
const auto filename = filepath.isEmpty()
? QString()
: fileinfo.fileName();
savename = documentSaveFilename(
data,
(mode == Mode::ToNewFile),
filename,
filedir);
if (savename.isEmpty()) {
return;
}
}
data->save(origin, savename);
}
void DocumentSaveClickHandler::onClickImpl() const {
Save(context(), document());
}
void DocumentCancelClickHandler::onClickImpl() const {
const auto data = document();
if (!data->date) return;
if (data->uploading()) {
2019-04-25 12:45:15 +00:00
if (const auto item = data->owner().message(context())) {
App::main()->cancelUploadLayer(item);
}
} else {
data->cancel();
}
}
void DocumentOpenWithClickHandler::Open(
Data::FileOrigin origin,
not_null<DocumentData*> data) {
if (!data->date) {
return;
}
if (data->loaded()) {
const auto path = data->filepath(
FilePathResolve::SaveFromDataSilent);
if (!path.isEmpty()) {
File::OpenWith(path, QCursor::pos());
return;
}
}
DocumentSaveClickHandler::Save(
origin,
data,
DocumentSaveClickHandler::Mode::ToFile);
}
void DocumentOpenWithClickHandler::onClickImpl() const {
Open(context(), document());
}
2018-07-13 21:25:47 +00:00
Data::FileOrigin StickerData::setOrigin() const {
return set.match([&](const MTPDinputStickerSetID &data) {
return Data::FileOrigin(
Data::FileOriginStickerSet(data.vid.v, data.vaccess_hash.v));
}, [&](const auto &) {
return Data::FileOrigin();
});
}
VoiceData::~VoiceData() {
if (!waveform.isEmpty()
&& waveform[0] == -1
&& waveform.size() > int32(sizeof(TaskId))) {
auto taskId = TaskId();
memcpy(&taskId, waveform.constData() + 1, sizeof(taskId));
Local::cancelTask(taskId);
}
}
DocumentData::DocumentData(not_null<Data::Session*> owner, DocumentId id)
: id(id)
, _owner(owner) {
}
Data::Session &DocumentData::owner() const {
return *_owner;
}
AuthSession &DocumentData::session() const {
return _owner->session();
}
2019-02-27 11:36:19 +00:00
void DocumentData::setattributes(
const QVector<MTPDocumentAttribute> &attributes) {
_flags &= ~(Flag::ImageType | kStreamingSupportedMask);
_flags |= kStreamingSupportedUnknown;
2019-02-27 11:36:19 +00:00
for (const auto &attribute : attributes) {
attribute.match([&](const MTPDdocumentAttributeImageSize & data) {
dimensions = QSize(data.vw.v, data.vh.v);
}, [&](const MTPDdocumentAttributeAnimated & data) {
2019-01-16 12:25:29 +00:00
if (type == FileDocument
|| type == StickerDocument
|| type == VideoDocument) {
type = AnimatedDocument;
_additional = nullptr;
2019-02-27 11:36:19 +00:00
}
}, [&](const MTPDdocumentAttributeSticker & data) {
if (type == FileDocument) {
type = StickerDocument;
_additional = std::make_unique<StickerData>();
}
if (sticker()) {
2019-02-27 11:36:19 +00:00
sticker()->alt = qs(data.valt);
2019-01-16 12:25:29 +00:00
if (sticker()->set.type() != mtpc_inputStickerSetID
2019-02-27 11:36:19 +00:00
|| data.vstickerset.type() == mtpc_inputStickerSetID) {
sticker()->set = data.vstickerset;
}
}
2019-02-27 11:36:19 +00:00
}, [&](const MTPDdocumentAttributeVideo & data) {
if (type == FileDocument) {
2019-02-27 11:36:19 +00:00
type = data.is_round_message()
2019-01-16 12:25:29 +00:00
? RoundVideoDocument
: VideoDocument;
}
2019-02-27 11:36:19 +00:00
_duration = data.vduration.v;
2019-03-05 13:56:27 +00:00
setMaybeSupportsStreaming(data.is_supports_streaming());
2019-02-27 11:36:19 +00:00
dimensions = QSize(data.vw.v, data.vh.v);
}, [&](const MTPDdocumentAttributeAudio & data) {
if (type == FileDocument) {
2019-02-27 11:36:19 +00:00
if (data.is_voice()) {
type = VoiceDocument;
_additional = std::make_unique<VoiceData>();
} else {
type = SongDocument;
_additional = std::make_unique<SongData>();
}
}
if (const auto voiceData = voice()) {
2019-02-27 11:36:19 +00:00
voiceData->duration = data.vduration.v;
voiceData->waveform = documentWaveformDecode(
qba(data.vwaveform));
voiceData->wavemax = voiceData->waveform.empty()
? uchar(0)
: *ranges::max_element(voiceData->waveform);
} else if (const auto songData = song()) {
2019-02-27 11:36:19 +00:00
songData->duration = data.vduration.v;
songData->title = qs(data.vtitle);
songData->performer = qs(data.vperformer);
}
2019-02-27 11:36:19 +00:00
}, [&](const MTPDdocumentAttributeFilename & data) {
_filename = qs(data.vfile_name);
// We don't want LTR/RTL mark/embedding/override/isolate chars
// in filenames, because they introduce a security issue, when
// an executable "Fil[x]gepj.exe" may look like "Filexe.jpeg".
QChar controls[] = {
0x200E, // LTR Mark
0x200F, // RTL Mark
0x202A, // LTR Embedding
0x202B, // RTL Embedding
0x202D, // LTR Override
0x202E, // RTL Override
0x2066, // LTR Isolate
0x2067, // RTL Isolate
};
for (const auto ch : controls) {
_filename = std::move(_filename).replace(ch, "_");
}
2019-02-27 11:36:19 +00:00
}, [&](const MTPDdocumentAttributeHasStickers &data) {
});
}
2019-05-10 08:38:23 +00:00
validateLottieSticker();
if (type == StickerDocument) {
if (dimensions.width() <= 0
|| dimensions.height() <= 0
|| dimensions.width() > StickerMaxSize
|| dimensions.height() > StickerMaxSize
|| !saveToCache()) {
type = FileDocument;
_additional = nullptr;
}
}
validateGoodThumbnail();
2019-03-05 13:56:27 +00:00
if (isAudioFile()
|| (isAnimation() && !isVideoMessage())
|| isVoiceMessage()) {
setMaybeSupportsStreaming(true);
}
}
2019-05-10 08:38:23 +00:00
void DocumentData::validateLottieSticker() {
2019-05-12 21:27:16 +00:00
if (type == FileDocument
&& _filename.endsWith(qstr(".tgs"))
&& _mimeString == qstr("application/x-tgsticker")
&& _thumbnail) {
2019-05-10 08:38:23 +00:00
type = StickerDocument;
_additional = std::make_unique<StickerData>();
sticker()->animated = true;
dimensions = kAnimatedStickerDimensions;
2019-05-10 08:38:23 +00:00
}
}
2019-01-16 12:25:29 +00:00
bool DocumentData::checkWallPaperProperties() {
if (type == WallPaperDocument) {
return true;
}
2019-01-16 12:25:29 +00:00
if (type != FileDocument
|| !_thumbnail
2019-01-16 12:25:29 +00:00
|| !dimensions.width()
|| !dimensions.height()
|| dimensions.width() > Storage::kMaxWallPaperDimension
|| dimensions.height() > Storage::kMaxWallPaperDimension
|| size > Storage::kMaxWallPaperInMemory) {
return false;
}
type = WallPaperDocument;
validateGoodThumbnail();
2019-01-16 12:25:29 +00:00
return true;
}
void DocumentData::updateThumbnails(
ImagePtr thumbnailInline,
ImagePtr thumbnail) {
if (thumbnailInline && !_thumbnailInline) {
_thumbnailInline = thumbnailInline;
}
if (thumbnail
&& (!_thumbnail
|| (sticker()
&& (_thumbnail->width() < thumbnail->width()
|| _thumbnail->height() < thumbnail->height())))) {
_thumbnail = thumbnail;
}
}
2019-01-16 12:25:29 +00:00
bool DocumentData::isWallPaper() const {
return (type == WallPaperDocument);
}
bool DocumentData::isPatternWallPaper() const {
return isWallPaper() && hasMimeType(qstr("image/png"));
}
bool DocumentData::hasThumbnail() const {
return !_thumbnail->isNull();
}
Image *DocumentData::thumbnailInline() const {
return _thumbnailInline ? _thumbnailInline.get() : nullptr;
}
Image *DocumentData::thumbnail() const {
return _thumbnail ? _thumbnail.get() : nullptr;
}
void DocumentData::loadThumbnail(Data::FileOrigin origin) {
if (_thumbnail && !_thumbnail->loaded()) {
_thumbnail->load(origin);
}
}
Storage::Cache::Key DocumentData::goodThumbnailCacheKey() const {
return Data::DocumentThumbCacheKey(_dc, id);
}
Image *DocumentData::goodThumbnail() const {
return _goodThumbnail.get();
}
void DocumentData::validateGoodThumbnail() {
if (!isVideoFile() && !isAnimation() && !isWallPaper()) {
_goodThumbnail = nullptr;
} else if (!_goodThumbnail && hasRemoteLocation()) {
_goodThumbnail = std::make_unique<Image>(
std::make_unique<Data::GoodThumbSource>(this));
}
}
void DocumentData::refreshGoodThumbnail() {
if (_goodThumbnail && hasRemoteLocation()) {
replaceGoodThumbnail(std::make_unique<Data::GoodThumbSource>(this));
}
}
void DocumentData::replaceGoodThumbnail(
std::unique_ptr<Images::Source> &&source) {
_goodThumbnail->replaceSource(std::move(source));
}
2019-03-01 11:16:55 +00:00
void DocumentData::setGoodThumbnailOnUpload(
QImage &&image,
QByteArray &&bytes) {
Expects(uploadingData != nullptr);
if (image.isNull()) {
return;
}
_goodThumbnail = std::make_unique<Image>(
std::make_unique<Images::LocalFileSource>(
QString(), std::move(bytes), "JPG", std::move(image)));
}
bool DocumentData::saveToCache() const {
return (type == StickerDocument && size < Storage::kMaxStickerInMemory)
|| (isAnimation() && size < Storage::kMaxAnimationInMemory)
2019-01-16 12:25:29 +00:00
|| (isVoiceMessage() && size < Storage::kMaxVoiceInMemory)
|| (type == WallPaperDocument);
}
void DocumentData::unload() {
// Forget thumb only when image cache limit exceeds.
//
// Also, you can't unload() images that you don't own
// from the destructor, because they're already destroyed.
//
//_thumbnailInline->unload();
//_thumbnail->unload();
2018-10-23 12:28:36 +00:00
if (sticker()) {
if (sticker()->image) {
2018-10-23 12:28:36 +00:00
ActiveCache().decrement(ComputeUsage(sticker()));
sticker()->image = nullptr;
2018-10-23 12:28:36 +00:00
}
}
_replyPreview.clear();
2018-10-30 06:17:34 +00:00
if (!_data.isEmpty()) {
ActiveCache().decrement(_data.size());
_data.clear();
}
}
2018-07-13 21:25:47 +00:00
void DocumentData::automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) {
2018-11-30 13:27:33 +00:00
if (status != FileReady || loaded() || cancelled()) {
return;
} else if (!item && type != StickerDocument && !isAnimation()) {
return;
}
const auto toCache = saveToCache();
if (!toCache && Global::AskDownloadPath()) {
// We need a filename, but we're supposed to ask user for it.
// No automatic download in this case.
return;
}
2018-11-30 13:27:33 +00:00
const auto filename = toCache
? QString()
: documentSaveFilename(this);
const auto shouldLoadFromCloud = !Data::IsExecutableName(filename)
&& (item
? Data::AutoDownload::Should(
Auth().settings().autoDownload(),
item->history()->peer,
this)
: Data::AutoDownload::Should(
Auth().settings().autoDownload(),
this));
2018-11-30 13:27:33 +00:00
const auto loadFromCloud = shouldLoadFromCloud
? LoadFromCloudOrLocal
: LoadFromLocalOnly;
save(
origin,
filename,
loadFromCloud,
true);
}
void DocumentData::automaticLoadSettingsChanged() {
2018-11-30 13:27:33 +00:00
if (!cancelled() || status != FileReady || loaded()) {
return;
}
_loader = nullptr;
_flags &= ~Flag::DownloadCancelled;
}
bool DocumentData::loaded(FilePathResolve resolve) const {
if (loading() && _loader->finished()) {
if (_loader->cancelled()) {
_flags |= Flag::DownloadCancelled;
destroyLoader();
} else {
auto that = const_cast<DocumentData*>(this);
that->setLocation(FileLocation(_loader->fileName()));
2018-10-23 12:28:36 +00:00
ActiveCache().decrement(that->_data.size());
that->_data = _loader->bytes();
2018-10-23 12:28:36 +00:00
ActiveCache().increment(that->_data.size());
2018-10-23 12:28:36 +00:00
if (that->sticker()
&& !that->sticker()->image
2018-10-23 12:28:36 +00:00
&& !_loader->imageData().isNull()) {
that->sticker()->image = std::make_unique<Image>(
std::make_unique<Images::LocalFileSource>(
QString(),
_data,
_loader->imageFormat(),
_loader->imageData()));
2018-10-23 12:28:36 +00:00
ActiveCache().increment(ComputeUsage(that->sticker()));
}
that->refreshGoodThumbnail();
2018-11-23 14:39:14 +00:00
destroyLoader();
if (!that->_data.isEmpty() || that->getStickerLarge()) {
ActiveCache().up(that);
}
}
_owner->notifyDocumentLayoutChanged(this);
}
return !data().isEmpty() || !filepath(resolve).isEmpty();
}
void DocumentData::destroyLoader() const {
if (!_loader) {
return;
}
const auto loader = base::take(_loader);
2018-11-30 13:27:33 +00:00
if (cancelled()) {
2018-11-23 14:39:14 +00:00
loader->cancel();
}
loader->stop();
}
bool DocumentData::loading() const {
return (_loader != nullptr);
}
QString DocumentData::loadingFilePath() const {
return loading() ? _loader->fileName() : QString();
}
bool DocumentData::displayLoading() const {
return loading()
? (!_loader->loadingLocal() || !_loader->autoLoading())
: (uploading() && !waitingForAlbum());
}
float64 DocumentData::progress() const {
if (uploading()) {
if (uploadingData->size > 0) {
const auto result = float64(uploadingData->offset)
/ uploadingData->size;
return snap(result, 0., 1.);
}
return 0.;
}
return loading() ? _loader->currentProgress() : (loaded() ? 1. : 0.);
}
int DocumentData::loadOffset() const {
return loading() ? _loader->currentOffset() : 0;
}
bool DocumentData::uploading() const {
return (uploadingData != nullptr);
}
bool DocumentData::loadedInMediaCache() const {
return (_flags & Flag::LoadedInMediaCache);
}
void DocumentData::setLoadedInMediaCache(bool loaded) {
const auto flags = loaded
? (_flags | Flag::LoadedInMediaCache)
: (_flags & ~Flag::LoadedInMediaCache);
if (_flags == flags) {
return;
}
_flags = flags;
if (!this->loaded()) {
if (loadedInMediaCache()) {
Local::writeFileLocation(
mediaKey(),
FileLocation::InMediaCacheLocation());
} else {
Local::removeFileLocation(mediaKey());
}
owner().requestDocumentViewRepaint(this);
}
}
void DocumentData::setLoadedInMediaCacheLocation() {
_location = FileLocation();
_flags |= Flag::LoadedInMediaCache;
}
void DocumentData::setWaitingForAlbum() {
if (uploading()) {
uploadingData->waitingForAlbum = true;
}
}
bool DocumentData::waitingForAlbum() const {
return uploading() && uploadingData->waitingForAlbum;
}
void DocumentData::save(
2018-07-13 21:25:47 +00:00
Data::FileOrigin origin,
const QString &toFile,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
if (loaded(FilePathResolve::Checked)) {
auto &l = location(true);
if (!toFile.isEmpty()) {
if (!_data.isEmpty()) {
QFile f(toFile);
f.open(QIODevice::WriteOnly);
f.write(_data);
f.close();
setLocation(FileLocation(toFile));
Local::writeFileLocation(mediaKey(), FileLocation(toFile));
} else if (l.accessEnable()) {
2019-03-11 05:45:06 +00:00
const auto &alreadyName = l.name();
if (alreadyName != toFile) {
QFile(toFile).remove();
QFile(alreadyName).copy(toFile);
}
l.accessDisable();
}
}
return;
}
if (_loader) {
if (!_loader->setFileName(toFile)) {
cancel();
_flags &= ~Flag::DownloadCancelled;
}
}
if (_loader) {
2018-11-30 13:27:33 +00:00
if (fromCloud == LoadFromCloudOrLocal) {
_loader->permitLoadFromCloud();
}
} else {
status = FileReady;
auto reader = owner().documentStreamedReader(this, origin, true);
if (reader) {
_loader = std::make_unique<Storage::StreamedFileDownloader>(
id,
_dc,
origin,
2019-04-12 07:34:39 +00:00
Data::DocumentCacheKey(_dc, id),
mediaKey(),
std::move(reader),
toFile,
size,
locationType(),
(saveToCache() ? LoadToCacheAsWell : LoadToFileOnly),
fromCloud,
autoLoading,
cacheTag());
} else if (hasWebLocation()) {
_loader = std::make_unique<mtpFileLoader>(
_urlLocation,
2018-07-13 16:49:46 +00:00
size,
fromCloud,
autoLoading,
cacheTag());
} else if (!_access && !_url.isEmpty()) {
_loader = std::make_unique<webFileLoader>(
2018-07-13 16:49:46 +00:00
_url,
toFile,
fromCloud,
autoLoading,
cacheTag());
} else {
_loader = std::make_unique<mtpFileLoader>(
StorageFileLocation(
_dc,
session().userId(),
MTP_inputDocumentFileLocation(
MTP_long(id),
MTP_long(_access),
MTP_bytes(_fileReference),
MTP_string(QString()))),
2018-07-13 21:25:47 +00:00
origin,
2018-07-13 16:49:46 +00:00
locationType(),
toFile,
size,
(saveToCache() ? LoadToCacheAsWell : LoadToFileOnly),
fromCloud,
autoLoading,
cacheTag());
}
QObject::connect(
_loader.get(),
&FileLoader::progress,
App::main(),
[=](FileLoader *l) { App::main()->documentLoadProgress(l); });
QObject::connect(
_loader.get(),
&FileLoader::failed,
App::main(),
&MainWidget::documentLoadFailed);
2018-11-30 13:27:33 +00:00
}
if (loading()) {
_loader->start();
}
_owner->notifyDocumentLayoutChanged(this);
}
void DocumentData::cancel() {
if (!loading()) {
return;
}
_flags |= Flag::DownloadCancelled;
destroyLoader();
_owner->notifyDocumentLayoutChanged(this);
2018-11-30 13:27:33 +00:00
App::main()->documentLoadProgress(this);
}
2018-11-30 13:27:33 +00:00
bool DocumentData::cancelled() const {
return (_flags & Flag::DownloadCancelled);
2018-11-30 13:27:33 +00:00
}
VoiceWaveform documentWaveformDecode(const QByteArray &encoded5bit) {
auto bitsCount = static_cast<int>(encoded5bit.size() * 8);
auto valuesCount = bitsCount / 5;
if (!valuesCount) {
return VoiceWaveform();
}
// Read each 5 bit of encoded5bit as 0-31 unsigned char.
// We count the index of the byte in which the desired 5-bit sequence starts.
// And then we read a uint16 starting from that byte to guarantee to get all of those 5 bits.
//
// BUT! if it is the last byte we have, we're not allowed to read a uint16 starting with it.
// Because it will be an overflow (we'll access one byte after the available memory).
// We see, that only the last 5 bits could start in the last available byte and be problematic.
// So we read in a general way all the entries in a general way except the last one.
auto result = VoiceWaveform(valuesCount, 0);
auto bitsData = encoded5bit.constData();
for (auto i = 0, l = valuesCount - 1; i != l; ++i) {
auto byteIndex = (i * 5) / 8;
auto bitShift = (i * 5) % 8;
auto value = *reinterpret_cast<const uint16*>(bitsData + byteIndex);
result[i] = static_cast<char>((value >> bitShift) & 0x1F);
}
auto lastByteIndex = ((valuesCount - 1) * 5) / 8;
auto lastBitShift = ((valuesCount - 1) * 5) % 8;
auto lastValue = (lastByteIndex == encoded5bit.size() - 1)
? static_cast<uint16>(*reinterpret_cast<const uchar*>(bitsData + lastByteIndex))
: *reinterpret_cast<const uint16*>(bitsData + lastByteIndex);
result[valuesCount - 1] = static_cast<char>((lastValue >> lastBitShift) & 0x1F);
return result;
}
QByteArray documentWaveformEncode5bit(const VoiceWaveform &waveform) {
auto bitsCount = waveform.size() * 5;
auto bytesCount = (bitsCount + 7) / 8;
auto result = QByteArray(bytesCount + 1, 0);
auto bitsData = result.data();
// Write each 0-31 unsigned char as 5 bit to result.
// We reserve one extra byte to be able to dereference any of required bytes
// as a uint16 without overflowing, even the byte with index "bytesCount - 1".
for (auto i = 0, l = waveform.size(); i < l; ++i) {
auto byteIndex = (i * 5) / 8;
auto bitShift = (i * 5) % 8;
auto value = (static_cast<uint16>(waveform[i]) & 0x1F) << bitShift;
*reinterpret_cast<uint16*>(bitsData + byteIndex) |= value;
}
result.resize(bytesCount);
return result;
}
QByteArray DocumentData::data() const {
2018-10-23 12:28:36 +00:00
if (!_data.isEmpty()) {
ActiveCache().up(const_cast<DocumentData*>(this));
}
return _data;
}
const FileLocation &DocumentData::location(bool check) const {
if (check && !_location.check()) {
const auto location = Local::readFileLocation(mediaKey());
const auto that = const_cast<DocumentData*>(this);
if (location.inMediaCache()) {
that->setLoadedInMediaCacheLocation();
} else {
that->_location = location;
}
}
return _location;
}
void DocumentData::setLocation(const FileLocation &loc) {
if (loc.inMediaCache()) {
setLoadedInMediaCacheLocation();
} else if (loc.check()) {
_location = loc;
}
}
QString DocumentData::filepath(FilePathResolve resolve) const {
bool check = (resolve != FilePathResolve::Cached);
QString result = (check && _location.name().isEmpty()) ? QString() : location(check).name();
bool saveFromData = result.isEmpty() && !data().isEmpty();
if (saveFromData) {
if (resolve != FilePathResolve::SaveFromData
&& resolve != FilePathResolve::SaveFromDataSilent) {
saveFromData = false;
} else if (resolve == FilePathResolve::SaveFromDataSilent
&& Global::AskDownloadPath()) {
saveFromData = false;
}
}
if (saveFromData) {
QString filename = documentSaveFilename(this);
if (!filename.isEmpty()) {
QFile f(filename);
if (f.open(QIODevice::WriteOnly)) {
if (f.write(data()) == data().size()) {
f.close();
const_cast<DocumentData*>(this)->_location = FileLocation(filename);
Local::writeFileLocation(mediaKey(), _location);
result = filename;
}
}
}
}
return result;
}
bool DocumentData::isStickerSetInstalled() const {
Expects(sticker() != nullptr);
const auto &sets = _owner->stickerSets();
return sticker()->set.match([&](const MTPDinputStickerSetID &data) {
const auto i = sets.constFind(data.vid.v);
return (i != sets.cend())
&& !(i->flags & MTPDstickerSet::Flag::f_archived)
&& (i->flags & MTPDstickerSet::Flag::f_installed_date);
}, [&](const MTPDinputStickerSetShortName &data) {
const auto name = qs(data.vshort_name).toLower();
for (const auto &set : sets) {
if (set.shortName.toLower() == name) {
return !(set.flags & MTPDstickerSet::Flag::f_archived)
&& (set.flags & MTPDstickerSet::Flag::f_installed_date);
}
}
return false;
}, [&](const MTPDinputStickerSetEmpty &) {
return false;
});
}
Image *DocumentData::getReplyPreview(Data::FileOrigin origin) {
if (!_thumbnail) {
return nullptr;
} else if (_replyPreview
&& (_replyPreview.good() || !_thumbnail->loaded())) {
return _replyPreview.image();
}
const auto option = isVideoMessage()
? Images::Option::Circled
: Images::Option::None;
if (_thumbnail->loaded()) {
_replyPreview.prepare(
_thumbnail.get(),
origin,
option);
} else {
_thumbnail->load(origin);
if (_thumbnailInline) {
_replyPreview.prepare(
_thumbnailInline.get(),
origin,
option | Images::Option::Blurred);
}
}
return _replyPreview.image();
}
StickerData *DocumentData::sticker() const {
return (type == StickerDocument)
? static_cast<StickerData*>(_additional.get())
: nullptr;
}
void DocumentData::checkStickerLarge() {
const auto data = sticker();
if (!data) return;
2018-07-13 21:25:47 +00:00
automaticLoad(stickerSetOrigin(), nullptr);
if (!data->image && !data->animated && loaded()) {
if (_data.isEmpty()) {
2018-07-13 21:25:47 +00:00
const auto &loc = location(true);
if (loc.accessEnable()) {
data->image = std::make_unique<Image>(
std::make_unique<Images::LocalFileSource>(loc.name()));
loc.accessDisable();
}
} else {
auto format = QByteArray();
auto image = App::readImage(_data, &format, false);
data->image = std::make_unique<Image>(
std::make_unique<Images::LocalFileSource>(
QString(),
_data,
format,
std::move(image)));
}
2018-10-23 12:28:36 +00:00
if (const auto usage = ComputeUsage(data)) {
ActiveCache().increment(usage);
ActiveCache().up(this);
}
}
}
void DocumentData::checkStickerSmall() {
if (thumbnailEnoughForSticker()) {
_thumbnail->load(stickerSetOrigin());
2018-07-13 21:25:47 +00:00
} else {
checkStickerLarge();
2018-07-13 21:25:47 +00:00
}
}
Image *DocumentData::getStickerLarge() {
checkStickerLarge();
if (const auto data = sticker()) {
return data->image.get();
}
return nullptr;
}
Image *DocumentData::getStickerSmall() {
if (thumbnailEnoughForSticker()) {
return _thumbnail->isNull() ? nullptr : _thumbnail.get();
2018-07-13 21:25:47 +00:00
} else if (const auto data = sticker()) {
return data->image.get();
2018-07-13 21:25:47 +00:00
}
return nullptr;
2018-07-13 21:25:47 +00:00
}
Data::FileOrigin DocumentData::stickerSetOrigin() const {
if (const auto data = sticker()) {
if (const auto result = data->setOrigin()) {
return result;
} else if (Stickers::IsFaved(this)) {
return Data::FileOriginStickerSet(Stickers::FavedSetId, 0);
}
2018-07-13 21:25:47 +00:00
}
return Data::FileOrigin();
}
Data::FileOrigin DocumentData::stickerOrGifOrigin() const {
return (sticker()
? stickerSetOrigin()
: isGifv()
? Data::FileOriginSavedGifs()
: Data::FileOrigin());
}
SongData *DocumentData::song() {
return isSong()
? static_cast<SongData*>(_additional.get())
: nullptr;
}
const SongData *DocumentData::song() const {
return const_cast<DocumentData*>(this)->song();
}
VoiceData *DocumentData::voice() {
return isVoiceMessage()
? static_cast<VoiceData*>(_additional.get())
: nullptr;
}
const VoiceData *DocumentData::voice() const {
return const_cast<DocumentData*>(this)->voice();
}
bool DocumentData::hasRemoteLocation() const {
return (_dc != 0 && _access != 0);
}
bool DocumentData::useStreamingLoader() const {
return isAnimation()
|| isVideoFile()
|| isAudioFile()
|| isVoiceMessage();
}
2019-02-27 11:36:19 +00:00
bool DocumentData::canBeStreamed() const {
// For now video messages are not streamed.
return hasRemoteLocation() && supportsStreaming() && !isVideoMessage();
2019-02-27 11:36:19 +00:00
}
bool DocumentData::canBePlayed() const {
return !(_flags & Flag::StreamingPlaybackFailed)
&& useStreamingLoader()
2019-02-27 11:36:19 +00:00
&& (loaded() || canBeStreamed());
}
void DocumentData::setInappPlaybackFailed() {
_flags |= Flag::StreamingPlaybackFailed;
}
bool DocumentData::inappPlaybackFailed() const {
return (_flags & Flag::StreamingPlaybackFailed);
}
auto DocumentData::createStreamingLoader(
Data::FileOrigin origin,
bool forceRemoteLoader) const
-> std::unique_ptr<Media::Streaming::Loader> {
if (!useStreamingLoader()) {
return nullptr;
}
if (!forceRemoteLoader) {
const auto &location = this->location(true);
if (!data().isEmpty()) {
return Media::Streaming::MakeBytesLoader(data());
} else if (!location.isEmpty() && location.accessEnable()) {
auto result = Media::Streaming::MakeFileLoader(location.name());
location.accessDisable();
return result;
}
2019-03-04 12:26:29 +00:00
}
return hasRemoteLocation()
? std::make_unique<Media::Streaming::LoaderMtproto>(
&session().downloader(),
StorageFileLocation(
_dc,
session().userId(),
MTP_inputDocumentFileLocation(
MTP_long(id),
MTP_long(_access),
MTP_bytes(_fileReference),
MTP_string(QString()))),
size,
origin)
: nullptr;
}
bool DocumentData::hasWebLocation() const {
return !_urlLocation.url().isEmpty();
}
bool DocumentData::isNull() const {
return !hasRemoteLocation() && !hasWebLocation() && _url.isEmpty();
}
MTPInputDocument DocumentData::mtpInput() const {
if (_access) {
return MTP_inputDocument(
MTP_long(id),
2018-07-13 16:49:46 +00:00
MTP_long(_access),
MTP_bytes(_fileReference));
}
return MTP_inputDocumentEmpty();
}
QByteArray DocumentData::fileReference() const {
return _fileReference;
}
void DocumentData::refreshFileReference(const QByteArray &value) {
_fileReference = value;
_thumbnail->refreshFileReference(value);
if (const auto data = sticker()) {
data->loc.refreshFileReference(value);
}
}
void DocumentData::refreshStickerThumbFileReference() {
if (const auto data = sticker()) {
if (_thumbnail->loading()) {
data->loc.refreshFileReference(
_thumbnail->location().fileReference());
}
}
}
QString DocumentData::filename() const {
return _filename;
}
QString DocumentData::mimeString() const {
return _mimeString;
}
bool DocumentData::hasMimeType(QLatin1String mime) const {
return !_mimeString.compare(mime, Qt::CaseInsensitive);
}
void DocumentData::setMimeString(const QString &mime) {
_mimeString = mime;
}
MediaKey DocumentData::mediaKey() const {
2018-07-13 21:25:47 +00:00
return ::mediaKey(locationType(), _dc, id);
}
Storage::Cache::Key DocumentData::cacheKey() const {
if (hasWebLocation()) {
return Data::WebDocumentCacheKey(_urlLocation);
} else if (!_access && !_url.isEmpty()) {
return Data::UrlCacheKey(_url);
} else {
return Data::DocumentCacheKey(_dc, id);
}
}
uint8 DocumentData::cacheTag() const {
if (type == StickerDocument) {
return Data::kStickerCacheTag;
} else if (isVoiceMessage()) {
return Data::kVoiceMessageCacheTag;
} else if (isVideoMessage()) {
return Data::kVideoMessageCacheTag;
} else if (isAnimation()) {
return Data::kAnimationCacheTag;
} else if (type == WallPaperDocument) {
return Data::kImageCacheTag;
}
return 0;
}
QString DocumentData::composeNameString() const {
if (auto songData = song()) {
return ComposeNameString(
_filename,
songData->title,
songData->performer);
}
return ComposeNameString(_filename, QString(), QString());
}
LocationType DocumentData::locationType() const {
return isVoiceMessage()
? AudioFileLocation
: isVideoFile()
? VideoFileLocation
: DocumentFileLocation;
}
bool DocumentData::isVoiceMessage() const {
return (type == VoiceDocument);
}
bool DocumentData::isVideoMessage() const {
return (type == RoundVideoDocument);
}
bool DocumentData::isAnimation() const {
return (type == AnimatedDocument)
|| isVideoMessage()
|| (hasMimeType(qstr("image/gif"))
&& !(_flags & Flag::StreamingPlaybackFailed));
}
bool DocumentData::isGifv() const {
return (type == AnimatedDocument)
&& hasMimeType(qstr("video/mp4"));
}
bool DocumentData::isTheme() const {
return
_filename.endsWith(
qstr(".tdesktop-theme"),
Qt::CaseInsensitive)
|| _filename.endsWith(
qstr(".tdesktop-palette"),
Qt::CaseInsensitive);
}
bool DocumentData::isSong() const {
return (type == SongDocument);
}
bool DocumentData::isAudioFile() const {
if (isVoiceMessage()) {
return false;
} else if (isSong()) {
return true;
}
const auto prefix = qstr("audio/");
if (!_mimeString.startsWith(prefix, Qt::CaseInsensitive)) {
2019-03-04 10:13:09 +00:00
if (_filename.endsWith(qstr(".opus"), Qt::CaseInsensitive)) {
return true;
}
return false;
}
const auto left = _mimeString.midRef(prefix.size()).toString();
const auto types = { qstr("x-wav"), qstr("wav"), qstr("mp4") };
return ranges::find(types, left) != end(types);
}
bool DocumentData::isSharedMediaMusic() const {
if (const auto songData = song()) {
return (songData->duration > 0);
}
return false;
}
bool DocumentData::isVideoFile() const {
return (type == VideoDocument);
}
TimeId DocumentData::getDuration() const {
if (const auto song = this->song()) {
return std::max(song->duration, 0);
} else if (const auto voice = this->voice()) {
return std::max(voice->duration, 0);
} else if (isAnimation() || isVideoFile()) {
return std::max(_duration, 0);
}
return -1;
}
bool DocumentData::isImage() const {
return (_flags & Flag::ImageType);
}
bool DocumentData::supportsStreaming() const {
return (_flags & kStreamingSupportedMask) == kStreamingSupportedMaybeYes;
2019-03-05 13:56:27 +00:00
}
void DocumentData::setNotSupportsStreaming() {
_flags &= ~kStreamingSupportedMask;
_flags |= kStreamingSupportedNo;
2019-03-05 13:56:27 +00:00
}
void DocumentData::setMaybeSupportsStreaming(bool supports) {
if ((_flags & kStreamingSupportedMask) == kStreamingSupportedNo) {
2019-03-05 13:56:27 +00:00
return;
}
_flags &= ~kStreamingSupportedMask;
_flags |= supports
? kStreamingSupportedMaybeYes
: kStreamingSupportedMaybeNo;
}
void DocumentData::recountIsImage() {
const auto isImage = !isAnimation()
&& !isVideoFile()
&& fileIsImage(filename(), mimeString());
if (isImage) {
_flags |= Flag::ImageType;
} else {
_flags &= ~Flag::ImageType;
}
}
bool DocumentData::thumbnailEnoughForSticker() const {
return !_thumbnail->isNull()
&& ((_thumbnail->width() >= 128) || (_thumbnail->height() >= 128));
}
2018-07-13 16:49:46 +00:00
void DocumentData::setRemoteLocation(
int32 dc,
uint64 access,
const QByteArray &fileReference) {
_fileReference = fileReference;
if (_dc != dc || _access != access) {
_dc = dc;
_access = access;
if (!isNull()) {
if (_location.check()) {
Local::writeFileLocation(mediaKey(), _location);
} else {
_location = Local::readFileLocation(mediaKey());
if (_location.inMediaCache()) {
setLoadedInMediaCacheLocation();
} else if (_location.isEmpty() && loadedInMediaCache()) {
Local::writeFileLocation(
mediaKey(),
FileLocation::InMediaCacheLocation());
}
}
}
}
validateGoodThumbnail();
}
void DocumentData::setContentUrl(const QString &url) {
_url = url;
}
void DocumentData::setWebLocation(const WebFileLocation &location) {
_urlLocation = location;
}
void DocumentData::collectLocalData(not_null<DocumentData*> local) {
if (local == this) {
return;
}
_owner->cache().copyIfEmpty(local->cacheKey(), cacheKey());
if (!local->_data.isEmpty()) {
2018-10-23 12:28:36 +00:00
ActiveCache().decrement(_data.size());
_data = local->_data;
2018-10-23 12:28:36 +00:00
ActiveCache().increment(_data.size());
if (!_data.isEmpty()) {
ActiveCache().up(this);
}
}
if (!local->_location.inMediaCache() && !local->_location.isEmpty()) {
_location = local->_location;
Local::writeFileLocation(mediaKey(), _location);
}
}
DocumentData::~DocumentData() {
destroyLoader();
2018-10-30 06:17:34 +00:00
unload();
ActiveCache().remove(this);
}
QString DocumentData::ComposeNameString(
const QString &filename,
const QString &songTitle,
const QString &songPerformer) {
if (songTitle.isEmpty() && songPerformer.isEmpty()) {
return filename.isEmpty() ? qsl("Unknown File") : filename;
}
if (songPerformer.isEmpty()) {
return songTitle;
}
auto trackTitle = (songTitle.isEmpty() ? qsl("Unknown Track") : songTitle);
return songPerformer + QString::fromUtf8(" \xe2\x80\x93 ") + trackTitle;
}
2018-12-05 08:07:17 +00:00
namespace Data {
QString FileExtension(const QString &filepath) {
const auto reversed = ranges::view::reverse(filepath);
const auto last = ranges::find_first_of(reversed, ".\\/");
if (last == reversed.end() || *last != '.') {
return QString();
}
return QString(last.base(), last - reversed.begin());
}
bool IsValidMediaFile(const QString &filepath) {
static const auto kExtensions = [] {
const auto list = qsl("\
2019-03-04 10:13:09 +00:00
16svx 2sf 3g2 3gp 8svx aac aaf aif aifc aiff amr amv ape asf ast au aup \
avchd avi brstm bwf cam cdda cust dat divx drc dsh dsf dts dtshd dtsma \
dvr-ms dwd evo f4a f4b f4p f4v fla flac flr flv gif gifv gsf gsm gym iff \
ifo it jam la ly m1v m2p m2ts m2v m4a m4p m4v mcf mid mk3d mka mks mkv mng \
mov mp1 mp2 mp3 mp4 minipsf mod mpc mpe mpeg mpg mpv mscz mt2 mus mxf mxl \
niff nsf nsv off ofr ofs ogg ogv opus ots pac ps psf psf2 psflib ptb qsf \
qt ra raw rka rm rmj rmvb roq s3m shn sib sid smi smp sol spc spx ssf svi \
swa swf tak ts tta txm usf vgm vob voc vox vqf wav webm wma wmv wrap wtv \
wv xm xml ym yuv").split(' ');
2018-12-05 08:07:17 +00:00
return base::flat_set<QString>(list.begin(), list.end());
}();
return ranges::binary_search(
kExtensions,
FileExtension(filepath).toLower());
}
bool IsExecutableName(const QString &filepath) {
static const auto kExtensions = [] {
const auto joined =
#ifdef Q_OS_MAC
2019-01-28 14:10:45 +00:00
qsl("\
action app bin command csh osx workflow terminal url caction mpkg pkg xhtm \
webarchive");
2018-12-05 08:07:17 +00:00
#elif defined Q_OS_LINUX // Q_OS_MAC
qsl("bin csh deb desktop ksh out pet pkg pup rpm run shar slp");
2018-12-05 08:07:17 +00:00
#else // Q_OS_MAC || Q_OS_LINUX
qsl("\
ad ade adp app application appref-ms asp asx bas bat bin cer cfg chi chm \
cmd cnt com cpl crt csh der diagcab dll drv eml exe fon fxp gadget grp hlp \
hpj hta htt inf ini ins inx isp isu its jar jnlp job js jse ksh lnk local \
mad maf mag mam manifest maq mar mas mat mau mav maw mcf mda mdb mde mdt \
mdw mdz mht mhtml mmc mof msc msg msh msh1 msh2 msh1xml msh2xml mshxml msi \
msp mst ops osd paf pcd pif pl plg prf prg ps1 ps2 ps1xml ps2xml psc1 psc2 \
pst reg rgs scf scr sct search-ms settingcontent-ms shb shs slk sys tmp \
u3p url vb vbe vbp vbs vbscript vdx vsmacros vsd vsdm vsdx vss vssm vssx \
vst vstm vstx vsw vsx vtx website ws wsc wsf wsh xbap xll xnk");
2018-12-05 08:07:17 +00:00
#endif // !Q_OS_MAC && !Q_OS_LINUX
const auto list = joined.split(' ');
return base::flat_set<QString>(list.begin(), list.end());
}();
return ranges::binary_search(
kExtensions,
FileExtension(filepath).toLower());
}
base::binary_guard ReadImageAsync(
not_null<DocumentData*> document,
FnMut<QImage(QImage)> postprocess,
FnMut<void(QImage&&)> done) {
2019-03-27 12:11:38 +00:00
auto result = base::binary_guard();
crl::async([
bytes = document->data(),
path = document->filepath(),
postprocess = std::move(postprocess),
2019-03-27 12:11:38 +00:00
guard = result.make_guard(),
callback = std::move(done)
]() mutable {
auto format = QByteArray();
if (bytes.isEmpty()) {
QFile f(path);
if (f.size() <= App::kImageSizeLimit
&& f.open(QIODevice::ReadOnly)) {
bytes = f.readAll();
}
}
auto image = bytes.isEmpty()
? QImage()
: App::readImage(bytes, &format, false, nullptr);
if (postprocess) {
image = postprocess(std::move(image));
}
2019-02-17 11:52:57 +00:00
crl::on_main(std::move(guard), [
image = std::move(image),
callback = std::move(callback)
]() mutable {
callback(std::move(image));
});
});
2019-03-27 12:11:38 +00:00
return result;
}
2019-03-05 13:56:27 +00:00
//void HandleUnsupportedMedia(
// not_null<DocumentData*> document,
// FullMsgId contextId) {
// using Error = ::Media::Streaming::Error;
//
// document->setInappPlaybackFailed();
// const auto filepath = document->filepath(
// DocumentData::FilePathResolve::SaveFromData);
2019-03-05 13:56:27 +00:00
// if (filepath.isEmpty()) {
// const auto save = [=] {
// Ui::hideLayer();
// DocumentSaveClickHandler::Save(
// (contextId ? contextId : Data::FileOrigin()),
// document,
2019-04-25 12:45:15 +00:00
// document->owner().message(contextId));
2019-03-05 13:56:27 +00:00
// };
// Ui::show(Box<ConfirmBox>(
2019-06-19 15:09:03 +00:00
// tr::lng_player_cant_stream(tr::now),
// tr::lng_player_download(tr::now),
// tr::lng_cancel(tr::now),
2019-03-05 13:56:27 +00:00
// save));
// } else if (IsValidMediaFile(filepath)) {
// File::Launch(filepath);
// }
//}
2018-12-05 08:07:17 +00:00
} // namespace Data