2017-09-26 11:49:16 +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.
|
2017-09-26 11:49:16 +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
|
2017-09-26 11:49:16 +00:00
|
|
|
*/
|
|
|
|
#include "data/data_document.h"
|
|
|
|
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2019-12-28 14:56:06 +00:00
|
|
|
#include "data/data_streaming.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "data/data_document_media.h"
|
2020-04-08 15:09:29 +00:00
|
|
|
#include "data/data_reply_preview.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
2020-05-04 13:38:49 +00:00
|
|
|
#include "main/main_session.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "core/file_utilities.h"
|
2018-06-13 22:06:21 +00:00
|
|
|
#include "core/mime_type.h"
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
|
|
|
#include "data/stickers/data_stickers_set.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2019-02-28 21:03:25 +00:00
|
|
|
#include "media/player/media_player_instance.h"
|
2019-04-10 11:26:15 +00:00
|
|
|
#include "media/streaming/media_streaming_loader_mtproto.h"
|
|
|
|
#include "media/streaming/media_streaming_loader_local.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "storage/localstorage.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "storage/storage_account.h"
|
2019-04-10 11:26:15 +00:00
|
|
|
#include "storage/streamed_file_downloader.h"
|
2019-12-04 12:15:58 +00:00
|
|
|
#include "storage/file_download_mtproto.h"
|
|
|
|
#include "storage/file_download_web.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2020-07-14 13:23:52 +00:00
|
|
|
#include "platform/platform_file_utilities.h"
|
|
|
|
#include "base/platform/base_platform_info.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2020-07-02 09:43:39 +00:00
|
|
|
#include "history/view/media/history_view_gif.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-08-27 11:35:58 +00:00
|
|
|
#include "storage/cache/storage_cache_database.h"
|
2018-12-05 08:07:17 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2019-06-12 20:11:41 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2019-10-20 10:22:46 +00:00
|
|
|
#include "base/base_file_utilities.h"
|
2018-01-21 14:49:42 +00:00
|
|
|
#include "mainwindow.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-04-29 13:08:09 +00:00
|
|
|
#include "lottie/lottie_animation.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2019-02-13 18:10:18 +00:00
|
|
|
|
2017-12-10 10:26:58 +00:00
|
|
|
namespace {
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2020-07-15 07:49:54 +00:00
|
|
|
const auto kAnimatedStickerDimensions = QSize(
|
|
|
|
kStickerSideSize,
|
|
|
|
kStickerSideSize);
|
2018-10-23 12:28:36 +00:00
|
|
|
|
2017-12-10 10:26:58 +00:00
|
|
|
QString JoinStringList(const QStringList &list, const QString &separator) {
|
|
|
|
const auto count = list.size();
|
|
|
|
if (!count) {
|
|
|
|
return QString();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2017-12-10 10:26:58 +00:00
|
|
|
|
|
|
|
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]);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
void LaunchWithWarning(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const QString &name,
|
|
|
|
HistoryItem *item) {
|
2018-12-07 16:15:58 +00:00
|
|
|
const auto warn = [&] {
|
|
|
|
if (!Data::IsExecutableName(name)) {
|
|
|
|
return false;
|
2020-06-18 18:04:16 +00:00
|
|
|
} else if (!Core::App().settings().exeLaunchWarning()) {
|
2018-12-07 16:15:58 +00:00
|
|
|
return false;
|
|
|
|
} else if (item && item->history()->peer->isVerified()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
2020-07-14 13:23:52 +00:00
|
|
|
const auto extension = '.' + Data::FileExtension(name);
|
|
|
|
if (Platform::IsWindows() && extension == u"."_q) {
|
|
|
|
// If you launch a file without extension, like "test", in case
|
|
|
|
// there is an executable file with the same name in this folder,
|
|
|
|
// like "test.bat", the executable file will be launched.
|
|
|
|
//
|
|
|
|
// Now we always force an Open With dialog box for such files.
|
|
|
|
crl::on_main([=] {
|
|
|
|
Platform::File::UnsafeShowOpenWith(name);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (!warn) {
|
2018-12-05 08:07:17 +00:00
|
|
|
File::Launch(name);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-18 18:04:16 +00:00
|
|
|
const auto callback = [=](bool checked) {
|
2018-12-05 08:07:17 +00:00
|
|
|
if (checked) {
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().setExeLaunchWarning(false);
|
|
|
|
Core::App().saveSettingsDelayed();
|
2018-12-05 08:07:17 +00:00
|
|
|
}
|
|
|
|
File::Launch(name);
|
2020-06-18 18:04:16 +00:00
|
|
|
};
|
2018-12-05 08:07:17 +00:00
|
|
|
Ui::show(Box<ConfirmDontWarnBox>(
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_launch_exe_warning(
|
2018-12-05 08:07:17 +00:00
|
|
|
lt_extension,
|
2019-06-18 16:53:27 +00:00
|
|
|
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),
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_launch_exe_sure(),
|
2018-12-05 08:07:17 +00:00
|
|
|
callback));
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:26:58 +00:00
|
|
|
} // 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;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:04:47 +00:00
|
|
|
QString FileNameUnsafe(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2018-03-06 16:04:47 +00:00
|
|
|
const QString &title,
|
|
|
|
const QString &filter,
|
|
|
|
const QString &prefix,
|
|
|
|
QString name,
|
|
|
|
bool savingAs,
|
|
|
|
const QDir &dir) {
|
2019-10-20 10:22:46 +00:00
|
|
|
name = base::FileNameFromUserString(name);
|
2020-06-18 18:04:16 +00:00
|
|
|
if (Core::App().settings().askDownloadPath() || savingAs) {
|
2017-09-26 11:49:16 +00:00
|
|
|
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);
|
2020-06-09 16:57:05 +00:00
|
|
|
Local::writeSettings();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2017-09-26 11:49:16 +00:00
|
|
|
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();
|
2017-12-10 10:26:58 +00:00
|
|
|
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);
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2017-12-10 10:26:58 +00:00
|
|
|
fil = first.mid(0, start + 3) + ext + qsl(" *.") + first.mid(start + 3) + sep + JoinStringList(filters.mid(1), sep);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fil = QString();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fil = QString();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fil = QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filedialogGetSaveFile(name, title, fil, name) ? name : QString();
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
auto path = [&] {
|
|
|
|
const auto path = Core::App().settings().downloadPath();
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return File::DefaultDownloadPath(session);
|
|
|
|
} else if (path == qsl("tmp")) {
|
2020-06-23 16:02:15 +00:00
|
|
|
return session->local().tempDirectory();
|
2020-06-18 18:04:16 +00:00
|
|
|
} else {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}();
|
2017-09-26 11:49:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:04:47 +00:00
|
|
|
QString FileNameForSave(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2018-03-06 16:04:47 +00:00
|
|
|
const QString &title,
|
|
|
|
const QString &filter,
|
|
|
|
const QString &prefix,
|
|
|
|
QString name,
|
|
|
|
bool savingAs,
|
|
|
|
const QDir &dir) {
|
|
|
|
const auto result = FileNameUnsafe(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2018-03-06 16:04:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:19:43 +00:00
|
|
|
QString DocumentFileNameForSave(
|
|
|
|
not_null<const DocumentData*> data,
|
|
|
|
bool forceSavingAs,
|
|
|
|
const QString &already,
|
|
|
|
const QDir &dir) {
|
2017-09-26 11:49:16 +00:00
|
|
|
auto alreadySavingFilename = data->loadingFilePath();
|
|
|
|
if (!alreadySavingFilename.isEmpty()) {
|
|
|
|
return alreadySavingFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString name, filter, caption, prefix;
|
2018-06-13 22:06:21 +00:00
|
|
|
const auto mimeType = Core::MimeTypeForName(data->mimeString());
|
2017-09-26 11:49:16 +00:00
|
|
|
QStringList p = mimeType.globPatterns();
|
|
|
|
QString pattern = p.isEmpty() ? QString() : p.front();
|
2017-12-10 10:26:58 +00:00
|
|
|
if (data->isVoiceMessage()) {
|
2017-11-05 11:00:48 +00:00
|
|
|
auto mp3 = data->hasMimeType(qstr("audio/mp3"));
|
2017-09-26 11:49:16 +00:00
|
|
|
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);
|
2017-09-26 11:49:16 +00:00
|
|
|
prefix = qsl("audio");
|
2017-12-10 10:26:58 +00:00
|
|
|
} else if (data->isVideoFile()) {
|
2017-11-05 11:00:48 +00:00
|
|
|
name = already.isEmpty() ? data->filename() : already;
|
2017-09-26 11:49:16 +00:00
|
|
|
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);
|
2017-09-26 11:49:16 +00:00
|
|
|
prefix = qsl("video");
|
|
|
|
} else {
|
2017-11-05 11:00:48 +00:00
|
|
|
name = already.isEmpty() ? data->filename() : already;
|
2017-09-26 11:49:16 +00:00
|
|
|
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);
|
2017-09-26 11:49:16 +00:00
|
|
|
prefix = qsl("doc");
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
return FileNameForSave(
|
|
|
|
&data->session(),
|
|
|
|
caption,
|
|
|
|
filter,
|
|
|
|
prefix,
|
|
|
|
name,
|
|
|
|
forceSavingAs,
|
|
|
|
dir);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:38:49 +00:00
|
|
|
DocumentClickHandler::DocumentClickHandler(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
FullMsgId context)
|
2020-06-09 09:36:40 +00:00
|
|
|
: FileClickHandler(&document->session(), context)
|
2020-05-04 13:38:49 +00:00
|
|
|
, _document(document) {
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void DocumentOpenClickHandler::Open(
|
|
|
|
Data::FileOrigin origin,
|
2017-12-15 16:25:47 +00:00
|
|
|
not_null<DocumentData*> data,
|
2019-03-11 14:35:11 +00:00
|
|
|
HistoryItem *context) {
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-03-13 09:35:47 +00:00
|
|
|
const auto openFile = [&] {
|
|
|
|
const auto &location = data->location(true);
|
|
|
|
if (data->size < App::kImageSizeLimit && location.accessEnable()) {
|
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
location.accessDisable();
|
|
|
|
});
|
2019-04-29 13:08:09 +00:00
|
|
|
const auto path = location.name();
|
2020-06-26 09:41:53 +00:00
|
|
|
if (Core::MimeTypeForFile(path).name().startsWith("image/") && QImageReader(path).canRead()) {
|
2019-04-29 13:08:09 +00:00
|
|
|
Core::App().showDocument(data, context);
|
|
|
|
return;
|
2019-03-13 09:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 09:06:50 +00:00
|
|
|
LaunchWithWarning(&data->session(), location.name(), context);
|
2019-03-13 09:35:47 +00:00
|
|
|
};
|
2020-04-10 13:18:51 +00:00
|
|
|
const auto media = data->createMediaView();
|
2019-02-28 21:03:25 +00:00
|
|
|
const auto &location = data->location(true);
|
2020-04-10 13:18:51 +00:00
|
|
|
if (data->isTheme() && media->loaded(true)) {
|
2019-02-28 21:03:25 +00:00
|
|
|
Core::App().showDocument(data, context);
|
|
|
|
location.accessDisable();
|
2020-04-10 13:18:51 +00:00
|
|
|
} else if (media->canBePlayed()) {
|
2019-03-04 11:38:48 +00:00
|
|
|
if (data->isAudioFile()
|
|
|
|
|| data->isVoiceMessage()
|
|
|
|
|| data->isVideoMessage()) {
|
2019-03-11 14:35:11 +00:00
|
|
|
const auto msgId = context ? context->fullId() : FullMsgId();
|
2019-02-28 21:03:25 +00:00
|
|
|
Media::Player::instance()->playPause({ data, msgId });
|
2020-07-02 09:43:39 +00:00
|
|
|
} else if (context
|
|
|
|
&& data->isAnimation()
|
|
|
|
&& HistoryView::Gif::CanPlayInline(data)) {
|
2019-03-11 14:35:11 +00:00
|
|
|
data->owner().requestAnimationPlayInline(context);
|
2019-02-28 21:03:25 +00:00
|
|
|
} else {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().showDocument(data, context);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-04-09 13:02:01 +00:00
|
|
|
} else if (data->saveFromDataSilent()) {
|
2019-03-13 09:35:47 +00:00
|
|
|
openFile();
|
2019-03-11 14:35:11 +00:00
|
|
|
} else if (data->status == FileReady
|
|
|
|
|| data->status == FileDownloadFailed) {
|
|
|
|
DocumentSaveClickHandler::Save(origin, data);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentOpenClickHandler::onClickImpl() const {
|
2020-06-09 09:36:40 +00:00
|
|
|
Open(context(), document(), getActionItem());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void DocumentSaveClickHandler::Save(
|
|
|
|
Data::FileOrigin origin,
|
2017-12-15 16:25:47 +00:00
|
|
|
not_null<DocumentData*> data,
|
2019-03-11 14:35:11 +00:00
|
|
|
Mode mode) {
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-03-11 14:35:11 +00:00
|
|
|
auto savename = QString();
|
|
|
|
if (mode != Mode::ToCacheOrFile || !data->saveToCache()) {
|
2020-04-09 13:02:01 +00:00
|
|
|
if (mode != Mode::ToNewFile && data->saveFromData()) {
|
2019-03-11 14:35:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-09 14:02:09 +00:00
|
|
|
const auto filepath = data->filepath(true);
|
2020-04-09 13:02:01 +00:00
|
|
|
const auto fileinfo = QFileInfo(
|
|
|
|
);
|
2019-03-11 14:35:11 +00:00
|
|
|
const auto filedir = filepath.isEmpty()
|
|
|
|
? QDir()
|
|
|
|
: fileinfo.dir();
|
|
|
|
const auto filename = filepath.isEmpty()
|
|
|
|
? QString()
|
|
|
|
: fileinfo.fileName();
|
2020-04-10 14:19:43 +00:00
|
|
|
savename = DocumentFileNameForSave(
|
2019-03-11 14:35:11 +00:00
|
|
|
data,
|
|
|
|
(mode == Mode::ToNewFile),
|
|
|
|
filename,
|
|
|
|
filedir);
|
|
|
|
if (savename.isEmpty()) {
|
|
|
|
return;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 14:35:11 +00:00
|
|
|
data->save(origin, savename);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentSaveClickHandler::onClickImpl() const {
|
2020-06-09 09:36:40 +00:00
|
|
|
Save(context(), document());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentCancelClickHandler::onClickImpl() const {
|
2020-05-04 13:38:49 +00:00
|
|
|
const auto data = document();
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
} else if (data->uploading()) {
|
2019-04-25 12:45:15 +00:00
|
|
|
if (const auto item = data->owner().message(context())) {
|
2020-06-12 14:09:04 +00:00
|
|
|
if (const auto m = App::main()) { // multi good
|
|
|
|
if (&m->session() == &data->session()) {
|
|
|
|
m->cancelUploadLayer(item);
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:35:47 +00:00
|
|
|
void DocumentOpenWithClickHandler::Open(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
not_null<DocumentData*> data) {
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:02:01 +00:00
|
|
|
data->saveFromDataSilent();
|
2020-04-09 14:02:09 +00:00
|
|
|
const auto path = data->filepath(true);
|
2020-04-09 13:02:01 +00:00
|
|
|
if (!path.isEmpty()) {
|
|
|
|
File::OpenWith(path, QCursor::pos());
|
|
|
|
} else {
|
|
|
|
DocumentSaveClickHandler::Save(
|
|
|
|
origin,
|
|
|
|
data,
|
|
|
|
DocumentSaveClickHandler::Mode::ToFile);
|
2019-03-13 09:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentOpenWithClickHandler::onClickImpl() const {
|
2020-06-09 09:36:40 +00:00
|
|
|
Open(context(), document());
|
2019-03-13 09:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
Data::FileOrigin StickerData::setOrigin() const {
|
|
|
|
return set.match([&](const MTPDinputStickerSetID &data) {
|
|
|
|
return Data::FileOrigin(
|
2019-07-05 13:38:38 +00:00
|
|
|
Data::FileOriginStickerSet(data.vid().v, data.vaccess_hash().v));
|
2018-07-13 21:25:47 +00:00
|
|
|
}, [&](const auto &) {
|
|
|
|
return Data::FileOrigin();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
VoiceData::~VoiceData() {
|
2018-01-27 09:21:57 +00:00
|
|
|
if (!waveform.isEmpty()
|
|
|
|
&& waveform[0] == -1
|
|
|
|
&& waveform.size() > int32(sizeof(TaskId))) {
|
2019-04-12 09:25:00 +00:00
|
|
|
auto taskId = TaskId();
|
2017-09-26 11:49:16 +00:00
|
|
|
memcpy(&taskId, waveform.constData() + 1, sizeof(taskId));
|
|
|
|
Local::cancelTask(taskId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
DocumentData::DocumentData(not_null<Data::Session*> owner, DocumentId id)
|
2017-09-26 11:49:16 +00:00
|
|
|
: id(id)
|
2019-01-25 14:37:28 +00:00
|
|
|
, _owner(owner) {
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:09:29 +00:00
|
|
|
DocumentData::~DocumentData() {
|
2020-05-26 11:59:45 +00:00
|
|
|
base::take(_thumbnail.loader).reset();
|
|
|
|
base::take(_videoThumbnail.loader).reset();
|
2020-04-08 15:09:29 +00:00
|
|
|
destroyLoader();
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
Data::Session &DocumentData::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Session &DocumentData::session() const {
|
2019-01-25 14:37:28 +00:00
|
|
|
return _owner->session();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void DocumentData::setattributes(
|
|
|
|
const QVector<MTPDocumentAttribute> &attributes) {
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags &= ~(Flag::ImageType | kStreamingSupportedMask);
|
|
|
|
_flags |= kStreamingSupportedUnknown;
|
2019-06-24 15:09:37 +00:00
|
|
|
|
|
|
|
validateLottieSticker();
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
for (const auto &attribute : attributes) {
|
2019-06-24 15:09:37 +00:00
|
|
|
attribute.match([&](const MTPDdocumentAttributeImageSize &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
dimensions = QSize(data.vw().v, data.vh().v);
|
2019-06-24 15:09:37 +00:00
|
|
|
}, [&](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
|
|
|
}
|
2019-06-24 15:09:37 +00:00
|
|
|
}, [&](const MTPDdocumentAttributeSticker &data) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (type == FileDocument) {
|
|
|
|
type = StickerDocument;
|
|
|
|
_additional = std::make_unique<StickerData>();
|
|
|
|
}
|
|
|
|
if (sticker()) {
|
2019-07-05 13:38:38 +00:00
|
|
|
sticker()->alt = qs(data.valt());
|
2019-01-16 12:25:29 +00:00
|
|
|
if (sticker()->set.type() != mtpc_inputStickerSetID
|
2019-07-05 13:38:38 +00:00
|
|
|
|| data.vstickerset().type() == mtpc_inputStickerSetID) {
|
|
|
|
sticker()->set = data.vstickerset();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 15:09:37 +00:00
|
|
|
}, [&](const MTPDdocumentAttributeVideo &data) {
|
2017-09-26 11:49:16 +00:00
|
|
|
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;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
_duration = data.vduration().v;
|
2019-03-05 13:56:27 +00:00
|
|
|
setMaybeSupportsStreaming(data.is_supports_streaming());
|
2019-07-05 13:38:38 +00:00
|
|
|
dimensions = QSize(data.vw().v, data.vh().v);
|
2019-06-24 15:09:37 +00:00
|
|
|
}, [&](const MTPDdocumentAttributeAudio &data) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (type == FileDocument) {
|
2019-02-27 11:36:19 +00:00
|
|
|
if (data.is_voice()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
type = VoiceDocument;
|
|
|
|
_additional = std::make_unique<VoiceData>();
|
|
|
|
} else {
|
|
|
|
type = SongDocument;
|
|
|
|
_additional = std::make_unique<SongData>();
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 10:26:58 +00:00
|
|
|
if (const auto voiceData = voice()) {
|
2019-07-05 13:38:38 +00:00
|
|
|
voiceData->duration = data.vduration().v;
|
2019-02-27 11:36:19 +00:00
|
|
|
voiceData->waveform = documentWaveformDecode(
|
2019-07-05 13:38:38 +00:00
|
|
|
data.vwaveform().value_or_empty());
|
2019-02-27 11:36:19 +00:00
|
|
|
voiceData->wavemax = voiceData->waveform.empty()
|
|
|
|
? uchar(0)
|
|
|
|
: *ranges::max_element(voiceData->waveform);
|
2017-12-10 10:26:58 +00:00
|
|
|
} else if (const auto songData = song()) {
|
2019-07-05 13:38:38 +00:00
|
|
|
songData->duration = data.vduration().v;
|
|
|
|
songData->title = qs(data.vtitle().value_or_empty());
|
|
|
|
songData->performer = qs(data.vperformer().value_or_empty());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-06-24 15:09:37 +00:00
|
|
|
}, [&](const MTPDdocumentAttributeFilename &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
_filename = qs(data.vfile_name());
|
2017-11-05 11:00:48 +00:00
|
|
|
|
2018-02-15 12:34:38 +00:00
|
|
|
// 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) {
|
|
|
|
});
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-07-15 07:49:54 +00:00
|
|
|
if (type == StickerDocument
|
2020-07-17 04:13:45 +00:00
|
|
|
&& ((size > Storage::kMaxStickerBytesSize)
|
|
|
|
|| (!sticker()->animated
|
|
|
|
&& !GoodStickerDimensions(
|
|
|
|
dimensions.width(),
|
|
|
|
dimensions.height())))) {
|
2020-07-15 07:49:54 +00:00
|
|
|
type = FileDocument;
|
|
|
|
_additional = nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-12-13 14:51:14 +00:00
|
|
|
if (isAudioFile() || isAnimation() || isVoiceMessage()) {
|
2019-03-05 13:56:27 +00:00
|
|
|
setMaybeSupportsStreaming(true);
|
|
|
|
}
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 08:38:23 +00:00
|
|
|
void DocumentData::validateLottieSticker() {
|
2019-05-12 21:27:16 +00:00
|
|
|
if (type == FileDocument
|
2019-05-27 12:57:26 +00:00
|
|
|
&& _mimeString == qstr("application/x-tgsticker")
|
2020-04-15 14:06:34 +00:00
|
|
|
&& hasThumbnail()) {
|
2019-05-10 08:38:23 +00:00
|
|
|
type = StickerDocument;
|
|
|
|
_additional = std::make_unique<StickerData>();
|
2019-05-27 12:57:26 +00:00
|
|
|
sticker()->animated = true;
|
|
|
|
dimensions = kAnimatedStickerDimensions;
|
2019-05-10 08:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 11:33:51 +00:00
|
|
|
void DocumentData::setDataAndCache(const QByteArray &data) {
|
2020-04-09 12:27:53 +00:00
|
|
|
if (const auto media = activeMediaView()) {
|
|
|
|
media->setBytes(data);
|
|
|
|
}
|
2019-09-06 11:33:51 +00:00
|
|
|
if (saveToCache() && data.size() <= Storage::kMaxFileInMemory) {
|
2020-02-21 07:58:50 +00:00
|
|
|
owner().cache().put(
|
2019-09-06 11:33:51 +00:00
|
|
|
cacheKey(),
|
|
|
|
Storage::Cache::Database::TaggedValue(
|
|
|
|
base::duplicate(data),
|
|
|
|
cacheTag()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
bool DocumentData::checkWallPaperProperties() {
|
2019-01-17 08:18:23 +00:00
|
|
|
if (type == WallPaperDocument) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-16 12:25:29 +00:00
|
|
|
if (type != FileDocument
|
2020-04-15 14:06:34 +00:00
|
|
|
|| !hasThumbnail()
|
2019-01-16 12:25:29 +00:00
|
|
|
|| !dimensions.width()
|
|
|
|
|| !dimensions.height()
|
|
|
|
|| dimensions.width() > Storage::kMaxWallPaperDimension
|
|
|
|
|| dimensions.height() > Storage::kMaxWallPaperDimension
|
2019-12-29 11:41:10 +00:00
|
|
|
|| size > Storage::kMaxWallPaperInMemory
|
|
|
|
|| mimeString() == qstr("application/x-tgwallpattern")) {
|
|
|
|
return false; // #TODO themes support svg patterns
|
2019-01-16 12:25:29 +00:00
|
|
|
}
|
|
|
|
type = WallPaperDocument;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
void DocumentData::updateThumbnails(
|
2020-04-08 15:09:29 +00:00
|
|
|
const QByteArray &inlineThumbnailBytes,
|
2020-05-22 11:15:09 +00:00
|
|
|
const ImageWithLocation &thumbnail,
|
|
|
|
const ImageWithLocation &videoThumbnail) {
|
2020-04-08 15:09:29 +00:00
|
|
|
if (!inlineThumbnailBytes.isEmpty()
|
|
|
|
&& _inlineThumbnailBytes.isEmpty()) {
|
|
|
|
_inlineThumbnailBytes = inlineThumbnailBytes;
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
2020-05-26 11:59:45 +00:00
|
|
|
Data::UpdateCloudFile(
|
|
|
|
_thumbnail,
|
|
|
|
thumbnail,
|
|
|
|
owner().cache(),
|
2020-05-27 09:01:25 +00:00
|
|
|
Data::kImageCacheTag,
|
2020-05-26 11:59:45 +00:00
|
|
|
[&](Data::FileOrigin origin) { loadThumbnail(origin); },
|
|
|
|
[&](QImage preloaded) {
|
2020-04-17 14:02:10 +00:00
|
|
|
if (const auto media = activeMediaView()) {
|
2020-05-26 11:59:45 +00:00
|
|
|
media->setThumbnail(std::move(preloaded));
|
2020-04-17 14:02:10 +00:00
|
|
|
}
|
2020-05-26 11:59:45 +00:00
|
|
|
});
|
|
|
|
Data::UpdateCloudFile(
|
|
|
|
_videoThumbnail,
|
|
|
|
videoThumbnail,
|
|
|
|
owner().cache(),
|
2020-05-27 09:01:25 +00:00
|
|
|
Data::kAnimationCacheTag,
|
2020-05-26 11:59:45 +00:00
|
|
|
[&](Data::FileOrigin origin) { loadVideoThumbnail(origin); });
|
2020-04-15 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
bool DocumentData::isWallPaper() const {
|
|
|
|
return (type == WallPaperDocument);
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:26:19 +00:00
|
|
|
bool DocumentData::isPatternWallPaper() const {
|
|
|
|
return isWallPaper() && hasMimeType(qstr("image/png"));
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
bool DocumentData::hasThumbnail() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _thumbnail.location.valid();
|
2020-04-15 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::thumbnailLoading() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _thumbnail.loader != nullptr;
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:06:34 +00:00
|
|
|
bool DocumentData::thumbnailFailed() const {
|
2020-05-27 09:01:25 +00:00
|
|
|
return (_thumbnail.flags & Data::CloudFile::Flag::Failed);
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::loadThumbnail(Data::FileOrigin origin) {
|
2020-04-15 14:06:34 +00:00
|
|
|
const auto autoLoading = false;
|
2020-06-08 15:17:33 +00:00
|
|
|
const auto finalCheck = [=] {
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto active = activeMediaView()) {
|
|
|
|
return !active->thumbnail();
|
2020-04-15 14:06:34 +00:00
|
|
|
}
|
2020-05-27 09:01:25 +00:00
|
|
|
return true;
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
const auto done = [=](QImage result) {
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto active = activeMediaView()) {
|
|
|
|
active->setThumbnail(std::move(result));
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
Data::LoadCloudFile(
|
|
|
|
&session(),
|
|
|
|
_thumbnail,
|
|
|
|
origin,
|
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
autoLoading,
|
|
|
|
Data::kImageCacheTag,
|
|
|
|
finalCheck,
|
|
|
|
done);
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:15:09 +00:00
|
|
|
const ImageLocation &DocumentData::thumbnailLocation() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _thumbnail.location;
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 13:50:04 +00:00
|
|
|
int DocumentData::thumbnailByteSize() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _thumbnail.byteSize;
|
2020-05-22 13:50:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:15:09 +00:00
|
|
|
bool DocumentData::hasVideoThumbnail() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _videoThumbnail.location.valid();
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::videoThumbnailLoading() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _videoThumbnail.loader != nullptr;
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::videoThumbnailFailed() const {
|
2020-05-27 09:01:25 +00:00
|
|
|
return (_videoThumbnail.flags & Data::CloudFile::Flag::Failed);
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::loadVideoThumbnail(Data::FileOrigin origin) {
|
|
|
|
const auto autoLoading = false;
|
2020-06-08 15:17:33 +00:00
|
|
|
const auto finalCheck = [=] {
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto active = activeMediaView()) {
|
|
|
|
return active->videoThumbnailContent().isEmpty();
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
2020-05-27 09:01:25 +00:00
|
|
|
return true;
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
const auto done = [=](QByteArray result) {
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto active = activeMediaView()) {
|
|
|
|
active->setVideoThumbnail(std::move(result));
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
Data::LoadCloudFile(
|
|
|
|
&session(),
|
|
|
|
_videoThumbnail,
|
|
|
|
origin,
|
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
autoLoading,
|
|
|
|
Data::kAnimationCacheTag,
|
|
|
|
finalCheck,
|
|
|
|
done);
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ImageLocation &DocumentData::videoThumbnailLocation() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _videoThumbnail.location;
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 13:50:04 +00:00
|
|
|
int DocumentData::videoThumbnailByteSize() const {
|
2020-05-26 11:59:45 +00:00
|
|
|
return _videoThumbnail.byteSize;
|
2020-05-22 13:50:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:12:56 +00:00
|
|
|
Storage::Cache::Key DocumentData::goodThumbnailCacheKey() const {
|
|
|
|
return Data::DocumentThumbCacheKey(_dc, id);
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
bool DocumentData::goodThumbnailChecked() const {
|
|
|
|
return (_goodThumbnailState & GoodThumbnailFlag::Mask)
|
|
|
|
== GoodThumbnailFlag::Checked;
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
bool DocumentData::goodThumbnailGenerating() const {
|
|
|
|
return (_goodThumbnailState & GoodThumbnailFlag::Mask)
|
|
|
|
== GoodThumbnailFlag::Generating;
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
bool DocumentData::goodThumbnailNoData() const {
|
|
|
|
return (_goodThumbnailState & GoodThumbnailFlag::Mask)
|
|
|
|
== GoodThumbnailFlag::NoData;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
void DocumentData::setGoodThumbnailGenerating() {
|
|
|
|
_goodThumbnailState = (_goodThumbnailState & ~GoodThumbnailFlag::Mask)
|
|
|
|
| GoodThumbnailFlag::Generating;
|
2018-10-25 09:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
void DocumentData::setGoodThumbnailDataReady() {
|
|
|
|
_goodThumbnailState = GoodThumbnailFlag::DataReady
|
|
|
|
| (goodThumbnailNoData()
|
|
|
|
? GoodThumbnailFlag(0)
|
|
|
|
: (_goodThumbnailState & GoodThumbnailFlag::Mask));
|
|
|
|
}
|
2018-10-25 08:47:06 +00:00
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
void DocumentData::setGoodThumbnailChecked(bool hasData) {
|
|
|
|
if (!hasData && (_goodThumbnailState & GoodThumbnailFlag::DataReady)) {
|
|
|
|
_goodThumbnailState &= ~GoodThumbnailFlag::DataReady;
|
|
|
|
_goodThumbnailState &= ~GoodThumbnailFlag::Mask;
|
|
|
|
Data::DocumentMedia::CheckGoodThumbnail(this);
|
2018-10-25 08:47:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-27 11:40:50 +00:00
|
|
|
_goodThumbnailState = (_goodThumbnailState & ~GoodThumbnailFlag::Mask)
|
|
|
|
| (hasData
|
|
|
|
? GoodThumbnailFlag::Checked
|
|
|
|
: GoodThumbnailFlag::NoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Data::DocumentMedia> DocumentData::createMediaView() {
|
|
|
|
if (auto result = activeMediaView()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
auto result = std::make_shared<Data::DocumentMedia>(this);
|
|
|
|
_media = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:15:47 +00:00
|
|
|
std::shared_ptr<Data::DocumentMedia> DocumentData::activeMediaView() const {
|
2020-03-27 11:40:50 +00:00
|
|
|
return _media.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setGoodThumbnailPhoto(not_null<PhotoData*> photo) {
|
|
|
|
_goodThumbnailPhoto = photo;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoData *DocumentData::goodThumbnailPhoto() const {
|
|
|
|
return _goodThumbnailPhoto;
|
2018-10-25 08:47:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:18 +00:00
|
|
|
Storage::Cache::Key DocumentData::bigFileBaseCacheKey() const {
|
|
|
|
return hasRemoteLocation()
|
|
|
|
? StorageFileLocation(
|
2019-06-26 10:01:04 +00:00
|
|
|
_dc,
|
|
|
|
session().userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
MTP_long(id),
|
|
|
|
MTP_long(_access),
|
|
|
|
MTP_bytes(_fileReference),
|
2020-05-20 12:28:18 +00:00
|
|
|
MTP_string())).bigFileBaseCacheKey()
|
|
|
|
: Storage::Cache::Key();
|
2019-06-26 10:01:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
bool DocumentData::saveToCache() const {
|
2020-04-09 12:27:53 +00:00
|
|
|
return (size < Storage::kMaxFileInMemory)
|
|
|
|
&& ((type == StickerDocument)
|
|
|
|
|| isAnimation()
|
|
|
|
|| isVoiceMessage()
|
|
|
|
|| (type == WallPaperDocument)
|
2020-06-16 15:31:50 +00:00
|
|
|
|| isTheme()
|
|
|
|
|| (mimeString() == qstr("image/png")
|
|
|
|
&& _filename.startsWith("image_")));
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::automaticLoadSettingsChanged() {
|
2020-04-09 14:02:09 +00:00
|
|
|
if (!cancelled() || status != FileReady) {
|
2017-09-26 11:49:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_loader = nullptr;
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags &= ~Flag::DownloadCancelled;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:17:10 +00:00
|
|
|
void DocumentData::finishLoad() {
|
2020-04-14 11:38:29 +00:00
|
|
|
// NB! _loader may be in ~FileLoader() already.
|
2020-04-10 11:17:10 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
destroyLoader();
|
|
|
|
});
|
2020-05-21 12:49:47 +00:00
|
|
|
if (!_loader || _loader->cancelled()) {
|
2020-04-10 11:17:10 +00:00
|
|
|
_flags |= Flag::DownloadCancelled;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLocation(FileLocation(_loader->fileName()));
|
|
|
|
setGoodThumbnailDataReady();
|
|
|
|
if (const auto media = activeMediaView()) {
|
|
|
|
media->setBytes(_loader->bytes());
|
|
|
|
media->checkStickerLarge(_loader.get());
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:06:34 +00:00
|
|
|
void DocumentData::destroyLoader() {
|
2019-04-30 12:53:44 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::loading() const {
|
2019-04-30 12:53:44 +00:00
|
|
|
return (_loader != nullptr);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DocumentData::loadingFilePath() const {
|
|
|
|
return loading() ? _loader->fileName() : QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::displayLoading() const {
|
2017-12-25 14:17:00 +00:00
|
|
|
return loading()
|
|
|
|
? (!_loader->loadingLocal() || !_loader->autoLoading())
|
|
|
|
: (uploading() && !waitingForAlbum());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float64 DocumentData::progress() const {
|
|
|
|
if (uploading()) {
|
2017-12-25 14:17:00 +00:00
|
|
|
if (uploadingData->size > 0) {
|
|
|
|
const auto result = float64(uploadingData->offset)
|
|
|
|
/ uploadingData->size;
|
|
|
|
return snap(result, 0., 1.);
|
|
|
|
}
|
|
|
|
return 0.;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-04-10 13:18:51 +00:00
|
|
|
return loading() ? _loader->currentProgress() : 0.;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 11:26:15 +00:00
|
|
|
int DocumentData::loadOffset() const {
|
2017-09-26 11:49:16 +00:00
|
|
|
return loading() ? _loader->currentOffset() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::uploading() const {
|
2017-12-25 14:17:00 +00:00
|
|
|
return (uploadingData != nullptr);
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:45:03 +00:00
|
|
|
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;
|
2020-04-09 14:02:09 +00:00
|
|
|
if (filepath().isEmpty()) {
|
2019-05-31 16:45:03 +00:00
|
|
|
if (loadedInMediaCache()) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(
|
2019-05-31 16:45:03 +00:00
|
|
|
mediaKey(),
|
|
|
|
FileLocation::InMediaCacheLocation());
|
|
|
|
} else {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().removeFileLocation(mediaKey());
|
2019-05-31 16:45:03 +00:00
|
|
|
}
|
|
|
|
owner().requestDocumentViewRepaint(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setLoadedInMediaCacheLocation() {
|
|
|
|
_location = FileLocation();
|
|
|
|
_flags |= Flag::LoadedInMediaCache;
|
|
|
|
}
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void DocumentData::setWaitingForAlbum() {
|
|
|
|
if (uploading()) {
|
|
|
|
uploadingData->waitingForAlbum = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::waitingForAlbum() const {
|
|
|
|
return uploading() && uploadingData->waitingForAlbum;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void DocumentData::save(
|
2018-07-13 21:25:47 +00:00
|
|
|
Data::FileOrigin origin,
|
2018-01-17 16:21:01 +00:00
|
|
|
const QString &toFile,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) {
|
2020-06-04 08:22:37 +00:00
|
|
|
if (const auto media = activeMediaView(); media && media->loaded(true)) {
|
2017-09-26 11:49:16 +00:00
|
|
|
auto &l = location(true);
|
|
|
|
if (!toFile.isEmpty()) {
|
2020-04-10 13:18:51 +00:00
|
|
|
if (!media->bytes().isEmpty()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
QFile f(toFile);
|
|
|
|
f.open(QIODevice::WriteOnly);
|
2020-04-10 13:18:51 +00:00
|
|
|
f.write(media->bytes());
|
2017-09-26 11:49:16 +00:00
|
|
|
f.close();
|
|
|
|
|
|
|
|
setLocation(FileLocation(toFile));
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(
|
|
|
|
mediaKey(),
|
|
|
|
FileLocation(toFile));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else if (l.accessEnable()) {
|
2019-03-11 05:45:06 +00:00
|
|
|
const auto &alreadyName = l.name();
|
2017-09-26 11:49:16 +00:00
|
|
|
if (alreadyName != toFile) {
|
|
|
|
QFile(toFile).remove();
|
|
|
|
QFile(alreadyName).copy(toFile);
|
|
|
|
}
|
|
|
|
l.accessDisable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_loader) {
|
|
|
|
if (!_loader->setFileName(toFile)) {
|
2019-04-10 11:26:15 +00:00
|
|
|
cancel();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 11:21:43 +00:00
|
|
|
_flags &= ~Flag::DownloadCancelled;
|
2017-09-26 11:49:16 +00:00
|
|
|
|
|
|
|
if (_loader) {
|
2018-11-30 13:27:33 +00:00
|
|
|
if (fromCloud == LoadFromCloudOrLocal) {
|
|
|
|
_loader->permitLoadFromCloud();
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
|
|
|
status = FileReady;
|
2019-12-28 14:56:06 +00:00
|
|
|
auto reader = owner().streaming().sharedReader(this, origin, true);
|
2019-04-12 09:25:00 +00:00
|
|
|
if (reader) {
|
2019-04-30 12:53:44 +00:00
|
|
|
_loader = std::make_unique<Storage::StreamedFileDownloader>(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session(),
|
2019-04-10 11:26:15 +00:00
|
|
|
id,
|
2019-04-11 07:59:18 +00:00
|
|
|
_dc,
|
2019-04-10 11:26:15 +00:00
|
|
|
origin,
|
2019-04-12 07:34:39 +00:00
|
|
|
Data::DocumentCacheKey(_dc, id),
|
|
|
|
mediaKey(),
|
2019-04-10 11:26:15 +00:00
|
|
|
std::move(reader),
|
|
|
|
toFile,
|
|
|
|
size,
|
|
|
|
locationType(),
|
|
|
|
(saveToCache() ? LoadToCacheAsWell : LoadToFileOnly),
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag());
|
2019-04-11 07:59:18 +00:00
|
|
|
} else if (hasWebLocation()) {
|
2019-04-30 12:53:44 +00:00
|
|
|
_loader = std::make_unique<mtpFileLoader>(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session(),
|
2019-03-25 11:50:42 +00:00
|
|
|
_urlLocation,
|
2018-07-13 16:49:46 +00:00
|
|
|
size,
|
|
|
|
fromCloud,
|
2018-08-28 21:09:55 +00:00
|
|
|
autoLoading,
|
|
|
|
cacheTag());
|
2018-03-04 20:04:13 +00:00
|
|
|
} else if (!_access && !_url.isEmpty()) {
|
2019-04-30 12:53:44 +00:00
|
|
|
_loader = std::make_unique<webFileLoader>(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session(),
|
2018-07-13 16:49:46 +00:00
|
|
|
_url,
|
|
|
|
toFile,
|
|
|
|
fromCloud,
|
2018-08-28 21:09:55 +00:00
|
|
|
autoLoading,
|
|
|
|
cacheTag());
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2019-04-30 12:53:44 +00:00
|
|
|
_loader = std::make_unique<mtpFileLoader>(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session(),
|
2019-03-25 11:50:42 +00:00
|
|
|
StorageFileLocation(
|
|
|
|
_dc,
|
|
|
|
session().userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
MTP_long(id),
|
|
|
|
MTP_long(_access),
|
|
|
|
MTP_bytes(_fileReference),
|
2019-07-05 13:38:38 +00:00
|
|
|
MTP_string())),
|
2018-07-13 21:25:47 +00:00
|
|
|
origin,
|
2018-07-13 16:49:46 +00:00
|
|
|
locationType(),
|
|
|
|
toFile,
|
|
|
|
size,
|
|
|
|
(saveToCache() ? LoadToCacheAsWell : LoadToFileOnly),
|
|
|
|
fromCloud,
|
2018-08-28 21:09:55 +00:00
|
|
|
autoLoading,
|
|
|
|
cacheTag());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-04-10 11:17:10 +00:00
|
|
|
handleLoaderUpdates();
|
2018-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
if (loading()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_loader->start();
|
|
|
|
}
|
2019-01-25 14:37:28 +00:00
|
|
|
_owner->notifyDocumentLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:17:10 +00:00
|
|
|
void DocumentData::handleLoaderUpdates() {
|
|
|
|
_loader->updates(
|
|
|
|
) | rpl::start_with_next_error_done([=] {
|
|
|
|
_owner->documentLoadProgress(this);
|
|
|
|
}, [=](bool started) {
|
2020-05-21 12:49:47 +00:00
|
|
|
if (started && _loader) {
|
2020-04-10 11:17:10 +00:00
|
|
|
const auto origin = _loader->fileOrigin();
|
|
|
|
const auto failedFileName = _loader->fileName();
|
|
|
|
const auto retry = [=] {
|
|
|
|
Ui::hideLayer();
|
|
|
|
save(origin, failedFileName);
|
|
|
|
};
|
|
|
|
Ui::show(Box<ConfirmBox>(
|
|
|
|
tr::lng_download_finish_failed(tr::now),
|
|
|
|
crl::guard(&session(), retry)));
|
|
|
|
} else {
|
|
|
|
// Sometimes we have LOCATION_INVALID error in documents / stickers.
|
|
|
|
// Sometimes FILE_REFERENCE_EXPIRED could not be handled.
|
|
|
|
//
|
|
|
|
//const auto openSettings = [=] {
|
2020-06-18 18:04:16 +00:00
|
|
|
// Core::App().settings().etDownloadPathBookmark(QByteArray());
|
|
|
|
// Core::App().settings().setDownloadPath(QString());
|
2020-04-10 11:17:10 +00:00
|
|
|
// Ui::show(Box<DownloadPathBox>());
|
|
|
|
//};
|
|
|
|
//Ui::show(Box<ConfirmBox>(
|
|
|
|
// tr::lng_download_path_failed(tr::now),
|
|
|
|
// tr::lng_download_path_settings(tr::now),
|
|
|
|
// crl::guard(&session(), openSettings)));
|
|
|
|
}
|
|
|
|
finishLoad();
|
|
|
|
status = FileDownloadFailed;
|
|
|
|
_owner->documentLoadFail(this, started);
|
|
|
|
}, [=] {
|
|
|
|
finishLoad();
|
|
|
|
_owner->documentLoadDone(this);
|
2020-05-21 12:49:47 +00:00
|
|
|
}, _loader->lifetime());
|
2020-04-10 11:17:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void DocumentData::cancel() {
|
2018-01-17 16:21:01 +00:00
|
|
|
if (!loading()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags |= Flag::DownloadCancelled;
|
|
|
|
destroyLoader();
|
2020-04-10 11:17:10 +00:00
|
|
|
_owner->documentLoadDone(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:27:33 +00:00
|
|
|
bool DocumentData::cancelled() const {
|
2019-04-30 12:53:44 +00:00
|
|
|
return (_flags & Flag::DownloadCancelled);
|
2018-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileLocation &DocumentData::location(bool check) const {
|
|
|
|
if (check && !_location.check()) {
|
2020-06-09 16:57:05 +00:00
|
|
|
const auto location = session().local().readFileLocation(mediaKey());
|
2019-05-31 16:45:03 +00:00
|
|
|
const auto that = const_cast<DocumentData*>(this);
|
|
|
|
if (location.inMediaCache()) {
|
|
|
|
that->setLoadedInMediaCacheLocation();
|
|
|
|
} else {
|
|
|
|
that->_location = location;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
return _location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setLocation(const FileLocation &loc) {
|
2019-05-31 16:45:03 +00:00
|
|
|
if (loc.inMediaCache()) {
|
|
|
|
setLoadedInMediaCacheLocation();
|
|
|
|
} else if (loc.check()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_location = loc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:02:09 +00:00
|
|
|
QString DocumentData::filepath(bool check) const {
|
2020-04-09 13:02:01 +00:00
|
|
|
return (check && _location.name().isEmpty())
|
2020-04-09 12:27:53 +00:00
|
|
|
? QString()
|
|
|
|
: location(check).name();
|
2020-04-09 13:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::saveFromData() {
|
2020-04-09 14:02:09 +00:00
|
|
|
return !filepath(true).isEmpty() || saveFromDataChecked();
|
2020-04-09 13:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::saveFromDataSilent() {
|
2020-04-09 14:02:09 +00:00
|
|
|
return !filepath(true).isEmpty()
|
2020-06-18 18:04:16 +00:00
|
|
|
|| (!Core::App().settings().askDownloadPath()
|
|
|
|
&& saveFromDataChecked());
|
2020-04-09 13:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::saveFromDataChecked() {
|
|
|
|
const auto media = activeMediaView();
|
|
|
|
if (!media) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto bytes = media->bytes();
|
|
|
|
if (bytes.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 14:19:43 +00:00
|
|
|
const auto path = DocumentFileNameForSave(this);
|
2020-04-09 13:02:01 +00:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto file = QFile(path);
|
|
|
|
if (!file.open(QIODevice::WriteOnly)
|
|
|
|
|| file.write(bytes) != bytes.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
_location = FileLocation(path);
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(mediaKey(), _location);
|
2020-04-09 13:02:01 +00:00
|
|
|
return true;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:21:57 +00:00
|
|
|
bool DocumentData::isStickerSetInstalled() const {
|
|
|
|
Expects(sticker() != nullptr);
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto &sets = _owner->stickers().sets();
|
2019-04-12 09:25:00 +00:00
|
|
|
return sticker()->set.match([&](const MTPDinputStickerSetID &data) {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto i = sets.find(data.vid().v);
|
2019-04-12 09:25:00 +00:00
|
|
|
return (i != sets.cend())
|
2020-05-28 10:00:51 +00:00
|
|
|
&& !(i->second->flags & MTPDstickerSet::Flag::f_archived)
|
|
|
|
&& (i->second->flags & MTPDstickerSet::Flag::f_installed_date);
|
2019-04-12 09:25:00 +00:00
|
|
|
}, [&](const MTPDinputStickerSetShortName &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto name = qs(data.vshort_name()).toLower();
|
2020-05-28 10:00:51 +00:00
|
|
|
for (const auto &[id, set] : sets) {
|
|
|
|
if (set->shortName.toLower() == name) {
|
|
|
|
return !(set->flags & MTPDstickerSet::Flag::f_archived)
|
|
|
|
&& (set->flags & MTPDstickerSet::Flag::f_installed_date);
|
2018-01-27 09:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 09:25:00 +00:00
|
|
|
return false;
|
2020-02-07 16:06:35 +00:00
|
|
|
}, [](const MTPDinputStickerSetEmpty &) {
|
2019-04-12 09:25:00 +00:00
|
|
|
return false;
|
2020-02-07 16:06:35 +00:00
|
|
|
}, [](const MTPDinputStickerSetAnimatedEmoji &) {
|
|
|
|
return false;
|
|
|
|
}, [](const MTPDinputStickerSetDice &) {
|
2019-08-01 09:22:49 +00:00
|
|
|
return false;
|
2019-04-12 09:25:00 +00:00
|
|
|
});
|
2018-01-27 09:21:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 15:03:38 +00:00
|
|
|
Image *DocumentData::getReplyPreview(Data::FileOrigin origin) {
|
2020-04-15 14:06:34 +00:00
|
|
|
if (!hasThumbnail()) {
|
2019-01-25 14:37:28 +00:00
|
|
|
return nullptr;
|
2020-04-08 15:09:29 +00:00
|
|
|
} else if (!_replyPreview) {
|
|
|
|
_replyPreview = std::make_unique<Data::ReplyPreview>(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
return _replyPreview->image(origin);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:21:57 +00:00
|
|
|
StickerData *DocumentData::sticker() const {
|
|
|
|
return (type == StickerDocument)
|
|
|
|
? static_cast<StickerData*>(_additional.get())
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
Data::FileOrigin DocumentData::stickerSetOrigin() const {
|
|
|
|
if (const auto data = sticker()) {
|
2018-08-28 11:32:27 +00:00
|
|
|
if (const auto result = data->setOrigin()) {
|
|
|
|
return result;
|
2020-06-08 17:24:36 +00:00
|
|
|
} else if (owner().stickers().isFaved(this)) {
|
|
|
|
return Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0);
|
2018-08-28 11:32:27 +00:00
|
|
|
}
|
2018-07-13 21:25:47 +00:00
|
|
|
}
|
|
|
|
return Data::FileOrigin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::FileOrigin DocumentData::stickerOrGifOrigin() const {
|
|
|
|
return (sticker()
|
|
|
|
? stickerSetOrigin()
|
|
|
|
: isGifv()
|
|
|
|
? Data::FileOriginSavedGifs()
|
|
|
|
: Data::FileOrigin());
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:21:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-04-12 08:00:06 +00:00
|
|
|
bool DocumentData::useStreamingLoader() const {
|
|
|
|
return isAnimation()
|
|
|
|
|| isVideoFile()
|
|
|
|
|| isAudioFile()
|
|
|
|
|| isVoiceMessage();
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
bool DocumentData::canBeStreamed() const {
|
2019-03-11 14:35:11 +00:00
|
|
|
// For now video messages are not streamed.
|
2019-12-12 13:26:53 +00:00
|
|
|
return hasRemoteLocation() && supportsStreaming();
|
2019-02-27 11:36:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 11:28:52 +00:00
|
|
|
void DocumentData::setInappPlaybackFailed() {
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags |= Flag::StreamingPlaybackFailed;
|
2019-03-04 11:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::inappPlaybackFailed() const {
|
2019-04-30 12:53:44 +00:00
|
|
|
return (_flags & Flag::StreamingPlaybackFailed);
|
2019-03-04 11:28:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
auto DocumentData::createStreamingLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool forceRemoteLoader) const
|
2019-02-13 18:10:18 +00:00
|
|
|
-> std::unique_ptr<Media::Streaming::Loader> {
|
2019-04-12 08:00:06 +00:00
|
|
|
if (!useStreamingLoader()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-12 09:25:00 +00:00
|
|
|
if (!forceRemoteLoader) {
|
2020-04-10 13:18:51 +00:00
|
|
|
const auto media = activeMediaView();
|
2019-04-12 09:25:00 +00:00
|
|
|
const auto &location = this->location(true);
|
2020-04-10 13:18:51 +00:00
|
|
|
if (media && !media->bytes().isEmpty()) {
|
|
|
|
return Media::Streaming::MakeBytesLoader(media->bytes());
|
2019-04-12 09:25:00 +00:00
|
|
|
} 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
|
|
|
}
|
2019-02-13 18:10:18 +00:00
|
|
|
return hasRemoteLocation()
|
|
|
|
? std::make_unique<Media::Streaming::LoaderMtproto>(
|
2019-04-12 10:50:41 +00:00
|
|
|
&session().downloader(),
|
2019-03-25 09:17:47 +00:00
|
|
|
StorageFileLocation(
|
|
|
|
_dc,
|
|
|
|
session().userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
MTP_long(id),
|
|
|
|
MTP_long(_access),
|
|
|
|
MTP_bytes(_fileReference),
|
2019-07-05 13:38:38 +00:00
|
|
|
MTP_string())),
|
2019-02-13 18:10:18 +00:00
|
|
|
size,
|
|
|
|
origin)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-04 20:04:13 +00:00
|
|
|
bool DocumentData::hasWebLocation() const {
|
2019-03-25 11:50:42 +00:00
|
|
|
return !_urlLocation.url().isEmpty();
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
bool DocumentData::isNull() const {
|
|
|
|
return !hasRemoteLocation() && !hasWebLocation() && _url.isEmpty();
|
2018-01-27 09:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2018-01-27 09:21:57 +00:00
|
|
|
}
|
|
|
|
return MTP_inputDocumentEmpty();
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:31:07 +00:00
|
|
|
QByteArray DocumentData::fileReference() const {
|
|
|
|
return _fileReference;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::refreshFileReference(const QByteArray &value) {
|
|
|
|
_fileReference = value;
|
2020-05-26 11:59:45 +00:00
|
|
|
_thumbnail.location.refreshFileReference(value);
|
|
|
|
_videoThumbnail.location.refreshFileReference(value);
|
2018-07-16 17:31:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:21:57 +00:00
|
|
|
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);
|
2018-01-27 09:21:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 11:35:58 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:09:55 +00:00
|
|
|
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;
|
2019-01-17 08:18:23 +00:00
|
|
|
} else if (type == WallPaperDocument) {
|
|
|
|
return Data::kImageCacheTag;
|
2018-08-28 21:09:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:21:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:26:58 +00:00
|
|
|
bool DocumentData::isVoiceMessage() const {
|
|
|
|
return (type == VoiceDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isVideoMessage() const {
|
|
|
|
return (type == RoundVideoDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isAnimation() const {
|
|
|
|
return (type == AnimatedDocument)
|
|
|
|
|| isVideoMessage()
|
2019-04-30 12:53:44 +00:00
|
|
|
|| (hasMimeType(qstr("image/gif"))
|
|
|
|
&& !(_flags & Flag::StreamingPlaybackFailed));
|
2017-12-10 10:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isGifv() const {
|
|
|
|
return (type == AnimatedDocument)
|
|
|
|
&& hasMimeType(qstr("video/mp4"));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isTheme() const {
|
|
|
|
return
|
2019-09-09 11:56:05 +00:00
|
|
|
_mimeString == qstr("application/x-tgtheme-tdesktop")
|
|
|
|
|| _filename.endsWith(
|
2017-12-10 10:26:58 +00:00
|
|
|
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()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-13 20:32:45 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-11-13 20:32:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto left = _mimeString.midRef(prefix.size()).toString();
|
|
|
|
const auto types = { qstr("x-wav"), qstr("wav"), qstr("mp4") };
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::contains(types, left);
|
2017-12-10 10:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isSharedMediaMusic() const {
|
|
|
|
if (const auto songData = song()) {
|
|
|
|
return (songData->duration > 0);
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:26:58 +00:00
|
|
|
bool DocumentData::isVideoFile() const {
|
|
|
|
return (type == VideoDocument);
|
|
|
|
}
|
|
|
|
|
2019-03-06 08:21:42 +00:00
|
|
|
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;
|
2017-12-10 10:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::isImage() const {
|
2019-04-30 12:53:44 +00:00
|
|
|
return (_flags & Flag::ImageType);
|
2018-11-07 14:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentData::supportsStreaming() const {
|
2019-04-30 12:53:44 +00:00
|
|
|
return (_flags & kStreamingSupportedMask) == kStreamingSupportedMaybeYes;
|
2019-03-05 13:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setNotSupportsStreaming() {
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags &= ~kStreamingSupportedMask;
|
|
|
|
_flags |= kStreamingSupportedNo;
|
2019-03-05 13:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setMaybeSupportsStreaming(bool supports) {
|
2019-04-30 12:53:44 +00:00
|
|
|
if ((_flags & kStreamingSupportedMask) == kStreamingSupportedNo) {
|
2019-03-05 13:56:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-30 12:53:44 +00:00
|
|
|
_flags &= ~kStreamingSupportedMask;
|
|
|
|
_flags |= supports
|
|
|
|
? kStreamingSupportedMaybeYes
|
|
|
|
: kStreamingSupportedMaybeNo;
|
2017-12-10 10:26:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void DocumentData::recountIsImage() {
|
2019-04-30 12:53:44 +00:00
|
|
|
const auto isImage = !isAnimation()
|
2018-11-07 14:07:20 +00:00
|
|
|
&& !isVideoFile()
|
|
|
|
&& fileIsImage(filename(), mimeString());
|
2019-04-30 12:53:44 +00:00
|
|
|
if (isImage) {
|
|
|
|
_flags |= Flag::ImageType;
|
|
|
|
} else {
|
|
|
|
_flags &= ~Flag::ImageType;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 16:49:46 +00:00
|
|
|
void DocumentData::setRemoteLocation(
|
|
|
|
int32 dc,
|
|
|
|
uint64 access,
|
|
|
|
const QByteArray &fileReference) {
|
|
|
|
_fileReference = fileReference;
|
2018-01-19 12:13:06 +00:00
|
|
|
if (_dc != dc || _access != access) {
|
|
|
|
_dc = dc;
|
|
|
|
_access = access;
|
2019-01-25 14:37:28 +00:00
|
|
|
if (!isNull()) {
|
2018-01-19 12:13:06 +00:00
|
|
|
if (_location.check()) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(mediaKey(), _location);
|
2018-01-19 12:13:06 +00:00
|
|
|
} else {
|
2020-06-09 16:57:05 +00:00
|
|
|
_location = session().local().readFileLocation(mediaKey());
|
2019-05-31 16:45:03 +00:00
|
|
|
if (_location.inMediaCache()) {
|
|
|
|
setLoadedInMediaCacheLocation();
|
|
|
|
} else if (_location.isEmpty() && loadedInMediaCache()) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(
|
2019-05-31 16:45:03 +00:00
|
|
|
mediaKey(),
|
|
|
|
FileLocation::InMediaCacheLocation());
|
|
|
|
}
|
2018-01-19 12:13:06 +00:00
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentData::setContentUrl(const QString &url) {
|
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2018-03-04 20:04:13 +00:00
|
|
|
void DocumentData::setWebLocation(const WebFileLocation &location) {
|
|
|
|
_urlLocation = location;
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
void DocumentData::collectLocalData(not_null<DocumentData*> local) {
|
|
|
|
if (local == this) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
_owner->cache().copyIfEmpty(local->cacheKey(), cacheKey());
|
2020-05-22 18:01:04 +00:00
|
|
|
if (const auto localMedia = local->activeMediaView()) {
|
2020-05-26 14:32:38 +00:00
|
|
|
auto media = createMediaView();
|
2020-05-22 18:01:04 +00:00
|
|
|
media->collectLocalData(localMedia.get());
|
2020-05-26 14:32:38 +00:00
|
|
|
_owner->keepAlive(std::move(media));
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-05-31 16:45:03 +00:00
|
|
|
if (!local->_location.inMediaCache() && !local->_location.isEmpty()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_location = local->_location;
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFileLocation(mediaKey(), _location);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 11:00:48 +00:00
|
|
|
QString DocumentData::ComposeNameString(
|
|
|
|
const QString &filename,
|
|
|
|
const QString &songTitle,
|
|
|
|
const QString &songPerformer) {
|
2017-09-26 11:49:16 +00:00
|
|
|
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("\
|
2019-12-08 14:54:21 +00:00
|
|
|
applescript action app bin command csh osx workflow terminal url caction \
|
|
|
|
mpkg pkg scpt scptd xhtm webarchive");
|
2020-06-13 01:03:23 +00:00
|
|
|
#elif defined Q_OS_UNIX // Q_OS_MAC
|
2019-12-08 14:54:21 +00:00
|
|
|
qsl("bin csh deb desktop ksh out pet pkg pup rpm run sh shar \
|
|
|
|
slp zsh");
|
2020-06-13 01:03:23 +00:00
|
|
|
#else // Q_OS_MAC || Q_OS_UNIX
|
2018-12-05 08:07:17 +00:00
|
|
|
qsl("\
|
2019-12-08 14:54:21 +00:00
|
|
|
ad ade adp app application appref-ms asp asx bas bat bin cdxml 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 lua mad maf mag mam manifest maq mar mas mat mau mav maw mcf mda mdb \
|
|
|
|
mde mdt mdw mdz mht mhtml mjs mmc mof msc msg msh msh1 msh2 msh1xml msh2xml \
|
|
|
|
mshxml msi msp mst ops osd paf pcd phar php php3 php4 php5 php7 phps php-s \
|
|
|
|
pht phtml pif pl plg pm pod prf prg ps1 ps2 ps1xml ps2xml psc1 psc2 psd1 \
|
|
|
|
psm1 pssc pst py py3 pyc pyd pyi pyo pyw pywz pyz rb reg rgs scf scr sct \
|
|
|
|
search-ms settingcontent-ms shb shs slk sys t 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 xs");
|
2020-06-13 01:03:23 +00:00
|
|
|
#endif // !Q_OS_MAC && !Q_OS_UNIX
|
2018-12-05 08:07:17 +00:00
|
|
|
const auto list = joined.split(' ');
|
|
|
|
return base::flat_set<QString>(list.begin(), list.end());
|
|
|
|
}();
|
|
|
|
|
|
|
|
return ranges::binary_search(
|
|
|
|
kExtensions,
|
|
|
|
FileExtension(filepath).toLower());
|
|
|
|
}
|
|
|
|
|
2019-01-17 08:18:23 +00:00
|
|
|
base::binary_guard ReadImageAsync(
|
2020-04-10 13:18:51 +00:00
|
|
|
not_null<Data::DocumentMedia*> media,
|
2019-02-04 15:53:00 +00:00
|
|
|
FnMut<QImage(QImage)> postprocess,
|
2019-01-17 08:18:23 +00:00
|
|
|
FnMut<void(QImage&&)> done) {
|
2019-03-27 12:11:38 +00:00
|
|
|
auto result = base::binary_guard();
|
2019-01-17 08:18:23 +00:00
|
|
|
crl::async([
|
2020-04-10 13:18:51 +00:00
|
|
|
bytes = media->bytes(),
|
|
|
|
path = media->owner()->filepath(),
|
2019-02-04 15:53:00 +00:00
|
|
|
postprocess = std::move(postprocess),
|
2019-03-27 12:11:38 +00:00
|
|
|
guard = result.make_guard(),
|
2019-01-17 08:18:23 +00:00
|
|
|
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);
|
2019-02-04 15:53:00 +00:00
|
|
|
if (postprocess) {
|
|
|
|
image = postprocess(std::move(image));
|
|
|
|
}
|
2019-02-17 11:52:57 +00:00
|
|
|
crl::on_main(std::move(guard), [
|
2019-01-17 08:18:23 +00:00
|
|
|
image = std::move(image),
|
|
|
|
callback = std::move(callback)
|
|
|
|
]() mutable {
|
|
|
|
callback(std::move(image));
|
|
|
|
});
|
|
|
|
});
|
2019-03-27 12:11:38 +00:00
|
|
|
return result;
|
2019-01-17 08:18:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 08:07:17 +00:00
|
|
|
} // namespace Data
|