4
1
mirror of https://github.com/QuasarApp/qTbot.git synced 2025-05-06 10:19:37 +00:00

remove deprecated code

This commit is contained in:
Andrei Yankovich 2024-12-11 00:03:44 +01:00
parent 06eff18b91
commit 4479664879
5 changed files with 53 additions and 134 deletions

@ -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<iFile> &file) {
if (!file->isFinished()) {
auto address = reinterpret_cast<size_t>(file.get());
_files[address] = file;
connect(file.get(), &qTbot::iFile::finishedChanged, this, &FileWaiter::handleFileFinished,
Qt::QueuedConnection);
}
}
void FileWaiter::handleFileFinished() {
auto address = reinterpret_cast<size_t>(sender());
_files.remove(address);
}
}

@ -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<iFile>& file);
private slots:
void handleFileFinished();
private:
QHash<size_t, QSharedPointer<iFile>> _files;
};
}
#endif // FILEWAITER_H

@ -13,12 +13,10 @@
#include "qTbot/iupdate.h"
#include "qTbot/irequest.h"
#include "ifile.h"
#include "qfileinfo.h"
#include <QMap>
#include <QHash>
#include <QSet>
#include <QFileInfo>
#include <QNetworkReply>
#include <QObject>
@ -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<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) = 0;
virtual QFuture<QByteArray> getFile(const QString& fileId, FileType fileType = Ram) = 0;
/**
* @brief send @a file .

@ -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 <QNetworkAccessManager>
#include <requests/telegramgetfile.h>
@ -283,7 +281,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args,
return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard));
}
QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, FileType fileType) {
if (fileId.isEmpty()) {
@ -293,24 +291,26 @@ QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, iFile::Type fil
auto localFilePath = findFileInlocatStorage(fileId);
if (!localFilePath.isEmpty()) {
QPromise<QByteArray> fileDataResult;
if (fileType == iFile::Ram) {
if (fileType == FileType::Ram) {
QFile localFile(localFilePath);
if (localFile.open(QIODevice::ReadOnly)) {
auto&& virtualFile = QSharedPointer<VirtualFile>::create(nullptr);
virtualFile->setArray(localFile.readAll());
QPromise<QByteArray> fileDataResult;
fileDataResult.addResult(localFile.readAll());
localFile.close();
result = virtualFile;
}
} else if (fileType == iFile::Local) {
result = QSharedPointer<File>::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<QByteArray> ITelegramBot::getFile(const QString &fileId, iFile::Type fil
if (localFilePath.isEmpty())
return result;
return {};
QFuture<QByteArray> &&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<VirtualFile>::create(replay);
} else if (fileType == iFile::Local) {
result = QSharedPointer<File>::create(replay, localFilePath);
}
QFuture<QByteArray> replay;
if (fileType == FileType::Ram) {
replay = sendRequest(msg);
} else {
replay = sendRequest(msg, localFilePath);
}
return result;
return replay;
}
}
if (fileType == iFile::Ram) {
result = QSharedPointer<VirtualFile>::create();
} else if (fileType == iFile::Local) {
result = QSharedPointer<File>::create(localFilePath);
}
auto&& future = getFileMeta(fileId, result.toWeakRef());
auto longWay = QSharedPointer<QPromise<QByteArray>>::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<QByteArray> ITelegramBot::getFileMeta(const QString &fileId, const QWeakPointer<iFile>& receiver) {
QFuture<QByteArray> ITelegramBot::getFileMeta(const QString &fileId) {
auto msg = QSharedPointer<TelegramGetFile>::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<iFile>& receiver) {
void ITelegramBot::handleFileHeader(const QByteArray& header) {
auto&& ansver = makeMesasge<TelegramUpdateAnswer>(header);
if (!ansver->isValid()) {
@ -592,11 +586,6 @@ void ITelegramBot::handleFileHeader(const QByteArray& header,
auto &&fileMetaInfo = makeMesasge<TelegramFile>(ansver->result().toObject());
_filesMetaInfo.insert(fileMetaInfo->fileId(), fileMetaInfo);
if (auto&& sharedPtr = receiver.lock()) {
auto&& downloadRequest = QSharedPointer<TelegrammDownloadFile>::create(fileMetaInfo->takePath());
sharedPtr->setDownloadRequest(sendRequest(downloadRequest));
}
}
QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const {

@ -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<QByteArray> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override;
QFuture<QByteArray> 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<QByteArray> getFileMeta(const QString& fileId,
const QWeakPointer<iFile> &receiver = {nullptr});
QFuture<QByteArray> 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<iFile> &receiver);
void handleFileHeader(const QByteArray &header);
private: