2009-10-08 23:48:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2009 by Pino Toscano <pino@kde.org> *
|
|
|
|
* *
|
|
|
|
* 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 "shellutils.h"
|
|
|
|
|
|
|
|
// qt/kde includes
|
2014-08-13 20:57:48 +00:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QFile>
|
2014-09-11 16:26:10 +00:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
2018-08-31 09:23:45 +00:00
|
|
|
#include <QCommandLineParser>
|
2009-10-08 23:48:04 +00:00
|
|
|
|
|
|
|
namespace ShellUtils
|
|
|
|
{
|
|
|
|
|
2017-01-15 23:33:25 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
bool qfileExistFunc( const QString& fileName )
|
|
|
|
{
|
|
|
|
return QFile::exists( fileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FileExistFunc qfileExistFunc()
|
|
|
|
{
|
|
|
|
return detail::qfileExistFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg )
|
2009-10-08 23:48:04 +00:00
|
|
|
{
|
2015-08-14 15:54:33 +00:00
|
|
|
QUrl url = QUrl::fromUserInput(_arg, QDir::currentPath(), QUrl::AssumeLocalFile);
|
|
|
|
if ( url.isLocalFile() ) {
|
|
|
|
// make sure something like /tmp/foo#bar.pdf is treated as a path name (default)
|
|
|
|
// but something like /tmp/foo.pdf#bar is foo.pdf plus an anchor "bar"
|
|
|
|
const QString path = url.path();
|
|
|
|
int hashIndex = path.lastIndexOf( QLatin1Char ( '#' ) );
|
2017-01-15 23:33:25 +00:00
|
|
|
if ( hashIndex != -1 && !exist_func(path) ) {
|
2015-08-14 15:54:33 +00:00
|
|
|
url.setPath( path.left( hashIndex ) );
|
|
|
|
url.setFragment( path.mid( hashIndex + 1 ) );
|
|
|
|
}
|
|
|
|
} else if ( !url.fragment().isEmpty() ) {
|
|
|
|
// make sure something like http://example.org/foo#bar.pdf is treated as a path name
|
|
|
|
// but something like http://example.org/foo.pdf#bar is foo.pdf plus an anchor "bar"
|
|
|
|
if ( url.fragment().contains( QLatin1Char( '.' ) ) ) {
|
2016-07-11 20:11:43 +00:00
|
|
|
url.setPath( url.path() + QLatin1Char ( '#' ) + url.fragment() );
|
2015-08-14 15:54:33 +00:00
|
|
|
url.setFragment( QString() );
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 16:26:10 +00:00
|
|
|
if ( !pageArg.isEmpty() )
|
2009-10-08 23:48:04 +00:00
|
|
|
{
|
2014-08-13 20:57:48 +00:00
|
|
|
url.setFragment( pageArg );
|
2009-10-08 23:48:04 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2014-08-14 11:35:43 +00:00
|
|
|
QString serializeOptions(const QCommandLineParser &args)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
const bool startInPresentation = args.isSet( QStringLiteral("presentation") );
|
|
|
|
const bool showPrintDialog = args.isSet( QStringLiteral("print") );
|
2018-03-03 16:21:15 +00:00
|
|
|
const bool showPrintDialogAndExit = args.isSet( QStringLiteral("print-and-exit") );
|
2015-10-29 12:37:11 +00:00
|
|
|
const bool unique = args.isSet(QStringLiteral("unique")) && args.positionalArguments().count() <= 1;
|
2016-07-12 19:12:58 +00:00
|
|
|
const bool noRaise = args.isSet(QStringLiteral("noraise"));
|
2015-10-29 12:37:11 +00:00
|
|
|
const QString page = args.value(QStringLiteral("page"));
|
2014-08-08 20:35:53 +00:00
|
|
|
|
2018-03-03 16:21:15 +00:00
|
|
|
return serializeOptions(startInPresentation, showPrintDialog, showPrintDialogAndExit, unique, noRaise, page);
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 16:21:15 +00:00
|
|
|
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2018-03-03 16:21:15 +00:00
|
|
|
return QStringLiteral("%1:%2:%3:%4:%5:%6").arg(startInPresentation).arg(showPrintDialog).arg(showPrintDialogAndExit).arg(unique).arg(noRaise).arg(page);
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 16:21:15 +00:00
|
|
|
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
const QStringList args = serializedOptions.split(QStringLiteral(":"));
|
2018-03-03 16:21:15 +00:00
|
|
|
if (args.count() == 6)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
*presentation = args[0] == QLatin1String("1");
|
|
|
|
*print = args[1] == QLatin1String("1");
|
2018-03-03 16:21:15 +00:00
|
|
|
*print_and_exit = args[2] == QLatin1String("1");
|
|
|
|
*unique = args[3] == QLatin1String("1");
|
|
|
|
*noraise = args[4] == QLatin1String("1");
|
|
|
|
*page = args[5];
|
2014-08-08 20:35:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startInPresentation(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2018-03-03 16:21:15 +00:00
|
|
|
return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummy, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool showPrintDialog(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2018-03-03 16:21:15 +00:00
|
|
|
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;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool unique(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2018-03-03 16:21:15 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummy, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool noRaise(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2018-03-03 16:21:15 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString page(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
bool dummy;
|
2018-03-03 16:21:15 +00:00
|
|
|
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &result);
|
2014-08-08 20:35:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-08 23:48:04 +00:00
|
|
|
}
|