1 | /* $Id: UINetworkRequestWidget.cpp 76606 2019-01-02 05:40:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkRequestWidget stuff implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes: */
|
---|
19 | #include <QGridLayout>
|
---|
20 | #include <QProgressBar>
|
---|
21 | #include <QStyle>
|
---|
22 | #include <QTimer>
|
---|
23 |
|
---|
24 | /* GUI includes: */
|
---|
25 | #include "QIRichTextLabel.h"
|
---|
26 | #include "QIToolButton.h"
|
---|
27 | #include "UIIconPool.h"
|
---|
28 | #include "UINetworkManager.h"
|
---|
29 | #include "UINetworkManagerDialog.h"
|
---|
30 | #include "UINetworkRequest.h"
|
---|
31 | #include "UINetworkRequestWidget.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | UINetworkRequestWidget::UINetworkRequestWidget(UINetworkManagerDialog *pParent, UINetworkRequest *pNetworkRequest)
|
---|
35 | : QIWithRetranslateUI<UIPopupBox>(pParent)
|
---|
36 | , m_pContentWidget(new QWidget(this))
|
---|
37 | , m_pMainLayout(new QGridLayout(m_pContentWidget))
|
---|
38 | , m_pProgressBar(new QProgressBar(m_pContentWidget))
|
---|
39 | , m_pRetryButton(new QIToolButton(m_pContentWidget))
|
---|
40 | , m_pCancelButton(new QIToolButton(m_pContentWidget))
|
---|
41 | , m_pErrorPane(new QIRichTextLabel(m_pContentWidget))
|
---|
42 | , m_pNetworkRequest(pNetworkRequest)
|
---|
43 | , m_pTimer(new QTimer(this))
|
---|
44 | {
|
---|
45 | /* Setup self: */
|
---|
46 | setTitleIcon(UIIconPool::iconSet(":/download_manager_16px.png"));
|
---|
47 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
---|
48 | setContentWidget(m_pContentWidget);
|
---|
49 | setOpen(true);
|
---|
50 |
|
---|
51 | /* Prepare listeners for m_pNetworkRequest: */
|
---|
52 | connect(m_pNetworkRequest, static_cast<void(UINetworkRequest::*)(qint64, qint64)>(&UINetworkRequest::sigProgress),
|
---|
53 | this, &UINetworkRequestWidget::sltSetProgress);
|
---|
54 | connect(m_pNetworkRequest, static_cast<void(UINetworkRequest::*)()>(&UINetworkRequest::sigStarted),
|
---|
55 | this, &UINetworkRequestWidget::sltSetProgressToStarted);
|
---|
56 | connect(m_pNetworkRequest, static_cast<void(UINetworkRequest::*)()>(&UINetworkRequest::sigFinished),
|
---|
57 | this, &UINetworkRequestWidget::sltSetProgressToFinished);
|
---|
58 | connect(m_pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QString&)>(&UINetworkRequest::sigFailed),
|
---|
59 | this, &UINetworkRequestWidget::sltSetProgressToFailed);
|
---|
60 |
|
---|
61 | /* Setup timer: */
|
---|
62 | m_pTimer->setInterval(5000);
|
---|
63 | connect(m_pTimer, &QTimer::timeout, this, &UINetworkRequestWidget::sltTimeIsOut);
|
---|
64 |
|
---|
65 | /* Setup main-layout: */
|
---|
66 | const int iL = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) / 2;
|
---|
67 | const int iT = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) / 2;
|
---|
68 | const int iR = qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) / 2;
|
---|
69 | const int iB = qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) / 2;
|
---|
70 | m_pMainLayout->setContentsMargins(iL, iT, iR, iB);
|
---|
71 |
|
---|
72 | /* Setup progress-bar: */
|
---|
73 | m_pProgressBar->setRange(0, 0);
|
---|
74 | m_pProgressBar->setMaximumHeight(16);
|
---|
75 |
|
---|
76 | /* Setup retry-button: */
|
---|
77 | m_pRetryButton->setHidden(true);
|
---|
78 | m_pRetryButton->removeBorder();
|
---|
79 | m_pRetryButton->setFocusPolicy(Qt::NoFocus);
|
---|
80 | m_pRetryButton->setIcon(UIIconPool::iconSet(":/refresh_16px.png"));
|
---|
81 | connect(m_pRetryButton, &QIToolButton::clicked, this, &UINetworkRequestWidget::sigRetry);
|
---|
82 |
|
---|
83 | /* Setup cancel-button: */
|
---|
84 | m_pCancelButton->removeBorder();
|
---|
85 | m_pCancelButton->setFocusPolicy(Qt::NoFocus);
|
---|
86 | m_pCancelButton->setIcon(UIIconPool::iconSet(":/cancel_16px.png"));
|
---|
87 | connect(m_pCancelButton, &QIToolButton::clicked, this, &UINetworkRequestWidget::sigCancel);
|
---|
88 |
|
---|
89 | /* Setup error-label: */
|
---|
90 | m_pErrorPane->setHidden(true);
|
---|
91 | m_pErrorPane->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
|
---|
92 | /* Calculate required width: */
|
---|
93 | int iMinimumWidth = pParent->minimumWidth();
|
---|
94 | int iLeft, iTop, iRight, iBottom;
|
---|
95 | /* Take into account content-widget layout margins: */
|
---|
96 | m_pMainLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
97 | iMinimumWidth -= iLeft;
|
---|
98 | iMinimumWidth -= iRight;
|
---|
99 | /* Take into account this layout margins: */
|
---|
100 | layout()->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
101 | iMinimumWidth -= iLeft;
|
---|
102 | iMinimumWidth -= iRight;
|
---|
103 | /* Take into account parent layout margins: */
|
---|
104 | QLayout *pParentLayout = qobject_cast<QMainWindow*>(parent())->centralWidget()->layout();
|
---|
105 | pParentLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
106 | iMinimumWidth -= iLeft;
|
---|
107 | iMinimumWidth -= iRight;
|
---|
108 | /* Set minimum text width: */
|
---|
109 | m_pErrorPane->setMinimumTextWidth(iMinimumWidth);
|
---|
110 |
|
---|
111 | /* Layout content: */
|
---|
112 | m_pMainLayout->addWidget(m_pProgressBar, 0, 0);
|
---|
113 | m_pMainLayout->addWidget(m_pRetryButton, 0, 1);
|
---|
114 | m_pMainLayout->addWidget(m_pCancelButton, 0, 2);
|
---|
115 | m_pMainLayout->addWidget(m_pErrorPane, 1, 0, 1, 3);
|
---|
116 |
|
---|
117 | /* Retranslate UI: */
|
---|
118 | retranslateUi();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void UINetworkRequestWidget::retranslateUi()
|
---|
122 | {
|
---|
123 | /* Get corresponding title: */
|
---|
124 | const QString &strTitle = m_pNetworkRequest->description();
|
---|
125 |
|
---|
126 | /* Set popup title (default if missed): */
|
---|
127 | setTitle(strTitle.isEmpty() ? UINetworkManagerDialog::tr("Network Operation") : strTitle);
|
---|
128 |
|
---|
129 | /* Translate retry button: */
|
---|
130 | m_pRetryButton->setStatusTip(UINetworkManagerDialog::tr("Restart network operation"));
|
---|
131 |
|
---|
132 | /* Translate cancel button: */
|
---|
133 | m_pCancelButton->setStatusTip(UINetworkManagerDialog::tr("Cancel network operation"));
|
---|
134 |
|
---|
135 | /* Translate error label: */
|
---|
136 | if (m_pNetworkRequest->reply())
|
---|
137 | m_pErrorPane->setText(composeErrorText(m_pNetworkRequest->reply()->errorString()));
|
---|
138 | }
|
---|
139 |
|
---|
140 | void UINetworkRequestWidget::sltSetProgress(qint64 iReceived, qint64 iTotal)
|
---|
141 | {
|
---|
142 | /* Restart timer: */
|
---|
143 | m_pTimer->start();
|
---|
144 |
|
---|
145 | /* Set current progress to passed: */
|
---|
146 | m_pProgressBar->setRange(0, iTotal);
|
---|
147 | m_pProgressBar->setValue(iReceived);
|
---|
148 | }
|
---|
149 |
|
---|
150 | void UINetworkRequestWidget::sltSetProgressToStarted()
|
---|
151 | {
|
---|
152 | /* Start timer: */
|
---|
153 | m_pTimer->start();
|
---|
154 |
|
---|
155 | /* Set current progress to 'started': */
|
---|
156 | m_pProgressBar->setRange(0, 1);
|
---|
157 | m_pProgressBar->setValue(0);
|
---|
158 |
|
---|
159 | /* Hide 'retry' button: */
|
---|
160 | m_pRetryButton->setHidden(true);
|
---|
161 |
|
---|
162 | /* Hide error label: */
|
---|
163 | m_pErrorPane->setHidden(true);
|
---|
164 | m_pErrorPane->setText(QString());
|
---|
165 | }
|
---|
166 |
|
---|
167 | void UINetworkRequestWidget::sltSetProgressToFinished()
|
---|
168 | {
|
---|
169 | /* Stop timer: */
|
---|
170 | m_pTimer->stop();
|
---|
171 |
|
---|
172 | /* Set current progress to 'started': */
|
---|
173 | m_pProgressBar->setRange(0, 1);
|
---|
174 | m_pProgressBar->setValue(1);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void UINetworkRequestWidget::sltSetProgressToFailed(const QString &strError)
|
---|
178 | {
|
---|
179 | /* Stop timer: */
|
---|
180 | m_pTimer->stop();
|
---|
181 |
|
---|
182 | /* Set current progress to 'failed': */
|
---|
183 | m_pProgressBar->setRange(0, 1);
|
---|
184 | m_pProgressBar->setValue(1);
|
---|
185 |
|
---|
186 | /* Show 'retry' button: */
|
---|
187 | m_pRetryButton->setHidden(false);
|
---|
188 |
|
---|
189 | /* Show error label: */
|
---|
190 | m_pErrorPane->setHidden(false);
|
---|
191 | m_pErrorPane->setText(composeErrorText(strError));
|
---|
192 | }
|
---|
193 |
|
---|
194 | void UINetworkRequestWidget::sltTimeIsOut()
|
---|
195 | {
|
---|
196 | /* Stop timer: */
|
---|
197 | m_pTimer->stop();
|
---|
198 |
|
---|
199 | /* Set current progress to unknown: */
|
---|
200 | m_pProgressBar->setRange(0, 0);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* static */
|
---|
204 | const QString UINetworkRequestWidget::composeErrorText(QString strErrorText)
|
---|
205 | {
|
---|
206 | /* Null-string for null-string: */
|
---|
207 | if (strErrorText.isEmpty())
|
---|
208 | return QString();
|
---|
209 |
|
---|
210 | /* Try to find all the links in the error-message,
|
---|
211 | * replace them with %increment if present: */
|
---|
212 | QRegExp linkRegExp("[\\S]+[\\./][\\S]+");
|
---|
213 | QStringList links;
|
---|
214 | for (int i = 1; linkRegExp.indexIn(strErrorText) != -1; ++i)
|
---|
215 | {
|
---|
216 | links << linkRegExp.cap();
|
---|
217 | strErrorText.replace(linkRegExp.cap(), QString("%%1").arg(i));
|
---|
218 | }
|
---|
219 | /* Return back all the links, just in bold: */
|
---|
220 | if (!links.isEmpty())
|
---|
221 | for (int i = 0; i < links.size(); ++i)
|
---|
222 | strErrorText = strErrorText.arg(QString("<b>%1</b>").arg(links[i]));
|
---|
223 |
|
---|
224 | /// @todo NLS: Embed <br> directly into error header text.
|
---|
225 | /* Prepend the error-message with <br> symbol: */
|
---|
226 | strErrorText.prepend("<br>");
|
---|
227 |
|
---|
228 | /* Return final result: */
|
---|
229 | return UINetworkManagerDialog::tr("The network operation failed with the following error: %1.").arg(strErrorText);
|
---|
230 | }
|
---|
231 |
|
---|