1 | /* $Id: UIDownloaderExtensionPack.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIDownloaderExtensionPack class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-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 | /* Qt includes: */
|
---|
29 | #include <QDir>
|
---|
30 | #include <QFile>
|
---|
31 | #include <QRegularExpression>
|
---|
32 | #include <QVariant>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "QIFileDialog.h"
|
---|
36 | #include "UIDownloaderExtensionPack.h"
|
---|
37 | #include "UIGlobalSession.h"
|
---|
38 | #include "UIMessageCenter.h"
|
---|
39 | #include "UIModalWindowManager.h"
|
---|
40 | #include "UINetworkReply.h"
|
---|
41 | #include "UINotificationCenter.h"
|
---|
42 | #include "UIVersion.h"
|
---|
43 |
|
---|
44 | /* Other VBox includes: */
|
---|
45 | #include <iprt/sha.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | UIDownloaderExtensionPack::UIDownloaderExtensionPack()
|
---|
49 | {
|
---|
50 | /* Get version number and adjust it for test and trunk builds. The server only has official releases. */
|
---|
51 | const QString strVersion = UIVersion(UIVersionInfo::vboxVersionStringNormalized()).effectiveReleasedVersion().toString();
|
---|
52 |
|
---|
53 | /* Prepare source/target: */
|
---|
54 | const QString strUnderscoredName = QString(GUI_ExtPackName).replace(' ', '_');
|
---|
55 | const QString strSourceName = QString("%1-%2.vbox-extpack").arg(strUnderscoredName, strVersion);
|
---|
56 | const QString strSourcePath = QString("https://download.virtualbox.org/virtualbox/%1/").arg(strVersion);
|
---|
57 | const QString strSource = strSourcePath + strSourceName;
|
---|
58 | const QString strPathSHA256SumsFile = QString("https://www.virtualbox.org/download/hashes/%1/SHA256SUMS").arg(strVersion);
|
---|
59 | const QString strTarget = QDir(gpGlobalSession->homeFolder()).absoluteFilePath(strSourceName);
|
---|
60 |
|
---|
61 | /* Set source/target: */
|
---|
62 | setSource(strSource);
|
---|
63 | setTarget(strTarget);
|
---|
64 | setPathSHA256SumsFile(strPathSHA256SumsFile);
|
---|
65 | }
|
---|
66 |
|
---|
67 | QString UIDownloaderExtensionPack::description() const
|
---|
68 | {
|
---|
69 | return UIDownloader::description().arg(tr("VirtualBox Extension Pack"));
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool UIDownloaderExtensionPack::askForDownloadingConfirmation(UINetworkReply *pReply)
|
---|
73 | {
|
---|
74 | return msgCenter().confirmDownloadExtensionPack(GUI_ExtPackName, source().toString(), pReply->header(UINetworkReply::ContentLengthHeader).toInt());
|
---|
75 | }
|
---|
76 |
|
---|
77 | void UIDownloaderExtensionPack::handleDownloadedObject(UINetworkReply *pReply)
|
---|
78 | {
|
---|
79 | m_receivedData = pReply->readAll();
|
---|
80 | }
|
---|
81 |
|
---|
82 | void UIDownloaderExtensionPack::handleVerifiedObject(UINetworkReply *pReply)
|
---|
83 | {
|
---|
84 | /* Try to verify the SHA-256 checksum: */
|
---|
85 | QString strCalculatedSumm;
|
---|
86 | bool fSuccess = false;
|
---|
87 | do
|
---|
88 | {
|
---|
89 | /* Read received data into the buffer: */
|
---|
90 | const QByteArray receivedData(pReply->readAll());
|
---|
91 | /* Make sure it's not empty: */
|
---|
92 | if (receivedData.isEmpty())
|
---|
93 | break;
|
---|
94 |
|
---|
95 | /* Parse buffer contents to dictionary: */
|
---|
96 | const QStringList dictionary(QString(receivedData).split("\n", Qt::SkipEmptyParts));
|
---|
97 | /* Make sure it's not empty: */
|
---|
98 | if (dictionary.isEmpty())
|
---|
99 | break;
|
---|
100 |
|
---|
101 | /* Parse each record to tags, look for the required one: */
|
---|
102 | foreach (const QString &strRecord, dictionary)
|
---|
103 | {
|
---|
104 | QRegularExpression separator(" \\*| ");
|
---|
105 | const QString strFileName = strRecord.section(separator, 1);
|
---|
106 | const QString strDownloadedSumm = strRecord.section(separator, 0, 0);
|
---|
107 | if (strFileName == source().fileName())
|
---|
108 | {
|
---|
109 | /* Calc the SHA-256 on the bytes, creating a string: */
|
---|
110 | uint8_t abHash[RTSHA256_HASH_SIZE];
|
---|
111 | RTSha256(m_receivedData.constData(), m_receivedData.length(), abHash);
|
---|
112 | char szDigest[RTSHA256_DIGEST_LEN + 1];
|
---|
113 | int rc = RTSha256ToString(abHash, szDigest, sizeof(szDigest));
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | {
|
---|
116 | AssertRC(rc);
|
---|
117 | szDigest[0] = '\0';
|
---|
118 | }
|
---|
119 | strCalculatedSumm = szDigest;
|
---|
120 | //printf("Downloaded SHA-256 summ: [%s]\n", strDownloadedSumm.toUtf8().constData());
|
---|
121 | //printf("Calculated SHA-256 summ: [%s]\n", strCalculatedSumm.toUtf8().constData());
|
---|
122 | /* Make sure checksum is valid: */
|
---|
123 | fSuccess = strDownloadedSumm == strCalculatedSumm;
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | while (false);
|
---|
129 |
|
---|
130 | /* If SHA-256 checksum verification failed: */
|
---|
131 | if (!fSuccess)
|
---|
132 | {
|
---|
133 | /* Warn the user about additions-image was downloaded and saved but checksum is invalid: */
|
---|
134 | UINotificationMessage::cannotValidateExtentionPackSHA256Sum(GUI_ExtPackName, source().toString(), QDir::toNativeSeparators(target()));
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* Serialize that buffer into the file: */
|
---|
139 | while (true)
|
---|
140 | {
|
---|
141 | /* Make sure the file already exists. If we reached
|
---|
142 | * this place, it's already written and checked. */
|
---|
143 | QFile file(target());
|
---|
144 | bool fSuccess = false;
|
---|
145 | /* Check step. Try to open file for reading first. */
|
---|
146 | if (file.open(QIODevice::ReadOnly))
|
---|
147 | fSuccess = true;
|
---|
148 | /* Failsafe step. Try to open file for writing otherwise. */
|
---|
149 | if (!fSuccess && file.open(QIODevice::WriteOnly))
|
---|
150 | {
|
---|
151 | /* Write buffer into the file: */
|
---|
152 | file.write(m_receivedData);
|
---|
153 | file.close();
|
---|
154 | fSuccess = true;
|
---|
155 | }
|
---|
156 | /* If the file already exists or was just written: */
|
---|
157 | if (fSuccess)
|
---|
158 | {
|
---|
159 | /* Warn the listener about extension-pack was downloaded: */
|
---|
160 | emit sigDownloadFinished(source().toString(), target(), strCalculatedSumm);
|
---|
161 | break;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Warn the user about extension-pack was downloaded but was NOT saved: */
|
---|
165 | msgCenter().cannotSaveExtensionPack(GUI_ExtPackName, source().toString(), QDir::toNativeSeparators(target()));
|
---|
166 |
|
---|
167 | /* Ask the user for another location for the extension-pack file: */
|
---|
168 | QString strTarget = QIFileDialog::getExistingDirectory(QFileInfo(target()).absolutePath(),
|
---|
169 | windowManager().mainWindowShown(),
|
---|
170 | tr("Select folder to save %1 to").arg(GUI_ExtPackName), true);
|
---|
171 |
|
---|
172 | /* Check if user had really set a new target: */
|
---|
173 | if (!strTarget.isNull())
|
---|
174 | setTarget(QDir(strTarget).absoluteFilePath(QFileInfo(target()).fileName()));
|
---|
175 | else
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | }
|
---|