kmwsocketutil.cpp
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be> 00004 * 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License version 2 as published by the Free Software Foundation. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 **/ 00020 00021 #include <config.h> 00022 00023 #include "kmwsocketutil.h" 00024 00025 #include <tqprogressbar.h> 00026 #include <tqlineedit.h> 00027 #include <tqlabel.h> 00028 #include <tqcombobox.h> 00029 #include <tqpushbutton.h> 00030 #include <tdemessagebox.h> 00031 #include <tqlayout.h> 00032 #include <tqregexp.h> 00033 #include <knumvalidator.h> 00034 00035 #include <tdeapplication.h> 00036 #include <tdelocale.h> 00037 #include <kextsock.h> 00038 #include <kdebug.h> 00039 00040 #include <unistd.h> 00041 00042 TQString localRootIP(); 00043 00044 //---------------------------------------------------------------------------------------- 00045 00046 SocketConfig::SocketConfig(KMWSocketUtil *util, TQWidget *parent, const char *name) 00047 : KDialogBase(parent, name, true, TQString::null, Ok|Cancel, Ok, true) 00048 { 00049 TQWidget *dummy = new TQWidget(this); 00050 setMainWidget(dummy); 00051 KIntValidator *val = new KIntValidator( this ); 00052 TQLabel *masklabel = new TQLabel(i18n("&Subnetwork:"),dummy); 00053 TQLabel *portlabel = new TQLabel(i18n("&Port:"),dummy); 00054 TQLabel *toutlabel = new TQLabel(i18n("&Timeout (ms):"),dummy); 00055 TQLineEdit *mm = new TQLineEdit(dummy); 00056 mm->setText(TQString::fromLatin1(".[0-255]")); 00057 mm->setReadOnly(true); 00058 mm->setFixedWidth(fontMetrics().width(mm->text())+10); 00059 00060 mask_ = new TQLineEdit(dummy); 00061 mask_->setAlignment(Qt::AlignRight); 00062 port_ = new TQComboBox(true,dummy); 00063 if ( port_->lineEdit() ) 00064 port_->lineEdit()->setValidator( val ); 00065 tout_ = new TQLineEdit(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(TQString::number(util->port_)); 00078 tout_->setText(TQString::number(util->timeout_)); 00079 00080 TQGridLayout *main_ = new TQGridLayout(dummy, 3, 2, 0, 10); 00081 TQHBoxLayout *lay1 = new TQHBoxLayout(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 TQString msg; 00102 TQRegExp 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 TQString& IPstr, int port, TQString* 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 TQString portname; 00151 KExtendedSocket::resolve((TDESocketAddress*)(sock.peerAddress()), *hostname, portname); 00152 } 00153 result = true; 00154 } 00155 sock.close(); 00156 return result; 00157 } 00158 00159 bool KMWSocketUtil::scanNetwork(TQProgressBar *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 TQString IPstr = root_ + "." + TQString::number(i); 00169 TQString hostname; 00170 if (checkPrinter(IPstr, port_, &hostname)) 00171 { // we found a printer at this address, create SocketInfo entry in printer list 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(TQWidget *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 TQString localRootIP() 00202 { 00203 char buf[256]; 00204 buf[0] = '\0'; 00205 if (!gethostname(buf, sizeof(buf))) 00206 buf[sizeof(buf)-1] = '\0'; 00207 TQPtrList<KAddressInfo> infos = KExtendedSocket::lookup(buf, TQString::null); 00208 infos.setAutoDelete(true); 00209 if (infos.count() > 0) 00210 { 00211 TQString IPstr = infos.first()->address()->nodeName(); 00212 int p = IPstr.findRev('.'); 00213 IPstr.truncate(p); 00214 return IPstr; 00215 } 00216 return TQString::null; 00217 } 00218 00219 #include "kmwsocketutil.moc"