1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/* This file is part of the KDE project
Copyright (C) 2002-2003 Tim Jansen <[email protected]>
Copyright (C) 2003-2004 Nadeem Hasan <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or ( at your option ) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqregexp.h>
#include <tqtimer.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <tdelistview.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include "hostpreferences.h"
#include "maindialogwidget.h"
static const TQString DEFAULT_SCOPE = "default";
class UrlListViewItem : public TDEListViewItem
{
public:
UrlListViewItem( TQListView *v, const TQString &url, const TQString &host,
const TQString &protocol, const TQString &type, const TQString &userid,
const TQString &fullname, const TQString &desc,
const TQString &serviceid )
: TDEListViewItem( v, host, i18n( "unknown" ), host, protocol ),
m_url( url ), m_serviceid( serviceid )
{
if ( !type.isNull() )
{
//User connects to somebody else's desktop, used for krfb
if ( type.lower() == "shared" )
setText( 1, i18n( "Shared Desktop" ) );
//User connects to desktop that exists only on the network
else if ( type.lower() == "private" )
setText( 1, i18n( "Standalone Desktop" ) );
}
if ( !desc.isNull() )
setText( 0, desc );
if ( ( !userid.isEmpty() ) && ( !fullname.isEmpty() ) )
setText( 0, TQString( "%1 (%2)" ).arg( fullname ).arg( userid ) );
else if ( !userid.isNull() )
setText( 0, userid );
else if ( !fullname.isNull() )
setText( 0, fullname );
}
TQString url()
{
return m_url;
}
const TQString& serviceid() const
{
return m_serviceid;
}
protected:
TQString m_url;
TQString m_serviceid;
};
MainDialogWidget::MainDialogWidget( TQWidget *parent, const char *name )
: MainDialogBase( parent, name ),
m_scanning( false )
{
HostPreferences *hp = HostPreferences::instance();
TQStringList list;
list = hp->serverCompletions();
m_serverInput->completionObject()->setItems( list );
list = hp->serverHistory();
m_serverInput->setHistoryItems( list );
m_searchInput->setTrapReturnKey( true );
connect( m_browsingView,
TQ_SIGNAL( selectionChanged( TQListViewItem * ) ),
TQ_SLOT( itemSelected( TQListViewItem * ) ) );
connect( m_browsingView,
TQ_SIGNAL( doubleClicked( TQListViewItem *, const TQPoint &, int ) ),
TQ_SLOT( itemDoubleClicked( TQListViewItem * ) ) );
connect( m_scopeCombo,
TQ_SIGNAL( activated( const TQString & ) ),
TQ_SLOT( scopeSelected( const TQString & ) ) );
connect( m_serverInput,
TQ_SIGNAL( returnPressed( const TQString & ) ),
TQ_SLOT( rescan() ) );
bool showBrowse = hp->showBrowsingPanel();
enableBrowsingArea( showBrowse );
m_locator_dnssd = new DNSSD::ServiceBrowser(TQStringList::split(',',"_rfb._tcp,_rdp._tcp"),0,DNSSD::ServiceBrowser::AutoResolve);
connect(m_locator_dnssd,TQ_SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),
TQ_SLOT(addedService(DNSSD::RemoteService::Ptr)));
connect(m_locator_dnssd,TQ_SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)),
TQ_SLOT(removedService(DNSSD::RemoteService::Ptr)));
m_locator_dnssd->startBrowse();
adjustSize();
}
MainDialogWidget::~MainDialogWidget()
{
delete m_locator_dnssd;
}
void MainDialogWidget::save()
{
HostPreferences *hp = HostPreferences::instance();
TQStringList list;
m_serverInput->addToHistory( m_serverInput->currentText() );
list = m_serverInput->completionObject()->items();
hp->setServerCompletions( list );
list = m_serverInput->historyItems();
hp->setServerHistory( list );
hp->setShowBrowsingPanel( m_browsingPanel->isVisible() );
}
void MainDialogWidget::setRemoteHost( const TQString &host )
{
m_serverInput->setEditText( host );
}
TQString MainDialogWidget::remoteHost()
{
return m_serverInput->currentText();
}
void MainDialogWidget::hostChanged( const TQString &text )
{
emit hostValid(text.contains(TQRegExp(":[0-9]+$")) ||
text.contains(TQRegExp("^vnc:/.+")) ||
text.contains(TQRegExp("^rdp:/.+")));
}
void MainDialogWidget::toggleBrowsingArea()
{
enableBrowsingArea(!m_browsingPanel->isVisible());
}
void MainDialogWidget::enableBrowsingArea( bool enable )
{
int hOffset = 0;
if (enable)
{
m_browsingPanel->show();
m_browsingPanel->setMaximumSize(1000, 1000);
m_browsingPanel->setEnabled(true);
m_browseButton->setText(m_browseButton->text().replace(">>", "<<"));
}
else
{
hOffset = m_browsingPanel->height();
m_browsingPanel->hide();
m_browsingPanel->setMaximumSize(0, 0);
m_browsingPanel->setEnabled(false);
m_browseButton->setText(m_browseButton->text().replace("<<", ">>"));
int h = minimumSize().height()-hOffset;
setMinimumSize(minimumSize().width(), (h > 0) ? h : 0);
resize(width(), height()-hOffset);
TQTimer::singleShot( 0, parentWidget(), TQ_SLOT( adjustSize() ) );
}
if (enable)
rescan();
}
void MainDialogWidget::itemSelected( TQListViewItem *item )
{
UrlListViewItem *u = ( UrlListViewItem* ) item;
TQRegExp rx( "^service:remotedesktop\\.kde:([^;]*)" );
if ( rx.search( u->url() ) < 0 )
m_serverInput->setCurrentText( u->url());
else m_serverInput->setCurrentText( rx.cap( 1 ) );
}
void MainDialogWidget::itemDoubleClicked( TQListViewItem *item )
{
itemSelected( item );
emit accept();
}
void MainDialogWidget::scopeSelected( const TQString &scope )
{
TQString s = scope;
if ( s == i18n( "default" ) )
s = DEFAULT_SCOPE;
if ( m_scope == s )
return;
m_scope = s;
rescan();
}
void MainDialogWidget::rescan()
{
TQStringList scopeList;
if ( m_scanning )
return;
m_scanning = true;
m_rescanButton->setEnabled( false );
m_scopeCombo->setEnabled( false );
if ( !ensureLocatorOpen() )
return;
m_browsingView->clear();
TQString filter;
if ( !m_searchInput->text().stripWhiteSpace().isEmpty() ) {
TQString ef = KServiceLocator::escapeFilter(
m_searchInput->text().stripWhiteSpace() );
filter = "(|(|(description=*"+ef+"*)(username=*"+ef+"*))(fullname=*"+ef+"*))";
}
if ( !m_locator->findServices( "service:remotedesktop.kde",
filter, m_scope ) ) {
kdWarning() << "Failure in findServices()" << endl;
errorScanning();
return;
}
}
bool MainDialogWidget::ensureLocatorOpen()
{
if ( m_locator )
return true;
m_locator = new KServiceLocator();
if ( !m_locator->available() ) {
#ifdef HAVE_SLP
KMessageBox::error( 0,
i18n( "Browsing the network is not possible. You probably "
"did not install SLP support correctly." ),
i18n( "Browsing Not Possible" ), false );
#endif
return false;
}
connect( m_locator, TQ_SIGNAL( foundService( TQString,int ) ),
TQ_SLOT( foundService( TQString,int ) ) );
connect( m_locator, TQ_SIGNAL( lastServiceSignal( bool ) ),
TQ_SLOT( lastSignalServices( bool ) ) );
connect( m_locator, TQ_SIGNAL( foundScopes( TQStringList ) ),
TQ_SLOT( foundScopes( TQStringList ) ) );
return true;
}
void MainDialogWidget::errorScanning()
{
KMessageBox::error( 0,
i18n( "An error occurred while scanning the network." ),
i18n( "Error While Scanning" ), false );
finishScanning();
}
void MainDialogWidget::finishScanning()
{
m_rescanButton->setEnabled( true );
m_scopeCombo->setEnabled( true );
m_scanning = false;
}
void MainDialogWidget::foundService( TQString url, int )
{
TQRegExp rx( "^service:remotedesktop\\.kde:(\\w+)://([^;]+);(.*)$" );
if ( rx.search( url ) < 0 )
{
rx = TQRegExp( "^service:remotedesktop\\.kde:(\\w+)://(.*)$" );
if ( rx.search( url ) < 0 )
return;
}
TQMap<TQString,TQString> map;
KServiceLocator::parseAttributeList( rx.cap( 3 ), map );
new UrlListViewItem( m_browsingView, url, rx.cap( 2 ), rx.cap( 1 ),
KServiceLocator::decodeAttributeValue( map[ "type" ] ),
KServiceLocator::decodeAttributeValue( map[ "username" ] ),
KServiceLocator::decodeAttributeValue( map[ "fullname" ] ),
KServiceLocator::decodeAttributeValue( map[ "description" ] ),
KServiceLocator::decodeAttributeValue( map[ "serviceid" ] ) );
}
void MainDialogWidget::addedService( DNSSD::RemoteService::Ptr service )
{
TQString type = service->type().mid(1,3);
if (type == "rfb") type = "vnc";
TQString url = type+"://"+service->hostName()+":"+TQString::number(service->port());
new UrlListViewItem( m_browsingView, url, service->serviceName(),
type.upper(),service->textData()["type"],
service->textData()["u"],service->textData()["fullname"],
service->textData()["description"],service->serviceName()+service->domain());
}
void MainDialogWidget::removedService( DNSSD::RemoteService::Ptr service )
{
TQListViewItemIterator it( m_browsingView );
while ( it.current() ) {
if ( ((UrlListViewItem*)it.current())->serviceid() == service->serviceName()+service->domain() )
delete it.current();
else ++it;
}
}
void MainDialogWidget::lastSignalServices( bool success )
{
if ( !success )
{
errorScanning();
return;
}
if ( !m_locator->findScopes() )
{
kdWarning() << "Failure in findScopes()" << endl;
errorScanning();
}
}
void MainDialogWidget::foundScopes( TQStringList scopeList )
{
int di = scopeList.findIndex( DEFAULT_SCOPE );
if ( di >= 0 )
scopeList[ di ] = i18n( "default" );
int ct = scopeList.findIndex( m_scopeCombo->currentText() );
m_scopeCombo->clear();
m_scopeCombo->insertStringList( scopeList );
if ( ct >= 0 )
m_scopeCombo->setCurrentItem( ct );
finishScanning();
}
#include "maindialogwidget.moc"
|