21
20
* You should have received a copy of the GNU General Public License *
22
21
* along with this program; if not, write to the *
23
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22
* Free Software Foundation, 51 Franklin Street, Suite 500, Boston, *
23
* MA 02110-1335, USA *
25
24
***************************************************************************/
29
#include <QCoreApplication>
28
32
#include <kglobal.h>
32
34
// application specific includes
33
35
#include <smb4kipaddressscanner.h>
36
#include <smb4kipaddressscanner_p.h>
34
37
#include <smb4kglobal.h>
35
#include <smb4khost.h>
36
#include <smb4ksambaoptionshandler.h>
37
#include <smb4khost.h>
38
#include <smb4kworkgroup.h>
39
#include <smb4kcoremessage.h>
38
#include <smb4kshare.h>
41
40
using namespace Smb4KGlobal;
43
class Smb4KIPAddressScannerPrivate
47
* The Smb4KIPAddressScanner instance
49
Smb4KIPAddressScanner instance;
53
K_GLOBAL_STATIC( Smb4KIPAddressScannerPrivate, m_priv );
56
Smb4KIPAddressScanner::Smb4KIPAddressScanner() : QObject()
60
m_process_error = (QProcess::ProcessError)(-1);
63
m_proc = new KProcess( this );
65
connect( m_proc, SIGNAL( readyReadStandardOutput() ),
66
this, SLOT( slotReceivedIPAddresses() ) );
68
connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ),
69
this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
71
connect( m_proc, SIGNAL( error( QProcess::ProcessError ) ),
72
this, SLOT( slotProcessError( QProcess::ProcessError ) ) );
43
K_GLOBAL_STATIC( Smb4KIPAddressScannerPrivate, p );
46
Smb4KIPAddressScanner::Smb4KIPAddressScanner() : KCompositeJob( 0 )
48
setAutoDelete( false );
50
connect( QCoreApplication::instance(), SIGNAL( aboutToQuit() ), SLOT( slotAboutToQuit() ) );
81
59
Smb4KIPAddressScanner *Smb4KIPAddressScanner::self()
83
return &m_priv->instance;
87
void Smb4KIPAddressScanner::triggerScan()
96
// We will start a timer (if necessary), that is
97
// used to check if the scan process ended and then
98
// starts the lookup process.
99
if ( m_timer_id == -1 )
101
m_timer_id = startTimer( timerInterval() );
110
// Immediately start the lookup process.
116
void Smb4KIPAddressScanner::setAutoScan( int interval )
120
if ( m_timer_id != -1 )
122
killTimer( m_timer_id );
131
m_timer_id = startTimer( interval );
135
killTimer( m_timer_id );
65
void Smb4KIPAddressScanner::lookup( bool force, QWidget *parent )
67
if ( !hostsList().isEmpty() )
69
for ( int i = 0; i < hostsList().size(); ++i )
71
// We do not need to lookup the IP address for a
72
// host that already has got one except 'force' is
74
if ( !hostsList().at( i )->ipChecked() || force )
76
// Create a new job and add it to the subjobs
77
Smb4KIPLookupJob *job = new Smb4KIPLookupJob( this );
78
job->setObjectName( QString( "IPLookupJob_%1" ).arg( hostsList().at( i )->unc() ) );
79
job->setupLookup( hostsList()[i], parent );
81
connect( job, SIGNAL( result( KJob * ) ), SLOT( slotJobFinished( KJob * ) ) );
82
connect( job, SIGNAL( ipAddress( Smb4KHost * ) ), SLOT( slotProcessIPAddress( Smb4KHost * ) ) );
83
connect( job, SIGNAL( aboutToStart( Smb4KHost * ) ), SIGNAL( aboutToStart( Smb4KHost * ) ) );
84
connect( job, SIGNAL( finished( Smb4KHost * ) ), SIGNAL( finished( Smb4KHost * ) ) );
92
emit ipAddress( hostsList()[i] );
103
void Smb4KIPAddressScanner::getIPAddress( Smb4KWorkgroup *workgroup )
105
for ( int i = 0; i < hostsList().size(); ++i )
107
if ( !hostsList().at( i )->workgroupName().isEmpty() )
109
if ( QString::compare( hostsList().at( i )->workgroupName(),
110
workgroup->workgroupName(), Qt::CaseInsensitive ) == 0 &&
111
QString::compare( hostsList().at( i )->hostName(),
112
workgroup->masterBrowserName(), Qt::CaseInsensitive ) == 0 )
114
// Only set the IP address, if there is one. We can avoid erasing
115
// already existing IP addresses.
116
if ( hostsList().at( i )->hasIP() )
118
workgroup->setMasterBrowserIP( hostsList().at( i )->ip() );
133
if ( QString::compare( hostsList().at( i )->hostName(),
134
workgroup->masterBrowserName(), Qt::CaseInsensitive ) == 0 )
136
// Only set the IP address, if there is one. We can avoid erasing
137
// already existing IP addresses.
138
if ( hostsList().at( i )->hasIP() )
140
workgroup->setMasterBrowserIP( hostsList().at( i )->ip() );
142
157
void Smb4KIPAddressScanner::getIPAddress( Smb4KHost *host )
146
// Lookup the host in the global host list and emit the
147
// IP address, if the host is present the ipChecked()
148
// function returns true.
149
Smb4KHost *knownHost = findHost( host->name(), host->workgroup() );
153
if ( knownHost->ipChecked() )
155
// Copy the IP address and emit the ipAddress() signal.
156
host->setIP( knownHost->ip() );
157
emit ipAddress( host );
161
// Do nothing. This will be done by the scanner
167
m_queue.append( *host );
179
void Smb4KIPAddressScanner::timerEvent( QTimerEvent */*e*/ )
185
// Kill the timer, because it served its purpose.
186
killTimer( m_timer_id );
203
void Smb4KIPAddressScanner::lookupIPAddresses()
207
QString command = QString();
209
// First process the buffered entries in the queue.
210
if ( !m_queue.isEmpty() )
212
for ( int i = 0; i < m_queue.size(); ++i )
214
command.append( "nmblookup" );
215
command.append( Smb4KSambaOptionsHandler::self()->nmblookupOptions() );
216
command.append( Smb4KSambaOptionsHandler::self()->winsServer().isEmpty() ? "" : " -R -U "
217
+KShell::quoteArg( Smb4KSambaOptionsHandler::self()->winsServer() ) );
218
command.append( " -- "+KShell::quoteArg( m_queue.at( i ).name() )+" | grep '<00>'" );
219
command.append( " ; " );
224
for ( int i = 0; i < hostsList()->size(); ++i )
226
if ( !hostsList()->at( i )->ipChecked() )
228
command.append( "nmblookup" );
229
command.append( Smb4KSambaOptionsHandler::self()->nmblookupOptions() );
230
command.append( Smb4KSambaOptionsHandler::self()->winsServer().isEmpty() ? "" : " -R -U "
231
+KShell::quoteArg( Smb4KSambaOptionsHandler::self()->winsServer() ) );
232
command.append( " -- "+KShell::quoteArg( Smb4KGlobal::hostsList()->at( i )->name() )+" | grep '<00>'" );
233
command.append( " ; " );
244
command.truncate( command.length() - 3 );
246
if ( !command.isEmpty() )
248
m_proc->setShellCommand( command );
249
m_proc->setOutputChannelMode( KProcess::SeparateChannels );
259
/////////////////////////////////////////////////////////////////////////////
260
// SLOT IMPLEMENTATIONS
261
/////////////////////////////////////////////////////////////////////////////
263
void Smb4KIPAddressScanner::slotReceivedIPAddresses()
265
QString buffer = QString::fromLocal8Bit( m_proc->readAllStandardOutput(), -1 );
267
if ( !buffer.trimmed().isEmpty() )
269
QString host_ip = buffer.trimmed().section( " ", 0, 0 ).trimmed();
270
QString host_name = buffer.trimmed().section( " ", 1, 1 ).section( "<00>", 0, 0 ).trimmed();
272
if ( !host_name.isEmpty() && !host_ip.isEmpty() )
274
if ( !m_queue.isEmpty() )
278
for ( int i = 0; i < m_queue.size(); ++i )
280
if ( QString::compare( m_queue.at( i ).name(), host_name, Qt::CaseInsensitive ) == 0 )
282
// Take the host from the list. Never remove or delete it here!
283
host = m_queue.takeAt( i );
293
host.setIP( host_ip );
295
emit ipAddress( &host );
299
Smb4KHost *host = findHost( host_name );
303
host->setIP( host_ip );
305
if ( host->isMasterBrowser() )
307
Smb4KWorkgroup *workgroup = findWorkgroup( host->workgroup() );
311
workgroup->setMasterBrowserIP( host_ip );
323
emit ipAddress( host );
343
void Smb4KIPAddressScanner::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
345
if ( exitStatus == QProcess::NormalExit )
347
m_proc->clearProgram();
351
if ( m_process_error != -1 )
353
Smb4KCoreMessage::processError( ERROR_PROCESS_ERROR, m_process_error );
357
Smb4KCoreMessage::processError( ERROR_PROCESS_EXIT, m_process_error );
361
m_process_error = (QProcess::ProcessError)(-1);
366
void Smb4KIPAddressScanner::slotProcessError( QProcess::ProcessError errorCode )
368
m_process_error = errorCode;
371
#include <smb4kipaddressscanner.moc>
159
for ( int i = 0; i < hostsList().size(); ++i )
161
if ( !hostsList().at( i )->workgroupName().isEmpty() && host->workgroupName().isEmpty() )
163
if ( QString::compare( hostsList().at( i )->workgroupName(),
164
host->workgroupName(), Qt::CaseInsensitive ) == 0 &&
165
QString::compare( hostsList().at( i )->hostName(),
166
host->hostName(), Qt::CaseInsensitive ) == 0 )
168
// Only set the IP address, if there is one. We can avoid erasing
169
// already existing IP addresses.
170
if ( hostsList().at( i )->hasIP() )
172
host->setIP( hostsList().at( i )->ip() );
187
if ( QString::compare( hostsList().at( i )->hostName(),
188
host->hostName(), Qt::CaseInsensitive ) == 0 )
190
// Only set the IP address, if there is one. We can avoid erasing
191
// already existing IP addresses.
192
if ( hostsList().at( i )->hasIP() )
194
host->setIP( hostsList().at( i )->ip() );
211
bool Smb4KIPAddressScanner::isRunning()
213
return !subjobs().isEmpty();
217
void Smb4KIPAddressScanner::abortAll()
219
for ( int i = 0; i < subjobs().size(); ++i )
221
subjobs().at( i )->kill( KJob::EmitResult );
226
void Smb4KIPAddressScanner::start()
228
QTimer::singleShot( 0, this, SLOT( slotStartJobs() ) );
232
/////////////////////////////////////////////////////////////////////////////
233
// SLOT IMPLEMENTATIONS
234
/////////////////////////////////////////////////////////////////////////////
236
void Smb4KIPAddressScanner::slotStartJobs()
238
// FIXME: Not implemented yet. I do not see a use case at the moment.
242
void Smb4KIPAddressScanner::slotProcessIPAddress( Smb4KHost *host )
244
if ( host->isMasterBrowser() )
246
Smb4KWorkgroup *workgroup = findWorkgroup( host->workgroupName() );
250
workgroup->setMasterBrowserIP( host->ip() );
262
QList<Smb4KShare *> shares = sharedResources( host );
264
if ( !shares.isEmpty() )
266
for ( int i = 0; i < shares.size(); ++i )
268
shares[i]->setHostIP( host->ip() );
276
emit ipAddress( host );
280
void Smb4KIPAddressScanner::slotJobFinished( KJob *job )
286
void Smb4KIPAddressScanner::slotAboutToQuit()
291
#include "smb4kipaddressscanner.moc"