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

« back to all changes in this revision

Viewing changes to core/smb4kscanner_p.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-06-15 16:27:38 UTC
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: package-import@ubuntu.com-20140615162738-t1426an8s5beix1b
Tags: upstream-1.1.2
ImportĀ upstreamĀ versionĀ 1.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    smb4kscanner_p  -  Private helper classes for the scanner
3
3
                             -------------------
4
4
    begin                : So Mai 22 2011
5
 
    copyright            : (C) 2011 by Alexander Reinholdt
 
5
    copyright            : (C) 2011-2013 by Alexander Reinholdt
6
6
    email                : alexander.reinholdt@kdemail.net
7
7
 ***************************************************************************/
8
8
 
19
19
 *                                                                         *
20
20
 *   You should have received a copy of the GNU General Public License     *
21
21
 *   along with this program; if not, write to the                         *
22
 
 *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
 
22
 *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
23
23
 *   MA 02110-1335, USA                                                    *
24
24
 ***************************************************************************/
25
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include <config.h>
 
28
#endif
 
29
 
 
30
// application specific includes
 
31
#include "smb4kscanner_p.h"
 
32
#include "smb4ksettings.h"
 
33
#include "smb4knotification.h"
 
34
#include "smb4kglobal.h"
 
35
#include "smb4kcustomoptionsmanager.h"
 
36
#include "smb4kcustomoptions.h"
 
37
#include "smb4kworkgroup.h"
 
38
#include "smb4khost.h"
 
39
#include "smb4kshare.h"
 
40
#include "smb4kwalletmanager.h"
 
41
 
26
42
// Qt includes
27
 
#include <QTimer>
28
 
#include <QDebug>
29
 
#include <QHostAddress>
30
 
#include <QAbstractSocket>
 
43
#include <QtCore/QTimer>
 
44
#include <QtCore/QDebug>
 
45
#include <QtCore/QLatin1String>
 
46
#include <QtNetwork/QHostAddress>
 
47
#include <QtNetwork/QAbstractSocket>
31
48
 
32
49
// KDE includes
33
50
#include <kstandarddirs.h>
34
51
#include <kshell.h>
35
52
 
36
 
// application specific includes
37
 
#include <smb4kscanner_p.h>
38
 
#include <smb4ksettings.h>
39
 
#include <smb4knotification.h>
40
 
#include <smb4kglobal.h>
41
 
#include <smb4kcustomoptionsmanager.h>
42
 
#include <smb4kcustomoptions.h>
43
 
#include <smb4kworkgroup.h>
44
 
#include <smb4khost.h>
45
 
#include <smb4kshare.h>
46
 
#include <smb4kwalletmanager.h>
47
 
 
48
53
using namespace Smb4KGlobal;
49
54
 
50
55
 
56
61
 
57
62
Smb4KLookupDomainsJob::~Smb4KLookupDomainsJob()
58
63
{
 
64
  while ( !m_workgroups_list.isEmpty() )
 
65
  {
 
66
    delete m_workgroups_list.takeFirst();
 
67
  }
59
68
}
60
69
 
61
70
 
62
71
void Smb4KLookupDomainsJob::start()
63
72
{
64
73
  m_started = true;
65
 
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
74
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
66
75
}
67
76
 
68
77
 
89
98
 
