author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
#include <QtNetwork> |
|
44 |
||
45 |
#include "blockingclient.h" |
|
46 |
||
47 |
BlockingClient::BlockingClient(QWidget *parent) |
|
48 |
: QDialog(parent) |
|
49 |
{ |
|
50 |
hostLabel = new QLabel(tr("&Server name:")); |
|
51 |
portLabel = new QLabel(tr("S&erver port:")); |
|
52 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
// find out which IP to connect to |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
QString ipAddress; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
// use the first non-localhost IPv4 address |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
for (int i = 0; i < ipAddressesList.size(); ++i) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
if (ipAddressesList.at(i) != QHostAddress::LocalHost && |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
59 |
ipAddressesList.at(i).toIPv4Address()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
ipAddress = ipAddressesList.at(i).toString(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
61 |
break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
// if we did not find one, use IPv4 localhost |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
if (ipAddress.isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
hostLineEdit = new QLineEdit(ipAddress); |
0 | 69 |
portLineEdit = new QLineEdit; |
70 |
portLineEdit->setValidator(new QIntValidator(1, 65535, this)); |
|
71 |
||
72 |
hostLabel->setBuddy(hostLineEdit); |
|
73 |
portLabel->setBuddy(portLineEdit); |
|
74 |
||
75 |
statusLabel = new QLabel(tr("This examples requires that you run the " |
|
76 |
"Fortune Server example as well.")); |
|
77 |
||
78 |
getFortuneButton = new QPushButton(tr("Get Fortune")); |
|
79 |
getFortuneButton->setDefault(true); |
|
80 |
getFortuneButton->setEnabled(false); |
|
81 |
||
82 |
quitButton = new QPushButton(tr("Quit")); |
|
83 |
||
84 |
buttonBox = new QDialogButtonBox; |
|
85 |
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); |
|
86 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
|
87 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
connect(hostLineEdit, SIGNAL(textChanged(QString)), |
0 | 89 |
this, SLOT(enableGetFortuneButton())); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
connect(portLineEdit, SIGNAL(textChanged(QString)), |
0 | 91 |
this, SLOT(enableGetFortuneButton())); |
92 |
connect(getFortuneButton, SIGNAL(clicked()), |
|
93 |
this, SLOT(requestNewFortune())); |
|
94 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
|
95 |
//! [0] |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
connect(&thread, SIGNAL(newFortune(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
this, SLOT(showFortune(QString))); |
0 | 98 |
//! [0] //! [1] |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
connect(&thread, SIGNAL(error(int,QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
this, SLOT(displayError(int,QString))); |
0 | 101 |
//! [1] |
102 |
||
103 |
QGridLayout *mainLayout = new QGridLayout; |
|
104 |
mainLayout->addWidget(hostLabel, 0, 0); |
|
105 |
mainLayout->addWidget(hostLineEdit, 0, 1); |
|
106 |
mainLayout->addWidget(portLabel, 1, 0); |
|
107 |
mainLayout->addWidget(portLineEdit, 1, 1); |
|
108 |
mainLayout->addWidget(statusLabel, 2, 0, 1, 2); |
|
109 |
mainLayout->addWidget(buttonBox, 3, 0, 1, 2); |
|
110 |
setLayout(mainLayout); |
|
111 |
||
112 |
setWindowTitle(tr("Blocking Fortune Client")); |
|
113 |
portLineEdit->setFocus(); |
|
114 |
} |
|
115 |
||
116 |
//! [2] |
|
117 |
void BlockingClient::requestNewFortune() |
|
118 |
{ |
|
119 |
getFortuneButton->setEnabled(false); |
|
120 |
thread.requestNewFortune(hostLineEdit->text(), |
|
121 |
portLineEdit->text().toInt()); |
|
122 |
} |
|
123 |
//! [2] |
|
124 |
||
125 |
//! [3] |
|
126 |
void BlockingClient::showFortune(const QString &nextFortune) |
|
127 |
{ |
|
128 |
if (nextFortune == currentFortune) { |
|
129 |
requestNewFortune(); |
|
130 |
return; |
|
131 |
} |
|
132 |
//! [3] |
|
133 |
||
134 |
//! [4] |
|
135 |
currentFortune = nextFortune; |
|
136 |
statusLabel->setText(currentFortune); |
|
137 |
getFortuneButton->setEnabled(true); |
|
138 |
} |
|
139 |
//! [4] |
|
140 |
||
141 |
void BlockingClient::displayError(int socketError, const QString &message) |
|
142 |
{ |
|
143 |
switch (socketError) { |
|
144 |
case QAbstractSocket::HostNotFoundError: |
|
145 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
146 |
tr("The host was not found. Please check the " |
|
147 |
"host and port settings.")); |
|
148 |
break; |
|
149 |
case QAbstractSocket::ConnectionRefusedError: |
|
150 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
151 |
tr("The connection was refused by the peer. " |
|
152 |
"Make sure the fortune server is running, " |
|
153 |
"and check that the host name and port " |
|
154 |
"settings are correct.")); |
|
155 |
break; |
|
156 |
default: |
|
157 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
158 |
tr("The following error occurred: %1.") |
|
159 |
.arg(message)); |
|
160 |
} |
|
161 |
||
162 |
getFortuneButton->setEnabled(true); |
|
163 |
} |
|
164 |
||
165 |
void BlockingClient::enableGetFortuneButton() |
|
166 |
{ |
|
167 |
getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty() |
|
168 |
&& !portLineEdit->text().isEmpty()); |
|
169 |
} |