~ubuntu-branches/ubuntu/vivid/smb4k/vivid

« back to all changes in this revision

Viewing changes to smb4k/core/smb4kscanner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2005-03-29 12:15:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050329121553-s5ohipujfw3wmo25
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    smb4kscanner.cpp  -  The network scan core class of Smb4K.
 
3
                             -------------------
 
4
    begin                : Sam Mai 31 2003
 
5
    copyright            : (C) 2003 by Alexander Reinholdt
 
6
    email                : dustpuppy@mail.berlios.de
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
// Qt includes
 
19
#include <qapplication.h>
 
20
#include <qmap.h>
 
21
 
 
22
// KDE includes
 
23
#include <klocale.h>
 
24
#include <kapplication.h>
 
25
 
 
26
// Application specific includes.
 
27
#include "smb4kscanner.h"
 
28
#include "smb4kcore.h"
 
29
 
 
30
 
 
31
Smb4KScanner::Smb4KScanner( QObject *parent, const char *name ) : QObject( parent, name )
 
32
{
 
33
  m_proc = new KProcess( this, "ScannerProcess" );
 
34
  m_proc->setUseShell( true );
 
35
 
 
36
  m_state = Idle;
 
37
  m_workgroup = QString::null;
 
38
  m_host = QString::null;
 
39
  m_share = QString::null;
 
40
  m_path = QString::null;
 
41
 
 
42
  m_config = kapp->config();
 
43
 
 
44
  m_timer = new QTimer( this );
 
45
  m_timer->start( 50, false );
 
46
 
 
47
  m_working = false;
 
48
  
 
49
  m_queue.setAutoDelete( true );
 
50
  
 
51
  QString *input =  new QString( QString( "%1:" ).arg( Init ) ); 
 
52
  m_queue.enqueue( input );
 
53
 
 
54
  connect( m_proc,  SIGNAL( receivedStdout( KProcess *, char *, int ) ), this, SLOT( slotReceivedStdout( KProcess *, char *, int ) ) );
 
55
  connect( m_proc,  SIGNAL( processExited( KProcess* ) ), this,                SLOT( slotProcessExited( KProcess * ) ) );
 
56
  connect( m_proc,  SIGNAL( receivedStderr( KProcess *, char *, int ) ), this, SLOT( slotReceivedStderr( KProcess *, char *, int ) ) );
 
57
  connect( m_timer, SIGNAL( timeout() ), this,                                 SLOT( start() ) );
 
58
}
 
59
 
 
60
 
 
61
Smb4KScanner::~Smb4KScanner()
 
62
{
 
63
  // Do not use the abort() function here, because this 
 
64
  // could cause crashes due to the emitted killed()
 
65
  // signal.
 
66
  if ( m_proc->isRunning() )
 
67
    m_proc->kill();
 
68
    
 
69
  m_workgroupList.clear();
 
70
}
 
71
 
 
72
 
 
73
 
 
74
/****************************************************************************
 
75
   These function initializes/aborts any action, that's done by the scanner.
 
76
****************************************************************************/
 
77
 
 
78
void Smb4KScanner::m_init()
 
79
{
 
80
  m_config->setGroup( "Browse Options" );
 
81
  bool auth_for_browse_list = m_config->readBoolEntry( "Auth For Browse List", false );
 
82
  QString method = m_config->readEntry( "Browse List", "nmblookup" );
 
83
  QString host_to_query = m_config->readEntry( "Query Host", QString::null );
 
84
  
 
85
  QString wins = ((Smb4KCore *)parent())->getWINSServer();
 
86
  QString nmblookup_options = getNmblookupOptions();
 
87
  QString smbclient_options = getSmbclientOptions();
 
88
  
 
89
  QString command;
 
90
 
 
91
  // Use nmblookup or query host to obtain the browse list?
 
92
  if ( method == "nmblookup" )
 
93
  {
 
94
    command = QString( "nmblookup -M" );
 
95
 
 
96
    if ( !nmblookup_options.stripWhiteSpace().isEmpty() )
 
97
      command.append( nmblookup_options );
 
98
 
 
99
    command.append( " -- - | grep '<01>' | awk '{print $1}'" );
 
100
 
 
101
    // Query the WINS server, if available    
 
102
    if ( wins.isEmpty() )
 
103
      command.append( " | xargs nmblookup -A" );
 
104
    else
 
105
      command.append( QString( " | xargs nmblookup -R -U %1 -A" ).arg( wins ) );
 
106
 
 
107
    *m_proc << command;
 
108
    startProcess( Groups );
 
109
  }
 
110
  else if ( method == "host" )
 
111
  {
 
112
    command = QString( "smbclient -d1" );
 
113
 
 
114
    if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
115
    {
 
116
      command.append( smbclient_options );
 
117
    }
 
118
 
 
119
    if ( auth_for_browse_list )
 
120
    {
 
121
      Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( QString::null, host_to_query, QString::null );
 
122
 
 
123
      if ( !auth->user().isEmpty() )
 
124
      {
 
125
        command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
 
126
 
 
127
        if ( !auth->password().isEmpty() )
 
128
        {
 
129
          m_proc->setEnvironment( "PASSWD", auth->password() );
 
130
        }
 
131
      }
 
132
      else
 
133
      {
 
134
        command.append( " -U %" );
 
135
      }
 
136
    }
 
137
    else
 
138
    {
 
139
      command.append( " -U %" );
 
140
    }
 
141
 
 
142
    command.append( QString( " -L %1" ).arg( KProcess::quote( host_to_query ) ) );
 
143
 
 
144
    *m_proc << command;
 
145
    
 
146
    startProcess( QueryHost );
 
147
  }
 
148
}
 
149
 
 
150
 
 
151
/****************************************************************************
 
152
   Aborts any process that is running.
 
153
****************************************************************************/
 
154
 
 
155
void Smb4KScanner::abort()
 
156
{
 
157
  if ( m_proc->isRunning() )
 
158
  {
 
159
    m_proc->kill();
 
160
    m_queue.clear();
 
161
    emit killed();
 
162
  }
 
163
}
 
164
 
 
165
 
 
166
/****************************************************************************
 
167
   Gets the preview of a share. (public part)
 
168
****************************************************************************/
 
169
 
 
170
void Smb4KScanner::getPreview( const QString &workgroup, const QString &host, const QString &ip, const QString &share, const QString &path )
 
