From 056d614dc2166cd25749caf264b1b4d9d348f4d4 Mon Sep 17 00:00:00 2001
From: Antonio Rojas <arojas@archlinux.org>
Date: Mon, 17 Jul 2023 20:29:37 +0000
Subject: [PATCH] Support podofo 0.10

Version 0.10 of podofo is a complete rewrite. krename's use of it is minimal, so porting is easy.

Switch the cmake module to use pkgconfig, which is available since 0.9.5 (release in 2017).

Unfortunately, the hack to find the version number is still needed, since the pc file is buggy and ships an empty "Version" field.
---
 cmake/modules/FindPoDoFo.cmake | 31 +++++++++++++++----------------
 src/podofoplugin.cpp           | 20 ++++++++++++++++++++
 2 files changed, 35 insertions(+), 16 deletions(-)

--- a/cmake/modules/FindPoDoFo.cmake
+++ b/cmake/modules/FindPoDoFo.cmake
@@ -36,15 +36,8 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-find_path(PoDoFo_INCLUDE_DIRS
-  NAMES podofo/podofo.h
-)
-find_library(PoDoFo_LIBRARIES
-  NAMES libpodofo podofo
-)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(PoDoFo DEFAULT_MSG PoDoFo_LIBRARIES PoDoFo_INCLUDE_DIRS)
+include(FindPkgConfig)
+pkg_search_module(PoDoFo libpodofo libpodofo-0)
 
 set(PoDoFo_DEFINITIONS)
 if(PoDoFo_FOUND)
@@ -61,17 +54,19 @@ if(PoDoFo_FOUND)
     endif()
   endif()
 
-  # PoDoFo-0.9.5 unconditionally includes openssl/opensslconf.h in a public
-  # header. The fix is in https://sourceforge.net/p/podofo/code/1830/ and will
-  # hopefully be released soon with 0.9.6. Note that krename doesn't use
-  # OpenSSL in any way.
-  file(STRINGS "${PoDoFo_INCLUDE_DIRS}/podofo/base/podofo_config.h" PoDoFo_MAJOR_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_MAJOR[ \t]+[0-9]+$")
-  file(STRINGS "${PoDoFo_INCLUDE_DIRS}/podofo/base/podofo_config.h" PoDoFo_MINOR_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_MINOR[ \t]+[0-9]+$")
-  file(STRINGS "${PoDoFo_INCLUDE_DIRS}/podofo/base/podofo_config.h" PoDoFo_PATCH_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_PATCH[ \t]+[0-9]+$")
+  find_file(PoDoFo_CONFIG podofo_config.h PATHS ${PoDoFo_INCLUDE_DIRS} PATH_SUFFIXES auxiliary base)
+  file(STRINGS "${PoDoFo_CONFIG}" PoDoFo_MAJOR_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_MAJOR[ \t]+[0-9]+$")
+  file(STRINGS "${PoDoFo_CONFIG}" PoDoFo_MINOR_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_MINOR[ \t]+[0-9]+$")
+  file(STRINGS "${PoDoFo_CONFIG}" PoDoFo_PATCH_VER_LINE REGEX "^#define[ \t]+PODOFO_VERSION_PATCH[ \t]+[0-9]+$")
   string(REGEX REPLACE "^#define[ \t]+PODOFO_VERSION_MAJOR[ \t]+([0-9]+)$" "\\1" PoDoFo_MAJOR_VER "${PoDoFo_MAJOR_VER_LINE}")
   string(REGEX REPLACE "^#define[ \t]+PODOFO_VERSION_MINOR[ \t]+([0-9]+)$" "\\1" PoDoFo_MINOR_VER "${PoDoFo_MINOR_VER_LINE}")
   string(REGEX REPLACE "^#define[ \t]+PODOFO_VERSION_PATCH[ \t]+([0-9]+)$" "\\1" PoDoFo_PATCH_VER "${PoDoFo_PATCH_VER_LINE}")
   set(PoDoFo_VERSION "${PoDoFo_MAJOR_VER}.${PoDoFo_MINOR_VER}.${PoDoFo_PATCH_VER}")
+
+  # PoDoFo-0.9.5 unconditionally includes openssl/opensslconf.h in a public
+  # header. The fix is in https://sourceforge.net/p/podofo/code/1830/ and will
+  # hopefully be released soon with 0.9.6. Note that krename doesn't use
+  # OpenSSL in any way.
   if(PoDoFo_VERSION VERSION_EQUAL "0.9.5")
     find_package(OpenSSL)
     if (OpenSSL_FOUND)
@@ -84,4 +79,8 @@ if(PoDoFo_FOUND)
   endif()
 endif()
 
+if(PoDoFo_VERSION VERSION_GREATER_EQUAL 0.10.0)
+  set(CMAKE_CXX_STANDARD 17)
+endif()
+
 mark_as_advanced(PoDoFo_INCLUDE_DIRS PoDoFo_LIBRARIES PoDoFo_DEFINITIONS)
--- a/src/podofoplugin.cpp
+++ b/src/podofoplugin.cpp
@@ -61,6 +61,25 @@ QString PodofoPlugin::processFile(BatchR
     try {
         PdfMemDocument doc;
         doc.Load(filename.toUtf8().data());
+#if (PODOFO_VERSION_MINOR>=10 || PODOFO_VERSION_MAJOR>=1)
+        const PdfInfo *info = doc.GetInfo();
+
+        if (token == "pdfauthor") {
+            return info->GetAuthor().has_value() ? QString::fromUtf8(info->GetAuthor()->GetString().c_str()) : QString();
+        } else if (token == "pdfcreator") {
+            return info->GetCreator().has_value() ? QString::fromUtf8(info->GetCreator()->GetString().c_str()) : QString();
+        } else if (token == "pdfkeywords") {
+            return info->GetKeywords().has_value() ? QString::fromUtf8(info->GetKeywords()->GetString().c_str()) : QString();
+        } else if (token == "pdfsubject") {
+            return info->GetSubject().has_value() ? QString::fromUtf8(info->GetSubject()->GetString().c_str()) : QString();
+        } else if (token == "pdftitle") {
+            return info->GetTitle().has_value() ? QString::fromUtf8(info->GetTitle()->GetString().c_str()) : QString();
+        } else if (token == "pdfproducer") {
+            return info->GetProducer().has_value() ? QString::fromUtf8(info->GetProducer()->GetString().c_str()) : QString();
+        } else if (token == "pdfpages") {
+            return QString::number(doc.GetPages().GetCount());
+        }
+#else
         PdfInfo *info = doc.GetInfo();
 
         if (token == "pdfauthor") {
@@ -78,6 +97,7 @@ QString PodofoPlugin::processFile(BatchR
         } else if (token == "pdfpages") {
             return QString::number(doc.GetPageCount());
         }
+#endif
     } catch (PdfError &error) {
         return QString::fromUtf8(error.what());
     }
