2021-06-17 23:28:09 +00:00
|
|
|
/*
|
|
|
|
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_file_click_handler.h"
|
|
|
|
|
2022-01-13 17:31:35 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2021-06-17 23:28:09 +00:00
|
|
|
#include "core/file_utilities.h"
|
2022-02-21 15:40:20 +00:00
|
|
|
#include "core/application.h"
|
2021-06-17 23:28:09 +00:00
|
|
|
#include "data/data_document.h"
|
2022-02-21 15:40:20 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_download_manager.h"
|
2021-06-17 23:28:09 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
|
2021-06-18 06:39:59 +00:00
|
|
|
FileClickHandler::FileClickHandler(FullMsgId context)
|
|
|
|
: _context(context) {
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileClickHandler::setMessageId(FullMsgId context) {
|
|
|
|
_context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullMsgId FileClickHandler::context() const {
|
|
|
|
return _context;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<DocumentData*> DocumentClickHandler::document() const {
|
|
|
|
return _document;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentWrappedClickHandler::DocumentWrappedClickHandler(
|
|
|
|
ClickHandlerPtr wrapped,
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
FullMsgId context)
|
|
|
|
: DocumentClickHandler(document, context)
|
|
|
|
, _wrapped(wrapped) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentWrappedClickHandler::onClickImpl() const {
|
|
|
|
_wrapped->onClick({ Qt::LeftButton });
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentClickHandler::DocumentClickHandler(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
FullMsgId context)
|
2021-06-18 06:39:59 +00:00
|
|
|
: FileClickHandler(context)
|
2021-06-17 23:28:09 +00:00
|
|
|
, _document(document) {
|
2022-01-13 17:31:35 +00:00
|
|
|
setProperty(
|
2022-01-17 12:12:14 +00:00
|
|
|
kDocumentLinkMediaProperty,
|
|
|
|
reinterpret_cast<qulonglong>(_document.get()));
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DocumentOpenClickHandler::DocumentOpenClickHandler(
|
|
|
|
not_null<DocumentData*> document,
|
2021-06-18 04:22:47 +00:00
|
|
|
Fn<void(FullMsgId)> &&callback,
|
|
|
|
FullMsgId context)
|
|
|
|
: DocumentClickHandler(document, context)
|
2021-06-17 23:28:09 +00:00
|
|
|
, _handler(std::move(callback)) {
|
2021-06-18 04:22:47 +00:00
|
|
|
Expects(_handler != nullptr);
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentOpenClickHandler::onClickImpl() const {
|
2021-06-18 04:22:47 +00:00
|
|
|
_handler(context());
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentSaveClickHandler::Save(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
not_null<DocumentData*> data,
|
|
|
|
Mode mode) {
|
2021-11-18 07:40:15 +00:00
|
|
|
if (data->isNull()) {
|
2021-06-17 23:28:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto savename = QString();
|
|
|
|
if (mode != Mode::ToCacheOrFile || !data->saveToCache()) {
|
|
|
|
if (mode != Mode::ToNewFile && data->saveFromData()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto filepath = data->filepath(true);
|
|
|
|
const auto fileinfo = QFileInfo(
|
|
|
|
);
|
|
|
|
const auto filedir = filepath.isEmpty()
|
|
|
|
? QDir()
|
|
|
|
: fileinfo.dir();
|
|
|
|
const auto filename = filepath.isEmpty()
|
|
|
|
? QString()
|
|
|
|
: fileinfo.fileName();
|
|
|
|
savename = DocumentFileNameForSave(
|
|
|
|
data,
|
|
|
|
(mode == Mode::ToNewFile),
|
|
|
|
filename,
|
|
|
|
filedir);
|
|
|
|
if (savename.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data->save(origin, savename);
|
|
|
|
}
|
|
|
|
|
2022-02-27 13:32:36 +00:00
|
|
|
void DocumentSaveClickHandler::SaveAndTrack(
|
|
|
|
FullMsgId itemId,
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Mode mode) {
|
|
|
|
Save(itemId ? itemId : Data::FileOrigin(), document, mode);
|
|
|
|
if (document->loading() && !document->loadingFilePath().isEmpty()) {
|
2022-02-21 15:40:20 +00:00
|
|
|
if (const auto item = document->owner().message(itemId)) {
|
|
|
|
Core::App().downloadManager().addLoading({
|
|
|
|
.item = item,
|
|
|
|
.document = document,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 13:32:36 +00:00
|
|
|
void DocumentSaveClickHandler::onClickImpl() const {
|
|
|
|
SaveAndTrack(context(), document());
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:20:49 +00:00
|
|
|
DocumentCancelClickHandler::DocumentCancelClickHandler(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Fn<void(FullMsgId)> &&callback,
|
|
|
|
FullMsgId context)
|
|
|
|
: DocumentClickHandler(document, context)
|
|
|
|
, _handler(std::move(callback)) {
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:28:09 +00:00
|
|
|
void DocumentCancelClickHandler::onClickImpl() const {
|
|
|
|
const auto data = document();
|
2021-11-18 07:40:15 +00:00
|
|
|
if (data->isNull()) {
|
2021-06-17 23:28:09 +00:00
|
|
|
return;
|
2021-06-18 06:20:49 +00:00
|
|
|
} else if (data->uploading() && _handler) {
|
|
|
|
_handler(context());
|
2021-06-17 23:28:09 +00:00
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentOpenWithClickHandler::Open(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
not_null<DocumentData*> data) {
|
2021-11-18 07:40:15 +00:00
|
|
|
if (data->isNull()) {
|
2021-06-17 23:28:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->saveFromDataSilent();
|
|
|
|
const auto path = data->filepath(true);
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
File::OpenWith(path, QCursor::pos());
|
|
|
|
} else {
|
|
|
|
DocumentSaveClickHandler::Save(
|
|
|
|
origin,
|
|
|
|
data,
|
|
|
|
DocumentSaveClickHandler::Mode::ToFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentOpenWithClickHandler::onClickImpl() const {
|
|
|
|
Open(context(), document());
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoClickHandler::PhotoClickHandler(
|
|
|
|
not_null<PhotoData*> photo,
|
|
|
|
FullMsgId context,
|
|
|
|
PeerData *peer)
|
2021-06-18 06:39:59 +00:00
|
|
|
: FileClickHandler(context)
|
2021-06-17 23:28:09 +00:00
|
|
|
, _photo(photo)
|
|
|
|
, _peer(peer) {
|
2022-01-17 12:12:14 +00:00
|
|
|
setProperty(
|
|
|
|
kPhotoLinkMediaProperty,
|
|
|
|
reinterpret_cast<qulonglong>(_photo.get()));
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
not_null<PhotoData*> PhotoClickHandler::photo() const {
|
|
|
|
return _photo;
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerData *PhotoClickHandler::peer() const {
|
|
|
|
return _peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoOpenClickHandler::PhotoOpenClickHandler(
|
|
|
|
not_null<PhotoData*> photo,
|
2021-06-18 04:22:47 +00:00
|
|
|
Fn<void(FullMsgId)> &&callback,
|
|
|
|
FullMsgId context)
|
|
|
|
: PhotoClickHandler(photo, context)
|
2021-06-17 23:28:09 +00:00
|
|
|
, _handler(std::move(callback)) {
|
2021-06-18 04:22:47 +00:00
|
|
|
Expects(_handler != nullptr);
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoOpenClickHandler::onClickImpl() const {
|
2021-06-18 04:22:47 +00:00
|
|
|
_handler(context());
|
2021-06-17 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoSaveClickHandler::onClickImpl() const {
|
|
|
|
const auto data = photo();
|
2021-11-18 07:40:15 +00:00
|
|
|
if (data->isNull()) {
|
2021-06-17 23:28:09 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
data->clearFailed(Data::PhotoSize::Large);
|
|
|
|
data->load(context());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:20:49 +00:00
|
|
|
PhotoCancelClickHandler::PhotoCancelClickHandler(
|
|
|
|
not_null<PhotoData*> photo,
|
|
|
|
Fn<void(FullMsgId)> &&callback,
|
|
|
|
FullMsgId context)
|
|
|
|
: PhotoClickHandler(photo, context)
|
|
|
|
, _handler(std::move(callback)) {
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:28:09 +00:00
|
|
|
void PhotoCancelClickHandler::onClickImpl() const {
|
|
|
|
const auto data = photo();
|
2021-11-18 07:40:15 +00:00
|
|
|
if (data->isNull()) {
|
2021-06-17 23:28:09 +00:00
|
|
|
return;
|
2021-06-18 06:20:49 +00:00
|
|
|
} else if (data->uploading() && _handler) {
|
|
|
|
_handler(context());
|
2021-06-17 23:28:09 +00:00
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|