171
{
 
172
  m_queue.enqueue( new QString( QString( "%1:%2:%3:%4:%5:%6" ).arg( Preview ).arg( workgroup ).arg( host ).arg( ip ).arg( share ).arg( path ) ) );
 
173
}
 
174
 
 
175
 
 
176
/****************************************************************************
 
177
   Gets the preview of a share. (private part)
 
178
****************************************************************************/
 
179
 
 
180
void Smb4KScanner::m_previewShare( const QString &workgroup, const QString &host, const QString &ip, const QString &share, const QString &path )
 
181
{
 
182
  QString smbclient_options = getSmbclientOptions();
 
183
  
 
184
  m_workgroup = workgroup;
 
185
  m_host = host;
 
186
  m_share = share;
 
187
  m_path = path;
 
188
  m_ip = ip;
 
189
 
 
190
  QString command;
 
191
  Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, host, share );
 
192
 
 
193
  // Assemble command:
 
194
  command = QString( "smbclient //%1/%2 -d1 -W %3 -c 'ls" ).arg( KProcess::quote( host ) ).arg( KProcess::quote( share ) ).arg( KProcess::quote( workgroup ) );
 
195
 
 
196
  if ( !path.isEmpty() )
 
197
  {
 
198
    // To be able to display directories, that contain umlauts, we have to do
 
199
    // two things:
 
200
    // (1) replace all slashes by backslashes;
 
201
    // (2) convert the directory name to local 8 bit.
 
202
    QString proc_path = path;
 
203
    proc_path = proc_path.replace( QChar( '/' ), QChar( '\\' ) ).local8Bit();
 
204
    command.append( " \"" ).append( proc_path ).append( "*\"" );
 
205
  }
 
206
  
 
207
  command.append( "'" );
 
208
  
 
209
  if ( !ip.isEmpty() )
 
210
  {
 
211
    command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
 
212
  }
 
213
 
 
214
  if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
215
  {
 
216
    command.append( smbclient_options );
 
217
  }
 
218
 
 
219
  if ( !auth->user().isEmpty() )
 
220
  {
 
221
    command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
 
222
    
 
223
    if ( !auth->password().isEmpty() )
 
224
    {
 
225
      m_proc->setEnvironment( "PASSWD", auth->password() );
 
226
    }
 
227
  }
 
228
  else
 
229
  {
 
230
    command.append( " -U %" );
 
231
  }
 
232
 
 
233
  m_proc->operator<<( command );
 
234
  startProcess( Preview );
 
235
}
 
236
 
 
237
 
 
238
/****************************************************************************
 
239
   Scans for workgroup members. (public part)
 
240
****************************************************************************/
 
241
 
 
242
void Smb4KScanner::getWorkgroupMembers( const QString &workgroup, const QString &master, const QString &ip )
 
243
{
 
244
  m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Hosts ).arg( workgroup ).arg( master ).arg( ip ) ) );
 
245
}
 
246
 
 
247
 
 
248
/****************************************************************************
 
249
   Scans for workgroup members. (private part)
 
250
****************************************************************************/
 
251
 
 
252
void Smb4KScanner::m_scanForWorkgroupMembers( const QString &workgroup, const QString &master, const QString &ip )
 
253
{
 
254
  m_config->setGroup( "Browse Options" );
 
255
  bool master_auth = m_config->readBoolEntry( "Master Browser Auth", false );
 
256
  
 
257
  QString smbclient_options = getSmbclientOptions();
 
258
  
 
259
  m_workgroup = workgroup;
 
260
  m_host = master;
 
261
  m_ip = ip;
 
262
 
 
263
  // Scan for the host list.
 
264
  QString command = QString( "smbclient -d1" );
 
265
 
 
266
  if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
267
  {
 
268
    command.append( smbclient_options );
 
269
  }
 
270
 
 
271
  if ( master_auth )
 
272
  {
 
273
    Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, master, QString::null );
 
274
 
 
275
    if ( !auth->user().isEmpty() )
 
276
    {
 
277
      command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
 
278
 
 
279
      if ( !auth->password().isEmpty() )
 
280
      {
 
281
        m_proc->setEnvironment( "PASSWD", auth->password() );
 
282
      }
 
283
    }
 
284
    else
 
285
    {
 
286
      command.append( " -U %" );
 
287
    }
 
288
  }
 
289
  else
 
290
  {
 
291
    command.append( " -U %" );
 
292
  }
 
293
 
 
294
  if ( !ip.isEmpty() )
 
295
  {
 
296
    command.append( QString( " -I %1" ).arg( ip ) );
 
297
  }
 
298
    
 
299
  command.append( QString( " -W %1 -L %2" ).arg( KProcess::quote( workgroup ) ).arg( KProcess::quote( master ) ) );
 
300
 
 
301
  m_proc->operator<<( command );
 
302
  startProcess( Hosts );
 
303
}
 
304
 
 
305
 
 
306
/****************************************************************************
 
307
   Scans for shares on a selected host. (public part)
 
308
****************************************************************************/
 
309
 
 
310
void Smb4KScanner::getShares( const QString &workgroup, const QString &host, const QString &ip )
 
311
{
 
312
  m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( workgroup ).arg( host ).arg( ip ) ) );
 
313
}
 
314
 
 
315
 
 
316
/****************************************************************************
 
317
   Scans for shares on a selected host. (private part)
 
318
****************************************************************************/
 
319
 
 
320
void Smb4KScanner::m_scanForShares( const QString &workgroup, const QString &host, const QString &ip )
 
321
{
 
322
  QString smbclient_options = getSmbclientOptions();
 
323
  
 
324
  m_workgroup = workgroup;
 
325
  m_host = host;
 
326
  m_ip = ip;
 
327
 
 
328
  Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, host, QString::null );
 
329
 
 
330
  // Assemble the scan command.
 
331
  QString command = QString( "smbclient -d1 -W %1" ).arg( KProcess::quote( workgroup ) );
 
332
  
 
333
  if ( !ip.isEmpty() )
 
334
  {
 
335
    command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
 
336
  }
 
337
 
 
338
  if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
339
  {
 
340
    command.append( smbclient_options );
 
341
  }
 
342
 
 
343
  if ( !auth->user().isEmpty() )
 
344
  {
 
345
    command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
 
346
    
 
347
    if ( !auth->password().isEmpty() )
 
348
    {
 
349
      m_proc->setEnvironment( "PASSWD", auth->password() );
 
350
    }
 
351
  }
 
