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>
|
2022-02-17 17:26:15 +00:00
|
|
|
#include <QSettings>
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2022-08-22 20:29:43 +00:00
|
|
|
#include <shared/pathcheck.h>
|
2023-04-15 18:07:27 +00:00
|
|
|
#include "urlstorage.h"
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
//TODO Need to describe how to get rid of records when file is no longer reachable;
|
2019-09-17 15:16:09 +00:00
|
|
|
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();
|
|
|
|
|
2019-11-09 14:04:27 +00:00
|
|
|
QString getFileRemoteUrl(const QString& path);
|
2021-04-18 12:49:20 +00:00
|
|
|
QString addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id);
|
|
|
|
void uploadFile(const Shared::MessageInfo& info, const QString& path, const QUrl& put, const QUrl& get, const QMap<QString, QString> headers);
|
|
|
|
bool checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path);
|
2021-04-28 20:26:19 +00:00
|
|
|
std::list<Shared::MessageInfo> reportPathInvalid(const QString& path);
|
2019-11-09 14:04:27 +00:00
|
|
|
|
2019-09-17 15:16:09 +00:00
|
|
|
signals:
|
2021-04-18 12:49:20 +00:00
|
|
|
void loadFileProgress(const std::list<Shared::MessageInfo>& msgs, qreal value, bool up);
|
|
|
|
void loadFileError(const std::list<Shared::MessageInfo>& msgs, const QString& text, bool up);
|
2021-10-15 16:20:31 +00:00
|
|
|
void uploadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& url, const QString& path);
|
2021-04-18 12:49:20 +00:00
|
|
|
void downloadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& path);
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
public slots:
|
2021-04-18 12:49:20 +00:00
|
|
|
void downladFile(const QString& url);
|
|
|
|
void registerFile(const QString& url, const QString& account, const QString& jid, const QString& id);
|
|
|
|
void registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id);
|
2022-02-17 17:26:15 +00:00
|
|
|
void moveFilesDirectory(const QString& newPath);
|
2019-09-17 15:16:09 +00:00
|
|
|
|
|
|
|
private:
|
2021-04-18 12:49:20 +00:00
|
|
|
void startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url);
|
2019-09-23 12:09:15 +00:00
|
|
|
QString getErrorText(QNetworkReply::NetworkError code);
|
2021-04-18 12:49:20 +00:00
|
|
|
QString prepareDirectory(const QString& jid);
|
|
|
|
QString checkFileName(const QString& name, const QString& path);
|
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);
|
2021-05-14 19:49:38 +00:00
|
|
|
void onDownloadSSLError(const QList<QSslError> &errors);
|
2019-09-23 12:09:15 +00:00
|
|
|
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;
|
2021-04-13 13:27:31 +00:00
|
|
|
UrlStorage storage;
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*> downloads;
|
|
|
|
std::map<QString, Transfer*> uploads;
|
2022-02-17 17:26:15 +00:00
|
|
|
QString currentPath;
|
2019-09-17 15:16:09 +00:00
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
struct Transfer {
|
2021-04-18 12:49:20 +00:00
|
|
|
std::list<Shared::MessageInfo> messages;
|
2019-09-17 15:16:09 +00:00
|
|
|
qreal progress;
|
|
|
|
QNetworkReply* reply;
|
|
|
|
bool success;
|
2019-09-23 12:09:15 +00:00
|
|
|
QString path;
|
2019-11-09 14:04:27 +00:00
|
|
|
QString url;
|
2019-09-23 12:09:15 +00:00
|
|
|
QFile* file;
|
2019-09-17 15:16:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CORE_NETWORKACCESS_H
|