author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QtNetwork> |
|
43 |
||
44 |
#include "blockingclient.h" |
|
45 |
||
46 |
BlockingClient::BlockingClient(QWidget *parent) |
|
47 |
: QDialog(parent) |
|
48 |
{ |
|
49 |
hostLabel = new QLabel(tr("&Server name:")); |
|
50 |
portLabel = new QLabel(tr("S&erver port:")); |
|
51 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
// find out which IP to connect to |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
QString ipAddress; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
// use the first non-localhost IPv4 address |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
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
|
57 |
if (ipAddressesList.at(i) != QHostAddress::LocalHost && |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
ipAddressesList.at(i).toIPv4Address()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
59 |
ipAddress = ipAddressesList.at(i).toString(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
61 |
} |
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 |
// 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
|
64 |
if (ipAddress.isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
hostLineEdit = new QLineEdit(ipAddress); |
0 | 68 |
portLineEdit = new QLineEdit; |
69 |
portLineEdit->setValidator(new QIntValidator(1, 65535, this)); |
|
70 |
||
71 |
hostLabel->setBuddy(hostLineEdit); |
|
72 |
portLabel->setBuddy(portLineEdit); |
|
73 |
||
74 |
statusLabel = new QLabel(tr("This examples requires that you run the " |
|
75 |
"Fortune Server example as well.")); |
|
76 |
||
77 |
getFortuneButton = new QPushButton(tr("Get Fortune")); |
|
78 |
getFortuneButton->setDefault(true); |
|
79 |
getFortuneButton->setEnabled(false); |
|
80 |
||
81 |
quitButton = new QPushButton(tr("Quit")); |
|
82 |
||
83 |
buttonBox = new QDialogButtonBox; |
|
84 |
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); |
|
85 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
|
86 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
connect(hostLineEdit, SIGNAL(textChanged(QString)), |
0 | 88 |
this, SLOT(enableGetFortuneButton())); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
connect(portLineEdit, SIGNAL(textChanged(QString)), |
0 | 90 |
this, SLOT(enableGetFortuneButton())); |
91 |
connect(getFortuneButton, SIGNAL(clicked()), |
|
92 |
this, SLOT(requestNewFortune())); |
|
93 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
|
94 |
//! [0] |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
95 |
connect(&thread, SIGNAL(newFortune(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
this, SLOT(showFortune(QString))); |
0 | 97 |
//! [0] //! [1] |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
connect(&thread, SIGNAL(error(int,QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
this, SLOT(displayError(int,QString))); |
0 | 100 |
//! [1] |
101 |
||
102 |
QGridLayout *mainLayout = new QGridLayout; |
|
103 |
mainLayout->addWidget(hostLabel, 0, 0); |
|
104 |
mainLayout->addWidget(hostLineEdit, 0, 1); |
|
105 |
mainLayout->addWidget(portLabel, 1, 0); |
|
106 |
mainLayout->addWidget(portLineEdit, 1, 1); |
|
107 |
mainLayout->addWidget(statusLabel, 2, 0, 1, 2); |
|
108 |
mainLayout->addWidget(buttonBox, 3, 0, 1, 2); |
|
109 |
setLayout(mainLayout); |
|
110 |
||
111 |
setWindowTitle(tr("Blocking Fortune Client")); |
|
112 |
portLineEdit->setFocus(); |
|
113 |
} |
|
114 |
||
115 |
//! [2] |
|
116 |
void BlockingClient::requestNewFortune() |
|
117 |
{ |
|
118 |
getFortuneButton->setEnabled(false); |
|
119 |
thread.requestNewFortune(hostLineEdit->text(), |
|
120 |
portLineEdit->text().toInt()); |
|
121 |
} |
|
122 |
//! [2] |
|
123 |
||
124 |
//! [3] |
|
125 |
void BlockingClient::showFortune(const QString &nextFortune) |
|
126 |
{ |
|
127 |
if (nextFortune == currentFortune) { |
|
128 |
requestNewFortune(); |
|
129 |
return; |
|
130 |
} |
|
131 |
//! [3] |
|
132 |
||
133 |
//! [4] |
|
134 |
currentFortune = nextFortune; |
|
135 |
statusLabel->setText(currentFortune); |
|
136 |
getFortuneButton->setEnabled(true); |
|
137 |
} |
|
138 |
//! [4] |
|
139 |
||
140 |
void BlockingClient::displayError(int socketError, const QString &message) |
|
141 |
{ |
|
142 |
switch (socketError) { |
|
143 |
case QAbstractSocket::HostNotFoundError: |
|
144 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
145 |
tr("The host was not found. Please check the " |
|
146 |
"host and port settings.")); |
|
147 |
break; |
|
148 |
case QAbstractSocket::ConnectionRefusedError: |
|
149 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
150 |
tr("The connection was refused by the peer. " |
|
151 |
"Make sure the fortune server is running, " |
|
152 |
"and check that the host name and port " |
|
153 |
"settings are correct.")); |
|
154 |
break; |
|
155 |
default: |
|
156 |
QMessageBox::information(this, tr("Blocking Fortune Client"), |
|
157 |
tr("The following error occurred: %1.") |
|
158 |
.arg(message)); |
|
159 |
} |
|
160 |
||
161 |
getFortuneButton->setEnabled(true); |
|
162 |
} |
|
163 |
||
164 |
void BlockingClient::enableGetFortuneButton() |
|
165 |
{ |
|
166 |
getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty() |
|
167 |
&& !portLineEdit->text().isEmpty()); |
|
168 |
} |