352
  else
 
353
  {
 
354
    command.append( " -U guest%" );
 
355
  }
 
356
 
 
357
  command.append( " -L " ).append( KProcess::quote( host ) );
 
358
  
 
359
  m_proc->operator<<( command );
 
360
  startProcess( Shares );
 
361
}
 
362
 
 
363
 
 
364
/****************************************************************************
 
365
   Gets more info on a selected host. (public part)
 
366
****************************************************************************/
 
367
 
 
368
void Smb4KScanner::getInfo( const QString &workgroup, const QString &host, const QString &ip )
 
369
{
 
370
  m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Info ).arg( workgroup ).arg( host ).arg( ip ) ) );
 
371
}
 
372
 
 
373
 
 
374
/****************************************************************************
 
375
   Gets more info on a selected host. (private part)
 
376
****************************************************************************/
 
377
 
 
378
void Smb4KScanner::m_scanForInfo( const QString &workgroup, const QString &host, const QString &ip )
 
379
{
 
380
  QString smbclient_options = getSmbclientOptions();
 
381
  
 
382
  m_workgroup = workgroup;
 
383
  m_host = host;
 
384
  
 
385
  QString command = QString( "smbclient -d1 -U % -W %1 -L %2" ).arg( KProcess::quote( workgroup ) ).arg( KProcess::quote( host ) );
 
386
  
 
387
  if ( !ip.isEmpty() )
 
388
  {
 
389
    command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
 
390
  }
 
391
 
 
392
  if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
393
  {
 
394
    command.append( smbclient_options );
 
395
  }
 
396
 
 
397
  m_proc->operator<<( command );
 
398
  
 
399
  startProcess( Info );
 
400
}
 
401
 
 
402
 
 
403
/****************************************************************************
 
404
   Searches for a host. (public part)
 
405
****************************************************************************/
 
406
 
 
407
void Smb4KScanner::makeSearch( const QString &host )
 
408
{
 
409
  m_queue.enqueue( new QString( QString( "%1:%2" ).arg( Search ).arg( host ) ) );
 
410
}
 
411
 
 
412
 
 
413
/****************************************************************************
 
414
   Searches for a host. (private part)
 
415
****************************************************************************/
 
416
 
 
417
void Smb4KScanner::m_searchForHost( const QString &host )
 
418
{
 
419
  m_config->setGroup( "Browse Options" );
 
420
  QString search_method = m_config->readEntry( "Network Search", "nmblookup" );
 
421
 
 
422
  if ( search_method == "smbclient" && host.stripWhiteSpace().contains( "." ) == 3 )
 
423
  {
 
424
    emit error( ERROR_IP_CANNOT_BE_USED, QString::null );
 
425
    m_working = false;
 
426
    emit running( SCANNER_STOP, m_working );    
 
427
    return;
 
428
  }
 
429
  
 
430
  QString wins = ((Smb4KCore *)parent())->getWINSServer();
 
431
  QString nmblookup_options = getNmblookupOptions();
 
432
  QString smbclient_options = getSmbclientOptions();
 
433
      
 
434
  m_host = host;
 
435
  
 
436
  QString command;
 
437
 
 
438
  if ( search_method == "nmblookup" )
 
439
  {
 
440
    command = QString( "nmblookup" );
 
441
 
 
442
    if ( !nmblookup_options.stripWhiteSpace().isEmpty() )
 
443
    {
 
444
      command.append( nmblookup_options );
 
445
    }
 
446
 
 
447
    if ( host.contains( '.', true ) != 3 )
 
448
    {
 
449
      if ( !wins.isEmpty() )
 
450
      {
 
451
        command.append( QString( " -R -U %1 %2 -S | grep '<00>' | sed -e 's/<00>.*//'" ).arg( wins ).arg( m_host ) );
 
452
      }
 
453
      else
 
454
      {
 
455
        command.append( QString( " %1 -S | grep '<00>' | sed -e 's/<00>.*//'" ).arg( m_host ) );
 
456
      }
 
457
    }
 
458
    else
 
459
    {
 
460
      if ( !wins.isEmpty() )
 
461
      {
 
462
        command.append( QString( " -R -U %1 %2 -A | grep '<00>' | sed -e 's/<00>.*//'" ).arg( wins ).arg( m_host ) );
 
463
      }
 
464
      else
 
465
      {
 
466
        command.append( QString( " %1 -A | grep '<00>' | sed -e 's/<00>.*//'" ).arg( m_host ) );
 
467
      }
 
468
    }
 
469
  }
 
470
  else
 
471
  {
 
472
    command = QString( "smbclient -d2 -U % -L %1" ).arg( m_host );
 
473
 
 
474
    if ( !smbclient_options.stripWhiteSpace().isEmpty() )
 
475
    {
 
476
      command.append( smbclient_options );
 
477
    }
 
478
  }
 
479
  
 
480
  m_proc->operator<<( command );
 
481
  
 
482
  startProcess( Search );
 
483
}
 
484
 
 
485
 
 
486
/****************************************************************************
 
487
   Starts the scanner.
 
488
****************************************************************************/
 
489
 
 
490
void Smb4KScanner::startProcess( int state )
 
491
{
 
492
  m_state = state;
 
493
  m_buffer = QString::null;
 
494
  QApplication::setOverrideCursor( waitCursor );
 
495
  m_proc->start( KProcess::NotifyOnExit, KProcess::AllOutput );
 
496
}
 
497
 
 
498
 
 
499
/****************************************************************************
 
500
   Tell the program what it has to do.
 
501
****************************************************************************/
 
502
 
 
503
void Smb4KScanner::endProcess()
 
504
{
 
505
  switch ( m_state )
 
506
  {
 
507
    case Groups:
 
508
      processWorkgroups();
 
509
      break;
 
510
    case QueryHost:
 
511
      processQueryHost();
 
512
      break;
 
513
    case Hosts:
 
514
      processHosts();
 
515
      break;
 
516
    case Shares:
 
517
      processShares();
 
518
      break;
 
519
    case Info:
 
520
      processInfo();
 
521
      break;
 
522
    case Preview:
 
523
      processPreview();
 
524
      break;
 
525
    case Search:
 
526
      processSearch();
 
527
      break;
 
528
    default:
 
529
      break;
 
530
  }
 
531
  m_state = Idle;
 
532
  m_workgroup = QString::null;
 
533
  m_host = QString::null;
 
534
  m_ip = QString::null;
 
535
  m_share = QString::null;
 
536
  m_path = QString::null;
 
537
  QApplication::restoreOverrideCursor();
 
538
  m_proc->clearArguments();
 
539
  
 
540
  m_working = false;
 
541
  emit running( SCANNER_STOP, m_working );
 
542
}
 
