continue to refactor of qTbor library

This commit is contained in:
Andrei Yankovich 2024-12-10 22:43:29 +01:00
parent 40d4a47a00
commit 06eff18b91
8 changed files with 9 additions and 501 deletions

View File

@ -1,56 +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 "file.h"
namespace qTbot {
File::File(const QSharedPointer<QNetworkReply> &replay, const QString &filePath): iFile(replay) {
_localFile.setFileName(filePath);
if (!_localFile.isOpen()) {
_localFile.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Append);
}
}
File::File(const QString &filePath):File(nullptr, filePath) {
}
const QFile & File::localFile() const {
return _localFile;
}
iFile::Type File::type() const {
return Type::Local;
}
void File::handleReadReady() {
auto&& bytes = replay()->readAll();
if (bytes.size()) {
_localFile.write(bytes);
_localFile.flush();
}
}
void File::handleFinished() {
handleReadReady();
_localFile.close();
iFile::handleFinished();
}
void File::handleError(QNetworkReply::NetworkError error) {
iFile::handleError(error);
_localFile.close();
_localFile.remove();
}
}

View File

@ -1,40 +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 FILE_H
#define FILE_H
#include "ifile.h"
namespace qTbot {
/**
* @brief The File class is implementations for local files.
*/
class QTBOT_EXPORT File: public iFile
{
Q_OBJECT
public:
File(const QSharedPointer<QNetworkReply>& replay, const QString &filePath);
File(const QString &filePath);
const QFile & localFile() const;
Type type() const override;
// iFile interface
protected slots:
void handleReadReady() override;
void handleFinished() override;
void handleError(QNetworkReply::NetworkError error) override;
private:
QFile _localFile;
};
}
#endif // FILE_H

View File

@ -1,140 +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 "ifile.h"
#include <QFutureWatcher>
namespace qTbot {
iFile::iFile(const QFuture<QByteArray> &replay) {
setDownloadRequest(replay);
}
float iFile::uploadProgress() const {
return _uploadProgress;
}
void iFile::setUploadProgress(float newUploadProgress) {
if (qFuzzyCompare(_uploadProgress, newUploadProgress))
return;
_uploadProgress = newUploadProgress;
emit uploadProgressChanged();
}
float iFile::downloadProgress() const {
return _downloadProgress;
}
void iFile::setDownloadProgress(float newDownloadProgress) {
if (qFuzzyCompare(_downloadProgress, newDownloadProgress))
return;
_downloadProgress = newDownloadProgress;
emit downloadProgressChanged();
}
int iFile::error() const {
return _error;
}
void iFile::setError(int newError) {
if (_error == newError)
return;
_error = newError;
emit errorChanged();
}
const QFuture<QByteArray> &iFile::replay() const {
return _replay;
}
bool iFile::isFinished() const {
return _finished;
}
void iFile::handleError(QNetworkReply::NetworkError error) {
setError(error);
setUploadProgress(0);
setDownloadProgress(0);
}
void iFile::handleUploadProgressChanged(qint64 bytesSent, qint64 bytesTotal) {
setUploadProgress(bytesSent / static_cast<float>(bytesTotal));
}
void iFile::handleDownloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal) {
setDownloadProgress(bytesReceived / static_cast<float>(bytesTotal));
}
void iFile::setDownloadRequest(const QFuture<QByteArray> &replay) {
if (_replay.isValid()) {
if (_replay.isRunning()) {
_replay.cancel();
}
// disconnect(_replay.get(), &QNetworkReply::finished,
// this, &iFile::handleFinished);
// disconnect(_replay.get(), &QNetworkReply::errorOccurred,
// this, &iFile::handleError);
// disconnect(_replay.get(), &QNetworkReply::readyRead,
// this, &iFile::handleReadReady);
// disconnect(_replay.get(), &QNetworkReply::uploadProgress,
// this, &iFile::handleUploadProgressChanged);
// disconnect(_replay.get(), &QNetworkReply::downloadProgress,
// this, &iFile::handleDownloadProgressChanged);
}
_replay = replay;
if (_replay.isValid()) {
_replay.then(this, [this](const QByteArray&) {
handleFinished();
});
QFutureWatcher<QByteArray> *watcher = new QFutureWatcher<QByteArray>();
watcher->setFuture(replay);
// connect(replay.get(), &QNetworkReply::finished,
// this, &iFile::handleFinished, Qt::DirectConnection);
// connect(replay.get(), &QNetworkReply::errorOccurred,
// this, &iFile::handleError, Qt::DirectConnection);
// connect(replay.get(), &QNetworkReply::readyRead,
// this, &iFile::handleReadReady, Qt::DirectConnection);
// connect(replay.get(), &QNetworkReply::uploadProgress,
// this, &iFile::handleUploadProgressChanged, Qt::DirectConnection);
// connect(replay.get(), &QNetworkReply::downloadProgress,
// this, &iFile::handleDownloadProgressChanged, Qt::DirectConnection);
}
}
void iFile::setFinished(bool newFinished) {
if (newFinished != _finished) {
_finished = newFinished;
emit finishedChanged();
}
}
void iFile::handleFinished() {
setFinished(true);
}
}

View File

