kcupsprinterimpl.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 "kcupsprinterimpl.h"
00021 #include "kprinter.h"
00022 #include "driver.h"
00023 #include "kmfactory.h"
00024 #include "kmmanager.h"
00025 #include "cupsinfos.h"
00026 
00027 #include <qfile.h>
00028 #include <cups/cups.h>
00029 #include <stdlib.h>
00030 #include <kprocess.h>
00031 
00032 static void mapToCupsOptions(const QMap<QString,QString>& opts, QString& cmd);
00033 
00034 QSize rangeToSize(const QString& s)
00035 {
00036     QString range = s;
00037     int p(-1);
00038     int from, to;
00039 
00040     if ((p=range.find(',')) != -1)
00041         range.truncate(p);
00042     if ((p=range.find('-')) != -1)
00043     {
00044         from = range.left(p).toInt();
00045         to = range.right(range.length()-p-1).toInt();
00046     }
00047     else if (!range.isEmpty())
00048         from = to = range.toInt();
00049     else
00050         from = to = 0;
00051 
00052     return QSize(from,to);
00053 }
00054 //******************************************************************************************************
00055 
00056 KCupsPrinterImpl::KCupsPrinterImpl(QObject *parent, const char *name, const QStringList & /*args*/)
00057 : KPrinterImpl(parent,name)
00058 {
00059 }
00060 
00061 KCupsPrinterImpl::~KCupsPrinterImpl()
00062 {
00063 }
00064 
00065 bool KCupsPrinterImpl::setupCommand(QString& cmd, KPrinter *printer)
00066 {
00067     // check printer object
00068     if (!printer) return false;
00069 
00070     QString hoststr = QString::fromLatin1("%1:%2").arg(CupsInfos::self()->host()).arg(CupsInfos::self()->port());
00071     cmd = QString::fromLatin1("cupsdoprint -P %1 -J %3 -H %2").arg(quote(printer->printerName())).arg(quote(hoststr)).arg(quote(printer->docName()));
00072     if (!CupsInfos::self()->login().isEmpty())
00073     {
00074         QString userstr(CupsInfos::self()->login());
00075         //if (!CupsInfos::self()->password().isEmpty())
00076         //  userstr += (":" + CupsInfos::self()->password());
00077         cmd.append(" -U ").append(quote(userstr));
00078     }
00079     mapToCupsOptions(printer->options(),cmd);
00080     return true;
00081 }
00082 
00083 void KCupsPrinterImpl::preparePrinting(KPrinter *printer)
00084 {
00085     // process orientation
00086     QString o = printer->option("orientation-requested");
00087     printer->setOption("kde-orientation",(o == "4" || o == "5" ? "Landscape" : "Portrait"));
00088     // if it's a Qt application, then convert orientation as it will be handled by Qt directly
00089     if (printer->applicationType() == KPrinter::Dialog)
00090         printer->setOption("orientation-requested",(o == "5" || o == "6" ? "6" : "3"));
00091 
00092     // translate copies number
00093     if (!printer->option("kde-copies").isEmpty()) printer->setOption("copies",printer->option("kde-copies"));
00094 
00095     // page ranges are handled by CUPS, so application should print all pages
00096     if (printer->pageSelection() == KPrinter::SystemSide)
00097     { // Qt => CUPS
00098         // translations
00099         if (!printer->option("kde-range").isEmpty())
00100             printer->setOption("page-ranges",printer->option("kde-range"));
00101         if (printer->option("kde-pageorder") == "Reverse")
00102             printer->setOption("OutputOrder",printer->option("kde-pageorder"));
00103         o = printer->option("kde-pageset");
00104         if (!o.isEmpty() && o != "0")
00105             printer->setOption("page-set",(o == "1" ? "odd" : "even"));
00106         printer->setOption("multiple-document-handling",(printer->option("kde-collate") == "Collate" ? "separate-documents-collated-copies" : "separate-documents-uncollated-copies"));
00107     }
00108     else
00109     { // No translation needed (but range => (from,to))
00110         QString range = printer->option("kde-range");
00111         if (!range.isEmpty())
00112         {
00113             QSize   s = rangeToSize(range);
00114             printer->setOption("kde-from",QString::number(s.width()));
00115             printer->setOption("kde-to",QString::number(s.height()));
00116         }
00117     }
00118 
00119     // needed for page size and margins
00120     KPrinterImpl::preparePrinting(printer);
00121 }
00122 
00123 void KCupsPrinterImpl::broadcastOption(const QString& key, const QString& value)
00124 {
00125     KPrinterImpl::broadcastOption(key,value);
00126     if (key == "kde-orientation")
00127         KPrinterImpl::broadcastOption("orientation-requested",(value == "Landscape" ? "4" : "3"));
00128     else if (key == "kde-pagesize")
00129     {
00130         QString pagename = QString::fromLatin1(pageSizeToPageName((KPrinter::PageSize)value.toInt()));
00131         KPrinterImpl::broadcastOption("PageSize",pagename);
00132         // simple hack for classes
00133         KPrinterImpl::broadcastOption("media",pagename);
00134     }
00135 }
00136 
00137 //******************************************************************************************************
00138 
00139 static void mapToCupsOptions(const QMap<QString,QString>& opts, QString& cmd)
00140 {
00141     QString optstr;
00142     for (QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
00143     {
00144         // only encode those options that doesn't start with "kde-" or "app-".
00145         if (!it.key().startsWith("kde-") && !it.key().startsWith("app-") && !it.key().startsWith("_kde"))
00146         {
00147             QString key = it.key();
00148             if (key.startsWith("KDEPrint-"))
00149                 /* Those are keys added by the "Additional Tags" page. *
00150                  * Strip the prefix to build valid a CUPS option.      */
00151                 key = key.mid(9);
00152             optstr.append(" ").append(key);
00153             if (!it.data().isEmpty())
00154                 optstr.append("=").append(it.data());
00155         }
00156     }
00157     if (!optstr.isEmpty())
00158         cmd.append(" -o ").append( KProcess::quote( optstr ) );
00159 }
KDE Home | KDE Accessibility Home | Description of Access Keys