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

« back to all changes in this revision

Viewing changes to core/smb4kipaddressscanner.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-05-19 18:54:34 UTC
  • mfrom: (1.1.20)
  • Revision ID: package-import@ubuntu.com-20120519185434-duffny2n87214n1n
Tags: 1.0.1-1
* New upstream release.
* Update debian/compat: bump to 9.
* Update debian/control:
  - bump debhelper to 9.
  - bump kdelibs5-dev build dependency to 4:4.4.0.
  - bump Standards-Version to 3.9.3 (no changes needed).
  - Replace smbfs dependency by cifs-utils. (Closes: #638162)
* Update debian/copyright:
  - update upstream URL.
  - update upstream e-mail.
* Update debian/smb4k.lintian-overrides file.
* Update debian/watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
    smb4kipaddressscanner  -  This class scans for IP addresses. It
3
 
    belongs to the core classes of Smb4K.
 
2
    smb4kipaddressscanner  -  This class scans for IP addresses.
4
3
                             -------------------
5
 
    begin                : Di Apr 22 2008
6
 
    copyright            : (C) 2008 by Alexander Reinholdt
7
 
    email                : dustpuppy@users.berlios.de
 
4
    begin                : Fri Mar 18 2011
 
5
    copyright            : (C) 2011 by Alexander Reinholdt
 
6
    email                : alexander.reinholdt@kdemail.net
8
7
 ***************************************************************************/
9
8
 
10
9
/***************************************************************************
20
19
 *                                                                         *
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,   *
24
 
 *   MA  02111-1307 USA                                                    *
 
22
 *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
 
23
 *   MA 02110-1335, USA                                                    *
25
24
 ***************************************************************************/
26
25
 
 
26
// Qt includes
 
27
#include <QTimer>
 
28
#include <QDebug>
 
29
#include <QCoreApplication>
 
30
 
27
31
// KDE includes
28
32
#include <kglobal.h>
29
 
#include <kdebug.h>
30
 
#include <kshell.h>
31
33
 
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>
40
39
 
41
40
using namespace Smb4KGlobal;
42
41
 
43
 
class Smb4KIPAddressScannerPrivate
44
 
{
45
 
  public:
46
 
    /**
47
 
     * The Smb4KIPAddressScanner instance
48
 
     */
49
 
    Smb4KIPAddressScanner instance;
50
 
};
51
 
 
52
 
 
53
 
K_GLOBAL_STATIC( Smb4KIPAddressScannerPrivate, m_priv );
54
 
 
55
 
 
56
 
Smb4KIPAddressScanner::Smb4KIPAddressScanner() : QObject()
57
 
{
58
 
  m_auto_scan = false;
59
 
  m_timer_id = -1;
60
 
  m_process_error = (QProcess::ProcessError)(-1);
61
 
  m_working = false;
62
 
 
63
 
  m_proc = new KProcess( this );
64
 
 
65
 
  connect( m_proc, SIGNAL( readyReadStandardOutput() ),
66
 
           this,   SLOT( slotReceivedIPAddresses() ) );
67
 
 
68
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ),
69
 
           this,   SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
70
 
 
71
 
  connect( m_proc, SIGNAL( error( QProcess::ProcessError ) ),
72
 
           this,   SLOT( slotProcessError( QProcess::ProcessError ) ) );
 
42
 
 
43
K_GLOBAL_STATIC( Smb4KIPAddressScannerPrivate, p );
 
44
 
 
45
 
 
46
Smb4KIPAddressScanner::Smb4KIPAddressScanner() : KCompositeJob( 0 )
 
47
{
 
48
  setAutoDelete( false );
 
49
  
 
50
  connect( QCoreApplication::instance(), SIGNAL( aboutToQuit() ), SLOT( slotAboutToQuit() ) );
73
51
}
74
52
 
75
53
 
80
58
 
81
59
Smb4KIPAddressScanner *Smb4KIPAddressScanner::self()
82
60
{
83
 
  return &m_priv->instance;
84
 
}
85
 
 
86
 
 
87
 
void Smb4KIPAddressScanner::triggerScan()
88
 
{
89
 
  if ( m_auto_scan )
90
 
  {
91
 
    return;
92
 
  }
93
 
 
94
 
  if ( m_working )
95
 
  {
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 )
100
 
    {
101
 
      m_timer_id = startTimer( timerInterval() );
102
 
    }
103
 
    else
104
 
    {
105
 
      // Do nothing
106
 
    }
107
 
  }
108
 
  else
109
 
  {
110
 
    // Immediately start the lookup process.
111
 
    lookupIPAddresses();
112
 
  }
113
 
}
114
 
 
115
 
 
116
 
