diff --git a/src/qTbot/src/public/qTbot/filewaiter.cpp b/src/qTbot/src/public/qTbot/filewaiter.cpp deleted file mode 100644 index 72162f3..0000000 --- a/src/qTbot/src/public/qTbot/filewaiter.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//# -//# Copyright (C) 2023-2024 QuasarApp. -//# Distributed under the GPLv3 software license, see the accompanying -//# Everyone is permitted to copy and distribute verbatim copies -//# of this license document, but changing it is not allowed. -//# -#include "filewaiter.h" -namespace qTbot { - -FileWaiter::FileWaiter() -{ - -} - -void FileWaiter::wait(const QSharedPointer &file) { - if (!file->isFinished()) { - auto address = reinterpret_cast(file.get()); - - _files[address] = file; - - - connect(file.get(), &qTbot::iFile::finishedChanged, this, &FileWaiter::handleFileFinished, - Qt::QueuedConnection); - } -} - -void FileWaiter::handleFileFinished() { - auto address = reinterpret_cast(sender()); - - _files.remove(address); - -} - -} diff --git a/src/qTbot/src/public/qTbot/filewaiter.h b/src/qTbot/src/public/qTbot/filewaiter.h deleted file mode 100644 index 4b0b9c3..0000000 --- a/src/qTbot/src/public/qTbot/filewaiter.h +++ /dev/null @@ -1,43 +0,0 @@ -//# -//# Copyright (C) 2023-2024 QuasarApp. -//# Distributed under the GPLv3 software license, see the accompanying -//# Everyone is permitted to copy and distribute verbatim copies -//# of this license document, but changing it is not allowed. -//# - - -#ifndef FILEWAITER_H -#define FILEWAITER_H - -#include "ifile.h" - -namespace qTbot { - -/** - * @brief The FileWaiter class. This is a simple storage for the shared pointer of files. - * All added files will be removed (shared object) after finish donwload or upload. - */ -class QTBOT_EXPORT FileWaiter: public QObject -{ - Q_OBJECT -public: - FileWaiter(); - - /** - * @brief wait This method add shared pointer of file in to local storage, and remove it from this when @a file change state to finish. - * @param file This is a processed file. - * @note The file will not added if the file alredey finished. - * @note This method not stop thread, it is just save a file until it is is progres - */ - void wait(const QSharedPointer& file); - -private slots: - void handleFileFinished(); - -private: - QHash> _files; - - -}; -} -#endif // FILEWAITER_H diff --git a/src/qTbot/src/public/qTbot/ibot.h b/src/qTbot/src/public/qTbot/ibot.h index e82a12c..835d1f5 100644 --- a/src/qTbot/src/public/qTbot/ibot.h +++ b/src/qTbot/src/public/qTbot/ibot.h @@ -13,12 +13,10 @@ #include "qTbot/iupdate.h" #include "qTbot/irequest.h" -#include "ifile.h" -#include "qfileinfo.h" - #include #include #include +#include #include #include @@ -39,6 +37,19 @@ public: IBot(); ~IBot(); + /** + * @brief The FileType enum is is file types, deffine how we should download a file - as a local object in file system or into virtual memory. + */ + enum FileType { + /// The Ram is a Virtual type of download files will save all file data into QFuture bytes array. + Ram, + + /// The Local file will saved in internal file storage. + /// This file type can use the filse system as cache. + /// and will doenload file with same id only one time. + Local + }; + /** * @brief login This method get bae information of the bot from remote server. * @param token This is token value for login @@ -75,10 +86,10 @@ public: * This function allows you to retrieve a file by its ID. * * @param fileId The ID of the file to retrieve. - * @param fileType This is a saving way, by Default will be used a iFile::Type::Ram + * @param fileType This is a saving way, by Default will be used a FileType::Ram * @return Returns true if the file retrieval operation was successfully initiated and false in case of an error. */ - virtual QSharedPointer getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) = 0; + virtual QFuture getFile(const QString& fileId, FileType fileType = Ram) = 0; /** * @brief send @a file . diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 95db260..1be1214 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -7,13 +7,11 @@ #include "itelegrambot.h" #include "qTbot/messages/telegramupdateanswer.h" -#include "file.h" #include "requests/telegrammdownloadfile.h" #include "qdir.h" #include "requests/telegramsendcontact.h" #include "requests/telegramsenddocument.h" #include "httpexception.h" -#include "virtualfile.h" #include #include @@ -283,7 +281,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args, return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard)); } -QFuture ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) { +QFuture ITelegramBot::getFile(const QString &fileId, FileType fileType) { if (fileId.isEmpty()) { @@ -293,24 +291,26 @@ QFuture ITelegramBot::getFile(const QString &fileId, iFile::Type fil auto localFilePath = findFileInlocatStorage(fileId); if (!localFilePath.isEmpty()) { + QPromise fileDataResult; - if (fileType == iFile::Ram) { + if (fileType == FileType::Ram) { QFile localFile(localFilePath); if (localFile.open(QIODevice::ReadOnly)) { - auto&& virtualFile = QSharedPointer::create(nullptr); - virtualFile->setArray(localFile.readAll()); + QPromise fileDataResult; + fileDataResult.addResult(localFile.readAll()); localFile.close(); - - result = virtualFile; } - } else if (fileType == iFile::Local) { - result = QSharedPointer::create(nullptr, localFilePath); + } else if (fileType == FileType::Local) { + fileDataResult.addResult(localFilePath.toUtf8()); } - result->setDownloadProgress(1); - result->setFinished(true); - return result; + fileDataResult.setProgressRange(0,1); + fileDataResult.setProgressValue(1); + + fileDataResult.finish(); + + return fileDataResult.future(); } auto&& metaInfo = getFileInfoByUniqueId(fileId); @@ -325,45 +325,40 @@ QFuture ITelegramBot::getFile(const QString &fileId, iFile::Type fil if (localFilePath.isEmpty()) - return result; + return {}; - QFuture &&replay = sendRequest(msg); - if (replay.isValid()) { - // here i must be receive responce and prepare new request to file from the call back function. - if (fileType == iFile::Ram) { - result = QSharedPointer::create(replay); - } else if (fileType == iFile::Local) { - result = QSharedPointer::create(replay, localFilePath); - } + QFuture replay; + if (fileType == FileType::Ram) { + replay = sendRequest(msg); + } else { + replay = sendRequest(msg, localFilePath); } - return result; + return replay; } } - - if (fileType == iFile::Ram) { - result = QSharedPointer::create(); - } else if (fileType == iFile::Local) { - result = QSharedPointer::create(localFilePath); - } - - auto&& future = getFileMeta(fileId, result.toWeakRef()); + auto longWay = QSharedPointer>::create(); + auto&& future = getFileMeta(fileId); if (!future.isValid()) { - return nullptr; + return {}; } - return result; + future.then([this, fileId, fileType, longWay](const QByteArray& header){ + handleFileHeader(header); + + getFile(fileId, fileType).then([longWay](const QByteArray& data){ + longWay->addResult(data); + }); + }); + + return longWay->future(); } -QFuture ITelegramBot::getFileMeta(const QString &fileId, const QWeakPointer& receiver) { +QFuture ITelegramBot::getFileMeta(const QString &fileId) { auto msg = QSharedPointer::create(fileId); auto && future = sendRequest(msg); if (future.isValid()) { - future.then([this, receiver](const QByteArray&data){ - handleFileHeader(data, receiver); - }); - return future; } @@ -580,8 +575,7 @@ void ITelegramBot::handleLoginErr(QNetworkReply::NetworkError err) { } } -void ITelegramBot::handleFileHeader(const QByteArray& header, - const QWeakPointer& receiver) { +void ITelegramBot::handleFileHeader(const QByteArray& header) { auto&& ansver = makeMesasge(header); if (!ansver->isValid()) { @@ -592,11 +586,6 @@ void ITelegramBot::handleFileHeader(const QByteArray& header, auto &&fileMetaInfo = makeMesasge(ansver->result().toObject()); _filesMetaInfo.insert(fileMetaInfo->fileId(), fileMetaInfo); - - if (auto&& sharedPtr = receiver.lock()) { - auto&& downloadRequest = QSharedPointer::create(fileMetaInfo->takePath()); - sharedPtr->setDownloadRequest(sendRequest(downloadRequest)); - } } QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const { diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 300ec38..873f9b1 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -193,17 +193,14 @@ public: * @param fileType this is type of file. Depends of this argument future will be contains deffrent result if it is Local type then future will contains link to local file path else file source as bytes. * @return futur with file source or path to file depends of type. */ - QFuture getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override; + QFuture getFile(const QString& fileId, FileType fileType = FileType::Ram) override; /** * @brief getFileMeta This method receive meta information of the file. * @param fileId This is id of the file. - * @param receiver this is wrapper of the file. Set to nullptr if you no need to wait a physical file. * @return future objectl with result. */ - - QFuture getFileMeta(const QString& fileId, - const QWeakPointer &receiver = {nullptr}); + QFuture getFileMeta(const QString& fileId); bool sendFile( const QFileInfo& file, const QVariant& chatId) override; @@ -397,8 +394,7 @@ protected: private slots: void handleLogin(const QByteArray &ansver); void handleLoginErr(QNetworkReply::NetworkError err); - void handleFileHeader(const QByteArray &header, - const QWeakPointer &receiver); + void handleFileHeader(const QByteArray &header); private: