2007-11-26 21:43:54 +00:00
|
|
|
/***************************************************************************
|
2010-06-04 08:25:25 +00:00
|
|
|
* Copyright (C) 2007,2010 by John Layt <john@layt.net> *
|
2007-11-26 21:43:54 +00:00
|
|
|
* *
|
|
|
|
* FilePrinterPreview based on KPrintPreview (originally LGPL) *
|
|
|
|
* Copyright (c) 2007 Alex Merry <huntedhacker@tiscali.co.uk> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "fileprinter.h"
|
|
|
|
|
2014-08-07 23:35:27 +00:00
|
|
|
#include <QtPrintSupport/QPrinter>
|
2007-11-26 21:43:54 +00:00
|
|
|
#include <QPrintEngine>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QSize>
|
|
|
|
#include <QtCore/QFile>
|
2008-12-14 23:18:52 +00:00
|
|
|
#include <QtCore/QFileInfo>
|
2014-08-07 23:35:27 +00:00
|
|
|
#include <QtWidgets/QLabel>
|
2007-11-26 21:43:54 +00:00
|
|
|
#include <QtGui/QShowEvent>
|
2008-12-11 23:04:07 +00:00
|
|
|
#include <QtNetwork/QTcpSocket>
|
2007-11-26 21:43:54 +00:00
|
|
|
|
|
|
|
#include <KProcess>
|
|
|
|
#include <KShell>
|
2014-09-11 19:12:27 +00:00
|
|
|
#include <QtCore/QDebug>
|
2014-10-07 11:21:58 +00:00
|
|
|
#include <QStandardPaths>
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2008-01-20 16:39:21 +00:00
|
|
|
#include "debug_p.h"
|
2007-11-26 21:43:54 +00:00
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
int FilePrinter::printFile( QPrinter &printer, const QString file,
|
|
|
|
QPrinter::Orientation documentOrientation, FileDeletePolicy fileDeletePolicy,
|
|
|
|
PageSelectPolicy pageSelectPolicy, const QString &pageRange )
|
2007-11-26 21:43:54 +00:00
|
|
|
{
|
|
|
|
FilePrinter fp;
|
2011-06-05 22:40:22 +00:00
|
|
|
return fp.doPrintFiles( printer, QStringList( file ), fileDeletePolicy, pageSelectPolicy, pageRange,
|
|
|
|
documentOrientation );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
int FilePrinter::doPrintFiles( QPrinter &printer, QStringList fileList, FileDeletePolicy fileDeletePolicy,
|
|
|
|
PageSelectPolicy pageSelectPolicy, const QString &pageRange,
|
|
|
|
QPrinter::Orientation documentOrientation )
|
|
|
|
{
|
2007-11-26 21:43:54 +00:00
|
|
|
|
|
|
|
if ( fileList.size() < 1 ) {
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
|
2008-11-11 18:48:40 +00:00
|
|
|
for (QStringList::ConstIterator it = fileList.constBegin(); it != fileList.constEnd(); ++it) {
|
2007-11-26 21:43:54 +00:00
|
|
|
if (!QFile::exists(*it)) {
|
|
|
|
return -7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( printer.printerState() == QPrinter::Aborted || printer.printerState() == QPrinter::Error ) {
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
QString exe;
|
|
|
|
QStringList argList;
|
|
|
|
int ret;
|
|
|
|
|
2007-11-26 21:43:54 +00:00
|
|
|
// Print to File if a filename set, assumes there must be only 1 file
|
|
|
|
if ( !printer.outputFileName().isEmpty() ) {
|
|
|
|
|
|
|
|
if ( QFile::exists( printer.outputFileName() ) ) {
|
|
|
|
QFile::remove( printer.outputFileName() );
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
QFileInfo inputFileInfo = QFileInfo( fileList[0] );
|
|
|
|
QFileInfo outputFileInfo = QFileInfo( printer.outputFileName() );
|
|
|
|
|
2010-04-14 21:38:40 +00:00
|
|
|
bool doDeleteFile = (fileDeletePolicy == FilePrinter::SystemDeletesFiles);
|
2008-12-14 23:18:52 +00:00
|
|
|
if ( inputFileInfo.suffix() == outputFileInfo.suffix() ) {
|
2010-04-14 21:38:40 +00:00
|
|
|
if ( doDeleteFile ) {
|
|
|
|
bool res = QFile::rename( fileList[0], printer.outputFileName() );
|
|
|
|
if ( res ) {
|
|
|
|
doDeleteFile = false;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = -5;
|
|
|
|
}
|
2009-12-24 14:39:49 +00:00
|
|
|
} else {
|
2010-04-14 21:38:40 +00:00
|
|
|
bool res = QFile::copy( fileList[0], printer.outputFileName() );
|
|
|
|
if ( res ) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = -5;
|
|
|
|
}
|
2009-12-24 14:39:49 +00:00
|
|
|
}
|
2015-10-29 12:37:11 +00:00
|
|
|
} else if ( inputFileInfo.suffix() == QLatin1String("ps") && printer.outputFormat() == QPrinter::PdfFormat && ps2pdfAvailable() ) {
|
|
|
|
exe = QStringLiteral("ps2pdf");
|
2008-12-14 23:18:52 +00:00
|
|
|
argList << fileList[0] << printer.outputFileName();
|
2014-09-11 17:36:01 +00:00
|
|
|
qCDebug(OkularCoreDebug) << "Executing" << exe << "with arguments" << argList;
|
2008-12-14 23:18:52 +00:00
|
|
|
ret = KProcess::execute( exe, argList );
|
2016-07-21 18:33:04 +00:00
|
|
|
} else if ( inputFileInfo.suffix() == "pdf" && printer.outputFormat() == QPrinter::NativeFormat && pdf2psAvailable() ) {
|
|
|
|
exe = "pdf2ps";
|
|
|
|
argList << fileList[0] << printer.outputFileName();
|
|
|
|
qCDebug(OkularCoreDebug) << "Executing" << exe << "with arguments" << argList;
|
|
|
|
ret = KProcess::execute( exe, argList );
|
2008-12-14 23:18:52 +00:00
|
|
|
} else {
|
|
|
|
ret = -5;
|
|
|
|
}
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2010-04-14 21:38:40 +00:00
|
|
|
if ( doDeleteFile ) {
|
2007-11-26 21:43:54 +00:00
|
|
|
QFile::remove( fileList[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
} else { // Print to a printer via lpr command
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
//Decide what executable to use to print with, need the CUPS version of lpr if available
|
|
|
|
//Some distros name the CUPS version of lpr as lpr-cups or lpr.cups so try those first
|
|
|
|
//before default to lpr, or failing that to lp
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( !QStandardPaths::findExecutable(QStringLiteral("lpr-cups")).isEmpty() ) {
|
|
|
|
exe = QStringLiteral("lpr-cups");
|
|
|
|
} else if ( !QStandardPaths::findExecutable(QStringLiteral("lpr.cups")).isEmpty() ) {
|
|
|
|
exe = QStringLiteral("lpr.cups");
|
|
|
|
} else if ( !QStandardPaths::findExecutable(QStringLiteral("lpr")).isEmpty() ) {
|
|
|
|
exe = QStringLiteral("lpr");
|
|
|
|
} else if ( !QStandardPaths::findExecutable(QStringLiteral("lp")).isEmpty() ) {
|
|
|
|
exe = QStringLiteral("lp");
|
2008-12-14 23:18:52 +00:00
|
|
|
} else {
|
|
|
|
return -9;
|
|
|
|
}
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
bool useCupsOptions = cupsAvailable();
|
|
|
|
argList = printArguments( printer, fileDeletePolicy, pageSelectPolicy,
|
2011-06-05 22:40:22 +00:00
|
|
|
useCupsOptions, pageRange, exe, documentOrientation ) << fileList;
|
2014-09-11 17:36:01 +00:00
|
|
|
qCDebug(OkularCoreDebug) << "Executing" << exe << "with arguments" << argList;
|
2007-11-26 21:43:54 +00:00
|
|
|
|
|
|
|
ret = KProcess::execute( exe, argList );
|
2008-12-14 23:18:52 +00:00
|
|
|
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-04 19:04:05 +00:00
|
|
|
QList<int> FilePrinter::pageList( QPrinter &printer, int lastPage, const QList<int> &selectedPageList )
|
|
|
|
{
|
|
|
|
return pageList( printer, lastPage, 0, selectedPageList );
|
|
|
|
}
|
|
|
|
|
2010-06-04 08:25:25 +00:00
|
|
|
QList<int> FilePrinter::pageList( QPrinter &printer, int lastPage,
|
|
|
|
int currentPage, const QList<int> &selectedPageList )
|
2007-11-26 21:43:54 +00:00
|
|
|
{
|
|
|
|
if ( printer.printRange() == QPrinter::Selection) {
|
|
|
|
return selectedPageList;
|
|
|
|
}
|
|
|
|
|
|
|
|
int startPage, endPage;
|
|
|
|
QList<int> list;
|
|
|
|
|
|
|
|
if ( printer.printRange() == QPrinter::PageRange ) {
|
|
|
|
startPage = printer.fromPage();
|
|
|
|
endPage = printer.toPage();
|
2010-06-04 08:25:25 +00:00
|
|
|
} else if ( printer.printRange() == QPrinter::CurrentPage) {
|
|
|
|
startPage = currentPage;
|
|
|
|
endPage = currentPage;
|
2007-11-26 21:43:54 +00:00
|
|
|
} else { //AllPages
|
|
|
|
startPage = 1;
|
|
|
|
endPage = lastPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = startPage; i <= endPage; i++ ) {
|
|
|
|
list << i;
|
|
|
|
}
|
2009-12-24 14:39:49 +00:00
|
|
|
|
2007-11-26 21:43:54 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FilePrinter::pageRange( QPrinter &printer, int lastPage, const QList<int> &selectedPageList )
|
|
|
|
{
|
|
|
|
if ( printer.printRange() == QPrinter::Selection) {
|
|
|
|
return pageListToPageRange( selectedPageList );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( printer.printRange() == QPrinter::PageRange ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("%1-%2").arg(printer.fromPage()).arg(printer.toPage());
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("1-%2").arg( lastPage );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FilePrinter::pageListToPageRange( const QList<int> &pageList )
|
|
|
|
{
|
|
|
|
QString pageRange;
|
|
|
|
int count = pageList.count();
|
|
|
|
int i = 0;
|
|
|
|
int seqStart = i;
|
|
|
|
int seqEnd;
|
|
|
|
|
|
|
|
while ( i != count ) {
|
|
|
|
|
|
|
|
if ( i + 1 == count || pageList[i] + 1 != pageList[i+1] ) {
|
|
|
|
|
|
|
|
seqEnd = i;
|
|
|
|
|
|
|
|
if ( !pageRange.isEmpty() ) {
|
2016-07-11 20:07:57 +00:00
|
|
|
pageRange.append(QLatin1Char(','));
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( seqStart == seqEnd ) {
|
|
|
|
pageRange.append(pageList[i]);
|
|
|
|
} else {
|
2015-10-29 12:37:11 +00:00
|
|
|
pageRange.append(QStringLiteral("%1-%2").arg(seqStart).arg(seqEnd));
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seqStart = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageRange;
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:18:52 +00:00
|
|
|
bool FilePrinter::ps2pdfAvailable()
|
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
return ( !QStandardPaths::findExecutable(QStringLiteral("ps2pdf")).isEmpty() );
|
2008-12-14 23:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePrinter::pdf2psAvailable()
|
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
return ( !QStandardPaths::findExecutable(QStringLiteral("pdf2ps")).isEmpty() );
|
2008-12-14 23:18:52 +00:00
|
|
|
}
|
|
|
|
|
2007-11-26 21:43:54 +00:00
|
|
|
bool FilePrinter::cupsAvailable()
|
|
|
|
{
|
2016-02-16 00:26:48 +00:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(Q_OS_OSX)
|
2010-02-11 22:48:06 +00:00
|
|
|
// Ideally we would have access to the private Qt method
|
|
|
|
// QCUPSSupport::cupsAvailable() to do this as it is very complex routine.
|
|
|
|
// However, if CUPS is available then QPrinter::numCopies() will always return 1
|
|
|
|
// whereas if CUPS is not available it will return the real number of copies.
|
|
|
|
// This behaviour is guaranteed never to change, so we can use it as a reliable substitute.
|
|
|
|
QPrinter testPrinter;
|
|
|
|
testPrinter.setNumCopies( 2 );
|
|
|
|
return ( testPrinter.numCopies() == 1 );
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePrinter::detectCupsService()
|
|
|
|
{
|
2008-12-11 23:04:07 +00:00
|
|
|
QTcpSocket qsock;
|
2015-10-29 12:37:11 +00:00
|
|
|
qsock.connectToHost(QStringLiteral("localhost"), 631);
|
2008-12-11 23:04:07 +00:00
|
|
|
bool rtn = qsock.waitForConnected() && qsock.isValid();
|
|
|
|
qsock.abort();
|
|
|
|
return rtn;
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePrinter::detectCupsConfig()
|
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( QFile::exists(QStringLiteral("/etc/cups/cupsd.conf")) ) return true;
|
|
|
|
if ( QFile::exists(QStringLiteral("/usr/etc/cups/cupsd.conf")) ) return true;
|
|
|
|
if ( QFile::exists(QStringLiteral("/usr/local/etc/cups/cupsd.conf")) ) return true;
|
|
|
|
if ( QFile::exists(QStringLiteral("/opt/etc/cups/cupsd.conf")) ) return true;
|
|
|
|
if ( QFile::exists(QStringLiteral("/opt/local/etc/cups/cupsd.conf")) ) return true;
|
2007-11-26 21:43:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize FilePrinter::psPaperSize( QPrinter &printer )
|
|
|
|
{
|
2017-07-05 18:18:44 +00:00
|
|
|
QSize size = printer.pageLayout().pageSize().sizePoints();
|
|
|
|
|
|
|
|
if ( printer.pageSize() == QPrinter::Custom )
|
|
|
|
{
|
|
|
|
return QSize( (int) printer.widthMM() * ( 25.4 / 72 ),
|
|
|
|
(int) printer.heightMM() * ( 25.4 / 72 ) );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( printer.orientation() == QPrinter::Landscape ) {
|
|
|
|
size.transpose();
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-04-16 21:56:16 +00:00
|
|
|
Generator::PrintError FilePrinter::printError( int c )
|
|
|
|
{
|
|
|
|
Generator::PrintError pe;
|
|
|
|
if ( c >= 0 )
|
|
|
|
{
|
|
|
|
pe = Generator::NoPrintError;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch ( c )
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
pe = Generator::PrintingProcessCrashPrintError;
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
pe = Generator::PrintingProcessStartPrintError;
|
|
|
|
break;
|
|
|
|
case -5:
|
|
|
|
pe = Generator::PrintToFilePrintError;
|
|
|
|
break;
|
|
|
|
case -6:
|
|
|
|
pe = Generator::InvalidPrinterStatePrintError;
|
|
|
|
break;
|
|
|
|
case -7:
|
|
|
|
pe = Generator::UnableToFindFilePrintError;
|
|
|
|
break;
|
|
|
|
case -8:
|
|
|
|
pe = Generator::NoFileToPrintError;
|
|
|
|
break;
|
|
|
|
case -9:
|
|
|
|
pe = Generator::NoBinaryToPrintError;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pe = Generator::UnknownPrintError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pe;
|
|
|
|
}
|
2007-11-26 21:43:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
QStringList FilePrinter::printArguments( QPrinter &printer, FileDeletePolicy fileDeletePolicy,
|
|
|
|
PageSelectPolicy pageSelectPolicy, bool useCupsOptions,
|
|
|
|
const QString &pageRange, const QString &version,
|
|
|
|
QPrinter::Orientation documentOrientation )
|
2007-11-26 21:43:54 +00:00
|
|
|
{
|
|
|
|
QStringList argList;
|
|
|
|
|
|
|
|
if ( ! destination( printer, version ).isEmpty() ) {
|
|
|
|
argList << destination( printer, version );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! copies( printer, version ).isEmpty() ) {
|
|
|
|
argList << copies( printer, version );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! jobname( printer, version ).isEmpty() ) {
|
|
|
|
argList << jobname( printer, version );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! pages( printer, pageSelectPolicy, pageRange, useCupsOptions, version ).isEmpty() ) {
|
|
|
|
argList << pages( printer, pageSelectPolicy, pageRange, useCupsOptions, version );
|
|
|
|
}
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
if ( useCupsOptions && ! cupsOptions( printer, documentOrientation ).isEmpty() ) {
|
|
|
|
argList << cupsOptions( printer, documentOrientation);
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! deleteFile( printer, fileDeletePolicy, version ).isEmpty() ) {
|
|
|
|
argList << deleteFile( printer, fileDeletePolicy, version );
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
argList << QStringLiteral("--");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return argList;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::destination( QPrinter &printer, const QString &version )
|
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
return QStringList(QStringLiteral("-d")) << printer.printerName();
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version.startsWith( QLatin1String("lpr") ) ) {
|
|
|
|
return QStringList(QStringLiteral("-P")) << printer.printerName();
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::copies( QPrinter &printer, const QString &version )
|
|
|
|
{
|
2009-12-24 14:39:49 +00:00
|
|
|
int cp = printer.actualNumCopies();
|
2007-11-26 21:43:54 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
return QStringList(QStringLiteral("-n")) << QStringLiteral("%1").arg( cp );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version.startsWith( QLatin1String("lpr") ) ) {
|
|
|
|
return QStringList() << QStringLiteral("-#%1").arg( cp );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::jobname( QPrinter &printer, const QString &version )
|
|
|
|
{
|
|
|
|
if ( ! printer.docName().isEmpty() ) {
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
return QStringList(QStringLiteral("-t")) << printer.docName();
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version.startsWith( QLatin1String("lpr") ) ) {
|
2013-12-15 22:44:51 +00:00
|
|
|
const QString shortenedDocName = QString::fromUtf8(printer.docName().toUtf8().left(255));
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-J")) << shortenedDocName;
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2014-01-10 17:31:26 +00:00
|
|
|
QStringList FilePrinter::deleteFile( QPrinter &, FileDeletePolicy fileDeletePolicy, const QString &version )
|
2007-11-26 21:43:54 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( fileDeletePolicy == FilePrinter::SystemDeletesFiles && version.startsWith( QLatin1String("lpr") ) ) {
|
|
|
|
return QStringList(QStringLiteral("-r"));
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::pages( QPrinter &printer, PageSelectPolicy pageSelectPolicy, const QString &pageRange,
|
|
|
|
bool useCupsOptions, const QString &version )
|
|
|
|
{
|
|
|
|
if ( pageSelectPolicy == FilePrinter::SystemSelectsPages ) {
|
|
|
|
|
|
|
|
if ( printer.printRange() == QPrinter::Selection && ! pageRange.isEmpty() ) {
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
return QStringList(QStringLiteral("-P")) << pageRange ;
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version.startsWith( QLatin1String("lpr") ) && useCupsOptions ) {
|
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("page-ranges=%1").arg( pageRange );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( printer.printRange() == QPrinter::PageRange ) {
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version == QLatin1String("lp") ) {
|
|
|
|
return QStringList(QStringLiteral("-P")) << QStringLiteral("%1-%2").arg( printer.fromPage() )
|
2007-11-26 21:43:54 +00:00
|
|
|
.arg( printer.toPage() );
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( version.startsWith( QLatin1String("lpr") ) && useCupsOptions ) {
|
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("page-ranges=%1-%2").arg( printer.fromPage() )
|
2007-11-26 21:43:54 +00:00
|
|
|
.arg( printer.toPage() );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList(); // AllPages
|
|
|
|
}
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
QStringList FilePrinter::cupsOptions( QPrinter &printer, QPrinter::Orientation documentOrientation )
|
2007-11-26 21:43:54 +00:00
|
|
|
{
|
|
|
|
QStringList optionList;
|
|
|
|
|
|
|
|
if ( ! optionMedia( printer ).isEmpty() ) {
|
|
|
|
optionList << optionMedia( printer );
|
|
|
|
}
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
if ( ! optionOrientation( printer, documentOrientation ).isEmpty() ) {
|
|
|
|
optionList << optionOrientation( printer, documentOrientation );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! optionDoubleSidedPrinting( printer ).isEmpty() ) {
|
|
|
|
optionList << optionDoubleSidedPrinting( printer );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! optionPageOrder( printer ).isEmpty() ) {
|
|
|
|
optionList << optionPageOrder( printer );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! optionCollateCopies( printer ).isEmpty() ) {
|
|
|
|
optionList << optionCollateCopies( printer );
|
|
|
|
}
|
|
|
|
|
2013-10-29 19:44:48 +00:00
|
|
|
if ( ! optionPageMargins( printer ).isEmpty() ) {
|
|
|
|
optionList << optionPageMargins( printer );
|
|
|
|
}
|
|
|
|
|
2008-06-05 23:06:21 +00:00
|
|
|
optionList << optionCupsProperties( printer );
|
|
|
|
|
2007-11-26 21:43:54 +00:00
|
|
|
return optionList;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::optionMedia( QPrinter &printer )
|
|
|
|
{
|
|
|
|
if ( ! mediaPageSize( printer ).isEmpty() &&
|
|
|
|
! mediaPaperSource( printer ).isEmpty() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) <<
|
2016-10-29 14:32:24 +00:00
|
|
|
QStringLiteral("media=%1,%2").arg( mediaPageSize( printer ), mediaPaperSource( printer ) );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! mediaPageSize( printer ).isEmpty() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) <<
|
|
|
|
QStringLiteral("media=%1").arg( mediaPageSize( printer ) );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! mediaPaperSource( printer ).isEmpty() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) <<
|
|
|
|
QStringLiteral("media=%1").arg( mediaPaperSource( printer ) );
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FilePrinter::mediaPageSize( QPrinter &printer )
|
|
|
|
{
|
|
|
|
switch ( printer.pageSize() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
case QPrinter::A0: return QStringLiteral("A0");
|
|
|
|
case QPrinter::A1: return QStringLiteral("A1");
|
|
|
|
case QPrinter::A2: return QStringLiteral("A2");
|
|
|
|
case QPrinter::A3: return QStringLiteral("A3");
|
|
|
|
case QPrinter::A4: return QStringLiteral("A4");
|
|
|
|
case QPrinter::A5: return QStringLiteral("A5");
|
|
|
|
case QPrinter::A6: return QStringLiteral("A6");
|
|
|
|
case QPrinter::A7: return QStringLiteral("A7");
|
|
|
|
case QPrinter::A8: return QStringLiteral("A8");
|
|
|
|
case QPrinter::A9: return QStringLiteral("A9");
|
|
|
|
case QPrinter::B0: return QStringLiteral("B0");
|
|
|
|
case QPrinter::B1: return QStringLiteral("B1");
|
|
|
|
case QPrinter::B10: return QStringLiteral("B10");
|
|
|
|
case QPrinter::B2: return QStringLiteral("B2");
|
|
|
|
case QPrinter::B3: return QStringLiteral("B3");
|
|
|
|
case QPrinter::B4: return QStringLiteral("B4");
|
|
|
|
case QPrinter::B5: return QStringLiteral("B5");
|
|
|
|
case QPrinter::B6: return QStringLiteral("B6");
|
|
|
|
case QPrinter::B7: return QStringLiteral("B7");
|
|
|
|
case QPrinter::B8: return QStringLiteral("B8");
|
|
|
|
case QPrinter::B9: return QStringLiteral("B9");
|
|
|
|
case QPrinter::C5E: return QStringLiteral("C5"); //Correct Translation?
|
|
|
|
case QPrinter::Comm10E: return QStringLiteral("Comm10"); //Correct Translation?
|
|
|
|
case QPrinter::DLE: return QStringLiteral("DL"); //Correct Translation?
|
|
|
|
case QPrinter::Executive: return QStringLiteral("Executive");
|
|
|
|
case QPrinter::Folio: return QStringLiteral("Folio");
|
|
|
|
case QPrinter::Ledger: return QStringLiteral("Ledger");
|
|
|
|
case QPrinter::Legal: return QStringLiteral("Legal");
|
|
|
|
case QPrinter::Letter: return QStringLiteral("Letter");
|
|
|
|
case QPrinter::Tabloid: return QStringLiteral("Tabloid");
|
|
|
|
case QPrinter::Custom: return QStringLiteral("Custom.%1x%2mm")
|
2017-08-20 22:09:27 +00:00
|
|
|
.arg( printer.widthMM() )
|
|
|
|
.arg( printer.heightMM() );
|
2007-11-26 21:43:54 +00:00
|
|
|
default: return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// What about Upper and MultiPurpose? And others in PPD???
|
|
|
|
QString FilePrinter::mediaPaperSource( QPrinter &printer )
|
|
|
|
{
|
|
|
|
switch ( printer.paperSource() ) {
|
|
|
|
case QPrinter::Auto: return QString();
|
2015-10-29 12:37:11 +00:00
|
|
|
case QPrinter::Cassette: return QStringLiteral("Cassette");
|
|
|
|
case QPrinter::Envelope: return QStringLiteral("Envelope");
|
|
|
|
case QPrinter::EnvelopeManual: return QStringLiteral("EnvelopeManual");
|
|
|
|
case QPrinter::FormSource: return QStringLiteral("FormSource");
|
|
|
|
case QPrinter::LargeCapacity: return QStringLiteral("LargeCapacity");
|
|
|
|
case QPrinter::LargeFormat: return QStringLiteral("LargeFormat");
|
|
|
|
case QPrinter::Lower: return QStringLiteral("Lower");
|
|
|
|
case QPrinter::MaxPageSource: return QStringLiteral("MaxPageSource");
|
|
|
|
case QPrinter::Middle: return QStringLiteral("Middle");
|
|
|
|
case QPrinter::Manual: return QStringLiteral("Manual");
|
|
|
|
case QPrinter::OnlyOne: return QStringLiteral("OnlyOne");
|
|
|
|
case QPrinter::Tractor: return QStringLiteral("Tractor");
|
|
|
|
case QPrinter::SmallFormat: return QStringLiteral("SmallFormat");
|
2007-11-26 21:43:54 +00:00
|
|
|
default: return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-05 22:40:22 +00:00
|
|
|
QStringList FilePrinter::optionOrientation( QPrinter &printer, QPrinter::Orientation documentOrientation )
|
|
|
|
{
|
|
|
|
// portrait and landscape options rotate the document according to the document orientation
|
|
|
|
// If we want to print a landscape document as one would expect it, we have to pass the
|
2013-06-24 10:46:16 +00:00
|
|
|
// portrait option so that the document is not rotated additionally
|
2011-06-05 22:40:22 +00:00
|
|
|
if ( printer.orientation() == documentOrientation ) {
|
|
|
|
// the user wants the document printed as is
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("portrait");
|
2011-06-05 22:40:22 +00:00
|
|
|
} else {
|
|
|
|
// the user expects the document being rotated by 90 degrees
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("landscape");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::optionDoubleSidedPrinting( QPrinter &printer )
|
|
|
|
{
|
2008-12-11 23:04:07 +00:00
|
|
|
switch ( printer.duplex() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
case QPrinter::DuplexNone: return QStringList(QStringLiteral("-o")) << QStringLiteral("sides=one-sided");
|
2008-12-11 23:04:07 +00:00
|
|
|
case QPrinter::DuplexAuto: if ( printer.orientation() == QPrinter::Landscape ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("sides=two-sided-short-edge");
|
2008-12-11 23:04:07 +00:00
|
|
|
} else {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("sides=two-sided-long-edge");
|
2008-12-11 23:04:07 +00:00
|
|
|
}
|
2015-10-29 12:37:11 +00:00
|
|
|
case QPrinter::DuplexLongSide: return QStringList(QStringLiteral("-o")) << QStringLiteral("sides=two-sided-long-edge");
|
|
|
|
case QPrinter::DuplexShortSide: return QStringList(QStringLiteral("-o")) << QStringLiteral("sides=two-sided-short-edge");
|
2008-12-11 23:04:07 +00:00
|
|
|
default: return QStringList(); //Use printer default
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::optionPageOrder( QPrinter &printer )
|
|
|
|
{
|
|
|
|
if ( printer.pageOrder() == QPrinter::LastPageFirst ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("outputorder=reverse");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("outputorder=normal");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FilePrinter::optionCollateCopies( QPrinter &printer )
|
|
|
|
{
|
|
|
|
if ( printer.collateCopies() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("Collate=True");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("Collate=False");
|
2007-11-26 21:43:54 +00:00
|
|
|
}
|
|
|
|
|
2008-12-11 23:04:07 +00:00
|
|
|
QStringList FilePrinter::optionPageMargins( QPrinter &printer )
|
|
|
|
{
|
|
|
|
if (printer.printEngine()->property(QPrintEngine::PPK_PageMargins).isNull()) {
|
|
|
|
return QStringList();
|
|
|
|
} else {
|
|
|
|
qreal l, t, r, b;
|
|
|
|
printer.getPageMargins( &l, &t, &r, &b, QPrinter::Point );
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringList(QStringLiteral("-o")) << QStringLiteral("page-left=%1").arg(l)
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("page-top=%1").arg(t)
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("page-right=%1").arg(r)
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("page-bottom=%1").arg(b) << QStringLiteral("-o") << QStringLiteral("fit-to-page");
|
2008-12-11 23:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-05 23:06:21 +00:00
|
|
|
QStringList FilePrinter::optionCupsProperties( QPrinter &printer )
|
|
|
|
{
|
|
|
|
QStringList dialogOptions = printer.printEngine()->property(QPrintEngine::PrintEnginePropertyKey(0xfe00)).toStringList();
|
|
|
|
QStringList cupsOptions;
|
|
|
|
|
|
|
|
for ( int i = 0; i < dialogOptions.count(); i = i + 2 ) {
|
|
|
|
if ( dialogOptions[i+1].isEmpty() ) {
|
2015-10-29 12:37:11 +00:00
|
|
|
cupsOptions << QStringLiteral("-o") << dialogOptions[i];
|
2008-06-05 23:06:21 +00:00
|
|
|
} else {
|
2016-07-11 20:07:57 +00:00
|
|
|
cupsOptions << QStringLiteral("-o") << dialogOptions[i] + QLatin1Char('=') + dialogOptions[i+1];
|
2008-06-05 23:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cupsOptions;
|
|
|
|
}
|
|
|
|
|
2010-04-14 21:38:40 +00:00
|
|
|
/* kate: replace-tabs on; indent-width 4; */
|