543
 
 
544
 
 
545
/****************************************************************************
 
546
   Process the list of workgroups.
 
547
****************************************************************************/
 
548
 
 
549
void Smb4KScanner::processWorkgroups()
 
550
{
 
551
  QStringList list = QStringList::split( '\n', m_buffer, false );
 
552
  
 
553
  m_workgroupList.clear();
 
554
  
 
555
  QString group, master, ip;
 
556
  
 
557
  for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
 
558
  {
 
559
    if ( (*it).stripWhiteSpace().startsWith( "Looking" ) )
 
560
    {
 
561
      ip = (*it).stripWhiteSpace().section( " ", -1, -1 );
 
562
      continue;
 
563
    }
 
564
    else if ( (*it).contains( "<00>" ) != 0 && (*it).contains( "<GROUP>" ) == 0 )
 
565
    {
 
566
      if ( group.isEmpty() && master.isEmpty() && !ip.isEmpty() )
 
567
        master = (*it).section( "<00>", 0, 0 ).stripWhiteSpace();
 
568
        
 
569
      continue;
 
570
    }
 
571
    else if ( (*it).contains( "<00>" ) != 0 && (*it).contains( "<GROUP>" ) != 0 )
 
572
    {
 
573
      if ( group.isEmpty() && !master.isEmpty() && !ip.isEmpty() )
 
574
      {
 
575
        group = (*it).left( (*it).find( "<00>" ) ).stripWhiteSpace();
 
576
        
 
577
        m_workgroupList.append( new Smb4KWorkgroupItem( group, master, ip ) );
 
578
        
 
579
        group = QString::null;
 
580
        master = QString::null;
 
581
        ip = QString::null;
 
582
      }
 
583
      
 
584
      continue;
 
585
    }
 
586
  }
 
587
  
 
588
  emit workgroups( m_workgroupList );
 
589
}
 
590
 
 
591
 
 
592
void Smb4KScanner::processQueryHost()
 
593
{
 
594
  m_config->setGroup( "Browse Options" );
 
595
  QString host_to_query = m_config->readEntry( "Query Host", QString::null );
 
596
  
 
597
  QStringList list = QStringList::split( '\n', m_buffer, false );
 
598
  
 
599
  m_workgroupList.clear();
 
600
  
 
601
  if ( list.grep( "tree connect failed: NT_STATUS_ACCESS_DENIED" ).count() == 0 && 
 
602
       list.grep( "NT_STATUS_LOGON_FAILURE" ).count() == 0 &&
 
603
       list.grep( "Connection to "+host_to_query+" failed" ).count() == 0 )
 
604
  {
 
605
    // Get the line starting with Workgroup.
 
606
    uint index( 0 );
 
607
 
 
608
    while( index < list.count() )
 
609
    {
 
610
      if ( list[index++].stripWhiteSpace().startsWith( "Workgroup" ) )
 
611
        break;
 
612
      else
 
613
        continue;
 
614
    }
 
615
 
 
616
    index++;
 
617
 
 
618
    while ( index < list.count() )
 
619
    {
 
620
      QString line = list[index].stripWhiteSpace();
 
621
      QString master, workgroup;
 
622
      master = line.section( "   ", -1, -1 ).stripWhiteSpace();
 
623
      workgroup = line.section( "   ", 0, -2 ).stripWhiteSpace();
 
624
 
 
625
      if ( workgroup.isEmpty() )
 
626
      {
 
627
        if ( master.isEmpty() )
 
628
          break;
 
629
        else
 
630
          continue;
 
631
      }
 
632
      else
 
633
        m_workgroupList.append( new Smb4KWorkgroupItem( workgroup, master ) );
 
634
              
 
635
      index++;
 
636
    }
 
637
 
 
638
    emit workgroups( m_workgroupList );
 
639
  }
 
640
  else
 
641
  {
 
642
    emit workgroups( m_workgroupList );
 
643
    
 
644
    if ( list.grep( "tree connect failed: NT_STATUS_ACCESS_DENIED" ).count() != 0 )
 
645
    {
 
646
      // We should find an entry for the workgroup here:
 
647
      QString workgroup = list.grep( "Domain" ).first().section( "Domain=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace();
 
648
 
 
649
      if ( !workgroup.isEmpty() )
 
650
      {
 
651
        if ( ((Smb4KCore *)parent())->passwdReader()->askpass( workgroup, host_to_query, QString::null, Smb4KPasswdReader::AccessDenied ) )
 
652
        {
 
653
          QString *input = new QString( QString( "%1:" ).arg( Init ) );
 
654
          m_queue.enqueue( input );
 
655
        }
 
656
      }
 
657
      else
 
658
      {
 
659
        emit error( ERROR_GETTING_BROWSELIST, list.grep( "NT_STATUS" ).first() );
 
660
      }
 
661
    }
 
662
    else if ( list.grep( "NT_STATUS_LOGON_FAILURE" ).count() != 0 )
 
663
    {
 
664
      emit error( ERROR_GETTING_BROWSELIST, m_buffer );    
 
665
    }
 
666
    else if ( list.grep( "Connection to "+host_to_query+" failed" ).count() != 0 )
 
667
    {
 
668
      emit error( ERROR_GETTING_BROWSELIST, m_buffer ); 
 
669
    }
 
670
  }
 
671
}
 
672
 
 
673
 
 
674
/****************************************************************************
 
675
   Process the memeber list of a workgroup.
 
676
****************************************************************************/
 
677
 
 
678
void Smb4KScanner::processHosts()
 
679
{
 
680
  QStringList list = QStringList::split( '\n', m_buffer, false );
 
681
 
 
682
  if ( list.grep( "Connection to" ).count() != 0 )
 
683
  {
 
684
    QString errmsg = list.grep( "Connection to" ).first().stripWhiteSpace();
 
685
    emit error( ERROR_GETTING_MEMBERS, errmsg );
 
686
  }
 
687
  else if ( m_buffer.contains( "tree connect failed: NT_STATUS_ACCESS_DENIED" ) == 0 
 
688
            && m_buffer.contains( "NT_STATUS_LOGON_FAILURE" ) != 0  )
 
689
  {
 
690
    if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, Smb4KPasswdReader::LogonFailure ) )
 
691
    {
 
692
      QString *input = new QString( QString( "%1:%2:%3" ).arg( Hosts ).arg( m_workgroup ).arg( m_host ) );
 
693
      m_queue.enqueue( input );
 
694
    }
 
695
  }
 
