20 #include "kmlpdunixmanager.h"
21 #include "kmfactory.h"
22 #include "kmprinter.h"
26 #include <tqfileinfo.h>
27 #include <tqtextstream.h>
29 #include <tdelocale.h>
30 #include <kstandarddirs.h>
41 KTextBuffer(TQIODevice *dev) : m_stream(dev) {}
42 bool eof()
const {
return (m_stream.eof() && m_linebuf.isEmpty()); }
44 void unreadLine(
const TQString& l) { m_linebuf = l; }
46 TQTextStream m_stream;
50 TQString KTextBuffer::readLine()
53 if (!m_linebuf.isEmpty())
56 m_linebuf = TQString::null;
59 line = m_stream.readLine();
74 TQString readLine(KTextBuffer& t)
76 TQString line, buffer;
77 bool lineContinue(
false);
81 buffer = t.readLine().stripWhiteSpace();
82 if (buffer.isEmpty() || buffer[0] ==
'#')
84 if (buffer[0] ==
'|' || buffer[0] ==
':' || lineContinue || line.isEmpty())
87 if (line.right(1) ==
"\\")
89 line.truncate(line.length()-1);
90 line = line.stripWhiteSpace();
106 TQMap<TQString,TQString> readEntry(KTextBuffer& t)
108 TQString line = readLine(t);
109 TQMap<TQString,TQString> entry;
113 TQStringList l = TQStringList::split(
':',line,
false);
117 if ((p=l[0].find(
'|')) != -1)
118 entry[
"printer-name"] = l[0].left(p);
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();
125 entry[l[i].stripWhiteSpace()] = TQString::null;
132 KMPrinter* createPrinter(
const TQMap<TQString,TQString>& entry)
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);
141 KMPrinter* createPrinter(
const TQString& prname)
143 TQMap<TQString,TQString> map;
144 map[
"printer-name"] = prname;
145 return createPrinter(map);
150 TQString getPrintcapFileName()
153 TQString printcap(
"/etc/printcap");
154 TQFile f(
"/etc/lpd.conf");
155 if (f.exists() && f.open(IO_ReadOnly))
157 kdDebug() <<
"/etc/lpd.conf found: probably LPRng system" << endl;
162 line = t.readLine().stripWhiteSpace();
163 if (line.startsWith(
"printcap_path="))
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] ==
'|')
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());
179 kdDebug() <<
"printcap file returned: " << printcap << endl;
184 void KMLpdUnixManager::parseEtcPrintcap()
186 TQFile f(getPrintcapFileName());
187 if (f.exists() && f.open(IO_ReadOnly))
190 TQMap<TQString,TQString> entry;
194 entry = readEntry(t);
195 if (entry.isEmpty() || !entry.contains(
"printer-name") || entry.contains(
"server"))
197 if (entry[
"printer-name"] ==
"all")
199 if (entry.contains(
"all"))
202 int p = entry[
"all"].find(TQRegExp(
"[^a-zA-Z0-9_\\s-]"));
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)
209 KMPrinter *printer = ::createPrinter(*it);
210 printer->setDescription(i18n(
"Description unavailable"));
218 KMPrinter *printer = ::createPrinter(entry);
219 if (entry.contains(
"rm"))
220 printer->setDescription(i18n(
"Remote printer queue on %1").arg(entry[
"rm"]));
222 printer->setDescription(i18n(
"Local printer"));
230 TQString getEtcPrintersConfName()
232 TQString printersconf(
"/etc/printers.conf");
233 if (!TQFile::exists(printersconf) && !TDEStandardDirs::findExe(
"ypcat" ).isEmpty())
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));
245 void KMLpdUnixManager::parseEtcPrintersConf()
247 TQFile f(getEtcPrintersConfName());
248 if (f.exists() && f.open(IO_ReadOnly))
251 TQMap<TQString,TQString> entry;
252 TQString default_printer;
256 entry = readEntry(t);
257 if (entry.isEmpty() || !entry.contains(
"printer-name"))
259 TQString prname = entry[
"printer-name"];
260 if (prname ==
"_default")
262 if (entry.contains(
"use"))
263 default_printer = entry[
"use"];
265 else if (prname !=
"_all")
267 KMPrinter *printer = ::createPrinter(entry);
268 if (entry.contains(
"bsdaddr"))
270 TQStringList l = TQStringList::split(
',',entry[
"bsdaddr"],
false);
271 printer->setDescription(i18n(
"Remote printer queue on %1").arg(l[0]));
274 printer->setDescription(i18n(
"Local printer"));
279 if (!default_printer.isEmpty())
280 setSoftDefault(findPrinter(default_printer));
285 void KMLpdUnixManager::parseEtcLpPrinters()
287 TQDir d(
"/etc/lp/printers");
288 const TQFileInfoList *prlist = d.entryInfoList(TQDir::Dirs);
292 TQFileInfoListIterator it(*prlist);
293 for (;it.current();++it)
295 if (it.current()->fileName() ==
"." || it.current()->fileName() ==
"..")
297 TQFile f(it.current()->absFilePath() +
"/configuration");
298 if (f.exists() && f.open(IO_ReadOnly))
301 TQString line, remote;
305 if (line.isEmpty())
continue;
306 if (line.startsWith(
"Remote:"))
308 TQStringList l = TQStringList::split(
':',line,
false);
309 if (l.count() > 1) remote = l[1];
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));
320 printer->setDescription(i18n(
"Local printer"));
327 void KMLpdUnixManager::parseEtcLpMember()
329 TQDir d(
"/etc/lp/member");
330 const TQFileInfoList *prlist = d.entryInfoList(TQDir::Files);
334 TQFileInfoListIterator it(*prlist);
335 for (;it.current();++it)
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"));
348 void KMLpdUnixManager::parseSpoolInterface()
350 TQDir d(
"/usr/spool/interfaces/lp");
351 const TQFileInfoList *prlist = d.entryInfoList(TQDir::Files);
355 TQFileInfoListIterator it(*prlist);
356 for (;it.current();++it)
358 TQFile f(it.current()->absFilePath());
359 if (f.exists() && f.open(IO_ReadOnly))
362 TQString line, remote;
366 line = t.readLine().stripWhiteSpace();
367 if (line.startsWith(
"HOSTNAME"))
369 TQStringList l = TQStringList::split(
'=',line,
false);
370 if (l.count() > 1) remote = l[1];
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));
382 printer->setDescription(i18n(
"Local printer"));
390 KMLpdUnixManager::KMLpdUnixManager(TQObject *parent,
const char *name,
const TQStringList & )
391 : KMManager(parent,name)
396 void KMLpdUnixManager::listPrinters()
402 parseEtcPrintersConf();
403 parseEtcLpPrinters();
405 parseSpoolInterface();
409 discardAllPrinters(
false);