summaryrefslogtreecommitdiffstats
path: root/krdc/maindialogwidget.cpp
blob: 076fc2f631c67d2bdcb0154334bc2090ebca1303 (plain)
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 <tim@tjansen.de>
   Copyright (C) 2003-2004 Nadeem Hasan <nhasan@kde.org>

   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 <qregexp.h>
#include <qtimer.h>

#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>

#include "hostpreferences.h"
#include "maindialogwidget.h"

static const QString DEFAULT_SCOPE = "default";

class UrlListViewItem : public KListViewItem
{
  public:
    UrlListViewItem( QListView *v, const QString &url, const QString &host,
        const QString &protocol, const QString &type, const QString &userid,
        const QString &fullname, const QString &desc,
        const QString &serviceid )
      : KListViewItem( 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, QString( "%1 (%2)" ).arg( fullname ).arg( userid ) );
      else if ( !userid.isNull() )
        setText( 0, userid );
      else if ( !fullname.isNull() )
        setText( 0, fullname );
   }

  QString url()
  {
    return m_url;
  }
  const QString& serviceid() const
  {
    return m_serviceid;
  }

  protected:
    QString m_url;
    QString m_serviceid;
};

MainDialogWidget::MainDialogWidget( QWidget *parent, const char *name )
    : MainDialogBase( parent, name ),
      m_scanning( false )
{
  HostPreferences *hp = HostPreferences::instance();
  QStringList list;

  list  = hp->serverCompletions();
  m_serverInput->completionObject()->setItems( list );
  list = hp->serverHistory();
  m_serverInput->setHistoryItems( list );

  m_searchInput->setTrapReturnKey( true );

  connect( m_browsingView,
      SIGNAL( selectionChanged( QListViewItem * ) ),
      SLOT( itemSelected( QListViewItem * ) ) );
  connect( m_browsingView,
      SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ),
      SLOT( itemDoubleClicked( QListViewItem * ) ) );
  connect( m_scopeCombo,
      SIGNAL( activated( const QString & ) ),
      SLOT( scopeSelected( const QString & ) ) );
  connect( m_serverInput,
      SIGNAL( returnPressed( const QString & ) ),
      SLOT( rescan() ) );

  bool showBrowse = hp->showBrowsingPanel();
  enableBrowsingArea( showBrowse );

  m_locator_dnssd = new DNSSD::ServiceBrowser(QStringList::split(',',"_rfb._tcp,_rdp._tcp"),0,DNSSD::ServiceBrowser::AutoResolve);
  connect(m_locator_dnssd,SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),
	  SLOT(addedService(DNSSD::RemoteService::Ptr)));
  connect(m_locator_dnssd,SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)),
	  SLOT(removedService(DNSSD::RemoteService::Ptr)));
  m_locator_dnssd->startBrowse();

  adjustSize();
}

MainDialogWidget::~MainDialogWidget()
{
  delete m_locator_dnssd;
}

void MainDialogWidget::save()
{
  HostPreferences *hp = HostPreferences::instance();
  QStringList 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 QString &host )
{
  m_serverInput->setEditText( host );
}

QString MainDialogWidget::remoteHost()
{
  return m_serverInput->currentText();
}

void MainDialogWidget::hostChanged( const QString &text )
{
  emit hostValid(text.contains(QRegExp(":[0-9]+$")) ||
                 text.contains(QRegExp("^vnc:/.+")) ||
                 text.contains(QRegExp("^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);

    QTimer::singleShot( 0, parentWidget(), SLOT( adjustSize() ) );
  }

  if (enable)
    rescan();
}

void MainDialogWidget::itemSelected( QListViewItem *item )
{
  UrlListViewItem *u = ( UrlListViewItem* ) item;
  QRegExp 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( QListViewItem *item )
{
  itemSelected( item );
  emit accept();
}

void MainDialogWidget::scopeSelected( const QString &scope )
{
  QString s = scope;
  if ( s == i18n( "default" ) )
    s = DEFAULT_SCOPE;

  if ( m_scope == s )
    return;
  m_scope = s;
  rescan();
}

void MainDialogWidget::rescan()
{
  QStringList scopeList;

  if ( m_scanning )
    return;
  m_scanning = true;
  m_rescanButton->setEnabled( false );
  m_scopeCombo->setEnabled( false );
  if ( !ensureLocatorOpen() )
    return;

  m_browsingView->clear();

  QString filter;
  if ( !m_searchInput->text().stripWhiteSpace().isEmpty() ) {
    QString 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, SIGNAL( foundService( QString,int ) ),
                      SLOT( foundService( QString,int ) ) );
  connect( m_locator, SIGNAL( lastServiceSignal( bool ) ),
                      SLOT( lastSignalServices( bool ) ) );
  connect( m_locator, SIGNAL( foundScopes( QStringList ) ),
                      SLOT( foundScopes( QStringList ) ) );
   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( QString url, int )
{
  QRegExp rx(  "^service:remotedesktop\\.kde:(\\w+)://([^;]+);(.*)$" );

  if ( rx.search( url ) < 0 )
  {
    rx = QRegExp(  "^service:remotedesktop\\.kde:(\\w+)://(.*)$" );
    if ( rx.search( url ) < 0 )
      return;
  }

  QMap<QString,QString> 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 )
{
QString type = service->type().mid(1,3);
if (type == "rfb") type = "vnc";
QString url = type+"://"+service->hostName()+":"+QString::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 )
{
  QListViewItemIterator 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( QStringList 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"