1 | /* $Id: UINetworkManager.cpp 41235 2012-05-10 13:20:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * UINetworkManager stuff implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2011-2012 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Global includes: */
|
---|
21 | #include <QWidget>
|
---|
22 | #include <QTimer>
|
---|
23 | #include <QGridLayout>
|
---|
24 | #include <QProgressBar>
|
---|
25 | #include <QMainWindow>
|
---|
26 | #include <QVBoxLayout>
|
---|
27 | #include <QLabel>
|
---|
28 | #include <QPushButton>
|
---|
29 | #include <QStatusBar>
|
---|
30 | #include <QKeyEvent>
|
---|
31 | #include <QNetworkReply>
|
---|
32 |
|
---|
33 | /* Local includes: */
|
---|
34 | #include "UINetworkManager.h"
|
---|
35 | #include "UINetworkCustomer.h"
|
---|
36 | #include "QIWithRetranslateUI.h"
|
---|
37 | #include "VBoxGlobal.h"
|
---|
38 | #include "UIMessageCenter.h"
|
---|
39 | #include "UIIconPool.h"
|
---|
40 | #include "QIDialogButtonBox.h"
|
---|
41 | #include "UIPopupBox.h"
|
---|
42 | #include "QIToolButton.h"
|
---|
43 | #include "QIRichTextLabel.h"
|
---|
44 |
|
---|
45 | /* Network-request widget: */
|
---|
46 | class UINetworkRequestWidget : public QIWithRetranslateUI<UIPopupBox>
|
---|
47 | {
|
---|
48 | Q_OBJECT;
|
---|
49 |
|
---|
50 | signals:
|
---|
51 |
|
---|
52 | /* Signal to retry network-request: */
|
---|
53 | void sigRetry();
|
---|
54 | /* Signal to cancel network-request: */
|
---|
55 | void sigCancel();
|
---|
56 |
|
---|
57 | public:
|
---|
58 |
|
---|
59 | /* Constructor: */
|
---|
60 | UINetworkRequestWidget(QMainWindow *pParent, UINetworkRequest *pNetworkRequest)
|
---|
61 | : QIWithRetranslateUI<UIPopupBox>(pParent)
|
---|
62 | , m_pNetworkRequest(pNetworkRequest)
|
---|
63 | , m_pTimer(new QTimer(this))
|
---|
64 | , m_pContentWidget(new QWidget(this))
|
---|
65 | , m_pMainLayout(new QGridLayout(m_pContentWidget))
|
---|
66 | , m_pProgressBar(new QProgressBar(m_pContentWidget))
|
---|
67 | , m_pRetryButton(new QIToolButton(m_pContentWidget))
|
---|
68 | , m_pCancelButton(new QIToolButton(m_pContentWidget))
|
---|
69 | , m_pErrorPane(new QIRichTextLabel(m_pContentWidget))
|
---|
70 | {
|
---|
71 | /* Setup self: */
|
---|
72 | setTitleIcon(UIIconPool::iconSet(":/nw_16px.png"));
|
---|
73 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
---|
74 | setContentWidget(m_pContentWidget);
|
---|
75 | setOpen(true);
|
---|
76 |
|
---|
77 | /* Prepare listeners for m_pNetworkRequest: */
|
---|
78 | connect(m_pNetworkRequest, SIGNAL(sigProgress(qint64, qint64)), this, SLOT(sltSetProgress(qint64, qint64)));
|
---|
79 | connect(m_pNetworkRequest, SIGNAL(sigStarted()), this, SLOT(sltSetProgressToStarted()));
|
---|
80 | connect(m_pNetworkRequest, SIGNAL(sigFinished()), this, SLOT(sltSetProgressToFinished()));
|
---|
81 | connect(m_pNetworkRequest, SIGNAL(sigFailed(const QString&)), this, SLOT(sltSetProgressToFailed(const QString&)));
|
---|
82 |
|
---|
83 | /* Setup timer: */
|
---|
84 | m_pTimer->setInterval(5000);
|
---|
85 | connect(m_pTimer, SIGNAL(timeout()), this, SLOT(sltTimeIsOut()));
|
---|
86 |
|
---|
87 | /* Setup main-layout: */
|
---|
88 | m_pMainLayout->setContentsMargins(6, 6, 6, 6);
|
---|
89 |
|
---|
90 | /* Setup progress-bar: */
|
---|
91 | m_pProgressBar->setRange(0, 0);
|
---|
92 | m_pProgressBar->setMaximumHeight(16);
|
---|
93 |
|
---|
94 | /* Setup retry-button: */
|
---|
95 | m_pRetryButton->setHidden(true);
|
---|
96 | m_pRetryButton->removeBorder();
|
---|
97 | m_pRetryButton->setFocusPolicy(Qt::NoFocus);
|
---|
98 | m_pRetryButton->setIcon(UIIconPool::iconSet(":/refresh_16px.png"));
|
---|
99 | connect(m_pRetryButton, SIGNAL(clicked(bool)), this, SIGNAL(sigRetry()));
|
---|
100 |
|
---|
101 | /* Setup cancel-button: */
|
---|
102 | m_pCancelButton->removeBorder();
|
---|
103 | m_pCancelButton->setFocusPolicy(Qt::NoFocus);
|
---|
104 | m_pCancelButton->setIcon(UIIconPool::iconSet(":/delete_16px.png"));
|
---|
105 | connect(m_pCancelButton, SIGNAL(clicked(bool)), this, SIGNAL(sigCancel()));
|
---|
106 |
|
---|
107 | /* Setup error-label: */
|
---|
108 | m_pErrorPane->setHidden(true);
|
---|
109 | m_pErrorPane->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
|
---|
110 | /* Calculate required width: */
|
---|
111 | int iMinimumWidth = pParent->minimumWidth();
|
---|
112 | int iLeft, iTop, iRight, iBottom;
|
---|
113 | /* Take into account content-widget layout margins: */
|
---|
114 | m_pMainLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
115 | iMinimumWidth -= iLeft;
|
---|
116 | iMinimumWidth -= iRight;
|
---|
117 | /* Take into account this layout margins: */
|
---|
118 | layout()->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
119 | iMinimumWidth -= iLeft;
|
---|
120 | iMinimumWidth -= iRight;
|
---|
121 | /* Take into account parent layout margins: */
|
---|
122 | QLayout *pParentLayout = qobject_cast<QMainWindow*>(parent())->centralWidget()->layout();
|
---|
123 | pParentLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom);
|
---|
124 | iMinimumWidth -= iLeft;
|
---|
125 | iMinimumWidth -= iRight;
|
---|
126 | /* Set minimum text width: */
|
---|
127 | m_pErrorPane->setMinimumTextWidth(iMinimumWidth);
|
---|
128 |
|
---|
129 | /* Layout content: */
|
---|
130 | m_pMainLayout->addWidget(m_pProgressBar, 0, 0);
|
---|
131 | m_pMainLayout->addWidget(m_pRetryButton, 0, 1);
|
---|
132 | m_pMainLayout->addWidget(m_pCancelButton, 0, 2);
|
---|
133 | m_pMainLayout->addWidget(m_pErrorPane, 1, 0, 1, 3);
|
---|
134 |
|
---|
135 | /* Retranslate UI: */
|
---|
136 | retranslateUi();
|
---|
137 | }
|
---|
138 |
|
---|
139 | private slots:
|
---|
140 |
|
---|
141 | /* Retranslate UI: */
|
---|
142 | void retranslateUi()
|
---|
143 | {
|
---|
144 | /* Get corresponding title: */
|
---|
145 | const QString &strTitle = m_pNetworkRequest->description();
|
---|
146 |
|
---|
147 | /* Set popup title (default if missed): */
|
---|
148 | setTitle(strTitle.isEmpty() ? UINetworkManager::tr("Network Operation") : strTitle);
|
---|
149 |
|
---|
150 | /* Translate retry button: */
|
---|
151 | m_pRetryButton->setStatusTip(UINetworkManager::tr("Restart network operation"));
|
---|
152 |
|
---|
153 | /* Translate cancel button: */
|
---|
154 | m_pCancelButton->setStatusTip(UINetworkManager::tr("Cancel network operation"));
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* Updates current network-request progess: */
|
---|
158 | void sltSetProgress(qint64 iReceived, qint64 iTotal)
|
---|
159 | {
|
---|
160 | /* Restart timer: */
|
---|
161 | m_pTimer->start();
|
---|
162 |
|
---|
163 | /* Set current progress to passed: */
|
---|
164 | m_pProgressBar->setRange(0, iTotal);
|
---|
165 | m_pProgressBar->setValue(iReceived);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* Set current network-request progress to 'started': */
|
---|
169 | void sltSetProgressToStarted()
|
---|
170 | {
|
---|
171 | /* Start timer: */
|
---|
172 | m_pTimer->start();
|
---|
173 |
|
---|
174 | /* Set current progress to 'started': */
|
---|
175 | m_pProgressBar->setRange(0, 1);
|
---|
176 | m_pProgressBar->setValue(0);
|
---|
177 |
|
---|
178 | /* Hide 'retry' button: */
|
---|
179 | m_pRetryButton->setHidden(true);
|
---|
180 |
|
---|
181 | /* Hide error label: */
|
---|
182 | m_pErrorPane->setHidden(true);
|
---|
183 | m_pErrorPane->setText(QString());
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Set current network-request progress to 'finished': */
|
---|
187 | void sltSetProgressToFinished()
|
---|
188 | {
|
---|
189 | /* Stop timer: */
|
---|
190 | m_pTimer->stop();
|
---|
191 |
|
---|
192 | /* Set current progress to 'started': */
|
---|
193 | m_pProgressBar->setRange(0, 1);
|
---|
194 | m_pProgressBar->setValue(1);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* Set current network-request progress to 'failed': */
|
---|
198 | void sltSetProgressToFailed(const QString &strError)
|
---|
199 | {
|
---|
200 | /* Stop timer: */
|
---|
201 | m_pTimer->stop();
|
---|
202 |
|
---|
203 | /* Set current progress to 'failed': */
|
---|
204 | m_pProgressBar->setRange(0, 1);
|
---|
205 | m_pProgressBar->setValue(1);
|
---|
206 |
|
---|
207 | /* Show 'retry' button: */
|
---|
208 | m_pRetryButton->setHidden(false);
|
---|
209 |
|
---|
210 | /* Try to find all the links in the error-message,
|
---|
211 | * replace them with %increment if present: */
|
---|
212 | QString strErrorText(strError);
|
---|
213 | QRegExp linkRegExp("[\\S]+[\\./][\\S]+");
|
---|
214 | QStringList links;
|
---|
215 | for (int i = 1; linkRegExp.indexIn(strErrorText) != -1; ++i)
|
---|
216 | {
|
---|
217 | links << linkRegExp.cap();
|
---|
218 | strErrorText.replace(linkRegExp.cap(), QString("%%1").arg(i));
|
---|
219 | }
|
---|
220 | /* Return back all the links, just in bold: */
|
---|
221 | if (!links.isEmpty())
|
---|
222 | for (int i = 0; i < links.size(); ++i)
|
---|
223 | strErrorText = strErrorText.arg(QString("<b>%1</b>").arg(links[i]));
|
---|
224 |
|
---|
225 | /* Show error label: */
|
---|
226 | m_pErrorPane->setHidden(false);
|
---|
227 | m_pErrorPane->setText(UINetworkManager::tr("Error: %1.").arg(strErrorText));
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Handle frozen progress: */
|
---|
231 | void sltTimeIsOut()
|
---|
232 | {
|
---|
233 | /* Stop timer: */
|
---|
234 | m_pTimer->stop();
|
---|
235 |
|
---|
236 | /* Set current progress to unknown: */
|
---|
237 | m_pProgressBar->setRange(0, 0);
|
---|
238 | }
|
---|
239 |
|
---|
240 | private:
|
---|
241 |
|
---|
242 | /* Objects: */
|
---|
243 | UINetworkRequest *m_pNetworkRequest;
|
---|
244 | QTimer *m_pTimer;
|
---|
245 |
|
---|
246 | /* Widgets: */
|
---|
247 | QWidget *m_pContentWidget;
|
---|
248 | QGridLayout *m_pMainLayout;
|
---|
249 | QProgressBar *m_pProgressBar;
|
---|
250 | QIToolButton *m_pRetryButton;
|
---|
251 | QIToolButton *m_pCancelButton;
|
---|
252 | QIRichTextLabel *m_pErrorPane;
|
---|
253 | };
|
---|
254 |
|
---|
255 | /* Network requests dialog: */
|
---|
256 | class UINetworkManagerDialog : public QIWithRetranslateUI<QMainWindow>
|
---|
257 | {
|
---|
258 | Q_OBJECT;
|
---|
259 |
|
---|
260 | signals:
|
---|
261 |
|
---|
262 | /* Signal to cancel all network-requests: */
|
---|
263 | void sigCancelNetworkRequests();
|
---|
264 |
|
---|
265 | public slots:
|
---|
266 |
|
---|
267 | /* Show the dialog, make sure its visible: */
|
---|
268 | void showNormal()
|
---|
269 | {
|
---|
270 | /* Show (restore if neessary): */
|
---|
271 | QMainWindow::showNormal();
|
---|
272 |
|
---|
273 | /* Raise above the others: */
|
---|
274 | raise();
|
---|
275 |
|
---|
276 | /* Activate: */
|
---|
277 | activateWindow();
|
---|
278 | }
|
---|
279 |
|
---|
280 | public:
|
---|
281 |
|
---|
282 | /* Constructor: */
|
---|
283 | UINetworkManagerDialog()
|
---|
284 | {
|
---|
285 | /* Do not count that window as important for application,
|
---|
286 | * it will NOT be taken into account when other top-level windows will be closed: */
|
---|
287 | setAttribute(Qt::WA_QuitOnClose, false);
|
---|
288 |
|
---|
289 | /* Set minimum width: */
|
---|
290 | setMinimumWidth(500);
|
---|
291 |
|
---|
292 | /* Prepare central-widget: */
|
---|
293 | setCentralWidget(new QWidget);
|
---|
294 |
|
---|
295 | /* Create main-layout: */
|
---|
296 | m_pMainLayout = new QVBoxLayout(centralWidget());
|
---|
297 | m_pMainLayout->setContentsMargins(6, 6, 6, 6);
|
---|
298 |
|
---|
299 | /* Create description-label: */
|
---|
300 | m_pLabel = new QLabel(centralWidget());
|
---|
301 | m_pLabel->setAlignment(Qt::AlignCenter);
|
---|
302 | m_pLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
|
---|
303 |
|
---|
304 | /* Create button-box: */
|
---|
305 | m_pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, centralWidget());
|
---|
306 | connect(m_pButtonBox, SIGNAL(rejected()), this, SLOT(sltHandleCancelAllButtonPress()));
|
---|
307 | m_pButtonBox->setHidden(true);
|
---|
308 |
|
---|
309 | /* Layout content: */
|
---|
310 | m_pMainLayout->addWidget(m_pLabel);
|
---|
311 | m_pMainLayout->addStretch();
|
---|
312 | m_pMainLayout->addWidget(m_pButtonBox);
|
---|
313 |
|
---|
314 | /* Create status-bar: */
|
---|
315 | setStatusBar(new QStatusBar);
|
---|
316 |
|
---|
317 | /* Translate dialog: */
|
---|
318 | retranslateUi();
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* Add network-request widget: */
|
---|
322 | UINetworkRequestWidget* addNetworkRequestWidget(UINetworkRequest *pNetworkRequest)
|
---|
323 | {
|
---|
324 | /* Make sure network-request is really exists: */
|
---|
325 | AssertMsg(pNetworkRequest, ("Network-request doesn't exists!\n"));
|
---|
326 |
|
---|
327 | /* Create new network-request widget: */
|
---|
328 | UINetworkRequestWidget *pNetworkRequestWidget = new UINetworkRequestWidget(this, pNetworkRequest);
|
---|
329 | m_pMainLayout->insertWidget(m_pMainLayout->count() - 2 /* before button-box and stretch */, pNetworkRequestWidget);
|
---|
330 | m_widgets.insert(pNetworkRequest->uuid(), pNetworkRequestWidget);
|
---|
331 |
|
---|
332 | /* Hide label: */
|
---|
333 | m_pLabel->setHidden(true);
|
---|
334 | /* Show button-box: */
|
---|
335 | m_pButtonBox->setHidden(false);
|
---|
336 | /* If customer made a force-call: */
|
---|
337 | if (pNetworkRequest->customer()->isItForceCall())
|
---|
338 | {
|
---|
339 | /* Show dialog: */
|
---|
340 | showNormal();
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* Return network-request widget: */
|
---|
344 | return pNetworkRequestWidget;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* Remove network-request widget: */
|
---|
348 | void removeNetworkRequestWidget(const QUuid &uuid)
|
---|
349 | {
|
---|
350 | /* Make sure network-request widget still present: */
|
---|
351 | AssertMsg(m_widgets.contains(uuid), ("Network-request widget already removed!\n"));
|
---|
352 |
|
---|
353 | /* Delete corresponding network-request widget: */
|
---|
354 | delete m_widgets.value(uuid);
|
---|
355 | m_widgets.remove(uuid);
|
---|
356 |
|
---|
357 | /* Check if dialog is empty: */
|
---|
358 | if (m_widgets.isEmpty())
|
---|
359 | {
|
---|
360 | /* Show label: */
|
---|
361 | m_pLabel->setHidden(false);
|
---|
362 | /* Hide button-box: */
|
---|
363 | m_pButtonBox->setHidden(true);
|
---|
364 | /* Let central-widget update its layout before being hidden: */
|
---|
365 | QCoreApplication::sendPostedEvents(centralWidget(), QEvent::LayoutRequest);
|
---|
366 | /* Hide dialog: */
|
---|
367 | hide();
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | private slots:
|
---|
372 |
|
---|
373 | /* Handler for 'Cancel All' button press: */
|
---|
374 | void sltHandleCancelAllButtonPress()
|
---|
375 | {
|
---|
376 | /* Ask if user wants to cancel all current network-requests: */
|
---|
377 | if (msgCenter().askAboutCancelAllNetworkRequest(this))
|
---|
378 | emit sigCancelNetworkRequests();
|
---|
379 | }
|
---|
380 |
|
---|
381 | private:
|
---|
382 |
|
---|
383 | /* Translate whole dialog: */
|
---|
384 | void retranslateUi()
|
---|
385 | {
|
---|
386 | /* Set window caption: */
|
---|
387 | setWindowTitle(UINetworkManager::tr("Network Operations Manager"));
|
---|
388 |
|
---|
389 | /* Set description-label text: */
|
---|
390 | m_pLabel->setText(UINetworkManager::tr("There are no active network operations."));
|
---|
391 |
|
---|
392 | /* Set buttons-box text: */
|
---|
393 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setText(UINetworkManager::tr("&Cancel All"));
|
---|
394 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setStatusTip(UINetworkManager::tr("Cancel all active network operations"));
|
---|
395 | }
|
---|
396 |
|
---|
397 | /* Overloaded show-event: */
|
---|
398 | void showEvent(QShowEvent *pShowEvent)
|
---|
399 | {
|
---|
400 | /* Resize to minimum size-hint: */
|
---|
401 | resize(minimumSizeHint());
|
---|
402 |
|
---|
403 | /* Center according current main application window: */
|
---|
404 | vboxGlobal().centerWidget(this, vboxGlobal().mainWindow(), false);
|
---|
405 |
|
---|
406 | /* Pass event to the base-class: */
|
---|
407 | QMainWindow::showEvent(pShowEvent);
|
---|
408 | }
|
---|
409 |
|
---|
410 | /* Overloaded keypress-event: */
|
---|
411 | void keyPressEvent(QKeyEvent *pKeyPressEvent)
|
---|
412 | {
|
---|
413 | /* 'Escape' key used to close the dialog: */
|
---|
414 | if (pKeyPressEvent->key() == Qt::Key_Escape)
|
---|
415 | {
|
---|
416 | close();
|
---|
417 | return;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /* Pass event to the base-class: */
|
---|
421 | QMainWindow::keyPressEvent(pKeyPressEvent);
|
---|
422 | }
|
---|
423 |
|
---|
424 | /* Main layout: */
|
---|
425 | QVBoxLayout *m_pMainLayout;
|
---|
426 | QLabel *m_pLabel;
|
---|
427 | QIDialogButtonBox *m_pButtonBox;
|
---|
428 |
|
---|
429 | /* Popup-widget map: */
|
---|
430 | QMap<QUuid, UINetworkRequestWidget*> m_widgets;
|
---|
431 | };
|
---|
432 |
|
---|
433 | /* Constructor: */
|
---|
434 | UINetworkRequest::UINetworkRequest(UINetworkManager *pNetworkManager,
|
---|
435 | UINetworkManagerDialog *pNetworkManagerDialog,
|
---|
436 | const QNetworkRequest &request, UINetworkRequestType type,
|
---|
437 | const QString &strDescription, UINetworkCustomer *pCustomer)
|
---|
438 | : QObject(pNetworkManager)
|
---|
439 | , m_pNetworkManagerDialog(pNetworkManagerDialog)
|
---|
440 | , m_pNetworkRequestWidget(0)
|
---|
441 | , m_uuid(QUuid::createUuid())
|
---|
442 | , m_requests(QList<QNetworkRequest>() << request)
|
---|
443 | , m_iCurrentRequestIndex(0)
|
---|
444 | , m_type(type)
|
---|
445 | , m_strDescription(strDescription)
|
---|
446 | , m_pCustomer(pCustomer)
|
---|
447 | , m_fRunning(false)
|
---|
448 | {
|
---|
449 | /* Initialize: */
|
---|
450 | initialize();
|
---|
451 | }
|
---|
452 |
|
---|
453 | UINetworkRequest::UINetworkRequest(UINetworkManager *pNetworkManager,
|
---|
454 | UINetworkManagerDialog *pNetworkManagerDialog,
|
---|
455 | const QList<QNetworkRequest> &requests, UINetworkRequestType type,
|
---|
456 | const QString &strDescription, UINetworkCustomer *pCustomer)
|
---|
457 | : QObject(pNetworkManager)
|
---|
458 | , m_pNetworkManagerDialog(pNetworkManagerDialog)
|
---|
459 | , m_pNetworkRequestWidget(0)
|
---|
460 | , m_uuid(QUuid::createUuid())
|
---|
461 | , m_requests(requests)
|
---|
462 | , m_iCurrentRequestIndex(0)
|
---|
463 | , m_type(type)
|
---|
464 | , m_strDescription(strDescription)
|
---|
465 | , m_pCustomer(pCustomer)
|
---|
466 | , m_fRunning(false)
|
---|
467 | {
|
---|
468 | /* Initialize: */
|
---|
469 | initialize();
|
---|
470 | }
|
---|
471 |
|
---|
472 | /* Destructor: */
|
---|
473 | UINetworkRequest::~UINetworkRequest()
|
---|
474 | {
|
---|
475 | /* Destroy network-reply: */
|
---|
476 | cleanupNetworkReply();
|
---|
477 |
|
---|
478 | /* Destroy network-request widget: */
|
---|
479 | m_pNetworkManagerDialog->removeNetworkRequestWidget(m_uuid);
|
---|
480 | }
|
---|
481 |
|
---|
482 | /* Network-reply progress handler: */
|
---|
483 | void UINetworkRequest::sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal)
|
---|
484 | {
|
---|
485 | /* Notify UINetworkManager: */
|
---|
486 | emit sigProgress(m_uuid, iReceived, iTotal);
|
---|
487 | /* Notify UINetworkRequestWidget: */
|
---|
488 | emit sigProgress(iReceived, iTotal);
|
---|
489 | }
|
---|
490 |
|
---|
491 | /* Network-reply finish handler: */
|
---|
492 | void UINetworkRequest::sltHandleNetworkReplyFinish()
|
---|
493 | {
|
---|
494 | /* Set as non-running: */
|
---|
495 | m_fRunning = false;
|
---|
496 |
|
---|
497 | /* Get sender network reply: */
|
---|
498 | QNetworkReply *pNetworkReply = static_cast<QNetworkReply*>(sender());
|
---|
499 |
|
---|
500 | /* If network-request was canceled: */
|
---|
501 | if (pNetworkReply->error() == QNetworkReply::OperationCanceledError)
|
---|
502 | {
|
---|
503 | /* Notify UINetworkManager: */
|
---|
504 | emit sigCanceled(m_uuid);
|
---|
505 | }
|
---|
506 | /* If network-reply has no errors: */
|
---|
507 | else if (pNetworkReply->error() == QNetworkReply::NoError)
|
---|
508 | {
|
---|
509 | /* Check if redirection required: */
|
---|
510 | QUrl redirect = pNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
---|
511 | if (redirect.isValid())
|
---|
512 | {
|
---|
513 | /* Cleanup current network-reply first: */
|
---|
514 | cleanupNetworkReply();
|
---|
515 |
|
---|
516 | /* Choose redirect-source as current: */
|
---|
517 | m_request.setUrl(redirect);
|
---|
518 |
|
---|
519 | /* Create new network-reply finally: */
|
---|
520 | prepareNetworkReply();
|
---|
521 | }
|
---|
522 | else
|
---|
523 | {
|
---|
524 | /* Notify UINetworkRequestWidget: */
|
---|
525 | emit sigFinished();
|
---|
526 | /* Notify UINetworkManager: */
|
---|
527 | emit sigFinished(m_uuid);
|
---|
528 | }
|
---|
529 | }
|
---|
530 | /* If some error occured: */
|
---|
531 | else
|
---|
532 | {
|
---|
533 | /* Check if we have other requests in set: */
|
---|
534 | if (m_iCurrentRequestIndex < m_requests.size() - 1)
|
---|
535 | {
|
---|
536 | /* Cleanup current network-reply first: */
|
---|
537 | cleanupNetworkReply();
|
---|
538 |
|
---|
539 | /* Choose next network-request as current: */
|
---|
540 | ++m_iCurrentRequestIndex;
|
---|
541 | m_request = m_requests[m_iCurrentRequestIndex];
|
---|
542 |
|
---|
543 | /* Create new network-reply finally: */
|
---|
544 | prepareNetworkReply();
|
---|
545 | }
|
---|
546 | else
|
---|
547 | {
|
---|
548 | /* Notify UINetworkRequestWidget: */
|
---|
549 | emit sigFailed(pNetworkReply->errorString());
|
---|
550 | /* Notify UINetworkManager: */
|
---|
551 | emit sigFailed(m_uuid, pNetworkReply->errorString());
|
---|
552 | }
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | /* Slot to retry network-request: */
|
---|
557 | void UINetworkRequest::sltRetry()
|
---|
558 | {
|
---|
559 | /* Cleanup current network-reply first: */
|
---|
560 | cleanupNetworkReply();
|
---|
561 |
|
---|
562 | /* Choose first network-request as current: */
|
---|
563 | m_iCurrentRequestIndex = 0;
|
---|
564 | m_request = m_requests[m_iCurrentRequestIndex];
|
---|
565 |
|
---|
566 | /* Create new network-reply finally: */
|
---|
567 | prepareNetworkReply();
|
---|
568 | }
|
---|
569 |
|
---|
570 | /* Slot to cancel network-request: */
|
---|
571 | void UINetworkRequest::sltCancel()
|
---|
572 | {
|
---|
573 | /* Abort network-reply if present: */
|
---|
574 | abortNetworkReply();
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* Initialize: */
|
---|
578 | void UINetworkRequest::initialize()
|
---|
579 | {
|
---|
580 | /* Prepare listeners for parent(): */
|
---|
581 | connect(parent(), SIGNAL(sigCancelNetworkRequests()), this, SLOT(sltCancel()));
|
---|
582 |
|
---|
583 | /* Create network-request widget: */
|
---|
584 | m_pNetworkRequestWidget = m_pNetworkManagerDialog->addNetworkRequestWidget(this);
|
---|
585 |
|
---|
586 | /* Prepare listeners for m_pNetworkRequestWidget: */
|
---|
587 | connect(m_pNetworkRequestWidget, SIGNAL(sigRetry()), this, SLOT(sltRetry()));
|
---|
588 | connect(m_pNetworkRequestWidget, SIGNAL(sigCancel()), this, SLOT(sltCancel()));
|
---|
589 |
|
---|
590 | /* Choose first network-request as current: */
|
---|
591 | m_iCurrentRequestIndex = 0;
|
---|
592 | m_request = m_requests[m_iCurrentRequestIndex];
|
---|
593 |
|
---|
594 | /* Create network-reply: */
|
---|
595 | prepareNetworkReply();
|
---|
596 | }
|
---|
597 |
|
---|
598 | /* Prepare network-reply: */
|
---|
599 | void UINetworkRequest::prepareNetworkReply()
|
---|
600 | {
|
---|
601 | /* Make network-request: */
|
---|
602 | switch (m_type)
|
---|
603 | {
|
---|
604 | case UINetworkRequestType_HEAD:
|
---|
605 | {
|
---|
606 | m_pReply = gNetworkManager->head(m_request);
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | case UINetworkRequestType_GET:
|
---|
610 | {
|
---|
611 | m_pReply = gNetworkManager->get(m_request);
|
---|
612 | break;
|
---|
613 | }
|
---|
614 | default:
|
---|
615 | break;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* Prepare listeners for m_pReply: */
|
---|
619 | AssertMsg(m_pReply, ("Unable to make network-request!\n"));
|
---|
620 | connect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(sltHandleNetworkReplyProgress(qint64, qint64)));
|
---|
621 | connect(m_pReply, SIGNAL(finished()), this, SLOT(sltHandleNetworkReplyFinish()));
|
---|
622 |
|
---|
623 | /* Set as running: */
|
---|
624 | m_fRunning = true;
|
---|
625 |
|
---|
626 | /* Notify UINetworkRequestWidget: */
|
---|
627 | emit sigStarted();
|
---|
628 | }
|
---|
629 |
|
---|
630 | /* Cleanup network-reply: */
|
---|
631 | void UINetworkRequest::cleanupNetworkReply()
|
---|
632 | {
|
---|
633 | /* Destroy current reply: */
|
---|
634 | AssertMsg(m_pReply, ("Network-reply already destroyed!\n"));
|
---|
635 | m_pReply->disconnect();
|
---|
636 | m_pReply->deleteLater();
|
---|
637 | m_pReply = 0;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /* Abort network-reply: */
|
---|
641 | void UINetworkRequest::abortNetworkReply()
|
---|
642 | {
|
---|
643 | /* Abort network-reply if present: */
|
---|
644 | if (m_pReply)
|
---|
645 | {
|
---|
646 | if (m_fRunning)
|
---|
647 | m_pReply->abort();
|
---|
648 | else
|
---|
649 | emit sigCanceled(m_uuid);
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | /* Instance: */
|
---|
654 | UINetworkManager* UINetworkManager::m_pInstance = 0;
|
---|
655 |
|
---|
656 | /* Create singleton: */
|
---|
657 | void UINetworkManager::create()
|
---|
658 | {
|
---|
659 | /* Check that instance do NOT exist: */
|
---|
660 | if (m_pInstance)
|
---|
661 | return;
|
---|
662 |
|
---|
663 | /* Create instance: */
|
---|
664 | new UINetworkManager;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /* Destroy singleton: */
|
---|
668 | void UINetworkManager::destroy()
|
---|
669 | {
|
---|
670 | /* Check that instance exists: */
|
---|
671 | if (!m_pInstance)
|
---|
672 | return;
|
---|
673 |
|
---|
674 | /* Destroy instance: */
|
---|
675 | delete m_pInstance;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* Network Access Manager GUI window: */
|
---|
679 | QWidget* UINetworkManager::window() const
|
---|
680 | {
|
---|
681 | return m_pNetworkProgressDialog;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* Show Network Access Manager GUI: */
|
---|
685 | void UINetworkManager::show()
|
---|
686 | {
|
---|
687 | /* Just show the dialog: */
|
---|
688 | m_pNetworkProgressDialog->showNormal();
|
---|
689 | }
|
---|
690 |
|
---|
691 | /* Network-request creation wrapper for UINetworkCustomer: */
|
---|
692 | void UINetworkManager::createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type,
|
---|
693 | const QString &strDescription, UINetworkCustomer *pCustomer)
|
---|
694 | {
|
---|
695 | /* Create new network-request: */
|
---|
696 | UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkProgressDialog,
|
---|
697 | request, type, strDescription, pCustomer);
|
---|
698 | /* Prepare created network-request: */
|
---|
699 | prepareNetworkRequest(pNetworkRequest);
|
---|
700 | }
|
---|
701 |
|
---|
702 | /* Network request (set) creation wrapper for UINetworkCustomer: */
|
---|
703 | void UINetworkManager::createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type,
|
---|
704 | const QString &strDescription, UINetworkCustomer *pCustomer)
|
---|
705 | {
|
---|
706 | /* Create new network-request: */
|
---|
707 | UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkProgressDialog,
|
---|
708 | requests, type, strDescription, pCustomer);
|
---|
709 | /* Prepare created network-request: */
|
---|
710 | prepareNetworkRequest(pNetworkRequest);
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* Constructor: */
|
---|
714 | UINetworkManager::UINetworkManager()
|
---|
715 | {
|
---|
716 | /* Prepare instance: */
|
---|
717 | m_pInstance = this;
|
---|
718 |
|
---|
719 | /* Prepare finally: */
|
---|
720 | prepare();
|
---|
721 | }
|
---|
722 |
|
---|
723 | /* Destructor: */
|
---|
724 | UINetworkManager::~UINetworkManager()
|
---|
725 | {
|
---|
726 | /* Cleanup first: */
|
---|
727 | cleanup();
|
---|
728 |
|
---|
729 | /* Cleanup instance: */
|
---|
730 | m_pInstance = 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Prepare: */
|
---|
734 | void UINetworkManager::prepare()
|
---|
735 | {
|
---|
736 | /* Prepare network manager GUI: */
|
---|
737 | m_pNetworkProgressDialog = new UINetworkManagerDialog;
|
---|
738 |
|
---|
739 | /* Prepare listeners for m_pNetworkProgressDialog: */
|
---|
740 | connect(m_pNetworkProgressDialog, SIGNAL(sigCancelNetworkRequests()), this, SIGNAL(sigCancelNetworkRequests()));
|
---|
741 | }
|
---|
742 |
|
---|
743 | /* Cleanup: */
|
---|
744 | void UINetworkManager::cleanup()
|
---|
745 | {
|
---|
746 | /* Cleanup network-requests first: */
|
---|
747 | cleanupNetworkRequests();
|
---|
748 |
|
---|
749 | /* Cleanup network manager GUI: */
|
---|
750 | delete m_pNetworkProgressDialog;
|
---|
751 | }
|
---|
752 |
|
---|
753 | /* Prepare network-request: */
|
---|
754 | void UINetworkManager::prepareNetworkRequest(UINetworkRequest *pNetworkRequest)
|
---|
755 | {
|
---|
756 | /* Prepare listeners for pNetworkRequest: */
|
---|
757 | connect(pNetworkRequest, SIGNAL(sigProgress(const QUuid&, qint64, qint64)), this, SLOT(sltHandleNetworkRequestProgress(const QUuid&, qint64, qint64)));
|
---|
758 | connect(pNetworkRequest, SIGNAL(sigCanceled(const QUuid&)), this, SLOT(sltHandleNetworkRequestCancel(const QUuid&)));
|
---|
759 | connect(pNetworkRequest, SIGNAL(sigFinished(const QUuid&)), this, SLOT(sltHandleNetworkRequestFinish(const QUuid&)));
|
---|
760 | connect(pNetworkRequest, SIGNAL(sigFailed(const QUuid&, const QString&)), this, SLOT(sltHandleNetworkRequestFailure(const QUuid&, const QString&)));
|
---|
761 |
|
---|
762 | /* Add this request into internal map: */
|
---|
763 | m_requests.insert(pNetworkRequest->uuid(), pNetworkRequest);
|
---|
764 | }
|
---|
765 |
|
---|
766 | /* Cleanup network-request: */
|
---|
767 | void UINetworkManager::cleanupNetworkRequest(QUuid uuid)
|
---|
768 | {
|
---|
769 | /* Cleanup network-request map: */
|
---|
770 | delete m_requests[uuid];
|
---|
771 | m_requests.remove(uuid);
|
---|
772 | }
|
---|
773 |
|
---|
774 | /* Cleanup all network-requests: */
|
---|
775 | void UINetworkManager::cleanupNetworkRequests()
|
---|
776 | {
|
---|
777 | /* Get all the request IDs: */
|
---|
778 | const QList<QUuid> &uuids = m_requests.keys();
|
---|
779 | /* Cleanup corresponding requests: */
|
---|
780 | for (int i = 0; i < uuids.size(); ++i)
|
---|
781 | cleanupNetworkRequest(uuids[i]);
|
---|
782 | }
|
---|
783 |
|
---|
784 | #if 0
|
---|
785 | /* Downloader creation notificator: */
|
---|
786 | void UINetworkManager::notifyDownloaderCreated(UIDownloadType downloaderType)
|
---|
787 | {
|
---|
788 | emit sigDownloaderCreated(downloaderType);
|
---|
789 | }
|
---|
790 | #endif
|
---|
791 |
|
---|
792 | /* Slot to handle network-request progress: */
|
---|
793 | void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
|
---|
794 | {
|
---|
795 | /* Make sure corresponding map contains received ID: */
|
---|
796 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
797 |
|
---|
798 | /* Get corresponding network-request: */
|
---|
799 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
800 |
|
---|
801 | /* Get corresponding customer: */
|
---|
802 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
803 |
|
---|
804 | /* Send to customer to process: */
|
---|
805 | pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);
|
---|
806 | }
|
---|
807 |
|
---|
808 | /* Slot to handle network-request cancel: */
|
---|
809 | void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid)
|
---|
810 | {
|
---|
811 | /* Make sure corresponding map contains received ID: */
|
---|
812 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
813 |
|
---|
814 | /* Get corresponding network-request: */
|
---|
815 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
816 |
|
---|
817 | /* Get corresponding customer: */
|
---|
818 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
819 |
|
---|
820 | /* Send to customer to process: */
|
---|
821 | pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());
|
---|
822 |
|
---|
823 | /* Cleanup network-request: */
|
---|
824 | cleanupNetworkRequest(uuid);
|
---|
825 | }
|
---|
826 |
|
---|
827 | /* Slot to handle network-request finish: */
|
---|
828 | void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid)
|
---|
829 | {
|
---|
830 | /* Make sure corresponding map contains received ID: */
|
---|
831 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
832 |
|
---|
833 | /* Get corresponding network-request: */
|
---|
834 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
835 |
|
---|
836 | /* Get corresponding customer: */
|
---|
837 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
838 |
|
---|
839 | /* Send to customer to process: */
|
---|
840 | pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());
|
---|
841 |
|
---|
842 | /* Cleanup network-request: */
|
---|
843 | cleanupNetworkRequest(uuid);
|
---|
844 | }
|
---|
845 |
|
---|
846 | /* Slot to handle network-request failure: */
|
---|
847 | void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &)
|
---|
848 | {
|
---|
849 | /* Make sure corresponding map contains received ID: */
|
---|
850 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
851 |
|
---|
852 | /* Get corresponding network-request: */
|
---|
853 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
854 |
|
---|
855 | /* Get corresponding customer: */
|
---|
856 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
857 |
|
---|
858 | /* If customer made a force-call: */
|
---|
859 | if (pNetworkCustomer->isItForceCall())
|
---|
860 | {
|
---|
861 | /* Just show the dialog: */
|
---|
862 | show();
|
---|
863 | }
|
---|
864 | }
|
---|
865 |
|
---|
866 | #include "UINetworkManager.moc"
|
---|
867 |
|
---|