File indexing completed on 2024-05-26 05:54:57

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2010 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "podofoplugin.h"
0005 
0006 #include <podofo/podofo.h>
0007 
0008 #include "batchrenamer.h"
0009 #include "tokenhelpdialog.h"
0010 
0011 using namespace PoDoFo;
0012 
0013 PodofoPlugin::PodofoPlugin(PluginLoader *loader)
0014     : FilePlugin(loader)
0015 {
0016     this->addSupportedToken("pdfAuthor");
0017     this->addSupportedToken("pdfCreator");
0018     this->addSupportedToken("pdfKeywords");
0019     this->addSupportedToken("pdfSubject");
0020     this->addSupportedToken("pdfTitle");
0021     this->addSupportedToken("pdfProducer");
0022     this->addSupportedToken("pdfPages");
0023     m_help.append("[pdfAuthor]" + TokenHelpDialog::getTokenSeparator() + i18n("Author of the PDF file"));
0024     m_help.append("[pdfCreator]" + TokenHelpDialog::getTokenSeparator() + i18n("Creator of the PDF file"));
0025     m_help.append("[pdfKeywords]" + TokenHelpDialog::getTokenSeparator() + i18n("Keywords of the PDF file"));
0026     m_help.append("[pdfSubject]" + TokenHelpDialog::getTokenSeparator() + i18n("Subject of the PDF file"));
0027     m_help.append("[pdfTitle]" + TokenHelpDialog::getTokenSeparator() + i18n("Title of the PDF file"));
0028     m_help.append("[pdfProducer]" + TokenHelpDialog::getTokenSeparator() + i18n("Producer of the PDF file"));
0029     m_help.append("[pdfPages]" + TokenHelpDialog::getTokenSeparator() + i18n("Number of pages in the PDF file"));
0030 
0031     m_name = i18n("PoDoFo (PDF) Plugin");
0032     m_comment = i18n("<qt>This plugin supports reading tags from "
0033                      "PDF files.</qt>");
0034 
0035     m_icon = "application-pdf";
0036 }
0037 
0038 QString PodofoPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType)
0039 {
0040     QString token(filenameOrToken.toLower());
0041     QString filename = (*b->files())[index].srcUrl().path();
0042 
0043     if (!this->supports(token)) {
0044         return QString("");
0045     }
0046 
0047     try {
0048         PdfMemDocument doc;
0049         doc.Load(filename.toUtf8().data());
0050 #if (PODOFO_VERSION_MINOR>=10 || PODOFO_VERSION_MAJOR>=1)
0051         const PdfInfo *info = doc.GetInfo();
0052 
0053         if (token == "pdfauthor") {
0054             return info->GetAuthor().has_value() ? QString::fromUtf8(info->GetAuthor()->GetString().c_str()) : QString();
0055         } else if (token == "pdfcreator") {
0056             return info->GetCreator().has_value() ? QString::fromUtf8(info->GetCreator()->GetString().c_str()) : QString();
0057         } else if (token == "pdfkeywords") {
0058             return info->GetKeywords().has_value() ? QString::fromUtf8(info->GetKeywords()->GetString().c_str()) : QString();
0059         } else if (token == "pdfsubject") {
0060             return info->GetSubject().has_value() ? QString::fromUtf8(info->GetSubject()->GetString().c_str()) : QString();
0061         } else if (token == "pdftitle") {
0062             return info->GetTitle().has_value() ? QString::fromUtf8(info->GetTitle()->GetString().c_str()) : QString();
0063         } else if (token == "pdfproducer") {
0064             return info->GetProducer().has_value() ? QString::fromUtf8(info->GetProducer()->GetString().c_str()) : QString();
0065         } else if (token == "pdfpages") {
0066             return QString::number(doc.GetPages().GetCount());
0067         }
0068 #else
0069         PdfInfo *info = doc.GetInfo();
0070 
0071         if (token == "pdfauthor") {
0072             return QString::fromUtf8(info->GetAuthor().GetStringUtf8().c_str());
0073         } else if (token == "pdfcreator") {
0074             return QString::fromUtf8(info->GetCreator().GetStringUtf8().c_str());
0075         } else if (token == "pdfkeywords") {
0076             return QString::fromUtf8(info->GetKeywords().GetStringUtf8().c_str());
0077         } else if (token == "pdfsubject") {
0078             return QString::fromUtf8(info->GetSubject().GetStringUtf8().c_str());
0079         } else if (token == "pdftitle") {
0080             return QString::fromUtf8(info->GetTitle().GetStringUtf8().c_str());
0081         } else if (token == "pdfproducer") {
0082             return QString::fromUtf8(info->GetProducer().GetStringUtf8().c_str());
0083         } else if (token == "pdfpages") {
0084             return QString::number(doc.GetPageCount());
0085         }
0086 #endif
0087     } catch (PdfError &error) {
0088         return QString::fromUtf8(error.what());
0089     }
0090 
0091     return QString("");
0092 }