23 #include "kmwsocketutil.h"
25 #include <tqprogressbar.h>
26 #include <tqlineedit.h>
28 #include <tqcombobox.h>
29 #include <tqpushbutton.h>
30 #include <tdemessagebox.h>
33 #include <knumvalidator.h>
35 #include <tdeapplication.h>
36 #include <tdelocale.h>
42 TQString localRootIP();
46 SocketConfig::SocketConfig(KMWSocketUtil *util, TQWidget *parent,
const char *name)
47 : KDialogBase(parent, name, true, TQString::null, Ok|Cancel, Ok, true)
49 TQWidget *dummy =
new TQWidget(
this);
51 KIntValidator *val =
new KIntValidator(
this );
52 TQLabel *masklabel =
new TQLabel(i18n(
"&Subnetwork:"),dummy);
53 TQLabel *portlabel =
new TQLabel(i18n(
"&Port:"),dummy);
54 TQLabel *toutlabel =
new TQLabel(i18n(
"&Timeout (ms):"),dummy);
55 TQLineEdit *mm =
new TQLineEdit(dummy);
56 mm->setText(TQString::fromLatin1(
".[0-255]"));
57 mm->setReadOnly(
true);
58 mm->setFixedWidth(fontMetrics().width(mm->text())+10);
60 mask_ =
new TQLineEdit(dummy);
61 mask_->setAlignment(TQt::AlignRight);
62 port_ =
new TQComboBox(
true,dummy);
63 if ( port_->lineEdit() )
64 port_->lineEdit()->setValidator( val );
65 tout_ =
new TQLineEdit(dummy);
66 tout_->setValidator( val );
68 masklabel->setBuddy(mask_);
69 portlabel->setBuddy(port_);
70 toutlabel->setBuddy(tout_);
72 mask_->setText(util->root_);
73 port_->insertItem(
"631");
74 port_->insertItem(
"9100");
75 port_->insertItem(
"9101");
76 port_->insertItem(
"9102");
77 port_->setEditText(TQString::number(util->port_));
78 tout_->setText(TQString::number(util->timeout_));
80 TQGridLayout *main_ =
new TQGridLayout(dummy, 3, 2, 0, 10);
81 TQHBoxLayout *lay1 =
new TQHBoxLayout(0, 0, 5);
82 main_->addWidget(masklabel, 0, 0);
83 main_->addWidget(portlabel, 1, 0);
84 main_->addWidget(toutlabel, 2, 0);
85 main_->addLayout(lay1, 0, 1);
86 main_->addWidget(port_, 1, 1);
87 main_->addWidget(tout_, 2, 1);
88 lay1->addWidget(mask_,1);
89 lay1->addWidget(mm,0);
92 setCaption(i18n(
"Scan Configuration"));
95 SocketConfig::~SocketConfig()
99 void SocketConfig::slotOk()
102 TQRegExp re(
"(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
103 if (!re.exactMatch(mask_->text()))
104 msg = i18n(
"Wrong subnetwork specification.");
107 for (
int i=1; i<=3; i++)
108 if (re.cap(i).toInt() >= 255)
110 msg = i18n(
"Wrong subnetwork specification.");
116 int v = tout_->text().toInt(&ok);
118 msg = i18n(
"Wrong timeout specification.");
119 v = port_->currentText().toInt(&ok);
121 msg = i18n(
"Wrong port specification.");
124 KMessageBox::error(
this,msg);
128 KDialogBase::slotOk();
133 KMWSocketUtil::KMWSocketUtil()
135 printerlist_.setAutoDelete(
true);
136 root_ = localRootIP();
141 bool KMWSocketUtil::checkPrinter(
const TQString& IPstr,
int port, TQString* hostname)
143 KExtendedSocket sock(IPstr, port, KExtendedSocket::inetSocket|KExtendedSocket::streamSocket);
145 sock.setTimeout(0, timeout_ * 1000);
146 if (sock.connect() == 0)
151 KExtendedSocket::resolve((TDESocketAddress*)(sock.peerAddress()), *hostname, portname);
159 bool KMWSocketUtil::scanNetwork(TQProgressBar *bar)
161 printerlist_.setAutoDelete(
true);
162 printerlist_.clear();
165 bar->setTotalSteps(n);
166 for (
int i=0; i<n; i++)
168 TQString IPstr = root_ +
"." + TQString::number(i);
170 if (checkPrinter(IPstr, port_, &hostname))
172 SocketInfo *info =
new SocketInfo;
175 info->Name = hostname;
176 printerlist_.append(info);
187 void KMWSocketUtil::configureScan(TQWidget *parent)
189 SocketConfig *dlg =
new SocketConfig(
this,parent);
192 root_ = dlg->mask_->text();
193 port_ = dlg->port_->currentText().toInt();
194 timeout_ = dlg->tout_->text().toInt();
201 TQString localRootIP()
205 if (!gethostname(buf,
sizeof(buf)))
206 buf[
sizeof(buf)-1] =
'\0';
207 TQPtrList<KAddressInfo> infos = KExtendedSocket::lookup(buf, TQString::null);
208 infos.setAutoDelete(
true);
209 if (infos.count() > 0)
211 TQString IPstr = infos.first()->address()->nodeName();
212 int p = IPstr.findRev(
'.');
216 return TQString::null;
219 #include "kmwsocketutil.moc"