10#include "fileprinter.h"
15#include <QPrintEngine>
24#include <QStandardPaths>
34 return fp.doPrintFiles(printer, QStringList(file), fileDeletePolicy, pageSelectPolicy, pageRange, documentOrientation, scaleMode);
41 return Document::PrintingProcessCrashPrintError;
44 return Document::PrintingProcessStartPrintError;
47 return Document::UnknownPrintError;
54FilePrinter::doPrintFiles(QPrinter &printer,
const QStringList &fileList, FileDeletePolicy fileDeletePolicy, PageSelectPolicy pageSelectPolicy,
const QString &pageRange,
QPageLayout::Orientation documentOrientation, ScaleMode scaleMode)
56 if (fileList.
size() < 1) {
57 return Document::NoFileToPrintError;
62 return Document::UnableToFindFilePrintError;
67 return Document::InvalidPrinterStatePrintError;
80 QFileInfo inputFileInfo = QFileInfo(fileList[0]);
83 bool doDeleteFile = (fileDeletePolicy == FilePrinter::SystemDeletesFiles);
91 ret = Document::PrintToFilePrintError;
98 ret = Document::PrintToFilePrintError;
102 exe = QStringLiteral(
"ps2pdf");
104 qCDebug(OkularCoreDebug) <<
"Executing" << exe <<
"with arguments" << argList;
105 ret = doKProcessExecute(exe, argList);
107 exe = QStringLiteral(
"pdf2ps");
109 qCDebug(OkularCoreDebug) <<
"Executing" << exe <<
"with arguments" << argList;
110 ret = doKProcessExecute(exe, argList);
112 ret = Document::PrintToFilePrintError;
126 exe = QStringLiteral(
"lpr-cups");
128 exe = QStringLiteral(
"lpr.cups");
130 exe = QStringLiteral(
"lpr");
132 exe = QStringLiteral(
"lp");
134 return Document::NoBinaryToPrintError;
137 bool useCupsOptions = cupsAvailable();
138 argList = printArguments(printer, fileDeletePolicy, pageSelectPolicy, useCupsOptions, pageRange, exe, documentOrientation, scaleMode) << fileList;
139 qCDebug(OkularCoreDebug) <<
"Executing" << exe <<
"with arguments" << argList;
141 ret = doKProcessExecute(exe, argList);
147QList<int> FilePrinter::pageList(QPrinter &printer,
int lastPage,
int currentPage,
const QList<int> &selectedPageList)
150 return selectedPageList;
153 int startPage, endPage;
158 endPage = printer.
toPage();
160 startPage = currentPage;
161 endPage = currentPage;
167 for (
int i = startPage; i <= endPage; i++) {
174bool FilePrinter::ps2pdfAvailable()
179bool FilePrinter::pdf2psAvailable()
184bool FilePrinter::cupsAvailable()
186#if defined(Q_OS_UNIX) && !defined(Q_OS_OSX)
192 QPrinter testPrinter;
199QStringList FilePrinter::printArguments(QPrinter &printer,
200 FileDeletePolicy fileDeletePolicy,
201 PageSelectPolicy pageSelectPolicy,
203 const QString &pageRange,
204 const QString &version,
210 if (!destination(printer, version).isEmpty()) {
211 argList << destination(printer, version);
214 if (!copies(printer, version).isEmpty()) {
215 argList << copies(printer, version);
218 if (!jobname(printer, version).isEmpty()) {
219 argList << jobname(printer, version);
222 if (!pages(printer, pageSelectPolicy, pageRange, useCupsOptions, version).isEmpty()) {
223 argList << pages(printer, pageSelectPolicy, pageRange, useCupsOptions, version);
226 if (useCupsOptions && !cupsOptions(printer, documentOrientation, scaleMode).isEmpty()) {
227 argList << cupsOptions(printer, documentOrientation, scaleMode);
230 if (!deleteFile(printer, fileDeletePolicy, version).isEmpty()) {
231 argList << deleteFile(printer, fileDeletePolicy, version);
234 if (version == QLatin1String(
"lp")) {
235 argList << QStringLiteral(
"--");
241QStringList FilePrinter::destination(QPrinter &printer,
const QString &version)
243 if (version == QLatin1String(
"lp")) {
244 return QStringList(QStringLiteral(
"-d")) << printer.
printerName();
248 return QStringList(QStringLiteral(
"-P")) << printer.
printerName();
251 return QStringList();
254QStringList FilePrinter::copies(QPrinter &printer,
const QString &version)
258 if (version == QLatin1String(
"lp")) {
259 return QStringList(QStringLiteral(
"-n")) << QStringLiteral(
"%1").arg(cp);
263 return QStringList() << QStringLiteral(
"-#%1").arg(cp);
266 return QStringList();
269QStringList FilePrinter::jobname(QPrinter &printer,
const QString &version)
272 if (version == QLatin1String(
"lp")) {
273 return QStringList(QStringLiteral(
"-t")) << printer.
docName();
278 return QStringList(QStringLiteral(
"-J")) << shortenedDocName;
282 return QStringList();
285QStringList FilePrinter::deleteFile(QPrinter &, FileDeletePolicy fileDeletePolicy,
const QString &version)
287 if (fileDeletePolicy == FilePrinter::SystemDeletesFiles &&
version.
startsWith(QLatin1String(
"lpr"))) {
288 return QStringList(QStringLiteral(
"-r"));
291 return QStringList();
294QStringList FilePrinter::pages(QPrinter &printer, PageSelectPolicy pageSelectPolicy,
const QString &pageRange,
bool useCupsOptions,
const QString &version)
296 if (pageSelectPolicy == FilePrinter::SystemSelectsPages) {
298 if (version == QLatin1String(
"lp")) {
299 return QStringList(QStringLiteral(
"-P")) << pageRange;
303 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"page-ranges=%1").arg(pageRange);
308 if (version == QLatin1String(
"lp")) {
309 return QStringList(QStringLiteral(
"-P")) << QStringLiteral(
"%1-%2").arg(printer.
fromPage()).arg(printer.
toPage());
313 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"page-ranges=%1-%2").arg(printer.
fromPage()).arg(printer.
toPage());
318 return QStringList();
321QStringList FilePrinter::cupsOptions(QPrinter &printer,
QPageLayout::Orientation documentOrientation, ScaleMode scaleMode)
323 QStringList optionList;
325 if (!optionMedia(printer).isEmpty()) {
326 optionList << optionMedia(printer);
329 if (!optionOrientation(printer, documentOrientation).isEmpty()) {
330 optionList << optionOrientation(printer, documentOrientation);
333 if (!optionDoubleSidedPrinting(printer).isEmpty()) {
334 optionList << optionDoubleSidedPrinting(printer);
337 if (!optionPageOrder(printer).isEmpty()) {
338 optionList << optionPageOrder(printer);
341 if (!optionCollateCopies(printer).isEmpty()) {
342 optionList << optionCollateCopies(printer);
345 if (!optionPageMargins(printer, scaleMode).isEmpty()) {
346 optionList << optionPageMargins(printer, scaleMode);
349 optionList << optionCupsProperties(printer);
354QStringList FilePrinter::optionMedia(QPrinter &printer)
356 if (!mediaPageSize(printer).isEmpty() && !mediaPaperSource(printer).isEmpty()) {
357 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"media=%1,%2").arg(mediaPageSize(printer), mediaPaperSource(printer));
360 if (!mediaPageSize(printer).isEmpty()) {
361 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"media=%1").arg(mediaPageSize(printer));
364 if (!mediaPaperSource(printer).isEmpty()) {
365 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"media=%1").arg(mediaPaperSource(printer));
368 return QStringList();
371QString FilePrinter::mediaPageSize(QPrinter &printer)
375 return QStringLiteral(
"A0");
377 return QStringLiteral(
"A1");
379 return QStringLiteral(
"A2");
381 return QStringLiteral(
"A3");
383 return QStringLiteral(
"A4");
385 return QStringLiteral(
"A5");
387 return QStringLiteral(
"A6");
389 return QStringLiteral(
"A7");
391 return QStringLiteral(
"A8");
393 return QStringLiteral(
"A9");
395 return QStringLiteral(
"B0");
397 return QStringLiteral(
"B1");
399 return QStringLiteral(
"B10");
401 return QStringLiteral(
"B2");
403 return QStringLiteral(
"B3");
405 return QStringLiteral(
"B4");
407 return QStringLiteral(
"B5");
409 return QStringLiteral(
"B6");
411 return QStringLiteral(
"B7");
413 return QStringLiteral(
"B8");
415 return QStringLiteral(
"B9");
417 return QStringLiteral(
"C5");
419 return QStringLiteral(
"Comm10");
421 return QStringLiteral(
"DL");
423 return QStringLiteral(
"Executive");
425 return QStringLiteral(
"Folio");
427 return QStringLiteral(
"Ledger");
429 return QStringLiteral(
"Legal");
431 return QStringLiteral(
"Letter");
433 return QStringLiteral(
"Tabloid");
435 return QStringLiteral(
"Custom.%1x%2mm").arg(printer.
widthMM()).arg(printer.
heightMM());
442QString FilePrinter::mediaPaperSource(QPrinter &printer)
448 return QStringLiteral(
"Cassette");
450 return QStringLiteral(
"Envelope");
452 return QStringLiteral(
"EnvelopeManual");
454 return QStringLiteral(
"FormSource");
456 return QStringLiteral(
"LargeCapacity");
458 return QStringLiteral(
"LargeFormat");
460 return QStringLiteral(
"Lower");
462 return QStringLiteral(
"MaxPageSource");
464 return QStringLiteral(
"Middle");
466 return QStringLiteral(
"Manual");
468 return QStringLiteral(
"OnlyOne");
470 return QStringLiteral(
"Tractor");
472 return QStringLiteral(
"SmallFormat");
485 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"portrait");
488 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"landscape");
492QStringList FilePrinter::optionDoubleSidedPrinting(QPrinter &printer)
494 switch (printer.
duplex()) {
496 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"sides=one-sided");
499 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"sides=two-sided-short-edge");
501 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"sides=two-sided-long-edge");
504 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"sides=two-sided-long-edge");
506 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"sides=two-sided-short-edge");
508 return QStringList();
512QStringList FilePrinter::optionPageOrder(QPrinter &printer)
515 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"outputorder=reverse");
517 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"outputorder=normal");
520QStringList FilePrinter::optionCollateCopies(QPrinter &printer)
523 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"Collate=True");
525 return QStringList(QStringLiteral(
"-o")) << QStringLiteral(
"Collate=False");
528QStringList FilePrinter::optionPageMargins(QPrinter &printer, ScaleMode scaleMode)
531 return QStringList();
533 qreal l(0), t(0), r(0), b(0);
538 r = marginsf.right();
539 b = marginsf.bottom();
541 QStringList marginOptions;
542 marginOptions << (QStringLiteral(
"-o")) << QStringLiteral(
"page-left=%1").arg(l) << QStringLiteral(
"-o") << QStringLiteral(
"page-top=%1").arg(t) << QStringLiteral(
"-o") << QStringLiteral(
"page-right=%1").arg(r)
543 << QStringLiteral(
"-o") << QStringLiteral(
"page-bottom=%1").arg(b);
544 if (scaleMode == ScaleMode::FitToPrintArea) {
545 marginOptions << QStringLiteral(
"-o") << QStringLiteral(
"fit-to-page");
548 return marginOptions;
552QStringList FilePrinter::optionCupsProperties(QPrinter &printer)
555 QStringList cupsOptions;
557 for (
int i = 0; i < dialogOptions.
count(); i = i + 2) {
558 if (dialogOptions[i + 1].isEmpty()) {
559 cupsOptions << QStringLiteral(
"-o") << dialogOptions[i];
561 cupsOptions << QStringLiteral(
"-o") << dialogOptions[i] + QLatin1Char(
'=') + dialogOptions[i + 1];
int execute(int msecs=-1)
@ NoPrintError
Printing succeeded.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QAction * lastPage(const QObject *recvr, const char *slot, QObject *parent)
NETWORKMANAGERQT_EXPORT QString version()
QByteArray left(qsizetype len) const const
bool copy(const QString &fileName, const QString &newName)
bool exists() const const
bool rename(const QString &newName)
QString suffix() const const
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
qsizetype size() const const
QPageLayout pageLayout() const const
QMarginsF margins() const const
Orientation orientation() const const
QPageSize pageSize() const const
PageSizeId id(const QSize &pointSize, SizeMatchPolicy matchPolicy)
int heightMM() const const
int widthMM() const const
virtual QVariant property(PrintEnginePropertyKey key) const const=0
bool collateCopies() const const
int copyCount() const const
QString docName() const const
DuplexMode duplex() const const
int fromPage() const const
bool fullPage() const const
QString outputFileName() const const
PageOrder pageOrder() const const
PaperSource paperSource() const const
QPrintEngine * printEngine() const const
PrintRange printRange() const const
QString printerName() const const
PrinterState printerState() const const
bool supportsMultipleCopies() const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toUtf8() const const
bool isNull() const const
QStringList toStringList() const const