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

tdeprint

  • tdeprint
  • lpr
apshandler.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 "apshandler.h"
21 #include "driver.h"
22 #include "printcapentry.h"
23 #include "kmprinter.h"
24 #include "lprsettings.h"
25 #include "kmmanager.h"
26 #include "util.h"
27 #include "kprinter.h"
28 
29 #include <tqfile.h>
30 #include <tqdir.h>
31 #include <tqtextstream.h>
32 #include <tqvaluestack.h>
33 #include <kstandarddirs.h>
34 #include <tdelocale.h>
35 #include <kdebug.h>
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 
40 ApsHandler::ApsHandler(KMManager *mgr)
41 : LprHandler("apsfilter", mgr)
42 {
43  m_counter = 1;
44 }
45 
46 bool ApsHandler::validate(PrintcapEntry *entry)
47 {
48  return (entry->field("if").right(9) == "apsfilter");
49 }
50 
51 KMPrinter* ApsHandler::createPrinter(PrintcapEntry *entry)
52 {
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);
55  m_counter++;
56  return LprHandler::createPrinter(entry);
57 }
58 
59 bool ApsHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool shortmode)
60 {
61  if (LprHandler::completePrinter(prt, entry, shortmode))
62  {
63  if (!shortmode)
64  {
65  TQMap<TQString,TQString> opts = loadResources(entry);
66  if (opts.contains("PRINTER"))
67  {
68  prt->setDescription(i18n("APS Driver (%1)").arg(opts["PRINTER"]));
69  prt->setDriverInfo(prt->description());
70  }
71  }
72  if (prt->device().isEmpty())
73  {
74  TQString prot;
75  TQString smbname(sysconfDir() + "/" + prt->printerName() + "/smbclient.conf");
76  TQString ncpname(sysconfDir() + "/" + prt->printerName() + "/netware.conf");
77  if (TQFile::exists(smbname))
78  {
79  TQMap<TQString,TQString> opts = loadVarFile(smbname);
80  if (opts.count() == 0)
81  prt->setDevice("smb://<unknown>/<unknown>");
82  else
83  {
84  prt->setDevice(buildSmbURI(
85  opts[ "SMB_WORKGROUP" ],
86  opts[ "SMB_SERVER" ],
87  opts[ "SMB_PRINTER" ],
88  opts[ "SMB_USER" ],
89  opts[ "SMB_PASSWD" ] ) );
90  }
91  prot = "smb";
92  }
93  else if (TQFile::exists(ncpname))
94  {
95  TQMap<TQString,TQString> opts = loadVarFile(ncpname);
96  if (opts.count() == 0)
97  prt->setDevice("ncp://<unknown>/<unknown>");
98  else
99  {
100  TQString uri = buildSmbURI(
101  TQString::null,
102  opts[ "NCP_SERVER" ],
103  opts[ "NCP_PRINTER" ],
104  opts[ "NCP_USER" ],
105  opts[ "NCP_PASSWD" ] );
106  uri.replace( 0, 3, "ncp" );
107  prt->setDevice(uri);
108  }
109  prot = "ncp";
110  }
111  if (!prt->device().isEmpty())
112  prt->setLocation(i18n("Network printer (%1)").arg(prot));
113  }
114  return true;
115  }
116  return false;
117 }
118 
119 TQString ApsHandler::sysconfDir()
120 {
121  return TQFile::encodeName("/etc/apsfilter");
122 }
123 
124 TQString ApsHandler::shareDir()
125 {
126  return driverDirectory();
127 }
128 
129 TQString ApsHandler::driverDirInternal()
130 {
131  return locateDir("apsfilter/setup", "/usr/share:/usr/local/share:/opt/share");
132 }
133 
134 TQMap<TQString,TQString> ApsHandler::loadResources(PrintcapEntry *entry)
135 {
136  return loadVarFile(sysconfDir() + "/" + (entry ? entry->name : TQString::null) + "/apsfilterrc");
137 }
138 
139 TQMap<TQString,TQString> ApsHandler::loadVarFile(const TQString& filename)
140 {
141  TQMap<TQString,TQString> opts;
142  TQFile f(filename);
143  if (f.open(IO_ReadOnly))
144  {
145  TQTextStream t(&f);
146  TQString line;
147  int p(-1);
148  while (!t.atEnd())
149  {
150  line = t.readLine().stripWhiteSpace();
151  if (line.isEmpty() || line[0] == '#' || (p = line.find('=')) == -1)
152  continue;
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;
158  }
159  }
160  return opts;
161 }
162 
163 DrMain* ApsHandler::loadDriver(KMPrinter *prt, PrintcapEntry *entry, bool config)
164 {
165  DrMain *driver = loadApsDriver(config);
166  if (driver /* && config */ ) // Load resources in all case, to get the correct page size
167  {
168  TQMap<TQString,TQString> opts = loadResources(entry);
169  if ( !config && opts.contains( "PAPERSIZE" ) )
170  {
171  // this is needed to keep applications informed
172  // about the current selected page size
173  opts[ "PageSize" ] = opts[ "PAPERSIZE" ];
174 
175  // default page size needs to be set to the actual
176  // value of the printer driver, otherwise it's blocked
177  // to A4
178  DrBase *opt = driver->findOption( "PageSize" );
179  if ( opt )
180  opt->set( "default", opts[ "PageSize" ] );
181  }
182  driver->setOptions(opts);
183  driver->set("gsdriver", opts["PRINTER"]);
184  }
185  return driver;
186 }
187 
188 DrMain* ApsHandler::loadDbDriver(const TQString& s)
189 {
190  int p = s.find('/');
191  DrMain *driver = loadApsDriver(true);
192  if (driver)
193  driver->set("gsdriver", s.mid(p+1));
194  return driver;
195 }
196 
197 DrMain* ApsHandler::loadApsDriver(bool config)
198 {
199  DrMain *driver = loadToolDriver(locate("data", (config ? "tdeprint/apsdriver1" : "tdeprint/apsdriver2")));
200  if (driver)
201  driver->set("text", "APS Common Driver");
202  return driver;
203 }
204 
205 void ApsHandler::reset()
206 {
207  m_counter = 1;
208 }
209 
210 PrintcapEntry* ApsHandler::createEntry(KMPrinter *prt)
211 {
212  TQString prot = prt->deviceProtocol();
213  if (prot != "parallel" && prot != "lpd" && prot != "smb" && prot != "ncp")
214  {
215  manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot));
216  return NULL;
217  }
218  TQString path = sysconfDir() + "/" + prt->printerName();
219  if (!TDEStandardDirs::makeDir(path, 0755))
220  {
221  manager()->setErrorMsg(i18n("Unable to create directory %1.").arg(path));
222  return NULL;
223  }
224  if (prot == "smb" || prot == "ncp")
225  {
226  // either "smb" or "ncp"
227  TQFile::remove(path + "/smbclient.conf");
228  TQFile::remove(path + "/netware.conf");
229  TQFile f;
230  if (prot == "smb")
231  {
232  f.setName(path + "/smbclient.conf");
233  if (f.open(IO_WriteOnly))
234  {
235  TQTextStream t(&f);
236  TQString work, server, printer, user, passwd;
237  if ( splitSmbURI( prt->device(), work, server, printer, user, passwd ) )
238  {
239  if (work.isEmpty())
240  {
241  manager()->setErrorMsg(i18n("Missing element: %1.").arg("Workgroup"));
242  return NULL;
243  }
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;
250  if (!user.isEmpty())
251  {
252  t << "SMB_USER='" << user << "'" << endl;
253  t << "SMB_PASSWD='" << passwd << "'" << endl;
254  }
255  }
256  else
257  {
258  manager()->setErrorMsg( i18n( "Invalid printer backend specification: %1" ).arg( prt->device() ) );
259  return NULL;
260  }
261  }
262  else
263  {
264  manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name()));
265  return NULL;
266  }
267  }
268  else
269  {
270  f.setName(path + "/netware.conf");
271  if (f.open(IO_WriteOnly))
272  {
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 ) )
277  {
278  TQTextStream t(&f);
279  t << "NCP_SERVER='" << server << "'" << endl;
280  t << "NCP_PRINTER='" << printer << "'" << endl;
281  if (!user.isEmpty())
282  {
283  t << "NCP_USER='" << user << "'" << endl;
284  t << "NCP_PASSWD='" << passwd << "'" << endl;
285  }
286  }
287  else
288  {
289  manager()->setErrorMsg( i18n( "Invalid printer backend specification: %1" ).arg( prt->device() ) );
290  return NULL;
291  }
292  }
293  else
294  {
295  manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name()));
296  return NULL;
297  }
298  }
299  // change file permissions
300  ::chmod(TQFile::encodeName(f.name()).data(), S_IRUSR|S_IWUSR);
301  }
302  PrintcapEntry *entry = LprHandler::createEntry(prt);
303  if (!entry)
304  {
305  entry = new PrintcapEntry;
306  entry->addField("lp", Field::String, "/dev/null");
307  }
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);
314  m_counter++;
315  return entry;
316 }
317 
318 bool ApsHandler::savePrinterDriver(KMPrinter *prt, PrintcapEntry *entry, DrMain *driver, bool*)
319 {
320  if (driver->get("gsdriver").isEmpty())
321  {
322  manager()->setErrorMsg(i18n("The APS driver is not defined."));
323  return false;
324  }
325  TQFile f(sysconfDir() + "/" + prt->printerName() + "/apsfilterrc");
326  if (f.open(IO_WriteOnly))
327  {
328  TQTextStream t(&f);
329  t << "# File generated by TDEPrint" << endl;
330  t << "PRINTER='" << driver->get("gsdriver") << "'" << endl;
331  TQValueStack<DrGroup*> stack;
332  stack.push(driver);
333  while (stack.count() > 0)
334  {
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());
340  TQString value;
341  for (; oit.current(); ++oit)
342  {
343  value = oit.current()->valueText();
344  switch (oit.current()->type())
345  {
346  case DrBase::Boolean:
347  if (value == "true")
348  t << oit.current()->name() << "='" << value << "'" << endl;
349  break;
350  case DrBase::List:
351  if (value != "(empty)")
352  t << oit.current()->name() << "='" << value << "'" << endl;
353  break;
354  case DrBase::String:
355  if (!value.isEmpty())
356  t << oit.current()->name() << "='" << value << "'" << endl;
357  break;
358  default:
359  break;
360  }
361  }
362  }
363  return true;
364  }
365  else
366  {
367  manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name()));
368  return false;
369  }
370 }
371 
372 bool ApsHandler::removePrinter(KMPrinter*, PrintcapEntry *entry)
373 {
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))
379  {
380  manager()->setErrorMsg(i18n("Unable to remove directory %1.").arg(path));
381  return false;
382  }
383  return true;
384 }
385 
386 TQString ApsHandler::printOptions(KPrinter *printer)
387 {
388  TQString optstr;
389  TQMap<TQString,TQString> opts = printer->options();
390  for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
391  {
392  if (it.key().startsWith("kde-") || it.key().startsWith("_kde-") || it.key().startsWith( "app-" ))
393  continue;
394  optstr.append((*it)).append(":");
395  }
396  if (!optstr.isEmpty())
397  {
398  optstr = optstr.left(optstr.length()-1);
399  if (LprSettings::self()->mode() == LprSettings::LPR)
400  optstr.prepend("-C '").append("'");
401  else
402  optstr.prepend("-Z '").append("'");
403  }
404  return optstr;
405 }
KPrinter::options
const TQMap< TQString, TQString > & options() const
Returns the complete set of print options from the KPrinter object.
Definition: kprinter.cpp:897
KPrinter
This class is the main interface to access the TDE print framework.
Definition: kprinter.h:88

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.