VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp@ 41152

Last change on this file since 41152 was 41152, checked in by vboxsync, 13 years ago

FE/Qt: Network Manager stuff: Switching Network Manager error-pane from QTextEdit to QIRichTextLabel.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 26.9 KB
Line 
1/* $Id: UINetworkManager.cpp 41152 2012-05-03 21:52:23Z 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: */
46class UINetworkRequestWidget : public QIWithRetranslateUI<UIPopupBox>
47{
48 Q_OBJECT;
49
50signals:
51
52 /* Signal to retry network-request: */
53 void sigRetry();
54 /* Signal to cancel network-request: */
55 void sigCancel();
56
57public:
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
139private 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
240private:
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: */
256class UINetworkManagerDialog : public QIWithRetranslateUI<QMainWindow>
257{
258 Q_OBJECT;
259
260signals:
261
262 /* Signal to cancel all network-requests: */
263 void sigCancelNetworkRequests();
264
265public 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
280public:
281
282 /* Constructor: */
283 UINetworkManagerDialog(QWidget *pParent = 0)
284 : QIWithRetranslateUI<QMainWindow>(pParent)
285 {
286 /* Do not count that window as important for application,
287 * it will NOT be taken into account when other top-level windows will be closed: */
288 setAttribute(Qt::WA_QuitOnClose, false);
289
290 /* Set minimum width: */
291 setMinimumWidth(500);
292
293 /* Prepare central-widget: */
294 setCentralWidget(new QWidget);
295
296 /* Create main-layout: */
297 m_pMainLayout = new QVBoxLayout(centralWidget());
298 m_pMainLayout->setContentsMargins(6, 6, 6, 6);
299
300 /* Create description-label: */
301 m_pLabel = new QLabel(centralWidget());
302 m_pLabel->setAlignment(Qt::AlignCenter);
303 m_pLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
304
305 /* Create button-box: */
306 m_pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, centralWidget());
307 connect(m_pButtonBox, SIGNAL(rejected()), this, SLOT(sltHandleCancelAllButtonPress()));
308 m_pButtonBox->setHidden(true);
309
310 /* Layout content: */
311 m_pMainLayout->addWidget(m_pLabel);
312 m_pMainLayout->addStretch();
313 m_pMainLayout->addWidget(m_pButtonBox);
314
315 /* Create status-bar: */
316 setStatusBar(new QStatusBar);
317
318 /* Translate dialog: */
319 retranslateUi();
320 }
321
322 /* Add network-request widget: */
323 UINetworkRequestWidget* addNetworkRequestWidget(UINetworkRequest *pNetworkRequest)
324 {
325 /* Make sure network-request is really exists: */
326 AssertMsg(pNetworkRequest, ("Network-request doesn't exists!\n"));
327
328 /* Create new network-request widget: */
329 UINetworkRequestWidget *pNetworkRequestWidget = new UINetworkRequestWidget(this, pNetworkRequest);
330 m_pMainLayout->insertWidget(m_pMainLayout->count() - 2 /* before button-box and stretch */, pNetworkRequestWidget);
331 m_widgets.insert(pNetworkRequest->uuid(), pNetworkRequestWidget);
332
333 /* Hide label: */
334 m_pLabel->setHidden(true);
335 /* Show button-box: */
336 m_pButtonBox->setHidden(false);
337 /* Show dialog: */
338 showNormal();
339
340 /* Return network-request widget: */
341 return pNetworkRequestWidget;
342 }
343
344 /* Remove network-request widget: */
345 void removeNetworkRequestWidget(const QUuid &uuid)
346 {
347 /* Make sure network-request widget still present: */
348 AssertMsg(m_widgets.contains(uuid), ("Network-request widget already removed!\n"));
349
350 /* Delete corresponding network-request widget: */
351 delete m_widgets.value(uuid);
352 m_widgets.remove(uuid);
353
354 /* Check if dialog is empty: */
355 if (m_widgets.isEmpty())
356 {
357 /* Show label: */
358 m_pLabel->setHidden(false);
359 /* Hide button-box: */
360 m_pButtonBox->setHidden(true);
361 /* Hide dialog: */
362 hide();
363 }
364 }
365
366private slots:
367
368 /* Handler for 'Cancel All' button press: */
369 void sltHandleCancelAllButtonPress()
370 {
371 /* Ask if user wants to cancel all current network-requests: */
372 if (msgCenter().askAboutCancelAllNetworkRequest(this))
373 emit sigCancelNetworkRequests();
374 }
375
376private:
377
378 /* Translate whole dialog: */
379 void retranslateUi()
380 {
381 /* Set window caption: */
382 setWindowTitle(UINetworkManager::tr("Network Operations Manager"));
383
384 /* Set description-label text: */
385 m_pLabel->setText(UINetworkManager::tr("There are no active network operations."));
386
387 /* Set buttons-box text: */
388 m_pButtonBox->button(QDialogButtonBox::Cancel)->setText(UINetworkManager::tr("&Cancel All"));
389 m_pButtonBox->button(QDialogButtonBox::Cancel)->setStatusTip(UINetworkManager::tr("Cancel all active network operations"));
390 }
391
392 /* Overloaded show-event: */
393 void showEvent(QShowEvent *pShowEvent)
394 {
395 /* Resize to minimum size: */
396 resize(minimumSize());
397
398 /* Center according current main application window: */
399 vboxGlobal().centerWidget(this, vboxGlobal().mainWindow(), false);
400
401 /* Pass event to the base-class: */
402 QMainWindow::showEvent(pShowEvent);
403 }
404
405 /* Overloaded close-event: */
406 void closeEvent(QCloseEvent *pCloseEvent)
407 {
408 /* If there are network-requests present
409 * ask if user wants to cancel all current network-requests
410 * or leave them working at the background: */
411 if (!m_widgets.isEmpty() &&
412 msgCenter().askAboutCancelOrLeaveAllNetworkRequest(this))
413 emit sigCancelNetworkRequests();
414
415 /* Pass event to the base-class: */
416 QMainWindow::closeEvent(pCloseEvent);
417 }
418
419 /* Overloaded keypress-event: */
420 void keyPressEvent(QKeyEvent *pKeyPressEvent)
421 {
422 /* 'Escape' key used to close the dialog: */
423 if (pKeyPressEvent->key() == Qt::Key_Escape)
424 {
425 close();
426 return;
427 }
428
429 /* Pass event to the base-class: */
430 QMainWindow::keyPressEvent(pKeyPressEvent);
431 }
432
433 /* Main layout: */
434 QVBoxLayout *m_pMainLayout;
435 QLabel *m_pLabel;
436 QIDialogButtonBox *m_pButtonBox;
437
438 /* Popup-widget map: */
439 QMap<QUuid, UINetworkRequestWidget*> m_widgets;
440};
441
442/* Constructor: */
443UINetworkRequest::UINetworkRequest(UINetworkManager *pNetworkManager,
444 UINetworkManagerDialog *pNetworkManagerDialog,
445 const QNetworkRequest &request, UINetworkRequestType type,
446 const QString &strDescription, UINetworkCustomer *pCustomer)
447 : QObject(pNetworkManager)
448 , m_pNetworkManagerDialog(pNetworkManagerDialog)
449 , m_pNetworkRequestWidget(0)
450 , m_uuid(QUuid::createUuid())
451 , m_requests(QList<QNetworkRequest>() << request)
452 , m_iCurrentRequestIndex(0)
453 , m_type(type)
454 , m_strDescription(strDescription)
455 , m_pCustomer(pCustomer)
456 , m_fRunning(false)
457{
458 /* Initialize: */
459 initialize();
460}
461
462UINetworkRequest::UINetworkRequest(UINetworkManager *pNetworkManager,
463 UINetworkManagerDialog *pNetworkManagerDialog,
464 const QList<QNetworkRequest> &requests, UINetworkRequestType type,
465 const QString &strDescription, UINetworkCustomer *pCustomer)
466 : QObject(pNetworkManager)
467 , m_pNetworkManagerDialog(pNetworkManagerDialog)
468 , m_pNetworkRequestWidget(0)
469 , m_uuid(QUuid::createUuid())
470 , m_requests(requests)
471 , m_iCurrentRequestIndex(0)
472 , m_type(type)
473 , m_strDescription(strDescription)
474 , m_pCustomer(pCustomer)
475 , m_fRunning(false)
476{
477 /* Initialize: */
478 initialize();
479}
480
481/* Destructor: */
482UINetworkRequest::~UINetworkRequest()
483{
484 /* Destroy network-reply: */
485 cleanupNetworkReply();
486
487 /* Destroy network-request widget: */
488 m_pNetworkManagerDialog->removeNetworkRequestWidget(m_uuid);
489}
490
491/* Network-reply progress handler: */
492void UINetworkRequest::sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal)
493{
494 /* Notify UINetworkManager: */
495 emit sigProgress(m_uuid, iReceived, iTotal);
496 /* Notify UINetworkRequestWidget: */
497 emit sigProgress(iReceived, iTotal);
498}
499
500/* Network-reply finish handler: */
501void UINetworkRequest::sltHandleNetworkReplyFinish()
502{
503 /* Set as non-running: */
504 m_fRunning = false;
505
506 /* Get sender network reply: */
507 QNetworkReply *pNetworkReply = static_cast<QNetworkReply*>(sender());
508
509 /* If network-request was canceled: */
510 if (pNetworkReply->error() == QNetworkReply::OperationCanceledError)
511 {
512 /* Notify UINetworkManager: */
513 emit sigCanceled(m_uuid);
514 }
515 /* If network-reply has no errors: */
516 else if (pNetworkReply->error() == QNetworkReply::NoError)
517 {
518 /* Check if redirection required: */
519 QUrl redirect = pNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
520 if (redirect.isValid())
521 {
522 /* Cleanup current network-reply first: */
523 cleanupNetworkReply();
524
525 /* Choose redirect-source as current: */
526 m_request.setUrl(redirect);
527
528 /* Create new network-reply finally: */
529 prepareNetworkReply();
530 }
531 else
532 {
533 /* Notify UINetworkRequestWidget: */
534 emit sigFinished();
535 /* Notify UINetworkManager: */
536 emit sigFinished(m_uuid);
537 }
538 }
539 /* If some error occured: */
540 else
541 {
542 /* Check if we have other requests in set: */
543 if (m_iCurrentRequestIndex < m_requests.size() - 1)
544 {
545 /* Cleanup current network-reply first: */
546 cleanupNetworkReply();
547
548 /* Choose next network-request as current: */
549 ++m_iCurrentRequestIndex;
550 m_request = m_requests[m_iCurrentRequestIndex];
551
552 /* Create new network-reply finally: */
553 prepareNetworkReply();
554 }
555 else
556 {
557 /* Notify UINetworkRequestWidget: */
558 emit sigFailed(pNetworkReply->errorString());
559 /* Notify UINetworkManager: */
560 emit sigFailed(m_uuid, pNetworkReply->errorString());
561 }
562 }
563}
564
565/* Slot to retry network-request: */
566void UINetworkRequest::sltRetry()
567{
568 /* Cleanup current network-reply first: */
569 cleanupNetworkReply();
570
571 /* Choose first network-request as current: */
572 m_iCurrentRequestIndex = 0;
573 m_request = m_requests[m_iCurrentRequestIndex];
574
575 /* Create new network-reply finally: */
576 prepareNetworkReply();
577}
578
579/* Slot to cancel network-request: */
580void UINetworkRequest::sltCancel()
581{
582 /* Abort network-reply if present: */
583 abortNetworkReply();
584}
585
586/* Initialize: */
587void UINetworkRequest::initialize()
588{
589 /* Prepare listeners for parent(): */
590 connect(parent(), SIGNAL(sigCancelNetworkRequests()), this, SLOT(sltCancel()));
591
592 /* Create network-request widget: */
593 m_pNetworkRequestWidget = m_pNetworkManagerDialog->addNetworkRequestWidget(this);
594
595 /* Prepare listeners for m_pNetworkRequestWidget: */
596 connect(m_pNetworkRequestWidget, SIGNAL(sigRetry()), this, SLOT(sltRetry()));
597 connect(m_pNetworkRequestWidget, SIGNAL(sigCancel()), this, SLOT(sltCancel()));
598
599 /* Choose first network-request as current: */
600 m_iCurrentRequestIndex = 0;
601 m_request = m_requests[m_iCurrentRequestIndex];
602
603 /* Create network-reply: */
604 prepareNetworkReply();
605}
606
607/* Prepare network-reply: */
608void UINetworkRequest::prepareNetworkReply()
609{
610 /* Make network-request: */
611 switch (m_type)
612 {
613 case UINetworkRequestType_HEAD:
614 {
615 m_pReply = gNetworkManager->head(m_request);
616 break;
617 }
618 case UINetworkRequestType_GET:
619 {
620 m_pReply = gNetworkManager->get(m_request);
621 break;
622 }
623 default:
624 break;
625 }
626
627 /* Prepare listeners for m_pReply: */
628 AssertMsg(m_pReply, ("Unable to make network-request!\n"));
629 connect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(sltHandleNetworkReplyProgress(qint64, qint64)));
630 connect(m_pReply, SIGNAL(finished()), this, SLOT(sltHandleNetworkReplyFinish()));
631
632 /* Set as running: */
633 m_fRunning = true;
634
635 /* Notify UINetworkRequestWidget: */
636 emit sigStarted();
637}
638
639/* Cleanup network-reply: */
640void UINetworkRequest::cleanupNetworkReply()
641{
642 /* Destroy current reply: */
643 AssertMsg(m_pReply, ("Network-reply already destroyed!\n"));
644 m_pReply->disconnect();
645 m_pReply->deleteLater();
646 m_pReply = 0;
647}
648
649/* Abort network-reply: */
650void UINetworkRequest::abortNetworkReply()
651{
652 /* Abort network-reply if present: */
653 if (m_pReply)
654 {
655 if (m_fRunning)
656 m_pReply->abort();
657 else
658 emit sigCanceled(m_uuid);
659 }
660}
661
662/* Instance: */
663UINetworkManager* UINetworkManager::m_pInstance = 0;
664
665/* Create singleton: */
666void UINetworkManager::create()
667{
668 /* Check that instance do NOT exist: */
669 if (m_pInstance)
670 return;
671
672 /* Create instance: */
673 new UINetworkManager;
674}
675
676/* Destroy singleton: */
677void UINetworkManager::destroy()
678{
679 /* Check that instance exists: */
680 if (!m_pInstance)
681 return;
682
683 /* Destroy instance: */
684 delete m_pInstance;
685}
686
687/* Network Access Manager GUI window: */
688QWidget* UINetworkManager::window() const
689{
690 return m_pNetworkProgressDialog;
691}
692
693/* Show Network Access Manager GUI: */
694void UINetworkManager::show()
695{
696 /* Just show the dialog: */
697 m_pNetworkProgressDialog->showNormal();
698}
699
700/* Network-request creation wrapper for UINetworkCustomer: */
701void UINetworkManager::createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type,
702 const QString &strDescription, UINetworkCustomer *pCustomer)
703{
704 /* Create new network-request: */
705 UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkProgressDialog,
706 request, type, strDescription, pCustomer);
707 /* Prepare created network-request: */
708 prepareNetworkRequest(pNetworkRequest);
709}
710
711/* Network request (set) creation wrapper for UINetworkCustomer: */
712void UINetworkManager::createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type,
713 const QString &strDescription, UINetworkCustomer *pCustomer)
714{
715 /* Create new network-request: */
716 UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkProgressDialog,
717 requests, type, strDescription, pCustomer);
718 /* Prepare created network-request: */
719 prepareNetworkRequest(pNetworkRequest);
720}
721
722/* Constructor: */
723UINetworkManager::UINetworkManager()
724{
725 /* Prepare instance: */
726 m_pInstance = this;
727
728 /* Prepare finally: */
729 prepare();
730}
731
732/* Destructor: */
733UINetworkManager::~UINetworkManager()
734{
735 /* Cleanup first: */
736 cleanup();
737
738 /* Cleanup instance: */
739 m_pInstance = 0;
740}
741
742/* Prepare: */
743void UINetworkManager::prepare()
744{
745 /* Prepare network manager GUI: */
746 m_pNetworkProgressDialog = new UINetworkManagerDialog;
747
748 /* Prepare listeners for m_pNetworkProgressDialog: */
749 connect(m_pNetworkProgressDialog, SIGNAL(sigCancelNetworkRequests()), this, SIGNAL(sigCancelNetworkRequests()));
750}
751
752/* Cleanup: */
753void UINetworkManager::cleanup()
754{
755 /* Cleanup network-requests first: */
756 cleanupNetworkRequests();
757
758 /* Cleanup network manager GUI: */
759 delete m_pNetworkProgressDialog;
760}
761
762/* Prepare network-request: */
763void UINetworkManager::prepareNetworkRequest(UINetworkRequest *pNetworkRequest)
764{
765 /* Prepare listeners for pNetworkRequest: */
766 connect(pNetworkRequest, SIGNAL(sigProgress(const QUuid&, qint64, qint64)), this, SLOT(sltHandleNetworkRequestProgress(const QUuid&, qint64, qint64)));
767 connect(pNetworkRequest, SIGNAL(sigCanceled(const QUuid&)), this, SLOT(sltHandleNetworkRequestCancel(const QUuid&)));
768 connect(pNetworkRequest, SIGNAL(sigFinished(const QUuid&)), this, SLOT(sltHandleNetworkRequestFinish(const QUuid&)));
769 connect(pNetworkRequest, SIGNAL(sigFailed(const QUuid&, const QString&)), this, SLOT(sltHandleNetworkRequestFailure(const QUuid&, const QString&)));
770
771 /* Add this request into internal map: */
772 m_requests.insert(pNetworkRequest->uuid(), pNetworkRequest);
773}
774
775/* Cleanup network-request: */
776void UINetworkManager::cleanupNetworkRequest(QUuid uuid)
777{
778 /* Cleanup network-request map: */
779 delete m_requests[uuid];
780 m_requests.remove(uuid);
781}
782
783/* Cleanup all network-requests: */
784void UINetworkManager::cleanupNetworkRequests()
785{
786 /* Get all the request IDs: */
787 const QList<QUuid> &uuids = m_requests.keys();
788 /* Cleanup corresponding requests: */
789 for (int i = 0; i < uuids.size(); ++i)
790 cleanupNetworkRequest(uuids[i]);
791}
792
793#if 0
794/* Downloader creation notificator: */
795void UINetworkManager::notifyDownloaderCreated(UIDownloadType downloaderType)
796{
797 emit sigDownloaderCreated(downloaderType);
798}
799#endif
800
801/* Slot to handle network-request progress: */
802void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
803{
804 /* Make sure corresponding map contains received ID: */
805 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
806
807 /* Get corresponding network-request: */
808 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
809
810 /* Get corresponding customer: */
811 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
812
813 /* Send to customer to process: */
814 pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);
815}
816
817/* Slot to handle network-request cancel: */
818void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid)
819{
820 /* Make sure corresponding map contains received ID: */
821 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
822
823 /* Get corresponding network-request: */
824 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
825
826 /* Get corresponding customer: */
827 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
828
829 /* Send to customer to process: */
830 pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());
831
832 /* Cleanup network-request: */
833 cleanupNetworkRequest(uuid);
834}
835
836/* Slot to handle network-request finish: */
837void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid)
838{
839 /* Make sure corresponding map contains received ID: */
840 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
841
842 /* Get corresponding network-request: */
843 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
844
845 /* Get corresponding customer: */
846 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
847
848 /* Send to customer to process: */
849 pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());
850
851 /* Cleanup network-request: */
852 cleanupNetworkRequest(uuid);
853}
854
855/* Slot to handle network-request failure: */
856void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &strError)
857{
858 /* Just show the dialog: */
859 show();
860 Q_UNUSED(uuid);
861 Q_UNUSED(strError);
862}
863
864#include "UINetworkManager.moc"
865
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette