diff --git a/autotests/mainshelltest.cpp b/autotests/mainshelltest.cpp --- a/autotests/mainshelltest.cpp +++ b/autotests/mainshelltest.cpp @@ -183,31 +183,36 @@ file1AndToc << QStringLiteral(KDESRCDIR "data/tocreload.pdf"); const QString tocReload = QStringLiteral(KDESRCDIR "data/tocreload.pdf"); - const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, QStringLiteral("2")); - const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, QStringLiteral("2")); - const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, QString()); - const QString optionsUnique = ShellUtils::serializeOptions(false, false, true, false, QString()); - - QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << 0u << false << false; - QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << 0u << false << false; - QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << 0u << false << false; - QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << 0u << false << false; - QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << 0u << false << false; - QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << 0u << false << false; - QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << 0u << false << false; - QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << 0u << false << false; - QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << 0u << false << false; - QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << true << 0u << false << false; - QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << true << 0u << false << false; - QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << false << false; - QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << true << false; - QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << 2u << false << true; - QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 3u << false << false; - QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << true << false; - QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << false << true; - QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 3u << false << false; - QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << true << false; - QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << false << true; + const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("2")); + const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, false, QStringLiteral("2")); + const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, false, QString()); + const QString optionsPrintAndExit = ShellUtils::serializeOptions(false, false, true, false, false, QString()); + const QString optionsUnique = ShellUtils::serializeOptions(false, false, false, true, false, QString()); + + QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << false << 0u << false << false; + QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << false << 0u << false << false; + QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << false << 0u << false << false; + QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << false << 0u << false << false; + QTest::newRow("open file print and exit") << file1 << optionsPrint << false << QString() << 0u << false << false << true << false << 0u << false << false; + QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << false << true << 0u << false << false; + QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << false << true << 0u << false << false; + QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << false << 2u << false << false; + QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << false << 2u << true << false; + QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << false << 2u << false << true; + QTest::newRow("print attach tabs and exit") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << true << false << false << 2u << false << true; + QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << false << true << 3u << false << false; + QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << false << true << 2u << true << false; + QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << false << true << 2u << false << true; + QTest::newRow("print attach unique and exit") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << false << true << 2u << false << true; + QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << false << true << 3u << false << false; + QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << false << true << 2u << true << false; + QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << false << true << 2u << false << true; + QTest::newRow("print attach unique tabs and exit") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << false << true << 2u << false << true; } void MainShellTest::testShell() @@ -219,10 +224,12 @@ QFETCH(uint, expectedPage); QFETCH(bool, expectPresentation); QFETCH(bool, expectPrintDialog); + QFETCH(bool, expectPrintDialogAndExit); QFETCH(bool, unique); QFETCH(uint, externalProcessExpectedPage); QFETCH(bool, externalProcessExpectPresentation); QFETCH(bool, externalProcessExpectPrintDialog); + QFETCH(bool, externalProcessExpectPrintDialogAndExit); QScopedPointer helper; @@ -234,6 +241,12 @@ QTimer::singleShot(0, helper.data(), SLOT(closePrintDialog())); } + if (expectPrintDialogAndExit || externalProcessExpectPrintDialogAndExit) { + const int expectedTab = externalProcessExpectPrintDialogAndExit && !unique ? 1 : 0; + helper.reset(new ClosePrintDialogHelper(expectedTab)); + QTimer::singleShot(0, helper.data(), SLOT(closePrintDialog())); + } + Okular::Status status = Okular::main(paths, serializedOptions); QCOMPARE(status, Okular::Success); Shell *s = findShell(); @@ -303,6 +316,8 @@ args << QStringLiteral("-presentation"); if (externalProcessExpectPrintDialog) args << QStringLiteral("-print"); + if (externalProcessExpectPrintDialogAndExit) + args << QStringLiteral("-print_and_exit"); p.start(QStringLiteral(OKULAR_BINARY), args); p.waitForStarted(); QCOMPARE(p.state(), QProcess::Running); @@ -406,9 +421,9 @@ const QStringList paths = QStringList(QStringLiteral(KDESRCDIR "data/contents.epub")); QString serializedOptions; if (mode == 1 || mode == 3) - serializedOptions = ShellUtils::serializeOptions(false, false, false, false, QString()); + serializedOptions = ShellUtils::serializeOptions(false, false, false, false, false, QString()); else - serializedOptions = ShellUtils::serializeOptions(false, false, true, false, QString()); + serializedOptions = ShellUtils::serializeOptions(false, false, false, true, false, QString()); Okular::Settings::self()->setShellOpenFileInTabs(mode == 3); @@ -459,10 +474,11 @@ { QTest::addColumn("serializedOptions"); - QTest::newRow("startInPresentation") << ShellUtils::serializeOptions(true, false, false, false, QString()); - QTest::newRow("showPrintDialog") << ShellUtils::serializeOptions(false, true, false, false, QString()); - QTest::newRow("unique") << ShellUtils::serializeOptions(false, false, true, false, QString()); - QTest::newRow("pageNumger") << ShellUtils::serializeOptions(false, false, false, false, QStringLiteral("3")); + QTest::newRow("startInPresentation") << ShellUtils::serializeOptions(true, false, false, false, false, QString()); + QTest::newRow("showPrintDialog") << ShellUtils::serializeOptions(false, true, false, false, false, QString()); + QTest::newRow("showPrintDialogAndExit") << ShellUtils::serializeOptions(false, false, true, false, false, QString()); + QTest::newRow("unique") << ShellUtils::serializeOptions(false, false, false, true, false, QString()); + QTest::newRow("pageNumger") << ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("3")); } void MainShellTest::test2FilesError() @@ -490,7 +506,7 @@ QStringList twoDocPaths( oneDocPaths ); twoDocPaths << QStringLiteral(KDESRCDIR "data/formSamples.pdf"); - const QString options = ShellUtils::serializeOptions(false, false, false, false, QString()); + const QString options = ShellUtils::serializeOptions(false, false, false, false, false, QString()); QTest::newRow("1 doc, 1 window, tabs") << oneDocPaths << options << true << true; QTest::newRow("2 docs, 1 window, tabs") << twoDocPaths << options << true << true; diff --git a/part.h b/part.h --- a/part.h +++ b/part.h @@ -165,6 +165,7 @@ Q_SCRIPTABLE void slotTogglePresentation(); Q_SCRIPTABLE Q_NOREPLY void reload(); Q_SCRIPTABLE Q_NOREPLY void enableStartWithPrint(); + Q_SCRIPTABLE Q_NOREPLY void enableExitAfterPrint(); Q_SIGNALS: void enablePrintAction(bool enable); @@ -261,7 +262,7 @@ void setupActions(); void setupPrint( QPrinter &printer ); - void doPrint( QPrinter &printer ); + bool doPrint( QPrinter &printer ); bool handleCompressed(QString &destpath, const QString &path, KCompressionDevice::CompressionType compressionType ); void rebuildBookmarkMenu( bool unplugActions = true ); void updateAboutBackendAction(); @@ -388,6 +389,7 @@ QList m_bookmarkActions; bool m_cliPresentation; bool m_cliPrint; + bool m_cliPrintAndExit; QString m_addBookmarkText; QIcon m_addBookmarkIcon; diff --git a/part.cpp b/part.cpp --- a/part.cpp +++ b/part.cpp @@ -304,7 +304,7 @@ const QVariantList &args) : KParts::ReadWritePart(parent), m_tempfile( nullptr ), m_documentOpenWithPassword( false ), m_swapInsteadOfOpening( false ), m_isReloading( false ), m_fileWasRemoved( false ), m_showMenuBarAction( nullptr ), m_showFullScreenAction( nullptr ), m_actionsSearched( false ), -m_cliPresentation(false), m_cliPrint(false), m_embedMode(detectEmbedMode(parentWidget, parent, args)), m_generatorGuiClient(nullptr), m_keeper( nullptr ) +m_cliPresentation(false), m_cliPrint(false), m_cliPrintAndExit(false), m_embedMode(detectEmbedMode(parentWidget, parent, args)), m_generatorGuiClient(nullptr), m_keeper( nullptr ) { // make sure that the component name is okular otherwise the XMLGUI .rc files are not found // when this part is used in an application other than okular (e.g. unit tests) @@ -1645,6 +1645,10 @@ m_cliPrint = false; slotPrint(); } + else if ( m_cliPrintAndExit ) + { + slotPrint(); + } return true; } @@ -3062,6 +3066,11 @@ m_cliPrint = true; } +void Part::enableExitAfterPrint() +{ + m_cliPrintAndExit = true; +} + void Part::slotAboutBackend() { const KPluginMetaData data = m_document->generatorInfo(); @@ -3200,13 +3209,17 @@ printDialog->setOption( QAbstractPrintDialog::PrintCurrentPage ); } + bool isError = false; if ( printDialog->exec() ) - doPrint( printer ); + isError = doPrint( printer ); delete printDialog; + if ( isError && m_cliPrintAndExit ) + exit ( EXIT_FAILURE ); + else if ( m_cliPrintAndExit ) + exit ( EXIT_SUCCESS ); } } - void Part::setupPrint( QPrinter &printer ) { printer.setOrientation(m_document->orientation()); @@ -3224,12 +3237,12 @@ } -void Part::doPrint(QPrinter &printer) +bool Part::doPrint(QPrinter &printer) { if (!m_document->isAllowed(Okular::AllowPrint)) { KMessageBox::error(widget(), i18n("Printing this document is not allowed.")); - return; + return true; } if (!m_document->print(printer)) @@ -3243,7 +3256,9 @@ { KMessageBox::error(widget(), i18n("Could not print the document. Detailed error is \"%1\". Please report to bugs.kde.org", error)); } + return true; } + return false; } void Part::psTransformEnded(int exit, QProcess::ExitStatus status) @@ -3491,6 +3506,7 @@ void Part::resetStartArguments() { m_cliPrint = false; + m_cliPrintAndExit = false; } #if PURPOSE_FOUND diff --git a/shell/main.cpp b/shell/main.cpp --- a/shell/main.cpp +++ b/shell/main.cpp @@ -49,6 +49,7 @@ parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("p") << QStringLiteral("page"), i18n("Page of the document to be shown"), QStringLiteral("number"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("presentation"), i18n("Start the document in presentation mode"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("print"), i18n("Start with print dialog"))); + parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("print_and_exit"), i18n("Start with print dialog and exit"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("unique"), i18n("\"Unique instance\" control"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("noraise"), i18n("Not raise window"))); parser.addPositionalArgument(QStringLiteral("urls"), i18n("Documents to open. Specify '-' to read from stdin.")); diff --git a/shell/shell.cpp b/shell/shell.cpp --- a/shell/shell.cpp +++ b/shell/shell.cpp @@ -684,6 +684,8 @@ doc->startPresentation(); if ( ShellUtils::showPrintDialog(serializedOptions) ) QMetaObject::invokeMethod( part, "enableStartWithPrint" ); + if ( ShellUtils::showPrintDialogAndExit(serializedOptions) ) + QMetaObject::invokeMethod( part, "enableExitAfterPrint" ); } void Shell::connectPart( QObject* part ) diff --git a/shell/shellutils.h b/shell/shellutils.h --- a/shell/shellutils.h +++ b/shell/shellutils.h @@ -24,12 +24,13 @@ FileExistFunc qfileExistFunc(); QUrl urlFromArg(const QString& _arg, FileExistFunc exist_func, const QString& pageArg = QString() ); QString serializeOptions(const QCommandLineParser &args); -QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool unique, bool noRaise, const QString &page); -bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *unique, bool *noraise, QString *page); +QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page); +bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page); bool unique(const QString &serializedOptions); bool noRaise(const QString &serializedOptions); bool startInPresentation(const QString &serializedOptions); bool showPrintDialog(const QString &serializedOptions); +bool showPrintDialogAndExit(const QString &serializedOptions); QString page(const QString &serializedOptions); } diff --git a/shell/shellutils.cpp b/shell/shellutils.cpp --- a/shell/shellutils.cpp +++ b/shell/shellutils.cpp @@ -66,28 +66,30 @@ { const bool startInPresentation = args.isSet( QStringLiteral("presentation") ); const bool showPrintDialog = args.isSet( QStringLiteral("print") ); + const bool showPrintDialogAndExit = args.isSet( QStringLiteral("print_and_exit") ); const bool unique = args.isSet(QStringLiteral("unique")) && args.positionalArguments().count() <= 1; const bool noRaise = args.isSet(QStringLiteral("noraise")); const QString page = args.value(QStringLiteral("page")); - return serializeOptions(startInPresentation, showPrintDialog, unique, noRaise, page); + return serializeOptions(startInPresentation, showPrintDialog, showPrintDialogAndExit, unique, noRaise, page); } -QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool unique, bool noRaise, const QString &page) +QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page) { - return QStringLiteral("%1:%2:%3:%4:%5").arg(startInPresentation).arg(showPrintDialog).arg(unique).arg(noRaise).arg(page); + return QStringLiteral("%1:%2:%3:%4:%5:%6").arg(startInPresentation).arg(showPrintDialog).arg(showPrintDialogAndExit).arg(unique).arg(noRaise).arg(page); } -bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *unique, bool *noraise, QString *page) +bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page) { const QStringList args = serializedOptions.split(QStringLiteral(":")); - if (args.count() == 5) + if (args.count() == 6) { *presentation = args[0] == QLatin1String("1"); *print = args[1] == QLatin1String("1"); - *unique = args[2] == QLatin1String("1"); - *noraise = args[3] == QLatin1String("1"); - *page = args[4]; + *print_and_exit = args[2] == QLatin1String("1"); + *unique = args[3] == QLatin1String("1"); + *noraise = args[4] == QLatin1String("1"); + *page = args[5]; return true; } return false; @@ -97,35 +99,42 @@ { bool result, dummy; QString dummyString; - return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummyString) && result; + return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummy, &dummyString) && result; } bool showPrintDialog(const QString &serializedOptions) { bool result, dummy; QString dummyString; - return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummyString) && result; + return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummy, &dummyString) && result; +} + +bool showPrintDialogAndExit(const QString &serializedOptions) +{ + bool result, dummy; + QString dummyString; + return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummy, &dummyString) && result; } bool unique(const QString &serializedOptions) { bool result, dummy; QString dummyString; - return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummyString) && result; + return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummy, &dummyString) && result; } bool noRaise(const QString &serializedOptions) { bool result, dummy; QString dummyString; - return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummyString) && result; + return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result, &dummyString) && result; } QString page(const QString &serializedOptions) { QString result; bool dummy; - unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result); + unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &result); return result; }