20 #include "apshandler.h"
22 #include "printcapentry.h"
23 #include "kmprinter.h"
24 #include "lprsettings.h"
25 #include "kmmanager.h"
31 #include <tqtextstream.h>
32 #include <tqvaluestack.h>
33 #include <kstandarddirs.h>
34 #include <tdelocale.h>
37 #include <sys/types.h>
40 ApsHandler::ApsHandler(KMManager *mgr)
41 : LprHandler(
"apsfilter", mgr)
46 bool ApsHandler::validate(PrintcapEntry *entry)
48 return (entry->field(
"if").right(9) ==
"apsfilter");
51 KMPrinter* ApsHandler::createPrinter(PrintcapEntry *entry)
53 entry->comment = TQString::fromLatin1(
"# APS%1_BEGIN:printer%2").arg(m_counter).arg(m_counter);
54 entry->postcomment = TQString::fromLatin1(
"# APS%1_END - don't delete this").arg(m_counter);
56 return LprHandler::createPrinter(entry);
59 bool ApsHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry,
bool shortmode)
61 if (LprHandler::completePrinter(prt, entry, shortmode))
65 TQMap<TQString,TQString> opts = loadResources(entry);
66 if (opts.contains(
"PRINTER"))
68 prt->setDescription(i18n(
"APS Driver (%1)").arg(opts[
"PRINTER"]));
69 prt->setDriverInfo(prt->description());
72 if (prt->device().isEmpty())
75 TQString smbname(sysconfDir() +
"/" + prt->printerName() +
"/smbclient.conf");
76 TQString ncpname(sysconfDir() +
"/" + prt->printerName() +
"/netware.conf");
77 if (TQFile::exists(smbname))
79 TQMap<TQString,TQString> opts = loadVarFile(smbname);
80 if (opts.count() == 0)
81 prt->setDevice(
"smb://<unknown>/<unknown>");
84 prt->setDevice(buildSmbURI(
85 opts[
"SMB_WORKGROUP" ],
87 opts[
"SMB_PRINTER" ],
89 opts[
"SMB_PASSWD" ] ) );
93 else if (TQFile::exists(ncpname))
95 TQMap<TQString,TQString> opts = loadVarFile(ncpname);
96 if (opts.count() == 0)
97 prt->setDevice(
"ncp://<unknown>/<unknown>");
100 TQString uri = buildSmbURI(
102 opts[
"NCP_SERVER" ],
103 opts[
"NCP_PRINTER" ],
105 opts[
"NCP_PASSWD" ] );
106 uri.replace( 0, 3,
"ncp" );
111 if (!prt->device().isEmpty())
112 prt->setLocation(i18n(
"Network printer (%1)").arg(prot));
119 TQString ApsHandler::sysconfDir()
121 return TQFile::encodeName(
"/etc/apsfilter");
124 TQString ApsHandler::shareDir()
126 return driverDirectory();
129 TQString ApsHandler::driverDirInternal()
131 return locateDir(
"apsfilter/setup",
"/usr/share:/usr/local/share:/opt/share");
134 TQMap<TQString,TQString> ApsHandler::loadResources(PrintcapEntry *entry)
136 return loadVarFile(sysconfDir() +
"/" + (entry ? entry->name : TQString::null) +
"/apsfilterrc");
139 TQMap<TQString,TQString> ApsHandler::loadVarFile(
const TQString& filename)
141 TQMap<TQString,TQString> opts;
143 if (f.open(IO_ReadOnly))
150 line = t.readLine().stripWhiteSpace();
151 if (line.isEmpty() || line[0] ==
'#' || (p = line.find(
'=')) == -1)
153 TQString variable = line.left(p).stripWhiteSpace();
154 TQString value = line.mid(p+1).stripWhiteSpace();
155 if (!value.isEmpty() && value[0] ==
'\'')
156 value = value.mid(1, value.length()-2);
157 opts[variable] = value;
163 DrMain* ApsHandler::loadDriver(KMPrinter *prt, PrintcapEntry *entry,
bool config)
165 DrMain *driver = loadApsDriver(config);
168 TQMap<TQString,TQString> opts = loadResources(entry);
169 if ( !config && opts.contains(
"PAPERSIZE" ) )
173 opts[
"PageSize" ] = opts[
"PAPERSIZE" ];
178 DrBase *opt = driver->findOption(
"PageSize" );
180 opt->set(
"default", opts[
"PageSize" ] );
182 driver->setOptions(opts);
183 driver->set(
"gsdriver", opts[
"PRINTER"]);
188 DrMain* ApsHandler::loadDbDriver(
const TQString& s)
191 DrMain *driver = loadApsDriver(
true);
193 driver->set(
"gsdriver", s.mid(p+1));
197 DrMain* ApsHandler::loadApsDriver(
bool config)
199 DrMain *driver = loadToolDriver(locate(
"data", (config ?
"tdeprint/apsdriver1" :
"tdeprint/apsdriver2")));
201 driver->set(
"text",
"APS Common Driver");
205 void ApsHandler::reset()
210 PrintcapEntry* ApsHandler::createEntry(KMPrinter *prt)
212 TQString prot = prt->deviceProtocol();
213 if (prot !=
"parallel" && prot !=
"lpd" && prot !=
"smb" && prot !=
"ncp")
215 manager()->setErrorMsg(i18n(
"Unsupported backend: %1.").arg(prot));
218 TQString path = sysconfDir() +
"/" + prt->printerName();
219 if (!TDEStandardDirs::makeDir(path, 0755))
221 manager()->setErrorMsg(i18n(
"Unable to create directory %1.").arg(path));
224 if (prot ==
"smb" || prot ==
"ncp")
227 TQFile::remove(path +
"/smbclient.conf");
228 TQFile::remove(path +
"/netware.conf");
232 f.setName(path +
"/smbclient.conf");
233 if (f.open(IO_WriteOnly))
236 TQString work, server, printer, user, passwd;
237 if ( splitSmbURI( prt->device(), work, server, printer, user, passwd ) )
241 manager()->setErrorMsg(i18n(
"Missing element: %1.").arg(
"Workgroup"));
244 t <<
"SMB_SERVER='" << server <<
"'" << endl;
245 t <<
"SMB_PRINTER='" << printer <<
"'" << endl;
246 t <<
"SMB_IP=''" << endl;
247 t <<
"SMB_WORKGROUP='" << work <<
"'" << endl;
248 t <<
"SMB_BUFFER=1400" << endl;
249 t <<
"SMB_FLAGS='-N'" << endl;
252 t <<
"SMB_USER='" << user <<
"'" << endl;
253 t <<
"SMB_PASSWD='" << passwd <<
"'" << endl;
258 manager()->setErrorMsg( i18n(
"Invalid printer backend specification: %1" ).arg( prt->device() ) );
264 manager()->setErrorMsg(i18n(
"Unable to create the file %1.").arg(f.name()));
270 f.setName(path +
"/netware.conf");
271 if (f.open(IO_WriteOnly))
273 TQString work, server, printer, user, passwd;
274 TQString uri = prt->device();
275 uri.replace( 0, 3,
"smb" );
276 if ( splitSmbURI( uri, work, server, printer, user, passwd ) )
279 t <<
"NCP_SERVER='" << server <<
"'" << endl;
280 t <<
"NCP_PRINTER='" << printer <<
"'" << endl;
283 t <<
"NCP_USER='" << user <<
"'" << endl;
284 t <<
"NCP_PASSWD='" << passwd <<
"'" << endl;
289 manager()->setErrorMsg( i18n(
"Invalid printer backend specification: %1" ).arg( prt->device() ) );
295 manager()->setErrorMsg(i18n(
"Unable to create the file %1.").arg(f.name()));
300 ::chmod(TQFile::encodeName(f.name()).data(), S_IRUSR|S_IWUSR);
302 PrintcapEntry *entry = LprHandler::createEntry(prt);
305 entry =
new PrintcapEntry;
306 entry->addField(
"lp", Field::String,
"/dev/null");
308 TQString sd = LprSettings::self()->baseSpoolDir() +
"/" + prt->printerName();
309 entry->addField(
"af", Field::String, sd +
"/acct");
310 entry->addField(
"lf", Field::String, sd +
"/log");
311 entry->addField(
"if", Field::String, sysconfDir() +
"/basedir/bin/apsfilter");
312 entry->comment = TQString::fromLatin1(
"# APS%1_BEGIN:printer%2").arg(m_counter).arg(m_counter);
313 entry->postcomment = TQString::fromLatin1(
"# APS%1_END").arg(m_counter);
318 bool ApsHandler::savePrinterDriver(KMPrinter *prt, PrintcapEntry *entry, DrMain *driver,
bool*)
320 if (driver->get(
"gsdriver").isEmpty())
322 manager()->setErrorMsg(i18n(
"The APS driver is not defined."));
325 TQFile f(sysconfDir() +
"/" + prt->printerName() +
"/apsfilterrc");
326 if (f.open(IO_WriteOnly))
329 t <<
"# File generated by TDEPrint" << endl;
330 t <<
"PRINTER='" << driver->get(
"gsdriver") <<
"'" << endl;
331 TQValueStack<DrGroup*> stack;
333 while (stack.count() > 0)
335 DrGroup *grp = stack.pop();
336 TQPtrListIterator<DrGroup> git(grp->groups());
337 for (; git.current(); ++git)
338 stack.push(git.current());
339 TQPtrListIterator<DrBase> oit(grp->options());
341 for (; oit.current(); ++oit)
343 value = oit.current()->valueText();
344 switch (oit.current()->type())
346 case DrBase::Boolean:
348 t << oit.current()->name() <<
"='" << value <<
"'" << endl;
351 if (value !=
"(empty)")
352 t << oit.current()->name() <<
"='" << value <<
"'" << endl;
355 if (!value.isEmpty())
356 t << oit.current()->name() <<
"='" << value <<
"'" << endl;
367 manager()->setErrorMsg(i18n(
"Unable to create the file %1.").arg(f.name()));
372 bool ApsHandler::removePrinter(KMPrinter*, PrintcapEntry *entry)
374 TQString path(sysconfDir() +
"/" + entry->name);
375 TQFile::remove(path +
"/smbclient.conf");
376 TQFile::remove(path +
"/netware.conf");
377 TQFile::remove(path +
"/apsfilterrc");
378 if (!TQDir(path).rmdir(path))
380 manager()->setErrorMsg(i18n(
"Unable to remove directory %1.").arg(path));
386 TQString ApsHandler::printOptions(
KPrinter *printer)
389 TQMap<TQString,TQString> opts = printer->
options();
390 for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
392 if (it.key().startsWith(
"kde-") || it.key().startsWith(
"_kde-") || it.key().startsWith(
"app-" ))
394 optstr.append((*it)).append(
":");
396 if (!optstr.isEmpty())
398 optstr = optstr.left(optstr.length()-1);
399 if (LprSettings::self()->mode() == LprSettings::LPR)
400 optstr.prepend(
"-C '").append(
"'");
402 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.