diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,12 +58,19 @@ URL "http://www.exiv2.org" TYPE OPTIONAL) -# Find podofo -find_package(PoDoFo) -set_package_properties(PoDoFo PROPERTIES - DESCRIPTION "A library to access PDF metadata" - URL "http://podofo.sourceforge.net/" - TYPE OPTIONAL) +find_package(Poppler COMPONENTS Qt5) +set_package_properties(Poppler PROPERTIES + TYPE OPTIONAL + PURPOSE "TODO") + +if(NOT Poppler_Qt5_FOUND) + # Find podofo + find_package(PoDoFo) + set_package_properties(PoDoFo PROPERTIES + DESCRIPTION "A library to access PDF metadata" + URL "http://podofo.sourceforge.net/" + TYPE OPTIONAL) +endif() # Find freetype find_package(Freetype) @@ -79,6 +86,7 @@ include(MacroBoolTo01) macro_bool_to_01(TAGLIB_FOUND HAVE_TAGLIB) macro_bool_to_01(EXIV2_FOUND HAVE_EXIV2) +macro_bool_to_01(Poppler_Qt5_FOUND HAVE_POPPLER) macro_bool_to_01(PoDoFo_FOUND HAVE_PODOFO) macro_bool_to_01(FREETYPE_FOUND HAVE_FREETYPE) configure_file(config-krename.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-krename.h) diff --git a/config-krename.h.cmake b/config-krename.h.cmake --- a/config-krename.h.cmake +++ b/config-krename.h.cmake @@ -6,6 +6,9 @@ /* have Exiv2 */ #define HAVE_EXIV2 ${HAVE_EXIV2} +/* have Poppler */ +#define HAVE_POPPLER ${HAVE_POPPLER} + /* have PoDoFo */ #define HAVE_PODOFO ${HAVE_PODOFO} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,7 +14,7 @@ include_directories(${EXIV2_INCLUDE_DIR}) endif() -if(PoDoFo_FOUND) +if(NOT Poppler_Qt5_FOUND AND PoDoFo_FOUND) include_directories(${PoDoFo_INCLUDE_DIRS}) add_definitions(${PoDoFo_DEFINITIONS}) endif() @@ -79,10 +79,12 @@ ) endif() -if(PoDoFo_FOUND) - set(krename_SRCS ${krename_SRCS} - podofoplugin.cpp - ) +if(Poppler_Qt5_FOUND) + set(krename_SRCS ${krename_SRCS} popplerplugin.cpp) +else() + if(PoDoFo_FOUND) + set(krename_SRCS ${krename_SRCS} podofoplugin.cpp) + endif() endif() if(FREETYPE_FOUND) @@ -136,10 +138,16 @@ ${EXIV2_LIBRARIES} ) endif() -if(PoDoFo_FOUND) +if(Poppler_Qt5_FOUND) target_link_libraries(krename - ${PoDoFo_LIBRARIES} + Poppler::Qt5 ) +else() + if(PoDoFo_FOUND) + target_link_libraries(krename + ${PoDoFo_LIBRARIES} + ) + endif() endif() if(FREETYPE_FOUND) target_link_libraries(krename diff --git a/src/pluginloader.cpp b/src/pluginloader.cpp --- a/src/pluginloader.cpp +++ b/src/pluginloader.cpp @@ -40,6 +40,9 @@ #if HAVE_EXIV2 #include "exiv2plugin.h" #endif // HAVE_EXIV2 +#if HAVE_POPPLER +#include "popplerplugin.h" +#endif #if HAVE_PODOFO # include "podofoplugin.h" #endif // HAVE_PODOFO @@ -150,6 +153,9 @@ #if HAVE_TAGLIB m_plugins.append(new TagLibPlugin(this)); #endif // HAVE_TAGLIB +#if HAVE_POPPLER + m_plugins.append(new PopplerPlugin(this)); +#endif #if HAVE_PODOFO m_plugins.append(new PodofoPlugin(this)); #endif // HAVE_PODOFO diff --git a/src/popplerplugin.h b/src/popplerplugin.h new file mode 100644 --- /dev/null +++ b/src/popplerplugin.h @@ -0,0 +1,78 @@ +/*************************************************************************** + popplerplugin.h - description + ------------------- + begin : Tue January 23th 2017 + copyright : (C) 2017 by Heiko Becker + email : heirecka@exherbo.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. * + * * + ***************************************************************************/ + +#ifndef POPPLER_PLUGIN_H +#define POPPLER_PLUGIN_H + +#include "fileplugin.h" + +#include + +class PopplerPlugin : public FilePlugin +{ +public: + explicit PopplerPlugin(PluginLoader *loader); + + /** + * This function is the core of your plugin. + * + * It does the actual processing of a file, filename or token depending of the type + * of your plugin. + * + * \see type() + * + * @param b the parent BatchRenamer instance calling this plugin + * @param index the index of the current file (i.e. the first file has index 0, + * the second file to be renamed has index 1 ....) + * @param filenameOrToken this parameter depends on the type of your plugin. + * If type is ePluginType_File, this is the absolute path + * or URL to the renamed file. + * If type is ePluginType_Filename, this is the filename + * (without path) as created by KRename. + * If type is ePluginType_Token, this is the contents of a token + * in brackets. If your plugin supports the token [example], + * KRename will pass the strign "example" to your method. + * @param eCurrentType the current type of plugin that is requested (for plugins that support more than one type) + * + * @returns the result of the function, depending on type(). + * @returns QString::null if this plugin has nothing to do. + * @returns A new filename if type is ePluginType_Filename + * @returns the value of the token if type is ePluginType_Token + * @returns an error message or QString::null if type is ePluginType_File + */ + virtual QString processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType eCurrentType); + + /** Returns help descriptions for the supported tokens + * + * The returned stringlist contains strings that are the tokens + * and the description separated by ;; + * + * @returns a stringlist containing help on the supported tokens + */ + inline virtual const QStringList &help() const; + +private: + QStringList m_help; + QMap m_mapRealKeys; +}; + +inline const QStringList &PopplerPlugin::help() const +{ + return m_help; +} + +#endif // POPPLER_PLUGIN_H diff --git a/src/popplerplugin.cpp b/src/popplerplugin.cpp new file mode 100644 --- /dev/null +++ b/src/popplerplugin.cpp @@ -0,0 +1,88 @@ +/*************************************************************************** + popplerplugin.cpp - description + ------------------- + begin : Wed Jan 23th 2017 + copyright : (C) 2017 by Heiko Becker + email : heirecka@web.de + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 "popplerplugin.h" + +#include + +#include + +#include "batchrenamer.h" +#include "tokenhelpdialog.h" + +PopplerPlugin::PopplerPlugin(PluginLoader *loader) + : FilePlugin(loader) +{ + this->addSupportedToken("pdfAuthor"); + this->addSupportedToken("pdfCreator"); + this->addSupportedToken("pdfKeywords"); + this->addSupportedToken("pdfSubject"); + this->addSupportedToken("pdfTitle"); + this->addSupportedToken("pdfProducer"); + this->addSupportedToken("pdfPages"); + m_help.append("[pdfAuthor]" + TokenHelpDialog::getTokenSeparator() + i18n("Author of the PDF file")); + m_help.append("[pdfCreator]" + TokenHelpDialog::getTokenSeparator() + i18n("Creator of the PDF file")); + m_help.append("[pdfKeywords]" + TokenHelpDialog::getTokenSeparator() + i18n("Keywords of the PDF file")); + m_help.append("[pdfSubject]" + TokenHelpDialog::getTokenSeparator() + i18n("Subject of the PDF file")); + m_help.append("[pdfTitle]" + TokenHelpDialog::getTokenSeparator() + i18n("Title of the PDF file")); + m_help.append("[pdfProducer]" + TokenHelpDialog::getTokenSeparator() + i18n("Producer of the PDF file")); + m_help.append("[pdfPages]" + TokenHelpDialog::getTokenSeparator() + i18n("Number of pages in the PDF file")); + + m_name = i18n("Poppler (PDF) Plugin"); + m_comment = i18n("This plugin supports reading tags from " + "PDF files."); + + m_icon = "application-pdf"; +} + +QString PopplerPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType) +{ + QString token(filenameOrToken.toLower()); + QString filename = (*b->files())[index].srcUrl().path(); + + if (!this->supports(token)) { + return QString(""); + } + + QScopedPointer document(Poppler::Document::load(filename.toUtf8())); + + if (!document || document->isLocked()) { + return QString(""); + } + + if (token == "pdfauthor") { + return document->info("Author"); + } else if (token == "pdfcreator") { + return document->info("Creator"); + } else if (token == "pdfkeywords") { + return document->info(QStringLiteral("Keywords")); + } else if (token == "pdfsubject") { + return document->info(QStringLiteral("Subject")); + } else if (token == "pdftitle") { + return document->info(QStringLiteral("Title")).trimmed(); + } else if (token == "pdfproducer") { + return document->info(QStringLiteral("Producer")); + } else if (token == "pdfpages") { + return QString::number(document->numPages()); + } + + /*} catch (PdfError &error) { + return QString::fromUtf8(error.what()); + }*/ + + return QString(""); +}