90
99
void Smb4KLookupDomainsJob::processWorkgroups()
91
100
{
92
 
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
101
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( '\n', QString::SkipEmptyParts );
93
102
 
94
103
  if ( !stdout.isEmpty() )
95
104
  {
96
 
    Smb4KWorkgroup workgroup;
 
105
    Smb4KWorkgroup *workgroup = new Smb4KWorkgroup();
97
106
 
98
107
    foreach ( const QString &line, stdout )
99
108
    {
100
 
      if ( line.startsWith( "Looking up status of" ) )
 
109
      if ( line.startsWith( QLatin1String( "Looking up status of" ) ) )
101
110
      {
102
111
        // Get the IP address of the master browser.
103
 
        workgroup.setMasterBrowserIP( line.section( "of", 1, 1 ).trimmed() );
 
112
        workgroup->setMasterBrowserIP( line.section( "of", 1, 1 ).trimmed() );
104
113
        continue;
105
114
      }
106
115
      else if ( line.contains( "MAC Address", Qt::CaseSensitive ) )
108
117
        // Add workgroup to the list. Ignore misconfigured master browsers,
109
118
        // that do not belong to a workgroup/domain, i.e. where the workgroup
110
119
        // name is empty.
111
 
        if ( !workgroup.workgroupName().isEmpty() && !workgroup.masterBrowserName().isEmpty() )
 
120
        if ( !workgroup->workgroupName().isEmpty() && !workgroup->masterBrowserName().isEmpty() )
112
121
        {
113
 
          m_workgroups_list << workgroup;
 
122
          m_workgroups_list << new Smb4KWorkgroup( *workgroup );
114
123
        }
115
124
        else
116
125
        {
117
126
          // Do nothing
118
127
        }
119
128
 
120
 
        workgroup = Smb4KWorkgroup();
 
129
        delete workgroup;
 
130
        workgroup = new Smb4KWorkgroup();
121
131
        continue;
122
132
      }
123
133
      else if ( line.contains( " <00> ", Qt::CaseSensitive ) )
126
136
        if ( line.contains( " <GROUP> ", Qt::CaseSensitive ) )
127
137
        {
128
138
          // Avoid setting the workgroup name twice.
129
 
          if ( workgroup.workgroupName().isEmpty() )
 
139
          if ( workgroup->workgroupName().isEmpty() )
130
140
          {
131
 
            workgroup.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
141
            workgroup->setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
132
142
          }
133
143
          else
134
144
          {
138
148
        else
139
149
        {
140
150
          // Avoid setting the name of the master browser twice.
141
 
          if ( workgroup.masterBrowserName().isEmpty() )
 
151
          if ( workgroup->masterBrowserName().isEmpty() )
142
152
          {
143
 
            workgroup.setMasterBrowserName( line.section( "<00>", 0, 0 ).trimmed() );
 
153
            workgroup->setMasterBrowserName( line.section( "<00>", 0, 0 ).trimmed() );
144
154
          }
145
155
          else
146
156
          {
153
163
      else if ( line.contains( " <1d> ", Qt::CaseSensitive ) )
154
164
      {
155
165
        // Get the workgroup name.
156
 
        if ( workgroup.workgroupName().isEmpty() )
 
166
        if ( workgroup->workgroupName().isEmpty() )
157
167
        {
158
 
          workgroup.setWorkgroupName( line.section( "<1d>", 0, 0 ).trimmed() );
 
168
          workgroup->setWorkgroupName( line.section( "<1d>", 0, 0 ).trimmed() );
159
169
        }
160
170
        else
161
171
        {
168
178
                line.contains( " <01> ", Qt::CaseSensitive ) )
169
179
      {
170
180
        // The host is a master browser.
171
 
        workgroup.setHasPseudoMasterBrowser( false );
 
181
        workgroup->setHasPseudoMasterBrowser( false );
172
182
        continue;
173
183
      }
174
184
      else
176
186
        continue;
177
187
      }
178
188
    }
 
189
 
 
190
    delete workgroup;
179
191
  }
180
192
  else
181
193
  {
405
417
  {
406
418
    // Do nothing
407
419
  }
408
 
 
 
420
  
409
421
  m_proc = new Smb4KProcess( this );
410
422
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
411
423
  m_proc->setShellCommand( arguments.join( " " ) );
412
424
 
413
 
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
414
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
425
  connect( m_proc, SIGNAL(readyReadStandardError()), this, SLOT(slotReadStandardError()) );
 
426
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
415
427
 
416
428
  emit aboutToStart();
417
429
 
435
447
    {
436
448
      QString test = it.next();
437
449
 
438
 
      if ( test.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
450
      if ( test.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
439
451
      {
440
452
        it.remove();
441
453
      }
502
514
 
503
515
Smb4KQueryMasterJob::~Smb4KQueryMasterJob()
504
516
{
 
517
  while ( !m_workgroups_list.isEmpty() )
 
518
  {
 
519
    delete m_workgroups_list.takeFirst();
 
520
  }
505
521
}
506
522
 
507
523
 
508
524
void Smb4KQueryMasterJob::start()
509
525
{
510
526
  m_started = true;
511
 
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
527
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
512
528
}
513
529
 
514
530
 
536
552
 
537
553
void Smb4KQueryMasterJob::processWorkgroups()
538
554
{
539
 
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
555
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( '\n', QString::SkipEmptyParts );
540
556
 
541
557
  if ( !stdout.isEmpty() )
542
558
  {
543
 
    Smb4KWorkgroup workgroup;
 
559
    Smb4KWorkgroup *workgroup = new Smb4KWorkgroup();
544
560
 
545
561
    foreach ( const QString &line, stdout )
546
562
    {
547
 
      if ( line.trimmed().startsWith( "Enumerating" ) )
548
 
      {
549
 
        continue;
550
 
      }
551
 
      else if ( line.trimmed().startsWith( "Domain name" ) )
552
 
      {
553
 
        continue;
554
 
      }
555
 
      else if ( line.trimmed().startsWith( "-------------" ) )
 
563
      if ( line.trimmed().startsWith( QLatin1String( "Enumerating" ) ) )
 
564
      {
 
565
        continue;
 
566
      }
 
567
      else if ( line.trimmed().startsWith( QLatin1String( "Domain name" ) ) )
 
568
      {
 
569
        continue;
 
570
      }
 
571
      else if ( line.trimmed().startsWith( QLatin1String( "-------------" ) ) )
556
572
      {
557
573
        continue;
558
574
      }
563
579
      else
564
580
      {
565
581
        // This is the workgroup and master entry. Process it.
566
 
        workgroup.setWorkgroupName( line.section( "   ", 0, 0 ).trimmed() );
567
 
        workgroup.setMasterBrowserName( line.section( "   ", 1, -1 ).trimmed() );
568
 
        workgroup.setHasPseudoMasterBrowser( false );
569
 
 
570
 
        m_workgroups_list << workgroup;
571
 
 
572
 
        workgroup = Smb4KWorkgroup();
 
582
        workgroup->setWorkgroupName( line.section( "   ", 0, 0 ).trimmed() );
 
583
        workgroup->setMasterBrowserName( line.section( "   ", 1, -1 ).trimmed() );
 
584
        workgroup->setHasPseudoMasterBrowser( false );
 
585
 
 
586
        m_workgroups_list << new Smb4KWorkgroup( *workgroup );
 
587
 
 
588
        delete workgroup;
 
589
        workgroup = new Smb4KWorkgroup();
573
590
        continue;
574
591
      }
575
592
    }
 
593
 
 
594
    delete workgroup;
576
595
  }
577
596
  else
578
597
  {
834
853
    m_proc->unsetEnv( "PASSWD" );
835
854
  }
836
855
 
837
 
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
838
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
856
  connect( m_proc, SIGNAL(readyReadStandardError()), this, SLOT(slotReadStandardError()) );
 
857
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
839
858
 
840
859
  emit aboutToStart();
841
860
 
859
878
    {
860
879
      QString test = it.next();
861
880
 
862
 
      if ( test.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
881
      if ( test.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
863
882
      {
864
883
        it.remove();
865
884
      }
967
986
 
968
987
Smb4KScanBAreasJob::~Smb4KScanBAreasJob()
969
988
{
 
989
  while ( !m_workgroups_list.isEmpty() )
 
990
  {
 
991
    delete m_workgroups_list.takeFirst();
 
992
  }
 
993
 
 
994
  while ( !m_hosts_list.isEmpty() )
 
995
  {
 
996
    delete m_hosts_list.takeFirst();
 
997
  }  
970
998
}
971
999
 
972
1000
 
973
1001
void Smb4KScanBAreasJob::start()
974
1002
{
975
1003
  m_started = true;
976
 
  QTimer::singleShot( 0, this, SLOT( slotStartScan() ) );
 
1004
  QTimer::singleShot( 0, this, SLOT(slotStartScan()) );
977
1005
}
978
1006
 
979
1007
 
1000
1028
 
1001
1029
void Smb4KScanBAreasJob::processScan()
1002
1030
{
1003
 
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).trimmed().split( "\n", QString::SkipEmptyParts );
 
1031
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).trimmed().split( '\n', QString::SkipEmptyParts );
1004
1032
 
1005
1033
  if ( !stdout.isEmpty() )
1006
1034
  {
1007
 
    Smb4KWorkgroup workgroup;
1008
 
    Smb4KHost host;
 
1035
    Smb4KWorkgroup *workgroup = new Smb4KWorkgroup();
 
1036
    Smb4KHost *host = new Smb4KHost();
1009
1037
    bool skip = false;
1010
1038
 
1011
1039
    foreach ( const QString &line, stdout )
1013
1041
      // Check if we have to skip this host entry.
1014
1042
      // A host entry is skipped if the IP address is invalid, i.e.
1015
1043
      // 0.0.0.0 is returned.
1016
 
      if ( line.startsWith( "Looking up status of" ) )
 
1044
      if ( line.startsWith( QLatin1String( "Looking up status of" ) ) )
1017
1045
      {
1018
1046
        QString ip_address = line.section( "of", 1, 1 ).trimmed();
1019
1047
        skip = (QString::compare( ip_address, "0.0.0.0" ) == 0);
1027
1055
      // skip lines until there is a valid host entry.
1028
1056
      if ( !skip )
1029
1057
      {
1030
 
        if ( line.startsWith( "Looking up status of" ) )
 
1058
        if ( line.startsWith( QLatin1String( "Looking up status of" ) ) )
1031
1059
        {
1032
1060
          // Set the IP address of the host.
1033
1061
          QString ip_address = line.section( "of", 1, 1 ).trimmed();
1034
1062
 
1035
1063
          if ( QString::compare( ip_address, "0.0.0.0" ) != 0 )
1036
1064
          {
1037
 
            host.setIP( ip_address );
 
1065
            host->setIP( ip_address );
1038
1066
          }
1039
1067
          else
1040
1068
          {
1046
1074
        {
1047
1075
          // Check that the workgroup object carries a workgroup
1048
1076
          // name and a master browser name.
1049
 
          if ( !workgroup.workgroupName().isEmpty() && !workgroup.masterBrowserName().isEmpty() )
 
1077
          if ( !workgroup->workgroupName().isEmpty() && !workgroup->masterBrowserName().isEmpty() )
1050
1078
          {
1051
1079
            // Check whether the workgroup has already been entered
1052
1080
            // into the list.
1054
1082
 
1055
1083
            for ( int i = 0; i < m_workgroups_list.size(); ++i )
1056
1084
            {
1057
 
              if ( QString::compare( m_workgroups_list.at( i ).workgroupName(), workgroup.workgroupName(), Qt::CaseInsensitive ) == 0 )
 
1085
              if ( QString::compare( m_workgroups_list.at( i )->workgroupName(), workgroup->workgroupName(), Qt::CaseInsensitive ) == 0 )
1058
1086
              {
1059
1087
                workgroup_found = true;
1060
1088
                break;
1067
1095
 
1068
1096
            if ( !workgroup_found )
1069
1097
            {
1070
 
              m_workgroups_list << workgroup;
 
1098
              m_workgroups_list << new Smb4KWorkgroup( *workgroup );
1071
1099
            }
1072
1100
            else
1073
1101
            {
1079
1107
            // Do nothing
1080
1108
          }
1081
1109
 
1082
 
          m_hosts_list << host;
1083
 
 
1084
 
          workgroup = Smb4KWorkgroup();
1085
 
          host = Smb4KHost();
 
1110
          m_hosts_list << new Smb4KHost( *host );
 
1111
 
 
1112
          delete workgroup;
 
1113
          workgroup = new Smb4KWorkgroup();
 
1114
 
 
1115
          delete host;
 
1116
          host = new Smb4KHost();
1086
1117
          continue;
1087
1118
        }
1088
1119
        else if ( line.contains( " <00> ", Qt::CaseSensitive ) )
1092
1123
          // or a workgroup name that we process here.
1093
1124
          if ( line.contains( " <GROUP> ", Qt::CaseSensitive ) )
1094
1125
          {
1095
 
            workgroup.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
1096
 
            host.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
1126
            workgroup->setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
1127
            host->setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
1097
1128
          }
1098
1129
          else
1099
1130
          {
1100
 
            host.setHostName( line.section( "<00>", 0, 0 ).trimmed() );
 
1131
            host->setHostName( line.section( "<00>", 0, 0 ).trimmed() );
1101
1132
          }
1102
1133
          continue;
1103
1134
        }
1111
1142
          // until the right master browser has been set.
1112
1143
          if ( line.contains( " <01> ", Qt::CaseSensitive ) )
1113
1144
          {
1114
 
            workgroup.setMasterBrowserName( host.hostName() );
1115
 
            workgroup.setMasterBrowserIP( host.ip() );
1116
 
            workgroup.setHasPseudoMasterBrowser( false );
1117
 
            host.setIsMasterBrowser( true );
 
1145
            workgroup->setMasterBrowserName( host->hostName() );
 
1146
            workgroup->setMasterBrowserIP( host->ip() );
 
1147
            workgroup->setHasPseudoMasterBrowser( false );
 
1148
            host->setIsMasterBrowser( true );
1118
1149
          }
1119
1150
          else
1120
1151
          {
1121
 
            if ( workgroup.masterBrowserName().isEmpty() )
 
1152
            if ( workgroup->masterBrowserName().isEmpty() )
1122
1153
            {
1123
 
              workgroup.setMasterBrowserName( host.hostName() );
1124
 
              workgroup.setMasterBrowserIP( host.ip() );
1125
 
              workgroup.setHasPseudoMasterBrowser( true );
 
1154
              workgroup->setMasterBrowserName( host->hostName() );
 
1155
              workgroup->setMasterBrowserIP( host->ip() );
 
1156
              workgroup->setHasPseudoMasterBrowser( true );
1126
1157
            }
1127
1158
            else
1128
1159
            {
1147
1178
    // Do nothing
1148
1179
  }
1149
1180
 
1150
 
  // Emit the list of workgroups and the list of hosts.
 
1181
  // Emit signals
1151
1182
  emit workgroups( m_workgroups_list );
1152
1183
  emit hosts( m_hosts_list );
 
1184
  
1153
1185
}
1154
1186
 
1155
1187
 
1221
1253
  // Broadcast areas/addresses
1222
1254
 
1223
1255
  // FIXME: Emit error message if the list is empty!!!
1224
 
  QStringList addresses = Smb4KSettings::broadcastAreas().split( ",", QString::SkipEmptyParts );
 
1256
  QStringList addresses = Smb4KSettings::broadcastAreas().split( ',', QString::SkipEmptyParts );
1225
1257
 
1226
1258
  // Assemble the command
1227
1259
  QStringList arguments;
1314
1346
    if ( !Smb4KSettings::domainName().isEmpty() &&
1315
1347
         QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
1316
1348
    {
1317
 
      QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
1349
      arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
1318
1350
    }
1319
1351
    else
1320
1352
    {
1394
1426
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
1395
1427
  m_proc->setShellCommand( arguments.join( " " ) );
1396
1428
 
1397
 
  connect( m_proc, SIGNAL( readyReadStandardError() ), SLOT( slotReadStandardError() ) );
1398
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
1429
  connect( m_proc, SIGNAL(readyReadStandardError()), SLOT(slotReadStandardError()) );
 
1430
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
1399
1431
 
1400
1432
  emit aboutToStart();
1401
1433
 
1418
1450
    {
1419
1451
      QString test = it.next();
1420
1452
 
1421
 
      if ( test.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
1453
      if ( test.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
1422
1454
      {
1423
1455
        it.remove();
1424
1456
      }
1485
1517
 
1486
1518
Smb4KLookupDomainMembersJob::~Smb4KLookupDomainMembersJob()
1487
1519
{
 
1520
  delete m_workgroup;
 
1521
 
 
1522
  // Do not delete m_master_browser here, because it is a pointer
 
1523
  // to an entry in Smb4KGlobal::hostsList().
 
1524
  
 
1525
  while ( !m_hosts_list.isEmpty() )
 
1526
  {
 
1527
    delete m_hosts_list.takeFirst();
 
1528
  }
1488
1529
}
1489
1530
 
1490
1531
 
1491
1532
void Smb4KLookupDomainMembersJob::start()
1492
1533
{
1493
1534
  m_started = true;
1494
 
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
1535
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
1495
1536
}
1496
1537
 
1497
1538
 
1498
1539
void Smb4KLookupDomainMembersJob::setupLookup( Smb4KWorkgroup *workgroup, QWidget *parent )
1499
1540
{
1500
1541
  Q_ASSERT( workgroup );
1501
 
  m_workgroup = *workgroup;
 
1542
  m_workgroup = new Smb4KWorkgroup( *workgroup );
1502
1543
  m_parent_widget = parent;
1503
1544
}
1504
1545
 
1520
1561
 
1521
1562
void Smb4KLookupDomainMembersJob::processHosts()
1522
1563
{
1523
 
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
1564
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( '\n', QString::SkipEmptyParts );
1524
1565
 
1525
1566
  if ( !stdout.isEmpty() )
1526
1567
  {
1527
 
    Smb4KHost host;
 
1568
    Smb4KHost *host = new Smb4KHost();
1528
1569
 
1529
1570
    foreach ( const QString &line, stdout )
1530
1571
    {
1531
 
      if ( line.trimmed().startsWith( "Enumerating" ) )
1532
 
      {
1533
 
        continue;
1534
 
      }
1535
 
      else if ( line.trimmed().startsWith( "Server name" ) )
1536
 
      {
1537
 
        continue;
1538
 
      }
1539
 
      else if ( line.trimmed().startsWith( "-------------" ) )
 
1572
      if ( line.trimmed().startsWith( QLatin1String( "Enumerating" ) ) )
 
1573
      {
 
1574
        continue;
 
1575
      }
 
1576
      else if ( line.trimmed().startsWith( QLatin1String( "Server name" ) ) )
 
1577
      {
 
1578
        continue;
 
1579
      }
 
1580
      else if ( line.trimmed().startsWith( QLatin1String( "-------------" ) ) )
1540
1581
      {
1541
1582
        continue;
1542
1583
      }
1543
1584
      else
1544
1585
      {
1545
 
        // Omit host names that contain spaces since QUrl cannot handle them.
 
1586
        // Omit host names that contain spaces since KUrl cannot handle them.
1546
1587
        // And, they are wrong, anyway.
1547
1588
        if ( !line.section( "   ", 0, 0 ).trimmed().contains( " " ) )
1548
1589
        {
1549
 
          host.setHostName( line.section( "   ", 0, 0 ).trimmed() );
1550
 
          host.setWorkgroupName( m_workgroup.workgroupName() );
1551
 
          host.setComment( line.section( "   ", 1, -1 ).trimmed() );
 
1590
          host->setHostName( line.section( "   ", 0, 0 ).trimmed() );
 
1591
          host->setWorkgroupName( m_workgroup->workgroupName() );
 
1592
          host->setComment( line.section( "   ", 1, -1 ).trimmed() );
1552
1593
          
1553
 
          if ( QString::compare( host.hostName(), m_workgroup.masterBrowserName() ) == 0 )
 
1594
          if ( QString::compare( host->hostName(), m_workgroup->masterBrowserName() ) == 0 )
1554
1595
          {
1555
 
            host.setLogin( m_master_browser.login() );
1556
 
            host.setPassword( m_master_browser.password() );
1557
 
            host.setIsMasterBrowser( true );
 
1596
            host->setLogin( m_master_browser->login() );
 
1597
            host->setPassword( m_master_browser->password() );
 
1598
            host->setIsMasterBrowser( true );
1558
1599
 
1559
 
            if ( m_workgroup.hasMasterBrowserIP() )
 
1600
            if ( m_workgroup->hasMasterBrowserIP() )
1560
1601
            {
1561
 
              host.setIP( m_workgroup.masterBrowserIP() );
 
1602
              host->setIP( m_workgroup->masterBrowserIP() );
1562
1603
            }
1563
1604
            else
1564
1605
            {
1567
1608
          }
1568
1609
          else
1569
1610
          {
1570
 
            host.setIsMasterBrowser( false );
 
1611
            host->setIsMasterBrowser( false );
1571
1612
          }
1572
1613
          
1573
 
          m_hosts_list << host;
 
1614
          m_hosts_list << new Smb4KHost( *host );
1574
1615
        }
1575
1616
        else
1576
1617
        {
1577
1618
          qDebug() << "This host name contains a space. I cannot handle this...";
1578
1619
        }
 
1620
 
 
1621
        delete host;
 
1622
        host = new Smb4KHost();
1579
1623
        
1580
 
        host = Smb4KHost();
1581
1624
        continue;
1582
1625
      }
1583
1626
    }
 
1627
 
 
1628
    delete host;
1584
1629
  }
1585
1630
  else
1586
1631
  {
1587
1632
    // Do nothing
1588
1633
  }
1589
1634
 
1590
 
  emit hosts( &m_workgroup, m_hosts_list );
 
1635
  emit hosts( m_workgroup, m_hosts_list );
1591
1636
}
1592
1637
 
1593
1638
 
1610
1655
 
1611
1656
  // Get the master browser of the defined workgroup, so that we
1612
1657
  // can connect to it.
1613
 
  Smb4KHost *host = findHost( m_workgroup.masterBrowserName(), m_workgroup.workgroupName() );
 
1658
  Smb4KHost *host = findHost( m_workgroup->masterBrowserName(), m_workgroup->workgroupName() );
1614
1659
 
1615
1660
  if ( host )
1616
1661
  {
1617
 
    // Copy host entry to private variable.
1618
 
    m_master_browser = *host;
 
1662
    m_master_browser = host;
1619
1663
    
1620
1664
    // If the master browsers need authentication, we read it now.
1621
1665
    if ( Smb4KSettings::masterBrowsersRequireAuth() )
1622
1666
    {
1623
 
      Smb4KWalletManager::self()->readAuthInfo( &m_master_browser );
 
1667
      Smb4KWalletManager::self()->readAuthInfo( m_master_browser );
1624
1668
    }
1625
1669
    else
1626
1670
    {
1631
1675
    QMap<QString,QString> samba_options = globalSambaOptions();
1632
1676
 
1633
1677
    // Custom options
1634
 
    Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_master_browser );
 
1678
    Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( m_master_browser );
1635
1679
 
1636
1680
    // Assemble the command.
1637
1681
    QStringList arguments;
1688
1732
    }
1689
1733
 
1690
1734
    // IP address of the master browser
1691
 
    if ( m_workgroup.hasMasterBrowserIP() )
 
1735
    if ( m_workgroup->hasMasterBrowserIP() )
1692
1736
    {
1693
 
      arguments << QString( "-I %1" ).arg( m_workgroup.masterBrowserIP() );
 
1737
      arguments << QString( "-I %1" ).arg( m_workgroup->masterBrowserIP() );
1694
1738
    }
1695
1739
    else
1696
1740
    {
1699
1743
 
1700
1744
    // Workgroup of the remote master browser that is to be 
1701
1745
    // queried for the workgroup/domain members.
1702
 
    arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_workgroup.workgroupName() ) );
 
1746
    arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_workgroup->workgroupName() ) );
1703
1747
 
1704
1748
    // Name of the remote master browser
1705
 
    arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_workgroup.masterBrowserName() ) );
 
1749
    arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_workgroup->masterBrowserName() ) );
1706
1750
 
1707
1751
    // Authentication, if needed
1708
 
    if ( Smb4KSettings::masterBrowsersRequireAuth() && !m_master_browser.login().isEmpty() )
 
1752
    if ( Smb4KSettings::masterBrowsersRequireAuth() && !m_master_browser->login().isEmpty() )
1709
1753
    {
1710
 
      arguments << QString( "-U %1" ).arg( m_master_browser.login() );
 
1754
      arguments << QString( "-U %1" ).arg( m_master_browser->login() );
1711
1755
    }
1712
1756
    else
1713
1757
    {
1719
1763
    m_proc->setShellCommand( arguments.join( " " ) );
1720
1764
    m_proc->setOutputChannelMode( KProcess::SeparateChannels );
1721
1765
 
1722
 
    if ( Smb4KSettings::self()->masterBrowsersRequireAuth() && !m_master_browser.password().isEmpty() )
 
1766
    if ( Smb4KSettings::self()->masterBrowsersRequireAuth() && !m_master_browser->password().isEmpty() )
1723
1767
    {
1724
 
      m_proc->setEnv( "PASSWD", m_master_browser.password(), true );
 
1768
      m_proc->setEnv( "PASSWD", m_master_browser->password(), true );
1725
1769
    }
1726
1770
    else
1727
1771
    {
1728
1772
      m_proc->unsetEnv( "PASSWD" );
1729
1773
    }
1730
1774
  
1731
 
    connect( m_proc, SIGNAL( readyReadStandardError() ), SLOT( slotReadStandardError() ) );
1732
 
    connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
1775
    connect( m_proc, SIGNAL(readyReadStandardError()), SLOT(slotReadStandardError()) );
 
1776
    connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
1733
1777
 
1734
 
    emit aboutToStart( &m_workgroup );
 
1778
    emit aboutToStart( m_workgroup );
1735
1779
    
1736
1780
    m_proc->start();
1737
1781
  }
1738
1782
  else
1739
1783
  {
1740
1784
    // The master browser could not be determined. End the
1741
 
    // job here and emit an empty hosts list.
1742
 
    emit hosts( &m_workgroup, m_hosts_list );
 
1785
    // job here and emit the signal nontheless.
 
1786
    emit hosts( m_workgroup, m_hosts_list );
1743
1787
    emitResult();
1744
1788
  }
1745
1789
}
1760
1804
    {
1761
1805
      QString test = it.next();
1762
1806
 
1763
 
      if ( test.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
1807
      if ( test.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
1764
1808
      {
1765
1809
        it.remove();
1766
1810
      }
1796
1840
    {
1797
1841
      // Notify the user that an error occurred.
1798
1842
      Smb4KNotification *notification = new Smb4KNotification();
1799
 
      notification->retrievingServersFailed( &m_workgroup, stderr );
 
1843
      notification->retrievingServersFailed( m_workgroup, stderr );
1800
1844
    }
1801
1845
  }
1802
1846
  else
1831
1875
  }
1832
1876
 
1833
1877
  emitResult();
1834
 
  emit finished( &m_workgroup );
 
1878
  emit finished( m_workgroup );
1835
1879
}
1836
1880
 
1837
1881
 
1844
1888
 
1845
1889
Smb4KLookupSharesJob::~Smb4KLookupSharesJob()
1846
1890
{
 
1891
  delete m_host;
 
1892
 
 
1893
  while ( !m_shares_list.isEmpty() )
 
1894
  {
 
1895
    delete m_shares_list.takeFirst();
 
1896
  }
1847
1897
}
1848
1898
 
1849
1899
 
1850
1900
void Smb4KLookupSharesJob::start()
1851
1901
{
1852
1902
  m_started = true;
1853
 
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
1903
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
1854
1904
}
1855
1905
 
1856
1906
 
1857
1907
void Smb4KLookupSharesJob::setupLookup( Smb4KHost *host, QWidget *parent )
1858
1908
{
1859
1909
  Q_ASSERT( host );
1860
 
  m_host = *host;
 
1910
  m_host = new Smb4KHost( *host );
1861
1911
  m_parent_widget = parent;
1862
1912
}
1863
1913
 
1879
1929
 
1880
1930
void Smb4KLookupSharesJob::processShares()
1881
1931
{
1882
 
  // Additional authentication error handling.
1883
 
  if ( m_proc->exitCode() == 104 /* access denied in W2k3 domain */ ||
1884
 
       m_proc->exitCode() == 235 /* wrong password in W2k3 domain */ )
1885
 
  {
1886
 
    emit authError( this );
1887
 
    // We can just return here, because this function is invoked
1888
 
    // in slotProcessFinished() and emitResult() will be emitted 
1889
 
    // at its end.
1890
 
    return;
1891
 
  }
1892
 
  else
1893
 
  {
1894
 
    // Do nothing
1895
 
  }
1896
 
 
1897
 
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
1932
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( '\n', QString::SkipEmptyParts );
1898
1933
  
1899
1934
  if ( !stdout.isEmpty() )
1900
1935
  {
1901
 
    Smb4KShare share;
 
1936
    Smb4KShare *share = new Smb4KShare();
1902
1937
    
1903
1938
    foreach ( const QString &line, stdout )
1904
1939
    {
1905
 
      if ( line.trimmed().startsWith( "Enumerating" ) )
1906
 
      {
1907
 
        continue;
1908
 
      }
1909
 
      else if ( line.trimmed().startsWith( "Share name" ) )
1910
 
      {
1911
 
        continue;
1912
 
      }
1913
 
      else if ( line.trimmed().startsWith( "----------" ) )
 
1940
      if ( line.trimmed().startsWith( QLatin1String( "Enumerating" ) ) )
 
1941
      {
 
1942
        continue;
 
1943
      }
 
1944
      else if ( line.trimmed().startsWith( QLatin1String( "Share name" ) ) )
 
1945
      {
 
1946
        continue;
 
1947
      }
 
1948
      else if ( line.trimmed().startsWith( QLatin1String( "----------" ) ) )
1914
1949
      {
1915
1950
        continue;
1916
1951
      }
1917
1952
      else if ( line.contains( " Disk     ", Qt::CaseSensitive ) /* line has comment */ ||
1918
1953
                (!line.contains( " Disk     ", Qt::CaseSensitive ) &&
1919
 
                line.trimmed().endsWith( " Disk", Qt::CaseSensitive ) /* line has no comment */) )
 
1954
                line.trimmed().endsWith( QLatin1String( " Disk" ), Qt::CaseSensitive ) /* line has no comment */) )
1920
1955
      {
1921
 
        if ( !line.trimmed().endsWith( " Disk", Qt::CaseSensitive ) )
 
1956
        if ( !line.trimmed().endsWith( QLatin1String( " Disk" ), Qt::CaseSensitive ) )
1922
1957
        {
1923
 
          share.setShareName( line.section( " Disk     ", 0, 0 ).trimmed() );
1924
 
          share.setComment( line.section( " Disk     ", 1, 1 ).trimmed() );
 
1958
          share->setShareName( line.section( " Disk     ", 0, 0 ).trimmed() );
 
1959
          share->setComment( line.section( " Disk     ", 1, 1 ).trimmed() );
1925
1960
        }
1926
1961
        else
1927
1962
        {
1928
 
          share.setShareName( line.section( " Disk", 0, 0 ).trimmed() );
1929
 
          share.setComment( "" );
 
1963
          share->setShareName( line.section( " Disk", 0, 0 ).trimmed() );
 
1964
          share->setComment( "" );
1930
1965
        }
1931
1966
            
1932
 
        share.setHostName( m_host.hostName() );
1933
 
        share.setWorkgroupName( m_host.workgroupName() );
1934
 
        share.setTypeString( "Disk" );
1935
 
        share.setLogin( m_host.login() );
1936
 
        share.setPassword( m_host.password() );
 
1967
        share->setHostName( m_host->hostName() );
 
1968
        share->setWorkgroupName( m_host->workgroupName() );
 
1969
        share->setTypeString( "Disk" );
 
1970
        share->setLogin( m_host->login() );
 
1971
        share->setPassword( m_host->password() );
1937
1972
 
1938
 
        if ( m_host.hasIP() )
 
1973
        if ( m_host->hasIP() )
1939
1974
        {
1940
 
          share.setHostIP( m_host.ip() );
 
1975
          share->setHostIP( m_host->ip() );
1941
1976
        }
1942
1977
        else
1943
1978
        {
1944
1979
          // Do nothing
1945
1980
        }
1946
1981
 
1947
 
        m_shares_list << share;
1948
 
        share = Smb4KShare();
 
1982
        m_shares_list << new Smb4KShare( *share );
 
1983
 
 
1984
        delete share;
 
1985
        share = new Smb4KShare();
 
1986
        
1949
1987
        continue;
1950
1988
      }
1951
1989
      else if ( line.contains( " IPC      ", Qt::CaseSensitive ) /* line has comment */ ||
1952
1990
                (!line.contains( " IPC      ", Qt::CaseSensitive ) &&
1953
 
                line.trimmed().endsWith( " IPC", Qt::CaseSensitive ) /* line has no comment */) )
 
1991
                line.trimmed().endsWith( QLatin1String( " IPC" ), Qt::CaseSensitive ) /* line has no comment */) )
1954
1992
      {
1955
 
        if ( !line.trimmed().endsWith( " IPC", Qt::CaseSensitive ) )
 
1993
        if ( !line.trimmed().endsWith( QLatin1String( " IPC" ), Qt::CaseSensitive ) )
1956
1994
        {
1957
 
          share.setShareName( line.section( " IPC      ", 0, 0 ).trimmed() );
1958
 
          share.setComment( line.section( " IPC      ", 1, 1 ).trimmed() );
 
1995
          share->setShareName( line.section( " IPC      ", 0, 0 ).trimmed() );
 
1996
          share->setComment( line.section( " IPC      ", 1, 1 ).trimmed() );
1959
1997
        }
1960
1998
        else
1961
1999
        {
1962
 
          share.setShareName( line.section( " IPC", 0, 0 ).trimmed() );
1963
 
          share.setComment( "" );
 
2000
          share->setShareName( line.section( " IPC", 0, 0 ).trimmed() );
 
2001
          share->setComment( "" );
1964
2002
        }
1965
2003
            
1966
 
        share.setHostName( m_host.hostName() );
1967
 
        share.setWorkgroupName( m_host.workgroupName() );
1968
 
        share.setTypeString( "IPC" );
1969
 
        share.setLogin( m_host.login() );
1970
 
        share.setPassword( m_host.password() );
 
2004
        share->setHostName( m_host->hostName() );
 
2005
        share->setWorkgroupName( m_host->workgroupName() );
 
2006
        share->setTypeString( "IPC" );
 
2007
        share->setLogin( m_host->login() );
 
2008
        share->setPassword( m_host->password() );
1971
2009
 
1972
 
        if ( m_host.hasIP() )
 
2010
        if ( m_host->hasIP() )
1973
2011
        {
1974
 
          share.setHostIP( m_host.ip() );
 
2012
          share->setHostIP( m_host->ip() );
1975
2013
        }
1976
2014
        else
1977
2015
        {
1978
2016
          // Do nothing
1979
2017
        }
1980
2018
 
1981
 
        m_shares_list << share;
1982
 
        share = Smb4KShare();
 
2019
        m_shares_list << new Smb4KShare( *share );
 
2020
 
 
2021
        delete share;
 
2022
        share = new Smb4KShare();
 
2023
 
1983
2024
        continue;
1984
2025
      }
1985
2026
      else if ( line.contains( " Print    ", Qt::CaseSensitive ) /* line has comment */ ||
1986
2027
                (!line.contains( " Print    ", Qt::CaseSensitive ) &&
1987
 
                line.trimmed().endsWith( " Print", Qt::CaseSensitive ) /* line has no comment */) )
 
2028
                line.trimmed().endsWith( QLatin1String( " Print" ), Qt::CaseSensitive ) /* line has no comment */) )
1988
2029
      {
1989
 
        if ( !line.trimmed().endsWith( " Print", Qt::CaseSensitive ) )
 
2030
        if ( !line.trimmed().endsWith( QLatin1String( " Print" ), Qt::CaseSensitive ) )
1990
2031
        {
1991
 
          share.setShareName( line.section( " Print    ", 0, 0 ).trimmed() );
1992
 
          share.setComment( line.section( " Print    ", 1, 1 ).trimmed() );
 
2032
          share->setShareName( line.section( " Print    ", 0, 0 ).trimmed() );
 
2033
          share->setComment( line.section( " Print    ", 1, 1 ).trimmed() );
1993
2034
        }
1994
2035
        else
1995
2036
        {
1996
 
          share.setShareName( line.section( " Print", 0, 0 ).trimmed() );
1997
 
          share.setComment( "" );
 
2037
          share->setShareName( line.section( " Print", 0, 0 ).trimmed() );
 
2038
          share->setComment( "" );
1998
2039
        }
1999
2040
            
2000
 
        share.setHostName( m_host.hostName() );
2001
 
        share.setWorkgroupName( m_host.workgroupName() );
2002
 
        share.setTypeString( "Printer" );
2003
 
        share.setLogin( m_host.login() );
2004
 
        share.setPassword( m_host.password() );
 
2041
        share->setHostName( m_host->hostName() );
 
2042
        share->setWorkgroupName( m_host->workgroupName() );
 
2043
        share->setTypeString( "Printer" );
 
2044
        share->setLogin( m_host->login() );
 
2045
        share->setPassword( m_host->password() );
2005
2046
 
2006
 
        if ( m_host.hasIP() )
 
2047
        if ( m_host->hasIP() )
2007
2048
        {
2008
 
          share.setHostIP( m_host.ip() );
 
2049
          share->setHostIP( m_host->ip() );
2009
2050
        }
2010
2051
        else
2011
2052
        {
2012
2053
          // Do nothing
2013
2054
        }
2014
2055
 
2015
 
        m_shares_list << share;
2016
 
        share = Smb4KShare();
 
2056
        m_shares_list << new Smb4KShare( *share );
 
2057
 
 
2058
        delete share;
 
2059
        share = new Smb4KShare();
 
2060
        
2017
2061
        continue;
2018
2062
      }
2019
2063
      else
2021
2065
        continue;
2022
2066
      }
2023
2067
    }
 
2068
 
 
2069
    delete share;
2024
2070
  }
2025
2071
  else
2026
2072
  {
2027
2073
    // Do nothing
2028
2074
  }
2029
2075
  
2030
 
  emit shares( &m_host, m_shares_list );
 
2076
  emit shares( m_host, m_shares_list );
2031
2077
}
2032
2078
 
2033
2079
 
2049
2095
  }
2050
2096
  
2051
2097
  // Authentication information.
2052
 
  Smb4KWalletManager::self()->readAuthInfo( &m_host );
 
2098
  Smb4KWalletManager::self()->readAuthInfo( m_host );
2053
2099
  
2054
2100
  // Global Samba and custom options
2055
2101
  QMap<QString,QString> samba_options = globalSambaOptions();
2056
 
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_host );
 
2102
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( m_host );
2057
2103
  
2058
2104
  // Assemble the command.
2059
2105
  QStringList arguments;
2150
2196
  // Port
2151
2197
  // If a port was defined for the host via Smb4KHost::port(), it will 
2152
2198
  // overwrite the other options.
2153
 
  if ( m_host.port() != -1 )
 
2199
  if ( m_host->port() != -1 )
2154
2200
  {
2155
 
    arguments << QString( "-p %1" ).arg( m_host.port() );
 
2201
    arguments << QString( "-p %1" ).arg( m_host->port() );
2156
2202
  }
2157
2203
  else
2158
2204
  {
2167
2213
  }
2168
2214
  
2169
2215
  // Remote domain/workgroup name
2170
 
  arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_host.workgroupName() ) );
 
2216
  arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_host->workgroupName() ) );
2171
2217
  
2172
2218
  // Remote host name
2173
 
  arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_host.hostName() ) );
 
2219
  arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_host->hostName() ) );
