1 | /* $Id: UIDownloader.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIDownloader class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | #include "VBoxUtils.h"
|
---|
30 | #include "UIDownloader.h"
|
---|
31 | #include "UIMessageCenter.h"
|
---|
32 | #include "UINetworkReply.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | UIDownloader::UIDownloader()
|
---|
36 | : m_state(UIDownloaderState_Null)
|
---|
37 | {
|
---|
38 | /* Connect async listeners for our own commands: */
|
---|
39 | connect(this, &UIDownloader::sigToStartAcknowledging, this, &UIDownloader::sltStartAcknowledging, Qt::QueuedConnection);
|
---|
40 | connect(this, &UIDownloader::sigToStartDownloading, this, &UIDownloader::sltStartDownloading, Qt::QueuedConnection);
|
---|
41 | connect(this, &UIDownloader::sigToStartVerifying, this, &UIDownloader::sltStartVerifying, Qt::QueuedConnection);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void UIDownloader::sltStartAcknowledging()
|
---|
45 | {
|
---|
46 | /* Set state to acknowledging: */
|
---|
47 | m_state = UIDownloaderState_Acknowledging;
|
---|
48 |
|
---|
49 | /* Send HEAD requests: */
|
---|
50 | createNetworkRequest(UINetworkRequestType_HEAD, m_sources);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void UIDownloader::sltStartDownloading()
|
---|
54 | {
|
---|
55 | /* Set state to downloading: */
|
---|
56 | m_state = UIDownloaderState_Downloading;
|
---|
57 |
|
---|
58 | /* Send GET request: */
|
---|
59 | createNetworkRequest(UINetworkRequestType_GET, QList<QUrl>() << m_source, m_strTarget);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void UIDownloader::sltStartVerifying()
|
---|
63 | {
|
---|
64 | /* Set state to verifying: */
|
---|
65 | m_state = UIDownloaderState_Verifying;
|
---|
66 |
|
---|
67 | /* Send GET request: */
|
---|
68 | createNetworkRequest(UINetworkRequestType_GET, QList<QUrl>() << m_strPathSHA256SumsFile);
|
---|
69 | }
|
---|
70 |
|
---|
71 | QString UIDownloader::description() const
|
---|
72 | {
|
---|
73 | /* Look for known state: */
|
---|
74 | switch (m_state)
|
---|
75 | {
|
---|
76 | case UIDownloaderState_Acknowledging: return tr("Looking for %1...");
|
---|
77 | case UIDownloaderState_Downloading: return tr("Downloading %1...");
|
---|
78 | case UIDownloaderState_Verifying: return tr("Verifying %1...");
|
---|
79 | default: break;
|
---|
80 | }
|
---|
81 | /* Return null-string by default: */
|
---|
82 | return QString();
|
---|
83 | }
|
---|
84 |
|
---|
85 | void UIDownloader::processNetworkReplyProgress(qint64 iReceived, qint64 iTotal)
|
---|
86 | {
|
---|
87 | /* Notify listeners: */
|
---|
88 | emit sigProgressChange((double)iReceived / iTotal * 100);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void UIDownloader::processNetworkReplyFailed(const QString &strError)
|
---|
92 | {
|
---|
93 | /* Notify listeners: */
|
---|
94 | emit sigProgressFailed(strError);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void UIDownloader::processNetworkReplyCanceled(UINetworkReply *)
|
---|
98 | {
|
---|
99 | /* Notify listeners: */
|
---|
100 | emit sigProgressCanceled();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void UIDownloader::processNetworkReplyFinished(UINetworkReply *pNetworkReply)
|
---|
104 | {
|
---|
105 | /* Process reply: */
|
---|
106 | switch (m_state)
|
---|
107 | {
|
---|
108 | case UIDownloaderState_Acknowledging:
|
---|
109 | {
|
---|
110 | handleAcknowledgingResult(pNetworkReply);
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | case UIDownloaderState_Downloading:
|
---|
114 | {
|
---|
115 | handleDownloadingResult(pNetworkReply);
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | case UIDownloaderState_Verifying:
|
---|
119 | {
|
---|
120 | handleVerifyingResult(pNetworkReply);
|
---|
121 | break;
|
---|
122 | }
|
---|
123 | default:
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | void UIDownloader::handleAcknowledgingResult(UINetworkReply *pNetworkReply)
|
---|
129 | {
|
---|
130 | /* Get the final source: */
|
---|
131 | m_source = pNetworkReply->url();
|
---|
132 |
|
---|
133 | /* Ask for downloading: */
|
---|
134 | if (askForDownloadingConfirmation(pNetworkReply))
|
---|
135 | {
|
---|
136 | /* Start downloading: */
|
---|
137 | startDelayedDownloading();
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | /* Notify listeners: */
|
---|
142 | emit sigProgressFinished();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void UIDownloader::handleDownloadingResult(UINetworkReply *pNetworkReply)
|
---|
147 | {
|
---|
148 | /* Handle downloaded object: */
|
---|
149 | handleDownloadedObject(pNetworkReply);
|
---|
150 |
|
---|
151 | /* Check whether we should do verification: */
|
---|
152 | if (!m_strPathSHA256SumsFile.isEmpty())
|
---|
153 | {
|
---|
154 | /* Start verifying: */
|
---|
155 | startDelayedVerifying();
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | /* Notify listeners: */
|
---|
160 | emit sigProgressFinished();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void UIDownloader::handleVerifyingResult(UINetworkReply *pNetworkReply)
|
---|
165 | {
|
---|
166 | /* Handle verified object: */
|
---|
167 | handleVerifiedObject(pNetworkReply);
|
---|
168 |
|
---|
169 | /* Notify listeners: */
|
---|
170 | emit sigProgressFinished();
|
---|
171 | }
|
---|