• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
  • management
networkscanner.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001-2002 Michael Goffioul <tdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #define USE_QSOCKET
21 
22 #include "networkscanner.h"
23 
24 #include <tqprogressbar.h>
25 #include <kpushbutton.h>
26 #include <tqlayout.h>
27 #include <tqtimer.h>
28 #include <tqlabel.h>
29 #include <tqcombobox.h>
30 #include <tqlineedit.h>
31 #include <tqregexp.h>
32 #include <tqsocket.h>
33 #include <tdelocale.h>
34 #include <kextendedsocket.h>
35 #include <tdemessagebox.h>
36 #include <knumvalidator.h>
37 #include <kdebug.h>
38 #include <unistd.h>
39 
40 class NetworkScanner::NetworkScannerPrivate
41 {
42 public:
43  int port;
44  TQString prefixaddress;
45  int currentaddress;
46  int timeout;
47  bool scanning;
48  TQPtrList<NetworkScanner::SocketInfo> printers;
49 
50  TQProgressBar *bar;
51  KPushButton *scan, *settings;
52  TQLabel *subnetlab;
53  TQTimer *timer;
54 #ifdef USE_QSOCKET
55  TQSocket *socket;
56 #else
57  KExtendedSocket *socket;
58 #endif
59 
60  NetworkScannerPrivate( int portvalue ) : port( portvalue )
61  {
62  prefixaddress = localPrefix();
63  currentaddress = 1;
64  timeout = 50;
65  scanning = false;
66  printers.setAutoDelete( true );
67  }
68  TQString localPrefix();
69  TQString scanString();
70 };
71 
72 TQString NetworkScanner::NetworkScannerPrivate::localPrefix()
73 {
74  char buf[256];
75  buf[0] = '\0';
76  if (!gethostname(buf, sizeof(buf)))
77  buf[sizeof(buf)-1] = '\0';
78  TQPtrList<KAddressInfo> infos = KExtendedSocket::lookup(buf, TQString::null);
79  infos.setAutoDelete(true);
80  if (infos.count() > 0)
81  {
82  TQString IPstr = infos.first()->address()->nodeName();
83  int p = IPstr.findRev('.');
84  IPstr.truncate(p);
85  return IPstr;
86  }
87  return TQString::null;
88 }
89 
90 TQString NetworkScanner::NetworkScannerPrivate::scanString()
91 {
92  TQString s = prefixaddress + ".*";
93  if ( port != -1 )
94  s.append( ":" ).append( TQString::number( port ) );
95  return s;
96 }
97 
98 NetworkScanner::NetworkScanner( int port, TQWidget *parent, const char *name )
99  : TQWidget( parent, name )
100 {
101  d = new NetworkScannerPrivate( port );
102  d->bar = new TQProgressBar( 256, this );
103  d->settings = new KPushButton( KGuiItem( i18n( "&Settings" ), "configure" ), this );
104  d->scan = new KPushButton( KGuiItem( i18n( "Sc&an" ), "viewmag" ), this );
105  d->timer = new TQTimer( this );
106 #ifdef USE_QSOCKET
107  d->socket = new TQSocket( this );
108 #else
109  d->socket = new KExtendedSocket();
110 #endif
111  TQLabel *label = new TQLabel( i18n( "Network scan:" ), this );
112  d->subnetlab = new TQLabel( i18n( "Subnet: %1" ).arg( d->scanString() ), this );
113 
114  TQGridLayout *l0 = new TQGridLayout( this, 4, 2, 0, 10 );
115  l0->addMultiCellWidget( label, 0, 0, 0, 1 );
116  l0->addMultiCellWidget( d->bar, 1, 1, 0, 1 );
117  l0->addMultiCellWidget( d->subnetlab, 2, 2, 0, 1 );
118  l0->addWidget( d->settings, 3, 0 );
119  l0->addWidget( d->scan, 3, 1 );
120 
121  connect( d->timer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotTimeout() ) );
122  connect( d->settings, TQ_SIGNAL( clicked() ), TQ_SLOT( slotSettingsClicked() ) );
123  connect( d->scan, TQ_SIGNAL( clicked() ), TQ_SLOT( slotScanClicked() ) );
124 #ifdef USE_QSOCKET
125  connect( d->socket, TQ_SIGNAL( connected() ), TQ_SLOT( slotConnectionSuccess() ) );
126  connect( d->socket, TQ_SIGNAL( error( int ) ), TQ_SLOT( slotConnectionFailed( int ) ) );
127 #else
128  connect( d->socket, TQ_SIGNAL( connectionSuccess() ), TQ_SLOT( slotConnectionSuccess() ) );
129  connect( d->socket, TQ_SIGNAL( connectionFailed( int ) ), TQ_SLOT( slotConnectionFailed( int ) ) );
130 #endif
131 }
132 
133 NetworkScanner::~NetworkScanner()
134 {
135 #ifndef USE_QSOCKET
136  delete d->socket;
137 #endif
138  delete d;
139 }
140 
141 void NetworkScanner::start()
142 {
143  if ( d->scanning )
144  return;
145 
146  d->printers.clear();
147  emit scanStarted();
148  d->settings->setEnabled( false );
149  d->scan->setGuiItem( KGuiItem( i18n( "&Abort" ), "process-stop" ) );
150  d->currentaddress = -1;
151  d->scanning = true;
152  next();
153 }
154 
155 void NetworkScanner::slotScanClicked()
156 {
157  if ( !d->scanning )
158  {
159  if ( d->localPrefix() == d->prefixaddress ||
160  KMessageBox::warningContinueCancel( this->parentWidget(),
161  i18n( "You are about to scan a subnet (%1.*) that does not "
162  "correspond to the current subnet of this computer (%2.*). Do you want "
163  "to scan the specified subnet anyway?" ).arg( d->prefixaddress ).arg( d->localPrefix() ),
164  TQString::null, KGuiItem( i18n( "&Scan" ), "viewmag" ), "askForScan" ) == KMessageBox::Continue )
165  start();
166  }
167  else
168  {
169 #ifdef USE_QSOCKET
170  d->socket->close();
171 #else
172  d->socket->cancelAsyncConnect();
173 #endif
174  finish();
175  }
176 }
177 
178 void NetworkScanner::finish()
179 {
180  if ( !d->scanning )
181  return;
182 
183  d->settings->setEnabled( true );
184  d->scan->setGuiItem( KGuiItem( i18n( "Sc&an" ), "viewmag" ) );
185  d->bar->reset();
186  d->scanning = false;
187  emit scanFinished();
188 }
189 
190 void NetworkScanner::slotSettingsClicked()
191 {
192  NetworkScannerConfig dlg( this );
193  dlg.exec();
194 }
195 
196 void NetworkScanner::slotNext()
197 {
198  if ( !d->scanning )
199  return;
200 
201  d->timer->stop();
202 #ifdef USE_QSOCKET
203  d->socket->connectToHost( d->prefixaddress + "." + TQString::number( d->currentaddress ), d->port );
204  kdDebug() << "Address: " << d->socket->peerName() << ", Port: " << d->socket->peerPort() << endl;
205 #else
206  d->socket->setAddress( d->prefixaddress + "." + TQString::number( d->currentaddress ), d->port );
207  d->socket->startAsyncLookup();
208  kdDebug() << "Address: " << d->socket->host() << ", Port: " << d->socket->port() << endl;
209 #endif
210  d->timer->start( d->timeout, true );
211 }
212 
213 void NetworkScanner::next()
214 {
215  //kdDebug() << "Next" << endl;
216  d->currentaddress++;
217  if ( d->currentaddress >= 256 )
218  finish();
219  else
220  {
221  d->bar->setProgress( d->currentaddress );
222  TQTimer::singleShot( 0, this, TQ_SLOT( slotNext() ) );
223  }
224 }
225 
226 void NetworkScanner::slotTimeout()
227 {
228  kdDebug() << "Timeout" << endl;
229  if ( !d->scanning )
230  return;
231 
232 #ifdef USE_QSOCKET
233  d->socket->close();
234 #else
235  d->socket->cancelAsyncConnect();
236 #endif
237  next();
238 }
239 
240 void NetworkScanner::slotConnectionSuccess()
241 {
242  kdDebug() << "Success" << endl;
243 #ifdef USE_QSOCKET
244  TDESocketAddress *addr = KExtendedSocket::peerAddress( d->socket->socket() );
245 #else
246  TDESocketAddress *addr = const_cast<TDESocketAddress*>( d->socket->peerAddress() );
247 #endif
248  kdDebug() << "Connection success: " << ( addr ? addr->pretty() : TQString( "ERROR" ) ) << endl;
249  kdDebug() << "Socket: " << d->socket->socket() << endl;
250  if ( addr )
251  {
252  SocketInfo *info = new SocketInfo;
253 #ifdef USE_QSOCKET
254  info->IP = d->socket->peerName();
255 #else
256  info->IP = d->socket->host();
257 #endif
258  info->Port = d->port;
259  TQString portname;
260  KExtendedSocket::resolve( addr, info->Name, portname );
261  d->printers.append( info );
262  d->socket->close();
263  delete addr;
264  }
265  else
266  kdDebug() << "Unconnected socket, skipping" << endl;
267  next();
268 }
269 
270 void NetworkScanner::slotConnectionFailed( int )
271 {
272  kdDebug() << "Failure" << endl;
273  next();
274 }
275 
276 const TQPtrList<NetworkScanner::SocketInfo>* NetworkScanner::printerList()
277 {
278  return &( d->printers );
279 }
280 
281 int NetworkScanner::timeout() const
282 {
283  return d->timeout;
284 }
285 
286 void NetworkScanner::setTimeout( int to )
287 {
288  d->timeout = to;
289 }
290 
291 TQString NetworkScanner::subnet() const
292 {
293  return d->prefixaddress;
294 }
295 
296 void NetworkScanner::setSubnet( const TQString& sn )
297 {
298  d->prefixaddress = sn;
299  d->subnetlab->setText( i18n( "Subnet: %1" ).arg( d->scanString() ) );
300 }
301 
302 int NetworkScanner::port() const
303 {
304  return d->port;
305 }
306 
307 void NetworkScanner::setPort( int p )
308 {
309  d->port = p;
310  d->subnetlab->setText( i18n( "Subnet: %1" ).arg( d->scanString() ) );
311 }
312 
313 bool NetworkScanner::checkPrinter( const TQString& host, int port )
314 {
315  // try first to find it in the SocketInfo list
316  TQPtrListIterator<NetworkScanner::SocketInfo> it( d->printers );
317  for ( ; it.current(); ++it )
318  {
319  if ( port == it.current()->Port && ( host == it.current()->IP ||
320  host == it.current()->Name ) )
321  return true;
322  }
323 
324  // not found in SocketInfo list, try to establish connection
325  KExtendedSocket extsock( host, port );
326  extsock.setBlockingMode( false );
327  extsock.setTimeout( 0, d->timeout * 1000 );
328  return ( extsock.connect() == 0 );
329 }
330 
331 NetworkScannerConfig::NetworkScannerConfig(NetworkScanner *scanner, const char *name)
332  : KDialogBase(scanner, name, true, TQString::null, Ok|Cancel, Ok, true)
333 {
334  scanner_ = scanner;
335  TQWidget *dummy = new TQWidget(this);
336  setMainWidget(dummy);
337  KIntValidator *val = new KIntValidator( this );
338  TQLabel *masklabel = new TQLabel(i18n("&Subnetwork:"),dummy);
339  TQLabel *portlabel = new TQLabel(i18n("&Port:"),dummy);
340  TQLabel *toutlabel = new TQLabel(i18n("&Timeout (ms):"),dummy);
341  TQLineEdit *mm = new TQLineEdit(dummy);
342  mm->setText(TQString::fromLatin1(".[0-255]"));
343  mm->setReadOnly(true);
344  mm->setFixedWidth(fontMetrics().width(mm->text())+10);
345 
346  mask_ = new TQLineEdit(dummy);
347  mask_->setAlignment(TQt::AlignRight);
348  port_ = new TQComboBox(true,dummy);
349  if ( port_->lineEdit() )
350  port_->lineEdit()->setValidator( val );
351  tout_ = new TQLineEdit(dummy);
352  tout_->setValidator( val );
353 
354  masklabel->setBuddy(mask_);
355  portlabel->setBuddy(port_);
356  toutlabel->setBuddy(tout_);
357 
358  mask_->setText(scanner_->subnet());
359  port_->insertItem("631");
360  port_->insertItem("9100");
361  port_->insertItem("9101");
362  port_->insertItem("9102");
363  port_->setEditText(TQString::number(scanner_->port()));
364  tout_->setText(TQString::number(scanner_->timeout()));
365 
366  TQGridLayout *main_ = new TQGridLayout(dummy, 3, 2, 0, 10);
367  TQHBoxLayout *lay1 = new TQHBoxLayout(0, 0, 5);
368  main_->addWidget(masklabel, 0, 0);
369  main_->addWidget(portlabel, 1, 0);
370  main_->addWidget(toutlabel, 2, 0);
371  main_->addLayout(lay1, 0, 1);
372  main_->addWidget(port_, 1, 1);
373  main_->addWidget(tout_, 2, 1);
374  lay1->addWidget(mask_,1);
375  lay1->addWidget(mm,0);
376 
377  resize(250,130);
378  setCaption(i18n("Scan Configuration"));
379 }
380 
381 NetworkScannerConfig::~NetworkScannerConfig()
382 {
383 }
384 
385 void NetworkScannerConfig::slotOk()
386 {
387  TQString msg;
388  TQRegExp re("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
389  if (!re.exactMatch(mask_->text()))
390  msg = i18n("Wrong subnetwork specification.");
391  else
392  {
393  for (int i=1; i<=3; i++)
394  if (re.cap(i).toInt() >= 255)
395  {
396  msg = i18n("Wrong subnetwork specification.");
397  break;
398  }
399  }
400 
401  bool ok(false);
402  int v = tout_->text().toInt(&ok);
403  if (!ok || v <= 0)
404  msg = i18n("Wrong timeout specification.");
405  v = port_->currentText().toInt(&ok);
406  if (!ok || v <= 0)
407  msg = i18n("Wrong port specification.");
408  if (!msg.isEmpty())
409  {
410  KMessageBox::error(this,msg);
411  return;
412  }
413 
414  scanner_->setTimeout( tout_->text().toInt() );
415  scanner_->setSubnet( mask_->text() );
416  scanner_->setPort( port_->currentText().toInt() );
417 
418  KDialogBase::slotOk();
419 }
420 
421 #include "networkscanner.moc"

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.1
This website is maintained by Timothy Pearson.