void Smb4KIPAddressScanner::setAutoScan( int interval )
117
 
{
118
 
  if ( interval != 0 )
119
 
  {
120
 
    if ( m_timer_id != -1 )
121
 
    {
122
 
      killTimer( m_timer_id );
123
 
 
124
 
      m_timer_id = -1;
125
 
    }
126
 
    else
127
 
    {
128
 
      // Do nothing
129
 
    }
130
 
 
131
 
    m_timer_id = startTimer( interval );
132
 
  }
133
 
  else
134
 
  {
135
 
    killTimer( m_timer_id );
136
 
 
137
 
    m_timer_id = -1;
 
61
  return &p->instance;
 
62
}
 
63
 
 
64
 
 
65
void Smb4KIPAddressScanner::lookup( bool force, QWidget *parent )
 
66
{
 
67
  if ( !hostsList().isEmpty() )
 
68
  {
 
69
    for ( int i = 0; i < hostsList().size(); ++i )
 
70
    {
 
71
      // We do not need to lookup the IP address for a
 
72
      // host that already has got one except 'force' is 
 
73
      // true.
 
74
      if ( !hostsList().at( i )->ipChecked() || force )
 
75
      {
 
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 );
 
80
 
 
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 * ) ) );
 
85
 
 
86
        addSubjob( job );
 
87
 
 
88
        job->start();
 
89
      }
 
90
      else
 
91
      {
 
92
        emit ipAddress( hostsList()[i] );
 
93
      }
 
94
    }
 
95
  }
 
96
  else
 
97
  {
 
98
    // Do nothing
 
99
  }
 
100
}
 
101
 
 
102
 
 
103
void Smb4KIPAddressScanner::getIPAddress( Smb4KWorkgroup *workgroup )
 
104
{
 
105
  for ( int i = 0; i < hostsList().size(); ++i )
 
106
  {
 
107
    if ( !hostsList().at( i )->workgroupName().isEmpty() )
 
108
    {
 
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 )
 
113
      {
 
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() )
 
117
        {
 
118
          workgroup->setMasterBrowserIP( hostsList().at( i )->ip() );
 
119
        }
 
120
        else
 
121
        {
 
122
          continue;
 
123
        }
 
124
        break;
 
125
      }
 
126
      else
 
127
      {
 
128
        // Do nothing
 
129
      }
 
130
    }
 
131
    else
 
132
    {
 
133
      if ( QString::compare( hostsList().at( i )->hostName(),
 
134
           workgroup->masterBrowserName(), Qt::CaseInsensitive ) == 0 )
 
135
      {
 
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() )
 
139
        {
 
140
          workgroup->setMasterBrowserIP( hostsList().at( i )->ip() );
 
141
        }
 
142
        else
 
143
        {
 
144
          // Do nothing
 
145
        }
 
146
        break;
 
147
      }
 
148
      else
 
149
      {
 
150
        continue;
 
151
      }
 
152
    }
138
153
  }
139
154
}
140
155
 
141
156
 
142
157
void Smb4KIPAddressScanner::getIPAddress( Smb4KHost *host )
143
158
{
144
 
  if ( host )
145
 
  {
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() );
150
 
 
151
 
    if ( knownHost )
152
 
    {
153
 
      if ( knownHost->ipChecked() )
154
 
      {
155
 
        // Copy the IP address and emit the ipAddress() signal.
156
 
        host->setIP( knownHost->ip() );
157
 
        emit ipAddress( host );
158
 
      }
159
 
      else
160
 
      {
161
 
        // Do nothing. This will be done by the scanner
162
 
        // automatically.
163
 
      }
164
 
    }
165
 
    else
166
 
    {
167
 
      m_queue.append( *host );
168
 
    }
169
 
  }
170
 
  else
171
 
  {
172
 
    // Do nothing
173
 
  }
174
 
 
175
 
  triggerScan();
176
 
}
177
 
 
178
 
 
179
 
void Smb4KIPAddressScanner::timerEvent( QTimerEvent */*e*/ )
180
 
{
181
 
  if ( !m_working )
182
 
  {
183
 
    if ( !m_auto_scan )
184
 
    {
185
 
      // Kill the timer, because it served its purpose.
186
 
      killTimer( m_timer_id );
187
 
      m_timer_id = -1;
188
 
    }
189
 
    else
190
 
    {
191
 
      // Do nothing
192
 
    }
193
 
 
194
 
    lookupIPAddresses();
195
 
  }
196
 
  else
197
 
  {
198
 
    // Do nothing
199
 
  }
200
 
}
201
 
 
202
 
 
203
 
