summaryrefslogtreecommitdiff
path: root/noncore/comm/keypebble/krfbconnection.cpp
Unidiff
Diffstat (limited to 'noncore/comm/keypebble/krfbconnection.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/comm/keypebble/krfbconnection.cpp272
1 files changed, 132 insertions, 140 deletions
diff --git a/noncore/comm/keypebble/krfbconnection.cpp b/noncore/comm/keypebble/krfbconnection.cpp
index c51f18a..389c836 100644
--- a/noncore/comm/keypebble/krfbconnection.cpp
+++ b/noncore/comm/keypebble/krfbconnection.cpp
@@ -5,238 +5,230 @@
5 5
6#include "krfbconnection.h" 6#include "krfbconnection.h"
7#include "krfblogin.h" 7#include "krfblogin.h"
8#include "krfboptions.h" 8#include "krfbserver.h"
9#include "krfbdecoder.h" 9#include "krfbdecoder.h"
10#include "krfbbuffer.h" 10#include "krfbbuffer.h"
11 11
12KRFBConnection::KRFBConnection( QObject *parent ) 12KRFBConnection::KRFBConnection( QObject *parent )
13 : QObject( parent, "KRFBConnection" ) 13 : QObject( parent, "KRFBConnection" )
14{ 14{
15 portBase_ = 5900; 15 portBase_ = 5900;
16 currentState_ = Disconnected; 16 currentState_ = Disconnected;
17 sock = 0; 17 sock = 0;
18 minData_ = 0; 18 minData_ = 0;
19 options_ = new KRFBOptions(); 19 options_ = new KRFBServer();
20 updater = 0; 20 updater = 0;
21 decoder_ = 0; 21 decoder_ = 0;
22 buffer_ = 0; 22 buffer_ = 0;
23} 23}
24 24
25KRFBConnection::~KRFBConnection() 25KRFBConnection::~KRFBConnection()
26{ 26{
27 if ( ( currentState_ != Disconnected ) && ( currentState_ != Disconnecting ) && sock ) { 27 if ( ( currentState_ != Disconnected ) && ( currentState_ != Disconnecting ) && sock ) {
28 disconnectDone(); 28 disconnectDone();
29 } 29 }
30 delete options_; 30 delete options_;
31} 31}
32 32
33void KRFBConnection::connectTo( const QCString &host, int display ) 33void KRFBConnection::connectTo( KRFBServer server)
34{ 34{
35 if ( currentState_ != Disconnected ); 35 if ( currentState_ != Disconnected )
36 disconnect(); 36 disconnect();
37
38 (*options_)=server;
37 39
38 this->host_= host; 40 sock = new QSocket( this, "rfbSocket" );
39 this->display_ = display; 41 CHECK_PTR( sock );
40 42
41 sock = new QSocket( this, "rfbSocket" ); 43 // Connect to something to notice connection or error
42 CHECK_PTR( sock ); 44 connect( sock, SIGNAL( error( int ) ), SLOT( gotSocketError( int ) ) );
45 connect( sock, SIGNAL( connected() ), SLOT( gotSocketConnection() ) );
43 46
44 // Connect to something to notice connection or error 47 qWarning( "Connecting..." );
45 connect( sock, SIGNAL( error( int ) ), SLOT( gotSocketError( int ) ) );
46 connect( sock, SIGNAL( connected() ), SLOT( gotSocketConnection() ) );
47 48
48 qWarning( "Connecting..." ); 49 currentState_ = Connecting;
49 50 sock->connectToHost( options_->hostname.latin1(), portBase_ + options_->display );
50 currentState_ = Connecting;
51 sock->connectToHost( host_, portBase_ + display_ );
52} 51}
53 52
54void KRFBConnection::disconnect() 53void KRFBConnection::disconnect()
55{ 54{
56 qWarning( "Disconnecting from server" ); 55 qWarning( "Disconnecting from server" );
57 56
58 if ( ( currentState_ != Disconnected ) 57 if ( ( currentState_ != Disconnected )
59 && ( currentState_ != Disconnecting ) 58 && ( currentState_ != Disconnecting )
60 && sock ) { 59 && sock ) {
61 currentState_ = Disconnecting; 60 currentState_ = Disconnecting;
62 61
63 connect( sock, SIGNAL( delayedCloseFinished() ), SLOT( disconnectDone() ) ); 62 connect( sock, SIGNAL( delayedCloseFinished() ), SLOT( disconnectDone() ) );
64 sock->close(); 63 sock->close();
65 64
66 if ( sock->state() != QSocket::Closing ) 65 if ( sock->state() != QSocket::Closing )
67 disconnectDone(); 66 disconnectDone();
68 } 67 }
69} 68}
70 69
71void KRFBConnection::disconnectDone() 70void KRFBConnection::disconnectDone()
72{ 71{
73 qWarning( "KRFBConnection disconnected" ); 72 currentState_ = Disconnected;
74 currentState_ = Disconnected; 73 delete sock;
75 delete sock; 74 sock = 0;
76 sock = 0; 75 minData_ = 0;
77 minData_ = 0; 76 delete updater;
78 delete updater; 77 delete decoder_;
79 delete decoder_; 78 delete buffer_;
80 delete buffer_; 79 emit disconnected();
81 emit disconnected();
82} 80}
83 81
84void KRFBConnection::gotSocketConnection() 82void KRFBConnection::gotSocketConnection()
85{ 83{
86 currentState_ = LoggingIn; 84 currentState_ = LoggingIn;
87 85
88 qWarning( "Connected, logging in..." ); 86 qWarning( "Connected, logging in..." );
89 87
90 static QString statusMsg = tr( "Connected" ); 88 static QString statusMsg = tr( "Connected" );
91 emit statusChanged( statusMsg ); 89 emit statusChanged( statusMsg );
92 90
93 // Do some login stuff 91 // Do some login stuff
94 login = new KRFBLogin( this ); 92 login = new KRFBLogin( this );
95} 93}
96 94
97void KRFBConnection::gotRFBConnection() 95void KRFBConnection::gotRFBConnection()
98{ 96{
99 qWarning( "Logged into server" ); 97 qWarning( "Logged into server" );
100 98
101 currentState_ = Connected; 99 currentState_ = Connected;
102 emit connected(); 100 emit connected();
103 101
104 // Create the decoder and start doing stuff 102 // Create the decoder and start doing stuff
105 decoder_ = new KRFBDecoder( this ); 103 decoder_ = new KRFBDecoder( this );
106 CHECK_PTR( decoder_ ); 104 CHECK_PTR( decoder_ );
107 105
108 buffer_ = new KRFBBuffer( decoder_, this, "RFB Buffer" ); 106 buffer_ = new KRFBBuffer( decoder_, this, "RFB Buffer" );
109 CHECK_PTR( buffer_ ); 107 CHECK_PTR( buffer_ );
110 decoder_->setBuffer( buffer_ ); 108 decoder_->setBuffer( buffer_ );
111 109
112 connect( decoder_, SIGNAL( status( const QString & ) ), 110 connect( decoder_, SIGNAL( status( const QString & ) ),
113 this, SIGNAL( statusChanged( const QString & ) ) ); 111 this, SIGNAL( statusChanged( const QString & ) ) );
114 emit loggedIn(); 112 emit loggedIn();
115 113
116 decoder_->start(); 114 decoder_->start();
117 115
118 updater = new QTimer; 116 updater = new QTimer;
119 connect( updater, SIGNAL( timeout() ), SLOT( updateTimer() ) ); 117 connect( updater, SIGNAL( timeout() ), SLOT( updateTimer() ) );
120 updater->start( options_->updateRate ); 118 updater->start( options_->updateRate );
121} 119}
122 120
123void KRFBConnection::gotSocketError( int err ) 121void KRFBConnection::gotSocketError( int err )
124{ 122{
125 currentState_ = Error; 123 currentState_ = Error;
126 124
127 // Do some error handling stuff 125 // Do some error handling stuff
128 qWarning( "KRFBConnection: Socket error %d", err ); 126 qWarning( "KRFBConnection: Socket error %d", err );
129 127
130 static QString refused = tr( "Connection Refused" ); 128 static QString refused = tr( "Connection Refused" );
131 static QString host = tr( "Host not found" ); 129 static QString host = tr( "Host not found" );
132 static QString read = tr( "Read Error: QSocket reported an error reading\n" 130 static QString read = tr( "Read Error: QSocket reported an error reading\n"
133 "data, the remote host has probably dropped the\n" 131 "data, the remote host has probably dropped the\n"
134 "connection." ); 132 "connection." );
135 static QString confused = tr( "QSocket reported an invalid error code" ); 133 static QString confused = tr( "QSocket reported an invalid error code" );
136 134
137 QString msg; 135 QString msg;
138 switch ( err ) { 136 switch ( err ) {
139 case QSocket::ErrConnectionRefused: 137 case QSocket::ErrConnectionRefused:
140 msg = refused; 138 msg = refused;
141 break; 139 break;
142 case QSocket::ErrHostNotFound: 140 case QSocket::ErrHostNotFound:
143 msg = host; 141 msg = host;
144 break; 142 break;
145 case QSocket::ErrSocketRead: 143 case QSocket::ErrSocketRead:
146 msg = read; 144 msg = read;
147 break; 145 break;
148 default: 146 default:
149 msg = confused; 147 msg = confused;
150 }; 148 };
151 149
152 QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); 150 QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) );
153 delete sock; 151 delete sock;
154 sock = 0; 152 sock = 0;
155 currentState_ = Disconnected; 153 currentState_ = Disconnected;
156 154
157 emit error( msg ); 155 emit error( msg );
158} 156}
159 157
160void KRFBConnection::gotMoreData() 158void KRFBConnection::gotMoreData()
161{ 159{
162 assert( minData_ > 0 ); 160 assert( minData_ > 0 );
163 161
164 if ( sock->size() >= minData_ ) { 162 if ( sock->size() >= minData_ ) {
165 minData_ = 0; 163 minData_ = 0;
166 QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); 164 QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) );
167 emit gotEnoughData(); 165 emit gotEnoughData();
168 } 166 }
169} 167}
170 168
171void KRFBConnection::waitForData( unsigned int sz ) 169void KRFBConnection::waitForData( unsigned int sz )
172{ 170{
173 assert( minData_ == 0 ); 171 assert( minData_ == 0 );
174 assert( sz > 0 ); 172 assert( sz > 0 );
175 assert( currentState_ != Error ); 173 assert( currentState_ != Error );
176 174
177 if ( sock->size() >= sz ) { 175 if ( sock->size() >= sz ) {
178 // qWarning( "No need to wait for data" ); 176 // qWarning( "No need to wait for data" );
179 emit gotEnoughData(); 177 emit gotEnoughData();
180 } 178 }
181 else { 179 else {
182 // qWarning( "Waiting for %u bytes", sz ); 180 // qWarning( "Waiting for %u bytes", sz );
183 181 minData_ = sz;
184 minData_ = sz; 182 connect( sock, SIGNAL( readyRead() ), SLOT( gotMoreData() ) );
185 connect( sock, SIGNAL( readyRead() ), SLOT( gotMoreData() ) ); 183 }
186 }
187} 184}
188 185
189int KRFBConnection::read( void *buf, int sz ) 186int KRFBConnection::read( void *buf, int sz )
190{ 187{
191 return sock->readBlock( (char *) buf, sz ); 188 return sock->readBlock( (char *) buf, sz );
192} 189}
193 190
194int KRFBConnection::write( void *buf, int sz ) 191int KRFBConnection::write( void *buf, int sz )
195{ 192{
196 return sock->writeBlock( (const char *) buf, sz ); 193 return sock->writeBlock( (const char *) buf, sz );
197} 194}
198 195
199KRFBConnection::State KRFBConnection::state() const 196KRFBConnection::State KRFBConnection::state() const
200{ 197{
201 return currentState_; 198 return currentState_;
202} 199}
203 200
204void KRFBConnection::setPortBase( int base ) 201void KRFBConnection::setPortBase( int base )
205{ 202{
206 portBase_ = base; 203 portBase_ = base;
207} 204}
208 205
209int KRFBConnection::portBase() const 206int KRFBConnection::portBase() const
210{ 207{
211 return portBase_; 208 return portBase_;
212}
213
214void KRFBConnection::setPassword( const QCString &pass )
215{
216 this->pass_ = pass;
217} 209}
218 210
219void KRFBConnection::updateTimer() 211void KRFBConnection::updateTimer()
220{ 212{
221 decoder_->sendUpdateRequest( true ); 213 decoder_->sendUpdateRequest( true );
222} 214}
223 215
224void KRFBConnection::refresh() 216void KRFBConnection::refresh()
225{ 217{
226 decoder_->sendUpdateRequest( false ); 218 decoder_->sendUpdateRequest( false );
227} 219}
228 220
229void KRFBConnection::sendCutText( const QString &text ) 221void KRFBConnection::sendCutText( const QString &text )
230{ 222{
231 decoder_->sendCutEvent( text ); 223 decoder_->sendCutEvent( text );
232} 224}
233 225
234const QUrl &KRFBConnection::url() 226const QUrl &KRFBConnection::url()
235{ 227{
236 url_.setProtocol( "vnc" ); 228 url_.setProtocol( "vnc" );
237 url_.setPort( display() ); 229 url_.setPort( display() );
238 url_.setHost( host() ); 230 url_.setHost( host() );
239 url_.setPath( "/" ); 231 url_.setPath( "/" );
240 232
241 return url_; 233 return url_;
242} 234}