2019-09-17 15:16:09 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CORE_NETWORKACCESS_H
|
|
|
|
#define CORE_NETWORKACCESS_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "storage.h"
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo write docs
|
|
|
|
*/
|
|
|
|
class NetworkAccess : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2019-09-23 12:09:15 +00:00
|
|
|
struct Transfer;
|
2019-09-17 15:16:09 +00:00
|
|
|
public:
|
|
|
|
NetworkAccess(QObject* parent = nullptr);
|
|
|
|
virtual ~NetworkAccess();
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void fileLocalPathResponse(const QString& messageId, const QString& path);
|
|
|
|
void downloadFileProgress(const QString& messageId, qreal value);
|
2019-09-18 13:27:47 +00:00
|
|
|
void downloadFileError(const QString& messageId, const QString& path);
|
2019-09-23 12:09:15 +00:00
|
|
|
void uploadFileProgress(const QString& messageId, qreal value);
|
|
|
|
void uploadFileError(const QString& messageId, const QString& path);
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
void fileLocalPathRequest(const QString& messageId, const QString& url);
|
|
|
|
void downladFileRequest(const QString& messageId, const QString& url);
|
2019-09-23 12:09:15 +00:00
|
|
|
void uploadFileRequest(const QString& messageId, const QString& url, const QString& path);
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void startDownload(const QString& messageId, const QString& url);
|
2019-09-23 12:09:15 +00:00
|
|
|
void startUpload(const QString& messageId, const QString& url, const QString& path);
|
|
|
|
QString getErrorText(QNetworkReply::NetworkError code);
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
2019-09-23 12:09:15 +00:00
|
|
|
void onDownloadError(QNetworkReply::NetworkError code);
|
|
|
|
void onDownloadFinished();
|
|
|
|
void onUploadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
|
|
void onUploadError(QNetworkReply::NetworkError code);
|
|
|
|
void onUploadFinished();
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool running;
|
2019-09-18 13:27:47 +00:00
|
|
|
QNetworkAccessManager* manager;
|
2019-09-17 15:16:09 +00:00
|
|
|
Storage files;
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*> downloads;
|
|
|
|
std::map<QString, Transfer*> uploads;
|
2019-09-17 15:16:09 +00:00
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
struct Transfer {
|
2019-09-17 15:16:09 +00:00
|
|
|
std::set<QString> messages;
|
|
|
|
qreal progress;
|
|
|
|
QNetworkReply* reply;
|
|
|
|
bool success;
|
2019-09-23 12:09:15 +00:00
|
|
|
QString path;
|
|
|
|
QFile* file;
|
2019-09-17 15:16:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CORE_NETWORKACCESS_H
|