diff --git a/PdfForQt.pro b/PdfForQt.pro index 1f1e53c..00ee9ae 100644 --- a/PdfForQt.pro +++ b/PdfForQt.pro @@ -22,6 +22,7 @@ SUBDIRS += \ JBIG2_Viewer \ PdfExampleGenerator \ PdfForQtLib \ + PdfTool \ UnitTests \ PdfForQtViewer diff --git a/PdfForQtViewer/main.cpp b/PdfForQtViewer/main.cpp index eeb3dab..a41c59a 100644 --- a/PdfForQtViewer/main.cpp +++ b/PdfForQtViewer/main.cpp @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationName("MelkaJ"); QCoreApplication::setApplicationName("PDF Viewer"); - QCoreApplication::setApplicationVersion("0.1"); + QCoreApplication::setApplicationVersion("1.0.0"); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); diff --git a/PdfTool/PdfTool.pro b/PdfTool/PdfTool.pro new file mode 100644 index 0000000..4ed7ca7 --- /dev/null +++ b/PdfTool/PdfTool.pro @@ -0,0 +1,57 @@ +# Copyright (C) 2020 Jakub Melka +# +# This file is part of PdfForQt. +# +# PdfForQt is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# PdfForQt is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with PDFForQt. If not, see . + +QT -= gui + +CONFIG += c++11 console +CONFIG -= app_bundle + +TARGET = PdfTool +VERSION = 1.0.0 + +QMAKE_TARGET_DESCRIPTION = "PDF tool for Qt" +QMAKE_TARGET_COPYRIGHT = "(c) Jakub Melka 2018-2020" + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +QMAKE_CXXFLAGS += /std:c++latest /utf-8 + +INCLUDEPATH += $$PWD/../PDFForQtLib/Sources + +DESTDIR = $$OUT_PWD/.. + +LIBS += -L$$OUT_PWD/.. + +LIBS += -lPDFForQtLib + +SOURCES += \ + main.cpp \ + pdftoolabstractapplication.cpp + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +application.files = $$DESTDIR/PdfTool.exe +application.path = $$DESTDIR/install +INSTALLS += application + +HEADERS += \ + pdftoolabstractapplication.h diff --git a/PdfTool/main.cpp b/PdfTool/main.cpp new file mode 100644 index 0000000..f3ae672 --- /dev/null +++ b/PdfTool/main.cpp @@ -0,0 +1,58 @@ +// Copyright (C) 2020 Jakub Melka +// +// This file is part of PdfForQt. +// +// PdfForQt is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// PdfForQt is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDFForQt. If not, see . + +#include "pdftoolabstractapplication.h" + +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + QCoreApplication::setOrganizationName("MelkaJ"); + QCoreApplication::setApplicationName("PdfTool"); + QCoreApplication::setApplicationVersion("1.0.0"); + + QStringList arguments = QCoreApplication::arguments(); + + QCommandLineParser parser; + parser.setApplicationDescription("PdfTool - work with pdf documents via command line"); + parser.addPositionalArgument("command", "Command to execute."); + parser.parse(arguments); + + QStringList positionalArguments = parser.positionalArguments(); + QString command = !positionalArguments.isEmpty() ? positionalArguments.front() : QString(); + arguments.removeOne(command); + + pdftool::PDFToolAbstractApplication* application = pdftool::PDFToolApplicationStorage::getApplicationByCommand(command); + if (!application) + { + application = pdftool::PDFToolApplicationStorage::getDefaultApplication(); + } + else + { + parser.clearPositionalArguments(); + } + + application->initializeCommandLineParser(&parser); + + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(arguments); + + return application->execute(application->getOptions(&parser)); +} diff --git a/PdfTool/pdftoolabstractapplication.cpp b/PdfTool/pdftoolabstractapplication.cpp new file mode 100644 index 0000000..7253a3d --- /dev/null +++ b/PdfTool/pdftoolabstractapplication.cpp @@ -0,0 +1,112 @@ +// Copyright (C) 2020 Jakub Melka +// +// This file is part of PdfForQt. +// +// PdfForQt is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// PdfForQt is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDFForQt. If not, see . + +#include "pdftoolabstractapplication.h" + +namespace pdftool +{ + +class PDFToolHelpApplication : public PDFToolAbstractApplication +{ +public: + PDFToolHelpApplication() : PDFToolAbstractApplication(true) { } + + virtual QString getStandardString(StandardString standardString) const override; + virtual int execute(const PDFToolOptions& options) override; +}; + +static PDFToolHelpApplication s_helpApplication; + +QString PDFToolHelpApplication::getStandardString(StandardString standardString) const +{ + switch (standardString) + { + case Command: + return "help"; + + case Name: + return PDFToolTranslationContext::tr("Help"); + + case Description: + return PDFToolTranslationContext::tr("Show list of all available commands."); + + default: + Q_ASSERT(false); + break; + } + + return QString(); +} + +int PDFToolHelpApplication::execute(const PDFToolOptions& options) +{ + return EXIT_SUCCESS; +} + +PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault) +{ + PDFToolApplicationStorage::registerApplication(this, isDefault); +} + +void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser* parser) const +{ + +} + +PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser) const +{ + PDFToolOptions options; + + return options; +} + +PDFToolAbstractApplication* PDFToolApplicationStorage::getApplicationByCommand(const QString& command) +{ + for (PDFToolAbstractApplication* application : getInstance()->m_applications) + { + if (application->getStandardString(PDFToolAbstractApplication::Command) == command) + { + return application; + } + } + + return nullptr; +} + +void PDFToolApplicationStorage::registerApplication(PDFToolAbstractApplication* application, bool isDefault) +{ + PDFToolApplicationStorage* storage = getInstance(); + storage->m_applications.push_back(application); + + if (isDefault) + { + storage->m_defaultApplication = application; + } +} + +PDFToolAbstractApplication* PDFToolApplicationStorage::getDefaultApplication() +{ + return getInstance()->m_defaultApplication; +} + +PDFToolApplicationStorage* PDFToolApplicationStorage::getInstance() +{ + static PDFToolApplicationStorage storage; + return &storage; +} + +} // pdftool diff --git a/PdfTool/pdftoolabstractapplication.h b/PdfTool/pdftoolabstractapplication.h new file mode 100644 index 0000000..34aee3b --- /dev/null +++ b/PdfTool/pdftoolabstractapplication.h @@ -0,0 +1,96 @@ +// Copyright (C) 2020 Jakub Melka +// +// This file is part of PdfForQt. +// +// PdfForQt is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// PdfForQt is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDFForQt. If not, see . + +#ifndef PDFTOOLABSTRACTAPPLICATION_H +#define PDFTOOLABSTRACTAPPLICATION_H + +#include +#include +#include + +#include + +class QCommandLineParser; + +namespace pdftool +{ + +struct PDFToolTranslationContext +{ + Q_DECLARE_TR_FUNCTIONS(PDFToolTranslationContext) +}; + +struct PDFToolOptions +{ + QString document; + QString password; +}; + +/// Base class for all applications +class PDFToolAbstractApplication +{ +public: + explicit PDFToolAbstractApplication(bool isDefault = false); + virtual ~PDFToolAbstractApplication() = default; + + enum StandardString + { + Command, ///< Command, by which is this application invoked + Name, ///< Name of application + Description ///< Description (what this application does) + }; + + virtual QString getStandardString(StandardString standardString) const = 0; + virtual int execute(const PDFToolOptions& options) = 0; + + void initializeCommandLineParser(QCommandLineParser* parser) const; + PDFToolOptions getOptions(QCommandLineParser* parser) const; +}; + +/// This class stores information about all applications available. Application +/// can be selected by command string. Also, default application is available. +class PDFToolApplicationStorage +{ +public: + + /// Returns application by command. If application for given command is not found, + /// then nullptr is returned. + /// \param command Command + /// \returns Application for given command or nullptr + static PDFToolAbstractApplication* getApplicationByCommand(const QString& command); + + /// Registers application to the application storage. If \p isDefault + /// is set to true, application is registered as default application. + /// \param application Apllication + /// \param isDefault Is default application? + static void registerApplication(PDFToolAbstractApplication* application, bool isDefault = false); + + /// Returns default application + /// \returns Default application + static PDFToolAbstractApplication* getDefaultApplication(); + +private: + PDFToolApplicationStorage() = default; + static PDFToolApplicationStorage* getInstance(); + + std::vector m_applications; + PDFToolAbstractApplication* m_defaultApplication = nullptr; +}; + +} // namespace pdftool + +#endif // PDFTOOLABSTRACTAPPLICATION_H