diff options
Diffstat (limited to 'kdeprint/lpd')
-rw-r--r-- | kdeprint/lpd/Makefile.am | 18 | ||||
-rw-r--r-- | kdeprint/lpd/gschecker.cpp | 61 | ||||
-rw-r--r-- | kdeprint/lpd/gschecker.h | 40 | ||||
-rw-r--r-- | kdeprint/lpd/klpdprinterimpl.cpp | 51 | ||||
-rw-r--r-- | kdeprint/lpd/klpdprinterimpl.h | 37 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpdfactory.cpp | 52 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpdfactory.h | 35 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpdmanager.cpp | 651 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpdmanager.h | 76 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpduimanager.cpp | 60 | ||||
-rw-r--r-- | kdeprint/lpd/kmlpduimanager.h | 35 | ||||
-rw-r--r-- | kdeprint/lpd/lpd.print | 87 | ||||
-rw-r--r-- | kdeprint/lpd/lpdtools.cpp | 417 | ||||
-rw-r--r-- | kdeprint/lpd/lpdtools.h | 76 | ||||
-rw-r--r-- | kdeprint/lpd/make_driver_db_lpd.c | 112 |
15 files changed, 1808 insertions, 0 deletions
diff --git a/kdeprint/lpd/Makefile.am b/kdeprint/lpd/Makefile.am new file mode 100644 index 000000000..41f285c3f --- /dev/null +++ b/kdeprint/lpd/Makefile.am @@ -0,0 +1,18 @@ +INCLUDES= -I$(top_srcdir) -I$(top_srcdir)/kdeprint -I$(top_srcdir)/kdeprint/management $(all_includes) + +kde_module_LTLIBRARIES = kdeprint_lpd.la + +kdeprint_lpd_la_SOURCES = kmlpdfactory.cpp kmlpdmanager.cpp klpdprinterimpl.cpp kmlpduimanager.cpp \ + lpdtools.cpp gschecker.cpp +kdeprint_lpd_la_LDFLAGS = $(all_libraries) -module -avoid-version -no-undefined +kdeprint_lpd_la_LIBADD = $(top_builddir)/kdeprint/management/libkdeprint_management.la +kdeprint_lpd_la_METASOURCES = AUTO + +noinst_HEADERS = kmlpdfactory.h kmlpdmanager.h klpdprinterimpl.h kmlpduimanager.h lpdtools.h gschecker.h + +bin_PROGRAMS = make_driver_db_lpd +make_driver_db_lpd_SOURCES = make_driver_db_lpd.c +make_driver_db_lpd_LDADD = $(top_builddir)/kdecore/libkdefakes.la + +entry_DATA = lpd.print +entrydir = $(kde_datadir)/kdeprint/plugins diff --git a/kdeprint/lpd/gschecker.cpp b/kdeprint/lpd/gschecker.cpp new file mode 100644 index 000000000..a4142abbb --- /dev/null +++ b/kdeprint/lpd/gschecker.cpp @@ -0,0 +1,61 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "gschecker.h" +#include "kpipeprocess.h" + +#include <qfile.h> +#include <qtextstream.h> + +GsChecker::GsChecker(QObject *parent, const char *name) +: QObject(parent,name) +{ +} + +bool GsChecker::checkGsDriver(const QString& name) +{ + if (m_driverlist.count() == 0) + loadDriverList(); + return m_driverlist.contains(name); +} + +void GsChecker::loadDriverList() +{ + KPipeProcess proc; + if (proc.open("gs -h",IO_ReadOnly)) + { + QTextStream t(&proc); + QString buffer, line; + bool ok(false); + while (!t.eof()) + { + line = t.readLine().stripWhiteSpace(); + if (ok) + { + if (line.find(':') != -1) + break; + else + buffer.append(line).append(" "); + } + else if (line.startsWith(QString::fromLatin1("Available devices:"))) + ok = true; + } + m_driverlist = QStringList::split(' ',buffer,false); + } +} diff --git a/kdeprint/lpd/gschecker.h b/kdeprint/lpd/gschecker.h new file mode 100644 index 000000000..d7c040315 --- /dev/null +++ b/kdeprint/lpd/gschecker.h @@ -0,0 +1,40 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef GSCHECKER_H +#define GSCHECKER_H + +#include <qstring.h> +#include <qstringlist.h> +#include <qobject.h> + +class GsChecker : public QObject +{ +public: + GsChecker(QObject *parent = 0, const char *name = 0); + bool checkGsDriver(const QString& name); + +protected: + void loadDriverList(); + +private: + QStringList m_driverlist; +}; + +#endif diff --git a/kdeprint/lpd/klpdprinterimpl.cpp b/kdeprint/lpd/klpdprinterimpl.cpp new file mode 100644 index 000000000..ad8912c47 --- /dev/null +++ b/kdeprint/lpd/klpdprinterimpl.cpp @@ -0,0 +1,51 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "klpdprinterimpl.h" +#include "kprinter.h" + +#include <qfile.h> +#include <kstandarddirs.h> +#include <klocale.h> + +KLpdPrinterImpl::KLpdPrinterImpl(QObject *parent, const char *name) +: KPrinterImpl(parent,name) +{ +} + +KLpdPrinterImpl::~KLpdPrinterImpl() +{ +} + +QString KLpdPrinterImpl::executable() +{ + return KStandardDirs::findExe("lpr"); +} + +bool KLpdPrinterImpl::setupCommand(QString& cmd, KPrinter *printer) +{ + QString exestr = executable(); + if (exestr.isEmpty()) + { + printer->setErrorMessage(i18n("The <b>%1</b> executable could not be found in your path. Check your installation.").arg("lpr")); + return false; + } + cmd = QString::fromLatin1("%1 -P %2 '-#%3'").arg(exestr).arg(quote(printer->printerName())).arg(printer->numCopies()); + return true; +} diff --git a/kdeprint/lpd/klpdprinterimpl.h b/kdeprint/lpd/klpdprinterimpl.h new file mode 100644 index 000000000..ea88c1138 --- /dev/null +++ b/kdeprint/lpd/klpdprinterimpl.h @@ -0,0 +1,37 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef KLPDPRINTERIMPL_H +#define KLPDPRINTERIMPL_H + +#include "kprinterimpl.h" + +class KLpdPrinterImpl : public KPrinterImpl +{ +public: + KLpdPrinterImpl(QObject *parent = 0, const char *name = 0); + ~KLpdPrinterImpl(); + + bool setupCommand(QString&, KPrinter*); + +protected: + QString executable(); +}; + +#endif diff --git a/kdeprint/lpd/kmlpdfactory.cpp b/kdeprint/lpd/kmlpdfactory.cpp new file mode 100644 index 000000000..3285a32a2 --- /dev/null +++ b/kdeprint/lpd/kmlpdfactory.cpp @@ -0,0 +1,52 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "kmlpdfactory.h" +#include "kmlpdmanager.h" +#include "kmlpduimanager.h" +#include "klpdprinterimpl.h" + +extern "C" +{ + void* init_kdeprint_lpd() + { + return new KLpdFactory; + } +}; + +KLpdFactory::KLpdFactory(QObject *parent, const char *name) +: KLibFactory(parent,name) +{ +} + +KLpdFactory::~KLpdFactory() +{ +} + +QObject* KLpdFactory::createObject(QObject *parent, const char *name, const char *classname, const QStringList&) +{ + if (strcmp(classname,"KMManager") == 0) + return new KMLpdManager(parent,name); + else if (strcmp(classname,"KMUiManager") == 0) + return new KMLpdUiManager(parent,name); + else if (strcmp(classname,"KPrinterImpl") == 0) + return new KLpdPrinterImpl(parent,name); + else + return NULL; +} diff --git a/kdeprint/lpd/kmlpdfactory.h b/kdeprint/lpd/kmlpdfactory.h new file mode 100644 index 000000000..2d4dc7efb --- /dev/null +++ b/kdeprint/lpd/kmlpdfactory.h @@ -0,0 +1,35 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef KMLPDFACTORY_H +#define KMLPDFACTORY_H + +#include <klibloader.h> + +class KLpdFactory : public KLibFactory +{ +public: + KLpdFactory(QObject *parent = 0, const char *name = 0); + virtual ~KLpdFactory(); + +protected: + QObject* createObject(QObject *parent = 0, const char *name = 0, const char *className = "QObject", const QStringList& args = QStringList()); +}; + +#endif diff --git a/kdeprint/lpd/kmlpdmanager.cpp b/kdeprint/lpd/kmlpdmanager.cpp new file mode 100644 index 000000000..a19cd3f5f --- /dev/null +++ b/kdeprint/lpd/kmlpdmanager.cpp @@ -0,0 +1,651 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "kmlpdmanager.h" +#include "kmprinter.h" +#include "kmdbentry.h" +#include "driver.h" +#include "kmfactory.h" +#include "lpdtools.h" +#include "gschecker.h" +#include "kpipeprocess.h" + +#include <qfile.h> +#include <qfileinfo.h> +#include <qtextstream.h> +#include <qmap.h> +#include <qregexp.h> + +#include <klocale.h> +#include <kstandarddirs.h> +#include <kconfig.h> +#include <kprocess.h> + +#include <pwd.h> +#include <unistd.h> +#include <stdlib.h> +#include <sys/stat.h> + +// only there to allow testing on my system. Should be removed +// when everything has proven to be working and stable +QString lpdprefix = ""; +QString ptPrinterType(KMPrinter*); + +//************************************************************************************************ + +KMLpdManager::KMLpdManager(QObject *parent, const char *name) +: KMManager(parent,name) +{ + m_entries.setAutoDelete(true); + m_ptentries.setAutoDelete(true); + setHasManagement(getuid() == 0); + setPrinterOperationMask(KMManager::PrinterCreation|KMManager::PrinterConfigure|KMManager::PrinterRemoval|KMManager::PrinterEnabling); + m_gschecker = new GsChecker(this,"GsChecker"); +} + +KMLpdManager::~KMLpdManager() +{ +} + +QString KMLpdManager::driverDbCreationProgram() +{ + return QString::fromLatin1("make_driver_db_lpd"); +} + +QString KMLpdManager::driverDirectory() +{ + return QString::fromLatin1("/usr/lib/rhs/rhs-printfilters"); +} + +bool KMLpdManager::completePrinter(KMPrinter *printer) +{ + return completePrinterShort(printer); +} + +bool KMLpdManager::completePrinterShort(KMPrinter *printer) +{ + PrintcapEntry *entry = m_entries.find(printer->name()); + if (entry) + { + QString type(entry->comment(2)), driver(entry->comment(7)), lp(entry->arg("lp")); + printer->setDescription(i18n("Local printer queue (%1)").arg(type.isEmpty() ? i18n("Unknown type of local printer queue", "Unknown") : type)); + printer->setLocation(i18n("<Not available>")); + printer->setDriverInfo(driver.isEmpty() ? i18n("Unknown Driver", "Unknown") : driver); + // device + KURL url; + if (!entry->arg("rm").isEmpty()) + { + url = QString::fromLatin1("lpd://%1/%2").arg(entry->arg("rm")).arg(entry->arg("rp")); + printer->setDescription(i18n("Remote LPD queue %1@%2").arg(entry->arg("rp")).arg(entry->arg("rm"))); + } + else if (!lp.isEmpty() && lp != "/dev/null") + url = QString::fromLatin1("parallel:%1").arg(lp); + else if (QFile::exists(entry->arg("sd")+"/.config")) + { + QMap<QString,QString> map = loadPrinttoolCfgFile(entry->arg("sd")+"/.config"); + if (type == "SMB") + { + QStringList l = QStringList::split('\\',map["share"],false); + if (map["workgroup"].isEmpty()) + url = QString::fromLatin1("smb://%1/%2").arg(l[0]).arg(l[1]); + else + url = QString::fromLatin1("smb://%1/%2/%3").arg(map["workgroup"]).arg(l[0]).arg(l[1]); + url.setUser(map["user"]); + url.setPass(map["password"]); + } + else if (type == "DIRECT") + url = QString::fromLatin1("socket://%1:%2").arg(map["printer_ip"]).arg(map["port"]); + else if (type == "NCP") + { + url = QString::fromLatin1("ncp://%1/%2").arg(map["server"]).arg(map["queue"]); + url.setUser(map["user"]); + url.setPass(map["password"]); + } + } + printer->setDevice(url); + return true; + } + else return false; +} + +bool KMLpdManager::createPrinter(KMPrinter *printer) +{ + // 1) create the printcap entry + PrintcapEntry *ent = findPrintcapEntry(printer->printerName()); + if (!ent) + { + ent = new PrintcapEntry(); + ent->m_name = printer->printerName(); + } + else + { + if (!printer->driver() && printer->option("kde-driver") != "raw") + printer->setDriver(loadPrinterDriver(printer,true)); + // remove it from current entries + ent = m_entries.take(ent->m_name); + ent->m_args.clear(); + } + // Standard options + if (printer->device().protocol() == "lpd") + { + // remote lpd queue + ent->m_args["rm"] = printer->device().host(); + ent->m_args["rp"] = printer->device().path().replace("/",QString::fromLatin1("")); + ent->m_args["lpd_bounce"] = "true"; + ent->m_comment = QString::fromLatin1("##PRINTTOOL3## REMOTE"); + } + ent->m_args["mx"] = (printer->option("mx").isEmpty() ? "#0" : printer->option("mx")); + ent->m_args["sh"] = QString::null; + // create spool directory (if necessary) and update PrintcapEntry object + if (!createSpooldir(ent)) + { + setErrorMsg(i18n("Unable to create spool directory %1 for printer %2.").arg(ent->arg("sd")).arg(ent->m_name)); + delete ent; + return false; + } + if (!printer->driver() || printer->driver()->get("drtype") == "printtool") + if (!createPrinttoolEntry(printer,ent)) + { + setErrorMsg(i18n("Unable to save information for printer <b>%1</b>.").arg(printer->printerName())); + delete ent; + return false; + } + + // 2) write the printcap file + m_entries.insert(ent->m_name,ent); + if (!writePrinters()) + return false; + + // 3) save the printer driver (if any) + if (printer->driver()) + { + if (!savePrinterDriver(printer,printer->driver())) + { + m_entries.remove(ent->m_name); + writePrinters(); + return false; + } + } + + // 4) change permissions of spool directory + QCString cmd = "chmod -R o-rwx,g+rwX "; + cmd += QFile::encodeName(KProcess::quote(ent->arg("sd"))); + cmd += "&& chown -R lp.lp "; + cmd += QFile::encodeName(KProcess::quote(ent->arg("sd"))); + if (system(cmd.data()) != 0) + { + setErrorMsg(i18n("Unable to set correct permissions on spool directory %1 for printer <b>%2</b>.").arg(ent->arg("sd")).arg(ent->m_name)); + return false; + } + + return true; +} + +bool KMLpdManager::removePrinter(KMPrinter *printer) +{ + PrintcapEntry *ent = findPrintcapEntry(printer->printerName()); + if (ent) + { + ent = m_entries.take(printer->printerName()); + if (!writePrinters()) + { + m_entries.insert(ent->m_name,ent); + return false; + } + QCString cmd = "rm -rf "; + cmd += QFile::encodeName(KProcess::quote(ent->arg("sd"))); + system(cmd.data()); + delete ent; + return true; + } + else + return false; +} + +bool KMLpdManager::enablePrinter(KMPrinter *printer, bool state) +{ + KPipeProcess proc; + QString cmd = programName(0); + cmd += " "; + cmd += state ? "up" : "down"; + cmd += " "; + cmd += KProcess::quote(printer->printerName()); + if (proc.open(cmd)) + { + QTextStream t(&proc); + QString buffer; + while (!t.eof()) + buffer.append(t.readLine()); + if (buffer.startsWith("?Privilege")) + { + setErrorMsg(i18n("Permission denied: you must be root.")); + return false; + } + return true; + } + else + { + setErrorMsg(i18n("Unable to execute command \"%1\".").arg(cmd)); + return false; + } +} + +bool KMLpdManager::enablePrinter(KMPrinter *printer) +{ + return enablePrinter(printer,true); +} + +bool KMLpdManager::disablePrinter(KMPrinter *printer) +{ + return enablePrinter(printer,false); +} + +void KMLpdManager::listPrinters() +{ + m_entries.clear(); + loadPrintcapFile(QString::fromLatin1("%1/etc/printcap").arg(lpdprefix)); + + QDictIterator<PrintcapEntry> it(m_entries); + for (;it.current();++it) + { + KMPrinter *printer = it.current()->createPrinter(); + addPrinter(printer); + } + + checkStatus(); +} + +QString KMLpdManager::programName(int f) +{ + KConfig *conf = KMFactory::self()->printConfig(); + conf->setGroup("LPD"); + switch (f) + { + case 0: return conf->readPathEntry("LpdCommand","/usr/sbin/lpc"); + case 1: return conf->readPathEntry("LpdQueue","lpq"); + case 2: return conf->readPathEntry("LpdRemove","lprm"); + } + return QString::null; +} + +void KMLpdManager::checkStatus() +{ + KPipeProcess proc; + QString cmd = programName(0) + " status all"; + if (proc.open(cmd)) + { + QTextStream t(&proc); + QString line; + KMPrinter *printer(0); + int p(-1); + while (!t.eof()) + { + line = t.readLine().stripWhiteSpace(); + if (line.isEmpty()) + continue; + if ((p=line.find(':')) != -1) + printer = findPrinter(line.left(p)); + else if (line.startsWith("printing") && printer) + printer->setState(line.find("enabled") != -1 ? KMPrinter::Idle : KMPrinter::Stopped); + else if (line.find("entries") != -1 && printer) + if (!line.startsWith("no") && printer->state() == KMPrinter::Idle) + printer->setState(KMPrinter::Processing); + } + } +} + +bool KMLpdManager::writePrinters() +{ + if (!writePrintcapFile(QString::fromLatin1("%1/etc/printcap").arg(lpdprefix))) + { + setErrorMsg(i18n("Unable to write printcap file.")); + return false; + } + return true; +} + +void KMLpdManager::loadPrintcapFile(const QString& filename) +{ + QFile f(filename); + if (f.exists() && f.open(IO_ReadOnly)) + { + QTextStream t(&f); + QString line, comment; + PrintcapEntry *entry; + while (!t.eof()) + { + line = getPrintcapLine(t,&comment); + if (line.isEmpty()) + continue; + entry = new PrintcapEntry; + if (entry->readLine(line)) + { + m_entries.insert(entry->m_name,entry); + entry->m_comment = comment; + } + else + { + delete entry; + break; + } + } + } +} + +bool KMLpdManager::writePrintcapFile(const QString& filename) +{ + QFile f(filename); + if (f.open(IO_WriteOnly)) + { + QTextStream t(&f); + t << "# File generated by KDE print (LPD plugin).\n#Don't edit by hand." << endl << endl; + QDictIterator<PrintcapEntry> it(m_entries); + for (;it.current();++it) + it.current()->writeEntry(t); + return true; + } + return false; +} + +PrinttoolEntry* KMLpdManager::findPrinttoolEntry(const QString& name) +{ + if (m_ptentries.count() == 0) + loadPrinttoolDb(driverDirectory()+"/printerdb"); + PrinttoolEntry *ent = m_ptentries.find(name); + if (!ent) + setErrorMsg(i18n("Couldn't find driver <b>%1</b> in printtool database.").arg(name)); + return ent; +} + +void KMLpdManager::loadPrinttoolDb(const QString& filename) +{ + QFile f(filename); + if (f.exists() && f.open(IO_ReadOnly)) + { + QTextStream t(&f); + PrinttoolEntry *entry = new PrinttoolEntry; + while (entry->readEntry(t)) + { + m_ptentries.insert(entry->m_name,entry); + entry = new PrinttoolEntry; + } + delete entry; + } +} + +DrMain* KMLpdManager::loadDbDriver(KMDBEntry *entry) +{ + QString ptdbfilename = driverDirectory() + "/printerdb"; + if (entry->file == ptdbfilename) + { + PrinttoolEntry *ptentry = findPrinttoolEntry(entry->modelname); + if (ptentry) + { + DrMain *dr = ptentry->createDriver(); + return dr; + } + } + return NULL; +} + +PrintcapEntry* KMLpdManager::findPrintcapEntry(const QString& name) +{ + PrintcapEntry *ent = m_entries.find(name); + if (!ent) + setErrorMsg(i18n("Couldn't find printer <b>%1</b> in printcap file.").arg(name)); + return ent; +} + +DrMain* KMLpdManager::loadPrinterDriver(KMPrinter *printer, bool config) +{ + PrintcapEntry *entry = findPrintcapEntry(printer->name()); + if (!entry) + return NULL; + + // check for printtool driver (only for configuration) + QString sd = entry->arg("sd"), dr(entry->comment(7)); + if (QFile::exists(sd+"/postscript.cfg") && config && !dr.isEmpty()) + { + QMap<QString,QString> map = loadPrinttoolCfgFile(sd+"/postscript.cfg"); + PrinttoolEntry *ptentry = findPrinttoolEntry(dr); + if (!ptentry) + return NULL; + DrMain *dr = ptentry->createDriver(); + dr->setOptions(map); + map = loadPrinttoolCfgFile(sd+"/general.cfg"); + dr->setOptions(map); + map = loadPrinttoolCfgFile(sd+"/textonly.cfg"); + dr->setOptions(map); + return dr; + } + + // default + if (entry->m_comment.startsWith("##PRINTTOOL3##")) + setErrorMsg(i18n("No driver found (raw printer)")); + else + setErrorMsg(i18n("Printer type not recognized.")); + return NULL; +} + +bool KMLpdManager::checkGsDriver(const QString& gsdriver) +{ + if (gsdriver == "ppa" || gsdriver == "POSTSCRIPT" || gsdriver == "TEXT") + return true; + else if (!m_gschecker->checkGsDriver(gsdriver)) + { + setErrorMsg(i18n("The driver device <b>%1</b> is not compiled in your GhostScript distribution. Check your installation or use another driver.").arg(gsdriver)); + return false; + } + return true; +} + +QMap<QString,QString> KMLpdManager::loadPrinttoolCfgFile(const QString& filename) +{ + QFile f(filename); + QMap<QString,QString> map; + if (f.exists() && f.open(IO_ReadOnly)) + { + QTextStream t(&f); + QString line, name, val; + int p(-1); + while (!t.eof()) + { + line = getPrintcapLine(t); + if (line.isEmpty()) + break; + if (line.startsWith("export ")) + line.replace(0,7,""); + if ((p=line.find('=')) != -1) + { + name = line.left(p); + val = line.right(line.length()-p-1); + val.replace("\"",""); + val.replace("'",""); + if (!name.isEmpty() && !val.isEmpty()) + map[name] = val; + } + } + } + return map; +} + +bool KMLpdManager::savePrinttoolCfgFile(const QString& templatefile, const QString& dirname, const QMap<QString,QString>& options) +{ + // defines input and output file + QString fname = QFileInfo(templatefile).fileName(); + fname.replace(QRegExp("\\.in$"),QString::fromLatin1("")); + QFile fin(templatefile); + QFile fout(dirname + "/" + fname); + if (fin.exists() && fin.open(IO_ReadOnly) && fout.open(IO_WriteOnly)) + { + QTextStream tin(&fin), tout(&fout); + QString line, name; + int p(-1); + while (!tin.eof()) + { + line = tin.readLine().stripWhiteSpace(); + if (line.isEmpty() || line[0] == '#') + { + tout << line << endl; + continue; + } + if (line.startsWith("export ")) + { + tout << "export "; + line.replace(0,7,QString::fromLatin1("")); + } + if ((p=line.find('=')) != -1) + { + name = line.left(p); + tout << name << '=' << options[name] << endl; + } + } + return true; + } + else return false; +} + +bool KMLpdManager::savePrinterDriver(KMPrinter *printer, DrMain *driver) +{ + // To be able to save a printer driver, a printcap entry MUST exist. + // We can then retrieve the spool directory from it. + QString spooldir; + PrintcapEntry *ent = findPrintcapEntry(printer->printerName()); + if (!ent) + return false; + spooldir = ent->arg("sd"); + + if (driver->get("drtype") == "printtool" && !spooldir.isEmpty()) + { + QMap<QString,QString> options; + driver->getOptions(options,true); + // add some standard options + options["DESIRED_TO"] = "ps"; + options["PRINTER_TYPE"] = ent->comment(2); // get type from printcap entry (works in anycases) + options["PS_SEND_EOF"] = "NO"; + if (!checkGsDriver(options["GSDEVICE"])) + return false; + QString resol(options["RESOLUTION"]), color(options["COLOR"]); + // update entry comment to make printtool happy and save printcap file + ent->m_comment = QString::fromLatin1("##PRINTTOOL3## %1 %2 %3 %4 {} {%5} %6 {}").arg(options["PRINTER_TYPE"]).arg(options["GSDEVICE"]).arg((resol.isEmpty() ? QString::fromLatin1("NAxNA") : resol)).arg(options["PAPERSIZE"]).arg(driver->name()).arg((color.isEmpty() ? QString::fromLatin1("Default") : color.right(color.length()-15))); + ent->m_args["if"] = spooldir+QString::fromLatin1("/filter"); + if (!writePrinters()) + return false; + // write various driver files using templates + QCString cmd = "cp "; + cmd += QFile::encodeName(KProcess::quote(driverDirectory()+"/master-filter")); + cmd += " "; + cmd += QFile::encodeName(KProcess::quote(spooldir + "/filter")); + if (system(cmd.data()) == 0 && + savePrinttoolCfgFile(driverDirectory()+"/general.cfg.in",spooldir,options) && + savePrinttoolCfgFile(driverDirectory()+"/postscript.cfg.in",spooldir,options) && + savePrinttoolCfgFile(driverDirectory()+"/textonly.cfg.in",spooldir,options)) + return true; + setErrorMsg(i18n("Unable to write driver associated files in spool directory.")); + } + return false; +} + +bool KMLpdManager::createPrinttoolEntry(KMPrinter *printer, PrintcapEntry *entry) +{ + KURL dev(printer->device()); + QString prot = dev.protocol(), sd(entry->arg("sd")); + entry->m_comment = QString::fromLatin1("##PRINTTOOL3## %1").arg(ptPrinterType(printer)); + if (prot == "smb" || prot == "ncp" || prot == "socket") + { + entry->m_args["af"] = sd+QString::fromLatin1("/acct"); + QFile f(sd+QString::fromLatin1("/.config")); + if (f.open(IO_WriteOnly)) + { + QTextStream t(&f); + if (prot == "socket") + { + t << "printer_ip=" << dev.host() << endl; + t << "port=" << dev.port() << endl; + entry->m_args["if"] = driverDirectory()+QString::fromLatin1("/directprint"); + } + else if (prot == "smb") + { + QStringList l = QStringList::split('/',dev.path(),false); + if (l.count() == 2) + { + t << "share='\\\\" << l[0] << '\\' << l[1] << '\'' << endl; + } + else if (l.count() == 1) + { + t << "share='\\\\" << dev.host() << '\\' << l[0] << '\'' << endl; + } + t << "hostip=" << endl; + t << "user='" << dev.user() << '\'' << endl; + t << "password='" << dev.pass() << '\'' << endl; + t << "workgroup='" << (l.count() == 2 ? dev.host() : QString::fromLatin1("")) << '\'' << endl; + entry->m_args["if"] = driverDirectory()+QString::fromLatin1("/smbprint"); + } + else if (prot == "ncp") + { + t << "server=" << dev.host() << endl; + t << "queue=" << dev.path().replace("/",QString::fromLatin1("")) << endl; + t << "user=" << dev.user() << endl; + t << "password=" << dev.pass() << endl; + entry->m_args["if"] = driverDirectory()+QString::fromLatin1("/ncpprint"); + } + } + else return false; + entry->m_args["lp"] = QString::fromLatin1("/dev/null"); + } + else if (prot != "lpd") + entry->m_args["lp"] = dev.path(); + return true; +} + +bool KMLpdManager::createSpooldir(PrintcapEntry *entry) +{ + // first check if it has a "sd" defined + if (entry->arg("sd").isEmpty()) + entry->m_args["sd"] = QString::fromLatin1("/var/spool/lpd/")+entry->m_name; + QString sd = entry->arg("sd"); + if (!KStandardDirs::exists(sd)) + { + if (!KStandardDirs::makeDir(sd,0750)) + return false; + struct passwd *lp_pw = getpwnam("lp"); + if (lp_pw && chown(QFile::encodeName(sd),lp_pw->pw_uid,lp_pw->pw_gid) != 0) + return false; + } + return true; +} + +bool KMLpdManager::validateDbDriver(KMDBEntry *entry) +{ + PrinttoolEntry *ptentry = findPrinttoolEntry(entry->modelname); + return (ptentry && checkGsDriver(ptentry->m_gsdriver)); +} + +//************************************************************************************************ + +QString ptPrinterType(KMPrinter *p) +{ + QString type, prot = p->device().protocol(); + if (prot == "lpd") type = "REMOTE"; + else if (prot == "smb") type = "SMB"; + else if (prot == "ncp") type = "NCP"; + else if (prot == "socket") type = "DIRECT"; + else type = "LOCAL"; + return type; +} diff --git a/kdeprint/lpd/kmlpdmanager.h b/kdeprint/lpd/kmlpdmanager.h new file mode 100644 index 000000000..e38fdb6aa --- /dev/null +++ b/kdeprint/lpd/kmlpdmanager.h @@ -0,0 +1,76 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef KMLPDMANAGER_H +#define KMLPDMANAGER_H + +#include "kmmanager.h" +#include <qdict.h> + +class PrintcapEntry; +class PrinttoolEntry; +class GsChecker; + +class KMLpdManager : public KMManager +{ +public: + KMLpdManager(QObject *parent = 0, const char *name = 0); + ~KMLpdManager(); + + bool completePrinterShort(KMPrinter*); + bool completePrinter(KMPrinter*); + bool createPrinter(KMPrinter*); + bool removePrinter(KMPrinter*); + bool enablePrinter(KMPrinter*); + bool disablePrinter(KMPrinter*); + + // Driver DB functions + QString driverDbCreationProgram(); + QString driverDirectory(); + + // Driver loading functions + DrMain* loadDbDriver(KMDBEntry*); + DrMain* loadPrinterDriver(KMPrinter *p, bool config = false); + bool savePrinterDriver(KMPrinter*, DrMain*); + bool validateDbDriver(KMDBEntry*); + +protected: + void listPrinters(); + bool writePrinters(); + void loadPrintcapFile(const QString& filename); + bool writePrintcapFile(const QString& filename); + void loadPrinttoolDb(const QString& filename); + QMap<QString,QString> loadPrinttoolCfgFile(const QString& filename); + bool savePrinttoolCfgFile(const QString& templatefile, const QString& dirname, const QMap<QString,QString>& options); + bool checkGsDriver(const QString& gsdriver); + bool createSpooldir(PrintcapEntry*); + bool createPrinttoolEntry(KMPrinter*, PrintcapEntry*); + PrintcapEntry* findPrintcapEntry(const QString& name); + PrinttoolEntry* findPrinttoolEntry(const QString& name); + QString programName(int); + void checkStatus(); + bool enablePrinter(KMPrinter*, bool); + +private: + QDict<PrintcapEntry> m_entries; + QDict<PrinttoolEntry> m_ptentries; + GsChecker *m_gschecker; +}; + +#endif diff --git a/kdeprint/lpd/kmlpduimanager.cpp b/kdeprint/lpd/kmlpduimanager.cpp new file mode 100644 index 000000000..911f96b12 --- /dev/null +++ b/kdeprint/lpd/kmlpduimanager.cpp @@ -0,0 +1,60 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "kmlpduimanager.h" +#include "kmfactory.h" +#include "kmmanager.h" +#include "kmwizard.h" +#include "kmwbackend.h" +#include "kmpropertypage.h" +#include "kmpropbackend.h" +#include "kmpropdriver.h" + +#include <qfile.h> +#include <klocale.h> + +KMLpdUiManager::KMLpdUiManager(QObject *parent, const char *name) +: KMUiManager(parent,name) +{ +} + +KMLpdUiManager::~KMLpdUiManager() +{ +} + +void KMLpdUiManager::setupWizard(KMWizard *wizard) +{ + KMWBackend *backend = wizard->backendPage(); + backend->addBackend(KMWizard::Local,i18n("Local printer (parallel, serial, USB)"),true); + backend->addBackend(KMWizard::LPD,i18n("Remote LPD queue"),true); + backend->addBackend(KMWizard::SMB,i18n("SMB shared printer (Windows)"),false,KMWizard::Password); + backend->addBackend(KMWizard::TCP,i18n("Network printer (TCP)"),false); + backend->addBackend(KMWizard::File,i18n("File printer (print to file)"),true); + + KMManager *mgr = KMFactory::self()->manager(); + if (QFile::exists(mgr->driverDirectory()+"/smbprint")) backend->enableBackend(KMWizard::SMB,true); + if (QFile::exists(mgr->driverDirectory()+"/directprint")) backend->enableBackend(KMWizard::TCP,true); + if (QFile::exists(mgr->driverDirectory()+"/ncpprint")) backend->enableBackend(KMWizard::Custom+1,true); +} + +void KMLpdUiManager::setupPropertyPages(KMPropertyPage *p) +{ + p->addPropPage(new KMPropBackend(p, "Backend")); + p->addPropPage(new KMPropDriver(p, "Driver")); +} diff --git a/kdeprint/lpd/kmlpduimanager.h b/kdeprint/lpd/kmlpduimanager.h new file mode 100644 index 000000000..df3fb4561 --- /dev/null +++ b/kdeprint/lpd/kmlpduimanager.h @@ -0,0 +1,35 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef KMLPDUIMANAGER_H +#define KMLPDUIMANAGER_H + +#include "kmuimanager.h" + +class KMLpdUiManager : public KMUiManager +{ +public: + KMLpdUiManager(QObject *parent = 0, const char *name = 0); + ~KMLpdUiManager(); + + void setupWizard(KMWizard*); + void setupPropertyPages(KMPropertyPage*); +}; + +#endif diff --git a/kdeprint/lpd/lpd.print b/kdeprint/lpd/lpd.print new file mode 100644 index 000000000..019d5b9c8 --- /dev/null +++ b/kdeprint/lpd/lpd.print @@ -0,0 +1,87 @@ +[KDE Print Entry] +PrintSystem=lpd +Comment=LPR (Standard BSD print system) +Comment[af]= Lpr (Standaard Bsd druk stelsel) +Comment[ar]=LPR (نظام طباعة BSD المعياري) +Comment[az]=LPR (Standard BSD çap sistemi) +Comment[be]=LPR (Звычайная сістэма друку BSD) +Comment[bn]=এল-পি-আর (স্ট্যাণ্ডার্ড বি-এস-ডি মুদ্রণ ব্যবস্থা) +Comment[br]=LPR (Reizhiad moulañ reoliek evit BSD) +Comment[bs]=LPR (standardni BSD sistem štampe) +Comment[ca]=LPR (sistema d'impressió estàndard de BSD) +Comment[cs]=LPR (Standardní tiskový systém na BSD) +Comment[csb]=LPR (sztandardowô systema drëkù BSD) +Comment[cy]=LPR (Cysawd argraffu cyffredinol BSD) +Comment[da]=LPR (Standard BSD-udskriftssystem) +Comment[de]=LPR (standardmäßiges BSD-Drucksystem) +Comment[el]=LPR (Τυπικό BSD σύστημα εκτύπωσης) +Comment[eo]=LPR (Normala BSD-pressistemo) +Comment[es]=LPR (sistema de impresión estándar de BSD) +Comment[et]=LPR (standardne BSD trükkimise süsteem) +Comment[eu]=LPR (BSDren inprimatze-sistema estandarra) +Comment[fa]=LPR (سیستم چاپ BSD استاندارد) +Comment[fi]=LPR (standardi BSD-tulostusjärjestelmä) +Comment[fr]=LPR (système d'impression BSD standard) +Comment[fy]=LPR (standert BSD-ôfdruksysteem) +Comment[ga]=LPR (Gnáthchóras priontála BSD) +Comment[gl]=LPR (Sistema de Impresión Estándar de BSD) +Comment[he]=מערכת ההדפסה הסטנדרטית של LPR) BSD) +Comment[hi]=LPR (मानक BSD छपाई पद्धत्ति) +Comment[hr]=LPR (standardni BSD sustav za ispis) +Comment[hsb]=LPR (Standardny ćišćenski system za BSD) +Comment[hu]=LPR (BSD-típusú nyomtatórendszer) +Comment[id]=LPR (sistem pencetakan standar BSD) +Comment[is]= LPR (Venjulega BSD prentkerfið) +Comment[it]=LPR (sistema di stampa standard di BSD) +Comment[ja]=LPR (標準 BSD 印刷システム) +Comment[ka]=LPR (სტანდარტული BSD ბეჭდვის სისტემა) +Comment[kk]=LPR (BSD жүйесіндегі стандартты басып шығаруы) +Comment[km]=LPR (ប្រព័ន្ធបោះពុម្ព BSD ខ្នាតគំរូ) +Comment[ko]=LPR (표준 BSD 인쇄 시스템) +Comment[lb]=LPR (Standard-Drécksystem vu BSD) +Comment[lt]=LPR (Standartinė BSD spausdinimo sistema) +Comment[lv]=LPR (Standarta BSD drukas sistēma) +Comment[mk]=LPR (Стандардниот BSD систем за печатење) +Comment[mn]=LPR (Стандарт BSD-Хэвлэх систем) +Comment[ms]=LPR (Sistem cetak piawai BSD) +Comment[mt]=LPR (sistema tal-ipprintjar standard BSD) +Comment[nb]=LPR (Standard BSD skriversystem) +Comment[nds]=LPR (Dat Standard-Drucksysteem vun BSD) +Comment[ne]=LPR (मानक BSD मुद्रण प्रणाली) +Comment[nl]=LPR (standaard BSD-afdruksysteem) +Comment[nn]=LPR (vanleg BSD-utskriftssystem) +Comment[nso]=LPR (System yeo e lekanetsego ya kgatiso ya BSD) +Comment[pa]=LPR (ਮਿਆਰੀ BSD ਪ੍ਰਿੰਟ ਸਿਸਟਮ) +Comment[pl]=LPR (standardowy system druku BSD) +Comment[pt]=LPR (o sistema de impressão do BSD) +Comment[pt_BR]=LPR (Sistema padrão de Impressão do BSD) +Comment[ro]=LPR (Sistemul de tipărire standard BSD) +Comment[ru]=LPR (стандартная система печати BSD) +Comment[rw]=LPR (Sisitemu yo gucapa BSD isanzwe) +Comment[se]=LPR (Standárda BSD čálihanvuogádat) +Comment[sk]=LPR (Štandardný tlačový systém BSD) +Comment[sl]=LPR (običajni tiskalniški sistem iz BSD) +Comment[sq]=LPR (BSD Sistem Standard Shtypi +Comment[sr]=LPR (стандардни BSD систем за штампање) +Comment[sr@Latn]=LPR (standardni BSD sistem za štampanje) +Comment[ss]=LPR (Umhini wekushicelela welizinga le BSD) +Comment[sv]=LPR (Standardskrivarsystem för BSD) +Comment[ta]=LPR (நிலையான BSD அச்சு அமைப்பு) +Comment[te]=ఎల్ పి ఆర్ (సాధరణ బి ఎస్ డి ప్రచురణ వ్యవస్థ) +Comment[tg]=LPR (системаи чопи стандартии BSD) +Comment[th]=LPR (ระบบการพิมพ์มาตรฐานของ BSD) +Comment[tr]=LPR (Standart BSD yazdırma sistemi) +Comment[tt]=LPR (BSD'nıñ töp bastıru sisteme) +Comment[uk]=LPR (типова система друку BSD) +Comment[uz]=LPR (BSD'ning andoza bosib chiqarish tizimi) +Comment[uz@cyrillic]=LPR (BSD'нинг андоза босиб чиқариш тизими) +Comment[ven]=LPR (maitele a u phirintha a zwino fana zwa BCD) +Comment[vi]=LPR (hệ thống in BSD chuẩn) +Comment[wa]=LPR (Sistinme d' imprimaedje BSD standård) +Comment[xh]=LPR (Indlela esezantsi yoshicilelo lwe BSD) +Comment[zh_CN]=LPR (标准 BSD 打印系统) +Comment[zh_HK]=LPR (標準 BSD 列印系統) +Comment[zh_TW]=LPR (標準 BSD 列印系統) +Comment[zu]=LPR (Isistimu yokushicelela evamile ye-BSD) +DetectUris=service:/printer,config:/printcap +DetectPrecedence=5 diff --git a/kdeprint/lpd/lpdtools.cpp b/kdeprint/lpd/lpdtools.cpp new file mode 100644 index 000000000..bba934e61 --- /dev/null +++ b/kdeprint/lpd/lpdtools.cpp @@ -0,0 +1,417 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "lpdtools.h" +#include "driver.h" +#include "kmprinter.h" + +#include <qfile.h> +#include <klocale.h> + +static const char *pt_pagesize[] = { + "ledger", I18N_NOOP("Ledger"), + "legal", I18N_NOOP("US Legal"), + "letter", I18N_NOOP("US Letter"), + "a4", I18N_NOOP("A4"), + "a3", I18N_NOOP("A3"), + "b4", I18N_NOOP("B4"), + "b5", I18N_NOOP("B5"), + 0 +}; +static int pt_nup[] = { 1, 2, 4, 8, -1 }; +static const char *pt_bool[] = { + "YES", I18N_NOOP("Enabled"), + "NO", I18N_NOOP("Disabled"), + 0 +}; + +void setupBooleanOption(DrBooleanOption *opt) +{ + int i(0); + while (pt_bool[i]) + { + DrBase *ch = new DrBase(); + ch->setName(pt_bool[i++]); + ch->set("text",pt_bool[i++]); + opt->addChoice(ch); + } +} + +QString nextWord(const QString& s, int& pos) +{ + int p1(pos), p2(0); + while (s[p1].isSpace() && p1 < (int)s.length()) p1++; + if (s[p1] == '{') + { + p1++; + p2 = s.find('}',p1); + } + else + { + p2 = p1; + while (!s[p2].isSpace() && p2 < (int)s.length()) p2++; + } + pos = (p2+1); + return s.mid(p1,p2-p1); +} + +//************************************************************************************************ + +bool PrintcapEntry::readLine(const QString& line) +{ + QStringList l = QStringList::split(':',line,false); + if (l.count() > 0) + { + m_name = l[0]; + int p(-1); + // discard aliases + if ((p=m_name.find('|')) != -1) + m_name = m_name.left(p); + m_args.clear(); + for (uint i=1; i<l.count(); i++) + { + int p = l[i].find('='); + if (p == -1) p = 2; + QString key = l[i].left(p); + QString value = l[i].right(l[i].length()-(l[i][p] == '=' ? p+1 : p)); + m_args[key] = value; + } + return true; + } + return false; +} + +void PrintcapEntry::writeEntry(QTextStream& t) +{ + if (m_comment.isEmpty()) t << "# Entry for printer " << m_name << endl; + else t << m_comment << endl; + t << m_name << ":"; + for (QMap<QString,QString>::ConstIterator it=m_args.begin(); it!=m_args.end(); ++it) + { + t << "\\\n\t:" << it.key(); + if (!it.data().isEmpty()) + t << ((*it)[0] == '#' ? "" : "=") << *it; + t << ":"; + } + t << endl << endl; +} + +QString PrintcapEntry::comment(int index) +{ + QString w; + if (m_comment.startsWith("##PRINTTOOL3##")) + { + int p(0); + for (int i=0;i<index;i++) + w = nextWord(m_comment,p); + } + return w; +} + +KMPrinter* PrintcapEntry::createPrinter() +{ + KMPrinter *printer = new KMPrinter(); + printer->setName(m_name); + printer->setPrinterName(m_name); + printer->setInstanceName(QString::null); + printer->setState(KMPrinter::Idle); + printer->setType(KMPrinter::Printer); + return printer; +} + +//************************************************************************************************ + +QStringList splitPrinttoolLine(const QString& line) +{ + QStringList l; + int p = line.find(':'); + if (p != -1) + { + l.append(line.left(p)); + p = line.find('{',p); + if (p == -1) + l.append(line.right(line.length()-l[0].length()-1).stripWhiteSpace()); + else + { + while (p != -1) + { + int q = line.find('}',p); + if (q != -1) + { + l.append(line.mid(p+1,q-p-1)); + p = line.find('{',q); + } + else break; + } + } + } + return l; +} + +bool PrinttoolEntry::readEntry(QTextStream& t) +{ + QString line; + QStringList args; + + m_resolutions.setAutoDelete(true); + m_depths.setAutoDelete(true); + m_resolutions.clear(); + m_depths.clear(); + while (!t.eof()) + { + line = getPrintcapLine(t); + if (line.isEmpty()) + break; + if (line == "EndEntry") + return !m_name.isEmpty(); + QStringList l = splitPrinttoolLine(line); + if (l.count() > 1) + { + if (l[0] == "StartEntry") m_name = l[1]; + else if (l[0] == "GSDriver") m_gsdriver = l[1]; + else if (l[0] == "About") m_about = l[1]; + else if (l[0] == "Description") m_description = l[1]; + else if (l[0] == "Resolution" && l.count() > 2) + { + Resolution *resol = new Resolution; + bool ok(false); + resol->xdpi = l[1].toInt(&ok); + if (ok) resol->ydpi = l[2].toInt(&ok); + if (l.count() > 3) + resol->comment = l[3]; + if (ok) m_resolutions.append(resol); + else delete resol; + } + else if (l[0] == "BitsPerPixel" && l.count() > 1) + { + BitsPerPixel *dpth = new BitsPerPixel; + dpth->bpp = l[1]; + if (l.count() > 2) + dpth->comment = l[2]; + m_depths.append(dpth); + } + } + } + return false; +} + +DrMain* PrinttoolEntry::createDriver() +{ + // create driver + DrMain *dr = new DrMain(); + dr->setName(m_name); + dr->set("description",m_description); + dr->set("text",m_description); + dr->set("drtype","printtool"); + + DrGroup *gr(0); + DrListOption *lopt(0); + DrStringOption *sopt(0); + DrBooleanOption *bopt(0); + DrBase *ch(0); + + if (m_gsdriver != "TEXT") + { + // create GS group + gr = new DrGroup(); + gr->set("text",i18n("GhostScript settings")); + dr->addGroup(gr); + + // Pseudo option to have access to GS driver + lopt = new DrListOption(); + lopt->setName("GSDEVICE"); + lopt->set("text",i18n("Driver")); + lopt->set("default",m_gsdriver); + gr->addOption(lopt); + ch = new DrBase(); + ch->setName(m_gsdriver); + ch->set("text",m_gsdriver); + lopt->addChoice(ch); + lopt->setValueText(m_gsdriver); + + + // Resolutions + if (m_resolutions.count() > 0) + { + lopt = new DrListOption(); + lopt->setName("RESOLUTION"); + lopt->set("text",i18n("Resolution")); + gr->addOption(lopt); + QPtrListIterator<Resolution> it(m_resolutions); + for (int i=0;it.current();++it,i++) + { + ch = new DrBase; + ch->setName(QString::fromLatin1("%1x%2").arg(it.current()->xdpi).arg(it.current()->ydpi)); + if (it.current()->comment.isEmpty()) + ch->set("text",QString::fromLatin1("%1x%2 DPI").arg(it.current()->xdpi).arg(it.current()->ydpi)); + else + ch->set("text",QString::fromLatin1("%2x%3 DPI (%1)").arg(it.current()->comment).arg(it.current()->xdpi).arg(it.current()->ydpi)); + lopt->addChoice(ch); + } + QString defval = lopt->choices()->first()->name(); + lopt->set("default",defval); + lopt->setValueText(defval); + } + + // BitsPerPixels + if (m_depths.count() > 0) + { + lopt = new DrListOption(); + lopt->setName("COLOR"); + lopt->set("text",i18n("Color depth")); + gr->addOption(lopt); + QPtrListIterator<BitsPerPixel> it(m_depths); + for (int i=0;it.current();++it,i++) + { + ch = new DrBase; + if (m_gsdriver != "uniprint") + ch->setName(QString::fromLatin1("-dBitsPerPixel=%1").arg(it.current()->bpp)); + else + ch->setName(it.current()->bpp); + if (it.current()->comment.isEmpty()) + ch->set("text",it.current()->bpp); + else + ch->set("text",QString::fromLatin1("%1 - %2").arg(it.current()->bpp).arg(it.current()->comment)); + lopt->addChoice(ch); + } + QString defval = lopt->choices()->first()->name(); + lopt->set("default",defval); + lopt->setValueText(defval); + } + + // additional GS options + sopt = new DrStringOption; + sopt->setName("EXTRA_GS_OPTIONS"); + sopt->set("text",i18n("Additional GS options")); + gr->addOption(sopt); + } + + // General group + gr = new DrGroup(); + gr->set("text",i18n("General")); + dr->addGroup(gr); + + // Page size + lopt = new DrListOption(); + lopt->setName("PAPERSIZE"); + lopt->set("text",i18n("Page size")); + lopt->set("default","letter"); + gr->addOption(lopt); + int i(0); + while (pt_pagesize[i]) + { + ch = new DrBase(); + ch->setName(pt_pagesize[i++]); + ch->set("text",i18n(pt_pagesize[i++])); + lopt->addChoice(ch); + } + lopt->setValueText("letter"); + + // Nup + lopt = new DrListOption(); + lopt->setName("NUP"); + lopt->set("text",i18n("Pages per sheet")); + lopt->set("default","1"); + gr->addOption(lopt); + i = 0; + while (pt_nup[i] != -1) + { + ch = new DrBase(); + ch->setName(QString::number(pt_nup[i++])); + ch->set("text",ch->name()); + lopt->addChoice(ch); + } + lopt->setValueText("1"); + + // Margins + sopt = new DrStringOption(); + sopt->setName("RTLFTMAR"); + sopt->set("text",i18n("Left/right margin (1/72 in)")); + sopt->setValueText("18"); + gr->addOption(sopt); + sopt = new DrStringOption(); + sopt->setName("TOPBOTMAR"); + sopt->set("text",i18n("Top/bottom margin (1/72 in)")); + sopt->setValueText("18"); + gr->addOption(sopt); + + // Text group + gr = new DrGroup(); + gr->set("text",i18n("Text options")); + dr->addGroup(gr); + + // Send EOF + bopt = new DrBooleanOption(); + bopt->setName("TEXT_SEND_EOF"); + bopt->set("text",i18n("Send EOF after job to eject page")); + gr->addOption(bopt); + setupBooleanOption(bopt); + bopt->setValueText("NO"); + + // Fix stair-stepping + bopt = new DrBooleanOption(); + bopt->setName("CRLFTRANS"); + bopt->set("text",i18n("Fix stair-stepping text")); + gr->addOption(bopt); + setupBooleanOption(bopt); + bopt->choices()->first()->setName("1"); + bopt->choices()->last()->setName("0"); + bopt->setValueText("0"); + + if (m_gsdriver != "POSTSCRIPT") + { + // Fast text printing + bopt = new DrBooleanOption(); + bopt->setName("ASCII_TO_PS"); + bopt->set("text",i18n("Fast text printing (non-PS printers only)")); + gr->addOption(bopt); + setupBooleanOption(bopt); + bopt->choices()->first()->setName("NO"); + bopt->choices()->last()->setName("YES"); + bopt->setValueText("NO"); + } + + return dr; +} + +//************************************************************************************************ + +QString getPrintcapLine(QTextStream& t, QString *lastcomment) +{ + QString line, buffer, comm; + while (!t.eof()) + { + buffer = t.readLine().stripWhiteSpace(); + if (buffer.isEmpty() || buffer[0] == '#') + { + comm = buffer; + continue; + } + line.append(buffer); + if (line.right(1) == "\\") + { + line.truncate(line.length()-1); + line = line.stripWhiteSpace(); + } + else break; + } + if (lastcomment) + *lastcomment = comm; + return line; +} diff --git a/kdeprint/lpd/lpdtools.h b/kdeprint/lpd/lpdtools.h new file mode 100644 index 000000000..2f8b8c975 --- /dev/null +++ b/kdeprint/lpd/lpdtools.h @@ -0,0 +1,76 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ +#ifndef LPDTOOLS_H +#define LPDTOOLS_H + +#include <qstring.h> +#include <qmap.h> +#include <qptrlist.h> +#include <qtextstream.h> + +class KMLpdManager; +class DrMain; +class KMPrinter; + +class PrintcapEntry +{ +friend class KMLpdManager; +public: + bool readLine(const QString& line); + void writeEntry(QTextStream&); + KMPrinter* createPrinter(); + QString arg(const QString& key) const { return m_args[key]; } + QString comment(int i); +private: + QString m_name; + QString m_comment; + QMap<QString,QString> m_args; +}; + +//***************************************************************************************************** + +struct Resolution +{ + int xdpi, ydpi; + QString comment; +}; + +struct BitsPerPixel +{ + QString bpp; + QString comment; +}; + +class PrinttoolEntry +{ +friend class KMLpdManager; +public: + bool readEntry(QTextStream& t); + DrMain* createDriver(); +private: + QString m_name, m_gsdriver, m_description, m_about; + QPtrList<Resolution> m_resolutions; + QPtrList<BitsPerPixel> m_depths; +}; + +//***************************************************************************************************** + +QString getPrintcapLine(QTextStream& t, QString *lastcomment = NULL); + +#endif // LPDTOOLS_H diff --git a/kdeprint/lpd/make_driver_db_lpd.c b/kdeprint/lpd/make_driver_db_lpd.c new file mode 100644 index 000000000..e907dbf2c --- /dev/null +++ b/kdeprint/lpd/make_driver_db_lpd.c @@ -0,0 +1,112 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> + +#include <config.h> + +#define BUFFER_SIZE 1024 + +int parseRhsPrinterDb(const char *filename, FILE *out); + +int main(int argc, char *argv[]) +{ + FILE *out; + + if (argc != 3) + { + fprintf(stderr,"usage: make_driver_db_lpd <dbdirectory> <dbfilename>\n"); + exit(-1); + } + out = fopen(argv[2],"w"); + if (out == NULL) + { + fprintf(stderr,"Unable to open DB file: %s\n",argv[2]); + exit(-1); + } + /* first parse RHS driver DB */ + if (!parseRhsPrinterDb("/usr/lib/rhs/rhs-printfilters/printerdb",out)) + fprintf(stderr,"Unable to parse RHS DB file\n"); + return 0; +} + +char* skipSpaces(char *c) +{ + char *cc = c; + while (cc && *cc && isspace(*cc)) cc++; + return cc; +} + +int parseRhsPrinterDb(const char *filename, FILE *out) +{ + FILE *in; + char buffer[BUFFER_SIZE], *c; + + in = fopen(filename,"r"); + if (in == NULL) + return 0; + while (fgets(buffer,BUFFER_SIZE,in)) + { + c = skipSpaces(buffer); /* skip leading white spaces */ + if (c == NULL || *c == '#') /* empty line or comment line */ + continue; + if (strncmp(c,"StartEntry:",11) == 0) /* start a new entry */ + { + fprintf(out,"\n"); + fprintf(out,"FILE=%s\n",filename); + c = skipSpaces(c+11); + if (c) + fprintf(out,"MODELNAME=%s",c); + } + else if (strncmp(c,"Description:",12) == 0) + { + char *c1, *c2; + c1 = strchr(c+12,'{'); + c2 = strchr(c+12,'}'); + if (c1 && c2) + { + char model[BUFFER_SIZE], manuf[BUFFER_SIZE]; + char *c3; + + *c2 = 0; + c1++; + c3 = strchr(c1,' '); + if (c3) + { + *c3 = 0; + c3++; + strlcpy(manuf,c1, sizeof(manuf)); + strlcpy(model,c3, sizeof(model)); + } + else + { + strlcpy(model,c1, sizeof(model)); + strlcpy(manuf,"PrintTool (RH)", sizeof(manuf)); + } + fprintf(out,"MANUFACTURER=%s\n",manuf); + fprintf(out,"MODEL=%s\n",model); + fprintf(out,"DESCRIPTION=%s %s\n",manuf,model); + } + } + } + return 1; +} |