You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.5 KiB
160 lines
5.5 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* 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 "kcupsprinterimpl.h"
|
||
|
#include "kprinter.h"
|
||
|
#include "driver.h"
|
||
|
#include "kmfactory.h"
|
||
|
#include "kmmanager.h"
|
||
|
#include "cupsinfos.h"
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
#include <cups/cups.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <kprocess.h>
|
||
|
|
||
15 years ago
|
static void mapToCupsOptions(const TQMap<TQString,TQString>& opts, TQString& cmd);
|
||
15 years ago
|
|
||
15 years ago
|
TQSize rangeToSize(const TQString& s)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString range = s;
|
||
15 years ago
|
int p(-1);
|
||
|
int from, to;
|
||
|
|
||
13 years ago
|
if ((p=range.find(',')) != -1)
|
||
15 years ago
|
range.truncate(p);
|
||
13 years ago
|
if ((p=range.find('-')) != -1)
|
||
15 years ago
|
{
|
||
|
from = range.left(p).toInt();
|
||
|
to = range.right(range.length()-p-1).toInt();
|
||
|
}
|
||
|
else if (!range.isEmpty())
|
||
|
from = to = range.toInt();
|
||
|
else
|
||
|
from = to = 0;
|
||
|
|
||
15 years ago
|
return TQSize(from,to);
|
||
15 years ago
|
}
|
||
|
//******************************************************************************************************
|
||
|
|
||
15 years ago
|
KCupsPrinterImpl::KCupsPrinterImpl(TQObject *parent, const char *name, const TQStringList & /*args*/)
|
||
15 years ago
|
: KPrinterImpl(parent,name)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
KCupsPrinterImpl::~KCupsPrinterImpl()
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KCupsPrinterImpl::setupCommand(TQString& cmd, KPrinter *printer)
|
||
15 years ago
|
{
|
||
|
// check printer object
|
||
|
if (!printer) return false;
|
||
|
|
||
14 years ago
|
TQString hoststr = TQString::tqfromLatin1("%1:%2").arg(CupsInfos::self()->host()).arg(CupsInfos::self()->port());
|
||
14 years ago
|
cmd = TQString::tqfromLatin1("cupsdoprint -P %1 -J %3 -H %2").arg(quote(printer->printerName())).arg(quote(hoststr)).arg(quote(printer->docName()));
|
||
15 years ago
|
if (!CupsInfos::self()->login().isEmpty())
|
||
|
{
|
||
14 years ago
|
TQString userstr(CupsInfos::self()->login());
|
||
15 years ago
|
//if (!CupsInfos::self()->password().isEmpty())
|
||
|
// userstr += (":" + CupsInfos::self()->password());
|
||
|
cmd.append(" -U ").append(quote(userstr));
|
||
|
}
|
||
|
mapToCupsOptions(printer->options(),cmd);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void KCupsPrinterImpl::preparePrinting(KPrinter *printer)
|
||
|
{
|
||
|
// process orientation
|
||
14 years ago
|
TQString o = printer->option("orientation-requested");
|
||
15 years ago
|
printer->setOption("kde-orientation",(o == "4" || o == "5" ? "Landscape" : "Portrait"));
|
||
14 years ago
|
// if it's a TQt application, then convert orientation as it will be handled by TQt directly
|
||
15 years ago
|
if (printer->applicationType() == KPrinter::Dialog)
|
||
|
printer->setOption("orientation-requested",(o == "5" || o == "6" ? "6" : "3"));
|
||
|
|
||
|
// translate copies number
|
||
|
if (!printer->option("kde-copies").isEmpty()) printer->setOption("copies",printer->option("kde-copies"));
|
||
|
|
||
|
// page ranges are handled by CUPS, so application should print all pages
|
||
|
if (printer->pageSelection() == KPrinter::SystemSide)
|
||
14 years ago
|
{ // TQt => CUPS
|
||
15 years ago
|
// translations
|
||
|
if (!printer->option("kde-range").isEmpty())
|
||
|
printer->setOption("page-ranges",printer->option("kde-range"));
|
||
|
if (printer->option("kde-pageorder") == "Reverse")
|
||
|
printer->setOption("OutputOrder",printer->option("kde-pageorder"));
|
||
|
o = printer->option("kde-pageset");
|
||
|
if (!o.isEmpty() && o != "0")
|
||
|
printer->setOption("page-set",(o == "1" ? "odd" : "even"));
|
||
|
printer->setOption("multiple-document-handling",(printer->option("kde-collate") == "Collate" ? "separate-documents-collated-copies" : "separate-documents-uncollated-copies"));
|
||
|
}
|
||
|
else
|
||
|
{ // No translation needed (but range => (from,to))
|
||
15 years ago
|
TQString range = printer->option("kde-range");
|
||
15 years ago
|
if (!range.isEmpty())
|
||
|
{
|
||
14 years ago
|
TQSize s = rangeToSize(range);
|
||
15 years ago
|
printer->setOption("kde-from",TQString::number(s.width()));
|
||
|
printer->setOption("kde-to",TQString::number(s.height()));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// needed for page size and margins
|
||
|
KPrinterImpl::preparePrinting(printer);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KCupsPrinterImpl::broadcastOption(const TQString& key, const TQString& value)
|
||
15 years ago
|
{
|
||
|
KPrinterImpl::broadcastOption(key,value);
|
||
|
if (key == "kde-orientation")
|
||
|
KPrinterImpl::broadcastOption("orientation-requested",(value == "Landscape" ? "4" : "3"));
|
||
|
else if (key == "kde-pagesize")
|
||
|
{
|
||
14 years ago
|
TQString pagename = TQString::tqfromLatin1(pageSizeToPageName((KPrinter::PageSize)value.toInt()));
|
||
15 years ago
|
KPrinterImpl::broadcastOption("PageSize",pagename);
|
||
|
// simple hack for classes
|
||
|
KPrinterImpl::broadcastOption("media",pagename);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//******************************************************************************************************
|
||
|
|
||
15 years ago
|
static void mapToCupsOptions(const TQMap<TQString,TQString>& opts, TQString& cmd)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString optstr;
|
||
15 years ago
|
for (TQMap<TQString,TQString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
|
||
15 years ago
|
{
|
||
|
// only encode those options that doesn't start with "kde-" or "app-".
|
||
|
if (!it.key().startsWith("kde-") && !it.key().startsWith("app-") && !it.key().startsWith("_kde"))
|
||
|
{
|
||
15 years ago
|
TQString key = it.key();
|
||
15 years ago
|
if (key.startsWith("KDEPrint-"))
|
||
|
/* Those are keys added by the "Additional Tags" page. *
|
||
|
* Strip the prefix to build valid a CUPS option. */
|
||
|
key = key.mid(9);
|
||
|
optstr.append(" ").append(key);
|
||
|
if (!it.data().isEmpty())
|
||
|
optstr.append("=").append(it.data());
|
||
|
}
|
||
|
}
|
||
|
if (!optstr.isEmpty())
|
||
|
cmd.append(" -o ").append( KProcess::quote( optstr ) );
|
||
|
}
|