2174
2220
 
2175
2221
  // IP address
2176
 
  if ( m_host.hasIP() )
 
2222
  if ( m_host->hasIP() )
2177
2223
  {
2178
 
    arguments << QString( "-I %1" ).arg( m_host.ip() );
 
2224
    arguments << QString( "-I %1" ).arg( m_host->ip() );
2179
2225
  }
2180
2226
  else
2181
2227
  {
2183
2229
  }
2184
2230
  
2185
2231
  // Authentication data
2186
 
  if ( !m_host.login().isEmpty() )
 
2232
  if ( !m_host->login().isEmpty() )
2187
2233
  {
2188
 
    arguments << QString( "-U %1" ).arg( m_host.login() );
 
2234
    arguments << QString( "-U %1" ).arg( m_host->login() );
2189
2235
  }
2190
2236
  else
2191
2237
  {
2199
2245
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
2200
2246
  m_proc->setShellCommand( arguments.join( " " ) );
2201
2247
  
2202
 
  if ( !m_host.password().isEmpty() )
 
2248
  if ( !m_host->password().isEmpty() )
2203
2249
  {
2204
 
    m_proc->setEnv( "PASSWD", m_host.password(), true );
 
2250
    m_proc->setEnv( "PASSWD", m_host->password(), true );
2205
2251
  }
2206
2252
  else
2207
2253
  {
2208
2254
    m_proc->unsetEnv( "PASSWD" );
2209
2255
  }
2210
2256
 
2211
 
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
2212
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
2257
  connect( m_proc, SIGNAL(readyReadStandardError()), this, SLOT(slotReadStandardError()) );
 
2258
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
2213
2259
 
2214
 
  emit aboutToStart( &m_host );
 
2260
  emit aboutToStart( m_host );
2215
2261
 
2216
2262
  m_proc->start();
2217
2263
}
2232
2278
    {
2233
2279
      QString test = it.next();
2234
2280
 
2235
 
      if ( test.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
2281
      if ( test.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
2236
2282
      {
2237
2283
        it.remove();
2238
2284
      }
2265
2311
    else if ( stderr.contains( "could not obtain sid for domain", Qt::CaseSensitive ) )
2266
2312
    {
2267
2313
      // FIXME
2268
 
      qDebug() << "FIXME: Wrong protocol used for host " << m_host.hostName();
 
2314
      qDebug() << "FIXME: Wrong protocol used for host " << m_host->hostName();
2269
2315
    }
2270
2316
    else
2271
2317
    {
2272
2318
      Smb4KNotification *notification = new Smb4KNotification();
2273
 
      notification->retrievingSharesFailed( &m_host, stderr );
 
2319
      notification->retrievingSharesFailed( m_host, stderr );
2274
2320
    }
2275
2321
  }
2276
2322
  else
2305
2351
  }
2306
2352
 
2307
2353
  emitResult();
2308
 
  emit finished( &m_host );
 
2354
  emit finished( m_host );
2309
2355
}
2310
2356
 
