Removed some stupid debug messages
[kdenetwork.git] / krdc / maindialogwidget.cpp
blob152892c1d1f065837c58b3a3ec764ccbb16207dd
1 /* This file is part of the KDE project
2 Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
3 Copyright (C) 2003-2004 Nadeem Hasan <nhasan@kde.org>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or ( at your option ) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 #include <qregexp.h>
22 #include <qtimer.h>
24 #include <kcombobox.h>
25 #include <kdebug.h>
26 #include <klineedit.h>
27 #include <klistview.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 #include <kpushbutton.h>
32 #include "hostpreferences.h"
33 #include "maindialogwidget.h"
35 static const QString DEFAULT_SCOPE = "default";
36 static const QString DNSSD_SCOPE = "DNS-SD";
38 class UrlListViewItem : public KListViewItem
40 public:
41 UrlListViewItem( QListView *v, const QString &url, const QString &host,
42 const QString &protocol, const QString &type, const QString &userid,
43 const QString &fullname, const QString &desc,
44 const QString &serviceid )
45 : KListViewItem( v, host, i18n( "unknown" ), host, protocol ),
46 m_url( url ), m_serviceid( serviceid )
48 if ( !type.isNull() )
50 //User connects to somebody else's desktop, used for krfb
51 if ( type.lower() == "shared" )
52 setText( 1, i18n( "Shared Desktop" ) );
53 //User connects to desktop that exists only on the network
54 else if ( type.lower() == "private" )
55 setText( 1, i18n( "Standalone Desktop" ) );
57 if ( !desc.isNull() )
58 setText( 0, desc );
59 if ( ( !userid.isEmpty() ) && ( !fullname.isEmpty() ) )
60 setText( 0, QString( "%1 (%2)" ).arg( fullname ).arg( userid ) );
61 else if ( !userid.isNull() )
62 setText( 0, userid );
63 else if ( !fullname.isNull() )
64 setText( 0, fullname );
67 QString url()
69 return m_url;
71 const QString& serviceid() const
73 return m_serviceid;
76 protected:
77 QString m_url;
78 QString m_serviceid;
81 MainDialogWidget::MainDialogWidget( QWidget *parent, const char *name )
82 : MainDialogBase( parent, name ),
83 m_scanning( false ), m_locator_dnssd(0)
85 HostPreferences *hp = HostPreferences::instance();
86 QStringList list;
88 list = hp->serverCompletions();
89 m_serverInput->completionObject()->setItems( list );
90 list = hp->serverHistory();
91 m_serverInput->setHistoryItems( list );
93 m_searchInput->setTrapReturnKey( true );
95 connect( m_browsingView,
96 SIGNAL( selectionChanged( QListViewItem * ) ),
97 SLOT( itemSelected( QListViewItem * ) ) );
98 connect( m_browsingView,
99 SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ),
100 SLOT( itemDoubleClicked( QListViewItem * ) ) );
101 connect( m_scopeCombo,
102 SIGNAL( activated( const QString & ) ),
103 SLOT( scopeSelected( const QString & ) ) );
104 connect( m_serverInput,
105 SIGNAL( returnPressed( const QString & ) ),
106 SLOT( rescan() ) );
108 bool showBrowse = hp->showBrowsingPanel();
109 enableBrowsingArea( showBrowse );
111 adjustSize();
114 void MainDialogWidget::save()
116 HostPreferences *hp = HostPreferences::instance();
117 QStringList list;
119 m_serverInput->addToHistory( m_serverInput->currentText() );
120 list = m_serverInput->completionObject()->items();
121 hp->setServerCompletions( list );
122 list = m_serverInput->historyItems();
123 hp->setServerHistory( list );
125 hp->setShowBrowsingPanel( m_browsingPanel->isVisible() );
128 void MainDialogWidget::setRemoteHost( const QString &host )
130 m_serverInput->setEditText( host );
133 QString MainDialogWidget::remoteHost()
135 return m_serverInput->currentText();
138 void MainDialogWidget::hostChanged( const QString &text )
140 emit hostValid(text.contains(QRegExp(":[0-9]+$")) ||
141 text.contains(QRegExp("^vnc:/.+")) ||
142 text.contains(QRegExp("^rdp:/.+")));
145 void MainDialogWidget::toggleBrowsingArea()
147 enableBrowsingArea(!m_browsingPanel->isVisible());
150 void MainDialogWidget::enableBrowsingArea( bool enable )
152 int hOffset = 0;
153 if (enable)
155 m_browsingPanel->show();
156 m_browsingPanel->setMaximumSize(1000, 1000);
157 m_browsingPanel->setEnabled(true);
158 m_browseButton->setText(m_browseButton->text().replace(">>", "<<"));
160 else
162 hOffset = m_browsingPanel->height();
163 m_browsingPanel->hide();
164 m_browsingPanel->setMaximumSize(0, 0);
165 m_browsingPanel->setEnabled(false);
166 m_browseButton->setText(m_browseButton->text().replace("<<", ">>"));
167 int h = minimumSize().height()-hOffset;
168 setMinimumSize(minimumSize().width(), (h > 0) ? h : 0);
169 resize(width(), height()-hOffset);
171 QTimer::singleShot( 0, parentWidget(), SLOT( adjustSize() ) );
174 if (enable)
175 rescan();
178 void MainDialogWidget::itemSelected( QListViewItem *item )
180 UrlListViewItem *u = ( UrlListViewItem* ) item;
181 QRegExp rx( "^service:remotedesktop\\.kde:([^;]*)" );
182 if ( rx.search( u->url() ) < 0 )
183 m_serverInput->setCurrentText( u->url());
184 else m_serverInput->setCurrentText( rx.cap( 1 ) );
187 void MainDialogWidget::itemDoubleClicked( QListViewItem *item )
189 itemSelected( item );
190 emit accept();
193 void MainDialogWidget::scopeSelected( const QString &scope )
195 QString s = scope;
196 if ( s == i18n( "default" ) )
197 s = DEFAULT_SCOPE;
199 if ( m_scope == s )
200 return;
201 m_scope = s;
202 rescan();
205 void MainDialogWidget::rescan()
207 QStringList scopeList;
209 if ( m_scanning )
210 return;
211 m_scanning = true;
212 m_rescanButton->setEnabled( false );
213 m_scopeCombo->setEnabled( false );
214 if ( !ensureLocatorOpen() )
215 return;
217 m_browsingView->clear();
219 if (m_locator_dnssd) {
220 delete m_locator_dnssd; // still active browsers
221 m_locator_dnssd = 0;
224 if (m_scope == DNSSD_SCOPE) {
225 kdDebug() << "Scope is DNSSD\n";
226 m_locator_dnssd = new DNSSD::ServiceBrowser(QStringList::split(',',"_rfb._tcp,_rdp._tcp"),0,DNSSD::ServiceBrowser::AutoResolve);
227 connect(m_locator_dnssd,SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),
228 SLOT(addedService(DNSSD::RemoteService::Ptr)));
229 connect(m_locator_dnssd,SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)),
230 SLOT(removedService(DNSSD::RemoteService::Ptr)));
231 m_locator_dnssd->startBrowse();
232 // now find scopes
233 lastSignalServices(true);
234 } else {
235 QString filter;
236 if ( !m_searchInput->text().stripWhiteSpace().isEmpty() ) {
237 QString ef = KServiceLocator::escapeFilter(
238 m_searchInput->text().stripWhiteSpace() );
239 filter = "(|(|(description=*"+ef+"*)(username=*"+ef+"*))(fullname=*"+ef+"*))";
242 if ( !m_locator->findServices( "service:remotedesktop.kde",
243 filter, m_scope ) ) {
244 kdWarning() << "Failure in findServices()" << endl;
245 errorScanning();
246 return;
251 bool MainDialogWidget::ensureLocatorOpen()
253 if ( m_locator )
254 return true;
256 m_locator = new KServiceLocator();
258 if ( !m_locator->available() ) {
259 #ifdef HAVE_SLP
260 KMessageBox::error( 0,
261 i18n( "Browsing the network is not possible. You probably "
262 "did not install SLP support correctly." ),
263 i18n( "Browsing Not Possible" ), false );
264 #endif
265 return false;
268 connect( m_locator, SIGNAL( foundService( QString,int ) ),
269 SLOT( foundService( QString,int ) ) );
270 connect( m_locator, SIGNAL( lastServiceSignal( bool ) ),
271 SLOT( lastSignalServices( bool ) ) );
272 connect( m_locator, SIGNAL( foundScopes( QStringList ) ),
273 SLOT( foundScopes( QStringList ) ) );
274 return true;
277 void MainDialogWidget::errorScanning()
279 KMessageBox::error( 0,
280 i18n( "An error occurred while scanning the network." ),
281 i18n( "Error While Scanning" ), false );
282 finishScanning();
285 void MainDialogWidget::finishScanning()
287 m_rescanButton->setEnabled( true );
288 m_scopeCombo->setEnabled( true );
289 m_scanning = false;
292 void MainDialogWidget::foundService( QString url, int )
294 QRegExp rx( "^service:remotedesktop\\.kde:(\\w+)://([^;]+);(.*)$" );
296 if ( rx.search( url ) < 0 )
298 rx = QRegExp( "^service:remotedesktop\\.kde:(\\w+)://(.*)$" );
299 if ( rx.search( url ) < 0 )
300 return;
303 QMap<QString,QString> map;
304 KServiceLocator::parseAttributeList( rx.cap( 3 ), map );
306 new UrlListViewItem( m_browsingView, url, rx.cap( 2 ), rx.cap( 1 ),
307 KServiceLocator::decodeAttributeValue( map[ "type" ] ),
308 KServiceLocator::decodeAttributeValue( map[ "username" ] ),
309 KServiceLocator::decodeAttributeValue( map[ "fullname" ] ),
310 KServiceLocator::decodeAttributeValue( map[ "description" ] ),
311 KServiceLocator::decodeAttributeValue( map[ "serviceid" ] ) );
314 void MainDialogWidget::addedService( DNSSD::RemoteService::Ptr service )
316 QString type = service->type().mid(1,3);
317 if (type == "rfb") type = "vnc";
318 QString url = type+"://"+service->hostName()+":"+QString::number(service->port());
319 new UrlListViewItem( m_browsingView, url, service->serviceName(),
320 type.upper(),service->textData()["type"],
321 service->textData()["u"],service->textData()["fullname"],
322 service->textData()["description"],service->serviceName()+service->domain());
325 void MainDialogWidget::removedService( DNSSD::RemoteService::Ptr service )
327 QListViewItemIterator it( m_browsingView );
328 while ( it.current() ) {
329 if ( ((UrlListViewItem*)it.current())->serviceid() == service->serviceName()+service->domain() )
330 delete it.current();
331 else ++it;
336 void MainDialogWidget::lastSignalServices( bool success )
338 if ( !success )
340 errorScanning();
341 return;
344 if ( !m_locator->findScopes() )
346 kdWarning() << "Failure in findScopes()" << endl;
347 errorScanning();
351 void MainDialogWidget::foundScopes( QStringList scopeList )
353 scopeList << DNSSD_SCOPE;
355 int di = scopeList.findIndex( DEFAULT_SCOPE );
356 if ( di >= 0 )
357 scopeList[ di ] = i18n( "default" );
359 int ct = scopeList.findIndex( m_scopeCombo->currentText() );
360 m_scopeCombo->clear();
361 m_scopeCombo->insertStringList( scopeList );
362 if ( ct >= 0 )
363 m_scopeCombo->setCurrentItem( ct );
364 finishScanning();
367 #include "maindialogwidget.moc"