kdeprint Library API Documentation

lprhandler.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include "lprhandler.h"
00021 #include "kmprinter.h"
00022 #include "printcapentry.h"
00023 #include "kmmanager.h"
00024 #include "lprsettings.h"
00025 #include "driver.h"
00026 
00027 #include <qfile.h>
00028 #include <qtextstream.h>
00029 #include <qvaluestack.h>
00030 #include <klocale.h>
00031 
00032 #include <unistd.h>
00033 
00034 LprHandler::LprHandler(const QString& name, KMManager *mgr)
00035 : m_name(name), m_manager(mgr)
00036 {
00037 }
00038 
00039 LprHandler::~LprHandler()
00040 {
00041 }
00042 
00043 bool LprHandler::validate(PrintcapEntry*)
00044 {
00045     return true;
00046 }
00047 
00048 KMPrinter* LprHandler::createPrinter(PrintcapEntry *entry)
00049 {
00050     KMPrinter   *prt = new KMPrinter;
00051     prt->setPrinterName(entry->name);
00052     prt->setName(entry->name);
00053     prt->setType(KMPrinter::Printer);
00054     return prt;
00055 }
00056 
00057 bool LprHandler::completePrinter(KMPrinter *prt, PrintcapEntry *entry, bool)
00058 {
00059     prt->setDescription(i18n("Unknown (unrecognized entry)"));
00060     QString val = entry->field("lp");
00061     KURL uri;
00062     if (!val.isEmpty() && val != "/dev/null")
00063     {
00064         int p = val.find('@');
00065         if (p != -1)
00066         {
00067             prt->setLocation(i18n("Remote queue (%1) on %2").arg(val.left(p)).arg(val.mid(p+1)));
00068             uri.setProtocol("lpd");
00069             uri.setHost(val.mid(p+1));
00070             uri.setPath("/" + val.left(p));
00071         }
00072         else if ((p = val.find('%')) != -1)
00073         {
00074             prt->setLocation(i18n("Network printer (%1)").arg("socket"));
00075             uri.setProtocol("socket");
00076             uri.setHost(val.left(p));
00077             uri.setPort(val.mid(p+1).toInt());
00078         }
00079         else
00080         {
00081             prt->setLocation(i18n("Local printer on %1").arg(val));
00082             uri.setProtocol("parallel");
00083             uri.setPath(val);
00084         }
00085     }
00086     else if (!(val = entry->field("rp")).isEmpty())
00087     {
00088         QString rm = entry->has("rm") ?
00089                 entry->field("rm") :
00090                 LprSettings::self()->defaultRemoteHost();
00091         prt->setLocation(i18n("Remote queue (%1) on %2").arg(val).arg(rm));
00092         uri.setProtocol("lpd");
00093         uri.setHost(rm);
00094         uri.setPath("/" + val);
00095     }
00096     else
00097         prt->setLocation(i18n("Unknown (unrecognized entry)"));
00098     prt->setDevice(uri.url());
00099     return true;
00100 }
00101 
00102 DrMain* LprHandler::loadDriver(KMPrinter*, PrintcapEntry*, bool)
00103 {
00104     manager()->setErrorMsg(i18n("Unrecognized entry."));
00105     return NULL;
00106 }
00107 
00108 bool LprHandler::savePrinterDriver(KMPrinter*, PrintcapEntry*, DrMain*, bool*)
00109 {
00110     manager()->setErrorMsg(i18n("Unrecognized entry."));
00111     return false;
00112 }
00113 
00114 DrMain* LprHandler::loadDbDriver(const QString&)
00115 {
00116     manager()->setErrorMsg(i18n("Unrecognized entry."));
00117     return NULL;
00118 }
00119 
00120 PrintcapEntry* LprHandler::createEntry(KMPrinter *prt)
00121 {
00122     // this default handler only supports local parallel and remote lpd URIs
00123     KURL    uri ( prt->device() );
00124     QString prot = uri.protocol();
00125     if (!prot.isEmpty() && prot != "parallel" && prot != "file" && prot != "lpd" && prot != "socket")
00126     {
00127         manager()->setErrorMsg(i18n("Unsupported backend: %1.").arg(prot));
00128         return NULL;
00129     }
00130     PrintcapEntry   *entry = new PrintcapEntry;
00131     entry->comment = "# Default handler";
00132     if (prot == "lpd")
00133     {
00134         entry->addField("rm", Field::String, uri.host());
00135         QString rp = uri.path();
00136         if (rp[0] == '/')
00137             rp = rp.mid(1);
00138         entry->addField("rp", Field::String, rp);
00139         // force this entry to null (otherwise it seems it's redirected
00140         // to /dev/lp0 by default)
00141         entry->addField("lp", Field::String, QString::null);
00142     }
00143     else if ( prot == "socket" )
00144     {
00145         QString lp = uri.host();
00146         if ( uri.port() == 0 )
00147             lp.append( "%9100" );
00148         else
00149             lp.append( "%" ).append( QString::number( uri.port() ) );
00150     }
00151     else
00152     {
00153         entry->addField("lp", Field::String, uri.path());
00154     }
00155     return entry;
00156 }
00157 
00158 bool LprHandler::removePrinter(KMPrinter*, PrintcapEntry*)
00159 {
00160     return true;
00161 }
00162 
00163 QString LprHandler::printOptions(KPrinter*)
00164 {
00165     return QString::null;
00166 }
00167 
00168 void LprHandler::reset()
00169 {
00170 }
00171 
00172 DrMain* LprHandler::loadToolDriver(const QString& filename)
00173 {
00174     QFile   f(filename);
00175     if (f.open(IO_ReadOnly))
00176     {
00177         DrMain  *driver = new DrMain;
00178         QValueStack<DrGroup*>   groups;
00179         QTextStream t(&f);
00180         QStringList l;
00181         DrListOption    *lopt(0);
00182         DrBase  *opt(0);
00183 
00184         groups.push(driver);
00185         driver->set("text", "Tool Driver");
00186         while (!t.atEnd())
00187         {
00188             l = QStringList::split('|', t.readLine().stripWhiteSpace(), false);
00189             if (l.count() == 0)
00190                 continue;
00191             if (l[0] == "GROUP")
00192             {
00193                 DrGroup *grp = new DrGroup;
00194                 grp->setName(l[1]);
00195                 grp->set("text", l[2]);
00196                 groups.top()->addGroup(grp);
00197                 groups.push(grp);
00198             }
00199             else if (l[0] == "ENDGROUP")
00200             {
00201                 groups.pop();
00202             }
00203             else if (l[0] == "OPTION")
00204             {
00205                 opt = 0;
00206                 lopt = 0;
00207                 if (l.count() > 3)
00208                 {
00209                     if (l[3] == "STRING")
00210                         opt = new DrStringOption;
00211                     else if (l[3] == "BOOLEAN")
00212                     {
00213                         lopt = new DrBooleanOption;
00214                         opt = lopt;
00215                     }
00216                 }
00217                 else
00218                 {
00219                     lopt = new DrListOption;
00220                     opt = lopt;
00221                 }
00222                 if (opt)
00223                 {
00224                     opt->setName(l[1]);
00225                     opt->set("text", l[2]);
00226                     groups.top()->addOption(opt);
00227                 }
00228             }
00229             else if (l[0] == "CHOICE" && lopt)
00230             {
00231                 DrBase  *ch = new DrBase;
00232                 ch->setName(l[1]);
00233                 ch->set("text", l[2]);
00234                 lopt->addChoice(ch);
00235             }
00236             else if (l[0] == "DEFAULT" && opt)
00237             {
00238                 opt->setValueText(l[1]);
00239                 opt->set("default", l[1]);
00240             }
00241         }
00242         return driver;
00243     }
00244     return NULL;
00245 }
00246 
00247 QString LprHandler::driverDirectory()
00248 {
00249     if (m_cacheddriverdir.isEmpty())
00250         m_cacheddriverdir = driverDirInternal();
00251     return m_cacheddriverdir;
00252 }
00253 
00254 QString LprHandler::driverDirInternal()
00255 {
00256     return QString::null;
00257 }
00258 
00259 QString LprHandler::locateDir(const QString& dirname, const QString& paths)
00260 {
00261     QStringList pathlist = QStringList::split(':', paths, false);
00262     for (QStringList::ConstIterator it=pathlist.begin(); it!=pathlist.end(); ++it)
00263     {
00264         QString testpath = *it + "/" + dirname;
00265         if (::access(QFile::encodeName(testpath), F_OK) == 0)
00266             return testpath;
00267     }
00268     return QString::null;
00269 }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:12:55 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003