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

tdeprint

  • tdeprint
  • management
kmwsocketutil.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4  *
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  **/
20 
21 #include <config.h>
22 
23 #include "kmwsocketutil.h"
24 
25 #include <tqprogressbar.h>
26 #include <tqlineedit.h>
27 #include <tqlabel.h>
28 #include <tqcombobox.h>
29 #include <tqpushbutton.h>
30 #include <tdemessagebox.h>
31 #include <tqlayout.h>
32 #include <tqregexp.h>
33 #include <knumvalidator.h>
34 
35 #include <tdeapplication.h>
36 #include <tdelocale.h>
37 #include <kextsock.h>
38 #include <kdebug.h>
39 
40 #include <unistd.h>
41 
42 TQString localRootIP();
43 
44 //----------------------------------------------------------------------------------------
45 
46 SocketConfig::SocketConfig(KMWSocketUtil *util, TQWidget *parent, const char *name)
47 : KDialogBase(parent, name, true, TQString::null, Ok|Cancel, Ok, true)
48 {
49  TQWidget *dummy = new TQWidget(this);
50  setMainWidget(dummy);
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);
59 
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 );
67 
68  masklabel->setBuddy(mask_);
69  portlabel->setBuddy(port_);
70  toutlabel->setBuddy(tout_);
71 
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_));
79 
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);
90 
91  resize(250,130);
92  setCaption(i18n("Scan Configuration"));
93 }
94 
95 SocketConfig::~SocketConfig()
96 {
97 }
98 
99 void SocketConfig::slotOk()
100 {
101  TQString msg;
102  TQRegExp re("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
103  if (!re.exactMatch(mask_->text()))
104  msg = i18n("Wrong subnetwork specification.");
105  else
106  {
107  for (int i=1; i<=3; i++)
108  if (re.cap(i).toInt() >= 255)
109  {
110  msg = i18n("Wrong subnetwork specification.");
111  break;
112  }
113  }
114 
115  bool ok(false);
116  int v = tout_->text().toInt(&ok);
117  if (!ok || v <= 0)
118  msg = i18n("Wrong timeout specification.");
119  v = port_->currentText().toInt(&ok);
120  if (!ok || v <= 0)
121  msg = i18n("Wrong port specification.");
122  if (!msg.isEmpty())
123  {
124  KMessageBox::error(this,msg);
125  return;
126  }
127 
128  KDialogBase::slotOk();
129 }
130 
131 //----------------------------------------------------------------------------------------
132 
133 KMWSocketUtil::KMWSocketUtil()
134 {
135  printerlist_.setAutoDelete(true);
136  root_ = localRootIP();
137  port_ = 9100;
138  timeout_ = 50;
139 }
140 
141 bool KMWSocketUtil::checkPrinter(const TQString& IPstr, int port, TQString* hostname)
142 {
143  KExtendedSocket sock(IPstr, port, KExtendedSocket::inetSocket|KExtendedSocket::streamSocket);
144  bool result(false);
145  sock.setTimeout(0, timeout_ * 1000);
146  if (sock.connect() == 0)
147  {
148  if (hostname)
149  {
150  TQString portname;
151  KExtendedSocket::resolve((TDESocketAddress*)(sock.peerAddress()), *hostname, portname);
152  }
153  result = true;
154  }
155  sock.close();
156  return result;
157 }
158 
159 bool KMWSocketUtil::scanNetwork(TQProgressBar *bar)
160 {
161  printerlist_.setAutoDelete(true);
162  printerlist_.clear();
163  int n(256);
164  if (bar)
165  bar->setTotalSteps(n);
166  for (int i=0; i<n; i++)
167  {
168  TQString IPstr = root_ + "." + TQString::number(i);
169  TQString hostname;
170  if (checkPrinter(IPstr, port_, &hostname))
171  { // we found a printer at this address, create SocketInfo entry in printer list
172  SocketInfo *info = new SocketInfo;
173  info->IP = IPstr;
174  info->Port = port_;
175  info->Name = hostname;
176  printerlist_.append(info);
177  }
178  if (bar)
179  {
180  bar->setProgress(i);
181  kapp->flushX();
182  }
183  }
184  return true;
185 }
186 
187 void KMWSocketUtil::configureScan(TQWidget *parent)
188 {
189  SocketConfig *dlg = new SocketConfig(this,parent);
190  if (dlg->exec())
191  {
192  root_ = dlg->mask_->text();
193  port_ = dlg->port_->currentText().toInt();
194  timeout_ = dlg->tout_->text().toInt();
195  }
196  delete dlg;
197 }
198 
199 //----------------------------------------------------------------------------------------
200 
201 TQString localRootIP()
202 {
203  char buf[256];
204  buf[0] = '\0';
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)
210  {
211  TQString IPstr = infos.first()->address()->nodeName();
212  int p = IPstr.findRev('.');
213  IPstr.truncate(p);
214  return IPstr;
215  }
216  return TQString::null;
217 }
218 
219 #include "kmwsocketutil.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.