From 966c7ffefff17b615fdcfc1567cae16ee75dd22f Mon Sep 17 00:00:00 2001 From: leha-bot Date: Thu, 4 Apr 2019 15:38:22 +0300 Subject: [PATCH] Extract PhotoData to new file Inspired by upstream commit https://github.com/telegramdesktop/tdesktop/commit/ffc20e44920465a4181320a050e22d4c599d0b12 Related to #174 --- Telegram/CMakeLists.txt | 1 + Telegram/SourceFiles/data/data_photo.cpp | 134 +++++++++++++++++++++++ Telegram/SourceFiles/data/data_photo.h | 111 +++++++++++++++++++ Telegram/SourceFiles/structs.cpp | 126 --------------------- Telegram/SourceFiles/structs.h | 84 +------------- 5 files changed, 247 insertions(+), 209 deletions(-) create mode 100644 Telegram/SourceFiles/data/data_photo.cpp create mode 100644 Telegram/SourceFiles/data/data_photo.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index b22c56968..e4fb1bf83 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -247,6 +247,7 @@ add_executable(Kepka WIN32 MACOSX_BUNDLE SourceFiles/data/data_abstract_structure.cpp SourceFiles/data/data_drafts.cpp + SourceFiles/data/data_photo.cpp SourceFiles/dialogs/dialogs_indexed_list.cpp SourceFiles/dialogs/dialogs_inner_widget.cpp diff --git a/Telegram/SourceFiles/data/data_photo.cpp b/Telegram/SourceFiles/data/data_photo.cpp new file mode 100644 index 000000000..0e47c4b2d --- /dev/null +++ b/Telegram/SourceFiles/data/data_photo.cpp @@ -0,0 +1,134 @@ +#include "data/data_photo.h" +#include "messenger.h" // Messenger:: +#include "app.h" // App:: +#include "facades.h" // Notify::historyItemLayoutChanged +#include "history/history_media.h" // HistoryMedia definition +#include "history/history_media_types.h" // HistoryPhoto +#include "mainwidget.h" // MainWidget + +PhotoData::PhotoData(const PhotoId &id, const quint64 &access, qint32 date, const ImagePtr &thumb, + const ImagePtr &medium, const ImagePtr &full) + : id(id) + , access(access) + , date(date) + , thumb(thumb) + , medium(medium) + , full(full) {} + +void PhotoData::automaticLoad(const HistoryItem *item) { + full->automaticLoad(item); +} + +void PhotoData::automaticLoadSettingsChanged() { + full->automaticLoadSettingsChanged(); +} + +void PhotoData::download() { + full->loadEvenCancelled(); + notifyLayoutChanged(); +} + +bool PhotoData::loaded() const { + bool wasLoading = loading(); + if (full->loaded()) { + if (wasLoading) { + notifyLayoutChanged(); + } + return true; + } + return false; +} + +bool PhotoData::loading() const { + return full->loading(); +} + +bool PhotoData::displayLoading() const { + return full->loading() ? full->displayLoading() : uploading(); +} + +void PhotoData::cancel() { + full->cancel(); + notifyLayoutChanged(); +} + +void PhotoData::notifyLayoutChanged() const { + auto &items = App::photoItems(); + auto i = items.constFind(const_cast(this)); + if (i != items.cend()) { + for_const (auto item, i.value()) { Notify::historyItemLayoutChanged(item); } + } +} + +double PhotoData::progress() const { + if (uploading()) { + if (uploadingData->size > 0) { + return double(uploadingData->offset) / uploadingData->size; + } + return 0; + } + return full->progress(); +} + +qint32 PhotoData::loadOffset() const { + return full->loadOffset(); +} + +bool PhotoData::uploading() const { + return !!uploadingData; +} + +void PhotoData::forget() { + thumb->forget(); + replyPreview->forget(); + medium->forget(); + full->forget(); +} + +ImagePtr PhotoData::makeReplyPreview() { + if (replyPreview->isNull() && !thumb->isNull()) { + if (thumb->loaded()) { + int w = thumb->width(), h = thumb->height(); + if (w <= 0) w = 1; + if (h <= 0) h = 1; + replyPreview = + ImagePtr(w > h ? thumb->pix(w * st::msgReplyBarSize.height() / h, st::msgReplyBarSize.height()) : + thumb->pix(st::msgReplyBarSize.height()), + "PNG"); + } else { + thumb->load(); + } + } + return replyPreview; +} + +void PhotoOpenClickHandler::onClickImpl() const { + Messenger::Instance().showPhoto(this, App::hoveredLinkItem() ? App::hoveredLinkItem() : App::contextItem()); +} + +void PhotoSaveClickHandler::onClickImpl() const { + auto data = photo(); + if (!data->date) return; + + data->download(); +} + +void PhotoCancelClickHandler::onClickImpl() const { + auto data = photo(); + if (!data->date) return; + + if (data->uploading()) { + if (auto item = + App::hoveredLinkItem() ? App::hoveredLinkItem() : (App::contextItem() ? App::contextItem() : nullptr)) { + if (auto media = item->getMedia()) { + if (media->type() == MediaTypePhoto && static_cast(media)->photo() == data) { + App::contextItem(item); + App::main()->cancelUploadLayer(); + } + } + } + } else { + data->cancel(); + } +} + diff --git a/Telegram/SourceFiles/data/data_photo.h b/Telegram/SourceFiles/data/data_photo.h new file mode 100644 index 000000000..c445f8206 --- /dev/null +++ b/Telegram/SourceFiles/data/data_photo.h @@ -0,0 +1,111 @@ +// +// This file is part of Kepka, +// an unofficial desktop version of Telegram messaging app, +// see https://github.com/procxx/kepka +// +// Kepka is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// It is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// In addition, as a special exception, the copyright holders give permission +// to link the code of portions of this program with the OpenSSL library. +// +// Full license: https://github.com/procxx/kepka/blob/master/LICENSE +// Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org +// Copyright (c) 2017- Kepka Contributors, https://github.com/procxx +// +#pragma once +#include // qint64, quint64 +#include "core/click_handler.h" // LeftButtonClickHandler +#include "ui/images.h" // ImagePtr + +using PhotoId = quint64; + +class PhotoData { +public: + PhotoData(const PhotoId &id, const quint64 &access = 0, qint32 date = 0, const ImagePtr &thumb = ImagePtr(), + const ImagePtr &medium = ImagePtr(), const ImagePtr &full = ImagePtr()); + + void automaticLoad(const HistoryItem *item); + void automaticLoadSettingsChanged(); + + void download(); + bool loaded() const; + bool loading() const; + bool displayLoading() const; + void cancel(); + double progress() const; + qint32 loadOffset() const; + bool uploading() const; + + void forget(); + ImagePtr makeReplyPreview(); + + PhotoId id; + quint64 access; + qint32 date; + ImagePtr thumb, replyPreview; + ImagePtr medium; + ImagePtr full; + + PeerData *peer = nullptr; // for chat and channel photos connection + // geo, caption + + struct UploadingData { + UploadingData(int size) + : size(size) {} + int offset = 0; + int size = 0; + }; + std::unique_ptr uploadingData; + +private: + void notifyLayoutChanged() const; +}; + +class PhotoClickHandler : public LeftButtonClickHandler { +public: + PhotoClickHandler(not_null photo, PeerData *peer = nullptr) + : _photo(photo) + , _peer(peer) {} + not_null photo() const { + return _photo; + } + PeerData *peer() const { + return _peer; + } + +private: + not_null _photo; + PeerData *_peer; +}; + +class PhotoOpenClickHandler : public PhotoClickHandler { +public: + using PhotoClickHandler::PhotoClickHandler; + +protected: + void onClickImpl() const override; +}; + +class PhotoSaveClickHandler : public PhotoClickHandler { +public: + using PhotoClickHandler::PhotoClickHandler; + +protected: + void onClickImpl() const override; +}; + +class PhotoCancelClickHandler : public PhotoClickHandler { +public: + using PhotoClickHandler::PhotoClickHandler; + +protected: + void onClickImpl() const override; +}; diff --git a/Telegram/SourceFiles/structs.cpp b/Telegram/SourceFiles/structs.cpp index 0c58896d8..82d784a04 100644 --- a/Telegram/SourceFiles/structs.cpp +++ b/Telegram/SourceFiles/structs.cpp @@ -1156,132 +1156,6 @@ bool PtsWaiter::check(ChannelData *channel, qint32 pts, return !count; } -PhotoData::PhotoData(const PhotoId &id, const quint64 &access, qint32 date, const ImagePtr &thumb, - const ImagePtr &medium, const ImagePtr &full) - : id(id) - , access(access) - , date(date) - , thumb(thumb) - , medium(medium) - , full(full) {} - -void PhotoData::automaticLoad(const HistoryItem *item) { - full->automaticLoad(item); -} - -void PhotoData::automaticLoadSettingsChanged() { - full->automaticLoadSettingsChanged(); -} - -void PhotoData::download() { - full->loadEvenCancelled(); - notifyLayoutChanged(); -} - -bool PhotoData::loaded() const { - bool wasLoading = loading(); - if (full->loaded()) { - if (wasLoading) { - notifyLayoutChanged(); - } - return true; - } - return false; -} - -bool PhotoData::loading() const { - return full->loading(); -} - -bool PhotoData::displayLoading() const { - return full->loading() ? full->displayLoading() : uploading(); -} - -void PhotoData::cancel() { - full->cancel(); - notifyLayoutChanged(); -} - -void PhotoData::notifyLayoutChanged() const { - auto &items = App::photoItems(); - auto i = items.constFind(const_cast(this)); - if (i != items.cend()) { - for_const (auto item, i.value()) { Notify::historyItemLayoutChanged(item); } - } -} - -double PhotoData::progress() const { - if (uploading()) { - if (uploadingData->size > 0) { - return double(uploadingData->offset) / uploadingData->size; - } - return 0; - } - return full->progress(); -} - -qint32 PhotoData::loadOffset() const { - return full->loadOffset(); -} - -bool PhotoData::uploading() const { - return !!uploadingData; -} - -void PhotoData::forget() { - thumb->forget(); - replyPreview->forget(); - medium->forget(); - full->forget(); -} - -ImagePtr PhotoData::makeReplyPreview() { - if (replyPreview->isNull() && !thumb->isNull()) { - if (thumb->loaded()) { - int w = thumb->width(), h = thumb->height(); - if (w <= 0) w = 1; - if (h <= 0) h = 1; - replyPreview = - ImagePtr(w > h ? thumb->pix(w * st::msgReplyBarSize.height() / h, st::msgReplyBarSize.height()) : - thumb->pix(st::msgReplyBarSize.height()), - "PNG"); - } else { - thumb->load(); - } - } - return replyPreview; -} - -void PhotoOpenClickHandler::onClickImpl() const { - Messenger::Instance().showPhoto(this, App::hoveredLinkItem() ? App::hoveredLinkItem() : App::contextItem()); -} - -void PhotoSaveClickHandler::onClickImpl() const { - auto data = photo(); - if (!data->date) return; - - data->download(); -} - -void PhotoCancelClickHandler::onClickImpl() const { - auto data = photo(); - if (!data->date) return; - - if (data->uploading()) { - if (auto item = - App::hoveredLinkItem() ? App::hoveredLinkItem() : (App::contextItem() ? App::contextItem() : nullptr)) { - if (auto media = item->getMedia()) { - if (media->type() == MediaTypePhoto && static_cast(media)->photo() == data) { - App::contextItem(item); - App::main()->cancelUploadLayer(); - } - } - } - } else { - data->cancel(); - } -} - QString joinList(const QStringList &list, const QString &sep) { QString result; if (list.isEmpty()) return result; diff --git a/Telegram/SourceFiles/structs.h b/Telegram/SourceFiles/structs.h index 38b51fc3f..8143db697 100644 --- a/Telegram/SourceFiles/structs.h +++ b/Telegram/SourceFiles/structs.h @@ -31,6 +31,7 @@ #include "ui/images.h" #include "ui/text/text.h" #include "ui/twidget.h" +#include "data/data_photo.h" using MediaKey = QPair; @@ -177,7 +178,6 @@ inline TimeId dateFromMessage(const MTPmessage &msg) { return 0; } -using PhotoId = quint64; using VideoId = quint64; using AudioId = quint64; using DocumentId = quint64; @@ -1117,88 +1117,6 @@ inline bool PeerData::canWrite() const { enum ActionOnLoad { ActionOnLoadNone, ActionOnLoadOpen, ActionOnLoadOpenWith, ActionOnLoadPlayInline }; typedef QMap PreparedPhotoThumbs; -class PhotoData { -public: - PhotoData(const PhotoId &id, const quint64 &access = 0, qint32 date = 0, const ImagePtr &thumb = ImagePtr(), - const ImagePtr &medium = ImagePtr(), const ImagePtr &full = ImagePtr()); - - void automaticLoad(const HistoryItem *item); - void automaticLoadSettingsChanged(); - - void download(); - bool loaded() const; - bool loading() const; - bool displayLoading() const; - void cancel(); - double progress() const; - qint32 loadOffset() const; - bool uploading() const; - - void forget(); - ImagePtr makeReplyPreview(); - - PhotoId id; - quint64 access; - qint32 date; - ImagePtr thumb, replyPreview; - ImagePtr medium; - ImagePtr full; - - PeerData *peer = nullptr; // for chat and channel photos connection - // geo, caption - - struct UploadingData { - UploadingData(int size) - : size(size) {} - int offset = 0; - int size = 0; - }; - std::unique_ptr uploadingData; - -private: - void notifyLayoutChanged() const; -}; - -class PhotoClickHandler : public LeftButtonClickHandler { -public: - PhotoClickHandler(not_null photo, PeerData *peer = nullptr) - : _photo(photo) - , _peer(peer) {} - not_null photo() const { - return _photo; - } - PeerData *peer() const { - return _peer; - } - -private: - not_null _photo; - PeerData *_peer; -}; - -class PhotoOpenClickHandler : public PhotoClickHandler { -public: - using PhotoClickHandler::PhotoClickHandler; - -protected: - void onClickImpl() const override; -}; - -class PhotoSaveClickHandler : public PhotoClickHandler { -public: - using PhotoClickHandler::PhotoClickHandler; - -protected: - void onClickImpl() const override; -}; - -class PhotoCancelClickHandler : public PhotoClickHandler { -public: - using PhotoClickHandler::PhotoClickHandler; - -protected: - void onClickImpl() const override; -}; enum FileStatus { FileDownloadFailed = -2,