mirror of
https://github.com/monero-project/monero-gui.git
synced 2025-01-10 04:45:00 +00:00
UpdateDialog: implement update download functionality
This commit is contained in:
parent
df54439972
commit
6ed7fcec67
13 changed files with 671 additions and 121 deletions
|
@ -33,11 +33,17 @@ import "../components" as MoneroComponents
|
||||||
|
|
||||||
Item {
|
Item {
|
||||||
id: button
|
id: button
|
||||||
|
property bool primary: true
|
||||||
property string rightIcon: ""
|
property string rightIcon: ""
|
||||||
property string rightIconInactive: ""
|
property string rightIconInactive: ""
|
||||||
property string textColor: button.enabled? MoneroComponents.Style.buttonTextColor: MoneroComponents.Style.buttonTextColorDisabled
|
property color textColor: !button.enabled
|
||||||
|
? MoneroComponents.Style.buttonTextColorDisabled
|
||||||
|
: primary
|
||||||
|
? MoneroComponents.Style.buttonTextColor
|
||||||
|
: MoneroComponents.Style.buttonSecondaryTextColor;
|
||||||
property bool small: false
|
property bool small: false
|
||||||
property alias text: label.text
|
property alias text: label.text
|
||||||
|
property alias fontBold: label.font.bold
|
||||||
property int fontSize: {
|
property int fontSize: {
|
||||||
if(small) return 14;
|
if(small) return 14;
|
||||||
else return 16;
|
else return 16;
|
||||||
|
@ -70,7 +76,9 @@ Item {
|
||||||
when: buttonArea.containsMouse || button.focus
|
when: buttonArea.containsMouse || button.focus
|
||||||
PropertyChanges {
|
PropertyChanges {
|
||||||
target: buttonRect
|
target: buttonRect
|
||||||
color: MoneroComponents.Style.buttonBackgroundColorHover
|
color: primary
|
||||||
|
? MoneroComponents.Style.buttonBackgroundColorHover
|
||||||
|
: MoneroComponents.Style.buttonSecondaryBackgroundColorHover
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
State {
|
State {
|
||||||
|
@ -78,7 +86,9 @@ Item {
|
||||||
when: button.enabled
|
when: button.enabled
|
||||||
PropertyChanges {
|
PropertyChanges {
|
||||||
target: buttonRect
|
target: buttonRect
|
||||||
color: MoneroComponents.Style.buttonBackgroundColor
|
color: primary
|
||||||
|
? MoneroComponents.Style.buttonBackgroundColor
|
||||||
|
: MoneroComponents.Style.buttonSecondaryBackgroundColor
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
State {
|
State {
|
||||||
|
|
|
@ -43,6 +43,9 @@ QtObject {
|
||||||
property string buttonInlineBackgroundColor: blackTheme ? _b_buttonInlineBackgroundColor : _w_buttonInlineBackgroundColor
|
property string buttonInlineBackgroundColor: blackTheme ? _b_buttonInlineBackgroundColor : _w_buttonInlineBackgroundColor
|
||||||
property string buttonTextColor: blackTheme ? _b_buttonTextColor : _w_buttonTextColor
|
property string buttonTextColor: blackTheme ? _b_buttonTextColor : _w_buttonTextColor
|
||||||
property string buttonTextColorDisabled: blackTheme ? _b_buttonTextColorDisabled : _w_buttonTextColorDisabled
|
property string buttonTextColorDisabled: blackTheme ? _b_buttonTextColorDisabled : _w_buttonTextColorDisabled
|
||||||
|
property string buttonSecondaryBackgroundColor: "#d9d9d9"
|
||||||
|
property string buttonSecondaryBackgroundColorHover: "#a6a6a6"
|
||||||
|
property string buttonSecondaryTextColor: "#4d4d4d"
|
||||||
property string dividerColor: blackTheme ? _b_dividerColor : _w_dividerColor
|
property string dividerColor: blackTheme ? _b_dividerColor : _w_dividerColor
|
||||||
property real dividerOpacity: blackTheme ? _b_dividerOpacity : _w_dividerOpacity
|
property real dividerOpacity: blackTheme ? _b_dividerOpacity : _w_dividerOpacity
|
||||||
|
|
||||||
|
|
200
components/UpdateDialog.qml
Normal file
200
components/UpdateDialog.qml
Normal file
|
@ -0,0 +1,200 @@
|
||||||
|
// Copyright (c) 2020, The Monero Project
|
||||||
|
//
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without modification, are
|
||||||
|
// permitted provided that the following conditions are met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||||
|
// conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||||
|
// of conditions and the following disclaimer in the documentation and/or other
|
||||||
|
// materials provided with the distribution.
|
||||||
|
//
|
||||||
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||||
|
// used to endorse or promote products derived from this software without specific
|
||||||
|
// prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||||
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||||
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
import QtQuick 2.9
|
||||||
|
import QtQuick.Controls 2.2
|
||||||
|
import QtQuick.Layouts 1.1
|
||||||
|
|
||||||
|
import moneroComponents.Downloader 1.0
|
||||||
|
|
||||||
|
import "../components" as MoneroComponents
|
||||||
|
|
||||||
|
Popup {
|
||||||
|
id: updateDialog
|
||||||
|
|
||||||
|
property bool allowed: true
|
||||||
|
property string error: ""
|
||||||
|
property string filename: ""
|
||||||
|
property double progress: url && downloader.total > 0 ? downloader.loaded * 100 / downloader.total : 0
|
||||||
|
property bool active: false
|
||||||
|
property string url: ""
|
||||||
|
property bool valid: false
|
||||||
|
property string version: ""
|
||||||
|
|
||||||
|
background: Rectangle {
|
||||||
|
border.color: MoneroComponents.Style.appWindowBorderColor
|
||||||
|
border.width: 1
|
||||||
|
color: MoneroComponents.Style.middlePanelBackgroundColor
|
||||||
|
}
|
||||||
|
closePolicy: Popup.NoAutoClose
|
||||||
|
padding: 20
|
||||||
|
visible: active && allowed
|
||||||
|
|
||||||
|
function show(version, url) {
|
||||||
|
updateDialog.error = "";
|
||||||
|
updateDialog.url = url;
|
||||||
|
updateDialog.valid = false;
|
||||||
|
updateDialog.version = version;
|
||||||
|
updateDialog.active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ColumnLayout {
|
||||||
|
id: mainLayout
|
||||||
|
spacing: updateDialog.padding
|
||||||
|
|
||||||
|
Text {
|
||||||
|
color: MoneroComponents.Style.defaultFontColor
|
||||||
|
font.bold: true
|
||||||
|
font.family: MoneroComponents.Style.fontRegular.name
|
||||||
|
font.pixelSize: 18
|
||||||
|
text: qsTr("New Monero version v%1 is available.").arg(updateDialog.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
Text {
|
||||||
|
id: errorText
|
||||||
|
color: "red"
|
||||||
|
font.family: MoneroComponents.Style.fontRegular.name
|
||||||
|
font.pixelSize: 18
|
||||||
|
text: updateDialog.error
|
||||||
|
visible: text
|
||||||
|
}
|
||||||
|
|
||||||
|
Text {
|
||||||
|
id: statusText
|
||||||
|
color: MoneroComponents.Style.defaultFontColor
|
||||||
|
font.family: MoneroComponents.Style.fontRegular.name
|
||||||
|
font.pixelSize: 18
|
||||||
|
visible: !errorText.visible
|
||||||
|
|
||||||
|
text: {
|
||||||
|
if (!updateDialog.url) {
|
||||||
|
return qsTr("Please visit getmonero.org for details") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
if (downloader.active) {
|
||||||
|
return "%1 (%2%)"
|
||||||
|
.arg(qsTr("Downloading"))
|
||||||
|
.arg(updateDialog.progress.toFixed(1))
|
||||||
|
+ translationManager.emptyString;
|
||||||
|
}
|
||||||
|
if (updateDialog.valid) {
|
||||||
|
return qsTr("Download finished") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
return qsTr("Do you want to download new version?") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle {
|
||||||
|
id: progressBar
|
||||||
|
color: MoneroComponents.Style.lightGreyFontColor
|
||||||
|
height: 3
|
||||||
|
Layout.fillWidth: true
|
||||||
|
visible: updateDialog.valid || downloader.active
|
||||||
|
|
||||||
|
Rectangle {
|
||||||
|
color: MoneroComponents.Style.buttonBackgroundColor
|
||||||
|
height: parent.height
|
||||||
|
width: parent.width * updateDialog.progress / 100
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RowLayout {
|
||||||
|
Layout.alignment: Qt.AlignRight
|
||||||
|
spacing: parent.spacing
|
||||||
|
|
||||||
|
MoneroComponents.StandardButton {
|
||||||
|
id: cancelButton
|
||||||
|
fontBold: false
|
||||||
|
primary: !updateDialog.url
|
||||||
|
text: {
|
||||||
|
if (!updateDialog.url) {
|
||||||
|
return qsTr("Ok") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
if (updateDialog.valid || downloader.active || errorText.visible) {
|
||||||
|
return qsTr("Cancel") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
return qsTr("Download later") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
|
||||||
|
onClicked: {
|
||||||
|
downloader.cancel();
|
||||||
|
updateDialog.active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoneroComponents.StandardButton {
|
||||||
|
id: downloadButton
|
||||||
|
KeyNavigation.tab: cancelButton
|
||||||
|
fontBold: false
|
||||||
|
text: (updateDialog.error ? qsTr("Retry") : qsTr("Download")) + translationManager.emptyString
|
||||||
|
visible: updateDialog.url && !updateDialog.valid && !downloader.active
|
||||||
|
|
||||||
|
onClicked: {
|
||||||
|
updateDialog.error = "";
|
||||||
|
updateDialog.filename = updateDialog.url.replace(/^.*\//, '');
|
||||||
|
const downloadingStarted = downloader.get(updateDialog.url, function(error) {
|
||||||
|
if (error) {
|
||||||
|
updateDialog.error = qsTr("Download failed") + translationManager.emptyString;
|
||||||
|
} else {
|
||||||
|
updateDialog.valid = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!downloadingStarted) {
|
||||||
|
updateDialog.error = qsTr("Failed to start download") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoneroComponents.StandardButton {
|
||||||
|
id: saveButton
|
||||||
|
KeyNavigation.tab: cancelButton
|
||||||
|
fontBold: false
|
||||||
|
onClicked: {
|
||||||
|
const fullPath = oshelper.openSaveFileDialog(
|
||||||
|
qsTr("Save as") + translationManager.emptyString,
|
||||||
|
oshelper.downloadLocation(),
|
||||||
|
updateDialog.filename);
|
||||||
|
if (!fullPath) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (downloader.saveToFile(fullPath)) {
|
||||||
|
cancelButton.clicked();
|
||||||
|
oshelper.openContainingFolder(fullPath);
|
||||||
|
} else {
|
||||||
|
updateDialog.error = qsTr("Save operation failed") + translationManager.emptyString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
text: qsTr("Save to file") + translationManager.emptyString
|
||||||
|
visible: updateDialog.valid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Downloader {
|
||||||
|
id: downloader
|
||||||
|
}
|
||||||
|
}
|
31
main.qml
31
main.qml
|
@ -1436,6 +1436,14 @@ ApplicationWindow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MoneroComponents.UpdateDialog {
|
||||||
|
id: updateDialog
|
||||||
|
|
||||||
|
allowed: !passwordDialog.visible && !inputDialog.visible && !splash.visible
|
||||||
|
x: (parent.width - width) / 2
|
||||||
|
y: (parent.height - height) / 2
|
||||||
|
}
|
||||||
|
|
||||||
// Choose blockchain folder
|
// Choose blockchain folder
|
||||||
FileDialog {
|
FileDialog {
|
||||||
id: blockchainFileDialog
|
id: blockchainFileDialog
|
||||||
|
@ -1691,7 +1699,7 @@ ApplicationWindow {
|
||||||
anchors.fill: blurredArea
|
anchors.fill: blurredArea
|
||||||
source: blurredArea
|
source: blurredArea
|
||||||
radius: 64
|
radius: 64
|
||||||
visible: passwordDialog.visible || inputDialog.visible || splash.visible
|
visible: passwordDialog.visible || inputDialog.visible || splash.visible || updateDialog.visible
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1798,11 +1806,6 @@ ApplicationWindow {
|
||||||
color: "#FFFFFF"
|
color: "#FFFFFF"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Notifier {
|
|
||||||
visible:false
|
|
||||||
id: notifier
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleLanguageView(){
|
function toggleLanguageView(){
|
||||||
|
@ -1981,16 +1984,7 @@ ApplicationWindow {
|
||||||
print("Update found: " + update)
|
print("Update found: " + update)
|
||||||
var parts = update.split("|")
|
var parts = update.split("|")
|
||||||
if (parts.length == 4) {
|
if (parts.length == 4) {
|
||||||
var version = parts[0]
|
updateDialog.show(parts[0], isMac || isWindows || isLinux ? parts[3] : "");
|
||||||
var hash = parts[1]
|
|
||||||
var user_url = parts[2]
|
|
||||||
var msg = qsTr("New version of Monero v%1 is available.").arg(version)
|
|
||||||
if (isMac || isWindows || isLinux) {
|
|
||||||
msg += "<br><br>%1:<br>%2<br><br>%3:<br>%4".arg(qsTr("Download")).arg(user_url).arg(qsTr("SHA256 Hash")).arg(hash) + translationManager.emptyString
|
|
||||||
} else {
|
|
||||||
msg += " " + qsTr("Check out getmonero.org") + translationManager.emptyString
|
|
||||||
}
|
|
||||||
notifier.show(msg)
|
|
||||||
} else {
|
} else {
|
||||||
print("Failed to parse update spec")
|
print("Failed to parse update spec")
|
||||||
}
|
}
|
||||||
|
@ -2102,6 +2096,11 @@ ApplicationWindow {
|
||||||
blackColor: "black"
|
blackColor: "black"
|
||||||
whiteColor: "white"
|
whiteColor: "white"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MouseArea {
|
||||||
|
anchors.fill: parent
|
||||||
|
hoverEnabled: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// borders on white theme + linux
|
// borders on white theme + linux
|
||||||
|
|
|
@ -76,6 +76,7 @@ HEADERS += \
|
||||||
src/libwalletqt/UnsignedTransaction.h \
|
src/libwalletqt/UnsignedTransaction.h \
|
||||||
src/main/Logger.h \
|
src/main/Logger.h \
|
||||||
src/main/MainApp.h \
|
src/main/MainApp.h \
|
||||||
|
src/qt/downloader.h \
|
||||||
src/qt/FutureScheduler.h \
|
src/qt/FutureScheduler.h \
|
||||||
src/qt/ipc.h \
|
src/qt/ipc.h \
|
||||||
src/qt/KeysFiles.h \
|
src/qt/KeysFiles.h \
|
||||||
|
@ -112,6 +113,7 @@ SOURCES += src/main/main.cpp \
|
||||||
src/libwalletqt/UnsignedTransaction.cpp \
|
src/libwalletqt/UnsignedTransaction.cpp \
|
||||||
src/main/Logger.cpp \
|
src/main/Logger.cpp \
|
||||||
src/main/MainApp.cpp \
|
src/main/MainApp.cpp \
|
||||||
|
src/qt/downloader.cpp \
|
||||||
src/qt/FutureScheduler.cpp \
|
src/qt/FutureScheduler.cpp \
|
||||||
src/qt/ipc.cpp \
|
src/qt/ipc.cpp \
|
||||||
src/qt/KeysFiles.cpp \
|
src/qt/KeysFiles.cpp \
|
||||||
|
|
2
qml.qrc
2
qml.qrc
|
@ -5,6 +5,7 @@
|
||||||
<file>MiddlePanel.qml</file>
|
<file>MiddlePanel.qml</file>
|
||||||
<file>components/Label.qml</file>
|
<file>components/Label.qml</file>
|
||||||
<file>components/SettingsListItem.qml</file>
|
<file>components/SettingsListItem.qml</file>
|
||||||
|
<file>components/UpdateDialog.qml</file>
|
||||||
<file>images/whatIsIcon.png</file>
|
<file>images/whatIsIcon.png</file>
|
||||||
<file>images/whatIsIcon@2x.png</file>
|
<file>images/whatIsIcon@2x.png</file>
|
||||||
<file>images/lockIcon.png</file>
|
<file>images/lockIcon.png</file>
|
||||||
|
@ -100,7 +101,6 @@
|
||||||
<file>components/DaemonManagerDialog.qml</file>
|
<file>components/DaemonManagerDialog.qml</file>
|
||||||
<file>version.js</file>
|
<file>version.js</file>
|
||||||
<file>components/QRCodeScanner.qml</file>
|
<file>components/QRCodeScanner.qml</file>
|
||||||
<file>components/Notifier.qml</file>
|
|
||||||
<file>components/TextBlock.qml</file>
|
<file>components/TextBlock.qml</file>
|
||||||
<file>components/RemoteNodeEdit.qml</file>
|
<file>components/RemoteNodeEdit.qml</file>
|
||||||
<file>pages/Keys.qml</file>
|
<file>pages/Keys.qml</file>
|
||||||
|
|
|
@ -61,6 +61,7 @@
|
||||||
#include "wallet/api/wallet2_api.h"
|
#include "wallet/api/wallet2_api.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "MainApp.h"
|
#include "MainApp.h"
|
||||||
|
#include "qt/downloader.h"
|
||||||
#include "qt/ipc.h"
|
#include "qt/ipc.h"
|
||||||
#include "qt/network.h"
|
#include "qt/network.h"
|
||||||
#include "qt/utils.h"
|
#include "qt/utils.h"
|
||||||
|
@ -295,6 +296,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
// registering types for QML
|
// registering types for QML
|
||||||
qmlRegisterType<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard");
|
qmlRegisterType<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard");
|
||||||
|
qmlRegisterType<Downloader>("moneroComponents.Downloader", 1, 0, "Downloader");
|
||||||
|
|
||||||
// Temporary Qt.labs.settings replacement
|
// Temporary Qt.labs.settings replacement
|
||||||
qmlRegisterType<MoneroSettings>("moneroComponents.Settings", 1, 0, "MoneroSettings");
|
qmlRegisterType<MoneroSettings>("moneroComponents.Settings", 1, 0, "MoneroSettings");
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#include "oshelper.h"
|
#include "oshelper.h"
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QStandardPaths>
|
||||||
#include <QTemporaryFile>
|
#include <QTemporaryFile>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
@ -82,6 +84,11 @@ OSHelper::OSHelper(QObject *parent) : QObject(parent)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OSHelper::downloadLocation() const
|
||||||
|
{
|
||||||
|
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
||||||
|
}
|
||||||
|
|
||||||
bool OSHelper::openContainingFolder(const QString &filePath) const
|
bool OSHelper::openContainingFolder(const QString &filePath) const
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_WIN)
|
#if defined(Q_OS_WIN)
|
||||||
|
@ -105,6 +112,12 @@ bool OSHelper::openContainingFolder(const QString &filePath) const
|
||||||
return QDesktopServices::openUrl(url);
|
return QDesktopServices::openUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OSHelper::openSaveFileDialog(const QString &title, const QString &folder, const QString &filename) const
|
||||||
|
{
|
||||||
|
const QString hint = (folder.isEmpty() ? "" : folder + QDir::separator()) + filename;
|
||||||
|
return QFileDialog::getSaveFileName(nullptr, title, hint);
|
||||||
|
}
|
||||||
|
|
||||||
QString OSHelper::temporaryFilename() const
|
QString OSHelper::temporaryFilename() const
|
||||||
{
|
{
|
||||||
QString tempFileName;
|
QString tempFileName;
|
||||||
|
|
|
@ -39,7 +39,9 @@ class OSHelper : public QObject
|
||||||
public:
|
public:
|
||||||
explicit OSHelper(QObject *parent = 0);
|
explicit OSHelper(QObject *parent = 0);
|
||||||
|
|
||||||
|
Q_INVOKABLE QString downloadLocation() const;
|
||||||
Q_INVOKABLE bool openContainingFolder(const QString &filePath) const;
|
Q_INVOKABLE bool openContainingFolder(const QString &filePath) const;
|
||||||
|
Q_INVOKABLE QString openSaveFileDialog(const QString &title, const QString &folder, const QString &filename) const;
|
||||||
Q_INVOKABLE QString temporaryFilename() const;
|
Q_INVOKABLE QString temporaryFilename() const;
|
||||||
Q_INVOKABLE QString temporaryPath() const;
|
Q_INVOKABLE QString temporaryPath() const;
|
||||||
Q_INVOKABLE bool removeTemporaryWallet(const QString &walletName) const;
|
Q_INVOKABLE bool removeTemporaryWallet(const QString &walletName) const;
|
||||||
|
|
207
src/qt/downloader.cpp
Normal file
207
src/qt/downloader.cpp
Normal file
|
@ -0,0 +1,207 @@
|
||||||
|
// Copyright (c) 2020, The Monero Project
|
||||||
|
//
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without modification, are
|
||||||
|
// permitted provided that the following conditions are met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||||
|
// conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||||
|
// of conditions and the following disclaimer in the documentation and/or other
|
||||||
|
// materials provided with the distribution.
|
||||||
|
//
|
||||||
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||||
|
// used to endorse or promote products derived from this software without specific
|
||||||
|
// prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||||
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||||
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
#include "downloader.h"
|
||||||
|
|
||||||
|
#include <QReadLocker>
|
||||||
|
#include <QWriteLocker>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
class DownloaderStateGuard
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DownloaderStateGuard(bool &active, QReadWriteLock &mutex, std::function<void()> onActiveChanged)
|
||||||
|
: m_active(active)
|
||||||
|
, m_acquired(false)
|
||||||
|
, m_mutex(mutex)
|
||||||
|
, m_onActiveChanged(std::move(onActiveChanged))
|
||||||
|
{
|
||||||
|
{
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
if (m_active)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_active = true;
|
||||||
|
}
|
||||||
|
m_onActiveChanged();
|
||||||
|
|
||||||
|
m_acquired = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
~DownloaderStateGuard()
|
||||||
|
{
|
||||||
|
if (!m_acquired)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
m_active = false;
|
||||||
|
}
|
||||||
|
m_onActiveChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool acquired() const
|
||||||
|
{
|
||||||
|
return m_acquired;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool &m_active;
|
||||||
|
bool m_acquired;
|
||||||
|
QReadWriteLock &m_mutex;
|
||||||
|
std::function<void()> m_onActiveChanged;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Downloader::Downloader(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_active(false)
|
||||||
|
, m_httpClient(new HttpClient())
|
||||||
|
, m_network(this)
|
||||||
|
, m_scheduler(this)
|
||||||
|
{
|
||||||
|
QObject::connect(m_httpClient.get(), SIGNAL(contentLengthChanged()), this, SIGNAL(totalChanged()));
|
||||||
|
QObject::connect(m_httpClient.get(), SIGNAL(receivedChanged()), this, SIGNAL(loadedChanged()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Downloader::~Downloader()
|
||||||
|
{
|
||||||
|
cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Downloader::cancel()
|
||||||
|
{
|
||||||
|
m_httpClient->cancel();
|
||||||
|
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
m_contents.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Downloader::get(const QString &url, const QJSValue &callback)
|
||||||
|
{
|
||||||
|
auto future = m_scheduler.run(
|
||||||
|
[this, url]() {
|
||||||
|
DownloaderStateGuard stateGuard(m_active, m_mutex, [this]() {
|
||||||
|
emit activeChanged();
|
||||||
|
});
|
||||||
|
if (!stateGuard.acquired())
|
||||||
|
{
|
||||||
|
return QJSValueList({"downloading is already running"});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
m_contents.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string response;
|
||||||
|
{
|
||||||
|
QString error;
|
||||||
|
auto task = m_scheduler.run([this, &error, &response, &url] {
|
||||||
|
error = m_network.get(m_httpClient, url, response);
|
||||||
|
});
|
||||||
|
if (!task.first)
|
||||||
|
{
|
||||||
|
return QJSValueList({"failed to start downloading task"});
|
||||||
|
}
|
||||||
|
task.second.waitForFinished();
|
||||||
|
|
||||||
|
if (!error.isEmpty())
|
||||||
|
{
|
||||||
|
return QJSValueList({error});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.empty())
|
||||||
|
{
|
||||||
|
return QJSValueList({"empty response"});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
m_contents = std::move(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
return QJSValueList({});
|
||||||
|
},
|
||||||
|
callback);
|
||||||
|
|
||||||
|
return future.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Downloader::saveToFile(const QString &path) const
|
||||||
|
{
|
||||||
|
QWriteLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
if (m_active || m_contents.empty())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile file(path);
|
||||||
|
if (!file.open(QIODevice::WriteOnly))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (static_cast<size_t>(file.write(m_contents.data(), m_contents.size())) != m_contents.size())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Downloader::active() const
|
||||||
|
{
|
||||||
|
QReadLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
return m_active;
|
||||||
|
}
|
||||||
|
|
||||||
|
quint64 Downloader::loaded() const
|
||||||
|
{
|
||||||
|
return m_httpClient->received();
|
||||||
|
}
|
||||||
|
|
||||||
|
quint64 Downloader::total() const
|
||||||
|
{
|
||||||
|
return m_httpClient->contentLength();
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2017-2018, The Monero Project
|
// Copyright (c) 2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -26,60 +26,42 @@
|
||||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
import QtQuick 2.9
|
#pragma once
|
||||||
import QtQuick.Controls 1.4
|
|
||||||
import moneroComponents.Wallet 1.0
|
|
||||||
import "." as MoneroComponents
|
|
||||||
|
|
||||||
Item {
|
#include <QReadWriteLock>
|
||||||
id: item
|
|
||||||
property string message: ""
|
|
||||||
property bool active: false
|
|
||||||
height: 180
|
|
||||||
width: 320
|
|
||||||
property int margin: 15
|
|
||||||
x: parent.width - width - margin
|
|
||||||
y: parent.height - height * scale.yScale - margin * scale.yScale
|
|
||||||
|
|
||||||
Rectangle {
|
#include "network.h"
|
||||||
color: "#FF6C3C"
|
|
||||||
border.color: "black"
|
|
||||||
anchors.fill: parent
|
|
||||||
|
|
||||||
TextArea {
|
class Downloader : public QObject
|
||||||
id:versionText
|
{
|
||||||
readOnly: true
|
Q_OBJECT
|
||||||
backgroundVisible: false
|
Q_PROPERTY(bool active READ active NOTIFY activeChanged);
|
||||||
textFormat: TextEdit.AutoText
|
Q_PROPERTY(quint64 loaded READ loaded NOTIFY loadedChanged);
|
||||||
anchors.fill: parent
|
Q_PROPERTY(quint64 total READ total NOTIFY totalChanged);
|
||||||
font.family: MoneroComponents.Style.fontRegular.name
|
|
||||||
font.pixelSize: 12
|
|
||||||
textMargin: 20
|
|
||||||
textColor: "white"
|
|
||||||
text: item.message
|
|
||||||
wrapMode: Text.WrapAnywhere
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
transform: Scale {
|
public:
|
||||||
id: scale
|
Downloader(QObject *parent = nullptr);
|
||||||
yScale: item.active ? 1 : 0
|
~Downloader();
|
||||||
|
|
||||||
Behavior on yScale {
|
Q_INVOKABLE void cancel();
|
||||||
NumberAnimation { duration: 500; easing.type: Easing.InOutCubic }
|
Q_INVOKABLE bool get(const QString &url, const QJSValue &callback);
|
||||||
}
|
Q_INVOKABLE bool saveToFile(const QString &path) const;
|
||||||
}
|
|
||||||
|
|
||||||
Timer {
|
signals:
|
||||||
id: hider
|
void activeChanged() const;
|
||||||
interval: 30000; running: false; repeat: false
|
void loadedChanged() const;
|
||||||
onTriggered: { item.active = false }
|
void totalChanged() const;
|
||||||
}
|
|
||||||
|
|
||||||
function show(message) {
|
private:
|
||||||
item.visible = true
|
bool active() const;
|
||||||
item.message = message
|
quint64 loaded() const;
|
||||||
item.active = true
|
quint64 total() const;
|
||||||
hider.running = true
|
|
||||||
}
|
private:
|
||||||
}
|
bool m_active;
|
||||||
|
std::string m_contents;
|
||||||
|
std::shared_ptr<HttpClient> m_httpClient;
|
||||||
|
mutable QReadWriteLock m_mutex;
|
||||||
|
Network m_network;
|
||||||
|
mutable FutureScheduler m_scheduler;
|
||||||
|
};
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2014-2019, The Monero Project
|
// Copyright (c) 2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -31,57 +31,83 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
|
|
||||||
// TODO: wallet_merged - epee library triggers the warnings
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
||||||
#pragma GCC diagnostic ignored "-Wreorder"
|
|
||||||
#include <net/http_client.h>
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
using epee::net_utils::http::fields_list;
|
||||||
|
using epee::net_utils::http::http_response_info;
|
||||||
|
using epee::net_utils::http::http_simple_client;
|
||||||
|
|
||||||
|
HttpClient::HttpClient(QObject *parent /* = nullptr */)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_cancel(false)
|
||||||
|
, m_contentLength(0)
|
||||||
|
, m_received(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void HttpClient::cancel()
|
||||||
|
{
|
||||||
|
m_cancel = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
quint64 HttpClient::contentLength() const
|
||||||
|
{
|
||||||
|
return m_contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
quint64 HttpClient::received() const
|
||||||
|
{
|
||||||
|
return m_received;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HttpClient::on_header(const http_response_info &headers)
|
||||||
|
{
|
||||||
|
if (m_cancel.exchange(false))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t contentLength = 0;
|
||||||
|
if (!epee::string_tools::get_xtype_from_string(contentLength, headers.m_header_info.m_content_length))
|
||||||
|
{
|
||||||
|
qWarning() << "Failed to get Content-Length";
|
||||||
|
}
|
||||||
|
m_contentLength = contentLength;
|
||||||
|
emit contentLengthChanged();
|
||||||
|
|
||||||
|
m_received = 0;
|
||||||
|
emit receivedChanged();
|
||||||
|
|
||||||
|
return http_simple_client::on_header(headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HttpClient::handle_target_data(std::string &piece_of_transfer)
|
||||||
|
{
|
||||||
|
if (m_cancel.exchange(false))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_received += piece_of_transfer.size();
|
||||||
|
emit receivedChanged();
|
||||||
|
|
||||||
|
return http_simple_client::handle_target_data(piece_of_transfer);
|
||||||
|
}
|
||||||
|
|
||||||
Network::Network(QObject *parent)
|
Network::Network(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_scheduler(this)
|
, m_scheduler(this)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::get(const QString &url, const QJSValue &callback, const QString &contentType) const
|
void Network::get(const QString &url, const QJSValue &callback, const QString &contentType /* = {} */) const
|
||||||
{
|
{
|
||||||
qDebug() << QString("Fetching: %1").arg(url);
|
|
||||||
|
|
||||||
m_scheduler.run(
|
m_scheduler.run(
|
||||||
[url, contentType] {
|
[this, url, contentType] {
|
||||||
epee::net_utils::http::http_simple_client httpClient;
|
std::string response;
|
||||||
|
std::shared_ptr<http_simple_client> httpClient(new http_simple_client());
|
||||||
const QUrl urlParsed(url);
|
QString error = get(httpClient, url, response, contentType);
|
||||||
httpClient.set_server(urlParsed.host().toStdString(), urlParsed.scheme() == "https" ? "443" : "80", {});
|
return QJSValueList({url, QString::fromStdString(response), error});
|
||||||
|
|
||||||
const QString uri = (urlParsed.hasQuery() ? urlParsed.path() + "?" + urlParsed.query() : urlParsed.path());
|
|
||||||
const epee::net_utils::http::http_response_info *pri = NULL;
|
|
||||||
constexpr std::chrono::milliseconds timeout = std::chrono::seconds(15);
|
|
||||||
|
|
||||||
epee::net_utils::http::fields_list headers({{"User-Agent", randomUserAgent().toStdString()}});
|
|
||||||
if (!contentType.isEmpty())
|
|
||||||
{
|
|
||||||
headers.push_back({"Content-Type", contentType.toStdString()});
|
|
||||||
}
|
|
||||||
const bool result = httpClient.invoke(uri.toStdString(), "GET", {}, timeout, std::addressof(pri), headers);
|
|
||||||
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return QJSValueList({QJSValue(), QJSValue(), "unknown error"});
|
|
||||||
}
|
|
||||||
if (!pri)
|
|
||||||
{
|
|
||||||
return QJSValueList({QJSValue(), QJSValue(), "internal error (null response ptr)"});
|
|
||||||
}
|
|
||||||
if (pri->m_response_code != 200)
|
|
||||||
{
|
|
||||||
return QJSValueList({QJSValue(), QJSValue(), QString("response code: %1").arg(pri->m_response_code)});
|
|
||||||
}
|
|
||||||
|
|
||||||
return QJSValueList({url, QString::fromStdString(pri->m_body)});
|
|
||||||
},
|
},
|
||||||
callback);
|
callback);
|
||||||
}
|
}
|
||||||
|
@ -90,3 +116,39 @@ void Network::getJSON(const QString &url, const QJSValue &callback) const
|
||||||
{
|
{
|
||||||
get(url, callback, "application/json; charset=utf-8");
|
get(url, callback, "application/json; charset=utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString Network::get(
|
||||||
|
std::shared_ptr<http_simple_client> httpClient,
|
||||||
|
const QString &url,
|
||||||
|
std::string &response,
|
||||||
|
const QString &contentType /* = {} */) const
|
||||||
|
{
|
||||||
|
const QUrl urlParsed(url);
|
||||||
|
httpClient->set_server(urlParsed.host().toStdString(), urlParsed.scheme() == "https" ? "443" : "80", {});
|
||||||
|
|
||||||
|
const QString uri = (urlParsed.hasQuery() ? urlParsed.path() + "?" + urlParsed.query() : urlParsed.path());
|
||||||
|
const http_response_info *pri = NULL;
|
||||||
|
constexpr std::chrono::milliseconds timeout = std::chrono::seconds(15);
|
||||||
|
|
||||||
|
fields_list headers({{"User-Agent", randomUserAgent().toStdString()}});
|
||||||
|
if (!contentType.isEmpty())
|
||||||
|
{
|
||||||
|
headers.push_back({"Content-Type", contentType.toStdString()});
|
||||||
|
}
|
||||||
|
const bool result = httpClient->invoke(uri.toStdString(), "GET", {}, timeout, std::addressof(pri), headers);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return "unknown error";
|
||||||
|
}
|
||||||
|
if (!pri)
|
||||||
|
{
|
||||||
|
return "internal error";
|
||||||
|
}
|
||||||
|
if (pri->m_response_code != 200)
|
||||||
|
{
|
||||||
|
return QString("response code %1").arg(pri->m_response_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
response = std::move(pri->m_body);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,72 @@
|
||||||
|
// Copyright (c) 2020, The Monero Project
|
||||||
|
//
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without modification, are
|
||||||
|
// permitted provided that the following conditions are met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||||
|
// conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||||
|
// of conditions and the following disclaimer in the documentation and/or other
|
||||||
|
// materials provided with the distribution.
|
||||||
|
//
|
||||||
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||||
|
// used to endorse or promote products derived from this software without specific
|
||||||
|
// prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||||
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||||
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QtNetwork>
|
#include <QtNetwork>
|
||||||
|
|
||||||
|
// TODO: wallet_merged - epee library triggers the warnings
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||||
|
#pragma GCC diagnostic ignored "-Wreorder"
|
||||||
|
#include <net/http_client.h>
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
#include "FutureScheduler.h"
|
#include "FutureScheduler.h"
|
||||||
|
|
||||||
|
class HttpClient : public QObject, public epee::net_utils::http::http_simple_client
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(quint64 contentLength READ contentLength NOTIFY contentLengthChanged);
|
||||||
|
Q_PROPERTY(quint64 received READ received NOTIFY receivedChanged);
|
||||||
|
|
||||||
|
public:
|
||||||
|
HttpClient(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
void cancel();
|
||||||
|
quint64 contentLength() const;
|
||||||
|
quint64 received() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void contentLengthChanged() const;
|
||||||
|
void receivedChanged() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool on_header(const epee::net_utils::http::http_response_info &headers) final;
|
||||||
|
bool handle_target_data(std::string &piece_of_transfer) final;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::atomic<bool> m_cancel;
|
||||||
|
std::atomic<size_t> m_contentLength;
|
||||||
|
std::atomic<size_t> m_received;
|
||||||
|
};
|
||||||
|
|
||||||
class Network : public QObject
|
class Network : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -15,6 +77,12 @@ public:
|
||||||
Q_INVOKABLE void get(const QString &url, const QJSValue &callback, const QString &contentType = {}) const;
|
Q_INVOKABLE void get(const QString &url, const QJSValue &callback, const QString &contentType = {}) const;
|
||||||
Q_INVOKABLE void getJSON(const QString &url, const QJSValue &callback) const;
|
Q_INVOKABLE void getJSON(const QString &url, const QJSValue &callback) const;
|
||||||
|
|
||||||
|
QString get(
|
||||||
|
std::shared_ptr<epee::net_utils::http::http_simple_client> httpClient,
|
||||||
|
const QString &url,
|
||||||
|
std::string &response,
|
||||||
|
const QString &contentType = {}) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable FutureScheduler m_scheduler;
|
mutable FutureScheduler m_scheduler;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue