Vidalia  0.3.1
UPNPTestDialog.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file UPNPTestDialog.cpp
13 ** \brief Dialog that displays the progress of a UPnP configuration test
14 */
15 
16 #include "UPNPTestDialog.h"
17 
18 
19 /** Default constructor. <b>orPort</b> and <b>dirPort</b> specify the ports
20  * used to test UPnP port forwarding. The original UPnP state will be restored
21  * when the test dialog is closed. */
22 UPNPTestDialog::UPNPTestDialog(quint16 orPort, quint16 dirPort, QWidget *parent)
23  : QDialog(parent), _orPort(orPort), _dirPort(dirPort)
24 {
25  ui.setupUi(this);
27 
28  ui.buttonBox->setStandardButtons(QDialogButtonBox::Close
29  | QDialogButtonBox::Help);
30 
31  ui.progressBar->setValue(0);
32  ui.progressBar->setFormat("");
33  ui.progressBar->setMinimum(0);
34  ui.progressBar->setMaximum(_upnp->discoverTimeout()/500 + 4);
35 
36  _discoverTimer.setInterval(500);
37  connect(&_discoverTimer, SIGNAL(timeout()), this, SLOT(discoverTimeout()));
38  connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)),
39  this, SLOT(clicked(QAbstractButton*)));
40 
42 }
43 
44 /** Shows or hides the dialog based on <b>visible</b>. The UPnP test will be
45  * started when the dialog is first shown. */
46 void
48 {
49  QWidget::setVisible(visible);
50 
51  if (visible)
52  startTest();
53  else
55 }
56 
57 /** Initiates a UPnP test. */
58 void
60 {
61  ui.buttonBox->setEnabled(false);
62  ui.progressBar->setValue(0);
63 
64  connect(UPNPControl::instance(), SIGNAL(stateChanged(UPNPControl::UPNPState)),
66 
68 }
69 
70 /** Called when the UPnP test successfully enables port forwarding. Enables
71  * the Close button, allowing the user to exit the test dialog. */
72 void
74 {
75  ui.buttonBox->setEnabled(true);
76  ui.buttonBox->setStandardButtons(QDialogButtonBox::Close
77  | QDialogButtonBox::Help);
78 
79  disconnect(UPNPControl::instance(), 0, this, 0);
80 }
81 
82 /** Called when the UPnP test fails due to an error. Enables the Close and
83  * Retry buttons, allowing the user to either rerun the test or give up. */
84 void
86 {
87  ui.buttonBox->setEnabled(true);
88  ui.buttonBox->setStandardButtons(QDialogButtonBox::Retry
89  | QDialogButtonBox::Close
90  | QDialogButtonBox::Help);
91 
92  disconnect(UPNPControl::instance(), 0, this, 0);
93 }
94 
95 /** Updates the progress bar to indicate the device discovery portion of the
96  * test is still in progress. */
97 void
99 {
100  ui.progressBar->setValue(ui.progressBar->value()+1);
101 }
102 
103 /** Updates the test UI based on the UPnP <b>state</b>. */
104 void
106 {
107  switch (state) {
109  _discoverTimer.start();
110  ui.progressBar->setValue(ui.progressBar->value()+1);
111  ui.lblCurrentState->setText(tr("Discovering UPnP-enabled devices"));
112  break;
113 
115  ui.progressBar->setValue(ui.progressBar->value()+1);
116  ui.lblCurrentState->setText(tr("Updating directory port mapping"));
117  break;
118 
120  ui.progressBar->setValue(ui.progressBar->value()+1);
121  ui.lblCurrentState->setText(tr("Updating relay port mapping"));
122  break;
123 
125  ui.progressBar->setValue(ui.progressBar->maximum());
126  ui.lblCurrentState->setText(tr("Test completed successfully!"));
127  testSuccessful();
128  break;
129 
131  ui.progressBar->setValue(ui.progressBar->maximum());
132  ui.lblCurrentState->setText(UPNPControl::instance()->errorString());
133  testFailed();
134  break;
135 
136  default:
137  break;
138  }
139  if (state != UPNPControl::DiscoverState)
140  _discoverTimer.stop();
141 }
142 
143 /** Called when a user clicks on a button in the dialog's button box. If Retry
144  * is clicked, another UPnP test will be conducted. If Close is clicked, then
145  * the dialog is closed and the original UPnP state restored. */
146 void
147 UPNPTestDialog::clicked(QAbstractButton *button)
148 {
149  switch (ui.buttonBox->standardButton(button)) {
150  case QDialogButtonBox::Retry:
151  startTest();
152  break;
153 
154  case QDialogButtonBox::Close:
155  done(0);
156  break;
157 
158  case QDialogButtonBox::Help:
159  emit help();
160  done(0);
161  break;
162 
163  default:
164  break;
165  }
166 }
167 
UPNPTestDialog::_dirPort
quint16 _dirPort
Definition: UPNPTestDialog.h:82
UPNPTestDialog::_oldOrPort
quint16 _oldOrPort
Definition: UPNPTestDialog.h:79
UPNPControl::instance
static UPNPControl * instance()
Definition: UPNPControl.cpp:31
UPNPControl::ForwardingCompleteState
@ ForwardingCompleteState
Definition: UPNPControl.h:49
UPNPTestDialog::clicked
void clicked(QAbstractButton *button)
Definition: UPNPTestDialog.cpp:147
UPNPControl::UPNPState
UPNPState
Definition: UPNPControl.h:43
UPNPControl::UpdatingDirPortState
@ UpdatingDirPortState
Definition: UPNPControl.h:48
UPNPControl::discoverTimeout
int discoverTimeout() const
Definition: UPNPControl.cpp:160
UPNPTestDialog::startTest
void startTest()
Definition: UPNPTestDialog.cpp:59
UPNPTestDialog::testFailed
void testFailed()
Definition: UPNPTestDialog.cpp:85
UPNPTestDialog::UPNPTestDialog
UPNPTestDialog(quint16 orPort, quint16 dirPort, QWidget *parent=0)
Definition: UPNPTestDialog.cpp:22
UPNPTestDialog::_oldDirPort
quint16 _oldDirPort
Definition: UPNPTestDialog.h:80
UPNPTestDialog::setVisible
void setVisible(bool visible)
Definition: UPNPTestDialog.cpp:47
UPNPControl::ErrorState
@ ErrorState
Definition: UPNPControl.h:45
UPNPTestDialog::upnpStateChanged
void upnpStateChanged(UPNPControl::UPNPState state)
Definition: UPNPTestDialog.cpp:105
UPNPControl::UpdatingORPortState
@ UpdatingORPortState
Definition: UPNPControl.h:47
UPNPTestDialog::discoverTimeout
void discoverTimeout()
Definition: UPNPTestDialog.cpp:98
UPNPTestDialog::ui
Ui::UPNPTestDialog ui
Definition: UPNPTestDialog.h:84
connect
stop errmsg connect(const QHostAddress &address, quint16 port)
UPNPControl::DiscoverState
@ DiscoverState
Definition: UPNPControl.h:46
UPNPTestDialog::testSuccessful
void testSuccessful()
Definition: UPNPTestDialog.cpp:73
UPNPTestDialog::_discoverTimer
QTimer _discoverTimer
Definition: UPNPTestDialog.h:77
UPNPControl::getDesiredState
void getDesiredState(quint16 *desiredDirPort, quint16 *desiredOrPort)
Definition: UPNPControl.cpp:78
disconnect
stop errmsg disconnect()
UPNPTestDialog::_upnp
UPNPControl * _upnp
Definition: UPNPTestDialog.h:73
UPNPTestDialog::_orPort
quint16 _orPort
Definition: UPNPTestDialog.h:81
UPNPTestDialog.h
UPNPControl::setDesiredState
void setDesiredState(quint16 desiredDirPort, quint16 desiredOrPort)
Definition: UPNPControl.cpp:89
UPNPTestDialog::help
void help()