void Smb4KIPAddressScanner::lookupIPAddresses()
204
 
{
205
 
  m_working = true;
206
 
 
207
 
  QString command = QString();
208
 
 
209
 
  // First process the buffered entries in the queue.
210
 
  if ( !m_queue.isEmpty() )
211
 
  {
212
 
    for ( int i = 0; i < m_queue.size(); ++i )
213
 
    {
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( " ; " );
220
 
    }
221
 
  }
222
 
  else
223
 
  {
224
 
    for ( int i = 0; i < hostsList()->size(); ++i )
225
 
    {
226
 
      if ( !hostsList()->at( i )->ipChecked() )
227
 
      {
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( " ; " );
234
 
 
235
 
        continue;
236
 
      }
237
 
      else
238
 
      {
239
 
        continue;
240
 
      }
241
 
    }
242
 
  }
243
 
 
244
 
  command.truncate( command.length() - 3 );
245
 
 
246
 
  if ( !command.isEmpty() )
247
 
  {
248
 
    m_proc->setShellCommand( command );
249
 
    m_proc->setOutputChannelMode( KProcess::SeparateChannels );
250
 
    m_proc->start();
251
 
  }
252
 
  else
253
 
  {
254
 
    m_working = false;
255
 
  }
256
 
}
257
 
 
258
 
 
259
 
/////////////////////////////////////////////////////////////////////////////
260
 
// SLOT IMPLEMENTATIONS
261
 
/////////////////////////////////////////////////////////////////////////////
262
 
 
263
 
void Smb4KIPAddressScanner::slotReceivedIPAddresses()
264
 
{
265
 
  QString buffer = QString::fromLocal8Bit( m_proc->readAllStandardOutput(), -1 );
266
 
 
267
 
  if ( !buffer.trimmed().isEmpty() )
268
 
  {
269
 
    QString host_ip   = buffer.trimmed().section( " ", 0, 0 ).trimmed();
270
 
    QString host_name = buffer.trimmed().section( " ", 1, 1 ).section( "<00>", 0, 0 ).trimmed();
271
 
 
272
 
    if ( !host_name.isEmpty() && !host_ip.isEmpty() )
273
 
    {
274
 
      if ( !m_queue.isEmpty() )
275
 
      {
276
 
        Smb4KHost host;
277
 
 
278
 
        for ( int i = 0; i < m_queue.size(); ++i )
279
 
        {
280
 
          if ( QString::compare( m_queue.at( i ).name(), host_name, Qt::CaseInsensitive ) == 0 )
281
 
          {
282
 
            // Take the host from the list. Never remove or delete it here!
283
 
            host = m_queue.takeAt( i );
284
 
 
285
 
            break;
286
 
          }
287
 
          else
288
 
          {
289
 
            continue;
290
 
          }
291
 
        }
292
 
 
293
 
        host.setIP( host_ip );
294
 
 
295
 
        emit ipAddress( &host );
296
 
      }
297
 
      else
298
 
      {
299
 
        Smb4KHost *host = findHost( host_name );
300
 
 
301
 
        if ( host )
302
 
        {
303
 
          host->setIP( host_ip );
304
 
 
305
 
          if ( host->isMasterBrowser() )
306
 
          {
307
 
            Smb4KWorkgroup *workgroup = findWorkgroup( host->workgroup() );
308
 
 
309
 
            if ( workgroup )
310
 
            {
311
 
              workgroup->setMasterBrowserIP( host_ip );
312
 
            }
313
 
            else
314
 
            {
315
 
              // Do nothing
316
 
            }
317
 
          }
318
 
          else
319
 
          {
320
 
            // Do nothing
321
 
          }
322
 
 
323
 
          emit ipAddress( host );
324
 
        }
325
 
        else
326
 
        {
327
 
          // Do nothing
328
 
        }
329
 
      }
330
 
    }
331
 
    else
332
 
    {
333
 
      // Do nothing
334
 
    }
335
 
  }
336
 
  else
337
 
  {
338
 
    // Do nothing
339
 
  }
340
 
}
341
 
 
342
 
 
343
 
void Smb4KIPAddressScanner::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
344
 
