From e16866e072f94410321d70daedbcb855ea878cac Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 6 Nov 2011 15:56:40 -0600 Subject: Actually move the kde files that were renamed in the last commit --- tdeprint/lpr/CMakeLists.txt | 80 +++++++ tdeprint/lpr/Makefile.am | 40 ++++ tdeprint/lpr/apsdriver1 | 84 +++++++ tdeprint/lpr/apsdriver2 | 83 +++++++ tdeprint/lpr/apshandler.cpp | 405 +++++++++++++++++++++++++++++++ tdeprint/lpr/apshandler.h | 57 +++++ tdeprint/lpr/editentrydialog.cpp | 157 ++++++++++++ tdeprint/lpr/editentrydialog.h | 62 +++++ tdeprint/lpr/klprfactory.cpp | 29 +++ tdeprint/lpr/klprprinterimpl.cpp | 59 +++++ tdeprint/lpr/klprprinterimpl.h | 38 +++ tdeprint/lpr/kmconfiglpr.cpp | 66 +++++ tdeprint/lpr/kmconfiglpr.h | 39 +++ tdeprint/lpr/kmlprjobmanager.cpp | 90 +++++++ tdeprint/lpr/kmlprjobmanager.h | 44 ++++ tdeprint/lpr/kmlprmanager.cpp | 492 ++++++++++++++++++++++++++++++++++++++ tdeprint/lpr/kmlprmanager.h | 82 +++++++ tdeprint/lpr/kmlpruimanager.cpp | 67 ++++++ tdeprint/lpr/kmlpruimanager.h | 37 +++ tdeprint/lpr/lpchelper.cpp | 320 +++++++++++++++++++++++++ tdeprint/lpr/lpchelper.h | 59 +++++ tdeprint/lpr/lpqhelper.cpp | 117 +++++++++ tdeprint/lpr/lpqhelper.h | 44 ++++ tdeprint/lpr/lpr.print | 86 +++++++ tdeprint/lpr/lprhandler.cpp | 270 +++++++++++++++++++++ tdeprint/lpr/lprhandler.h | 88 +++++++ tdeprint/lpr/lprngtooldriver1 | 85 +++++++ tdeprint/lpr/lprngtoolhandler.cpp | 393 ++++++++++++++++++++++++++++++ tdeprint/lpr/lprngtoolhandler.h | 54 +++++ tdeprint/lpr/lprsettings.cpp | 146 +++++++++++ tdeprint/lpr/lprsettings.h | 67 ++++++ tdeprint/lpr/make_driver_db_lpr.c | 192 +++++++++++++++ tdeprint/lpr/matichandler.cpp | 481 +++++++++++++++++++++++++++++++++++++ tdeprint/lpr/matichandler.h | 57 +++++ tdeprint/lpr/matichelper.cpp | 38 +++ tdeprint/lpr/matichelper.h | 29 +++ tdeprint/lpr/printcapentry.cpp | 82 +++++++ tdeprint/lpr/printcapentry.h | 81 +++++++ tdeprint/lpr/printcapreader.cpp | 176 ++++++++++++++ tdeprint/lpr/printcapreader.h | 43 ++++ 40 files changed, 4919 insertions(+) create mode 100644 tdeprint/lpr/CMakeLists.txt create mode 100644 tdeprint/lpr/Makefile.am create mode 100644 tdeprint/lpr/apsdriver1 create mode 100644 tdeprint/lpr/apsdriver2 create mode 100644 tdeprint/lpr/apshandler.cpp create mode 100644 tdeprint/lpr/apshandler.h create mode 100644 tdeprint/lpr/editentrydialog.cpp create mode 100644 tdeprint/lpr/editentrydialog.h create mode 100644 tdeprint/lpr/klprfactory.cpp create mode 100644 tdeprint/lpr/klprprinterimpl.cpp create mode 100644 tdeprint/lpr/klprprinterimpl.h create mode 100644 tdeprint/lpr/kmconfiglpr.cpp create mode 100644 tdeprint/lpr/kmconfiglpr.h create mode 100644 tdeprint/lpr/kmlprjobmanager.cpp create mode 100644 tdeprint/lpr/kmlprjobmanager.h create mode 100644 tdeprint/lpr/kmlprmanager.cpp create mode 100644 tdeprint/lpr/kmlprmanager.h create mode 100644 tdeprint/lpr/kmlpruimanager.cpp create mode 100644 tdeprint/lpr/kmlpruimanager.h create mode 100644 tdeprint/lpr/lpchelper.cpp create mode 100644 tdeprint/lpr/lpchelper.h create mode 100644 tdeprint/lpr/lpqhelper.cpp create mode 100644 tdeprint/lpr/lpqhelper.h create mode 100644 tdeprint/lpr/lpr.print create mode 100644 tdeprint/lpr/lprhandler.cpp create mode 100644 tdeprint/lpr/lprhandler.h create mode 100644 tdeprint/lpr/lprngtooldriver1 create mode 100644 tdeprint/lpr/lprngtoolhandler.cpp create mode 100644 tdeprint/lpr/lprngtoolhandler.h create mode 100644 tdeprint/lpr/lprsettings.cpp create mode 100644 tdeprint/lpr/lprsettings.h create mode 100644 tdeprint/lpr/make_driver_db_lpr.c create mode 100644 tdeprint/lpr/matichandler.cpp create mode 100644 tdeprint/lpr/matichandler.h create mode 100644 tdeprint/lpr/matichelper.cpp create mode 100644 tdeprint/lpr/matichelper.h create mode 100644 tdeprint/lpr/printcapentry.cpp create mode 100644 tdeprint/lpr/printcapentry.h create mode 100644 tdeprint/lpr/printcapreader.cpp create mode 100644 tdeprint/lpr/printcapreader.h (limited to 'tdeprint/lpr') diff --git a/tdeprint/lpr/CMakeLists.txt b/tdeprint/lpr/CMakeLists.txt new file mode 100644 index 000000000..c893411b1 --- /dev/null +++ b/tdeprint/lpr/CMakeLists.txt @@ -0,0 +1,80 @@ +################################################# +# +# (C) 2010 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +add_definitions( + -D_KDEPRINT_COMPILE +) + +include_directories( + ${TQT_INCLUDE_DIRS} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_BINARY_DIR}/tdecore + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/dcop + ${CMAKE_SOURCE_DIR}/tdecore + ${CMAKE_SOURCE_DIR}/tdeui + ${CMAKE_SOURCE_DIR}/tdeprint + ${CMAKE_SOURCE_DIR}/tdeprint/management +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### headers ################################### + +install( FILES + printcapentry.h lprhandler.h lprsettings.h + DESTINATION ${INCLUDE_INSTALL_DIR}/tdeprint/lpr ) + + +##### other data ################################ + +install( FILES lpr.print DESTINATION ${DATA_INSTALL_DIR}/tdeprint/plugins ) +install( FILES apsdriver1 apsdriver2 lprngtooldriver1 DESTINATION ${DATA_INSTALL_DIR}/tdeprint ) + + +#### tdeprint_lpr ############################### + +set( target tdeprint_lpr ) + +set( ${target}_SRCS + printcapreader.cpp lprhandler.cpp matichandler.cpp + klprfactory.cpp kmlprmanager.cpp kmlpruimanager.cpp + lpchelper.cpp lpqhelper.cpp kmlprjobmanager.cpp + matichelper.cpp klprprinterimpl.cpp printcapentry.cpp + lprsettings.cpp kmconfiglpr.cpp apshandler.cpp + lprngtoolhandler.cpp editentrydialog.cpp +) + +tde_add_kpart( ${target} AUTOMOC + SOURCES ${${target}_SRCS} + LINK tdeprint_management-shared + DESTINATION ${PLUGIN_INSTALL_DIR} +) + + +#### make_driver_db_lpr ######################### + +set( target make_driver_db_lpr ) + +set( ${target}_SRCS + make_driver_db_lpr.c +) + +tde_add_executable( ${target} + SOURCES ${${target}_SRCS} + LINK driverparse-static tdefakes-shared ${DL_LIBRARIES} + DESTINATION ${BIN_INSTALL_DIR} +) diff --git a/tdeprint/lpr/Makefile.am b/tdeprint/lpr/Makefile.am new file mode 100644 index 000000000..a9039888f --- /dev/null +++ b/tdeprint/lpr/Makefile.am @@ -0,0 +1,40 @@ +AM_CPPFLAGS = -D_KDEPRINT_COMPILE + +INCLUDES= -I$(top_srcdir) -I$(top_srcdir)/tdeprint -I$(top_srcdir)/tdeprint/management $(all_includes) + +kde_module_LTLIBRARIES = tdeprint_lpr.la + +tdeprint_lpr_la_SOURCES = printcapreader.cpp lprhandler.cpp \ + matichandler.cpp klprfactory.cpp kmlprmanager.cpp \ + kmlpruimanager.cpp lpchelper.cpp lpqhelper.cpp \ + kmlprjobmanager.cpp matichelper.cpp \ + klprprinterimpl.cpp printcapentry.cpp \ + lprsettings.cpp kmconfiglpr.cpp apshandler.cpp \ + lprngtoolhandler.cpp \ + editentrydialog.cpp +tdeprint_lpr_la_LDFLAGS = $(all_libraries) -module -avoid-version -no-undefined +tdeprint_lpr_la_LIBADD = $(top_builddir)/tdeprint/management/libtdeprint_management.la \ + $(LIB_QT) $(LIB_KDEPRINT) $(LIB_KDEUI) $(LIB_KDECORE) +tdeprint_lpr_la_METASOURCES = AUTO + +noinst_HEADERS = printcapreader.h \ + matichandler.h kmlprmanager.h \ + kmlpruimanager.h lpchelper.h lpqhelper.h \ + kmlprjobmanager.h matichelper.h klprprinterimpl.h \ + kmconfiglpr.h apshandler.h \ + lprngtoolhandler.h \ + editentrydialog.h + +tdeprintlpr_HEADERS = printcapentry.h lprhandler.h lprsettings.h +tdeprintlprdir = $(includedir)/tdeprint/lpr + +bin_PROGRAMS = make_driver_db_lpr +make_driver_db_lpr_SOURCES = make_driver_db_lpr.c +make_driver_db_lpr_LDFLAGS = $(all_libraries) +make_driver_db_lpr_LDADD = $(top_builddir)/tdecore/libtdefakes.la ../libdriverparse.a $(LIBDL) + +entry_DATA = lpr.print +entrydir = $(kde_datadir)/tdeprint/plugins + +miscdata_DATA = apsdriver1 apsdriver2 lprngtooldriver1 +miscdatadir = $(kde_datadir)/tdeprint diff --git a/tdeprint/lpr/apsdriver1 b/tdeprint/lpr/apsdriver1 new file mode 100644 index 000000000..fa6bbde0e --- /dev/null +++ b/tdeprint/lpr/apsdriver1 @@ -0,0 +1,84 @@ +GROUP|general|General +OPTION|PAPERSIZE|Page Size +CHOICE|a3|A3 +CHOICE|a4|A4 +CHOICE|legal|US Legal +CHOICE|letter|US Letter +CHOICE|ledger|Ledger +CHOICE|tabloid|Tabloid +DEFAULT|a4 +OPTION|COLOR|Color Mode +CHOICE|full|Color +CHOICE|gray|Grayscale +CHOICE|mono|Monochrome +DEFAULT|full +OPTION|QUALITY|Print Quality +CHOICE|draft|Draft +CHOICE|low|Low +CHOICE|medium|Medium +CHOICE|high|High +CHOICE|photo|Photo +DEFAULT|medium +OPTION|RESOLUTION|Print Resolution +CHOICE|150x150|150 DPI +CHOICE|180x180|180 DPI +CHOICE|300x300|300 DPI +CHOICE|360x180|360x180 DPI +CHOICE|360x360|360 DPI +CHOICE|600x600|600 DPI +CHOICE|720x720|720 DPI +CHOICE|1200x1200|1200 DPI +DEFAULT|300x300 +OPTION|METHOD|Print Method +CHOICE|auto|Automatic file conversion +CHOICE|ascii|Treat everything as text +CHOICE|raw|Pass-through mode +DEFAULT|auto +ENDGROUP| +GROUP|advanced|Advanced Settings +GROUP|paper|Paper Settings +OPTION|MEDIA|paper Type +CHOICE|plain|Plain paper +CHOICE|coated|Coated (InkJet) paper +CHOICE|glossy|Glossy paper +CHOICE|premium|Premium (Photo) paper +CHOICE|trans|Transparencies +CHOICE|(empty)|Default printer setting +DEFAULT|(empty) +OPTION|PAPERTRAY|Input Slot +CHOICE|tray0|Tray 0 +CHOICE|tray1|Tray 1 +CHOICE|tray2|Tray 2 +CHOICE|tray3|Tray 3 +CHOICE|tray4|Tray 4 +CHOICE|tray5|Tray 5 +CHOICE|(empty)|Default printer setting +DEFAULT|(empty) +OPTION|DUPLEX|Duplex Printing|BOOLEAN +CHOICE|true|Enabled +CHOICE|false|Disabled +DEFAULT|false +OPTION|BINDING|Paper Binding Edge +CHOICE|short|Short edge +CHOICE|long|Long edge +CHOICE|(empty)|Default printer setting +DEFAULT|(empty) +ENDGROUP| +GROUP|text|File Conversion Settings +OPTION|ASCII_HEADER|Text Header|BOOLEAN +CHOICE|true|Enabled +CHOICE|false|Disabled +DEFAULT|false +OPTION|ASCII_BORDER|Text Border|BOOLEAN +CHOICE|true|Enabled +CHOICE|false|Disabled +DEFAULT|false +OPTION|PRINT_DVI|DVI file converter|STRING +ENDGROUP| +GROUP|gs|Ghostscript Settings +OPTION|GS_FEATURES|Ghostscript options|STRING +OPTION|PS_INIT|Initialisation file|STRING +OPTION|PS_EXIT|Cleanup file|STRING +OPTION|POST_FILTER_OPTS|Post-Ghostscript filter options|STRING +ENDGROUP| +ENDGROUP| diff --git a/tdeprint/lpr/apsdriver2 b/tdeprint/lpr/apsdriver2 new file mode 100644 index 000000000..69d355f6c --- /dev/null +++ b/tdeprint/lpr/apsdriver2 @@ -0,0 +1,83 @@ +GROUP|general|General +OPTION|PageSize|Page Size +CHOICE|a3|A3 +CHOICE|a4|A4 +CHOICE|legal|US Legal +CHOICE|letter|US Letter +CHOICE|ledger|Ledger +CHOICE|tabloid|Tabloid +DEFAULT|a4 +OPTION|MEDIA|Paper Type +CHOICE|plain|Plain paper +CHOICE|coated|Coated (InkJet) paper +CHOICE|glossy|Glossy paper +CHOICE|premium|Premium (Photo) paper +CHOICE|trans|Transparencies +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|PAPERTRAY|Input Tray +CHOICE|tray0|Tray 0 +CHOICE|tray1|Tray 1 +CHOICE|tray2|Tray 2 +CHOICE|tray3|Tray 3 +CHOICE|tray4|Tray 4 +CHOICE|tray5|Tray 5 +CHOICE|(empty)|Default setting +DEFAULT|(empty) +ENDGROUP| +GROUP|others|Others +OPTION|COLOR|Color Mode +CHOICE|color|Color +CHOICE|gray|Grayscale +CHOICE|mono|Monochrome +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|DUPLEX|Duplex Printing +CHOICE|duplex|Enabled +CHOICE|simplex|Disabled +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|BINDING|Paper Binding Edge +CHOICE|shortbind|Short edge +CHOICE|longbind|Long edge +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|QUALITY|Printing Quality +CHOICE|draft|Draft +CHOICE|low|Low +CHOICE|medium|Medium +CHOICE|high|High +CHOICE|photo|Photo +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|PS_NUP|Pages per Sheet +CHOICE|1pps|1 +CHOICE|2pps|2 +CHOICE|4pps|4 +CHOICE|8pps|8 +DEFAULT|1pps +OPTION|SWEEP|Communication Type +CHOICE|uni|Unidirectional +CHOICE|bi|Bidirectional +CHOICE|(empty)|Default setting +DEFAULT|(empty) +ENDGROUP| +GROUP|text|Text Printing +OPTION|ASCII_FILTER|Text Converter +CHOICE|a2ps|a2ps +CHOICE|mpage|mpage +CHOICE|enscript|enscript +CHOICE|recode|recode +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|ASCII_HEADER|Header +CHOICE|header|Enabled +CHOICE|noheader|Disabled +CHOICE|(empty)|Default setting +DEFAULT|(empty) +OPTION|ASCII_BORDER|Border +CHOICE|border|Enabled +CHOICE|noborder|Disabled +CHOICE|(empty)|Default setting +DEFAULT|(empty) +ENDGROUP| diff --git a/tdeprint/lpr/apshandler.cpp b/tdeprint/lpr/apshandler.cpp new file mode 100644 index 000000000..d4107d7c7 --- /dev/null +++ b/tdeprint/lpr/apshandler.cpp @@ -0,0 +1,405 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "apshandler.h" +#include "driver.h" +#include "printcapentry.h" +#include "kmprinter.h" +#include "lprsettings.h" +#include "kmmanager.h" +#include "util.h" +#include "kprinter.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +ApsHandler::ApsHandler(KMManager *mgr) +: LprHandler("apsfilter", mgr) +{ + m_counter = 1; +} + +bool ApsHandler::validate(PrintcapEntry *entry) +{ + return (entry->field("if").right(9) == "apsfilter"); +} + +KMPrinter* ApsHandler::createPrinter(PrintcapEntry *entry) +{ + entry->comment = TQString::tqfromLatin1("# APS%1_BEGIN:printer%2").arg(m_counter).arg(m_counter); + entry->postcomment = TQString::tqfromLatin1("# APS%1_END - don't delete this").arg(m_counter); + m_counter++; + return LprHandler::createPrinter(entry); +} + +bool ApsHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool shortmode) +{ + if (LprHandler::completePrinter(prt, entry, shortmode)) + { + if (!shortmode) + { + TQMap opts = loadResources(entry); + if (opts.contains("PRINTER")) + { + prt->setDescription(i18n("APS Driver (%1)").arg(opts["PRINTER"])); + prt->setDriverInfo(prt->description()); + } + } + if (prt->device().isEmpty()) + { + TQString prot; + TQString smbname(sysconfDir() + "/" + prt->printerName() + "/smbclient.conf"); + TQString ncpname(sysconfDir() + "/" + prt->printerName() + "/netware.conf"); + if (TQFile::exists(smbname)) + { + TQMap opts = loadVarFile(smbname); + if (opts.count() == 0) + prt->setDevice("smb:///"); + else + { + prt->setDevice(buildSmbURI( + opts[ "SMB_WORKGROUP" ], + opts[ "SMB_SERVER" ], + opts[ "SMB_PRINTER" ], + opts[ "SMB_USER" ], + opts[ "SMB_PASSWD" ] ) ); + } + prot = "smb"; + } + else if (TQFile::exists(ncpname)) + { + TQMap opts = loadVarFile(ncpname); + if (opts.count() == 0) + prt->setDevice("ncp:///"); + else + { + TQString uri = buildSmbURI( + TQString::null, + opts[ "NCP_SERVER" ], + opts[ "NCP_PRINTER" ], + opts[ "NCP_USER" ], + opts[ "NCP_PASSWD" ] ); + uri.replace( 0, 3, "ncp" ); + prt->setDevice(uri); + } + prot = "ncp"; + } + if (!prt->device().isEmpty()) + prt->setLocation(i18n("Network printer (%1)").arg(prot)); + } + return true; + } + return false; +} + +TQString ApsHandler::sysconfDir() +{ + return TQFile::encodeName("/etc/apsfilter"); +} + +TQString ApsHandler::shareDir() +{ + return driverDirectory(); +} + +TQString ApsHandler::driverDirInternal() +{ + return locateDir("apsfilter/setup", "/usr/share:/usr/local/share:/opt/share"); +} + +TQMap ApsHandler::loadResources(PrintcapEntry *entry) +{ + return loadVarFile(sysconfDir() + "/" + (entry ? entry->name : TQString::null) + "/apsfilterrc"); +} + +TQMap ApsHandler::loadVarFile(const TQString& filename) +{ + TQMap opts; + TQFile f(filename); + if (f.open(IO_ReadOnly)) + { + TQTextStream t(&f); + TQString line; + int p(-1); + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.isEmpty() || line[0] == '#' || (p = line.find('=')) == -1) + continue; + TQString variable = line.left(p).stripWhiteSpace(); + TQString value = line.mid(p+1).stripWhiteSpace(); + if (!value.isEmpty() && value[0] == '\'') + value = value.mid(1, value.length()-2); + opts[variable] = value; + } + } + return opts; +} + +DrMain* ApsHandler::loadDriver(KMPrinter *prt, PrintcapEntry *entry, bool config) +{ + DrMain *driver = loadApsDriver(config); + if (driver /* && config */ ) // Load resources in all case, to get the correct page size + { + TQMap opts = loadResources(entry); + if ( !config && opts.contains( "PAPERSIZE" ) ) + { + // this is needed to keep applications informed + // about the current selected page size + opts[ "PageSize" ] = opts[ "PAPERSIZE" ]; + + // default page size needs to be set to the actual + // value of the printer driver, otherwise it's blocked + // to A4 + DrBase *opt = driver->findOption( "PageSize" ); + if ( opt ) + opt->set( "default", opts[ "PageSize" ] ); + } + driver->setOptions(opts); + driver->set("gsdriver", opts["PRINTER"]); + } + return driver; +} + +DrMain* ApsHandler::loadDbDriver(const TQString& s) +{ + int p = s.find('/'); + DrMain *driver = loadApsDriver(true); + if (driver) + driver->set("gsdriver", s.mid(p+1)); + return driver; +} + +DrMain* ApsHandler::loadApsDriver(bool config) +{ + DrMain *driver = loadToolDriver(locate("data", (config ? "tdeprint/apsdriver1" : "tdeprint/apsdriver2"))); + if (driver) + driver->set("text", "APS Common Driver"); + return driver; +} + +void ApsHandler::reset() +{ + m_counter = 1; +} + +PrintcapEntry* ApsHandler::createEntry(KMPrinter *prt) +{ + TQString prot = prt->deviceProtocol(); + if (prot != "parallel" && prot != "lpd" && prot != "smb" && prot != "ncp") + { + manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot)); + return NULL; + } + TQString path = sysconfDir() + "/" + prt->printerName(); + if (!KStandardDirs::makeDir(path, 0755)) + { + manager()->setErrorMsg(i18n("Unable to create directory %1.").arg(path)); + return NULL; + } + if (prot == "smb" || prot == "ncp") + { + // either "smb" or "ncp" + TQFile::remove(path + "/smbclient.conf"); + TQFile::remove(path + "/netware.conf"); + TQFile f; + if (prot == "smb") + { + f.setName(path + "/smbclient.conf"); + if (f.open(IO_WriteOnly)) + { + TQTextStream t(&f); + TQString work, server, printer, user, passwd; + if ( splitSmbURI( prt->device(), work, server, printer, user, passwd ) ) + { + if (work.isEmpty()) + { + manager()->setErrorMsg(i18n("Missing element: %1.").arg("Workgroup")); + return NULL; + } + t << "SMB_SERVER='" << server << "'" << endl; + t << "SMB_PRINTER='" << printer << "'" << endl; + t << "SMB_IP=''" << endl; + t << "SMB_WORKGROUP='" << work << "'" << endl; + t << "SMB_BUFFER=1400" << endl; + t << "SMB_FLAGS='-N'" << endl; + if (!user.isEmpty()) + { + t << "SMB_USER='" << user << "'" << endl; + t << "SMB_PASSWD='" << passwd << "'" << endl; + } + } + else + { + manager()->setErrorMsg( i18n( "Invalid printer backend specification: %1" ).arg( prt->device() ) ); + return NULL; + } + } + else + { + manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name())); + return NULL; + } + } + else + { + f.setName(path + "/netware.conf"); + if (f.open(IO_WriteOnly)) + { + TQString work, server, printer, user, passwd; + TQString uri = prt->device(); + uri.replace( 0, 3, "smb" ); + if ( splitSmbURI( uri, work, server, printer, user, passwd ) ) + { + TQTextStream t(&f); + t << "NCP_SERVER='" << server << "'" << endl; + t << "NCP_PRINTER='" << printer << "'" << endl; + if (!user.isEmpty()) + { + t << "NCP_USER='" << user << "'" << endl; + t << "NCP_PASSWD='" << passwd << "'" << endl; + } + } + else + { + manager()->setErrorMsg( i18n( "Invalid printer backend specification: %1" ).arg( prt->device() ) ); + return NULL; + } + } + else + { + manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name())); + return NULL; + } + } + // change file permissions + ::chmod(TQFile::encodeName(f.name()).data(), S_IRUSR|S_IWUSR); + } + PrintcapEntry *entry = LprHandler::createEntry(prt); + if (!entry) + { + entry = new PrintcapEntry; + entry->addField("lp", Field::String, "/dev/null"); + } + TQString sd = LprSettings::self()->baseSpoolDir() + "/" + prt->printerName(); + entry->addField("af", Field::String, sd + "/acct"); + entry->addField("lf", Field::String, sd + "/log"); + entry->addField("if", Field::String, sysconfDir() + "/basedir/bin/apsfilter"); + entry->comment = TQString::tqfromLatin1("# APS%1_BEGIN:printer%2").arg(m_counter).arg(m_counter); + entry->postcomment = TQString::tqfromLatin1("# APS%1_END").arg(m_counter); + m_counter++; + return entry; +} + +bool ApsHandler::savePrinterDriver(KMPrinter *prt, PrintcapEntry *entry, DrMain *driver, bool*) +{ + if (driver->get("gsdriver").isEmpty()) + { + manager()->setErrorMsg(i18n("The APS driver is not defined.")); + return false; + } + TQFile f(sysconfDir() + "/" + prt->printerName() + "/apsfilterrc"); + if (f.open(IO_WriteOnly)) + { + TQTextStream t(&f); + t << "# File generated by KDEPrint" << endl; + t << "PRINTER='" << driver->get("gsdriver") << "'" << endl; + TQValueStack stack; + stack.push(driver); + while (stack.count() > 0) + { + DrGroup *grp = stack.pop(); + TQPtrListIterator git(grp->groups()); + for (; git.current(); ++git) + stack.push(git.current()); + TQPtrListIterator oit(grp->options()); + TQString value; + for (; oit.current(); ++oit) + { + value = oit.current()->valueText(); + switch (oit.current()->type()) + { + case DrBase::Boolean: + if (value == "true") + t << oit.current()->name() << "='" << value << "'" << endl; + break; + case DrBase::List: + if (value != "(empty)") + t << oit.current()->name() << "='" << value << "'" << endl; + break; + case DrBase::String: + if (!value.isEmpty()) + t << oit.current()->name() << "='" << value << "'" << endl; + break; + default: + break; + } + } + } + return true; + } + else + { + manager()->setErrorMsg(i18n("Unable to create the file %1.").arg(f.name())); + return false; + } +} + +bool ApsHandler::removePrinter(KMPrinter*, PrintcapEntry *entry) +{ + TQString path(sysconfDir() + "/" + entry->name); + TQFile::remove(path + "/smbclient.conf"); + TQFile::remove(path + "/netware.conf"); + TQFile::remove(path + "/apsfilterrc"); + if (!TQDir(path).rmdir(path)) + { + manager()->setErrorMsg(i18n("Unable to remove directory %1.").arg(path)); + return false; + } + return true; +} + +TQString ApsHandler::printOptions(KPrinter *printer) +{ + TQString optstr; + TQMap opts = printer->options(); + for (TQMap::ConstIterator it=opts.begin(); it!=opts.end(); ++it) + { + if (it.key().startsWith("kde-") || it.key().startsWith("_kde-") || it.key().startsWith( "app-" )) + continue; + optstr.append((*it)).append(":"); + } + if (!optstr.isEmpty()) + { + optstr = optstr.left(optstr.length()-1); + if (LprSettings::self()->mode() == LprSettings::LPR) + optstr.prepend("-C '").append("'"); + else + optstr.prepend("-Z '").append("'"); + } + return optstr; +} diff --git a/tdeprint/lpr/apshandler.h b/tdeprint/lpr/apshandler.h new file mode 100644 index 000000000..db213d194 --- /dev/null +++ b/tdeprint/lpr/apshandler.h @@ -0,0 +1,57 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 APSHANDLER_H +#define APSHANDLER_H + +#include "lprhandler.h" + +#include + +class ApsHandler : public LprHandler +{ +public: + ApsHandler(KMManager*); + + bool validate(PrintcapEntry*); + KMPrinter* createPrinter(PrintcapEntry*); + bool completePrinter(KMPrinter*, PrintcapEntry*, bool = true); + DrMain* loadDriver(KMPrinter*, PrintcapEntry*, bool = false); + DrMain* loadDbDriver(const TQString&); + void reset(); + PrintcapEntry* createEntry(KMPrinter*); + bool savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool* = 0); + bool removePrinter(KMPrinter*, PrintcapEntry*); + TQString printOptions(KPrinter*); + +protected: + TQString driverDirInternal(); + +private: + TQMap loadResources(PrintcapEntry*); + TQMap loadVarFile(const TQString&); + TQString sysconfDir(); + TQString shareDir(); + DrMain* loadApsDriver(bool = false); + +private: + int m_counter; +}; + +#endif diff --git a/tdeprint/lpr/editentrydialog.cpp b/tdeprint/lpr/editentrydialog.cpp new file mode 100644 index 000000000..6c4ecc30c --- /dev/null +++ b/tdeprint/lpr/editentrydialog.cpp @@ -0,0 +1,157 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "editentrydialog.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +EditEntryDialog::EditEntryDialog(PrintcapEntry *entry, TQWidget *parent, const char *name) +: KDialogBase(parent, name, true, TQString::null, Ok|Cancel) +{ + TQWidget *w = new TQWidget(this); + setMainWidget(w); + + TQLabel *lab0 = new TQLabel(i18n("Aliases:"), w); + m_aliases = new TQLineEdit(w); + m_view = new KListView(w); + m_view->addColumn(""); + m_view->header()->hide(); + m_type = new TQComboBox(w); + m_type->insertItem(i18n("String")); + m_type->insertItem(i18n("Number")); + m_type->insertItem(i18n("Boolean")); + m_stack = new TQWidgetStack(w); + m_boolean = new TQCheckBox(i18n("Enabled"), m_stack); + m_string = new TQLineEdit(m_stack); + m_number = new TQSpinBox(0, 9999, 1, m_stack); + m_stack->addWidget(m_string, 0); + m_stack->addWidget(m_boolean, 2); + m_stack->addWidget(m_number, 1); + m_name = new TQLineEdit(w); + + TQVBoxLayout *l0 = new TQVBoxLayout(w, 0, 10); + TQHBoxLayout *l1 = new TQHBoxLayout(0, 0, 10); + TQHBoxLayout *l2 = new TQHBoxLayout(0, 0, 5); + l0->addLayout(l1); + l1->addWidget(lab0); + l1->addWidget(m_aliases); + l0->addWidget(m_view); + l0->addLayout(l2); + l2->addWidget(m_name, 0); + l2->addWidget(m_type, 0); + l2->addWidget(m_stack, 1); + + if (entry) + { + setCaption(i18n("Printcap Entry: %1").arg(entry->name)); + m_fields = entry->fields; + m_aliases->setText(entry->aliases.join("|")); + TQListViewItem *root = new TQListViewItem(m_view, entry->name), *item = 0; + root->setSelectable(false); + root->setOpen(true); + root->setPixmap(0, SmallIcon("fileprint")); + for (TQMap::ConstIterator it=m_fields.begin(); it!=m_fields.end(); ++it) + item = new TQListViewItem(root, item, (*it).toString(), it.key()); + } + + m_block = true; + enableButton(Ok, false); + slotItemSelected(NULL); + slotTypeChanged(0); + m_block = false; + + connect(m_view, TQT_SIGNAL(selectionChanged(TQListViewItem*)), TQT_SLOT(slotItemSelected(TQListViewItem*))); + connect(m_string, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(slotChanged())); + connect(m_boolean, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotChanged())); + connect(m_number, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotChanged())); + connect(m_type, TQT_SIGNAL(activated(int)), TQT_SLOT(slotTypeChanged(int))); + connect(m_name, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(slotChanged())); + + resize(500,400); +} + +Field EditEntryDialog::createField() +{ + Field f; + f.name = m_name->text(); + f.type = (Field::Type)(m_type->currentItem()); + switch (f.type) + { + case Field::String: f.value = m_string->text(); break; + case Field::Integer: f.value = m_number->cleanText(); break; + case Field::Boolean: f.value = (m_boolean->isChecked() ? "1" : "0"); break; + } + return f; +} + +void EditEntryDialog::slotChanged() +{ + if (!m_block && m_view->currentItem()) + { + Field f = createField(); + if (f.name != m_current) + m_fields.remove(m_current); + m_fields[f.name] = f; + m_view->currentItem()->setText(0, f.toString()); + } +} + +void EditEntryDialog::slotItemSelected(TQListViewItem *item) +{ + m_stack->setEnabled(item); + m_name->setEnabled(item); + m_type->setEnabled(item); + if (item) + { + m_block = true; + m_current = item->text(1); + Field f = m_fields[m_current]; + m_name->setText(f.name); + m_type->setCurrentItem(f.type); + slotTypeChanged(f.type); + m_string->setText(f.value); + m_number->setValue(f.value.toInt()); + m_boolean->setChecked(f.value.toInt() == 1); + m_block = false; + } +} + +void EditEntryDialog::fillEntry(PrintcapEntry *entry) +{ + entry->aliases = TQStringList::split('|', m_aliases->text(), false); + entry->fields = m_fields; +} + +void EditEntryDialog::slotTypeChanged(int ID) +{ + m_stack->raiseWidget(ID); + slotChanged(); +} + +#include "editentrydialog.moc" diff --git a/tdeprint/lpr/editentrydialog.h b/tdeprint/lpr/editentrydialog.h new file mode 100644 index 000000000..f4359dd89 --- /dev/null +++ b/tdeprint/lpr/editentrydialog.h @@ -0,0 +1,62 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 EDITENTRYDIALOG_H +#define EDITENTRYDIALOG_H + +#include +#include "printcapentry.h" + +class TQLineEdit; +class TQCheckBox; +class TQSpinBox; +class TQComboBox; +class TQListView; +class TQListviewItem; +class TQWidgetStack; + +class EditEntryDialog : public KDialogBase +{ + Q_OBJECT +public: + EditEntryDialog(PrintcapEntry *entry, TQWidget *parent = 0, const char *name = 0); + + void fillEntry(PrintcapEntry *entry); + +protected slots: + void slotItemSelected(TQListViewItem*); + void slotChanged(); + void slotTypeChanged(int); + +protected: + Field createField(); + +private: + TQMap m_fields; + TQLineEdit *m_name, *m_string, *m_aliases; + TQCheckBox *m_boolean; + TQComboBox *m_type; + TQSpinBox *m_number; + TQListView *m_view; + TQWidgetStack *m_stack; + TQString m_current; + bool m_block; +}; + +#endif diff --git a/tdeprint/lpr/klprfactory.cpp b/tdeprint/lpr/klprfactory.cpp new file mode 100644 index 000000000..4764dfc6a --- /dev/null +++ b/tdeprint/lpr/klprfactory.cpp @@ -0,0 +1,29 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "kmlprmanager.h" +#include "kmlpruimanager.h" +#include "kmlprjobmanager.h" +#include "klprprinterimpl.h" + +#include + +typedef K_TYPELIST_4( KMLprManager, KMLprUiManager, KMLprJobManager, KLprPrinterImpl ) Products; +K_EXPORT_COMPONENT_FACTORY( tdeprint_lpr, KGenericFactory< Products > ) + diff --git a/tdeprint/lpr/klprprinterimpl.cpp b/tdeprint/lpr/klprprinterimpl.cpp new file mode 100644 index 000000000..70356f23c --- /dev/null +++ b/tdeprint/lpr/klprprinterimpl.cpp @@ -0,0 +1,59 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "klprprinterimpl.h" +#include "kprinter.h" +#include "kmlprmanager.h" + +#include +#include +#include + +KLprPrinterImpl::KLprPrinterImpl(TQObject *parent, const char *name, const TQStringList & /*args*/) +: KPrinterImpl(parent,name) +{ + m_exepath = KStandardDirs::findExe("lpr"); +} + +KLprPrinterImpl::~KLprPrinterImpl() +{ +} + +bool KLprPrinterImpl::setupCommand(TQString& cmd, KPrinter *printer) +{ + // check printer object + if (!printer || m_exepath.isEmpty()) + return false; + + cmd = TQString::tqfromLatin1("%1 -P %1 '-#%1'").arg(m_exepath).arg(quote(printer->printerName())).arg( printer->numCopies() ); + QString opts = static_cast(KMManager::self())->printOptions(printer); + if (!opts.isEmpty()) + cmd += (" " + opts); + return true; +} + +void KLprPrinterImpl::broadcastOption(const TQString& key, const TQString& value) +{ + KPrinterImpl::broadcastOption(key,value); + if (key == "kde-pagesize") + { + QString pagename = TQString::tqfromLatin1(pageSizeToPageName((KPrinter::PageSize)value.toInt())); + KPrinterImpl::broadcastOption("PageSize",pagename); + } +} diff --git a/tdeprint/lpr/klprprinterimpl.h b/tdeprint/lpr/klprprinterimpl.h new file mode 100644 index 000000000..508173f1d --- /dev/null +++ b/tdeprint/lpr/klprprinterimpl.h @@ -0,0 +1,38 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 KLPRPRINTERIMPL_H +#define KLPRPRINTERIMPL_H + +#include "kprinterimpl.h" + +class KLprPrinterImpl : public KPrinterImpl +{ +public: + KLprPrinterImpl(TQObject *parent, const char *name, const TQStringList & /*args*/); + ~KLprPrinterImpl(); + + bool setupCommand(TQString&, KPrinter*); + void broadcastOption(const TQString& key, const TQString& value); + +private: + QString m_exepath; +}; + +#endif diff --git a/tdeprint/lpr/kmconfiglpr.cpp b/tdeprint/lpr/kmconfiglpr.cpp new file mode 100644 index 000000000..f43a8da2d --- /dev/null +++ b/tdeprint/lpr/kmconfiglpr.cpp @@ -0,0 +1,66 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "kmconfiglpr.h" +#include "lprsettings.h" + +#include +#include +#include +#include +#include +#include + +KMConfigLpr::KMConfigLpr(TQWidget *parent, const char *name) +: KMConfigPage(parent, name) +{ + setPageName(i18n("Spooler")); + setPageHeader(i18n("Spooler Settings")); + setPagePixmap("gear"); + + TQGroupBox *m_modebox = new TQGroupBox(1, Qt::Vertical, i18n("Spooler"), this); + + m_mode = new TQComboBox(m_modebox); + m_mode->insertItem("LPR (BSD compatible)"); + m_mode->insertItem("LPRng"); + + TQVBoxLayout *l0 = new TQVBoxLayout(this, 5, 10); + l0->addWidget(m_modebox); + l0->addStretch(1); +} + +void KMConfigLpr::loadConfig(KConfig*) +{ + m_mode->setCurrentItem(LprSettings::self()->mode()); +} + +void KMConfigLpr::saveConfig(KConfig *conf) +{ + LprSettings::self()->setMode((LprSettings::Mode)(m_mode->currentItem())); + + TQString modestr; + switch (m_mode->currentItem()) + { + default: + case 0: modestr = "LPR"; break; + case 1: modestr = "LPRng"; break; + } + conf->setGroup("LPR"); + conf->writeEntry("Mode", modestr); +} diff --git a/tdeprint/lpr/kmconfiglpr.h b/tdeprint/lpr/kmconfiglpr.h new file mode 100644 index 000000000..7f3d50857 --- /dev/null +++ b/tdeprint/lpr/kmconfiglpr.h @@ -0,0 +1,39 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 KMCONFIGLPR_H +#define KMCONFIGLPR_H + +#include "kmconfigpage.h" + +class TQComboBox; + +class KMConfigLpr : public KMConfigPage +{ +public: + KMConfigLpr(TQWidget *parent = 0, const char *name = 0); + + void loadConfig(KConfig*); + void saveConfig(KConfig*); + +private: + TQComboBox *m_mode; +}; + +#endif diff --git a/tdeprint/lpr/kmlprjobmanager.cpp b/tdeprint/lpr/kmlprjobmanager.cpp new file mode 100644 index 000000000..851663664 --- /dev/null +++ b/tdeprint/lpr/kmlprjobmanager.cpp @@ -0,0 +1,90 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "kmlprjobmanager.h" +#include "kmlprmanager.h" +#include "lpqhelper.h" +#include "lpchelper.h" +#include "kmjob.h" +#include "lprsettings.h" + +#include +#include + +KMLprJobManager::KMLprJobManager(TQObject *parent, const char *name, const TQStringList & /*args*/) +: KMJobManager(parent, name) +{ + m_lpqhelper = new LpqHelper(this, "LpqHelper"); +} + +bool KMLprJobManager::listJobs(const TQString& prname, JobType, int limit) +{ + TQPtrList jobList; + jobList.setAutoDelete(false); + m_lpqhelper->listJobs(jobList, prname, limit); + TQPtrListIterator it(jobList); + for (; it.current(); ++it) + addJob(it.current()); + return false; +} + +LpcHelper* KMLprJobManager::lpcHelper() +{ + return static_cast(KMManager::self())->lpcHelper(); +} + +int KMLprJobManager::actions() +{ + if (LprSettings::self()->mode() == LprSettings::LPR) + return KMJob::Remove; + else + // some additional actions to be added here + return (KMJob::Remove | KMJob::Hold | KMJob::Resume); +} + +bool KMLprJobManager::sendCommandSystemJob(const TQPtrList& jobs, int action, const TQString& arg) +{ + TQString msg; + TQPtrListIterator it(jobs); + bool status(true); + LpcHelper *helper = lpcHelper(); + + for (; it.current() && status; ++it) + { + switch (action) + { + case KMJob::Remove: + status = helper->removeJob(it.current(), msg); + break; + case KMJob::Hold: + status = helper->changeJobState(it.current(), KMJob::Held, msg); + break; + case KMJob::Resume: + status = helper->changeJobState(it.current(), KMJob::Queued, msg); + break; + default: + status = false; + msg = i18n("Unsupported operation."); + break; + } + } + if (!status && !msg.isEmpty()) + KMManager::self()->setErrorMsg(msg); + return status; +} diff --git a/tdeprint/lpr/kmlprjobmanager.h b/tdeprint/lpr/kmlprjobmanager.h new file mode 100644 index 000000000..d9a8eae3f --- /dev/null +++ b/tdeprint/lpr/kmlprjobmanager.h @@ -0,0 +1,44 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 KMLPRJOBMANAGER_H +#define KMLPRJOBMANAGER_H + +#include "kmjobmanager.h" + +class LpqHelper; +class LpcHelper; + +class KMLprJobManager : public KMJobManager +{ +public: + KMLprJobManager(TQObject *parent, const char *name, const TQStringList & /*args*/); + + int actions(); + +protected: + bool listJobs(const TQString&, JobType, int = 0); + LpcHelper* lpcHelper(); + bool sendCommandSystemJob(const TQPtrList&, int, const TQString& = TQString::null); + +private: + LpqHelper *m_lpqhelper; +}; + +#endif diff --git a/tdeprint/lpr/kmlprmanager.cpp b/tdeprint/lpr/kmlprmanager.cpp new file mode 100644 index 000000000..cf4dbbb94 --- /dev/null +++ b/tdeprint/lpr/kmlprmanager.cpp @@ -0,0 +1,492 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "kmlprmanager.h" +#include "printcapreader.h" +#include "printcapentry.h" +#include "lpchelper.h" +#include "matichandler.h" +#include "apshandler.h" +#include "lprngtoolhandler.h" +#include "lprsettings.h" +#include "driver.h" +#include "editentrydialog.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +KMLprManager::KMLprManager(TQObject *parent, const char *name, const TQStringList & /*args*/) +: KMManager(parent,name) +{ + m_handlers.setAutoDelete(true); + m_handlerlist.setAutoDelete(false); + m_entries.setAutoDelete(true); + + m_lpchelper = new LpcHelper(this); + m_currentprinter = 0; + + setHasManagement(getuid() == 0); + setPrinterOperationMask( + KMManager::PrinterEnabling | + KMManager::PrinterConfigure | + KMManager::PrinterTesting | + KMManager::PrinterCreation | + KMManager::PrinterRemoval | + KMManager::PrinterTesting + ); + + initHandlers(); +} + +void KMLprManager::listPrinters() +{ + TQFileInfo fi(LprSettings::self()->printcapFile()); + + if (m_lpchelper) + m_lpchelper->updateStates(); + + // update only if needed + if (!m_updtime.isValid() || m_updtime < fi.lastModified()) + { + // cleanup previous entries + m_entries.clear(); + // notify handlers + TQPtrListIterator hit(m_handlerlist); + for (; hit.current(); ++hit) + hit.current()->reset(); + + // try to open the printcap file and parse it + PrintcapReader reader; + TQFile f(fi.absFilePath()); + PrintcapEntry *entry; + if (f.exists() && f.open(IO_ReadOnly)) + { + reader.setPrintcapFile(&f); + while ((entry = reader.nextEntry()) != NULL) + { + TQPtrListIterator it(m_handlerlist); + for (; it.current(); ++it) + if (it.current()->validate(entry)) + { + KMPrinter *prt = it.current()->createPrinter(entry); + checkPrinterState(prt); + prt->setOption("kde-lpr-handler", it.current()->name()); + addPrinter(prt); + break; + } + m_entries.insert(entry->name, entry); + } + } + + // save update time + m_updtime = fi.lastModified(); + } + else + { + TQPtrListIterator it(m_printers); + for (; it.current(); ++it) + if (!it.current()->isSpecial()) + { + it.current()->setDiscarded(false); + checkPrinterState(it.current()); + } + } +} + +void KMLprManager::insertHandler(LprHandler *handler) +{ + m_handlers.insert(handler->name(), handler); + m_handlerlist.append(handler); + kdDebug() << "Handler: " << handler->name() << endl; +} + +void KMLprManager::initHandlers() +{ + m_handlers.clear(); + m_handlerlist.clear(); + + insertHandler(new MaticHandler(this)); + insertHandler(new ApsHandler(this)); + insertHandler(new LPRngToolHandler(this)); + + // now load external handlers + TQStringList l = KGlobal::dirs()->findAllResources("data", "tdeprint/lpr/*.la"); + for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it) + { + KLibrary *library = KLibLoader::self()->library(TQFile::encodeName(*it)); + if (library) + { + kdDebug() << "loading external handler from " << *it << endl; + LprHandler*(*func)(KMManager*) = (LprHandler*(*)(KMManager*))(library->symbol("create_handler")); + if (func) + insertHandler(func(this)); + else + kdDebug() << "couldn't find the symbol 'create_handler'" << endl; + } + } + + // default handler + insertHandler(new LprHandler("default", this)); +} + +LprHandler* KMLprManager::findHandler(KMPrinter *prt) +{ + TQString handlerstr(prt->option("kde-lpr-handler")); + LprHandler *handler(0); + if (handlerstr.isEmpty() || (handler = m_handlers.find(handlerstr)) == NULL) + { + return NULL; + } + return handler; +} + +PrintcapEntry* KMLprManager::findEntry(KMPrinter *prt) +{ + PrintcapEntry *entry = m_entries.find(prt->printerName()); + if (!entry) + { + return NULL; + } + return entry; +} + +bool KMLprManager::completePrinter(KMPrinter *prt) +{ + LprHandler *handler = findHandler(prt); + PrintcapEntry *entry = findEntry(prt); + if (handler && entry) + return handler->completePrinter(prt, entry, false); + return false; +} + +bool KMLprManager::completePrinterShort(KMPrinter *prt) +{ + LprHandler *handler = findHandler(prt); + PrintcapEntry *entry = findEntry(prt); + if (!handler || !entry) + return false; + + return handler->completePrinter(prt, entry, true); +} + +void KMLprManager::checkPrinterState(KMPrinter *prt) +{ + if (m_lpchelper) + { + KMPrinter::PrinterState st = m_lpchelper->state(prt); + prt->setState(st); + prt->setAcceptJobs(!(st & KMPrinter::Rejecting)); + } + else + { + prt->setState(KMPrinter::Idle); + prt->setAcceptJobs(true); + } +} + +DrMain* KMLprManager::loadPrinterDriver(KMPrinter *prt, bool config) +{ + if (!prt) + return NULL; + + LprHandler *handler = findHandler(prt); + PrintcapEntry *entry = findEntry(prt); + if (handler && entry) + { + DrMain *driver = handler->loadDriver(prt, entry, config); + if (driver) + driver->set("handler", handler->name()); + return driver; + } + return NULL; +} + +DrMain* KMLprManager::loadFileDriver(const TQString& filename) +{ + int p = filename.find('/'); + TQString handler_str = (p != -1 ? filename.left(p) : TQString::tqfromLatin1("default")); + LprHandler *handler = m_handlers.find(handler_str); + if (handler) + { + DrMain *driver = handler->loadDbDriver(filename); + if (driver) + driver->set("handler", handler->name()); + return driver; + } + return NULL; +} + +bool KMLprManager::enablePrinter(KMPrinter *prt, bool state) +{ + TQString msg; + if (!m_lpchelper->enable(prt, state, msg)) + { + setErrorMsg(msg); + return false; + } + return true; +} + +bool KMLprManager::startPrinter(KMPrinter *prt, bool state) +{ + TQString msg; + if (!m_lpchelper->start(prt, state, msg)) + { + setErrorMsg(msg); + return false; + } + return true; +} + +bool KMLprManager::savePrinterDriver(KMPrinter *prt, DrMain *driver) +{ + LprHandler *handler = findHandler(prt); + PrintcapEntry *entry = findEntry(prt); + if (handler && entry) + { + bool mustSave(false); + if (handler->savePrinterDriver(prt, entry, driver, &mustSave)) + { + if (mustSave) + return savePrintcapFile(); + return true; + } + } + return false; +} + +bool KMLprManager::savePrintcapFile() +{ + if (!LprSettings::self()->isLocalPrintcap()) + { + setErrorMsg(i18n("The printcap file is a remote file (NIS). It cannot be written.")); + return false; + } + TQFile f(LprSettings::self()->printcapFile()); + if (f.open(IO_WriteOnly)) + { + TQTextStream t(&f); + TQDictIterator it(m_entries); + for (; it.current(); ++it) + { + it.current()->writeEntry(t); + } + return true; + } + else + { + setErrorMsg(i18n("Unable to save printcap file. Check that " + "you have write permissions for that file.")); + return false; + } +} + +bool KMLprManager::createPrinter(KMPrinter *prt) +{ + // remove existing printcap entry + PrintcapEntry *oldEntry = m_entries.find(prt->printerName()); + + // look for the handler and re-create entry + LprHandler *handler(0); + // To look for the handler, either we base ourselves + // on the driver (1: new printer, 2: modifying driver) + // or we use the handler of the existing printer + // (modifying something else, handler stays the same) + if (prt->driver()) + handler = m_handlers.find(prt->driver()->get("handler")); + else if (oldEntry) + handler = findHandler(prt); + else + handler = m_handlers.find("default"); + if (!handler) + { + setErrorMsg(i18n("Internal error: no handler defined.")); + return false; + } + prt->setOption("kde-lpr-handler", handler->name()); + + // we reload the driver if the printer object doesn't have one + // and there's an old entry (sometimes needed to keep the backend + // like in Foomatic) + if (!prt->driver() && oldEntry) + prt->setDriver(handler->loadDriver(prt, oldEntry, true)); + + TQString sd = LprSettings::self()->baseSpoolDir(); + if (sd.isEmpty()) + { + setErrorMsg(i18n("Couldn't determine spool directory. See options dialog.")); + return false; + } + sd.append("/").append(prt->printerName()); + if (!KStandardDirs::makeDir(sd, 0755)) + { + setErrorMsg(i18n("Unable to create the spool directory %1. Check that you " + "have the required permissions for that operation.").arg(sd)); + return false; + } + PrintcapEntry *entry = handler->createEntry(prt); + if (!entry) + return false; // error should be set in the handler + // old entry can be removed now + m_entries.remove(prt->printerName()); + entry->name = prt->printerName(); + entry->addField("sh", Field::Boolean); + entry->addField("mx", Field::Integer, "0"); + entry->addField("sd", Field::String, sd); + if (!prt->option("kde-aliases").isEmpty()) + entry->aliases += TQStringList::split("|", prt->option("kde-aliases"), false); + + // insert the new entry and save printcap file + m_entries.insert(prt->printerName(), entry); + bool result = savePrintcapFile(); + if (result) + { + if (prt->driver()) + { + result = handler->savePrinterDriver(prt, entry, prt->driver()); + } + + // in case of LPRng, we need to tell the daemon about new printer + if (LprSettings::self()->mode() == LprSettings::LPRng) + { + TQString msg; + if (!m_lpchelper->restart(msg)) + { + setErrorMsg(i18n("The printer has been created but the print daemon " + "could not be restarted. %1").arg(msg)); + return false; + } + } + } + return result; +} + +bool KMLprManager::removePrinter(KMPrinter *prt) +{ + LprHandler *handler = findHandler(prt); + PrintcapEntry *entry = findEntry(prt); + if (handler && entry) + { + if (handler->removePrinter(prt, entry)) + { + TQString sd = entry->field("sd"); + // first try to save the printcap file, and if + // successful, remove the spool directory + m_entries.take(prt->printerName()); + bool status = savePrintcapFile(); + if (status) + { + // printcap file saved, entry can be deleted now + delete entry; + status = (::system(TQFile::encodeName("rm -rf " + KProcess::quote(sd))) == 0); + if (!status) + setErrorMsg(i18n("Unable to remove spool directory %1. " + "Check that you have write permissions " + "for that directory.").arg(sd)); + return status; + } + else + // push back the non-removed entry + m_entries.insert(prt->printerName(), entry); + } + } + return false; +} + +TQString KMLprManager::driverDbCreationProgram() +{ + return TQString::tqfromLatin1("make_driver_db_lpr"); +} + +TQString KMLprManager::driverDirectory() +{ + TQPtrListIterator it(m_handlerlist); + TQString dbDirs; + for (; it.current(); ++it) + { + TQString dir = it.current()->driverDirectory(); + if (!dir.isEmpty()) + dbDirs.append(dir).append(":"); + } + if (!dbDirs.isEmpty()) + dbDirs.truncate(dbDirs.length()-1); + return dbDirs; +} + +TQString KMLprManager::printOptions(KPrinter *prt) +{ + KMPrinter *mprt = findPrinter(prt->printerName()); + TQString opts; + if (mprt) + { + LprHandler *handler = findHandler(mprt); + if (handler) + return handler->printOptions(prt); + } + return TQString::null; +} + +void KMLprManager::createPluginActions(KActionCollection *coll) +{ + KAction *act = new KAction(i18n("&Edit printcap Entry..."), "tdeprint_report", 0, this, TQT_SLOT(slotEditPrintcap()), coll, "plugin_editprintcap"); + act->setGroup("plugin"); +} + +void KMLprManager::validatePluginActions(KActionCollection *coll, KMPrinter *prt) +{ + m_currentprinter = prt; + // FIXME: disabled until completion + coll->action("plugin_editprintcap")->setEnabled(0 && hasManagement() && prt && !prt->isSpecial()); +} + +void KMLprManager::slotEditPrintcap() +{ + if (!m_currentprinter || + KMessageBox::warningContinueCancel(NULL, + i18n("Editing a printcap entry manually should only be " + "done by confirmed system administrator. This may " + "prevent your printer from working. Do you want to " + "continue?"), TQString::null, KStdGuiItem::cont(), + "editPrintcap") == KMessageBox::Cancel) + return; + + PrintcapEntry *entry = findEntry(m_currentprinter); + EditEntryDialog dlg(entry, NULL); + if (dlg.exec()) + { + } +} + +TQString KMLprManager::stateInformation() +{ + return i18n("Spooler type: %1").arg(LprSettings::self()->mode() == LprSettings::LPR ? "LPR (BSD compatible)" : "LPRng"); +} + +#include "kmlprmanager.moc" diff --git a/tdeprint/lpr/kmlprmanager.h b/tdeprint/lpr/kmlprmanager.h new file mode 100644 index 000000000..34d38340f --- /dev/null +++ b/tdeprint/lpr/kmlprmanager.h @@ -0,0 +1,82 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 KMLRMANAGER_H +#define KMLRMANAGER_H + +#include "kmmanager.h" + +#include +#include +#include +#include + +class LprHandler; +class PrintcapEntry; +class LpcHelper; +class KPrinter; + +class KMLprManager : public KMManager +{ + Q_OBJECT +public: + KMLprManager(TQObject *parent, const char *name, const TQStringList & /*args*/); + + bool completePrinter(KMPrinter*); + bool completePrinterShort(KMPrinter*); + bool enablePrinter(KMPrinter*, bool); + bool startPrinter(KMPrinter*, bool); + bool savePrinterDriver(KMPrinter*, DrMain*); + DrMain* loadPrinterDriver(KMPrinter*, bool = false); + DrMain* loadFileDriver(const TQString&); + bool createPrinter(KMPrinter*); + bool removePrinter(KMPrinter*); + + TQString driverDbCreationProgram(); + TQString driverDirectory(); + + LpcHelper* lpcHelper() { return m_lpchelper; } + TQString printOptions(KPrinter*); + + void createPluginActions(KActionCollection*); + void validatePluginActions(KActionCollection*, KMPrinter*); + TQString stateInformation(); + +protected slots: + void slotEditPrintcap(); + +protected: + void listPrinters(); + void initHandlers(); + void insertHandler(LprHandler*); + PrintcapEntry* findEntry(KMPrinter*); + LprHandler* findHandler(KMPrinter*); + void checkPrinterState(KMPrinter*); + bool savePrintcapFile(); + +private: + TQDict m_handlers; + TQPtrList m_handlerlist; + TQDict m_entries; + TQDateTime m_updtime; + LpcHelper *m_lpchelper; + KMPrinter *m_currentprinter; +}; + +#endif diff --git a/tdeprint/lpr/kmlpruimanager.cpp b/tdeprint/lpr/kmlpruimanager.cpp new file mode 100644 index 000000000..655309e0c --- /dev/null +++ b/tdeprint/lpr/kmlpruimanager.cpp @@ -0,0 +1,67 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "kmlpruimanager.h" +#include "kmpropertypage.h" +#include "kprinterpropertydialog.h" +#include "kpqtpage.h" +#include "kmconfigdialog.h" + +#include "kmpropdriver.h" +#include "kmpropbackend.h" +#include "kmwizard.h" +#include "kmwbackend.h" +#include "kmconfiglpr.h" + +#include + +KMLprUiManager::KMLprUiManager(TQObject *parent, const char *name, const TQStringList & /*args*/) +: KMUiManager(parent,name) +{ +} + +KMLprUiManager::~KMLprUiManager() +{ +} + +void KMLprUiManager::setupPropertyPages(KMPropertyPage *pages) +{ + pages->addPropPage(new KMPropBackend(pages, "Backend")); + pages->addPropPage(new KMPropDriver(pages, "Driver")); +} + +void KMLprUiManager::setupPrinterPropertyDialog(KPrinterPropertyDialog *dlg) +{ + dlg->addPage(new KPQtPage(dlg->driver(), dlg, "QtPage")); +} + +void KMLprUiManager::setupWizard(KMWizard *wizard) +{ + KMWBackend *backend = wizard->backendPage(); + + backend->addBackend(KMWizard::Local, true); + backend->addBackend(KMWizard::LPD, true); + backend->addBackend(KMWizard::TCP, true); + backend->addBackend(KMWizard::SMB, true, KMWizard::Password); +} + +void KMLprUiManager::setupConfigDialog(KMConfigDialog *dlg) +{ + dlg->addConfigPage(new KMConfigLpr(dlg)); +} diff --git a/tdeprint/lpr/kmlpruimanager.h b/tdeprint/lpr/kmlpruimanager.h new file mode 100644 index 000000000..561f93500 --- /dev/null +++ b/tdeprint/lpr/kmlpruimanager.h @@ -0,0 +1,37 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 KMLPRUIMANAGER_H +#define KMLPRUIMANAGER_H + +#include "kmuimanager.h" + +class KMLprUiManager : public KMUiManager +{ +public: + KMLprUiManager(TQObject *parent, const char *name, const TQStringList & /*args*/); + ~KMLprUiManager(); + + void setupPropertyPages(KMPropertyPage*); + void setupPrinterPropertyDialog(KPrinterPropertyDialog*); + void setupWizard(KMWizard*); + void setupConfigDialog(KMConfigDialog*); +}; + +#endif diff --git a/tdeprint/lpr/lpchelper.cpp b/tdeprint/lpr/lpchelper.cpp new file mode 100644 index 000000000..c921a803c --- /dev/null +++ b/tdeprint/lpr/lpchelper.cpp @@ -0,0 +1,320 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "lpchelper.h" +#include "kpipeprocess.h" +#include "kmjob.h" +#include "lprsettings.h" + +#include +#include +#include +#include +#include +#include +#include + +static TQString execute(const TQString& cmd) +{ + KPipeProcess proc; + TQString output; + if (proc.open(cmd)) + { + TQTextStream t(&proc); + while (!t.atEnd()) + output.append(t.readLine()).append("\n"); + proc.close(); + } + return output; +} + +LpcHelper::LpcHelper(TQObject *parent, const char *name) +: TQObject(parent, name) +{ + // look for the "lpc" executable. Use the PATH variable and + // add some specific dirs. + TQString PATH = getenv("PATH"); + PATH.append(":/usr/sbin:/usr/local/sbin:/sbin:/opt/sbin:/opt/local/sbin"); + m_exepath = KStandardDirs::findExe("lpc", PATH); + m_checkpcpath = KStandardDirs::findExe("checkpc", PATH); + m_lprmpath = KStandardDirs::findExe("lprm"); +} + +LpcHelper::~LpcHelper() +{ +} + +KMPrinter::PrinterState LpcHelper::state(const TQString& prname) const +{ + if (m_state.contains(prname)) + return m_state[prname]; + return KMPrinter::Unknown; +} + +KMPrinter::PrinterState LpcHelper::state(KMPrinter *prt) const +{ + return state(prt->printerName()); +} + +void LpcHelper::parseStatusLPR(TQTextStream &t) +{ + TQString printer, line; + int p(-1); + + while (!t.atEnd()) + { + line = t.readLine(); + if (line.isEmpty()) + continue; + else if (!line[0].isSpace() && (p = line.find(':')) != -1) + { + printer = line.left(p); + m_state[printer] = KMPrinter::Idle; + } + else if (line.find("printing is disabled") != -1) + { + if (!printer.isEmpty()) + m_state[printer] = KMPrinter::PrinterState((KMPrinter::Stopped) | (m_state[printer] & ~KMPrinter::StateMask)); + } + else if (line.find("queuing is disabled") != -1) + { + if (!printer.isEmpty()) + m_state[printer] = KMPrinter::PrinterState((KMPrinter::Rejecting) | (m_state[printer] & KMPrinter::StateMask)); + } + else if (line.find("entries") != -1) + { + if (!printer.isEmpty() && + (m_state[printer] & KMPrinter::StateMask) != KMPrinter::Stopped && + line.find("no entries") == -1) + m_state[printer] = KMPrinter::PrinterState((m_state[printer] & ~KMPrinter::StateMask) | KMPrinter::Processing); + } + } +} + +void LpcHelper::parseStatusLPRng(TQTextStream& t) +{ + TQStringList l; + int p(-1); + TQString printer; + + while (!t.atEnd()) + if (t.readLine().stripWhiteSpace().startsWith("Printer")) + break; + while (!t.atEnd()) + { + l = TQStringList::split(TQRegExp("\\s"), t.readLine(), false); + if (l.count() < 4) + continue; + p = l[0].find('@'); + if (p == 0) + printer = l[0]; + else + printer = l[0].left(p); + int st(0); + if (l[1] == "disabled") + st = KMPrinter::Stopped; + else if (l[3] != "0") + st = KMPrinter::Processing; + else + st = KMPrinter::Idle; + if (l[2] == "disabled") + st |= KMPrinter::Rejecting; + m_state[printer] = KMPrinter::PrinterState(st); + } +} + +void LpcHelper::updateStates() +{ + KPipeProcess proc; + + m_state.clear(); + if (!m_exepath.isEmpty() && proc.open(m_exepath + " status all")) + { + TQTextStream t(&proc); + + switch (LprSettings::self()->mode()) + { + default: + case LprSettings::LPR: + parseStatusLPR(t); + break; + case LprSettings::LPRng: + parseStatusLPRng(t); + break; + } + proc.close(); + } + +} + +bool LpcHelper::enable(KMPrinter *prt, bool state, TQString& msg) +{ + int st = m_state[prt->printerName()] & KMPrinter::StateMask; + if (changeState(prt->printerName(), (state ? "enable" : "disable"), msg)) + { + m_state[prt->printerName()] = KMPrinter::PrinterState((state ? KMPrinter::Rejecting : 0) | st); + return true; + } + return false; +} + +bool LpcHelper::start(KMPrinter *prt, bool state, TQString& msg) +{ + int rej = m_state[prt->printerName()] & ~KMPrinter::StateMask; + if (changeState(prt->printerName(), (state ? "start" : "stop"), msg)) + { + m_state[prt->printerName()] = KMPrinter::PrinterState((state ? KMPrinter::Idle : KMPrinter::Stopped) | rej); + return true; + } + return false; +} + +// status +// 0 : success +// -1 : permission denied +// -2 : unknown printer +// 1 : unknown error +int LpcHelper::parseStateChangeLPR(const TQString& result, const TQString& printer) +{ + if (result.startsWith(printer + ":")) + return 0; + else if (result.startsWith("?Privileged")) + return -1; + else if (result.startsWith("unknown")) + return -2; + else + return 1; +} + +static TQString lprngAnswer(const TQString& result, const TQString& printer) +{ + int p, q; + + p = result.find("\n" + printer); + if (p != -1) + { + q = result.find(':', p)+2; + p = result.find('\n', q); + TQString answer = result.mid(q, p-q).stripWhiteSpace(); + return answer; + } + return TQString::null; +} + +int LpcHelper::parseStateChangeLPRng(const TQString& result, const TQString& printer) +{ + TQString answer = lprngAnswer(result, printer); + if (answer == "no") + return -1; + else if (answer == "disabled" || answer == "enabled" || answer == "started" || answer == "stopped") + return 0; + else + return 1; +} + +bool LpcHelper::changeState(const TQString& printer, const TQString& op, TQString& msg) +{ + if (m_exepath.isEmpty()) + { + msg = i18n("The executable %1 couldn't be found in your PATH.").arg("lpc"); + return false; + } + TQString result = execute(m_exepath + " " + op + " " + KProcess::quote(printer)); + int status; + + switch (LprSettings::self()->mode()) + { + default: + case LprSettings::LPR: + status = parseStateChangeLPR(result, printer); + break; + case LprSettings::LPRng: + status = parseStateChangeLPRng(result, printer); + break; + } + switch (status) + { + case 0: + break; + case -1: + msg = i18n("Permission denied."); + break; + case -2: + msg = i18n("Printer %1 does not exist.").arg(printer); + break; + default: + case 1: + msg = i18n("Unknown error: %1").arg(result.replace(TQRegExp("\\n"), " ")); + break; + } + return (status == 0); +} + +bool LpcHelper::removeJob(KMJob *job, TQString& msg) +{ + if (m_lprmpath.isEmpty()) + { + msg = i18n("The executable %1 couldn't be found in your PATH.").arg("lprm"); + return false; + } + TQString result = execute(m_lprmpath + " -P " + KProcess::quote(job->printer()) + " " + TQString::number(job->id())); + if (result.find("dequeued") != -1) + return true; + else if (result.find("Permission denied") != -1 || result.find("no permissions") != -1) + msg = i18n("Permission denied."); + else + msg = i18n("Execution of lprm failed: %1").arg(result); + return false; +} + +// LPRng only +bool LpcHelper::changeJobState(KMJob *job, int state, TQString& msg) +{ + if (m_lprmpath.isEmpty()) + { + msg = i18n("The executable %1 couldn't be found in your PATH.").arg("lpc"); + return false; + } + TQString result = execute(m_exepath + (state == KMJob::Held ? " hold " : " release ") + KProcess::quote(job->printer()) + " " + TQString::number(job->id())); + TQString answer = lprngAnswer(result, job->printer()); + if (answer == "no") + { + msg = i18n("Permission denied."); + return false; + } + else + return true; +} + +bool LpcHelper::restart(TQString& msg) +{ + TQString s; + if (m_exepath.isEmpty()) + s = "lpc"; + else if (m_checkpcpath.isEmpty()) + s = "checkpc"; + if (!s.isEmpty()) + { + msg = i18n("The executable %1 couldn't be found in your PATH.").arg(s); + return false; + } + ::system(TQFile::encodeName(m_exepath + " reread")); + ::system(TQFile::encodeName(m_checkpcpath + " -f")); + return true; +} diff --git a/tdeprint/lpr/lpchelper.h b/tdeprint/lpr/lpchelper.h new file mode 100644 index 000000000..b1e20f089 --- /dev/null +++ b/tdeprint/lpr/lpchelper.h @@ -0,0 +1,59 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 LPCHELPER_H +#define LPCHELPER_H + +#include +#include +#include +#include "kmprinter.h" + +class KMJob; + +class LpcHelper : public TQObject +{ +public: + LpcHelper(TQObject *parent = 0, const char *name = 0); + ~LpcHelper(); + + KMPrinter::PrinterState state(const TQString&) const; + KMPrinter::PrinterState state(KMPrinter*) const; + void updateStates(); + + bool enable(KMPrinter*, bool, TQString&); + bool start(KMPrinter*, bool, TQString&); + bool removeJob(KMJob*, TQString&); + bool changeJobState(KMJob*, int, TQString&); + + bool restart(TQString&); + +protected: + bool changeState(const TQString&, const TQString&, TQString&); + void parseStatusLPR(TQTextStream&); + void parseStatusLPRng(TQTextStream&); + int parseStateChangeLPR(const TQString&, const TQString&); + int parseStateChangeLPRng(const TQString&, const TQString&); + +private: + TQMap m_state; + TQString m_exepath, m_lprmpath, m_checkpcpath; +}; + +#endif diff --git a/tdeprint/lpr/lpqhelper.cpp b/tdeprint/lpr/lpqhelper.cpp new file mode 100644 index 000000000..30348f591 --- /dev/null +++ b/tdeprint/lpr/lpqhelper.cpp @@ -0,0 +1,117 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "lpqhelper.h" +#include "kmjob.h" +#include "kpipeprocess.h" +#include "lprsettings.h" + +#include +#include +#include + +LpqHelper::LpqHelper(TQObject *parent, const char *name) +: TQObject(parent, name) +{ + m_exepath = KStandardDirs::findExe("lpq"); +} + +LpqHelper::~LpqHelper() +{ +} + +KMJob* LpqHelper::parseLineLpr(const TQString& line) +{ + TQString rank = line.left(7); + if (!rank[0].isDigit() && rank != "active") + return NULL; + KMJob *job = new KMJob; + job->setState((rank[0].isDigit() ? KMJob::Queued : KMJob::Printing)); + job->setOwner(line.mid(7, 11).stripWhiteSpace()); + job->setId(line.mid(18, 5).toInt()); + job->setName(line.mid(23, 38).stripWhiteSpace()); + int p = line.find(' ', 61); + if (p != -1) + { + job->setSize(line.mid(61, p-61).toInt() / 1000); + } + return job; +} + +KMJob* LpqHelper::parseLineLPRng(const TQString& line) +{ + TQString rank = line.left(7).stripWhiteSpace(); + if (!rank[0].isDigit() && rank != "active" && rank != "hold") + return NULL; + KMJob *job = new KMJob; + job->setState((rank[0].isDigit() ? KMJob::Queued : (rank == "hold" ? KMJob::Held : KMJob::Printing))); + int p = line.find('@', 7), q = line.find(' ', 7); + job->setOwner(line.mid(7, TQMIN(p,q)-7)); + while (line[q].isSpace()) + q++; + q++; + while (line[q].isSpace()) + q++; + p = line.find(' ', q); + job->setId(line.mid(q, p-q).toInt()); + while (line[p].isSpace()) + p++; + q = p+25; + while (line[q].isDigit()) + q--; + job->setName(line.mid(p, q-p).stripWhiteSpace()); + job->setSize(line.mid(q+1, p+26-q).toInt() / 1000); + return job; +} + +void LpqHelper::listJobs(TQPtrList& jobs, const TQString& prname, int limit) +{ + KPipeProcess proc; + if (!m_exepath.isEmpty() && proc.open(m_exepath + " -P " + KProcess::quote(prname))) + { + TQTextStream t(&proc); + TQString line; + bool lprng = (LprSettings::self()->mode() == LprSettings::LPRng); + int count = 0; + + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.startsWith("Rank")) + break; + } + while (!t.atEnd()) + { + line = t.readLine(); + if ( limit > 0 && count >= limit ) + continue; + KMJob *job = (lprng ? parseLineLPRng(line) : parseLineLpr(line)); + if (job) + { + job->setPrinter(prname); + job->setUri("lpd://"+prname+"/"+TQString::number(job->id())); + jobs.append(job); + count++; + } + else + break; + } + proc.close(); + } +} diff --git a/tdeprint/lpr/lpqhelper.h b/tdeprint/lpr/lpqhelper.h new file mode 100644 index 000000000..2c38566c5 --- /dev/null +++ b/tdeprint/lpr/lpqhelper.h @@ -0,0 +1,44 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 LPQHELPER_H +#define LPQHELPER_H + +#include +#include + +class KMJob; + +class LpqHelper : public TQObject +{ +public: + LpqHelper(TQObject *parent = 0, const char *name = 0); + ~LpqHelper(); + + void listJobs(TQPtrList& jobs, const TQString& prname, int limit = 0); + +protected: + KMJob* parseLineLpr(const TQString&); + KMJob* parseLineLPRng(const TQString&); + +private: + TQString m_exepath; +}; + +#endif diff --git a/tdeprint/lpr/lpr.print b/tdeprint/lpr/lpr.print new file mode 100644 index 000000000..e8cd8a3d7 --- /dev/null +++ b/tdeprint/lpr/lpr.print @@ -0,0 +1,86 @@ +[KDE Print Entry] +PrintSystem=lpr +Comment=LPR/LPRng Print System +Comment[af]= lpr /Lprng Druk Stelsel +Comment[ar]=نظام طباعة LPR/LPRng +Comment[az]=LPR/LPRng Çap Sistemi +Comment[be]=Сістэма друку LPR/LPRng +Comment[bn]=এল-পি-আর/এল-পি-আর-এন-জি মুদ্রণ ব্যবস্থা +Comment[br]=Reizhiad moulañ LPR/LPRng +Comment[bs]=LPR/LPRng sistem štampe +Comment[ca]=Sistema d'impressió LPR/LPRng +Comment[cs]=Tiskový systém LPR/LPRng +Comment[csb]=Systema drëkù LPR/LPRng +Comment[cy]=Cysawd Argraffu LPR/LPRng +Comment[da]=LPR/LPRng-udskriftssystem +Comment[de]=LPR/LPRng-Drucksystem +Comment[el]=LPR/LPRng σύστημα εκτύπωσης +Comment[eo]=LPR/LPRng-presosistemo +Comment[es]=Sistema de impresión LPR/LPRNg +Comment[et]=LPR/LPRng trükkimise süsteem +Comment[eu]=LPR/LPRng inprimatze-sistema +Comment[fa]=سیستم چاپ LPR/LPRng +Comment[fi]=LPR/LPRng-tulostusjärjestelmä +Comment[fr]=Système d'impression LPR / LPRng +Comment[fy]=LPR-/LPRNG-ôfdruksysteem +Comment[ga]=Córas Priontála LPR/LPRng +Comment[gl]=Sistema de Impresión LPR/LPRng +Comment[he]=מערכת ההדפסה LPR/LPRng +Comment[hi]=LPR/LPRng छपाई पद्धत्ति +Comment[hr]=LPR/LPRng sustav za ispis +Comment[hu]=LPR/LPRng +Comment[id]=Sistem Pencetakan LPR/LPRng +Comment[is]=LPR/LPRng prentkerfið +Comment[it]=Sistema di stampa LPR/LPRng +Comment[ja]=LPR/LPRng 印刷システム +Comment[ka]=LPR/LPRng ბეჭდვის სისტემა +Comment[kk]=LPR/LPRng басып шығару жүйесі +Comment[km]=ប្រព័ន្ធ​បោះពុម្ព LPR/LPRng +Comment[ko]=LPR/LPRng 인쇄 시스템 +Comment[lb]=LPR/LPRng-Drécksystem +Comment[lt]=LPR/LPRng spausdinimo sistema +Comment[lv]=LPR/LPRng drukas sistēma +Comment[mk]=LPR/LPRng системот за печатење +Comment[mn]=LPR/LPRng-Хэвлэх систем +Comment[ms]=Sistem Cetak LPR/LPRng +Comment[mt]=Sistema tal-ipprintjar LPR/LPRng +Comment[nb]=Utskriftssystemet LPR/LPRng +Comment[nds]=Dat Drucksysteem LPR/LPRng +Comment[ne]=LPR/LPRng मुद्रण प्रणाली +Comment[nl]=LPR-/LPRNG-afdruksysteem +Comment[nn]=Utskriftssystemet LPR/LPRng +Comment[nso]=System ya Kgatiso ya LPR/LPRng +Comment[pa]=LPR/LPRng ਪ੍ਰਿੰਟਰ ਸਿਸਟਮ +Comment[pl]=System druku LPR/LPRng +Comment[pt]=O sistema de impressão LPR/LPRng +Comment[pt_BR]=Sistema de Impressão LPR/LPRng +Comment[ro]=Sistem de tipărire LPR/LPRng +Comment[ru]=Система печати LPR/LPRng +Comment[rw]=Sisitemu yo Gucapa LPR/LPRng +Comment[se]=LPR/LPRng čálihanvuogádat +Comment[sk]=Tlačový systém LPR/LPRng +Comment[sl]=Tiskalniški sistem za LPR/LPRng +Comment[sq]=Sistem Shtypi për LPR/LPRng +Comment[sr]=LPR/LPRng систем за штампање +Comment[sr@Latn]=LPR/LPRng sistem za štampanje +Comment[ss]=LPR/LPRng umhini wekushivelela +Comment[sv]=LPR/LPRNG-skrivarsystem +Comment[ta]=LPR/LPRng அச்சுத் தொகுதி +Comment[te]=ఎల్ పి ఆర్/ఎల్ పి ఆర్ ఎన్ జి ప్రచురణ వ్యవస్థ +Comment[tg]=Системаи чопи LPR/LPRng +Comment[th]=ระบบการพิมพ์ LPR/LPRng +Comment[tr]=LPR/LPRng Yazdırma Sistemi +Comment[tt]=LPR/LPRng Bastıru Sisteme +Comment[uk]=Система друку LPR/LPRng +Comment[uz]=LPR/LPRng bosib chiqarish tizimi +Comment[uz@cyrillic]=LPR/LPRng босиб чиқариш тизими +Comment[ven]=LPR/Maitele au phirintha a LPRng +Comment[vi]=Hệ thống in LPR/LPRng +Comment[wa]=Sistinme d' imprimaedje LPR/LPRng +Comment[xh]=LPR/LPRng Indlela Yokushicilela +Comment[zh_CN]=LPR/LPRng 打印系统 +Comment[zh_HK]=LPR/LPRng 列印系統 +Comment[zh_TW]=LPR/LPRng 列印系統 +Comment[zu]=Isistimu Yokushicilela ye-LPR/LRng +DetectUris=service:/printer +DetectPrecedence=0 diff --git a/tdeprint/lpr/lprhandler.cpp b/tdeprint/lpr/lprhandler.cpp new file mode 100644 index 000000000..fcb838bb9 --- /dev/null +++ b/tdeprint/lpr/lprhandler.cpp @@ -0,0 +1,270 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "lprhandler.h" +#include "kmprinter.h" +#include "printcapentry.h" +#include "kmmanager.h" +#include "lprsettings.h" +#include "driver.h" + +#include +#include +#include +#include + +#include + +LprHandler::LprHandler(const TQString& name, KMManager *mgr) +: m_name(name), m_manager(mgr) +{ +} + +LprHandler::~LprHandler() +{ +} + +bool LprHandler::validate(PrintcapEntry*) +{ + return true; +} + +KMPrinter* LprHandler::createPrinter(PrintcapEntry *entry) +{ + KMPrinter *prt = new KMPrinter; + prt->setPrinterName(entry->name); + prt->setName(entry->name); + prt->setType(KMPrinter::Printer); + return prt; +} + +bool LprHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool) +{ + prt->setDescription(i18n("Unknown (unrecognized entry)")); + TQString val = entry->field("lp"); + KURL uri; + if (!val.isEmpty() && val != "/dev/null") + { + int p = val.find('@'); + if (p != -1) + { + prt->setLocation(i18n("Remote queue (%1) on %2").arg(val.left(p)).arg(val.mid(p+1))); + uri.setProtocol("lpd"); + uri.setHost(val.mid(p+1)); + uri.setPath("/" + val.left(p)); + } + else if ((p = val.find('%')) != -1) + { + prt->setLocation(i18n("Network printer (%1)").arg("socket")); + uri.setProtocol("socket"); + uri.setHost(val.left(p)); + uri.setPort(val.mid(p+1).toInt()); + } + else + { + prt->setLocation(i18n("Local printer on %1").arg(val)); + uri.setProtocol("parallel"); + uri.setPath(val); + } + } + else if (!(val = entry->field("rp")).isEmpty()) + { + TQString rm = entry->has("rm") ? + entry->field("rm") : + LprSettings::self()->defaultRemoteHost(); + prt->setLocation(i18n("Remote queue (%1) on %2").arg(val).arg(rm)); + uri.setProtocol("lpd"); + uri.setHost(rm); + uri.setPath("/" + val); + } + else + prt->setLocation(i18n("Unknown (unrecognized entry)")); + prt->setDevice(uri.url()); + return true; +} + +DrMain* LprHandler::loadDriver(KMPrinter*, PrintcapEntry*, bool) +{ + manager()->setErrorMsg(i18n("Unrecognized entry.")); + return NULL; +} + +bool LprHandler::savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool*) +{ + manager()->setErrorMsg(i18n("Unrecognized entry.")); + return false; +} + +DrMain* LprHandler::loadDbDriver(const TQString&) +{ + manager()->setErrorMsg(i18n("Unrecognized entry.")); + return NULL; +} + +PrintcapEntry* LprHandler::createEntry(KMPrinter *prt) +{ + // this default handler only supports local parallel and remote lpd URIs + KURL uri ( prt->device() ); + TQString prot = uri.protocol(); + if (!prot.isEmpty() && prot != "parallel" && prot != "file" && prot != "lpd" && prot != "socket") + { + manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot)); + return NULL; + } + PrintcapEntry *entry = new PrintcapEntry; + entry->comment = "# Default handler"; + if (prot == "lpd") + { + entry->addField("rm", Field::String, uri.host()); + TQString rp = uri.path(); + if (rp[0] == '/') + rp = rp.mid(1); + entry->addField("rp", Field::String, rp); + // force this entry to null (otherwise it seems it's redirected + // to /dev/lp0 by default) + entry->addField("lp", Field::String, TQString::null); + } + else if ( prot == "socket" ) + { + TQString lp = uri.host(); + if ( uri.port() == 0 ) + lp.append( "%9100" ); + else + lp.append( "%" ).append( TQString::number( uri.port() ) ); + entry->addField("lp", Field::String, lp); + } + else + { + entry->addField("lp", Field::String, uri.path()); + } + return entry; +} + +bool LprHandler::removePrinter(KMPrinter*, PrintcapEntry*) +{ + return true; +} + +TQString LprHandler::printOptions(KPrinter*) +{ + return TQString::null; +} + +void LprHandler::reset() +{ +} + +DrMain* LprHandler::loadToolDriver(const TQString& filename) +{ + TQFile f(filename); + if (f.open(IO_ReadOnly)) + { + DrMain *driver = new DrMain; + TQValueStack groups; + TQTextStream t(&f); + TQStringList l; + DrListOption *lopt(0); + DrBase *opt(0); + + groups.push(driver); + driver->set("text", "Tool Driver"); + while (!t.atEnd()) + { + l = TQStringList::split('|', t.readLine().stripWhiteSpace(), false); + if (l.count() == 0) + continue; + if (l[0] == "GROUP") + { + DrGroup *grp = new DrGroup; + grp->setName(l[1]); + grp->set("text", l[2]); + groups.top()->addGroup(grp); + groups.push(grp); + } + else if (l[0] == "ENDGROUP") + { + groups.pop(); + } + else if (l[0] == "OPTION") + { + opt = 0; + lopt = 0; + if (l.count() > 3) + { + if (l[3] == "STRING") + opt = new DrStringOption; + else if (l[3] == "BOOLEAN") + { + lopt = new DrBooleanOption; + opt = lopt; + } + } + else + { + lopt = new DrListOption; + opt = lopt; + } + if (opt) + { + opt->setName(l[1]); + opt->set("text", l[2]); + groups.top()->addOption(opt); + } + } + else if (l[0] == "CHOICE" && lopt) + { + DrBase *ch = new DrBase; + ch->setName(l[1]); + ch->set("text", l[2]); + lopt->addChoice(ch); + } + else if (l[0] == "DEFAULT" && opt) + { + opt->setValueText(l[1]); + opt->set("default", l[1]); + } + } + return driver; + } + return NULL; +} + +TQString LprHandler::driverDirectory() +{ + if (m_cacheddriverdir.isEmpty()) + m_cacheddriverdir = driverDirInternal(); + return m_cacheddriverdir; +} + +TQString LprHandler::driverDirInternal() +{ + return TQString::null; +} + +TQString LprHandler::locateDir(const TQString& dirname, const TQString& paths) +{ + TQStringList pathlist = TQStringList::split(':', paths, false); + for (TQStringList::ConstIterator it=pathlist.begin(); it!=pathlist.end(); ++it) + { + TQString testpath = *it + "/" + dirname; + if (::access(TQFile::encodeName(testpath), F_OK) == 0) + return testpath; + } + return TQString::null; +} diff --git a/tdeprint/lpr/lprhandler.h b/tdeprint/lpr/lprhandler.h new file mode 100644 index 000000000..e0e66e146 --- /dev/null +++ b/tdeprint/lpr/lprhandler.h @@ -0,0 +1,88 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 LPRHANDLER_H +#define LPRHANDLER_H + +#if !defined( _KDEPRINT_COMPILE ) && defined( __GNUC__ ) +#warning internal header, do not use except if you are a KDEPrint developer +#endif + +#include + +class PrintcapEntry; +class KMPrinter; +class DrMain; +class KMManager; +class KPrinter; + +/** + * @internal + * This class is internal to KDEPrint and is not intended to be + * used outside it. Please do not make use of this header, except + * if you're a KDEPrint developer. The API might change in the + * future and binary compatibility might be broken. + */ +class LprHandler +{ +public: + LprHandler(const TQString& name, KMManager *mgr = 0); + virtual ~LprHandler(); + + virtual bool validate(PrintcapEntry*); + virtual KMPrinter* createPrinter(PrintcapEntry*); + virtual bool completePrinter(KMPrinter*, PrintcapEntry*, bool shortmode = true); + virtual DrMain* loadDriver(KMPrinter*, PrintcapEntry*, bool = false); + virtual DrMain* loadDbDriver(const TQString&); + virtual bool savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool* = 0); + virtual PrintcapEntry* createEntry(KMPrinter*); + virtual bool removePrinter(KMPrinter*, PrintcapEntry*); + virtual TQString printOptions(KPrinter*); + virtual void reset(); + + TQString name() const; + KMManager* manager() const; + TQString driverDirectory(); + +protected: + DrMain* loadToolDriver(const TQString&); + TQString locateDir(const TQString& dirname, const TQString& paths); + TQString cachedDriverDir() const; + void setCachedDriverDir(const TQString&); + virtual TQString driverDirInternal(); + +protected: + TQString m_name; + KMManager *m_manager; + TQString m_cacheddriverdir; +}; + +inline TQString LprHandler::name() const +{ return m_name; } + +inline KMManager* LprHandler::manager() const +{ return m_manager; } + +inline TQString LprHandler::cachedDriverDir() const +{ return m_cacheddriverdir; } + +inline void LprHandler::setCachedDriverDir(const TQString& s) +{ m_cacheddriverdir = s; } + +#endif diff --git a/tdeprint/lpr/lprngtooldriver1 b/tdeprint/lpr/lprngtooldriver1 new file mode 100644 index 000000000..11636403c --- /dev/null +++ b/tdeprint/lpr/lprngtooldriver1 @@ -0,0 +1,85 @@ +GROUP|general|General +OPTION|PageSize|Page Size +CHOICE|letter|US Letter +CHOICE|legal|US Legal +CHOICE|ledger|Ledger +CHOICE|11x17|11x17 +CHOICE|tabloid|Tabloid +CHOICE|oversize|Oversize +CHOICE|executive|Executive +CHOICE|envelope|Envelope +CHOICE|a0|A0 +CHOICE|a1|A1 +CHOICE|a2|A2 +CHOICE|a3|A3 +CHOICE|a4|A4 +DEFAULT|a4 +OPTION|MediaType|Media Type +CHOICE|bond|Bond paper +CHOICE|cardstock|Card stock +CHOICE|color|Color paper +CHOICE|glossy|Glossy paper +CHOICE|heavy|Heavy paper +CHOICE|labels|Labels +CHOICE|letterhead|Letter head +CHOICE|plain|Plain paper +CHOICE|preprinted|Preprinted paper +CHOICE|prepunched|Prepunched paper +CHOICE|recycle|Recycled paper +CHOICE|transparency|Transparencies +CHOICE|default|Default setting +DEFAULT|default +OPTION|InputSlot|Input Slot +CHOICE|manual|Manual tray +CHOICE|inupper|Upper tray +CHOICE|inmiddle|Middle tray +CHOICE|inlower|Lower tray +CHOICE|inlarge|Large tray +CHOICE|inright|Right tray +CHOICE|intray1|Tray 1 +CHOICE|intray2|Tray 2 +CHOICE|intray3|Tray 3 +CHOICE|intray4|Tray 4 +CHOICE|intray5|Tray 5 +CHOICE|intray6|Tray 6 +CHOICE|intray7|Tray 7 +CHOICE|intray8|Tray 8 +CHOICE|intray9|Tray 9 +CHOICE|intray10|Tray 10 +CHOICE|inlargecapacity|Large capacity tray +CHOICE|inhighcapacity|High capacity tray +CHOICE|default|Default setting +DEFAULT|default +OPTION|OutputBin|Output Tray +CHOICE|outupper|Upper tray +CHOICE|outlower|Lower tray +CHOICE|outbin2|Tray 2 +CHOICE|outbin3|Tray 3 +CHOICE|outbin4|Tray 4 +CHOICE|outbin5|Tray 5 +CHOICE|outbin6|Tray 6 +CHOICE|outbin7|Tray 7 +CHOICE|outbin8|Tray 8 +CHOICE|outbin9|Tray 9 +CHOICE|default|Default setting +DEFAULT|default +ENDGROUP +GROUP|others|Others +OPTION|Duplex|Duplex +CHOICE|simplex|Simplex +CHOICE|duplex|Duplex +CHOICE|duplexshort|Duplex (short edge) +CHOICE|tumble|Tumble +CHOICE|default|Default setting +DEFAULT|default +OPTION|Resolution|Printing Resolution +CHOICE|300|300 DPI +CHOICE|600|600 DPI +CHOICE|1200|1200 DPI +CHOICE|default|Default setting +DEFAULT|default +ENDGROUP +GROUP|advanced|Advanced Settings +OPTION|filter|Filter options (comma sepatared)|STRING +OPTION|lpr|LPR options (comma separated)|STRING +ENDGROUP diff --git a/tdeprint/lpr/lprngtoolhandler.cpp b/tdeprint/lpr/lprngtoolhandler.cpp new file mode 100644 index 000000000..1d76cf2f5 --- /dev/null +++ b/tdeprint/lpr/lprngtoolhandler.cpp @@ -0,0 +1,393 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "lprngtoolhandler.h" +#include "printcapentry.h" +#include "kmprinter.h" +#include "util.h" +#include "lprsettings.h" +#include "driver.h" +#include "kmmanager.h" +#include "kprinter.h" + +#include +#include +#include +#include +#include + +LPRngToolHandler::LPRngToolHandler(KMManager *mgr) +: LprHandler("lprngtool", mgr) +{ +} + +bool LPRngToolHandler::validate(PrintcapEntry *entry) +{ + if (entry->comment.startsWith("##LPRNGTOOL##") && + entry->comment.find("UNKNOWN") == -1) + return true; + return false; +} + +bool LPRngToolHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool shortmode) +{ + TQString str, lp; + + // look for type in comment + TQStringList l = TQStringList::split(' ', entry->comment, false); + lp = entry->field("lp"); + if (l.count() < 1) + return false; + + if (l[1] == "DEVICE" || l[1] == "SOCKET" || l[1] == "QUEUE") + LprHandler::completePrinter(prt, entry, shortmode); + else if (l[1] == "SMB") + { + TQMap opts = parseXferOptions(entry->field("xfer_options")); + TQString user, pass; + loadAuthFile(LprSettings::self()->baseSpoolDir() + "/" + entry->name + "/" + opts["authfile"], user, pass); + TQString uri = buildSmbURI( + opts[ "workgroup" ], + opts[ "host" ], + opts[ "printer" ], + user, + pass ); + prt->setDevice( uri ); + prt->setLocation(i18n("Network printer (%1)").arg("smb")); + } + + // look for comment + if (!(str=entry->field("cm")).isEmpty()) + prt->setDescription(str); + + // driver + //if (!shortmode) + //{ + if (!(str=entry->field("ifhp")).isEmpty()) + { + TQString model; + int p = str.find("model"); + if (p != -1) + { + p = str.find('=', p); + if (p != -1) + { + p++; + int q = str.find(',', p); + if (q == -1) + model = str.mid(p); + else + model = str.mid(p, q-p); + } + } + prt->setDriverInfo(i18n("IFHP Driver (%1)").arg((model.isEmpty() ? i18n("unknown") : model))); + prt->setOption("driverID", model); + } + //} + return true; +} + +TQMap LPRngToolHandler::parseXferOptions(const TQString& str) +{ + uint p(0), q; + TQMap opts; + TQString key, val; + + while (p < str.length()) + { + key = val = TQString::null; + // skip spaces + while (p < str.length() && str[p].isSpace()) + p++; + q = p; + while (q < str.length() && str[q] != '=') + q++; + key = str.mid(p, q-p); + p = q+2; + while (p < str.length() && str[p] != '"') + p++; + val = str.mid(q+2, p-q-2); + if (!key.isEmpty()) + opts[key] = val; + p++; + } + return opts; +} + +void LPRngToolHandler::loadAuthFile(const TQString& filename, TQString& user, TQString& pass) +{ + TQFile f(filename); + if (f.open(IO_ReadOnly)) + { + TQTextStream t(&f); + TQString line; + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.isEmpty()) + continue; + int p = line.find('='); + if (p != -1) + { + TQString key = line.left(p); + if (key == "username") + user = line.mid(p+1); + else if (key == "password") + pass = line.mid(p+1); + } + } + } +} + +DrMain* LPRngToolHandler::loadDriver(KMPrinter *prt, PrintcapEntry *entry, bool config) +{ + if (entry->field("lprngtooloptions").isEmpty()) + { + manager()->setErrorMsg(i18n("No driver defined for that printer. It might be a raw printer.")); + return NULL; + } + + DrMain* driver = loadToolDriver(locate("data", "tdeprint/lprngtooldriver1")); + if (driver) + { + TQString model = prt->option("driverID"); + driver->set("text", i18n("LPRngTool Common Driver (%1)").arg((model.isEmpty() ? i18n("unknown") : model))); + if (!model.isEmpty()) + driver->set("driverID", model); + TQMap opts = parseZOptions(entry->field("prefix_z")); + opts["lpr"] = entry->field("lpr"); + driver->setOptions(opts); + // if not configuring, don't show the "lpr" options + if (!config) + driver->removeOptionGlobally("lpr"); + } + return driver; +} + +DrMain* LPRngToolHandler::loadDbDriver(const TQString& s) +{ + int p = s.find('/'); + DrMain* driver = loadToolDriver(locate("data", "tdeprint/lprngtooldriver1")); + if (driver) + driver->set("driverID", s.mid(p+1)); + return driver; +} + +TQValueList< TQPair > LPRngToolHandler::loadChoiceDict(const TQString& filename) +{ + TQFile f(filename); + TQValueList< TQPair > dict; + if (f.open(IO_ReadOnly)) + { + TQTextStream t(&f); + TQString line, key; + TQStringList l; + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.startsWith("OPTION")) + { + if (l.count() > 0 && !key.isEmpty()) + dict << TQPair(key, l); + l.clear(); + key = TQString::null; + if (line.contains('|') == 2 || line.right(7) == "BOOLEAN") + { + int p = line.find('|', 7); + key = line.mid(7, p-7); + } + } + else if (line.startsWith("CHOICE")) + { + int p = line.find('|', 7); + l << line.mid(7, p-7); + } + } + } + return dict; +} + +TQMap LPRngToolHandler::parseZOptions(const TQString& optstr) +{ + TQMap opts; + TQStringList l = TQStringList::split(',', optstr, false); + if (l.count() == 0) + return opts; + + if (m_dict.count() == 0) + m_dict = loadChoiceDict(locate("data", "tdeprint/lprngtooldriver1")); + + TQString unknown; + for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it) + { + bool found(false); + for (TQValueList< TQPair >::ConstIterator p=m_dict.begin(); p!=m_dict.end() && !found; ++p) + { + if ((*p).second.find(*it) != (*p).second.end()) + { + opts[(*p).first] = (*it); + found = true; + } + } + if (!found) + { + unknown.append(*it).append(','); + } + } + if (!unknown.isEmpty()) + { + unknown.truncate(unknown.length()-1); + opts["filter"] = unknown; + } + return opts; +} + +TQString LPRngToolHandler::filterDir() +{ + return driverDirectory(); +} + +TQString LPRngToolHandler::driverDirInternal() +{ + return locateDir("filters", "/usr/lib:/usr/local/lib:/opt/lib:/usr/libexec:/usr/local/libexec:/opt/libexec"); +} + +PrintcapEntry* LPRngToolHandler::createEntry(KMPrinter *prt) +{ + TQString prot = prt->deviceProtocol(); + if (prot != "parallel" && prot != "lpd" && prot != "smb" && prot != "socket") + { + manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot)); + return NULL; + } + PrintcapEntry *entry = new PrintcapEntry; + entry->addField("cm", Field::String, prt->description()); + TQString lp, comment("##LPRNGTOOL## "); + if (prot == "parallel") + { + comment.append("DEVICE "); + lp = prt->device().mid( 9 ); + entry->addField("rw@", Field::Boolean); + } + else if (prot == "socket") + { + comment.append("SOCKET "); + KURL url( prt->device() ); + lp = url.host(); + if (url.port() == 0) + lp.append("%9100"); + else + lp.append("%").append(TQString::number(url.port())); + } + else if (prot == "lpd") + { + comment.append("QUEUE "); + KURL url( prt->device() ); + lp = url.path().mid(1) + "@" + url.host(); + } + else if (prot == "smb") + { + comment.append("SMB "); + lp = "| " + filterDir() + "/smbprint"; + TQString work, server, printer, user, passwd; + if ( splitSmbURI( prt->device(), work, server, printer, user, passwd ) ) + { + entry->addField("xfer_options", Field::String, TQString::tqfromLatin1("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)); + TQFile authfile(LprSettings::self()->baseSpoolDir() + "/" + prt->printerName() + "/auth"); + if (authfile.open(IO_WriteOnly)) + { + TQTextStream t(&authfile); + t << "username=" << user << endl; + t << "password=" << passwd << endl; + authfile.close(); + } + } + else + { + manager()->setErrorMsg( i18n( "Invalid printer backend specification: %1" ).arg( prt->device() ) ); + delete entry; + return NULL; + } + } + + if (prt->driver()) + { + DrMain *driver = prt->driver(); + comment.append("filtertype=IFHP ifhp_options=status@,sync@,pagecount@,waitend@ printerdb_entry="); + comment.append(driver->get("driverID")); + entry->addField("ifhp", Field::String, TQString::tqfromLatin1("model=%1,status@,sync@,pagecount@,waitend@").arg(driver->get("driverID"))); + entry->addField("lprngtooloptions", Field::String, TQString::tqfromLatin1("FILTERTYPE=\"IFHP\" IFHP_OPTIONS=\"status@,sync@,pagecount@,waitend@\" PRINTERDB_ENTRY=\"%1\"").arg(driver->get("driverID"))); + TQMap opts; + TQString optstr; + driver->getOptions(opts, false); + for (TQMap::ConstIterator it=opts.begin(); it!=opts.end(); ++it) + if (it.key() != "lpr") + optstr.append(*it).append(","); + if (!optstr.isEmpty()) + { + optstr.truncate(optstr.length()-1); + entry->addField("prefix_z", Field::String, optstr); + } + if (!opts["lpr"].isEmpty()) + entry->addField("lpr", Field::String, opts["lpr"]); + } + + entry->addField("lp", Field::String, lp); + entry->comment = comment; + + return entry; +} + +bool LPRngToolHandler::savePrinterDriver(KMPrinter*, PrintcapEntry *entry, DrMain *driver, bool *mustSave) +{ + // save options in the "prefix_z" field and tell the manager to save the printcap file + TQMap opts; + TQString optstr; + driver->getOptions(opts, false); + for (TQMap::ConstIterator it=opts.begin(); it!=opts.end(); ++it) + if (it.key() != "lpr") + optstr.append(*it).append(","); + if (!optstr.isEmpty()) + optstr.truncate(optstr.length()-1); + // save options in any case, otherwise nothing will happen whn + // options are reset to their default value + entry->addField("prefix_z", Field::String, optstr); + entry->addField("lpr", Field::String, opts["lpr"]); + if (mustSave) + *mustSave = true; + return true; +} + +TQString LPRngToolHandler::printOptions(KPrinter *printer) +{ + TQString optstr; + TQMap opts = printer->options(); + for (TQMap::ConstIterator it=opts.begin(); it!=opts.end(); ++it) + { + if (it.key().startsWith("kde-") || it.key().startsWith("_kde-") || it.key() == "lpr" || it.key().startsWith( "app-" )) + continue; + optstr.append(*it).append(","); + } + if (!optstr.isEmpty()) + { + optstr.truncate(optstr.length()-1); + optstr.prepend("-Z '").append("'"); + } + return optstr; +} diff --git a/tdeprint/lpr/lprngtoolhandler.h b/tdeprint/lpr/lprngtoolhandler.h new file mode 100644 index 000000000..49641ca3a --- /dev/null +++ b/tdeprint/lpr/lprngtoolhandler.h @@ -0,0 +1,54 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 LPRNGTOOLHANDLER_H +#define LPRNGTOOLHANDLER_H + +#include "lprhandler.h" +#include +#include +#include + +class LPRngToolHandler : public LprHandler +{ +public: + LPRngToolHandler(KMManager *mgr = 0); + + bool validate(PrintcapEntry*); + bool completePrinter(KMPrinter*, PrintcapEntry*, bool = true); + DrMain* loadDriver(KMPrinter*, PrintcapEntry*, bool = false); + DrMain* loadDbDriver(const TQString&); + PrintcapEntry* createEntry(KMPrinter*); + bool savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool* = 0); + TQString printOptions(KPrinter*); + +protected: + TQMap parseXferOptions(const TQString&); + void loadAuthFile(const TQString&, TQString&, TQString&); + TQValueList< TQPair > loadChoiceDict(const TQString&); + TQMap parseZOptions(const TQString&); + TQString filterDir(); + TQString driverDirInternal(); + + +private: + TQValueList< TQPair > m_dict; +}; + +#endif diff --git a/tdeprint/lpr/lprsettings.cpp b/tdeprint/lpr/lprsettings.cpp new file mode 100644 index 000000000..0c1cec2a9 --- /dev/null +++ b/tdeprint/lpr/lprsettings.cpp @@ -0,0 +1,146 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001,2002 Michael Goffioul + * + * 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 "lprsettings.h" +#include "kmmanager.h" +#include "kmfactory.h" + +#include +#include +#include + +#define LPDCONF "/etc/lpd.conf" +#define PRINTCAP "/etc/printcap" + +LprSettings* LprSettings::m_self = 0; + +LprSettings::LprSettings(TQObject *parent, const char *name) +: TQObject(parent, name), KPReloadObject(true) +{ + init(); +} + +LprSettings::~LprSettings() +{ + m_self = 0; +} + +LprSettings* LprSettings::self() +{ + if (!m_self) + { + m_self = new LprSettings(KMManager::self(), "LprSettings"); + } + return m_self; +} + +void LprSettings::init() +{ + // LPR/LPRng mode + KConfig *conf = KMFactory::self()->printConfig(); + conf->setGroup("LPR"); + TQString modestr = conf->readEntry("Mode"); + if (modestr == "LPRng") + m_mode = LPRng; + else if (modestr == "LPR") + m_mode = LPR; + else + { + // try to guess + if (TQFile::exists(LPDCONF)) + m_mode = LPRng; + else + m_mode = LPR; + } + + // Printcap file + m_printcapfile = TQString(); + m_local = true; + + // Spool directory + m_spooldir = "/var/spool/lpd"; +} + +TQString LprSettings::printcapFile() +{ + if (m_printcapfile.isEmpty()) + { + // default value + m_printcapfile = PRINTCAP; + if (m_mode == LPRng) + { + // look into /etc/lpd/conf file + TQFile cf(LPDCONF); + if (cf.open(IO_ReadOnly)) + { + TQTextStream t(&cf); + TQString line; + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.startsWith("printcap_path")) + { + TQString filename = line.mid(14).stripWhiteSpace(); + if (filename[0] != '|') + m_printcapfile = filename; + else + { + // should download the printcap file + // and set m_local to false + } + } + } + } + } + } + return m_printcapfile; +} + +TQString LprSettings::defaultRemoteHost() +{ + if (m_defaultremotehost.isEmpty()) + { + m_defaultremotehost = "localhost"; + TQFile cf(LPDCONF); + if (cf.open(IO_ReadOnly)) + { + TQTextStream t(&cf); + TQString line; + while (!t.atEnd()) + { + line = t.readLine().stripWhiteSpace(); + if (line.startsWith("default_remote_host")) + { + TQString hostname = line.mid(20).stripWhiteSpace(); + m_defaultremotehost = hostname; + } + } + } + } + return m_defaultremotehost; +} + +void LprSettings::reload() +{ +} + +void LprSettings::configChanged() +{ + init(); +} diff --git a/tdeprint/lpr/lprsettings.h b/tdeprint/lpr/lprsettings.h new file mode 100644 index 000000000..01abbeeb3 --- /dev/null +++ b/tdeprint/lpr/lprsettings.h @@ -0,0 +1,67 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001,2002 Michael Goffioul + * + * 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 LPRSETTINGS_H +#define LPRSETTINGS_H + +#if !defined( _KDEPRINT_COMPILE ) && defined( __GNUC__ ) +#warning internal header, do not use except if you are a KDEPrint developer +#endif + +#include +#include + +/** + * @internal + * This class is internal to KDEPrint and is not intended to be + * used outside it. Please do not make use of this header, except + * if you're a KDEPrint developer. The API might change in the + * future and binary compatibility might be broken. + */ +class LprSettings : public TQObject, public KPReloadObject +{ +public: + LprSettings(TQObject *parent = 0, const char *name = 0); + ~LprSettings(); + + static LprSettings* self(); + + enum Mode { LPR, LPRng }; + Mode mode() const { return m_mode; } + void setMode(Mode m) { m_mode = m; } + TQString printcapFile(); + TQString defaultRemoteHost(); + bool isLocalPrintcap() { return m_local; } + TQString baseSpoolDir() { return m_spooldir; } + +protected: + void init(); + void reload(); + void configChanged(); + +private: + static LprSettings* m_self; + Mode m_mode; + QString m_printcapfile; + bool m_local; + QString m_spooldir; + TQString m_defaultremotehost; +}; + +#endif diff --git a/tdeprint/lpr/make_driver_db_lpr.c b/tdeprint/lpr/make_driver_db_lpr.c new file mode 100644 index 000000000..fabbcb2fe --- /dev/null +++ b/tdeprint/lpr/make_driver_db_lpr.c @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "driverparse.h" + +void simplifyModel(const char *modelname) +{ + char *g; + + if ((g=strchr(modelname, ',')) != NULL) + *g = 0; + else if ((g=strchr(modelname, '(')) != NULL || (g=strchr(modelname, '{')) != NULL) + { + if (g != modelname) + { + if (isspace(*(g-1))) + g--; + *g = 0; + } + } +} + +int parseApsFile(const char *filename, FILE *output) +{ + FILE *apsfile; + char buf[256], modelname[256]; + char *c, *d; + + apsfile = fopen(filename, "r"); + if (apsfile == NULL) + return 0; + while (fgets(buf, 255, apsfile) != NULL) + { + if ((c = strchr(buf, '\n')) != NULL) + *c = 0; + if (strlen(buf) == 0 || buf[0] == '#' || (c = strchr(buf, '|')) == NULL) + continue; + *c = 0; + fprintf(output, "FILE=apsfilter/%s\n", c+1); + d = strchr(buf, ' '); + if (d) + { + *d = 0; + strlcpy(modelname, d+1, 255); + simplifyModel(modelname); + fprintf(output, "MANUFACTURER=%s\n", buf); + fprintf(output, "MODELNAME=%s\n", modelname); + fprintf(output, "MODEL=%s\n", modelname); + fprintf(output, "DESCRIPTION=%s %s (APSFilter + %s)\n", buf, d+1, c+1); + } + else + { + strlcpy(modelname, buf, 255); + simplifyModel(modelname); + fprintf(output, "MANUFACTURER=Unknown\n"); + fprintf(output, "MODELNAME=%s\n", modelname); + fprintf(output, "MODEL=%s\n", modelname); + fprintf(output, "DESCRIPTION=%s (APSFilter + %s)\n", buf, c+1); + } + fprintf(output, "\n"); + } + fclose(apsfile); + return 1; +} + +void initAps(const char *base) +{ + char drFile[256]; + DIR *apsdir; + struct dirent *d; + int gsversion = 0; + + if (strstr(base, "apsfilter") == NULL) + return; + + apsdir = opendir(base); + if (apsdir == NULL) + return; + while ((d = readdir(apsdir)) != NULL) + { + if (strncmp(d->d_name, "printer-", 8) != 0) + continue; + if (isdigit(d->d_name[8])) + { + if (gsversion) + continue; + gsversion = 1; + } + snprintf(drFile, 256, "apsfilter:%s/%s", base, d->d_name); + addFile(drFile); + } + closedir(apsdir); +} + +char* nextWord(char *c) +{ + char *d = c; + while (*d && isspace(*d)) + d++; + return d; +} + +int parseIfhpFile(const char *filename, FILE *output) +{ + char buf[1024]; + FILE *in; + char model[32], desc[256]; + + in = fopen(filename, "r"); + if (in == NULL) + return 0; + while (fgets(buf, 1023, in)) + { + char *c; + if ((c = strchr(buf, '\n')) != NULL) + *c = 0; + c = buf; + while (*c && isspace(*c)) + c++; + if (*c == '#') + continue; + if (strncmp(c, "IfhpModel:", 10) == 0) + strlcpy(model, nextWord(c+11), 31); + else if (strncmp(c, "Description:", 12) == 0) + strlcpy(desc, nextWord(c+13), 255); + else if (strncmp(c, "EndEntry", 8) == 0) + { + char *d = desc, *e, make[32] = {0}; + int first_time = 1; + do + { + e = strchr(d, ','); + if (e) + *e = 0; + if (first_time) + { + char *f = strchr(d, ' '); + if (f) + strlcpy(make, d, f-d); + first_time = 0; + } + if (strstr(d, "Family") == NULL) + { + char modelname[256] = {0}; + + strlcpy(modelname, d, 255); + simplifyModel(modelname); + fprintf(output, "FILE=lprngtool/%s\n", model); + fprintf(output, "MANUFACTURER=%s\n", make); + fprintf(output, "MODEL=%s\n", modelname); + fprintf(output, "MODELNAME=%s\n", modelname); + fprintf(output, "DESCRIPTION=%s (IFHP + %s)\n", d, model); + fprintf(output, "\n"); + } + if (e) + { + d = e+1; + while (*d && isspace(*d)) + d++; + } + } while (e); + } + } + fclose(in); + return 1; +} + +void initIfhp(const char *base) +{ + char path[256]; + + snprintf(path, 255, "lprngtool:%s/printerdb", base); + if (access(path+10, R_OK) == 0) + { + addFile(path); + } +} + +int main(int argc, char **argv) +{ + initFoomatic(); + registerHandler("apsfilter:", initAps, parseApsFile); + registerHandler("lprngtool:", initIfhp, parseIfhpFile); + return execute(argc, argv); +} diff --git a/tdeprint/lpr/matichandler.cpp b/tdeprint/lpr/matichandler.cpp new file mode 100644 index 000000000..ef2f1584b --- /dev/null +++ b/tdeprint/lpr/matichandler.cpp @@ -0,0 +1,481 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "matichandler.h" +#include "printcapentry.h" +#include "kmprinter.h" +#include "matichelper.h" +#include "driver.h" +#include "kpipeprocess.h" +#include "kmmanager.h" +#include "kprinter.h" +#include "lprsettings.h" +#include "util.h" +#include "foomatic2loader.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +MaticHandler::MaticHandler(KMManager *mgr) +: LprHandler("foomatic", mgr) +{ + TQString PATH = getenv("PATH"); + PATH.append(":/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local/sbin"); + m_exematicpath = KStandardDirs::findExe("lpdomatic", PATH); + m_ncpath = KStandardDirs::findExe("nc"); + m_smbpath = KStandardDirs::findExe("smbclient"); + m_rlprpath = KStandardDirs::findExe("rlpr"); +} + +bool MaticHandler::validate(PrintcapEntry *entry) +{ + if (entry) + return (entry->field("if").right(9) == "lpdomatic"); + return false; +} + +KMPrinter* MaticHandler::createPrinter(PrintcapEntry *entry) +{ + if (entry && validate(entry)) + { + KMPrinter *prt = new KMPrinter; + prt->setName(entry->name); + prt->setPrinterName(entry->name); + prt->setType(KMPrinter::Printer); + //if (entry->field("lp") == "/dev/null" || entry->field("lp").isEmpty()) + // prt->addType(KMPrinter::Remote); + return prt; + } + return NULL; +} + +bool MaticHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool shortmode) +{ + TQString val = entry->field("lp"); + if (val == "/dev/null" || val.isEmpty()) + { + prt->setLocation(i18n("Network printer")); + } + else + { + prt->setLocation(i18n("Local printer on %1").arg(val)); + KURL url(val); + if (val.find("usb") != -1) + url.setProtocol("usb"); + else + url.setProtocol("parallel"); + prt->setDevice(url.url()); + } + prt->setDescription(entry->aliases.join(", ")); + + if (!shortmode) + { + Foomatic2Loader loader; + if ( loader.readFromFile( maticFile( entry ) ) ) + { + TQString postpipe = loader.data()[ "POSTPIPE" ].toString(); + if (!postpipe.isEmpty()) + { + KURL url ( parsePostpipe(postpipe) ); + if (!url.isEmpty()) + { + TQString ds = TQString::tqfromLatin1("%1 (%2)").arg(prt->location()).arg(url.protocol()); + prt->setDevice(url.url()); + prt->setLocation(ds); + } + } + + TQMap m = loader.data()[ "VAR" ].toMap(); + if ( !m.isEmpty() ) + { + prt->setManufacturer(m["make"].toString()); + prt->setModel(m["model"].toString()); + prt->setDriverInfo(TQString::tqfromLatin1("%1 %2 (%3)").arg(prt->manufacturer()).arg(prt->model()).arg(m["driver"].toString())); + } + } + } + + return true; +} + +TQString MaticHandler::parsePostpipe(const TQString& s) +{ + TQString url; + int p = s.findRev('|'); + TQStringList args = TQStringList::split(" ", s.right(s.length()-p-1)); + + if (args.count() != 0) + { + // socket printer + if (args[0].right(3) == "/nc") + { + url = "socket://" + args[ 1 ]; + if ( args.count() > 2 ) + url += ":" + args[ 2 ]; + else + url += ":9100"; + } + // smb printer + else if (args[0].right(10) == "/smbclient") + { + TQStringList host_components = TQStringList::split(TQRegExp("/|\\\\\""), args[1], false); + TQString workgrp, user, pass; + for (uint i=2; irandomString(8)); + ::system(TQFile::encodeName("cp " + KProcess::quote(origfilename) + " " + KProcess::quote(filename))); + DrMain *driver = Foomatic2Loader::loadDriver(filename); + if (driver) + { + driver->set("template", filename); + driver->set("temporary", "true"); + return driver; + } + else + return NULL; +} + +DrMain* MaticHandler::loadDbDriver(const TQString& path) +{ + TQStringList comps = TQStringList::split('/', path, false); + if (comps.count() < 3 || comps[0] != "foomatic") + { + manager()->setErrorMsg(i18n("Internal error.")); + return NULL; + } + + TQString tmpFile = locateLocal("tmp", "foomatic_" + kapp->randomString(8)); + TQString PATH = getenv("PATH") + TQString::tqfromLatin1(":/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local/sbin"); + TQString exe = KStandardDirs::findExe("foomatic-datafile", PATH); + if (exe.isEmpty()) + { + manager()->setErrorMsg(i18n("Unable to find the executable foomatic-datafile " + "in your PATH. Check that Foomatic is correctly installed.")); + return NULL; + } + + KPipeProcess in; + TQFile out(tmpFile); + TQString cmd = KProcess::quote(exe); + cmd += " -t lpd -d "; + cmd += KProcess::quote(comps[2]); + cmd += " -p "; + cmd += KProcess::quote(comps[1]); + if (in.open(cmd) && out.open(IO_WriteOnly)) + { + TQTextStream tin(&in), tout(&out); + TQString line; + while (!tin.atEnd()) + { + line = tin.readLine(); + tout << line << endl; + } + in.close(); + out.close(); + + DrMain *driver = Foomatic2Loader::loadDriver(tmpFile); + if (driver) + { + driver->set("template", tmpFile); + driver->set("temporary", tmpFile); + return driver; + } + } + manager()->setErrorMsg(i18n("Unable to create the Foomatic driver [%1,%2]. " + "Either that driver does not exist, or you don't have " + "the required permissions to perform that operation.").arg(comps[1]).arg(comps[2])); + return NULL; +} + +bool MaticHandler::savePrinterDriver(KMPrinter *prt, PrintcapEntry *entry, DrMain *driver, bool*) +{ + TQFile tmpFile(locateLocal("tmp", "foomatic_" + kapp->randomString(8))); + TQFile inFile(driver->get("template")); + TQString outFile = maticFile(entry); + bool result(false); + TQString postpipe = createPostpipe(prt->device()); + + if (inFile.open(IO_ReadOnly) && tmpFile.open(IO_WriteOnly)) + { + TQTextStream tin(&inFile), tout(&tmpFile); + TQString line, optname; + int p(-1), q(-1); + if (!postpipe.isEmpty()) + tout << "$postpipe = \"" << postpipe << "\";" << endl; + while (!tin.atEnd()) + { + line = tin.readLine(); + if (line.stripWhiteSpace().startsWith("$postpipe")) + continue; + else if ((p = line.find("'name'")) != -1) + { + p = line.find('\'', p+6)+1; + q = line.find('\'', p); + optname = line.mid(p, q-p); + } + else if ((p = line.find("'default'")) != -1) + { + DrBase *opt = driver->findOption(optname); + if (opt) + { + tout << line.left(p+9) << " => '" << opt->valueText() << "'," << endl; + continue; + } + } + tout << line << endl; + } + inFile.close(); + tmpFile.close(); + + TQString cmd = "mv " + KProcess::quote(tmpFile.name()) + " " + KProcess::quote(outFile); + int status = ::system(TQFile::encodeName(cmd).data()); + TQFile::remove(tmpFile.name()); + result = (status != -1 && WEXITSTATUS(status) == 0); + } + + if (!result) + manager()->setErrorMsg(i18n("You probably don't have the required permissions " + "to perform that operation.")); + TQFile::remove(tmpFile.name()); + if (!result || entry->field("ppdfile").isEmpty()) + return result; + else + return savePpdFile(driver, entry->field("ppdfile")); +} + +bool MaticHandler::savePpdFile(DrMain *driver, const TQString& filename) +{ + TQString mdriver(driver->get("matic_driver")), mprinter(driver->get("matic_printer")); + if (mdriver.isEmpty() || mprinter.isEmpty()) + return true; + + TQString PATH = getenv("PATH") + TQString::tqfromLatin1(":/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local/sbin"); + TQString exe = KStandardDirs::findExe("foomatic-datafile", PATH); + if (exe.isEmpty()) + { + manager()->setErrorMsg(i18n("Unable to find the executable foomatic-datafile " + "in your PATH. Check that Foomatic is correctly installed.")); + return false; + } + + KPipeProcess in; + TQFile out(filename); + if (in.open(exe + " -t cups -d " + mdriver + " -p " + mprinter) && out.open(IO_WriteOnly)) + { + TQTextStream tin(&in), tout(&out); + TQString line, optname; + TQRegExp re("^\\*Default(\\w+):"), foo("'name'\\s+=>\\s+'(\\w+)'"), foo2("'\\w+'\\s*,\\s*$"); + while (!tin.atEnd()) + { + line = tin.readLine(); + if (line.startsWith("*% COMDATA #")) + { + if (line.find("'default'") != -1) + { + DrBase *opt = (optname.isEmpty() ? NULL : driver->findOption(optname)); + if (opt) + { + line.replace(foo2, "'"+opt->valueText()+"',"); + } + } + else if (foo.search(line) != -1) + optname = foo.cap(1); + } + else if (re.search(line) != -1) + { + DrBase *opt = driver->findOption(re.cap(1)); + if (opt) + { + TQString val = opt->valueText(); + if (opt->type() == DrBase::Boolean) + val = (val == "1" ? "True" : "False"); + tout << "*Default" << opt->name() << ": " << val << endl; + continue; + } + } + tout << line << endl; + } + in.close(); + out.close(); + + return true; + } + manager()->setErrorMsg(i18n("Unable to create the Foomatic driver [%1,%2]. " + "Either that driver does not exist, or you don't have " + "the required permissions to perform that operation.").arg(mdriver).arg(mprinter)); + + return false; +} + +PrintcapEntry* MaticHandler::createEntry(KMPrinter *prt) +{ + KURL url( prt->device() ); + TQString prot = url.protocol(); + if ((prot != "lpd" || m_rlprpath.isEmpty()) && + (prot != "socket" || m_ncpath.isEmpty()) && + (prot != "smb" || m_smbpath.isEmpty()) && + prot != "parallel") + { + manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot)); + return NULL; + } + if (m_exematicpath.isEmpty()) + { + manager()->setErrorMsg(i18n("Unable to find executable lpdomatic. " + "Check that Foomatic is correctly installed " + "and that lpdomatic is installed in a standard " + "location.")); + return NULL; + } + PrintcapEntry *entry = new PrintcapEntry; + entry->addField("lf", Field::String, "/var/log/lp-errs"); + entry->addField("lp", Field::String, (prot != "parallel" ? "/dev/null" : url.path())); + entry->addField("if", Field::String, m_exematicpath); + if (LprSettings::self()->mode() == LprSettings::LPRng) + { + entry->addField("filter_options", Field::String, " --lprng $Z /etc/foomatic/lpd/"+prt->printerName()+".lom"); + entry->addField("force_localhost", Field::Boolean); + entry->addField("ppdfile", Field::String, "/etc/foomatic/"+prt->printerName()+".ppd"); + } + else + entry->addField("af", Field::String, "/etc/foomatic/lpd/"+prt->printerName()+".lom"); + if (!prt->description().isEmpty()) + entry->aliases << prt->description(); + return entry; +} + +bool MaticHandler::removePrinter(KMPrinter *prt, PrintcapEntry *entry) +{ + // remove Foomatic driver + TQString af = entry->field("af"); + if (af.isEmpty()) + return true; + if (!TQFile::remove(af)) + { + manager()->setErrorMsg(i18n("Unable to remove driver file %1.").arg(af)); + return false; + } + return true; +} + +TQString MaticHandler::printOptions(KPrinter *printer) +{ + TQMap opts = printer->options(); + TQString str; + for (TQMap::Iterator it=opts.begin(); it!=opts.end(); ++it) + { + if (it.key().startsWith("kde-") || it.key().startsWith("_kde-") || it.key().startsWith( "app-" )) + continue; + str += (" " + it.key() + "=" + (*it)); + } + if (!str.isEmpty()) + str.prepend("-J '").append("'"); + return str; +} + +TQString MaticHandler::driverDirInternal() +{ + return locateDir("foomatic/db/source", "/usr/share:/usr/local/share:/opt/share"); +} diff --git a/tdeprint/lpr/matichandler.h b/tdeprint/lpr/matichandler.h new file mode 100644 index 000000000..e30eee39e --- /dev/null +++ b/tdeprint/lpr/matichandler.h @@ -0,0 +1,57 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 MATICHANDLER_H +#define MATICHANDLER_H + +#include "lprhandler.h" + +#include + +class MaticBlock; + +class MaticHandler : public LprHandler +{ +public: + MaticHandler(KMManager *mgr = 0); + + bool validate(PrintcapEntry*); + KMPrinter* createPrinter(PrintcapEntry*); + bool completePrinter(KMPrinter*, PrintcapEntry*, bool = true); + DrMain* loadDriver(KMPrinter*, PrintcapEntry*, bool = false); + DrMain* loadDbDriver(const TQString&); + bool savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool* = 0); + PrintcapEntry* createEntry(KMPrinter*); + bool removePrinter(KMPrinter*, PrintcapEntry*); + TQString printOptions(KPrinter*); + +protected: + TQString driverDirInternal(); + +private: + TQString parsePostpipe(const TQString&); + TQString createPostpipe(const TQString&); + bool savePpdFile(DrMain*, const TQString&); + +private: + TQString m_exematicpath; + TQString m_ncpath, m_smbpath, m_rlprpath; +}; + +#endif diff --git a/tdeprint/lpr/matichelper.cpp b/tdeprint/lpr/matichelper.cpp new file mode 100644 index 000000000..ec0041b7c --- /dev/null +++ b/tdeprint/lpr/matichelper.cpp @@ -0,0 +1,38 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2002 Michael Goffioul + * + * 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 "matichelper.h" +#include "printcapentry.h" +#include "driver.h" + +TQString maticFile(PrintcapEntry *entry) +{ + TQString s(entry->field("af")); + if (s.isEmpty()) + { + s = entry->field("filter_options"); + if (!s.isEmpty()) + { + int p = s.findRev(' '); + if (p != -1) + s = s.mid(p+1); + } + } + return s; +} diff --git a/tdeprint/lpr/matichelper.h b/tdeprint/lpr/matichelper.h new file mode 100644 index 000000000..5a2b64fe3 --- /dev/null +++ b/tdeprint/lpr/matichelper.h @@ -0,0 +1,29 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2002 Michael Goffioul + * + * 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 MATICHELPER_H +#define MATICHELPER_H + +#include + +class PrintcapEntry; + +TQString maticFile(PrintcapEntry*); + +#endif diff --git a/tdeprint/lpr/printcapentry.cpp b/tdeprint/lpr/printcapentry.cpp new file mode 100644 index 000000000..9c83537e4 --- /dev/null +++ b/tdeprint/lpr/printcapentry.cpp @@ -0,0 +1,82 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001,2002 Michael Goffioul + * + * 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 "printcapentry.h" + +TQString Field::toString() const +{ + TQString s = name; + switch (type) + { + case String: + s += ("=" + value); + break; + case Integer: + s += ("#" + value); + break; + case Boolean: + if (!value.toInt()) + s += "@"; + break; + } + return s; +} + +bool PrintcapEntry::writeEntry(TQTextStream& t) +{ + t << comment << endl; + t << name; + if (aliases.count() > 0) + t << '|' << aliases.join("|"); + t << ':'; + for (TQMap::ConstIterator it=fields.begin(); it!=fields.end(); ++it) + { + t << '\\' << endl << " :"; + t << (*it).name; + switch ((*it).type) + { + case Field::String: + t << '=' << (*it).value << ':'; + break; + case Field::Integer: + t << '#' << (*it).value << ':'; + break; + case Field::Boolean: + t << ':'; + break; + default: + t << endl << endl; + return false; + } + } + t << endl; + if (!postcomment.isEmpty()) + t << postcomment << endl; + t << endl; + return true; +} + +void PrintcapEntry::addField(const TQString& name, Field::Type type, const TQString& value) +{ + Field f; + f.name = name; + f.type = type; + f.value = value; + fields[name] = f; +} diff --git a/tdeprint/lpr/printcapentry.h b/tdeprint/lpr/printcapentry.h new file mode 100644 index 000000000..f8ba99197 --- /dev/null +++ b/tdeprint/lpr/printcapentry.h @@ -0,0 +1,81 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 PRINTCAPENTRY_H +#define PRINTCAPENTRY_H + +#if !defined( _KDEPRINT_COMPILE ) && defined( __GNUC__ ) +#warning internal header, do not use except if you are a KDEPrint developer +#endif + +#include +#include +#include +#include + +/** + * @internal + * This class is internal to KDEPrint and is not intended to be + * used outside it. Please do not make use of this header, except + * if you're a KDEPrint developer. The API might change in the + * future and binary compatibility might be broken. + */ +class Field +{ +public: + enum Type { String, Integer, Boolean }; + Field() : type(String) {} + Field(const Field &f) : type(f.type), name(f.name), value(f.value) {} + Field& operator= (const Field& f) + { + type = f.type; + name = f.name; + value = f.value; + return (*this); + } + TQString toString() const; + + Type type; + TQString name; + TQString value; +}; + +/** + * @internal + * This class is internal to KDEPrint and is not intended to be + * used outside it. Please do not make use of this header, except + * if you're a KDEPrint developer. The API might change in the + * future and binary compatibility might be broken. + */ +class PrintcapEntry +{ +public: + TQString name; + TQStringList aliases; + TQString comment; + TQMap fields; + TQString postcomment; + + bool has(const TQString& f) const { return fields.contains(f); } + TQString field(const TQString& f) const { return fields[f].value; } + bool writeEntry(TQTextStream&); + void addField(const TQString& name, Field::Type type = Field::Boolean, const TQString& value = TQString::null); +}; + +#endif diff --git a/tdeprint/lpr/printcapreader.cpp b/tdeprint/lpr/printcapreader.cpp new file mode 100644 index 000000000..00016c14f --- /dev/null +++ b/tdeprint/lpr/printcapreader.cpp @@ -0,0 +1,176 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 "printcapreader.h" +#include "printcapentry.h" + +#include +#include + +void PrintcapReader::setPrintcapFile(TQFile *f) +{ + if (f->isOpen()) + { + m_stream.setDevice(TQT_TQIODEVICE(f)); + m_buffer = TQString::null; + } +} + +bool PrintcapReader::nextLine(TQString& line) +{ + if (m_stream.atEnd() && m_buffer.isEmpty()) + return false; + else if (!m_buffer.isEmpty()) + { + line = m_buffer; + m_buffer = TQString::null; + } + else + line = m_stream.readLine().stripWhiteSpace(); + // strip any '\' at the end + if (line[line.length()-1] == '\\') + line = line.left(line.length()-1).stripWhiteSpace(); + return true; +} + +void PrintcapReader::unputLine(const TQString& s) +{ + m_buffer = s; +} + +PrintcapEntry* PrintcapReader::nextEntry() +{ + if (!m_stream.tqdevice()) + return NULL; + + TQString line, comment, name, fields, buf; + // skip comments, keep last one + while (1) + { + if (!nextLine(line)) + return NULL; + else if (line.isEmpty()) + continue; + else if (line[0] == '#') + comment = line; + else + { + buf = line; + break; + } + } + + // look for the complete entry + while (1) + { + // we found the entry if we reached the end of file or + // found an empty line + if (!nextLine(line) || line.isEmpty()) + break; + // just skip comments + else if (line[0] == '#') + continue; + // lines starting with ':' or '|' are appended + else if (line[0] == ':' || line[0] == '|') + buf += line; + // otherwise it's another entry, put it back in the + // buffer + else + { + unputLine(line); + break; + } + } + + // now parse the entry + kdDebug() << "COMMENT: " << comment << endl; + kdDebug() << "LINE: " << buf << endl; + int p = buf.find(':'); + if (p == -1) + name = buf; + else + { + name = buf.left(p); + fields = buf.right(buf.length()-p-1); + } + + // construct the printcap entry + if (!name.isEmpty()) + { + PrintcapEntry *entry = new PrintcapEntry; + TQStringList l = TQStringList::split('|', name, false); + entry->name = l[0]; + entry->comment = comment; + // kdDebug() << "Printer: " << entry->name << endl; + // kdDebug() << "Aliases:" << endl; + for (uint i=1; ialiases << l[i]; + // kdDebug() << " " << l[i] << endl; + } + if (!fields.isEmpty()) + { + // kdDebug() << "Fields:" << endl; + // kdDebug() << "(" << fields << ")" << endl; + l = TQStringList::split(':', fields, false); + for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it) + { + Field f; + int p = (*it).find('='); + if (p == -1) + { + p = (*it).find('#'); + if (p == -1) + { + f.type = Field::Boolean; + p = (*it).find('@'); + if (p == -1) + { + f.name = (*it); + f.value = "1"; + } + else + { + f.name = (*it).left(p); + f.value = "0"; + } + } + else + { + f.type = Field::Integer; + f.name = (*it).left(p); + f.value = (*it).mid(p+1); + } + } + else + { + f.type = Field::String; + f.name = (*it).left(p); + f.value = (*it).mid(p+1); + if (f.value.startsWith("\"")) + f.value = f.value.mid(1, f.value.length()-2); + } + entry->fields[f.name] = f; + } + } + // kdDebug() << endl; + return entry; + } + return NULL; +} diff --git a/tdeprint/lpr/printcapreader.h b/tdeprint/lpr/printcapreader.h new file mode 100644 index 000000000..2236e5068 --- /dev/null +++ b/tdeprint/lpr/printcapreader.h @@ -0,0 +1,43 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * 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 PRINTCAPREADER_H +#define PRINTCAPREADER_H + +#include +#include + +class TQFile; +class PrintcapEntry; + +class PrintcapReader +{ +public: + void setPrintcapFile(TQFile*); + PrintcapEntry* nextEntry(); + +private: + TQString m_buffer; + TQTextStream m_stream; + + bool nextLine(TQString&); + void unputLine(const TQString&); +}; + +#endif -- cgit v1.2.1