Changeset 41440 in vbox for trunk/src/VBox
- Timestamp:
- May 24, 2012 4:37:17 PM (13 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox
- Files:
-
- 3 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk
r41387 r41440 272 272 src/globals/VBoxGlobal.h \ 273 273 src/globals/VBoxUtils.h \ 274 src/net/UINetworkManager.h \ 275 src/net/UINetworkManagerDialog.h \ 276 src/net/UINetworkRequest.h \ 277 src/net/UINetworkRequestWidget.h \ 278 src/net/UINetworkCustomer.h \ 279 src/net/UIUpdateManager.h \ 274 280 src/net/UIDownloader.h \ 275 281 src/net/UIDownloaderAdditions.h \ 276 282 src/net/UIDownloaderExtensionPack.h \ 277 283 src/net/UIDownloaderUserManual.h \ 278 src/net/UINetworkManager.h \279 src/net/UIUpdateManager.h \280 src/net/UINetworkCustomer.h \281 284 src/runtime/UIActionPoolRuntime.h \ 282 285 src/runtime/UIConsoleEventHandler.h \ … … 404 407 src/globals/UIActionPool.cpp \ 405 408 src/globals/UIExtraDataEventHandler.cpp \ 406 src/net/UINetworkManager.cpp \407 409 src/net/UIUpdateManager.cpp \ 408 410 src/runtime/UIActionPoolRuntime.cpp \ … … 471 473 src/globals/VBoxGlobal.cpp \ 472 474 src/main.cpp \ 475 src/net/UINetworkManager.cpp \ 476 src/net/UINetworkManagerDialog.cpp \ 477 src/net/UINetworkRequest.cpp \ 478 src/net/UINetworkRequestWidget.cpp \ 479 src/net/UINetworkCustomer.cpp \ 473 480 src/net/UIDownloader.cpp \ 474 481 src/net/UIDownloaderAdditions.cpp \ 475 482 src/net/UIDownloaderExtensionPack.cpp \ 476 483 src/net/UIDownloaderUserManual.cpp \ 477 src/net/UINetworkManager.cpp \478 484 src/net/UIUpdateDefs.cpp \ 479 485 src/net/UIUpdateManager.cpp \ 480 src/net/UINetworkCustomer.cpp \481 486 src/runtime/UIActionPoolRuntime.cpp \ 482 487 src/runtime/UIConsoleEventHandler.cpp \ -
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp
r41433 r41440 20 20 /* Global includes: */ 21 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 22 33 23 /* Local includes: */ 34 24 #include "UINetworkManager.h" 25 #include "UINetworkManagerDialog.h" 26 #include "UINetworkRequest.h" 35 27 #include "UINetworkCustomer.h" 36 #include "QIWithRetranslateUI.h"37 28 #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: */ 29 654 30 UINetworkManager* UINetworkManager::m_pInstance = 0; 655 31 656 /* Create singleton: */657 32 void UINetworkManager::create() 658 33 { … … 663 38 /* Create instance: */ 664 39 new UINetworkManager; 665 } 666 667 /* Destroy singleton: */ 40 41 /* Prepare instance: */ 42 m_pInstance->prepare(); 43 } 44 668 45 void UINetworkManager::destroy() 669 46 { … … 672 49 return; 673 50 51 /* Cleanup instance: */ 52 m_pInstance->cleanup(); 53 674 54 /* Destroy instance: */ 675 55 delete m_pInstance; 676 56 } 677 57 678 /* Network Access Manager GUI window: */679 58 QWidget* UINetworkManager::window() const 680 59 { 681 return m_pNetworkProgressDialog; 682 } 683 684 /* Show Network Access Manager GUI: */ 60 return m_pNetworkManagerDialog; 61 } 62 685 63 void UINetworkManager::show() 686 64 { 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); 65 /* Show network-manager dialog: */ 66 m_pNetworkManagerDialog->showNormal(); 67 } 68 69 void UINetworkManager::createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type, const QString &strDescription, 70 UINetworkCustomer *pCustomer) 71 { 72 /* Create network-request: */ 73 UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkManagerDialog, request, type, strDescription, pCustomer); 698 74 /* Prepare created network-request: */ 699 75 prepareNetworkRequest(pNetworkRequest); 700 76 } 701 77 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); 78 void UINetworkManager::createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type, const QString &strDescription, 79 UINetworkCustomer *pCustomer) 80 { 81 /* Create network-request: */ 82 UINetworkRequest *pNetworkRequest = new UINetworkRequest(this, m_pNetworkManagerDialog, requests, type, strDescription, pCustomer); 709 83 /* Prepare created network-request: */ 710 84 prepareNetworkRequest(pNetworkRequest); 711 85 } 712 86 713 /* Constructor: */714 87 UINetworkManager::UINetworkManager() 715 88 { 716 89 /* Prepare instance: */ 717 90 m_pInstance = this; 718 719 /* Prepare finally: */ 720 prepare(); 721 } 722 723 /* Destructor: */ 91 } 92 724 93 UINetworkManager::~UINetworkManager() 725 94 { 726 /* Cleanup first: */727 cleanup();728 729 95 /* Cleanup instance: */ 730 96 m_pInstance = 0; 731 97 } 732 98 733 /* Prepare: */734 99 void UINetworkManager::prepare() 735 100 { 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: */ 101 /* Prepare network-manager dialog: */ 102 m_pNetworkManagerDialog = new UINetworkManagerDialog; 103 connect(m_pNetworkManagerDialog, SIGNAL(sigCancelNetworkRequests()), this, SIGNAL(sigCancelNetworkRequests())); 104 } 105 744 106 void UINetworkManager::cleanup() 745 107 { … … 747 109 cleanupNetworkRequests(); 748 110 749 /* Cleanup network manager GUI: */ 750 delete m_pNetworkProgressDialog; 751 } 752 753 /* Prepare network-request: */ 111 /* Cleanup network-manager dialog: */ 112 delete m_pNetworkManagerDialog; 113 } 114 754 115 void UINetworkManager::prepareNetworkRequest(UINetworkRequest *pNetworkRequest) 755 116 { 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: */ 117 /* Prepare listeners for network-request: */ 118 connect(pNetworkRequest, SIGNAL(sigProgress(const QUuid&, qint64, qint64)), 119 this, SLOT(sltHandleNetworkRequestProgress(const QUuid&, qint64, qint64))); 120 connect(pNetworkRequest, SIGNAL(sigCanceled(const QUuid&)), 121 this, SLOT(sltHandleNetworkRequestCancel(const QUuid&))); 122 connect(pNetworkRequest, SIGNAL(sigFinished(const QUuid&)), 123 this, SLOT(sltHandleNetworkRequestFinish(const QUuid&))); 124 connect(pNetworkRequest, SIGNAL(sigFailed(const QUuid&, const QString&)), 125 this, SLOT(sltHandleNetworkRequestFailure(const QUuid&, const QString&))); 126 127 /* Add network-request into map: */ 763 128 m_requests.insert(pNetworkRequest->uuid(), pNetworkRequest); 764 129 } 765 130 766 /* Cleanup network-request: */767 131 void UINetworkManager::cleanupNetworkRequest(QUuid uuid) 768 132 { 769 /* Cleanup network-requestmap: */133 /* Delete network-request from map: */ 770 134 delete m_requests[uuid]; 771 135 m_requests.remove(uuid); 772 136 } 773 137 774 /* Cleanup all network-requests: */775 138 void UINetworkManager::cleanupNetworkRequests() 776 139 { … … 782 145 } 783 146 784 /* Slot to handle network-request progress: */785 147 void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal) 786 148 { … … 798 160 } 799 161 800 /* Slot to handle network-request cancel: */801 162 void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid) 802 163 { … … 817 178 } 818 179 819 /* Slot to handle network-request finish: */820 180 void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid) 821 181 { … … 836 196 } 837 197 838 /* Slot to handle network-request failure: */839 198 void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &) 840 199 { … … 856 215 } 857 216 858 #include "UINetworkManager.moc"859 -
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h
r41433 r41440 25 25 #include <QMap> 26 26 #include <QNetworkRequest> 27 #include <QNetworkReply>28 #include <QPointer>29 27 30 28 /* Local inludes: */ … … 32 30 33 31 /* Forward declarations: */ 34 class UINetworkManager; 32 class QWidget; 33 class UINetworkRequest; 35 34 class UINetworkCustomer; 36 class UINetworkRequestWidget;37 35 class UINetworkManagerDialog; 38 36 39 /* Network request contianer: */ 40 class UINetworkRequest : public QObject 41 { 42 Q_OBJECT; 43 44 signals: 45 46 /* Notifications to UINetworkManager: */ 47 void sigProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal); 48 void sigCanceled(const QUuid &uuid); 49 void sigFinished(const QUuid &uuid); 50 void sigFailed(const QUuid &uuid, const QString &strError); 51 52 /* Notifications to UINetworkRequestWidget: */ 53 void sigProgress(qint64 iReceived, qint64 iTotal); 54 void sigStarted(); 55 void sigFinished(); 56 void sigFailed(const QString &strError); 57 58 public: 59 60 /* Constructor/destructor: */ 61 UINetworkRequest(UINetworkManager *pNetworkManager, 62 UINetworkManagerDialog *pNetworkManagerDialog, 63 const QNetworkRequest &request, UINetworkRequestType type, 64 const QString &strDescription, UINetworkCustomer *pCustomer); 65 UINetworkRequest(UINetworkManager *pNetworkManager, 66 UINetworkManagerDialog *pNetworkManagerDialog, 67 const QList<QNetworkRequest> &requests, UINetworkRequestType type, 68 const QString &strDescription, UINetworkCustomer *pCustomer); 69 ~UINetworkRequest(); 70 71 /* Getters: */ 72 const QUuid& uuid() const { return m_uuid; } 73 const QString& description() const { return m_strDescription; } 74 UINetworkCustomer* customer() { return m_pCustomer; } 75 QNetworkReply* reply() { return m_pReply; } 76 77 private slots: 78 79 /* Network-reply progress handler: */ 80 void sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal); 81 /* Network-reply finish handler: */ 82 void sltHandleNetworkReplyFinish(); 83 84 /* Slot to retry network-request: */ 85 void sltRetry(); 86 /* Slot to cancel network-request: */ 87 void sltCancel(); 88 89 private: 90 91 /* Initialize: */ 92 void initialize(); 93 94 /* Prepare network-reply: */ 95 void prepareNetworkReply(); 96 /* Cleanup network-reply: */ 97 void cleanupNetworkReply(); 98 /* Abort network-reply: */ 99 void abortNetworkReply(); 100 101 /* Widgets: */ 102 UINetworkManagerDialog *m_pNetworkManagerDialog; 103 UINetworkRequestWidget *m_pNetworkRequestWidget; 104 105 /* Variables: */ 106 QUuid m_uuid; 107 QList<QNetworkRequest> m_requests; 108 QNetworkRequest m_request; 109 int m_iCurrentRequestIndex; 110 UINetworkRequestType m_type; 111 QString m_strDescription; 112 UINetworkCustomer *m_pCustomer; 113 QPointer<QNetworkReply> m_pReply; 114 bool m_fRunning; 115 }; 116 117 /* QNetworkAccessManager class reimplementation providing 118 * network access for the VirtualBox application purposes. */ 37 /* QNetworkAccessManager class reimplementation. 38 * Providing network access for VirtualBox application purposes. */ 119 39 class UINetworkManager : public QNetworkAccessManager 120 40 { … … 123 43 signals: 124 44 125 /* Notification to UINetworkRequest: */45 /* Ask listeners (network-requests) to cancel: */ 126 46 void sigCancelNetworkRequests(); 127 47 … … 135 55 static void destroy(); 136 56 137 /* Network Access Manager GUI window: */57 /* Pointer to network-manager dialog: */ 138 58 QWidget* window() const; 139 59 140 60 public slots: 141 61 142 /* Show Network Access Manager GUI: */62 /* Show network-manager dialog: */ 143 63 void show(); 144 64 145 65 protected: 146 66 147 /* Allow UINetworkCustomer to implicitly use next mothods: */67 /* Allow UINetworkCustomer to create network-request: */ 148 68 friend class UINetworkCustomer; 149 /* Network-request creation wrapper for UINetworkCustomer: */ 150 void createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type, 151 const QString &strDescription, UINetworkCustomer *pCustomer); 152 /* Network request (set) creation wrapper for UINetworkCustomer: */ 153 void createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type, 154 const QString &strDescription, UINetworkCustomer *pCustomer); 69 /* Network-request creation wrappers for UINetworkCustomer: */ 70 void createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type, const QString &strDescription, 71 UINetworkCustomer *pCustomer); 72 void createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type, const QString &strDescription, 73 UINetworkCustomer *pCustomer); 155 74 156 75 private: … … 190 109 QMap<QUuid, UINetworkRequest*> m_requests; 191 110 192 /* Network manager UI: */193 UINetworkManagerDialog *m_pNetwork ProgressDialog;111 /* Network-manager dialog: */ 112 UINetworkManagerDialog *m_pNetworkManagerDialog; 194 113 }; 195 114 #define gNetworkManager UINetworkManager::instance() 196 115 197 116 #endif // __UINetworkManager_h__ 117 -
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManagerDialog.cpp
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 74233
r41434 r41440 3 3 * 4 4 * VBox frontends: Qt GUI ("VirtualBox"): 5 * UINetworkManager stuff implementation5 * UINetworkManagerDialog stuff implementation 6 6 */ 7 7 … … 19 19 20 20 /* Global includes: */ 21 #include <QWidget>22 #include <QTimer>23 #include <QGridLayout>24 #include <QProgressBar>25 #include <QMainWindow>26 21 #include <QVBoxLayout> 27 22 #include <QLabel> … … 29 24 #include <QStatusBar> 30 25 #include <QKeyEvent> 31 #include <QNetworkReply>32 26 33 27 /* Local includes: */ 28 #include "UINetworkManagerDialog.h" 34 29 #include "UINetworkManager.h" 30 #include "UINetworkRequest.h" 31 #include "UINetworkRequestWidget.h" 35 32 #include "UINetworkCustomer.h" 36 #include " QIWithRetranslateUI.h"33 #include "UIIconPool.h" 37 34 #include "VBoxGlobal.h" 38 35 #include "UIMessageCenter.h" 39 #include "UIIconPool.h"40 36 #include "QIDialogButtonBox.h" 41 #include "UIPopupBox.h"42 #include "QIToolButton.h"43 #include "QIRichTextLabel.h"44 37 45 /* Network-request widget: */ 46 class UINetworkRequestWidget : public QIWithRetranslateUI<UIPopupBox> 38 void UINetworkManagerDialog::showNormal() 47 39 { 48 Q_OBJECT; 40 /* Show (restore if necessary): */ 41 QMainWindow::showNormal(); 49 42 50 signals: 43 /* Raise above the others: */ 44 raise(); 51 45 52 /* Signal to retry network-request: */ 53 void sigRetry(); 54 /* Signal to cancel network-request: */ 55 void sigCancel(); 46 /* Activate: */ 47 activateWindow(); 48 } 56 49 57 public: 50 UINetworkManagerDialog::UINetworkManagerDialog() 51 { 52 /* Apply window icons: */ 53 setWindowIcon(UIIconPool::iconSetFull(QSize (32, 32), QSize (16, 16), ":/nw_32px.png", ":/nw_16px.png")); 58 54 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)) 55 /* Do not count that window as important for application, 56 * it will NOT be taken into account when other top-level windows will be closed: */ 57 setAttribute(Qt::WA_QuitOnClose, false); 58 59 /* Set minimum width: */ 60 setMinimumWidth(500); 61 62 /* Prepare central-widget: */ 63 setCentralWidget(new QWidget); 64 65 /* Create main-layout: */ 66 QVBoxLayout *pMainLayout = new QVBoxLayout(centralWidget()); 67 pMainLayout->setContentsMargins(6, 6, 6, 6); 68 69 /* Create description-label: */ 70 m_pLabel = new QLabel(centralWidget()); 71 m_pLabel->setAlignment(Qt::AlignCenter); 72 m_pLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); 73 74 /* Create layout for network-request widgets: */ 75 m_pWidgetsLayout = new QVBoxLayout; 76 77 /* Create button-box: */ 78 m_pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, centralWidget()); 79 connect(m_pButtonBox, SIGNAL(rejected()), this, SLOT(sltHandleCancelAllButtonPress())); 80 m_pButtonBox->setHidden(true); 81 82 /* Layout content: */ 83 pMainLayout->addWidget(m_pLabel); 84 pMainLayout->addLayout(m_pWidgetsLayout); 85 pMainLayout->addStretch(); 86 pMainLayout->addWidget(m_pButtonBox); 87 88 /* Create status-bar: */ 89 setStatusBar(new QStatusBar); 90 91 /* Translate dialog: */ 92 retranslateUi(); 93 } 94 95 UINetworkRequestWidget* UINetworkManagerDialog::addNetworkRequestWidget(UINetworkRequest *pNetworkRequest) 96 { 97 /* Make sure network-request is really exists: */ 98 AssertMsg(pNetworkRequest, ("Network-request doesn't exists!\n")); 99 100 /* Create new network-request widget: */ 101 UINetworkRequestWidget *pNetworkRequestWidget = new UINetworkRequestWidget(this, pNetworkRequest); 102 m_pWidgetsLayout->addWidget(pNetworkRequestWidget); 103 m_widgets.insert(pNetworkRequest->uuid(), pNetworkRequestWidget); 104 105 /* Hide label: */ 106 m_pLabel->hide(); 107 /* Show button-box: */ 108 m_pButtonBox->show(); 109 /* If customer made a force-call: */ 110 if (pNetworkRequest->customer()->isItForceCall()) 70 111 { 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(); 112 /* Show dialog: */ 113 showNormal(); 137 114 } 138 115 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(); 116 /* Return network-request widget: */ 117 return pNetworkRequestWidget; 451 118 } 452 119 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) 120 void UINetworkManagerDialog::removeNetworkRequestWidget(const QUuid &uuid) 467 121 { 468 /* Initialize: */ 469 initialize(); 470 } 122 /* Make sure network-request widget still present: */ 123 AssertMsg(m_widgets.contains(uuid), ("Network-request widget already removed!\n")); 471 124 472 /* Destructor: */ 473 UINetworkRequest::~UINetworkRequest() 474 { 475 /* Destroy network-reply: */ 476 cleanupNetworkReply(); 125 /* Delete corresponding network-request widget: */ 126 delete m_widgets.value(uuid); 127 m_widgets.remove(uuid); 477 128 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) 129 /* Check if dialog is empty: */ 130 if (m_widgets.isEmpty()) 502 131 { 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 } 132 /* Show label: */ 133 m_pLabel->show(); 134 /* Hide button-box: */ 135 m_pButtonBox->hide(); 136 /* Let central-widget update its layout before being hidden: */ 137 QCoreApplication::sendPostedEvents(centralWidget(), QEvent::LayoutRequest); 138 /* Hide dialog: */ 139 hide(); 553 140 } 554 141 } 555 142 556 /* Slot to retry network-request: */ 557 void UINetworkRequest::sltRetry() 143 void UINetworkManagerDialog::sltHandleCancelAllButtonPress() 558 144 { 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(); 145 /* Ask if user wants to cancel all current network-requests: */ 146 if (msgCenter().askAboutCancelAllNetworkRequest(this)) 147 emit sigCancelNetworkRequests(); 568 148 } 569 149 570 /* Slot to cancel network-request: */ 571 void UINetworkRequest::sltCancel() 150 void UINetworkManagerDialog::retranslateUi() 572 151 { 573 /* Abort network-reply if present: */ 574 abortNetworkReply(); 152 /* Set window caption: */ 153 setWindowTitle(tr("Network Operations Manager")); 154 155 /* Set description-label text: */ 156 m_pLabel->setText(tr("There are no active network operations.")); 157 158 /* Set buttons-box text: */ 159 m_pButtonBox->button(QDialogButtonBox::Cancel)->setText(tr("&Cancel All")); 160 m_pButtonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Cancel all active network operations")); 575 161 } 576 162 577 /* Initialize: */ 578 void UINetworkRequest::initialize() 163 void UINetworkManagerDialog::showEvent(QShowEvent *pShowEvent) 579 164 { 580 /* Prepare listeners for parent(): */581 connect(parent(), SIGNAL(sigCancelNetworkRequests()), this, SLOT(sltCancel()));165 /* Resize to minimum size-hint: */ 166 resize(minimumSizeHint()); 582 167 583 /* C reate network-request widget: */584 m_pNetworkRequestWidget = m_pNetworkManagerDialog->addNetworkRequestWidget(this);168 /* Center according current main application window: */ 169 vboxGlobal().centerWidget(this, vboxGlobal().mainWindow(), false); 585 170 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(); 171 /* Pass event to the base-class: */ 172 QMainWindow::showEvent(pShowEvent); 596 173 } 597 174 598 /* Prepare network-reply: */ 599 void UINetworkRequest::prepareNetworkReply() 175 void UINetworkManagerDialog::keyPressEvent(QKeyEvent *pKeyPressEvent) 600 176 { 601 /* Make network-request: */602 switch (m_type)177 /* 'Escape' key used to close the dialog: */ 178 if (pKeyPressEvent->key() == Qt::Key_Escape) 603 179 { 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; 180 close(); 181 return; 616 182 } 617 183 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(); 184 /* Pass event to the base-class: */ 185 QMainWindow::keyPressEvent(pKeyPressEvent); 628 186 } 629 187 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 else649 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() const680 {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 /* Slot to handle network-request progress: */785 void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)786 {787 /* Make sure corresponding map contains received ID: */788 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));789 790 /* Get corresponding network-request: */791 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);792 793 /* Get corresponding customer: */794 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();795 796 /* Send to customer to process: */797 pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);798 }799 800 /* Slot to handle network-request cancel: */801 void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid)802 {803 /* Make sure corresponding map contains received ID: */804 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));805 806 /* Get corresponding network-request: */807 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);808 809 /* Get corresponding customer: */810 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();811 812 /* Send to customer to process: */813 pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());814 815 /* Cleanup network-request: */816 cleanupNetworkRequest(uuid);817 }818 819 /* Slot to handle network-request finish: */820 void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid)821 {822 /* Make sure corresponding map contains received ID: */823 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));824 825 /* Get corresponding network-request: */826 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);827 828 /* Get corresponding customer: */829 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();830 831 /* Send to customer to process: */832 pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());833 834 /* Cleanup network-request: */835 cleanupNetworkRequest(uuid);836 }837 838 /* Slot to handle network-request failure: */839 void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &)840 {841 /* Make sure corresponding map contains received ID: */842 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));843 844 /* Get corresponding network-request: */845 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);846 847 /* Get corresponding customer: */848 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();849 850 /* If customer made a force-call: */851 if (pNetworkCustomer->isItForceCall())852 {853 /* Just show the dialog: */854 show();855 }856 }857 858 #include "UINetworkManager.moc"859 -
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManagerDialog.h
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 74233
r41434 r41440 2 2 * 3 3 * VBox frontends: Qt GUI ("VirtualBox"): 4 * UINetworkManager stuff declaration4 * UINetworkManagerDialog stuff declaration 5 5 */ 6 6 … … 17 17 */ 18 18 19 #ifndef __UINetworkManager _h__20 #define __UINetworkManager _h__19 #ifndef __UINetworkManagerDialog_h__ 20 #define __UINetworkManagerDialog_h__ 21 21 22 22 /* Global includes: */ 23 #include <QNetworkAccessManager> 23 #include <QMainWindow> 24 #include <QMap> 24 25 #include <QUuid> 25 #include <QMap>26 #include <QNetworkRequest>27 #include <QNetworkReply>28 #include <QPointer>29 26 30 /* Local in ludes: */31 #include " UINetworkDefs.h"27 /* Local includes: */ 28 #include "QIWithRetranslateUI.h" 32 29 33 30 /* Forward declarations: */ 34 class UINetworkManager; 35 class UINetworkCustomer; 31 class UINetworkRequest; 32 class QVBoxLayout; 33 class QLabel; 34 class QIDialogButtonBox; 36 35 class UINetworkRequestWidget; 37 class UINetworkManagerDialog;38 36 39 /* Network request contianer: */40 class UINetwork Request : public QObject37 /* QMainWindow reimplementation to reflect network-requests: */ 38 class UINetworkManagerDialog : public QIWithRetranslateUI<QMainWindow> 41 39 { 42 40 Q_OBJECT; … … 44 42 signals: 45 43 46 /* Notifications to UINetworkManager: */ 47 void sigProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal); 48 void sigCanceled(const QUuid &uuid); 49 void sigFinished(const QUuid &uuid); 50 void sigFailed(const QUuid &uuid, const QString &strError); 44 /* Ask listener (network-manager) to cancel all network-requests: */ 45 void sigCancelNetworkRequests(); 51 46 52 /* Notifications to UINetworkRequestWidget: */ 53 void sigProgress(qint64 iReceived, qint64 iTotal); 54 void sigStarted(); 55 void sigFinished(); 56 void sigFailed(const QString &strError); 47 public slots: 57 48 58 public: 49 /* Show the dialog, make sure its visible: */ 50 void showNormal(); 59 51 60 /* Constructor/destructor: */ 61 UINetworkRequest(UINetworkManager *pNetworkManager, 62 UINetworkManagerDialog *pNetworkManagerDialog, 63 const QNetworkRequest &request, UINetworkRequestType type, 64 const QString &strDescription, UINetworkCustomer *pCustomer); 65 UINetworkRequest(UINetworkManager *pNetworkManager, 66 UINetworkManagerDialog *pNetworkManagerDialog, 67 const QList<QNetworkRequest> &requests, UINetworkRequestType type, 68 const QString &strDescription, UINetworkCustomer *pCustomer); 69 ~UINetworkRequest(); 52 protected: 70 53 71 /* Getters: */ 72 const QUuid& uuid() const { return m_uuid; } 73 const QString& description() const { return m_strDescription; } 74 UINetworkCustomer* customer() { return m_pCustomer; } 75 QNetworkReply* reply() { return m_pReply; } 54 /* Allow creation of UINetworkManagerDialog to UINetworkManager: */ 55 friend class UINetworkManager; 56 /* Constructor: */ 57 UINetworkManagerDialog(); 58 59 /* Allow adding/removing network-request widgets to UINetworkRequest: */ 60 friend class UINetworkRequest; 61 /* Add network-request widget: */ 62 UINetworkRequestWidget* addNetworkRequestWidget(UINetworkRequest *pNetworkRequest); 63 /* Remove network-request widget: */ 64 void removeNetworkRequestWidget(const QUuid &uuid); 76 65 77 66 private slots: 78 67 79 /* Network-reply progress handler: */ 80 void sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal); 81 /* Network-reply finish handler: */ 82 void sltHandleNetworkReplyFinish(); 83 84 /* Slot to retry network-request: */ 85 void sltRetry(); 86 /* Slot to cancel network-request: */ 87 void sltCancel(); 68 /* Handler for 'Cancel All' button-press: */ 69 void sltHandleCancelAllButtonPress(); 88 70 89 71 private: 90 72 91 /* Initialize: */92 void initialize();73 /* Translation stuff: */ 74 void retranslateUi(); 93 75 94 /* Prepare network-reply: */ 95 void prepareNetworkReply(); 96 /* Cleanup network-reply: */ 97 void cleanupNetworkReply(); 98 /* Abort network-reply: */ 99 void abortNetworkReply(); 76 /* Overloaded event-handlers: */ 77 void showEvent(QShowEvent *pShowEvent); 78 void keyPressEvent(QKeyEvent *pKeyPressEvent); 100 79 101 80 /* Widgets: */ 102 UINetworkManagerDialog *m_pNetworkManagerDialog; 103 UINetworkRequestWidget *m_pNetworkRequestWidget; 104 105 /* Variables: */ 106 QUuid m_uuid; 107 QList<QNetworkRequest> m_requests; 108 QNetworkRequest m_request; 109 int m_iCurrentRequestIndex; 110 UINetworkRequestType m_type; 111 QString m_strDescription; 112 UINetworkCustomer *m_pCustomer; 113 QPointer<QNetworkReply> m_pReply; 114 bool m_fRunning; 81 QLabel *m_pLabel; 82 QVBoxLayout *m_pWidgetsLayout; 83 QIDialogButtonBox *m_pButtonBox; 84 QMap<QUuid, UINetworkRequestWidget*> m_widgets; 115 85 }; 116 86 117 /* QNetworkAccessManager class reimplementation providing 118 * network access for the VirtualBox application purposes. */ 119 class UINetworkManager : public QNetworkAccessManager 120 { 121 Q_OBJECT; 87 #endif // __UINetworkManagerDialog_h__ 122 88 123 signals:124 125 /* Notification to UINetworkRequest: */126 void sigCancelNetworkRequests();127 128 public:129 130 /* Instance: */131 static UINetworkManager* instance() { return m_pInstance; }132 133 /* Create/destroy singleton: */134 static void create();135 static void destroy();136 137 /* Network Access Manager GUI window: */138 QWidget* window() const;139 140 public slots:141 142 /* Show Network Access Manager GUI: */143 void show();144 145 protected:146 147 /* Allow UINetworkCustomer to implicitly use next mothods: */148 friend class UINetworkCustomer;149 /* Network-request creation wrapper for UINetworkCustomer: */150 void createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type,151 const QString &strDescription, UINetworkCustomer *pCustomer);152 /* Network request (set) creation wrapper for UINetworkCustomer: */153 void createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type,154 const QString &strDescription, UINetworkCustomer *pCustomer);155 156 private:157 158 /* Constructor/destructor: */159 UINetworkManager();160 ~UINetworkManager();161 162 /* Prepare/cleanup: */163 void prepare();164 void cleanup();165 166 /* Network-request prepare helper: */167 void prepareNetworkRequest(UINetworkRequest *pNetworkRequest);168 /* Network-request cleanup helper: */169 void cleanupNetworkRequest(QUuid uuid);170 /* Network-requests cleanup helper: */171 void cleanupNetworkRequests();172 173 private slots:174 175 /* Slot to handle network-request progress: */176 void sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal);177 /* Slot to handle network-request cancel: */178 void sltHandleNetworkRequestCancel(const QUuid &uuid);179 /* Slot to handle network-request finish: */180 void sltHandleNetworkRequestFinish(const QUuid &uuid);181 /* Slot to handle network-request failure: */182 void sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &strError);183 184 private:185 186 /* Instance: */187 static UINetworkManager *m_pInstance;188 189 /* Network-request map: */190 QMap<QUuid, UINetworkRequest*> m_requests;191 192 /* Network manager UI: */193 UINetworkManagerDialog *m_pNetworkProgressDialog;194 };195 #define gNetworkManager UINetworkManager::instance()196 197 #endif // __UINetworkManager_h__ -
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkRequest.cpp
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 74233
r41433 r41440 3 3 * 4 4 * VBox frontends: Qt GUI ("VirtualBox"): 5 * UINetwork Managerstuff implementation5 * UINetworkRequest stuff implementation 6 6 */ 7 7 … … 19 19 20 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 21 #include <QNetworkReply> 32 22 33 23 /* Local includes: */ 24 #include "UINetworkRequest.h" 25 #include "UINetworkRequestWidget.h" 34 26 #include "UINetworkManager.h" 27 #include "UINetworkManagerDialog.h" 35 28 #include "UINetworkCustomer.h" 36 #include "QIWithRetranslateUI.h"37 29 #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 30 433 31 /* Constructor: */ … … 483 81 void UINetworkRequest::sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal) 484 82 { 485 /* Notify UINetworkManager: */83 /* Notify network-manager: */ 486 84 emit sigProgress(m_uuid, iReceived, iTotal); 487 /* Notify UINetworkRequestWidget: */85 /* Notify network-request widget: */ 488 86 emit sigProgress(iReceived, iTotal); 489 87 } … … 495 93 m_fRunning = false; 496 94 497 /* Get sender network 95 /* Get sender network-reply: */ 498 96 QNetworkReply *pNetworkReply = static_cast<QNetworkReply*>(sender()); 499 97 … … 501 99 if (pNetworkReply->error() == QNetworkReply::OperationCanceledError) 502 100 { 503 /* Notify UINetworkManager: */101 /* Notify network-manager: */ 504 102 emit sigCanceled(m_uuid); 505 103 } … … 651 249 } 652 250 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() const680 {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 /* Slot to handle network-request progress: */785 void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)786 {787 /* Make sure corresponding map contains received ID: */788 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));789 790 /* Get corresponding network-request: */791 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);792 793 /* Get corresponding customer: */794 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();795 796 /* Send to customer to process: */797 pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);798 }799 800 /* Slot to handle network-request cancel: */801 void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid)802 {803 /* Make sure corresponding map contains received ID: */804 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));805 806 /* Get corresponding network-request: */807 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);808 809 /* Get corresponding customer: */810 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();811 812 /* Send to customer to process: */813 pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());814 815 /* Cleanup network-request: */816 cleanupNetworkRequest(uuid);817 }818 819 /* Slot to handle network-request finish: */820 void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid)821 {822 /* Make sure corresponding map contains received ID: */823 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));824 825 /* Get corresponding network-request: */826 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);827 828 /* Get corresponding customer: */829 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();830 831 /* Send to customer to process: */832 pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());833 834 /* Cleanup network-request: */835 cleanupNetworkRequest(uuid);836 }837 838 /* Slot to handle network-request failure: */839 void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &)840 {841 /* Make sure corresponding map contains received ID: */842 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));843 844 /* Get corresponding network-request: */845 UINetworkRequest *pNetworkRequest = m_requests.value(uuid);846 847 /* Get corresponding customer: */848 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();849 850 /* If customer made a force-call: */851 if (pNetworkCustomer->isItForceCall())852 {853 /* Just show the dialog: */854 show();855 }856 }857 858 #include "UINetworkManager.moc"859 -
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkRequest.h
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 74233
r41433 r41440 2 2 * 3 3 * VBox frontends: Qt GUI ("VirtualBox"): 4 * UINetwork Managerstuff declaration4 * UINetworkRequest stuff declaration 5 5 */ 6 6 … … 17 17 */ 18 18 19 #ifndef __UINetwork Manager_h__20 #define __UINetwork Manager_h__19 #ifndef __UINetworkRequest_h__ 20 #define __UINetworkRequest_h__ 21 21 22 22 /* Global includes: */ 23 #include <QNetworkAccessManager>24 23 #include <QUuid> 25 #include <QMap>26 24 #include <QNetworkRequest> 27 25 #include <QNetworkReply> … … 33 31 /* Forward declarations: */ 34 32 class UINetworkManager; 33 class UINetworkManagerDialog; 34 class UINetworkRequestWidget; 35 35 class UINetworkCustomer; 36 class UINetworkRequestWidget;37 class UINetworkManagerDialog;38 36 39 /* Network 37 /* Network-request contianer: */ 40 38 class UINetworkRequest : public QObject 41 39 { … … 115 113 }; 116 114 117 /* QNetworkAccessManager class reimplementation providing 118 * network access for the VirtualBox application purposes. */ 119 class UINetworkManager : public QNetworkAccessManager 120 { 121 Q_OBJECT; 122 123 signals: 124 125 /* Notification to UINetworkRequest: */ 126 void sigCancelNetworkRequests(); 127 128 public: 129 130 /* Instance: */ 131 static UINetworkManager* instance() { return m_pInstance; } 132 133 /* Create/destroy singleton: */ 134 static void create(); 135 static void destroy(); 136 137 /* Network Access Manager GUI window: */ 138 QWidget* window() const; 139 140 public slots: 141 142 /* Show Network Access Manager GUI: */ 143 void show(); 144 145 protected: 146 147 /* Allow UINetworkCustomer to implicitly use next mothods: */ 148 friend class UINetworkCustomer; 149 /* Network-request creation wrapper for UINetworkCustomer: */ 150 void createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type, 151 const QString &strDescription, UINetworkCustomer *pCustomer); 152 /* Network request (set) creation wrapper for UINetworkCustomer: */ 153 void createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type, 154 const QString &strDescription, UINetworkCustomer *pCustomer); 155 156 private: 157 158 /* Constructor/destructor: */ 159 UINetworkManager(); 160 ~UINetworkManager(); 161 162 /* Prepare/cleanup: */ 163 void prepare(); 164 void cleanup(); 165 166 /* Network-request prepare helper: */ 167 void prepareNetworkRequest(UINetworkRequest *pNetworkRequest); 168 /* Network-request cleanup helper: */ 169 void cleanupNetworkRequest(QUuid uuid); 170 /* Network-requests cleanup helper: */ 171 void cleanupNetworkRequests(); 172 173 private slots: 174 175 /* Slot to handle network-request progress: */ 176 void sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal); 177 /* Slot to handle network-request cancel: */ 178 void sltHandleNetworkRequestCancel(const QUuid &uuid); 179 /* Slot to handle network-request finish: */ 180 void sltHandleNetworkRequestFinish(const QUuid &uuid); 181 /* Slot to handle network-request failure: */ 182 void sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &strError); 183 184 private: 185 186 /* Instance: */ 187 static UINetworkManager *m_pInstance; 188 189 /* Network-request map: */ 190 QMap<QUuid, UINetworkRequest*> m_requests; 191 192 /* Network manager UI: */ 193 UINetworkManagerDialog *m_pNetworkProgressDialog; 194 }; 195 #define gNetworkManager UINetworkManager::instance() 196 197 #endif // __UINetworkManager_h__ 115 #endif // __UINetworkRequest_h__ -
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkRequestWidget.cpp
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.cpp 74233
r41435 r41440 3 3 * 4 4 * VBox frontends: Qt GUI ("VirtualBox"): 5 * UINetwork Managerstuff implementation5 * UINetworkRequestWidget stuff implementation 6 6 */ 7 7 … … 19 19 20 20 /* Global includes: */ 21 #include <QWidget>22 21 #include <QTimer> 23 22 #include <QGridLayout> 24 23 #include <QProgressBar> 25 #include <QMainWindow>26 #include <QVBoxLayout>27 #include <QLabel>28 #include <QPushButton>29 #include <QStatusBar>30 #include <QKeyEvent>31 #include <QNetworkReply>32 24 33 25 /* Local includes: */ 26 #include "UINetworkRequestWidget.h" 27 #include "UINetworkRequest.h" 34 28 #include "UINetworkManager.h" 35 #include "UINetworkCustomer.h" 36 #include "QIWithRetranslateUI.h" 37 #include "VBoxGlobal.h" 38 #include "UIMessageCenter.h" 29 #include "UINetworkManagerDialog.h" 39 30 #include "UIIconPool.h" 40 #include "QIDialogButtonBox.h"41 #include "UIPopupBox.h"42 31 #include "QIToolButton.h" 43 32 #include "QIRichTextLabel.h" 44 33 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)) 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(":/nw_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, SIGNAL(sigProgress(qint64, qint64)), this, SLOT(sltSetProgress(qint64, qint64))); 53 connect(m_pNetworkRequest, SIGNAL(sigStarted()), this, SLOT(sltSetProgressToStarted())); 54 connect(m_pNetworkRequest, SIGNAL(sigFinished()), this, SLOT(sltSetProgressToFinished())); 55 connect(m_pNetworkRequest, SIGNAL(sigFailed(const QString&)), this, SLOT(sltSetProgressToFailed(const QString&))); 56 57 /* Setup timer: */ 58 m_pTimer->setInterval(5000); 59 connect(m_pTimer, SIGNAL(timeout()), this, SLOT(sltTimeIsOut())); 60 61 /* Setup main-layout: */ 62 m_pMainLayout->setContentsMargins(6, 6, 6, 6); 63 64 /* Setup progress-bar: */ 65 m_pProgressBar->setRange(0, 0); 66 m_pProgressBar->setMaximumHeight(16); 67 68 /* Setup retry-button: */ 69 m_pRetryButton->setHidden(true); 70 m_pRetryButton->removeBorder(); 71 m_pRetryButton->setFocusPolicy(Qt::NoFocus); 72 m_pRetryButton->setIcon(UIIconPool::iconSet(":/refresh_16px.png")); 73 connect(m_pRetryButton, SIGNAL(clicked(bool)), this, SIGNAL(sigRetry())); 74 75 /* Setup cancel-button: */ 76 m_pCancelButton->removeBorder(); 77 m_pCancelButton->setFocusPolicy(Qt::NoFocus); 78 m_pCancelButton->setIcon(UIIconPool::iconSet(":/delete_16px.png")); 79 connect(m_pCancelButton, SIGNAL(clicked(bool)), this, SIGNAL(sigCancel())); 80 81 /* Setup error-label: */ 82 m_pErrorPane->setHidden(true); 83 m_pErrorPane->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); 84 /* Calculate required width: */ 85 int iMinimumWidth = pParent->minimumWidth(); 86 int iLeft, iTop, iRight, iBottom; 87 /* Take into account content-widget layout margins: */ 88 m_pMainLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom); 89 iMinimumWidth -= iLeft; 90 iMinimumWidth -= iRight; 91 /* Take into account this layout margins: */ 92 layout()->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom); 93 iMinimumWidth -= iLeft; 94 iMinimumWidth -= iRight; 95 /* Take into account parent layout margins: */ 96 QLayout *pParentLayout = qobject_cast<QMainWindow*>(parent())->centralWidget()->layout(); 97 pParentLayout->getContentsMargins(&iLeft, &iTop, &iRight, &iBottom); 98 iMinimumWidth -= iLeft; 99 iMinimumWidth -= iRight; 100 /* Set minimum text width: */ 101 m_pErrorPane->setMinimumTextWidth(iMinimumWidth); 102 103 /* Layout content: */ 104 m_pMainLayout->addWidget(m_pProgressBar, 0, 0); 105 m_pMainLayout->addWidget(m_pRetryButton, 0, 1); 106 m_pMainLayout->addWidget(m_pCancelButton, 0, 2); 107 m_pMainLayout->addWidget(m_pErrorPane, 1, 0, 1, 3); 108 109 /* Retranslate UI: */ 110 retranslateUi(); 111 } 112 113 void UINetworkRequestWidget::sltSetProgress(qint64 iReceived, qint64 iTotal) 114 { 115 /* Restart timer: */ 116 m_pTimer->start(); 117 118 /* Set current progress to passed: */ 119 m_pProgressBar->setRange(0, iTotal); 120 m_pProgressBar->setValue(iReceived); 121 } 122 123 void UINetworkRequestWidget::sltSetProgressToStarted() 124 { 125 /* Start timer: */ 126 m_pTimer->start(); 127 128 /* Set current progress to 'started': */ 129 m_pProgressBar->setRange(0, 1); 130 m_pProgressBar->setValue(0); 131 132 /* Hide 'retry' button: */ 133 m_pRetryButton->setHidden(true); 134 135 /* Hide error label: */ 136 m_pErrorPane->setHidden(true); 137 m_pErrorPane->setText(QString()); 138 } 139 140 void UINetworkRequestWidget::sltSetProgressToFinished() 141 { 142 /* Stop timer: */ 143 m_pTimer->stop(); 144 145 /* Set current progress to 'started': */ 146 m_pProgressBar->setRange(0, 1); 147 m_pProgressBar->setValue(1); 148 } 149 150 void UINetworkRequestWidget::sltSetProgressToFailed(const QString &strError) 151 { 152 /* Stop timer: */ 153 m_pTimer->stop(); 154 155 /* Set current progress to 'failed': */ 156 m_pProgressBar->setRange(0, 1); 157 m_pProgressBar->setValue(1); 158 159 /* Show 'retry' button: */ 160 m_pRetryButton->setHidden(false); 161 162 /* Try to find all the links in the error-message, 163 * replace them with %increment if present: */ 164 QString strErrorText(strError); 165 QRegExp linkRegExp("[\\S]+[\\./][\\S]+"); 166 QStringList links; 167 for (int i = 1; linkRegExp.indexIn(strErrorText) != -1; ++i) 70 168 { 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(); 169 links << linkRegExp.cap(); 170 strErrorText.replace(linkRegExp.cap(), QString("%%1").arg(i)); 137 171 } 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 /* Slot to handle network-request progress: */ 785 void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal) 786 { 787 /* Make sure corresponding map contains received ID: */ 788 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n")); 789 790 /* Get corresponding network-request: */ 791 UINetworkRequest *pNetworkRequest = m_requests.value(uuid); 792 793 /* Get corresponding customer: */ 794 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer(); 795 796 /* Send to customer to process: */ 797 pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal); 798 } 799 800 /* Slot to handle network-request cancel: */ 801 void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid) 802 { 803 /* Make sure corresponding map contains received ID: */ 804 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n")); 805 806 /* Get corresponding network-request: */ 807 UINetworkRequest *pNetworkRequest = m_requests.value(uuid); 808 809 /* Get corresponding customer: */ 810 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer(); 811 812 /* Send to customer to process: */ 813 pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply()); 814 815 /* Cleanup network-request: */ 816 cleanupNetworkRequest(uuid); 817 } 818 819 /* Slot to handle network-request finish: */ 820 void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid) 821 { 822 /* Make sure corresponding map contains received ID: */ 823 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n")); 824 825 /* Get corresponding network-request: */ 826 UINetworkRequest *pNetworkRequest = m_requests.value(uuid); 827 828 /* Get corresponding customer: */ 829 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer(); 830 831 /* Send to customer to process: */ 832 pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply()); 833 834 /* Cleanup network-request: */ 835 cleanupNetworkRequest(uuid); 836 } 837 838 /* Slot to handle network-request failure: */ 839 void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &) 840 { 841 /* Make sure corresponding map contains received ID: */ 842 AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n")); 843 844 /* Get corresponding network-request: */ 845 UINetworkRequest *pNetworkRequest = m_requests.value(uuid); 846 847 /* Get corresponding customer: */ 848 UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer(); 849 850 /* If customer made a force-call: */ 851 if (pNetworkCustomer->isItForceCall()) 852 { 853 /* Just show the dialog: */ 854 show(); 855 } 856 } 857 858 #include "UINetworkManager.moc" 859 172 /* Return back all the links, just in bold: */ 173 if (!links.isEmpty()) 174 for (int i = 0; i < links.size(); ++i) 175 strErrorText = strErrorText.arg(QString("<b>%1</b>").arg(links[i])); 176 177 /* Show error label: */ 178 m_pErrorPane->setHidden(false); 179 m_pErrorPane->setText(UINetworkManagerDialog::tr("Error: %1.").arg(strErrorText)); 180 } 181 182 void UINetworkRequestWidget::sltTimeIsOut() 183 { 184 /* Stop timer: */ 185 m_pTimer->stop(); 186 187 /* Set current progress to unknown: */ 188 m_pProgressBar->setRange(0, 0); 189 } 190 191 void UINetworkRequestWidget::retranslateUi() 192 { 193 /* Get corresponding title: */ 194 const QString &strTitle = m_pNetworkRequest->description(); 195 196 /* Set popup title (default if missed): */ 197 setTitle(strTitle.isEmpty() ? UINetworkManagerDialog::tr("Network Operation") : strTitle); 198 199 /* Translate retry button: */ 200 m_pRetryButton->setStatusTip(UINetworkManagerDialog::tr("Restart network operation")); 201 202 /* Translate cancel button: */ 203 m_pCancelButton->setStatusTip(UINetworkManagerDialog::tr("Cancel network operation")); 204 } -
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkRequestWidget.h
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/VBox-3.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 58652,70973 /branches/VBox-3.2/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 66309,66318 /branches/VBox-4.0/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 70873 /branches/VBox-4.1/src/VBox/Frontends/VirtualBox/src/net/UINetworkManager.h 74233
r41435 r41440 2 2 * 3 3 * VBox frontends: Qt GUI ("VirtualBox"): 4 * UINetwork Managerstuff declaration4 * UINetworkRequestWidget stuff declaration 5 5 */ 6 6 … … 17 17 */ 18 18 19 #ifndef __UINetworkManager_h__ 20 #define __UINetworkManager_h__ 21 22 /* Global includes: */ 23 #include <QNetworkAccessManager> 24 #include <QUuid> 25 #include <QMap> 26 #include <QNetworkRequest> 27 #include <QNetworkReply> 28 #include <QPointer> 19 #ifndef __UINetworkRequestWidget_h__ 20 #define __UINetworkRequestWidget_h__ 29 21 30 22 /* Local inludes: */ 31 #include "UINetworkDefs.h" 23 #include "QIWithRetranslateUI.h" 24 #include "UIPopupBox.h" 32 25 33 26 /* Forward declarations: */ 34 class UINetworkManager;35 class UINetworkCustomer;36 class UINetworkRequestWidget;37 27 class UINetworkManagerDialog; 28 class QWidget; 29 class QGridLayout; 30 class QProgressBar; 31 class QIToolButton; 32 class QIRichTextLabel; 33 class UINetworkRequest; 34 class QTimer; 38 35 39 /* Network request contianer: */40 class UINetworkRequest : public QObject36 /* UIPopupBox reimplementation to reflect network-request status: */ 37 class UINetworkRequestWidget : public QIWithRetranslateUI<UIPopupBox> 41 38 { 42 39 Q_OBJECT; … … 44 41 signals: 45 42 46 /* Notifications to UINetworkManager: */ 47 void sigProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal); 48 void sigCanceled(const QUuid &uuid); 49 void sigFinished(const QUuid &uuid); 50 void sigFailed(const QUuid &uuid, const QString &strError); 43 /* Signal to retry network-request: */ 44 void sigRetry(); 45 /* Signal to cancel network-request: */ 46 void sigCancel(); 51 47 52 /* Notifications to UINetworkRequestWidget: */ 53 void sigProgress(qint64 iReceived, qint64 iTotal); 54 void sigStarted(); 55 void sigFinished(); 56 void sigFailed(const QString &strError); 48 protected: 57 49 58 public: 59 60 /* Constructor/destructor: */ 61 UINetworkRequest(UINetworkManager *pNetworkManager, 62 UINetworkManagerDialog *pNetworkManagerDialog, 63 const QNetworkRequest &request, UINetworkRequestType type, 64 const QString &strDescription, UINetworkCustomer *pCustomer); 65 UINetworkRequest(UINetworkManager *pNetworkManager, 66 UINetworkManagerDialog *pNetworkManagerDialog, 67 const QList<QNetworkRequest> &requests, UINetworkRequestType type, 68 const QString &strDescription, UINetworkCustomer *pCustomer); 69 ~UINetworkRequest(); 70 71 /* Getters: */ 72 const QUuid& uuid() const { return m_uuid; } 73 const QString& description() const { return m_strDescription; } 74 UINetworkCustomer* customer() { return m_pCustomer; } 75 QNetworkReply* reply() { return m_pReply; } 50 /* Allow creation of UINetworkRequestWidget to UINetworkManagerDialog only: */ 51 friend class UINetworkManagerDialog; 52 /* Constructor: */ 53 UINetworkRequestWidget(UINetworkManagerDialog *pParent, UINetworkRequest *pNetworkRequest); 76 54 77 55 private slots: 78 56 79 /* Network-reply progress handler: */ 80 void sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal); 81 /* Network-reply finish handler: */ 82 void sltHandleNetworkReplyFinish(); 57 /* Updates current network-request progess: */ 58 void sltSetProgress(qint64 iReceived, qint64 iTotal); 83 59 84 /* Slot to retry network-request: */ 85 void sltRetry(); 86 /* Slot to cancel network-request: */ 87 void sltCancel(); 60 /* Set current network-request progress to 'started': */ 61 void sltSetProgressToStarted(); 62 /* Set current network-request progress to 'finished': */ 63 void sltSetProgressToFinished(); 64 /* Set current network-request progress to 'failed': */ 65 void sltSetProgressToFailed(const QString &strError); 66 67 /* Handle frozen progress: */ 68 void sltTimeIsOut(); 88 69 89 70 private: 90 71 91 /* Initialize: */ 92 void initialize(); 93 94 /* Prepare network-reply: */ 95 void prepareNetworkReply(); 96 /* Cleanup network-reply: */ 97 void cleanupNetworkReply(); 98 /* Abort network-reply: */ 99 void abortNetworkReply(); 72 /* Translation stuff: */ 73 void retranslateUi(); 100 74 101 75 /* Widgets: */ 102 UINetworkManagerDialog *m_pNetworkManagerDialog; 103 UINetworkRequestWidget *m_pNetworkRequestWidget; 76 QWidget *m_pContentWidget; 77 QGridLayout *m_pMainLayout; 78 QProgressBar *m_pProgressBar; 79 QIToolButton *m_pRetryButton; 80 QIToolButton *m_pCancelButton; 81 QIRichTextLabel *m_pErrorPane; 104 82 105 /* Variables: */ 106 QUuid m_uuid; 107 QList<QNetworkRequest> m_requests; 108 QNetworkRequest m_request; 109 int m_iCurrentRequestIndex; 110 UINetworkRequestType m_type; 111 QString m_strDescription; 112 UINetworkCustomer *m_pCustomer; 113 QPointer<QNetworkReply> m_pReply; 114 bool m_fRunning; 83 /* Objects: */ 84 UINetworkRequest *m_pNetworkRequest; 85 QTimer *m_pTimer; 115 86 }; 116 87 117 /* QNetworkAccessManager class reimplementation providing 118 * network access for the VirtualBox application purposes. */ 119 class UINetworkManager : public QNetworkAccessManager 120 { 121 Q_OBJECT; 88 #endif // __UINetworkRequestWidget_h__ 122 89 123 signals:124 125 /* Notification to UINetworkRequest: */126 void sigCancelNetworkRequests();127 128 public:129 130 /* Instance: */131 static UINetworkManager* instance() { return m_pInstance; }132 133 /* Create/destroy singleton: */134 static void create();135 static void destroy();136 137 /* Network Access Manager GUI window: */138 QWidget* window() const;139 140 public slots:141 142 /* Show Network Access Manager GUI: */143 void show();144 145 protected:146 147 /* Allow UINetworkCustomer to implicitly use next mothods: */148 friend class UINetworkCustomer;149 /* Network-request creation wrapper for UINetworkCustomer: */150 void createNetworkRequest(const QNetworkRequest &request, UINetworkRequestType type,151 const QString &strDescription, UINetworkCustomer *pCustomer);152 /* Network request (set) creation wrapper for UINetworkCustomer: */153 void createNetworkRequest(const QList<QNetworkRequest> &requests, UINetworkRequestType type,154 const QString &strDescription, UINetworkCustomer *pCustomer);155 156 private:157 158 /* Constructor/destructor: */159 UINetworkManager();160 ~UINetworkManager();161 162 /* Prepare/cleanup: */163 void prepare();164 void cleanup();165 166 /* Network-request prepare helper: */167 void prepareNetworkRequest(UINetworkRequest *pNetworkRequest);168 /* Network-request cleanup helper: */169 void cleanupNetworkRequest(QUuid uuid);170 /* Network-requests cleanup helper: */171 void cleanupNetworkRequests();172 173 private slots:174 175 /* Slot to handle network-request progress: */176 void sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal);177 /* Slot to handle network-request cancel: */178 void sltHandleNetworkRequestCancel(const QUuid &uuid);179 /* Slot to handle network-request finish: */180 void sltHandleNetworkRequestFinish(const QUuid &uuid);181 /* Slot to handle network-request failure: */182 void sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &strError);183 184 private:185 186 /* Instance: */187 static UINetworkManager *m_pInstance;188 189 /* Network-request map: */190 QMap<QUuid, UINetworkRequest*> m_requests;191 192 /* Network manager UI: */193 UINetworkManagerDialog *m_pNetworkProgressDialog;194 };195 #define gNetworkManager UINetworkManager::instance()196 197 #endif // __UINetworkManager_h__ -
Property svn:mergeinfo
set to (toggle deleted branches)
Note:
See TracChangeset
for help on using the changeset viewer.