696
  else
 
697
  {
 
698
    // Get the begin of the host listing.
 
699
    uint index( 0 );
 
700
    for ( ; index < list.count(); index++ )
 
701
      if ( list[index].stripWhiteSpace().startsWith( "Server" ) )
 
702
        break;
 
703
 
 
704
    index += 2;
 
705
 
 
706
    // Process list of hosts:
 
707
    QValueList<Smb4KHostItem *> hosts;
 
708
    
 
709
    QString line;
 
710
    
 
711
    while ( index < list.count() )
 
712
    {
 
713
      line = list[index++].stripWhiteSpace();
 
714
      
 
715
      if ( line.startsWith( "Workgroup" ) )
 
716
      {
 
717
        break;
 
718
      }
 
719
      else if ( line.contains( "  " ) == 0 )
 
720
      {
 
721
        hosts.append( new Smb4KHostItem( m_workgroup, line ) );
 
722
      }
 
723
      else
 
724
      {
 
725
        hosts.append( new Smb4KHostItem( m_workgroup, line.section( "  ", 0, 0 ).stripWhiteSpace(), line.section( "  ", 1, -1 ).stripWhiteSpace() ) );
 
726
      }
 
727
    }
 
728
    
 
729
    // If the list is empty, put the master in.
 
730
    if ( hosts.isEmpty() )
 
731
      hosts.append( new Smb4KHostItem( m_workgroup, m_host ) );
 
732
    
 
733
    emit members( hosts );
 
734
  }
 
735
}
 
736
 
 
737
 
 
738
/****************************************************************************
 
739
   Process the share list of a host.
 
740
****************************************************************************/
 
741
 
 
742
void Smb4KScanner::processShares()
 
743
{
 
744
  QStringList list = QStringList::split( "\n", m_buffer, false );
 
745
  
 
746
  // Get the OS and Server strings.
 
747
  Smb4KHostItem *host = new Smb4KHostItem( m_workgroup, m_host );
 
748
  uint index( 0 );
 
749
    
 
750
  for ( ; index < list.count(); index++ )
 
751
  {
 
752
    if ( list[index].stripWhiteSpace().startsWith( "Domain" ) || list[index].stripWhiteSpace().startsWith( "OS" ) )
 
753
    {
 
754
      host->setOSString( list[index].section( "OS=[", 1, 1 ).section( "]", 0, 0 ) );
 
755
      host->setServerString( list[index].section( "Server=[", 1, 1 ).section( "]", 0, 0 ) );
 
756
    }
 
757
    else if ( list[index].stripWhiteSpace().startsWith( "Sharename" ) )
 
758
    {
 
759
      break;
 
760
    }
 
761
    else
 
762
    {
 
763
      continue;
 
764
    }
 
765
  }
 
766
  
 
767
  emit info( host );
 
768
  
 
769
  delete host;
 
770
  
 
771
  // Now do the rest:
 
772
  if ( list.grep( "NT_STATUS" ).count() != 0 || list.grep( "Connection to" ).count() != 0 || list.grep( "tree connect failed:" ).count() != 0 )
 
773
  {
 
774
    if ( list.grep( "NT_STATUS" ).count() != 0  )
 
775
    {
 
776
      QString errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", -1, -1 );
 
777
 
 
778
      // The error output of smbclient is a little bit inconsistent:
 
779
      if ( errmsg.contains( "NT_STATUS" ) == 0 )
 
780
      {
 
781
        errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", 0, 0 );
 
782
      }
 
783
        
 
784
      if ( errmsg == "NT_STATUS_ACCESS_DENIED" || errmsg == "NT_STATUS_LOGON_FAILURE" )
 
785
      {
 
786
        int state = Smb4KPasswdReader::None;
 
787
        
 
788
        if ( errmsg == "NT_STATUS_ACCESS_DENIED" )
 
789
        {
 
790
          state = Smb4KPasswdReader::AccessDenied;
 
791
        }
 
792
        else if ( errmsg == "NT_STATUS_LOGON_FAILURE" )
 
793
        {
 
794
          state = Smb4KPasswdReader::LogonFailure;
 
795
        }
 
796
        
 
797
        if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, state )
 
798
)       {
 
799
          m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( m_workgroup ).arg( m_host ).arg( m_ip ) ) );
 
800
        }
 
801
      }
 
802
      else
 
803
      {
 
804
        emit error( ERROR_GETTING_SHARES, list.grep( "NT_STATUS" ).first().stripWhiteSpace() );
 
805
      }
 
806
    }
 
807
    else if ( list.grep( "tree connect failed:" ).count() != 0 )
 
808
    {
 
809
      if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, Smb4KPasswdReader::AccessDenied ) )
 
810
      {
 
811
        m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( m_workgroup ).arg( m_host ).arg( m_ip ) ) );
 
812
      }
 
813
    }
 
814
    else
 
815
    {
 
816
      emit error( ERROR_GETTING_SHARES, list.grep( "Connection to" ).first().stripWhiteSpace() );
 
817
    }
 
818
  }
 
819
  else if ( list.grep( "Error returning browse list:" ).count() != 0 && list.grep( "NT_STATUS" ).count() == 0 )
 
820
  {
 
821
    emit error( ERROR_GETTING_SHARES, list.grep( "Error returning browse list:" ).first().stripWhiteSpace() );
 
822
  }
 
823
  else
 
