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., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, 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         entry->addField("lp", Field::String, lp);
00151     }
00152     else
00153     {
00154         entry->addField("lp", Field::String, uri.path());
00155     }
00156     return entry;
00157 }
00158 
00159 bool LprHandler::removePrinter(KMPrinter*, PrintcapEntry*)
00160 {
00161     return true;
00162 }
00163 
00164 QString LprHandler::printOptions(KPrinter*)
00165 {
00166     return QString::null;
00167 }
00168 
00169 void LprHandler::reset()
00170 {
00171 }
00172 
00173 DrMain* LprHandler::loadToolDriver(const QString& filename)
00174 {
00175     QFile   f(filename);
00176     if (f.open(IO_ReadOnly))
00177     {
00178         DrMain  *driver = new DrMain;
00179         QValueStack<DrGroup*>   groups;
00180         QTextStream t(&f);
00181         QStringList l;
00182         DrListOption    *lopt(0);
00183         DrBase  *opt(0);
00184 
00185         groups.push(driver);
00186         driver->set("text", "Tool Driver");
00187         while (!t.atEnd())
00188         {
00189             l = QStringList::split('|', t.readLine().stripWhiteSpace(), false);
00190             if (l.count() == 0)
00191                 continue;
00192             if (l[0] == "GROUP")
00193             {
00194                 DrGroup *grp = new DrGroup;
00195                 grp->setName(l[1]);
00196                 grp->set("text", l[2]);
00197                 groups.top()->addGroup(grp);
00198                 groups.push(grp);
00199             }
00200             else if (l[0] == "ENDGROUP")
00201             {
00202                 groups.pop();
00203             }
00204             else if (l[0] == "OPTION")
00205             {
00206                 opt = 0;
00207                 lopt = 0;
00208                 if (l.count() > 3)
00209                 {
00210                     if (l[3] == "STRING")
00211                         opt = new DrStringOption;
00212                     else if (l[3] == "BOOLEAN")
00213                     {
00214                         lopt = new DrBooleanOption;
00215                         opt = lopt;
00216                     }
00217                 }
00218                 else
00219                 {
00220                     lopt = new DrListOption;
00221                     opt = lopt;
00222                 }
00223                 if (opt)
00224                 {
00225                     opt->setName(l[1]);
00226                     opt->set("text", l[2]);
00227                     groups.top()->addOption(opt);
00228                 }
00229             }
00230             else if (l[0] == "CHOICE" && lopt)
00231             {
00232                 DrBase  *ch = new DrBase;
00233                 ch->setName(l[1]);
00234                 ch->set("text", l[2]);
00235                 lopt->addChoice(ch);
00236             }
00237             else if (l[0] == "DEFAULT" && opt)
00238             {
00239                 opt->setValueText(l[1]);
00240                 opt->set("default", l[1]);
00241             }
00242         }
00243         return driver;
00244     }
00245     return NULL;
00246 }
00247 
00248 QString LprHandler::driverDirectory()
00249 {
00250     if (m_cacheddriverdir.isEmpty())
00251         m_cacheddriverdir = driverDirInternal();
00252     return m_cacheddriverdir;
00253 }
00254 
00255 QString LprHandler::driverDirInternal()
00256 {
00257     return QString::null;
00258 }
00259 
00260 QString LprHandler::locateDir(const QString& dirname, const QString& paths)
00261 {
00262     QStringList pathlist = QStringList::split(':', paths, false);
00263     for (QStringList::ConstIterator it=pathlist.begin(); it!=pathlist.end(); ++it)
00264     {
00265         QString testpath = *it + "/" + dirname;
00266         if (::access(QFile::encodeName(testpath), F_OK) == 0)
00267             return testpath;
00268     }
00269     return QString::null;
00270 }
KDE Home | KDE Accessibility Home | Description of Access Keys