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