@ -1,175 +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 IFILE_H
#define IFILE_H
#include <qTbot/global.h>
#include <QFile>
#include <QFuture>
#include <QNetworkReply>
namespace qTbot {
/**
* @brief The iFile class This is main interface for all implementations of the files.
*/
class QTBOT_EXPORT iFile: public QObject
{
Q_OBJECT
public:
/**
* @brief The Type enum is type of the file object.
*/
enum Type {
/// This is local file, all receive bytes will be save directed into file.
Local,
/// This is memory saved file. All received bytes will be saved into QByteArray object.
Ram
};
iFile(const QFuture<QByteArray>& replay);
/**
* @brief Get the current upload progress.
* @return The current upload progress as a floating-point value.
*/
float uploadProgress() const;
/**
* @brief Set the upload progress.
* @param newUploadProgress The new upload progress value to set.
*/
void setUploadProgress(float newUploadProgress);
/**
* @brief Get the current download progress.
* @return The current download progress as a floating-point value.
*/
float downloadProgress() const;
/**
* @brief Set the download progress.
* @param newDownloadProgress The new download progress value to set.
*/
void setDownloadProgress(float newDownloadProgress);
/**
* @brief Get the error code associated with this file.
* @return The error code as an integer value.
*/
int error() const;
/**
* @brief Set the error code for this file.
* @param newError The new error code to set.
*/
void setError(int newError);
/**
* @brief Get the shared pointer to the associated QNetworkReply.
* @return A shared pointer to the associated QNetworkReply.
*/
const QFuture<QByteArray>& replay() const;
/**
* @brief type This is type of the file object.
* @return type of the file object.
*/
virtual Type type() const = 0;
/**
* @brief finished return true if the request was finished else false.
* @return true if the request was finished else false
*/
bool isFinished() const;
/**
* @brief setDownloadRequest This method sets replay for the file.
* @param replay This is pointer to the replay.
*/
void setDownloadRequest(const QFuture<QByteArray> &replay);
protected:
/**
* @brief setFinished monual sets finished flag.
* @param newFinished new value for the finished flag.
*/
void setFinished(bool newFinished);
protected slots:
/**
* @brief Slot to handle when data is ready to be read.
*/
virtual void handleReadReady() = 0;
/**
* @brief Slot to handle when the network operation is finished.
*/
virtual void handleFinished();
/**
* @brief Slot to handle errors in the network operation.
* @param error This is error code.
*/
virtual void handleError(QNetworkReply::NetworkError error);
private slots:
/**
* @brief Slot to handle changes in upload progress.
* @param bytesSent current snet bytes.
* @param bytesTotal total to sent bytes.
*/
void handleUploadProgressChanged(qint64 bytesSent, qint64 bytesTotal);
/**
* @brief Slot to handle changes in download progress.
* @param bytesReceived current received bytes.
* @param bytesTotal total to receive bytes.
*/
void handleDownloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal);
signals:
/**
* @brief Signal emitted when the upload progress changes.
*/
void uploadProgressChanged();
/**
* @brief Signal emitted when the download progress changes.
*/
void downloadProgressChanged();
/**
* @brief Signal emitted when the error code changes.
*/
void errorChanged();
/**
* @brief Signal emitted when the associated QNetworkReply changes.
*/
void replayChanged();
/**
* @brief Signal emitted when the associated finished changes.
*/
void finishedChanged();
private:
float _uploadProgress = 0;
float _downloadProgress = 0;
int _error = 0;
bool _finished = false;
QFuture<QByteArray> _replay;
friend class ITelegramBot;
};
}
#endif // IFILE_H

View File

@ -283,12 +283,11 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args,
return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard));
}
QSharedPointer<iFile> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
QSharedPointer<iFile> result = nullptr;
if (fileId.isEmpty()) {
return result;
return {};
}
auto localFilePath = findFileInlocatStorage(fileId);

View File

@ -187,8 +187,13 @@ public:
bool editSpecificMessage(const QVariant &messageId,
const TelegramArgs& args);
[[nodiscard("do not forget to save shared pointer of file handler, because it's will not save inner bot object.")]]
QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override;
/**
* @brief getFile This method sent request to get a file by id. The files can be saved into local storage if the Type choosed as Local.
* @param fileId This is Telegram file id.
* @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;
/**
* @brief getFileMeta This method receive meta information of the file.

View File

@ -1,46 +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 "virtualfile.h"
namespace qTbot {
VirtualFile::VirtualFile(const QSharedPointer<QNetworkReply> &replay): iFile(replay) {
}
const QByteArray& VirtualFile::array() const {
return _array;
}
iFile::Type VirtualFile::type() const {
return Type::Ram;
}
void VirtualFile::handleReadReady() {
_array.append(replay()->readAll());
}
void VirtualFile::handleFinished() {
handleReadReady();
iFile::handleFinished();
}
void VirtualFile::handleError(QNetworkReply::NetworkError error) {
iFile::handleError(error);
_array.clear();
}
void VirtualFile::setArray(const QByteArray &newArray) {
_array = newArray;
}
}

View File

@ -1,39 +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 VIRTUALFILE_H
#define VIRTUALFILE_H
#include "ifile.h"
namespace qTbot {
/**
* @brief The VirtualFile class write and read data from the Ram.
*/
class QTBOT_EXPORT VirtualFile : public iFile
{
public:
VirtualFile(const QSharedPointer<QNetworkReply>& replay = nullptr);
// iFile interface
const QByteArray &array() const;
Type type() const override;
void setArray(const QByteArray &newArray);
protected slots:
void handleReadReady() override;
void handleFinished() override;
void handleError(QNetworkReply::NetworkError error) override;
private:
QByteArray _array;
};
}
#endif // VIRTUALFILE_H