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

tdeprint

  • tdeprint
  • lpdunix
kmlpdunixmanager.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 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 #include "kmlpdunixmanager.h"
21 #include "kmfactory.h"
22 #include "kmprinter.h"
23 
24 #include <tqfile.h>
25 #include <tqdir.h>
26 #include <tqfileinfo.h>
27 #include <tqtextstream.h>
28 #include <tqregexp.h>
29 #include <tdelocale.h>
30 #include <kstandarddirs.h>
31 #include <kdebug.h>
32 
33 #include <stdlib.h>
34 
35 /*****************
36  * Utility class *
37  *****************/
38 class KTextBuffer
39 {
40 public:
41  KTextBuffer(TQIODevice *dev) : m_stream(dev) {}
42  bool eof() const { return (m_stream.eof() && m_linebuf.isEmpty()); }
43  TQString readLine();
44  void unreadLine(const TQString& l) { m_linebuf = l; }
45 private:
46  TQTextStream m_stream;
47  TQString m_linebuf;
48 };
49 
50 TQString KTextBuffer::readLine()
51 {
52  TQString line;
53  if (!m_linebuf.isEmpty())
54  {
55  line = m_linebuf;
56  m_linebuf = TQString::null;
57  }
58  else
59  line = m_stream.readLine();
60  return line;
61 }
62 
63 /*****************************
64  * Various parsing functions *
65  *****************************/
66 
67 // Extract a line from a KTextBuffer:
68 // '#' -> comments
69 // '\' -> line continue
70 // ':' or '|' -> line continue (LPRng)
71 //
72 // New entry is detected by a line which have first character different from
73 // '#', '|', ':'. The line is then put back in the IODevice.
74 TQString readLine(KTextBuffer& t)
75 {
76  TQString line, buffer;
77  bool lineContinue(false);
78 
79  while (!t.eof())
80  {
81  buffer = t.readLine().stripWhiteSpace();
82  if (buffer.isEmpty() || buffer[0] == '#')
83  continue;
84  if (buffer[0] == '|' || buffer[0] == ':' || lineContinue || line.isEmpty())
85  {
86  line.append(buffer);
87  if (line.right(1) == "\\")
88  {
89  line.truncate(line.length()-1);
90  line = line.stripWhiteSpace();
91  lineContinue = true;
92  }
93  else
94  lineContinue = false;
95  }
96  else
97  {
98  t.unreadLine(buffer);
99  break;
100  }
101  }
102  return line;
103 }
104 
105 // extact an entry (printcap-like)
106 TQMap<TQString,TQString> readEntry(KTextBuffer& t)
107 {
108  TQString line = readLine(t);
109  TQMap<TQString,TQString> entry;
110 
111  if (!line.isEmpty())
112  {
113  TQStringList l = TQStringList::split(':',line,false);
114  if (l.count() > 0)
115  {
116  int p(-1);
117  if ((p=l[0].find('|')) != -1)
118  entry["printer-name"] = l[0].left(p); // only keep first name (discard aliases
119  else
120  entry["printer-name"] = l[0];
121  for (uint i=1; i<l.count(); i++)
122  if ((p=l[i].find('=')) != -1)
123  entry[l[i].left(p).stripWhiteSpace()] = l[i].right(l[i].length()-p-1).stripWhiteSpace();
124  else
125  entry[l[i].stripWhiteSpace()] = TQString::null;
126  }
127  }
128  return entry;
129 }
130 
131 // create basic printer from entry
132 KMPrinter* createPrinter(const TQMap<TQString,TQString>& entry)
133 {
134  KMPrinter *printer = new KMPrinter();
135  printer->setName(entry["printer-name"]);
136  printer->setPrinterName(entry["printer-name"]);
137  printer->setType(KMPrinter::Printer);
138  printer->setState(KMPrinter::Idle);
139  return printer;
140 }
141 KMPrinter* createPrinter(const TQString& prname)
142 {
143  TQMap<TQString,TQString> map;
144  map["printer-name"] = prname;
145  return createPrinter(map);
146 }
147 
148 // this function support LPRng piping feature, it defaults to
149 // /etc/printcap in any other cases (basic support)
150 TQString getPrintcapFileName()
151 {
152  // check if LPRng system
153  TQString printcap("/etc/printcap");
154  TQFile f("/etc/lpd.conf");
155  if (f.exists() && f.open(IO_ReadOnly))
156  {
157  kdDebug() << "/etc/lpd.conf found: probably LPRng system" << endl;
158  TQTextStream t(&f);
159  TQString line;
160  while (!t.eof())
161  {
162  line = t.readLine().stripWhiteSpace();
163  if (line.startsWith("printcap_path="))
164  {
165  kdDebug() << "printcap_path entry found: " << line << endl;
166  TQString pcentry = line.mid(14).stripWhiteSpace();
167  kdDebug() << "printcap_path value: " << pcentry << endl;
168  if (pcentry[0] == '|')
169  { // printcap through pipe
170  printcap = locateLocal("tmp","printcap");
171  TQString cmd = TQString::fromLatin1("echo \"all\" | %1 > %2").arg(pcentry.mid(1)).arg(printcap);
172  kdDebug() << "printcap obtained through pipe" << endl << "executing: " << cmd << endl;
173  ::system(cmd.local8Bit());
174  }
175  break;
176  }
177  }
178  }
179  kdDebug() << "printcap file returned: " << printcap << endl;
180  return printcap;
181 }
182 
183 // "/etc/printcap" file parsing (Linux/LPR)
184 void KMLpdUnixManager::parseEtcPrintcap()
185 {
186  TQFile f(getPrintcapFileName());
187  if (f.exists() && f.open(IO_ReadOnly))
188  {
189  KTextBuffer t(TQT_TQIODEVICE(&f));
190  TQMap<TQString,TQString> entry;
191 
192  while (!t.eof())
193  {
194  entry = readEntry(t);
195  if (entry.isEmpty() || !entry.contains("printer-name") || entry.contains("server"))
196  continue;
197  if (entry["printer-name"] == "all")
198  {
199  if (entry.contains("all"))
200  {
201  // find separator
202  int p = entry["all"].find(TQRegExp("[^a-zA-Z0-9_\\s-]"));
203  if (p != -1)
204  {
205  TQChar c = entry["all"][p];
206  TQStringList prs = TQStringList::split(c,entry["all"],false);
207  for (TQStringList::ConstIterator it=prs.begin(); it!=prs.end(); ++it)
208  {
209  KMPrinter *printer = ::createPrinter(*it);
210  printer->setDescription(i18n("Description unavailable"));
211  addPrinter(printer);
212  }
213  }
214  }
215  }
216  else
217  {
218  KMPrinter *printer = ::createPrinter(entry);
219  if (entry.contains("rm"))
220  printer->setDescription(i18n("Remote printer queue on %1").arg(entry["rm"]));
221  else
222  printer->setDescription(i18n("Local printer"));
223  addPrinter(printer);
224  }
225  }
226  }
227 }
228 
229 // helper function for NIS support in Solaris-2.6 (use "ypcat printers.conf.byname")
230 TQString getEtcPrintersConfName()
231 {
232  TQString printersconf("/etc/printers.conf");
233  if (!TQFile::exists(printersconf) && !TDEStandardDirs::findExe( "ypcat" ).isEmpty())
234  {
235  // standard file not found, try NIS
236  printersconf = locateLocal("tmp","printers.conf");
237  TQString cmd = TQString::fromLatin1("ypcat printers.conf.byname > %1").arg(printersconf);
238  kdDebug() << "printers.conf obtained from NIS server: " << cmd << endl;
239  ::system(TQFile::encodeName(cmd));
240  }
241  return printersconf;
242 }
243 
244 // "/etc/printers.conf" file parsing (Solaris 2.6)
245 void KMLpdUnixManager::parseEtcPrintersConf()
246 {
247  TQFile f(getEtcPrintersConfName());
248  if (f.exists() && f.open(IO_ReadOnly))
249  {
250  KTextBuffer t(TQT_TQIODEVICE(&f));
251  TQMap<TQString,TQString> entry;
252  TQString default_printer;
253 
254  while (!t.eof())
255  {
256  entry = readEntry(t);
257  if (entry.isEmpty() || !entry.contains("printer-name"))
258  continue;
259  TQString prname = entry["printer-name"];
260  if (prname == "_default")
261  {
262  if (entry.contains("use"))
263  default_printer = entry["use"];
264  }
265  else if (prname != "_all")
266  {
267  KMPrinter *printer = ::createPrinter(entry);
268  if (entry.contains("bsdaddr"))
269  {
270  TQStringList l = TQStringList::split(',',entry["bsdaddr"],false);
271  printer->setDescription(i18n("Remote printer queue on %1").arg(l[0]));
272  }
273  else
274  printer->setDescription(i18n("Local printer"));
275  addPrinter(printer);
276  }
277  }
278 
279  if (!default_printer.isEmpty())
280  setSoftDefault(findPrinter(default_printer));
281  }
282 }
283 
284 // "/etc/lp/printers/" directory parsing (Solaris non-2.6)
285 void KMLpdUnixManager::parseEtcLpPrinters()
286 {
287  TQDir d("/etc/lp/printers");
288  const TQFileInfoList *prlist = d.entryInfoList(TQDir::Dirs);
289  if (!prlist)
290  return;
291 
292  TQFileInfoListIterator it(*prlist);
293  for (;it.current();++it)
294  {
295  if (it.current()->fileName() == "." || it.current()->fileName() == "..")
296  continue;
297  TQFile f(it.current()->absFilePath() + "/configuration");
298  if (f.exists() && f.open(IO_ReadOnly))
299  {
300  KTextBuffer t(TQT_TQIODEVICE(&f));
301  TQString line, remote;
302  while (!t.eof())
303  {
304  line = readLine(t);
305  if (line.isEmpty()) continue;
306  if (line.startsWith("Remote:"))
307  {
308  TQStringList l = TQStringList::split(':',line,false);
309  if (l.count() > 1) remote = l[1];
310  }
311  }
312  KMPrinter *printer = new KMPrinter;
313  printer->setName(it.current()->fileName());
314  printer->setPrinterName(it.current()->fileName());
315  printer->setType(KMPrinter::Printer);
316  printer->setState(KMPrinter::Idle);
317  if (!remote.isEmpty())
318  printer->setDescription(i18n("Remote printer queue on %1").arg(remote));
319  else
320  printer->setDescription(i18n("Local printer"));
321  addPrinter(printer);
322  }
323  }
324 }
325 
326 // "/etc/lp/member/" directory parsing (HP-UX)
327 void KMLpdUnixManager::parseEtcLpMember()
328 {
329  TQDir d("/etc/lp/member");
330  const TQFileInfoList *prlist = d.entryInfoList(TQDir::Files);
331  if (!prlist)
332  return;
333 
334  TQFileInfoListIterator it(*prlist);
335  for (;it.current();++it)
336  {
337  KMPrinter *printer = new KMPrinter;
338  printer->setName(it.current()->fileName());
339  printer->setPrinterName(it.current()->fileName());
340  printer->setType(KMPrinter::Printer);
341  printer->setState(KMPrinter::Idle);
342  printer->setDescription(i18n("Local printer"));
343  addPrinter(printer);
344  }
345 }
346 
347 // "/usr/spool/lp/interfaces/" directory parsing (IRIX 6.x)
348 void KMLpdUnixManager::parseSpoolInterface()
349 {
350  TQDir d("/usr/spool/interfaces/lp");
351  const TQFileInfoList *prlist = d.entryInfoList(TQDir::Files);
352  if (!prlist)
353  return;
354 
355  TQFileInfoListIterator it(*prlist);
356  for (;it.current();++it)
357  {
358  TQFile f(it.current()->absFilePath());
359  if (f.exists() && f.open(IO_ReadOnly))
360  {
361  KTextBuffer t(TQT_TQIODEVICE(&f));
362  TQString line, remote;
363 
364  while (!t.eof())
365  {
366  line = t.readLine().stripWhiteSpace();
367  if (line.startsWith("HOSTNAME"))
368  {
369  TQStringList l = TQStringList::split('=',line,false);
370  if (l.count() > 1) remote = l[1];
371  }
372  }
373 
374  KMPrinter *printer = new KMPrinter;
375  printer->setName(it.current()->fileName());
376  printer->setPrinterName(it.current()->fileName());
377  printer->setType(KMPrinter::Printer);
378  printer->setState(KMPrinter::Idle);
379  if (!remote.isEmpty())
380  printer->setDescription(i18n("Remote printer queue on %1").arg(remote));
381  else
382  printer->setDescription(i18n("Local printer"));
383  addPrinter(printer);
384  }
385  }
386 }
387 
388 //*********************************************************************************************************
389 
390 KMLpdUnixManager::KMLpdUnixManager(TQObject *parent, const char *name, const TQStringList & /*args*/)
391 : KMManager(parent,name)
392 {
393  m_loaded = false;
394 }
395 
396 void KMLpdUnixManager::listPrinters()
397 {
398  // load only once, if already loaded, just keep them (remove discard flag)
399  if (!m_loaded)
400  {
401  parseEtcPrintcap();
402  parseEtcPrintersConf();
403  parseEtcLpPrinters();
404  parseEtcLpMember();
405  parseSpoolInterface();
406  m_loaded = true;
407  }
408  else
409  discardAllPrinters(false);
410 }

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.8.8
This website is maintained by Timothy Pearson.