kmfoomaticmanager.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 "kmfoomaticmanager.h"
00021 #include "kpipeprocess.h"
00022 #include "driver.h"
00023 
00024 #include <qdom.h>
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <kprocess.h>
00028 
00029 #include <unistd.h>
00030 
00031 KMFoomaticManager::KMFoomaticManager(QObject *parent, const char *name, const QStringList & /*args*/)
00032 : KMManager(parent,name)
00033 {
00034     setHasManagement(getuid() == 0);
00035     setPrinterOperationMask(KMManager::PrinterConfigure);
00036 }
00037 
00038 KMFoomaticManager::~KMFoomaticManager()
00039 {
00040 }
00041 
00042 void KMFoomaticManager::listPrinters()
00043 {
00044     KPipeProcess    proc("foomatic-configure -Q -q -r");
00045     QDomDocument    doc;
00046 
00047     doc.setContent(&proc);
00048     QDomElement docElem = doc.documentElement();
00049     if (docElem.isNull() || docElem.tagName() != "queues")
00050         return;
00051 
00052     QDomNode    queueNode = docElem.firstChild();
00053     while (!queueNode.isNull())
00054     {
00055         QDomElement queueElem = queueNode.toElement();
00056         if (!queueElem.isNull() && queueElem.tagName() == "queue")
00057         {
00058             KMPrinter   *printer = createPrinterFromElement(&queueElem);
00059             if (printer)
00060                 addPrinter(printer);
00061         }
00062         queueNode = queueNode.nextSibling();
00063     }
00064 }
00065 
00066 DrMain* KMFoomaticManager::loadPrinterDriver(KMPrinter *printer, bool)
00067 {
00068     if (printer->option("foomatic") != "1")
00069     {
00070         setErrorMsg(i18n("This is not a Foomatic printer"));
00071         return NULL;
00072     }
00073     else if (printer->option("driver").isEmpty() || printer->option("printer").isEmpty())
00074     {
00075         setErrorMsg(i18n("Some printer information are missing"));
00076         return NULL;
00077     }
00078 
00079     QString cmd = "foomatic-combo-xml -p ";
00080     cmd += KProcess::quote(printer->option("printer"));
00081     cmd += " -d ";
00082     cmd += KProcess::quote(printer->option("driver"));
00083     KPipeProcess    proc(cmd);
00084     QDomDocument    doc;
00085     doc.setContent(&proc);
00086     QDomElement docElem = doc.documentElement();
00087     return createDriverFromXML(&docElem);
00088 }
00089 
00090 KMPrinter* KMFoomaticManager::createPrinterFromElement(QDomElement *elem)
00091 {
00092     QDomElement e = elem->namedItem("name").toElement();
00093     if (!e.isNull())
00094     {
00095         KMPrinter   *printer = new KMPrinter;
00096         printer->setType(KMPrinter::Printer);
00097         printer->setName(e.text());
00098         printer->setPrinterName(e.text());
00099         printer->setState(KMPrinter::Idle);
00100         /*if (printer->name().find('/') != -1)
00101         {
00102             QString s(printer->name());
00103             int p = s.find('/');
00104             printer->setPrinterName(s.left(p));
00105             printer->setInstanceName(s.mid(p+1));
00106             printer->addType(KMPrinter::Virtual);
00107         }*/
00108 
00109         if (!(e=elem->namedItem("description").toElement()).isNull())
00110             printer->setDescription(e.text());
00111         if (!(e=elem->namedItem("location").toElement()).isNull())
00112             printer->setLocation(e.text());
00113         if (!(e=elem->namedItem("connect").toElement()).isNull())
00114             printer->setDevice(e.text());
00115 
00116         printer->setOption("foomatic", elem->attribute("foomatic"));
00117         printer->setOption("spooler", elem->attribute("spooler"));
00118         if (elem->attribute("foomatic") == "1")
00119         {
00120             if (!(e=elem->namedItem("printer").toElement()).isNull())
00121                 printer->setOption("printer", e.text());
00122             if (!(e=elem->namedItem("driver").toElement()).isNull())
00123                 printer->setOption("driver", e.text());
00124         }
00125 
00126         return printer;
00127     }
00128     return NULL;
00129 }
00130 
00131 DrMain* KMFoomaticManager::createDriverFromXML(QDomElement *elem)
00132 {
00133     DrMain  *driver = new DrMain();
00134     QDomElement pelem = elem->namedItem("printer").toElement(), delem = elem->namedItem("driver").toElement();
00135     if (!pelem.isNull() && !delem.isNull())
00136     {
00137         driver->set("manufacturer", pelem.namedItem("make").toElement().text());
00138         driver->set("model", pelem.namedItem("model").toElement().text());
00139         QString s = QString::fromLatin1("%1 %2 (%3)").arg(driver->get("manufacturer")).arg(driver->get("model")).arg(delem.namedItem("name").toElement().text());
00140         driver->set("description", s);
00141         driver->set("text", s);
00142 
00143         QDomElement opts = elem->namedItem("options").toElement();
00144         if (!opts.isNull())
00145         {
00146             QDomElement o = opts.firstChild().toElement();
00147             while (!o.isNull())
00148             {
00149                 if (o.tagName() == "option")
00150                 {
00151                     QString type = o.attribute("type");
00152                     DrBase *dropt(0);
00153 
00154                     if (type == "bool" || type == "enum")
00155                     {
00156                         if (type == "bool") dropt = new DrBooleanOption();
00157                         else dropt = new DrListOption();
00158                         QString defval = o.namedItem("arg_defval").toElement().text(), valuetext;
00159                         QDomNode    val = o.namedItem("enum_vals").firstChild();
00160                         while (!val.isNull())
00161                         {
00162                             DrBase  *choice = new DrBase();
00163                             choice->setName(val.namedItem("ev_shortname").namedItem("en").toElement().text());
00164                             choice->set("text", i18n(val.namedItem("ev_longname").namedItem("en").toElement().text().latin1()));
00165                             static_cast<DrListOption*>(dropt)->addChoice(choice);
00166                             if (val.toElement().attribute("id") == defval)
00167                                 valuetext = choice->name();
00168 
00169                             val = val.nextSibling();
00170                         }
00171                         dropt->set("default", valuetext);
00172                         dropt->setValueText(valuetext);
00173                     }
00174                     else if (type == "int" || type == "float")
00175                     {
00176                         if (type == "int") dropt = new DrIntegerOption();
00177                         else dropt = new DrFloatOption();
00178                         dropt->set("minval", o.namedItem("arg_min").toElement().text());
00179                         dropt->set("maxval", o.namedItem("arg_max").toElement().text());
00180                         QString defval = o.namedItem("arg_defval").toElement().text();
00181                         dropt->set("default", defval);
00182                         dropt->setValueText(defval);
00183                     }
00184 
00185                     if (dropt)
00186                     {
00187                         dropt->setName(o.namedItem("arg_shortname").namedItem("en").toElement().text());
00188                         dropt->set("text", i18n(o.namedItem("arg_longname").namedItem("en").toElement().text().latin1()));
00189                         driver->addOption(dropt);
00190                     }
00191                 }
00192                 o = o.nextSibling().toElement();
00193             }
00194         }
00195     }
00196     return driver;
00197 }
KDE Home | KDE Accessibility Home | Description of Access Keys