Vidalia 0.2.12

UPNPTestDialog.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to
00008 **  the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file UPNPTestDialog.cpp
00013 ** \brief Dialog that displays the progress of a UPnP configuration test
00014 */
00015 
00016 #include "UPNPTestDialog.h"
00017 
00018 
00019 /** Default constructor. <b>orPort</b> and <b>dirPort</b> specify the ports
00020  * used to test UPnP port forwarding. The original UPnP state will be restored
00021  * when the test dialog is closed. */
00022 UPNPTestDialog::UPNPTestDialog(quint16 orPort, quint16 dirPort, QWidget *parent)
00023   : QDialog(parent), _orPort(orPort), _dirPort(dirPort)
00024 {
00025   ui.setupUi(this);
00026   _upnp = UPNPControl::instance();
00027 
00028   ui.buttonBox->setStandardButtons(QDialogButtonBox::Close
00029                                      | QDialogButtonBox::Help);
00030   
00031   ui.progressBar->setValue(0);
00032   ui.progressBar->setFormat("");
00033   ui.progressBar->setMinimum(0);
00034   ui.progressBar->setMaximum(_upnp->discoverTimeout()/500 + 4);
00035 
00036   _discoverTimer.setInterval(500);
00037   connect(&_discoverTimer, SIGNAL(timeout()), this, SLOT(discoverTimeout()));
00038   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)),
00039           this, SLOT(clicked(QAbstractButton*)));
00040 
00041   _upnp->getDesiredState(&_oldDirPort, &_oldOrPort);
00042 }
00043 
00044 /** Shows or hides the dialog based on <b>visible</b>. The UPnP test will be
00045  * started when the dialog is first shown. */
00046 void
00047 UPNPTestDialog::setVisible(bool visible)
00048 {
00049   QWidget::setVisible(visible);
00050 
00051   if (visible)
00052     startTest();
00053   else
00054     _upnp->setDesiredState(_oldDirPort, _oldOrPort);
00055 }
00056 
00057 /** Initiates a UPnP test. */
00058 void
00059 UPNPTestDialog::startTest()
00060 {
00061   ui.buttonBox->setEnabled(false);
00062   ui.progressBar->setValue(0);
00063 
00064   connect(UPNPControl::instance(), SIGNAL(stateChanged(UPNPControl::UPNPState)),
00065           this, SLOT(upnpStateChanged(UPNPControl::UPNPState)));
00066   
00067   UPNPControl::instance()->setDesiredState(_dirPort, _orPort);  
00068 }
00069 
00070 /** Called when the UPnP test successfully enables port forwarding. Enables
00071  * the Close button, allowing the user to exit the test dialog. */
00072 void
00073 UPNPTestDialog::testSuccessful()
00074 {
00075   ui.buttonBox->setEnabled(true);
00076   ui.buttonBox->setStandardButtons(QDialogButtonBox::Close
00077                                      | QDialogButtonBox::Help);
00078 
00079   disconnect(UPNPControl::instance(), 0, this, 0);
00080 }
00081 
00082 /** Called when the UPnP test fails due to an error. Enables the Close and
00083  * Retry buttons, allowing the user to either rerun the test or give up. */
00084 void
00085 UPNPTestDialog::testFailed()
00086 {
00087   ui.buttonBox->setEnabled(true);
00088   ui.buttonBox->setStandardButtons(QDialogButtonBox::Retry
00089                                      | QDialogButtonBox::Close
00090                                      | QDialogButtonBox::Help);
00091   
00092   disconnect(UPNPControl::instance(), 0, this, 0);
00093 }
00094 
00095 /** Updates the progress bar to indicate the device discovery portion of the
00096  * test is still in progress. */
00097 void
00098 UPNPTestDialog::discoverTimeout()
00099 {
00100   ui.progressBar->setValue(ui.progressBar->value()+1);
00101 }
00102 
00103 /** Updates the test UI based on the UPnP <b>state</b>. */
00104 void
00105 UPNPTestDialog::upnpStateChanged(UPNPControl::UPNPState state)
00106 {
00107   switch (state) {
00108     case UPNPControl::DiscoverState:
00109       _discoverTimer.start();
00110       ui.progressBar->setValue(ui.progressBar->value()+1);
00111       ui.lblCurrentState->setText(tr("Discovering UPnP-enabled devices"));
00112       break;
00113 
00114     case UPNPControl::UpdatingDirPortState:
00115       ui.progressBar->setValue(ui.progressBar->value()+1);
00116       ui.lblCurrentState->setText(tr("Updating directory port mapping"));
00117       break;
00118 
00119     case UPNPControl::UpdatingORPortState:
00120       ui.progressBar->setValue(ui.progressBar->value()+1);
00121       ui.lblCurrentState->setText(tr("Updating relay port mapping"));
00122       break;
00123 
00124     case UPNPControl::ForwardingCompleteState:
00125       ui.progressBar->setValue(ui.progressBar->maximum());
00126       ui.lblCurrentState->setText(tr("Test completed successfully!"));
00127       testSuccessful();
00128       break;
00129 
00130     case UPNPControl::ErrorState:
00131       ui.progressBar->setValue(ui.progressBar->maximum());
00132       ui.lblCurrentState->setText(UPNPControl::instance()->errorString());
00133       testFailed();
00134       break;
00135 
00136     default:
00137       break;
00138   }
00139   if (state != UPNPControl::DiscoverState)
00140     _discoverTimer.stop();
00141 }
00142 
00143 /** Called when a user clicks on a button in the dialog's button box. If Retry
00144  * is clicked, another UPnP test will be conducted. If Close is clicked, then
00145  * the dialog is closed and the original UPnP state restored. */
00146 void
00147 UPNPTestDialog::clicked(QAbstractButton *button)
00148 {
00149   switch (ui.buttonBox->standardButton(button)) {
00150     case QDialogButtonBox::Retry:
00151       startTest();
00152       break;
00153 
00154     case QDialogButtonBox::Close:
00155       done(0);
00156       break;
00157 
00158     case QDialogButtonBox::Help:
00159       emit help();
00160       done(0);
00161       break;
00162 
00163     default:
00164       break;
00165   }
00166 }
00167