00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include "kmwsocketutil.h"
00024
00025 #include <qprogressbar.h>
00026 #include <qlineedit.h>
00027 #include <qlabel.h>
00028 #include <qcombobox.h>
00029 #include <qpushbutton.h>
00030 #include <kmessagebox.h>
00031 #include <qlayout.h>
00032 #include <qregexp.h>
00033 #include <knumvalidator.h>
00034
00035 #include <kapplication.h>
00036 #include <klocale.h>
00037 #include <kextsock.h>
00038 #include <kdebug.h>
00039
00040 #include <unistd.h>
00041
00042 QString localRootIP();
00043
00044
00045
00046 SocketConfig::SocketConfig(KMWSocketUtil *util, QWidget *parent, const char *name)
00047 : KDialogBase(parent, name, true, QString::null, Ok|Cancel, Ok, true)
00048 {
00049 QWidget *dummy = new QWidget(this);
00050 setMainWidget(dummy);
00051 KIntValidator *val = new KIntValidator( this );
00052 QLabel *masklabel = new QLabel(i18n("&Subnetwork:"),dummy);
00053 QLabel *portlabel = new QLabel(i18n("&Port:"),dummy);
00054 QLabel *toutlabel = new QLabel(i18n("&Timeout (ms):"),dummy);
00055 QLineEdit *mm = new QLineEdit(dummy);
00056 mm->setText(QString::fromLatin1(".[0-255]"));
00057 mm->setReadOnly(true);
00058 mm->setFixedWidth(fontMetrics().width(mm->text())+10);
00059
00060 mask_ = new QLineEdit(dummy);
00061 mask_->setAlignment(Qt::AlignRight);
00062 port_ = new QComboBox(true,dummy);
00063 if ( port_->lineEdit() )
00064 port_->lineEdit()->setValidator( val );
00065 tout_ = new QLineEdit(dummy);
00066 tout_->setValidator( val );
00067
00068 masklabel->setBuddy(mask_);
00069 portlabel->setBuddy(port_);
00070 toutlabel->setBuddy(tout_);
00071
00072 mask_->setText(util->root_);
00073 port_->insertItem("631");
00074 port_->insertItem("9100");
00075 port_->insertItem("9101");
00076 port_->insertItem("9102");
00077 port_->setEditText(QString::number(util->port_));
00078 tout_->setText(QString::number(util->timeout_));
00079
00080 QGridLayout *main_ = new QGridLayout(dummy, 3, 2, 0, 10);
00081 QHBoxLayout *lay1 = new QHBoxLayout(0, 0, 5);
00082 main_->addWidget(masklabel, 0, 0);
00083 main_->addWidget(portlabel, 1, 0);
00084 main_->addWidget(toutlabel, 2, 0);
00085 main_->addLayout(lay1, 0, 1);
00086 main_->addWidget(port_, 1, 1);
00087 main_->addWidget(tout_, 2, 1);
00088 lay1->addWidget(mask_,1);
00089 lay1->addWidget(mm,0);
00090
00091 resize(250,130);
00092 setCaption(i18n("Scan Configuration"));
00093 }
00094
00095 SocketConfig::~SocketConfig()
00096 {
00097 }
00098
00099 void SocketConfig::slotOk()
00100 {
00101 QString msg;
00102 QRegExp re("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
00103 if (!re.exactMatch(mask_->text()))
00104 msg = i18n("Wrong subnetwork specification.");
00105 else
00106 {
00107 for (int i=1; i<=3; i++)
00108 if (re.cap(i).toInt() >= 255)
00109 {
00110 msg = i18n("Wrong subnetwork specification.");
00111 break;
00112 }
00113 }
00114
00115 bool ok(false);
00116 int v = tout_->text().toInt(&ok);
00117 if (!ok || v <= 0)
00118 msg = i18n("Wrong timeout specification.");
00119 v = port_->currentText().toInt(&ok);
00120 if (!ok || v <= 0)
00121 msg = i18n("Wrong port specification.");
00122 if (!msg.isEmpty())
00123 {
00124 KMessageBox::error(this,msg);
00125 return;
00126 }
00127
00128 KDialogBase::slotOk();
00129 }
00130
00131
00132
00133 KMWSocketUtil::KMWSocketUtil()
00134 {
00135 printerlist_.setAutoDelete(true);
00136 root_ = localRootIP();
00137 port_ = 9100;
00138 timeout_ = 50;
00139 }
00140
00141 bool KMWSocketUtil::checkPrinter(const QString& IPstr, int port, QString* hostname)
00142 {
00143 KExtendedSocket sock(IPstr, port, KExtendedSocket::inetSocket|KExtendedSocket::streamSocket);
00144 bool result(false);
00145 sock.setTimeout(0, timeout_ * 1000);
00146 if (sock.connect() == 0)
00147 {
00148 if (hostname)
00149 {
00150 QString portname;
00151 KExtendedSocket::resolve((KSocketAddress*)(sock.peerAddress()), *hostname, portname);
00152 }
00153 result = true;
00154 }
00155 sock.close();
00156 return result;
00157 }
00158
00159 bool KMWSocketUtil::scanNetwork(QProgressBar *bar)
00160 {
00161 printerlist_.setAutoDelete(true);
00162 printerlist_.clear();
00163 int n(256);
00164 if (bar)
00165 bar->setTotalSteps(n);
00166 for (int i=0; i<n; i++)
00167 {
00168 QString IPstr = root_ + "." + QString::number(i);
00169 QString hostname;
00170 if (checkPrinter(IPstr, port_, &hostname))
00171 {
00172 SocketInfo *info = new SocketInfo;
00173 info->IP = IPstr;
00174 info->Port = port_;
00175 info->Name = hostname;
00176 printerlist_.append(info);
00177 }
00178 if (bar)
00179 {
00180 bar->setProgress(i);
00181 kapp->flushX();
00182 }
00183 }
00184 return true;
00185 }
00186
00187 void KMWSocketUtil::configureScan(QWidget *parent)
00188 {
00189 SocketConfig *dlg = new SocketConfig(this,parent);
00190 if (dlg->exec())
00191 {
00192 root_ = dlg->mask_->text();
00193 port_ = dlg->port_->currentText().toInt();
00194 timeout_ = dlg->tout_->text().toInt();
00195 }
00196 delete dlg;
00197 }
00198
00199
00200
00201 QString localRootIP()
00202 {
00203 char buf[256];
00204 buf[0] = '\0';
00205 if (!gethostname(buf, sizeof(buf)))
00206 buf[sizeof(buf)-1] = '\0';
00207 QPtrList<KAddressInfo> infos = KExtendedSocket::lookup(buf, QString::null);
00208 infos.setAutoDelete(true);
00209 if (infos.count() > 0)
00210 {
00211 QString IPstr = infos.first()->address()->nodeName();
00212 int p = IPstr.findRev('.');
00213 IPstr.truncate(p);
00214 return IPstr;
00215 }
00216 return QString::null;
00217 }
00218
00219 #include "kmwsocketutil.moc"