824
  {
 
825
    index += 2;
 
826
 
 
827
    // Process the share listing.
 
828
    QValueList<Smb4KShareItem *> sharesList;
 
829
 
 
830
    QString line;
 
831
       
 
832
    for ( ; index < list.count(); index++ )
 
833
    {
 
834
      line = list[index].stripWhiteSpace();
 
835
      
 
836
      QString name, type, comment;
 
837
      
 
838
      if ( line.contains( "Disk" ) )
 
839
      {
 
840
        name = line.section( "Disk", 0, 0 ).stripWhiteSpace();
 
841
        type = "Disk";
 
842
        comment = line.section( "Disk", 1, 1 ).stripWhiteSpace();
 
843
      }
 
844
      else if ( line.contains( "Printer" ) )
 
845
      {
 
846
        name = line.section( "Printer", 0, 0 ).stripWhiteSpace();
 
847
        type = "Printer";
 
848
        comment = line.section( "Printer", 1, 1 ).stripWhiteSpace();        
 
849
      }
 
850
      else if ( line.contains( "  IPC  " ) )
 
851
      {
 
852
        name = line.section( "  IPC  ", 0, 0 ).stripWhiteSpace();
 
853
        type = "IPC";
 
854
        comment = line.section( "  IPC  ", 1, 1 ).stripWhiteSpace();
 
855
      }
 
856
      else if ( line.startsWith( "Anonymous" ) )
 
857
      {
 
858
        continue;
 
859
      }
 
860
      else if ( line.startsWith( "Server" ) )
 
861
      {
 
862
        break;
 
863
      }
 
864
      
 
865
      sharesList.append( new Smb4KShareItem( m_workgroup, m_host, name, type, comment ) );
 
866
    }
 
867
 
 
868
    emit shares( sharesList );
 
869
  }
 
870
}
 
871
 
 
872
 
 
873
/****************************************************************************
 
874
   Process the information about a host.
 
875
****************************************************************************/
 
876
 
 
877
void Smb4KScanner::processInfo()
 