2311
2357
 
2318
2364
 
2319
2365
Smb4KLookupInfoJob::~Smb4KLookupInfoJob()
2320
2366
{
 
2367
  delete m_host;
2321
2368
}
2322
2369
 
2323
2370
 
2324
2371
void Smb4KLookupInfoJob::start()
2325
2372
{
2326
2373
  m_started = true;
2327
 
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
2374
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
2328
2375
}
2329
2376
 
2330
2377
 
2331
2378
void Smb4KLookupInfoJob::setupLookup( Smb4KHost *host, QWidget* parent )
2332
2379
{
2333
2380
  Q_ASSERT( host );
2334
 
  m_host = *host;
 
2381
  m_host = new Smb4KHost( *host );
2335
2382
  m_parent_widget = parent;
2336
2383
}
2337
2384
 
2359
2406
  
2360
2407
  if ( stdout.contains( "OS=" ) || stdout.contains( "Server=" ) )
2361
2408
  {
2362
 
    QStringList stdout_list = stdout.split( "\n", QString::SkipEmptyParts );
 
2409
    QStringList stdout_list = stdout.split( '\n', QString::SkipEmptyParts );
2363
2410
    
2364
2411
    foreach ( const QString &line, stdout_list )
2365
2412
    {
2366
2413
      if ( line.contains( "OS=" ) || line.contains( "Server=" ) )
2367
2414
      {
2368
 
        QString server = line.section( "Server=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
2369
 
        QString os = line.section( "OS=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
2370
 
        m_host.setInfo( server, os );
2371
 
        emit info( &m_host );
 
2415
        QString server = line.section( "Server=[", 1, 1 ).section( ']', 0, 0 ).trimmed();
 
2416
        QString os = line.section( "OS=[", 1, 1 ).section( ']', 0, 0 ).trimmed();
 
2417
        m_host->setInfo( server, os );
 
2418
        emit info( m_host );
2372
2419
        break;
2373
2420
      }
2374
2421
      else
2383
2430
    
2384
2431
    if ( stderr.contains( "OS=" ) || stderr.contains( "Server=" ) )
2385
2432
    {
2386
 
      QStringList stderr_list = stderr.split( "\n", QString::SkipEmptyParts );
 
2433
      QStringList stderr_list = stderr.split( '\n', QString::SkipEmptyParts );
2387
2434
      
2388
2435
      foreach ( const QString &line, stderr_list )
2389
2436
      {
2390
2437
        if ( line.contains( "OS=" ) || line.contains( "Server=" ) )
2391
2438
        {
2392
 
          QString server = line.section( "Server=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
2393
 
          QString os = line.section( "OS=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
2394
 
          m_host.setInfo( server, os );
2395
 
          emit info( &m_host );
 
2439
          QString server = line.section( "Server=[", 1, 1 ).section( ']', 0, 0 ).trimmed();
 
2440
          QString os = line.section( "OS=[", 1, 1 ).section( ']', 0, 0 ).trimmed();
 
2441
          m_host->setInfo( server, os );
 
2442
          emit info( m_host );
2396
2443
          break;
2397
2444
        }
2398
2445
        else
2427
2474
  arguments << smbclient;
2428
2475
  arguments << "-d1";
2429
2476
  arguments << "-N";
2430
 
  arguments << QString( "-W %1" ).arg( KShell::quoteArg( m_host.workgroupName() ) );
2431
 
  arguments << QString( "-L %1" ).arg( KShell::quoteArg( m_host.hostName() ) );
 
2477
  arguments << QString( "-W %1" ).arg( KShell::quoteArg( m_host->workgroupName() ) );
 
2478
  arguments << QString( "-L %1" ).arg( KShell::quoteArg( m_host->hostName() ) );
2432
2479
 
2433
 
  if ( m_host.hasIP() )
 
2480
  if ( m_host->hasIP() )
2434
2481
  {
2435
 
    arguments << QString( "-I %1" ).arg( m_host.ip() );
 
2482
    arguments << QString( "-I %1" ).arg( m_host->ip() );
2436
2483
  }
2437
2484
  else
2438
2485
  {
2489
2536
  
2490
2537
  // Get global Samba and custom options
2491
2538
  QMap<QString,QString> samba_options = globalSambaOptions();
2492
 
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_host );
 
2539
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( m_host );
2493
2540
  
2494
2541
  // Port
2495
2542
  // If a port was defined for the host via Smb4KHost::port(), it will 
2496
2543
  // overwrite the other options.
2497
 
  if ( m_host.port() != -1 )
 
2544
  if ( m_host->port() != -1 )
2498
2545
  {
2499
 
    arguments << QString( "-p %1" ).arg( m_host.port() );
 
2546
    arguments << QString( "-p %1" ).arg( m_host->port() );
2500
2547
  }
2501
2548
  else
2502
2549
  {
2623
2670
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
2624
2671
  m_proc->setShellCommand( arguments.join( " " ) );
2625
2672
 
2626
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
2673
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
2627
2674
 
2628
 
  emit aboutToStart( &m_host );
 
2675
  emit aboutToStart( m_host );
2629
2676
 
2630
2677
  m_proc->start();  
2631
2678
}
2656
2703
  }
2657
2704
 
2658
2705
  emitResult();
2659
 
  emit finished( &m_host );
2660
 
}
2661
 
 
2662
 
 
2663
 
 
2664
 
 
2665
 
Smb4KScannerPrivate::Smb4KScannerPrivate()
2666
 
{
2667
 
}
2668
 
 
2669
 
 
2670
 
Smb4KScannerPrivate::~Smb4KScannerPrivate()
2671
 
{
2672
 
}
 
2706
  emit finished( m_host );
 
2707
}
 
2708
 
 
2709
 
 
2710
 
 
2711
Smb4KLookupIPAddressJob::Smb4KLookupIPAddressJob( QObject *parent )
 
2712
: KJob( parent ), m_started( false ), m_host( NULL ), m_parent_widget( NULL ), m_proc( NULL )
 
2713
{
 
2714
}
 
2715
 
 
2716
 
 
2717
Smb4KLookupIPAddressJob::~Smb4KLookupIPAddressJob()
 
2718
{
 
2719
  delete m_host;
 
2720
}
 
2721
 
 
2722
 
 
2723
void Smb4KLookupIPAddressJob::start()
 
2724
{
 
2725
  m_started = true;
 
2726
  QTimer::singleShot( 0, this, SLOT(slotStartLookup()) );
 
2727
}
 
2728
 
 
2729
 
 
2730
void Smb4KLookupIPAddressJob::setupLookup( Smb4KHost *host, QWidget *parent )
 
2731
{
 
2732
  Q_ASSERT( host );
 
2733
  m_host = new Smb4KHost( *host );
 
2734
  m_parent_widget = parent;
 
2735
}
 
2736
 
 
2737
 
 
2738
bool Smb4KLookupIPAddressJob::doKill()
 
2739
{
 
2740
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
2741
  {
 
2742
    m_proc->abort();
 
2743
  }
 
2744
  else
 
2745
  {
 
2746
    // Do nothing
 
2747
  }
 
2748
 
 
2749
  return KJob::doKill();
 
2750
}
 
2751
 
 
2752
 
 
2753
void Smb4KLookupIPAddressJob::processIPAddress()
 
2754
{
 
2755
  // Normally, there should only be one IP address. However, there might
 
2756
  // be more than one. So, split the incoming data and use the first entry
 
2757
  // as IP address (it's most likely the correct one). If there is no data,
 
2758
  // set the IP address to an empty string.
 
2759
  QStringList output = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( '\n', QString::SkipEmptyParts );
 
2760
 
 
2761
  foreach ( const QString &line, output )
 
2762
  {
 
2763
    if ( line.contains( "<00>" ) )
 
2764
    {
 
2765
      QString ip_address = line.section( ' ', 0, 0 ).trimmed();
 
2766
      m_host->setIP( ip_address );
 
2767
      break;
 
2768
    }
 
2769
    else
 
2770
    {
 
2771
      continue;
 
2772
    }
 
2773
  }
 
2774
 
 
2775
  emit ipAddress( m_host );
 
2776
}
 
2777
 
 
2778
 
 
2779
void Smb4KLookupIPAddressJob::slotStartLookup()
 
2780
{
 
2781
  // Find nmblookup program.
 
2782
  QString nmblookup = KStandardDirs::findExe( "nmblookup" );
 
2783
 
 
2784
  if ( nmblookup.isEmpty() )
 
2785
  {
 
2786
    Smb4KNotification *notification = new Smb4KNotification();
 
2787
    notification->commandNotFound( "nmblookup" );
 
2788
    emitResult();
 
2789
    return;
 
2790
  }
 
2791
  else
 
2792
  {
 
2793
    // Go ahead
 
2794
  }
 
2795
 
 
2796
  // Global Samba options
 
2797
  QMap<QString,QString> samba_options = globalSambaOptions();
 
2798
 
 
2799
  // Compile the arguments
 
2800
  QStringList arguments;
 
2801
  arguments << nmblookup;
 
2802
 
 
2803
  // Domain
 
2804
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
2805
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
2806
  {
 
2807
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
2808
  }
 
2809
  else
 
2810
  {
 
2811
    // Do nothing
 
2812
  }
 
2813
 
 
2814
  // NetBIOS name
 
2815
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
2816
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
2817
  {
 
2818
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
2819
  }
 
2820
  else
 
2821
  {
 
2822
    // Do nothing
 
2823
  }
 
2824
 
 
2825
  // NetBIOS scope
 
2826
  if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
2827
       QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
2828
  {
 
2829
    arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
2830
  }
 
2831
  else
 
2832
  {
 
2833
    // Do nothing
 
2834
  }
 
2835
 
 
2836
  // Socket options
 
2837
  if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
2838
       QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
2839
  {
 
2840
    arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
2841
  }
 
2842
  else
 
2843
  {
 
2844
    // Do nothing
 
2845
  }
 
2846
 
 
2847
  // Port 137
 
2848
  if ( Smb4KSettings::usePort137() )
 
2849
  {
 
2850
    arguments << "-r";
 
2851
  }
 
2852
  else
 
2853
  {
 
2854
    // Do nothing
 
2855
  }
 
2856
 
 
2857
  // Broadcast address
 
2858
  QHostAddress address( Smb4KSettings::broadcastAddress() );
 
2859
 
 
2860
  if ( !Smb4KSettings::broadcastAddress().isEmpty() &&
 
2861
       address.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol )
 
2862
  {
 
2863
    arguments << QString( "-B %1" ).arg( Smb4KSettings::broadcastAddress() );
 
2864
  }
 
2865
  else
 
2866
  {
 
2867
    // Do nothing
 
2868
  }
 
2869
 
 
2870
  // We do not use the WINS server here, because it emerged that using
 
2871
  // the WINS server is sometimes not very reliable. 
 
2872
  
 
2873
  
 
2874
  if ( !winsServer().isEmpty() )
 
2875
  {
 
2876
    arguments << "-R";
 
2877
    arguments << QString( "-U %1" ).arg( KShell::quoteArg( winsServer() ) );
 
2878
  }
 
2879
  else
 
2880
  {
 
2881
    // Do nothing
 
2882
  }
 
2883
 
 
2884
  arguments << "--";
 
2885
  arguments << KShell::quoteArg( m_host->hostName() );
 
2886
  
 
2887
  m_proc = new Smb4KProcess( this );
 
2888
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
2889
  m_proc->setShellCommand( arguments.join( " " ) );
 
2890
 
 
2891
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
 
2892
 
 
2893
  m_proc->start();
 
2894
}
 
2895
 
 
2896
 
 
2897
void Smb4KLookupIPAddressJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
2898
{
 
2899
  switch ( exitStatus )
 
2900
  {
 
2901
    case QProcess::CrashExit:
 
2902
    {
 
2903
      if ( !m_proc->isAborted() )
 
2904
      {
 
2905
        Smb4KNotification *notification = new Smb4KNotification();
 
2906
        notification->processError( m_proc->error() );
 
2907
      }
 
2908
      else
 
2909
      {
 
2910
        // Do nothing
 
2911
      }
 
2912
      break;
 
2913
    }
 
2914
    default:
 
2915
    {
 
2916
      processIPAddress();
 
2917
      break;
 
2918
    }
 
2919
  }
 
2920
 
 
2921
  emitResult();
 
2922
}
 
2923
 
2673
2924
 
2674
2925
 
2675
2926
#include "smb4kscanner_p.moc"