File indexing completed on 2022-09-27 16:33:55

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         PdfInfo *info = doc.GetInfo();
0051 
0052         if (token == "pdfauthor") {
0053             return QString::fromUtf8(info->GetAuthor().GetStringUtf8().c_str());
0054         } else if (token == "pdfcreator") {
0055             return QString::fromUtf8(info->GetCreator().GetStringUtf8().c_str());
0056         } else if (token == "pdfkeywords") {
0057             return QString::fromUtf8(info->GetKeywords().GetStringUtf8().c_str());
0058         } else if (token == "pdfsubject") {
0059             return QString::fromUtf8(info->GetSubject().GetStringUtf8().c_str());
0060         } else if (token == "pdftitle") {
0061             return QString::fromUtf8(info->GetTitle().GetStringUtf8().c_str());
0062         } else if (token == "pdfproducer") {
0063             return QString::fromUtf8(info->GetProducer().GetStringUtf8().c_str());
0064         } else if (token == "pdfpages") {
0065             return QString::number(doc.GetPageCount());
0066         }
0067     } catch (PdfError &error) {
0068         return QString::fromUtf8(error.what());
0069     }
0070 
0071     return QString("");
0072 }