Commit 09b7b079 authored by Dileep Sankhla's avatar Dileep Sankhla Committed by Albert Astals Cid

Option to exit after printing

Summary:
When running okular with the parameter --print to directly open the print mode, it doesn't exit after acknowledging the print dialog. Hence adding --print_and_exit option exits Okular after acknowledging the print dialog and thus is useful for the command line batch processing or a Dolphin service as the issue suggests.

FEATURE: 318998

Test Plan:
1. open a file in Okular using the parameter --print. It will open Okular in print mode with the print dialog
2. Either print the file or cancel the print dialog
3. You will find that Okular stays open
4. Now using this patch, see for available options with the --help parameter. You will find --print_and_exit option
5. Now open a file in Okular using the parameter --print_and_exit. It will open Okular in print mode with the print dialog
6. Either print the file or cancel the print dialog
7. You will find that Okular closes after acknowledging the dialog

Reviewers: aacid, #okular, ngraham

Subscribers: ltoscano, ngraham, aacid, #okular

Tags: #okular

Differential Revision: https://phabricator.kde.org/D10249
parent 13e719b3
......@@ -183,10 +183,10 @@ void MainShellTest::testShell_data()
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());
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 optionsUnique = ShellUtils::serializeOptions(false, 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;
......@@ -406,9 +406,9 @@ void MainShellTest::testFileRemembersPagePosition()
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 +459,10 @@ void MainShellTest::test2FilesError_data()
{
QTest::addColumn<QString>("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("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 +490,7 @@ void MainShellTest::testSessionRestore_data()
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;
......
......@@ -304,7 +304,7 @@ QObject *parent,
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 @@ bool Part::openFile()
m_cliPrint = false;
slotPrint();
}
else if ( m_cliPrintAndExit )
{
slotPrint();
}
return true;
}
......@@ -3068,6 +3072,11 @@ void Part::enableStartWithPrint()
m_cliPrint = true;
}
void Part::enableExitAfterPrint()
{
m_cliPrintAndExit = true;
}
void Part::slotAboutBackend()
{
const KPluginMetaData data = m_document->generatorInfo();
......@@ -3206,9 +3215,12 @@ void Part::slotPrint()
printDialog->setOption( QAbstractPrintDialog::PrintCurrentPage );
}
bool success = true;
if ( printDialog->exec() )
doPrint( printer );
success = doPrint( printer );
delete printDialog;
if ( m_cliPrintAndExit )
exit ( success ? EXIT_SUCCESS : EXIT_FAILURE );
}
}
......@@ -3230,12 +3242,12 @@ void Part::setupPrint( QPrinter &printer )
}
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 false;
}
if (!m_document->print(printer))
......@@ -3249,7 +3261,9 @@ void Part::doPrint(QPrinter &printer)
{
KMessageBox::error(widget(), i18n("Could not print the document. Detailed error is \"%1\". Please report to bugs.kde.org", error));
}
return false;
}
return true;
}
void Part::psTransformEnded(int exit, QProcess::ExitStatus status)
......@@ -3497,6 +3511,7 @@ void Part::updateAboutBackendAction()
void Part::resetStartArguments()
{
m_cliPrint = false;
m_cliPrintAndExit = false;
}
#if PURPOSE_FOUND
......
......@@ -165,6 +165,7 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
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 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
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 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
QList<QAction*> m_bookmarkActions;
bool m_cliPresentation;
bool m_cliPrint;
bool m_cliPrintAndExit;
QString m_addBookmarkText;
QIcon m_addBookmarkIcon;
......
......@@ -49,6 +49,7 @@ int main(int argc, char** argv)
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 after printing")));
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."));
......
......@@ -684,6 +684,8 @@ void Shell::applyOptionsToPart( QObject* part, const QString &serializedOptions
doc->startPresentation();
if ( ShellUtils::showPrintDialog(serializedOptions) )
QMetaObject::invokeMethod( part, "enableStartWithPrint" );
if ( ShellUtils::showPrintDialogAndExit(serializedOptions) )
QMetaObject::invokeMethod( part, "enableExitAfterPrint" );
}
void Shell::connectPart( QObject* part )
......
......@@ -66,28 +66,30 @@ QString serializeOptions(const QCommandLineParser &args)
{
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 startInPresentation(const QString &serializedOptions)
{
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;
}
......
......@@ -24,12 +24,13 @@ typedef bool (*FileExistFunc)( const QString& fileName );
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);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment