20 #include "lprngtoolhandler.h"
21 #include "printcapentry.h"
22 #include "kmprinter.h"
24 #include "lprsettings.h"
26 #include "kmmanager.h"
30 #include <tqtextstream.h>
31 #include <tdelocale.h>
33 #include <kstandarddirs.h>
35 LPRngToolHandler::LPRngToolHandler(KMManager *mgr)
36 : LprHandler(
"lprngtool", mgr)
40 bool LPRngToolHandler::validate(PrintcapEntry *entry)
42 if (entry->comment.startsWith(
"##LPRNGTOOL##") &&
43 entry->comment.find(
"UNKNOWN") == -1)
48 bool LPRngToolHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry,
bool shortmode)
53 TQStringList l = TQStringList::split(
' ', entry->comment,
false);
54 lp = entry->field(
"lp");
58 if (l[1] ==
"DEVICE" || l[1] ==
"SOCKET" || l[1] ==
"QUEUE")
59 LprHandler::completePrinter(prt, entry, shortmode);
60 else if (l[1] ==
"SMB")
62 TQMap<TQString,TQString> opts = parseXferOptions(entry->field(
"xfer_options"));
64 loadAuthFile(LprSettings::self()->baseSpoolDir() +
"/" + entry->name +
"/" + opts[
"authfile"], user, pass);
65 TQString uri = buildSmbURI(
71 prt->setDevice( uri );
72 prt->setLocation(i18n(
"Network printer (%1)").arg(
"smb"));
76 if (!(str=entry->field(
"cm")).isEmpty())
77 prt->setDescription(str);
82 if (!(str=entry->field(
"ifhp")).isEmpty())
85 int p = str.find(
"model");
92 int q = str.find(
',', p);
96 model = str.mid(p, q-p);
99 prt->setDriverInfo(i18n(
"IFHP Driver (%1)").arg((model.isEmpty() ? i18n(
"unknown") : model)));
100 prt->setOption(
"driverID", model);
106 TQMap<TQString,TQString> LPRngToolHandler::parseXferOptions(
const TQString& str)
109 TQMap<TQString,TQString> opts;
112 while (p < str.length())
114 key = val = TQString::null;
116 while (p < str.length() && str[p].isSpace())
119 while (q < str.length() && str[q] !=
'=')
121 key = str.mid(p, q-p);
123 while (p < str.length() && str[p] !=
'"')
125 val = str.mid(q+2, p-q-2);
133 void LPRngToolHandler::loadAuthFile(
const TQString& filename, TQString& user, TQString& pass)
136 if (f.open(IO_ReadOnly))
142 line = t.readLine().stripWhiteSpace();
145 int p = line.find(
'=');
148 TQString key = line.left(p);
149 if (key ==
"username")
150 user = line.mid(p+1);
151 else if (key ==
"password")
152 pass = line.mid(p+1);
158 DrMain* LPRngToolHandler::loadDriver(KMPrinter *prt, PrintcapEntry *entry,
bool config)
160 if (entry->field(
"lprngtooloptions").isEmpty())
162 manager()->setErrorMsg(i18n(
"No driver defined for that printer. It might be a raw printer."));
166 DrMain* driver = loadToolDriver(locate(
"data",
"tdeprint/lprngtooldriver1"));
169 TQString model = prt->option(
"driverID");
170 driver->set(
"text", i18n(
"LPRngTool Common Driver (%1)").arg((model.isEmpty() ? i18n(
"unknown") : model)));
171 if (!model.isEmpty())
172 driver->set(
"driverID", model);
173 TQMap<TQString,TQString> opts = parseZOptions(entry->field(
"prefix_z"));
174 opts[
"lpr"] = entry->field(
"lpr");
175 driver->setOptions(opts);
178 driver->removeOptionGlobally(
"lpr");
183 DrMain* LPRngToolHandler::loadDbDriver(
const TQString& s)
186 DrMain* driver = loadToolDriver(locate(
"data",
"tdeprint/lprngtooldriver1"));
188 driver->set(
"driverID", s.mid(p+1));
192 TQValueList< TQPair<TQString,TQStringList> > LPRngToolHandler::loadChoiceDict(
const TQString& filename)
195 TQValueList< TQPair<TQString,TQStringList> > dict;
196 if (f.open(IO_ReadOnly))
203 line = t.readLine().stripWhiteSpace();
204 if (line.startsWith(
"OPTION"))
206 if (l.count() > 0 && !key.isEmpty())
207 dict << TQPair<TQString,TQStringList>(key, l);
209 key = TQString::null;
210 if (line.contains(
'|') == 2 || line.right(7) ==
"BOOLEAN")
212 int p = line.find(
'|', 7);
213 key = line.mid(7, p-7);
216 else if (line.startsWith(
"CHOICE"))
218 int p = line.find(
'|', 7);
219 l << line.mid(7, p-7);
226 TQMap<TQString,TQString> LPRngToolHandler::parseZOptions(
const TQString& optstr)
228 TQMap<TQString,TQString> opts;
229 TQStringList l = TQStringList::split(
',', optstr,
false);
233 if (m_dict.count() == 0)
234 m_dict = loadChoiceDict(locate(
"data",
"tdeprint/lprngtooldriver1"));
237 for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
240 for (TQValueList< TQPair<TQString,TQStringList> >::ConstIterator p=m_dict.begin(); p!=m_dict.end() && !found; ++p)
242 if ((*p).second.find(*it) != (*p).second.end())
244 opts[(*p).first] = (*it);
250 unknown.append(*it).append(
',');
253 if (!unknown.isEmpty())
255 unknown.truncate(unknown.length()-1);
256 opts[
"filter"] = unknown;
261 TQString LPRngToolHandler::filterDir()
263 return driverDirectory();
266 TQString LPRngToolHandler::driverDirInternal()
268 return locateDir(
"filters",
"/usr/lib:/usr/local/lib:/opt/lib:/usr/libexec:/usr/local/libexec:/opt/libexec");
271 PrintcapEntry* LPRngToolHandler::createEntry(KMPrinter *prt)
273 TQString prot = prt->deviceProtocol();
274 if (prot !=
"parallel" && prot !=
"lpd" && prot !=
"smb" && prot !=
"socket")
276 manager()->setErrorMsg(i18n(
"Unsupported backend: %1.").arg(prot));
279 PrintcapEntry *entry =
new PrintcapEntry;
280 entry->addField(
"cm", Field::String, prt->description());
281 TQString lp, comment(
"##LPRNGTOOL## ");
282 if (prot ==
"parallel")
284 comment.append(
"DEVICE ");
285 lp = prt->device().mid( 9 );
286 entry->addField(
"rw@", Field::Boolean);
288 else if (prot ==
"socket")
290 comment.append(
"SOCKET ");
291 KURL url( prt->device() );
296 lp.append(
"%").append(TQString::number(url.port()));
298 else if (prot ==
"lpd")
300 comment.append(
"QUEUE ");
301 KURL url( prt->device() );
302 lp = url.path().mid(1) +
"@" + url.host();
304 else if (prot ==
"smb")
306 comment.append(
"SMB ");
307 lp =
"| " + filterDir() +
"/smbprint";
308 TQString work, server, printer, user, passwd;
309 if ( splitSmbURI( prt->device(), work, server, printer, user, passwd ) )
311 entry->addField(
"xfer_options", Field::String, TQString::fromLatin1(
"authfile=\"auth\" crlf=\"0\" hostip=\"\" host=\"%1\" printer=\"%2\" remote_mode=\"SMB\" share=\"//%3/%4\" workgroup=\"%5\"").arg(server).arg(printer).arg(server).arg(printer).arg(work));
312 TQFile authfile(LprSettings::self()->baseSpoolDir() +
"/" + prt->printerName() +
"/auth");
313 if (authfile.open(IO_WriteOnly))
315 TQTextStream t(&authfile);
316 t <<
"username=" << user << endl;
317 t <<
"password=" << passwd << endl;
323 manager()->setErrorMsg( i18n(
"Invalid printer backend specification: %1" ).arg( prt->device() ) );
331 DrMain *driver = prt->driver();
332 comment.append(
"filtertype=IFHP ifhp_options=status@,sync@,pagecount@,waitend@ printerdb_entry=");
333 comment.append(driver->get(
"driverID"));
334 entry->addField(
"ifhp", Field::String, TQString::fromLatin1(
"model=%1,status@,sync@,pagecount@,waitend@").arg(driver->get(
"driverID")));
335 entry->addField(
"lprngtooloptions", Field::String, TQString::fromLatin1(
"FILTERTYPE=\"IFHP\" IFHP_OPTIONS=\"status@,sync@,pagecount@,waitend@\" PRINTERDB_ENTRY=\"%1\"").arg(driver->get(
"driverID")));
336 TQMap<TQString,TQString> opts;
338 driver->getOptions(opts,
false);
339 for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
340 if (it.key() !=
"lpr")
341 optstr.append(*it).append(
",");
342 if (!optstr.isEmpty())
344 optstr.truncate(optstr.length()-1);
345 entry->addField(
"prefix_z", Field::String, optstr);
347 if (!opts[
"lpr"].isEmpty())
348 entry->addField(
"lpr", Field::String, opts[
"lpr"]);
351 entry->addField(
"lp", Field::String, lp);
352 entry->comment = comment;
357 bool LPRngToolHandler::savePrinterDriver(KMPrinter*, PrintcapEntry *entry, DrMain *driver,
bool *mustSave)
360 TQMap<TQString,TQString> opts;
362 driver->getOptions(opts,
false);
363 for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
364 if (it.key() !=
"lpr")
365 optstr.append(*it).append(
",");
366 if (!optstr.isEmpty())
367 optstr.truncate(optstr.length()-1);
370 entry->addField(
"prefix_z", Field::String, optstr);
371 entry->addField(
"lpr", Field::String, opts[
"lpr"]);
377 TQString LPRngToolHandler::printOptions(
KPrinter *printer)
380 TQMap<TQString,TQString> opts = printer->
options();
381 for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
383 if (it.key().startsWith(
"kde-") || it.key().startsWith(
"_kde-") || it.key() ==
"lpr" || it.key().startsWith(
"app-" ))
385 optstr.append(*it).append(
",");
387 if (!optstr.isEmpty())
389 optstr.truncate(optstr.length()-1);
390 optstr.prepend(
"-Z '").append(
"'");
This class is the main interface to access the TDE print framework.
const TQMap< TQString, TQString > & options() const
Returns the complete set of print options from the KPrinter object.