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