{
345
 
  if ( exitStatus == QProcess::NormalExit )
346
 
  {
347
 
    m_proc->clearProgram();
348
 
  }
349
 
  else
350
 
  {
351
 
    if ( m_process_error != -1 )
352
 
    {
353
 
      Smb4KCoreMessage::processError( ERROR_PROCESS_ERROR, m_process_error );
354
 
    }
355
 
    else
356
 
    {
357
 
      Smb4KCoreMessage::processError( ERROR_PROCESS_EXIT, m_process_error );
358
 
    }
359
 
  }
360
 
 
361
 
  m_process_error = (QProcess::ProcessError)(-1);
362
 
  m_working = false;
363
 
}
364
 
 
365
 
 
366
 
void Smb4KIPAddressScanner::slotProcessError( QProcess::ProcessError errorCode )
367
 
{
368
 
  m_process_error = errorCode;
369
 
}
370
 
 
371
 
#include <smb4kipaddressscanner.moc>
 
159
  for ( int i = 0; i < hostsList().size(); ++i )
 
160
  {
 
161
    if ( !hostsList().at( i )->workgroupName().isEmpty() && host->workgroupName().isEmpty() )
 
162
    {
 
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 )
 
167
      {
 
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() )
 
171
        {
 
172
          host->setIP( hostsList().at( i )->ip() );
 
173
        }
 
174
        else
 
175
        {
 
176
          // Do nothing
 
177
        }
 
178
        break;
 
179
      }
 
180
      else
 
181
      {
 
182
        continue;
 
183
      }
 
184
    }
 
185
    else
 
186
    {
 
187
      if ( QString::compare( hostsList().at( i )->hostName(),
 
188
           host->hostName(), Qt::CaseInsensitive ) == 0 )
 
189
      {
 
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() )
 
193
        {
 
194
          host->setIP( hostsList().at( i )->ip() );
 
195
        }
 
196
        else
 
197
        {
 
198
          // Do nothing
 
199
        }
 
200
        break;
 
201
      }
 
202
      else
 
203
      {
 
204
        continue;
 
205
      }
 
206
    }
 
207
  }
 
208
}
 
209
 
 
210
 
 
211
bool Smb4KIPAddressScanner::isRunning()
 
212
{
 
213
  return !subjobs().isEmpty();
 
214
}
 
215
 
 
216
 
 
217
void Smb4KIPAddressScanner::abortAll()
 
218
{
 
219
  for ( int i = 0; i < subjobs().size(); ++i )
 
220
  {
 
221
    subjobs().at( i )->kill( KJob::EmitResult );
 
222
  }
 
223
}
 
224
 
 
225
 
 
226
void Smb4KIPAddressScanner::start()
 
227
{
 
228
  QTimer::singleShot( 0, this, SLOT( slotStartJobs() ) );
 
229
}
 
230
 
 
231
 
 
232
/////////////////////////////////////////////////////////////////////////////
 
233
//   SLOT IMPLEMENTATIONS
 
234
/////////////////////////////////////////////////////////////////////////////
 
235
 
 
236
void Smb4KIPAddressScanner::slotStartJobs()
 
237
{
 
238
  // FIXME: Not implemented yet. I do not see a use case at the moment.
 
239
}
 
240
 
 
241
 
 
242
void Smb4KIPAddressScanner::slotProcessIPAddress( Smb4KHost *host )
 
243
{
 
244
  if ( host->isMasterBrowser() )
 
245
  {
 
246
    Smb4KWorkgroup *workgroup = findWorkgroup( host->workgroupName() );
 
247
 
 
248
    if ( workgroup )
 
249
    {
 
250
      workgroup->setMasterBrowserIP( host->ip() );
 
251
    }
 
252
    else
 
253
    {
 
254
      // Do nothing
 
255
    }
 
256
  }
 
257
  else
 
258
  {
 
259
    // Do nothing
 
260
  }
 
261
  
 
262
  QList<Smb4KShare *> shares = sharedResources( host );
 
263
  
 
264
  if ( !shares.isEmpty() )
 
265
  {
 
266
    for ( int i = 0; i < shares.size(); ++i )
 
267
    {
 
268
      shares[i]->setHostIP( host->ip() );
 
269
    }
 
270
  }
 
271
  else
 
272
  {
 
273
    // Do nothing
 
274
  }
 
275
 
 
276
  emit ipAddress( host );
 
277
}
 
278
 
 
279
 
 
280
void Smb4KIPAddressScanner::slotJobFinished( KJob *job )
 
281
{
 
282
  removeSubjob( job );
 
283
}
 
284
 
 
285
 
 
286
void Smb4KIPAddressScanner::slotAboutToQuit()
 
287
{
 
288
  abortAll();
 
289
}
 
290
 
 
291
#include "smb4kipaddressscanner.moc"