878
{
 
879
  QStringList list = QStringList::split( '\n', m_buffer, false );
 
880
  
 
881
  Smb4KHostItem *host = new Smb4KHostItem( m_workgroup, m_host );
 
882
 
 
883
  for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
 
884
  {
 
885
    if ( (*it).stripWhiteSpace().startsWith( "Domain" ) || (*it).stripWhiteSpace().startsWith( "OS" ) )
 
886
    {
 
887
      // The OS string.
 
888
      host->setOSString( (*it).section( "OS=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace() );
 
889
      
 
890
      // The Server string.
 
891
      host->setServerString( (*it).section( "Server=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace() );
 
892
      
 
893
      break;
 
894
    }
 
895
    else if ( (*it).contains( "Connection to", true ) != 0 )
 
896
    {
 
897
      break;
 
898
    }
 
899
  }
 
900
  
 
901
  emit info( host );
 
902
  
 
903
  delete host;
 
904
}
 
905
 
 
906
 
 
907
/****************************************************************************
 
908
   Process the preview.
 
909
****************************************************************************/
 
910
 
 
911
void Smb4KScanner::processPreview()
 
912
{
 
913
  QStringList list = QStringList::split( '\n', m_buffer, false );
 
914
 
 
915
  QValueList<Smb4KPreviewItem *> preview;
 
916
  
 
917
  // Now do the rest:
 
918
  if ( list.grep( "NT_STATUS" ).count() != 0 || list.grep( "Connection to" ).count() != 0 )
 
919
  {
 
920
    if ( list.grep( "NT_STATUS" ).count() != 0  )
 
921
    {
 
922
      QString errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", 0, 0 );
 
923
      
 
924
      // The error output of smbclient is a little bit inconsistent:
 
925
      if ( errmsg.contains( "NT_STATUS" ) == 0 )
 
926
      {
 
927
        errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", -1, -1 );
 
928
      }
 
929
      
 
930
      if ( errmsg == "NT_STATUS_ACCESS_DENIED" || errmsg == "NT_STATUS_LOGON_FAILURE" )
 
931
      {
 
932
        int state = Smb4KPasswdReader::None;
 
933
        
 
934
        if ( errmsg == "NT_STATUS_ACCESS_DENIED" )
 
935
        {
 
936
          state = Smb4KPasswdReader::AccessDenied;
 
937
        }
 
938
        else if ( errmsg == "NT_STATUS_LOGON_FAILURE" )
 
939
        {
 
940
          state = Smb4KPasswdReader::LogonFailure;
 
941
        }
 
942
        
 
943
        if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, m_share, state ) )
 
944
        {
 
945
          QString *input = new QString( QString( "%1:%2:%3:%4:%5:%6" ).arg( Preview ).arg( m_workgroup ).arg( m_host ).arg( m_ip ).arg( m_share ).arg( m_path ) );
 
946
          m_queue.enqueue( input );
 
947
        }
 
948
      }
 
949
      else
 
950
      {
 
951
        emit error( ERROR_GETTING_PREVIEW, list.grep( "NT_STATUS" ).first().stripWhiteSpace() );
 
952
      }
 
953
    }
 
954
    else
 
955
    {
 
956
      emit error( ERROR_GETTING_PREVIEW, list.grep( "Connection to" ).first().stripWhiteSpace() );
 
957
    }
 
958
  }
 
959
  else if ( list.grep( "Error returning browse list:" ).count() != 0 && list.grep( "NT_STATUS" ).count() == 0 )
 
960
  {
 
961
    emit error( ERROR_GETTING_PREVIEW, list.grep( "Error returning browse list:" ).first().stripWhiteSpace() );
 
962
  }
 
963
  else
 
964
  {
 
965
    for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
 
966
    {
 
967
      if ( (*it).stripWhiteSpace().startsWith( "Domain" ) || 
 
968
           (*it).stripWhiteSpace().startsWith( "OS" ) ||
 
969
           (*it).stripWhiteSpace().startsWith( "Anonymous" ) )
 
970
      {
 
971
        continue;
 
972
      }
 
973
      else if ( (*it).contains( "blocks of size" ) != 0 )
 
974
      {
 
975
        continue;
 
976
      }
 
977
      else
 
978
      {
 
979
        QString tmp = (*it).stripWhiteSpace().section( " ", 0, -9 ).stripWhiteSpace();
 
980
        
 
981
        QString item = tmp.section( "  ", 0, -2 ).stripWhiteSpace();
 
982
        
 
983
        bool isFile = true;
 
984
        
 
985
        if ( !item.isEmpty() && tmp.section( "  ", -1, -1 ).contains( "D" ) != 0 )
 
986
          isFile = false;
 
987
        else if ( item.isEmpty() )
 
988
          item = tmp;
 
989
        
 
990
        Smb4KPreviewItem *i = new Smb4KPreviewItem( m_host, m_share, m_path, item, isFile );
 
991
        
 
992
        preview.append( i );
 
993
      }
 
994
    }
 
995
  }
 
996
  
 
997
  emit previewResult( preview );
 
998
}
 
999
 
 
1000
 
 
1001
/****************************************************************************
 
1002
   Process the search data.
 
1003
****************************************************************************/
 
1004
 
 
1005
void Smb4KScanner::processSearch()
 
1006
{
 
1007
  m_config->setGroup( "Browse Options" );
 
1008
  QString search_method = m_config->readEntry( "Network Search", "nmblookup" );
 
1009
    
 
1010
  QStringList data = QStringList::split( "\n", m_buffer.stripWhiteSpace(), false );
 
1011
  
 
1012
  if ( search_method == "nmblookup" )
 
1013
  {
 
1014
    if ( !data.isEmpty() )
 
1015
    {
 
1016
      // The last entry in the list is the workgroup:
 
1017
      QString workgroup = data.last().stripWhiteSpace();
 
1018
      QString host, ip;
 
1019
      
 
1020
      if ( m_host.contains( ".", true ) != 3 )
 
1021
      {
 
1022
        // The IP address is in the first entry:
 
1023
        ip = data.first().stripWhiteSpace().section( " ", 0, 0 );
 
1024
        // The host.
 
1025
        host = m_host.upper();
 
1026
      }
 
1027
      else
 
1028
      {
 
1029
        ip = m_host;
 
1030
        host = data[0].stripWhiteSpace();        
 
1031
      }
 
1032
 
 
1033
      emit searchResult( new Smb4KHostItem( workgroup, host, QString::null, ip ) );
 
1034
    }
 
1035
    else
 
1036
      emit searchResult( new Smb4KHostItem() );
 
1037
  }
 
1038
  else
 
1039
  {
 
1040
    if ( data.count() > 1 && !data[1].isEmpty() )
 
1041
    {
 
1042
      if ( m_buffer.contains( QString( "Connection to %1 failed" ).arg( m_host ) ) != 0 )
 
1043
      {
 
1044
        emit searchResult( new Smb4KHostItem() );
 
1045
      }
 
1046
      else
 
1047
      {
 
1048
        QString workgroup = data.grep( "Domain" ).first().section( "Domain=[", 1, 1 ).section( "]", 0, 0 );
 
1049
        QString ip = data.grep( "Got a positive name query" ).first().section( "(", 1, 1 ).section( ")", 0, 0 ).stripWhiteSpace();
 
1050
 
 
1051
        emit searchResult( new Smb4KHostItem( workgroup, m_host.upper(), QString::null, ip ) );
 
1052
      }
 
1053
    }
 
1054
    else
 
1055
      emit searchResult( new Smb4KHostItem() );
 
1056
  }
 
1057
}
 
1058
 
 
1059
 
 
1060
/****************************************************************************
 
1061
   Appends an item to the list of workgroups.
 
1062
****************************************************************************/
 
1063
 
 
1064
void Smb4KScanner::appendWorkgroup( Smb4KWorkgroupItem *item )
 
1065
{
 
1066
  item->setPseudo();
 
1067
  
 
1068
  if ( getWorkgroup( item->workgroup() ) == 0 )
 
1069
  {
 
1070
    m_workgroupList.append( item );
 
1071
  }
 
1072
}
 
1073
 
 
1074
 
 
1075
/****************************************************************************
 
1076
   Get a workgroup item out of the workgroup list.
 
1077
****************************************************************************/
 
1078
 
 
1079
Smb4KWorkgroupItem *Smb4KScanner::getWorkgroup( const QString &workgroup )
 
1080
{
 
1081
  QValueListIterator<Smb4KWorkgroupItem *> it;
 
1082
  
 
1083
  for ( it = m_workgroupList.begin(); it != m_workgroupList.end(); ++it )
 
1084
  {
 
1085
    if ( (*it)->workgroup() == workgroup )
 
1086
      break;
 
1087
    else
 
1088
      continue;
 
1089
  }
 
1090
  
 
1091
  return it == m_workgroupList.end() ? 0 : *it;
 
1092
}
 
1093
 
 
1094
 
 
1095
/****************************************************************************
 
1096
   Returns the options for nmblookup
 
1097
****************************************************************************/
 
1098
 
 
1099
const QString Smb4KScanner::getNmblookupOptions()
 
1100
{
 
1101
  QString options;
 
1102
  
 
1103
  m_config->setGroup( "Samba" );
 
1104
  if ( !m_config->readEntry( "NetBIOS Name", QString::null ).isEmpty() )
 
1105
    options.append( QString( " -n \"%1\"" ).arg( m_config->readEntry( "NetBIOS Name", QString::null ) ) );
 
1106
 
 
1107
  if ( !m_config->readEntry( "NetBIOS Scope", QString::null ).isEmpty() )
 
1108
    options.append( QString( " -i \"%1\"" ).arg( m_config->readEntry( "NetBIOS Scope", QString::null ) ) );
 
1109
 
 
1110
  if ( !m_config->readEntry( "Socket Options", QString::null ).isEmpty() )
 
1111
    options.append( QString( " -O \"%1\"" ).arg( m_config->readEntry( "Socket Options", QString::null ) ) );
 
1112
  
 
1113
  if ( !m_config->readEntry( "NMB Domain", QString::null ).isEmpty() )
 
1114
  {
 
1115
    if ( ((Smb4KCore *)parent())->getSambaVersion().startsWith( "3" ) )
 
1116
    {
 
1117
      options.append( QString( " -W \"%1\"" ).arg( m_config->readEntry( "NMB Domain", QString::null ) ) );
 
1118
    }
 
1119
  }
 
1120
  
 
1121
  if ( !m_config->readEntry( "NMB Broadcast", QString::null ).isEmpty() )
 
1122
    options.append( QString( " -B \"%1\"" ).arg( m_config->readEntry( "NMB Broadcast", QString::null ) ) );
 
1123
    
 
1124
  if ( m_config->readBoolEntry( "NMB Port 137", false ) )
 
1125
    options.append( " -r" );  
 
1126
  
 
1127
  return options;
 
1128
}
 
1129
 
 
1130
 
 
1131
/****************************************************************************
 
1132
   Returns the options for smbclient
 
1133
****************************************************************************/
 
1134
 
 
1135
const QString Smb4KScanner::getSmbclientOptions()
 
1136
{
 
1137
  QString options;
 
1138
  
 
1139
  m_config->setGroup( "Samba" );
 
1140
 
 
1141
  if ( !m_config->readEntry( "Client Resolve Order", QString::null ).isEmpty() )
 
1142
    options.append( QString( " -R \"%1\"" ).arg( m_config->readEntry( "Client Resolve Order", QString::null ) ) );
 
1143
 
 
1144
  if ( !m_config->readEntry( "Port", QString::null ).isEmpty() )
 
1145
    options.append( QString( " -p %1" ).arg( m_config->readNumEntry( "Port", 139 ) ) );
 
1146
 
 
1147
  if ( !m_config->readEntry( "Client Buffer Size", QString::null ).isEmpty() )
 
1148
    options.append( QString( " -b %1" ).arg( m_config->readNumEntry( "Client Buffer Size", 65520 ) ) );
 
1149
 
 
1150
  if ( m_config->readBoolEntry( "Use Kerberos", false ) )
 
1151
  {
 
1152
    if ( ((Smb4KCore *)parent())->getSambaVersion().startsWith( "3" ) )
 
1153
      options.append( " -k" );
 
1154
  }
 
1155
 
 
1156
  if ( !m_config->readEntry( "NetBIOS Name", QString::null ).isEmpty() )
 
1157
    options.append( QString( " -n \"%1\"" ).arg( m_config->readEntry( "NetBIOS Name", QString::null ) ) );
 
1158
 
 
1159
  if ( !m_config->readEntry( "NetBIOS Scope", QString::null ).isEmpty() )
 
1160
    options.append( QString( " -i \"%1\"" ).arg( m_config->readEntry( "NetBIOS Scope", QString::null ) ) );
 
1161
 
 
1162
  if ( !m_config->readEntry( "Socket Options", QString::null ).isEmpty() )
 
1163
    options.append( QString( " -O \"%1\"" ).arg( m_config->readEntry( "Socket Options", QString::null ) ) );
 
1164
  
 
1165
  return options;
 
1166
}
 
1167
 
 
1168
 
 
1169
/////////////////////////////////////////////////////////////////////////////
 
1170
// SLOT IMPLEMENTATIONS
 
1171
/////////////////////////////////////////////////////////////////////////////
 
1172
 
 
1173
 
 
1174
/****************************************************************************
 
1175
   Internal slots.
 
1176
****************************************************************************/
 
1177
 
 
1178
void Smb4KScanner::slotReceivedStdout( KProcess *, char *buf, int len )
 
1179
{
 
1180
  m_buffer.append( QString::fromLocal8Bit( buf, len ) );
 
1181
}
 
1182
 
 
1183
 
 
1184
void Smb4KScanner::slotProcessExited( KProcess * )
 
1185
{
 
1186
  endProcess();
 
1187
}
 
1188
 
 
1189
 
 
1190
void Smb4KScanner::slotReceivedStderr( KProcess *, char  *buf, int len )
 
1191
{
 
1192
  m_buffer.append( QString::fromLocal8Bit( buf, len ) );
 
1193
}
 
1194
 
 
1195
 
 
1196
void Smb4KScanner::start()
 
1197
{
 
1198
  if ( !m_working && !m_queue.isEmpty() )
 
1199
  {
 
1200
    // Start processing...
 
1201
    QString *item = m_queue.dequeue();
 
1202
    
 
1203
    int todo = item->section( ":", 0, 0 ).toInt();
 
1204
    
 
1205
    // Tell the program, that the scanner is running again.
 
1206
    m_working = true;
 
1207
 
 
1208
    QString workgroup, host;
 
1209
 
 
1210
    switch ( todo )
 
1211
    {
 
1212
      case Init:
 
1213
        emit running( SCANNER_INIT, m_working );
 
1214
        m_init();
 
1215
        break;
 
1216
      case Hosts:
 
1217
        emit running( SCANNER_OPENING_WORKGROUP, m_working );
 
1218
        m_scanForWorkgroupMembers( item->section( ":", 1, 1 ), item->section( ":", 2, 2 ), item->section( ":", 3, 3 ) );
 
1219
        break;
 
1220
      case Shares:
 
1221
        emit running( SCANNER_OPENING_HOST, m_working );
 
1222
        workgroup = item->section( ":", 1, 1 );
 
1223
        host = item->section( ":", 2, 2 );
 
1224
        m_scanForShares( workgroup, host, item->section( ":", 3, 3) );
 
1225
        break;
 
1226
      case Info:
 
1227
        emit running( SCANNER_RETRIEVING_INFO, m_working );
 
1228
        workgroup = item->section( ":", 1, 1 );
 
1229
        host = item->section( ":", 2, 2 );
 
1230
        m_scanForInfo( workgroup, host, item->section( ":", 3, 3 ) );
 
1231
        break;
 
1232
      case Preview:
 
1233
        emit running( SCANNER_RETRIEVING_PREVIEW, m_working );
 
1234
        m_previewShare( item->section( ":", 1, 1 ), item->section( ":", 2, 2 ), item->section( ":", 3, 3 ), item->section( ":", 4, 4 ), item->section( ":", 5, 5 ) );
 
1235
        break;
 
1236
      case Search:
 
1237
        emit running( SCANNER_SEARCHING, m_working );
 
1238
        m_searchForHost( item->section( ":", 1, 1 ) );
 
1239
        break;
 
1240
      default:
 
1241
        break;
 
1242
    }
 
1243
 
 
1244
    // FIXME: The browser window sends todo == Shares and todo == GetInfo
 
1245
    // requests back-on-back. The latter are redundant, because the necessary
 
1246
    // info is also provided by processShares(). In the following we will remove
 
1247
    // these entries.
 
1248
    if ( !m_queue.isEmpty() )
 
1249
    {
 
1250
      if ( m_queue.head()->contains( QString( "%1" ).arg( Info ) ) && m_queue.head()->contains( workgroup ) && m_queue.head()->contains( host ) )
 
1251
      {
 
1252
        m_queue.remove();
 
1253
      }
 
1254
    }
 
1255
    
 
1256
    delete item;
 
1257
  }
 
1258
}
 
1259
 
 
1260
 
 
1261
/****************************************************************************
 
1262
   External slots.
 
1263
****************************************************************************/
 
1264
 
 
1265
void Smb4KScanner::slotInit()
 
1266
{
 
1267
  QString *input = new QString( QString( "%1:" ).arg( Init ) );
 
1268
  m_queue.enqueue( input );
 
1269
}
 
1270
 
 
1271
 
 
1272
#include "smb4kscanner.moc"
 
1273