20 #include "kmlprmanager.h"
21 #include "printcapreader.h"
22 #include "printcapentry.h"
23 #include "lpchelper.h"
24 #include "matichandler.h"
25 #include "apshandler.h"
26 #include "lprngtoolhandler.h"
27 #include "lprsettings.h"
29 #include "editentrydialog.h"
31 #include <tqfileinfo.h>
32 #include <tqptrlist.h>
33 #include <tdelocale.h>
34 #include <kstandarddirs.h>
37 #include <tdeprocess.h>
38 #include <tdeaction.h>
39 #include <tdemessagebox.h>
40 #include <klibloader.h>
45 KMLprManager::KMLprManager(TQObject *parent,
const char *name,
const TQStringList & )
46 : KMManager(parent,name)
48 m_handlers.setAutoDelete(
true);
49 m_handlerlist.setAutoDelete(
false);
50 m_entries.setAutoDelete(
true);
52 m_lpchelper =
new LpcHelper(
this);
55 setHasManagement(getuid() == 0);
56 setPrinterOperationMask(
57 KMManager::PrinterEnabling |
58 KMManager::PrinterConfigure |
59 KMManager::PrinterTesting |
60 KMManager::PrinterCreation |
61 KMManager::PrinterRemoval |
62 KMManager::PrinterTesting
68 void KMLprManager::listPrinters()
70 TQFileInfo fi(LprSettings::self()->printcapFile());
73 m_lpchelper->updateStates();
76 if (!m_updtime.isValid() || m_updtime < fi.lastModified())
81 TQPtrListIterator<LprHandler> hit(m_handlerlist);
82 for (; hit.current(); ++hit)
83 hit.current()->reset();
86 PrintcapReader reader;
87 TQFile f(fi.absFilePath());
89 if (f.exists() && f.open(IO_ReadOnly))
91 reader.setPrintcapFile(&f);
92 while ((entry = reader.nextEntry()) != NULL)
94 TQPtrListIterator<LprHandler> it(m_handlerlist);
95 for (; it.current(); ++it)
96 if (it.current()->validate(entry))
98 KMPrinter *prt = it.current()->createPrinter(entry);
99 checkPrinterState(prt);
100 prt->setOption(
"kde-lpr-handler", it.current()->name());
104 m_entries.insert(entry->name, entry);
109 m_updtime = fi.lastModified();
113 TQPtrListIterator<KMPrinter> it(m_printers);
114 for (; it.current(); ++it)
115 if (!it.current()->isSpecial())
117 it.current()->setDiscarded(
false);
118 checkPrinterState(it.current());
123 void KMLprManager::insertHandler(LprHandler *handler)
125 m_handlers.insert(handler->name(), handler);
126 m_handlerlist.append(handler);
127 kdDebug() <<
"Handler: " << handler->name() << endl;
130 void KMLprManager::initHandlers()
133 m_handlerlist.clear();
135 insertHandler(
new MaticHandler(
this));
136 insertHandler(
new ApsHandler(
this));
137 insertHandler(
new LPRngToolHandler(
this));
140 TQStringList l = TDEGlobal::dirs()->findAllResources(
"data",
"tdeprint/lpr/*.la");
141 for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
143 KLibrary *library = KLibLoader::self()->library(TQFile::encodeName(*it));
146 kdDebug() <<
"loading external handler from " << *it << endl;
147 LprHandler*(*func)(KMManager*) = (LprHandler*(*)(KMManager*))(library->symbol(
"create_handler"));
149 insertHandler(func(
this));
151 kdDebug() <<
"couldn't find the symbol 'create_handler'" << endl;
156 insertHandler(
new LprHandler(
"default",
this));
159 LprHandler* KMLprManager::findHandler(KMPrinter *prt)
161 TQString handlerstr(prt->option(
"kde-lpr-handler"));
162 LprHandler *handler(0);
163 if (handlerstr.isEmpty() || (handler = m_handlers.find(handlerstr)) == NULL)
170 PrintcapEntry* KMLprManager::findEntry(KMPrinter *prt)
172 PrintcapEntry *entry = m_entries.find(prt->printerName());
180 bool KMLprManager::completePrinter(KMPrinter *prt)
182 LprHandler *handler = findHandler(prt);
183 PrintcapEntry *entry = findEntry(prt);
184 if (handler && entry)
185 return handler->completePrinter(prt, entry,
false);
189 bool KMLprManager::completePrinterShort(KMPrinter *prt)
191 LprHandler *handler = findHandler(prt);
192 PrintcapEntry *entry = findEntry(prt);
193 if (!handler || !entry)
196 return handler->completePrinter(prt, entry,
true);
199 void KMLprManager::checkPrinterState(KMPrinter *prt)
203 KMPrinter::PrinterState st = m_lpchelper->state(prt);
205 prt->setAcceptJobs(!(st & KMPrinter::Rejecting));
209 prt->setState(KMPrinter::Idle);
210 prt->setAcceptJobs(
true);
214 DrMain* KMLprManager::loadPrinterDriver(KMPrinter *prt,
bool config)
219 LprHandler *handler = findHandler(prt);
220 PrintcapEntry *entry = findEntry(prt);
221 if (handler && entry)
223 DrMain *driver = handler->loadDriver(prt, entry, config);
225 driver->set(
"handler", handler->name());
231 DrMain* KMLprManager::loadFileDriver(
const TQString& filename)
233 int p = filename.find(
'/');
234 TQString handler_str = (p != -1 ? filename.left(p) : TQString::fromLatin1(
"default"));
235 LprHandler *handler = m_handlers.find(handler_str);
238 DrMain *driver = handler->loadDbDriver(filename);
240 driver->set(
"handler", handler->name());
246 bool KMLprManager::enablePrinter(KMPrinter *prt,
bool state)
249 if (!m_lpchelper->enable(prt, state, msg))
257 bool KMLprManager::startPrinter(KMPrinter *prt,
bool state)
260 if (!m_lpchelper->start(prt, state, msg))
268 bool KMLprManager::savePrinterDriver(KMPrinter *prt, DrMain *driver)
270 LprHandler *handler = findHandler(prt);
271 PrintcapEntry *entry = findEntry(prt);
272 if (handler && entry)
274 bool mustSave(
false);
275 if (handler->savePrinterDriver(prt, entry, driver, &mustSave))
278 return savePrintcapFile();
285 bool KMLprManager::savePrintcapFile()
287 if (!LprSettings::self()->isLocalPrintcap())
289 setErrorMsg(i18n(
"The printcap file is a remote file (NIS). It cannot be written."));
292 TQFile f(LprSettings::self()->printcapFile());
293 if (f.open(IO_WriteOnly))
296 TQDictIterator<PrintcapEntry> it(m_entries);
297 for (; it.current(); ++it)
299 it.current()->writeEntry(t);
305 setErrorMsg(i18n(
"Unable to save printcap file. Check that "
306 "you have write permissions for that file."));
311 bool KMLprManager::createPrinter(KMPrinter *prt)
314 PrintcapEntry *oldEntry = m_entries.find(prt->printerName());
317 LprHandler *handler(0);
323 handler = m_handlers.find(prt->driver()->get(
"handler"));
325 handler = findHandler(prt);
327 handler = m_handlers.find(
"default");
330 setErrorMsg(i18n(
"Internal error: no handler defined."));
333 prt->setOption(
"kde-lpr-handler", handler->name());
338 if (!prt->driver() && oldEntry)
339 prt->setDriver(handler->loadDriver(prt, oldEntry,
true));
341 TQString sd = LprSettings::self()->baseSpoolDir();
344 setErrorMsg(i18n(
"Couldn't determine spool directory. See options dialog."));
347 sd.append(
"/").append(prt->printerName());
348 if (!TDEStandardDirs::makeDir(sd, 0755))
350 setErrorMsg(i18n(
"Unable to create the spool directory %1. Check that you "
351 "have the required permissions for that operation.").arg(sd));
354 PrintcapEntry *entry = handler->createEntry(prt);
358 m_entries.remove(prt->printerName());
359 entry->name = prt->printerName();
360 entry->addField(
"sh", Field::Boolean);
361 entry->addField(
"mx", Field::Integer,
"0");
362 entry->addField(
"sd", Field::String, sd);
363 if (!prt->option(
"kde-aliases").isEmpty())
364 entry->aliases += TQStringList::split(
"|", prt->option(
"kde-aliases"),
false);
367 m_entries.insert(prt->printerName(), entry);
368 bool result = savePrintcapFile();
373 result = handler->savePrinterDriver(prt, entry, prt->driver());
377 if (LprSettings::self()->mode() == LprSettings::LPRng)
380 if (!m_lpchelper->restart(msg))
382 setErrorMsg(i18n(
"The printer has been created but the print daemon "
383 "could not be restarted. %1").arg(msg));
391 bool KMLprManager::removePrinter(KMPrinter *prt)
393 LprHandler *handler = findHandler(prt);
394 PrintcapEntry *entry = findEntry(prt);
395 if (handler && entry)
397 if (handler->removePrinter(prt, entry))
399 TQString sd = entry->field(
"sd");
402 m_entries.take(prt->printerName());
403 bool status = savePrintcapFile();
408 status = (::system(TQFile::encodeName(
"rm -rf " + TDEProcess::quote(sd))) == 0);
410 setErrorMsg(i18n(
"Unable to remove spool directory %1. "
411 "Check that you have write permissions "
412 "for that directory.").arg(sd));
417 m_entries.insert(prt->printerName(), entry);
423 TQString KMLprManager::driverDbCreationProgram()
425 return TQString::fromLatin1(
"make_driver_db_lpr");
428 TQString KMLprManager::driverDirectory()
430 TQPtrListIterator<LprHandler> it(m_handlerlist);
432 for (; it.current(); ++it)
434 TQString dir = it.current()->driverDirectory();
436 dbDirs.append(dir).append(
":");
438 if (!dbDirs.isEmpty())
439 dbDirs.truncate(dbDirs.length()-1);
443 TQString KMLprManager::printOptions(
KPrinter *prt)
449 LprHandler *handler = findHandler(mprt);
451 return handler->printOptions(prt);
453 return TQString::null;
456 void KMLprManager::createPluginActions(TDEActionCollection *coll)
458 TDEAction *act =
new TDEAction(i18n(
"&Edit printcap Entry..."),
"tdeprint_report", 0,
this, TQ_SLOT(slotEditPrintcap()), coll,
"plugin_editprintcap");
459 act->setGroup(
"plugin");
462 void KMLprManager::validatePluginActions(TDEActionCollection *coll, KMPrinter *prt)
464 m_currentprinter = prt;
466 coll->action(
"plugin_editprintcap")->setEnabled(0 && hasManagement() && prt && !prt->isSpecial());
469 void KMLprManager::slotEditPrintcap()
471 if (!m_currentprinter ||
472 KMessageBox::warningContinueCancel(NULL,
473 i18n(
"Editing a printcap entry manually should only be "
474 "done by confirmed system administrator. This may "
475 "prevent your printer from working. Do you want to "
476 "continue?"), TQString::null, KStdGuiItem::cont(),
477 "editPrintcap") == KMessageBox::Cancel)
480 PrintcapEntry *entry = findEntry(m_currentprinter);
481 EditEntryDialog dlg(entry, NULL);
487 TQString KMLprManager::stateInformation()
489 return i18n(
"Spooler type: %1").arg(LprSettings::self()->mode() == LprSettings::LPR ?
"LPR (BSD compatible)" :
"LPRng");
492 #include "kmlprmanager.moc"
This class is the main interface to access the TDE print framework.
TQString printerName() const
See TQPrinter::printerName().