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 <QCommandLineParser>
|
2014-09-11 16:26:10 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2014-08-13 20:57:48 +00:00
|
|
|
#include <QFile>
|
2014-09-11 16:26:10 +00:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QUrl>
|
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));
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 16:26:10 +00:00
|
|
|
if (!pageArg.isEmpty()) {
|
2014-08-13 20:57:48 +00:00
|
|
|
url.setFragment(pageArg);
|
2019-02-13 21:55:02 +00:00
|
|
|
}
|
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"));
|
2019-02-13 21:55:02 +00:00
|
|
|
const QString find = args.value(QStringLiteral("find"));
|
2014-08-08 20:35:53 +00:00
|
|
|
|
2019-02-13 21:55:02 +00:00
|
|
|
return serializeOptions(startInPresentation, showPrintDialog, showPrintDialogAndExit, unique, noRaise, page, find);
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:02 +00:00
|
|
|
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page, const QString &find)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2020-10-24 23:11:57 +00:00
|
|
|
return QStringLiteral("%1:%2:%3:%4:%5:%6:%7").arg(startInPresentation).arg(showPrintDialog).arg(showPrintDialogAndExit).arg(unique).arg(noRaise).arg(page, QString::fromLatin1(find.toUtf8().toBase64()));
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:02 +00:00
|
|
|
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page, QString *find)
|
2014-08-08 20:35:53 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
const QStringList args = serializedOptions.split(QStringLiteral(":"));
|
2019-02-13 21:55:02 +00:00
|
|
|
|
|
|
|
if (args.count() == 7) {
|
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];
|
2019-02-13 21:55:02 +00:00
|
|
|
*find = args[6];
|
2014-08-08 20:35:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startInPresentation(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2019-02-13 21:55:02 +00:00
|
|
|
return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummy, &dummyString, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool showPrintDialog(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2019-02-13 21:55:02 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummy, &dummyString, &dummyString) && result;
|
2018-03-03 16:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool showPrintDialogAndExit(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2019-02-13 21:55:02 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummy, &dummyString, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool unique(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2019-02-13 21:55:02 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummy, &dummyString, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool noRaise(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
bool result, dummy;
|
|
|
|
QString dummyString;
|
2019-02-13 21:55:02 +00:00
|
|
|
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result, &dummyString, &dummyString) && result;
|
2014-08-08 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString page(const QString &serializedOptions)
|
|
|
|
{
|
2019-02-13 21:55:02 +00:00
|
|
|
QString result, dummyString;
|
2014-08-08 20:35:53 +00:00
|
|
|
bool dummy;
|
2019-02-13 21:55:02 +00:00
|
|
|
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &result, &dummyString);
|
2014-08-08 20:35:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:02 +00:00
|
|
|
QString find(const QString &serializedOptions)
|
|
|
|
{
|
|
|
|
QString result, dummyString;
|
|
|
|
bool dummy;
|
|
|
|
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &dummyString, &result);
|
2020-10-24 23:11:57 +00:00
|
|
|
return QString::fromUtf8(QByteArray::fromBase64(result.toLatin1()));
|
2019-02-13 21:55:02 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 23:48:04 +00:00
|
|
|
}
|