File indexing completed on 2024-04-21 16:32:34

0001 /***************************************************************************
0002                       podofoplugin.cpp  -  description
0003                              -------------------
0004     begin                : Wed May 26th 2010
0005     copyright            : (C) 2010 by Dominik Seichter
0006     email                : domseichter@web.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "podofoplugin.h"
0019 
0020 #include <podofo/podofo.h>
0021 
0022 #include "batchrenamer.h"
0023 #include "tokenhelpdialog.h"
0024 
0025 using namespace PoDoFo;
0026 
0027 PodofoPlugin::PodofoPlugin(PluginLoader *loader)
0028     : FilePlugin(loader)
0029 {
0030     this->addSupportedToken("pdfAuthor");
0031     this->addSupportedToken("pdfCreator");
0032     this->addSupportedToken("pdfKeywords");
0033     this->addSupportedToken("pdfSubject");
0034     this->addSupportedToken("pdfTitle");
0035     this->addSupportedToken("pdfProducer");
0036     this->addSupportedToken("pdfPages");
0037     m_help.append("[pdfAuthor]" + TokenHelpDialog::getTokenSeparator() + i18n("Author of the PDF file"));
0038     m_help.append("[pdfCreator]" + TokenHelpDialog::getTokenSeparator() + i18n("Creator of the PDF file"));
0039     m_help.append("[pdfKeywords]" + TokenHelpDialog::getTokenSeparator() + i18n("Keywords of the PDF file"));
0040     m_help.append("[pdfSubject]" + TokenHelpDialog::getTokenSeparator() + i18n("Subject of the PDF file"));
0041     m_help.append("[pdfTitle]" + TokenHelpDialog::getTokenSeparator() + i18n("Title of the PDF file"));
0042     m_help.append("[pdfProducer]" + TokenHelpDialog::getTokenSeparator() + i18n("Producer of the PDF file"));
0043     m_help.append("[pdfPages]" + TokenHelpDialog::getTokenSeparator() + i18n("Number of pages in the PDF file"));
0044 
0045     m_name = i18n("PoDoFo (PDF) Plugin");
0046     m_comment = i18n("<qt>This plugin supports reading tags from "
0047                      "PDF files.</qt>");
0048 
0049     m_icon = "application-pdf";
0050 }
0051 
0052 QString PodofoPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType)
0053 {
0054     QString token(filenameOrToken.toLower());
0055     QString filename = (*b->files())[index].srcUrl().path();
0056 
0057     if (!this->supports(token)) {
0058         return QString("");
0059     }
0060 
0061     try {
0062         PdfMemDocument doc;
0063         doc.Load(filename.toUtf8().data());
0064         PdfInfo *info = doc.GetInfo();
0065 
0066         if (token == "pdfauthor") {
0067             return QString::fromUtf8(info->GetAuthor().GetStringUtf8().c_str());
0068         } else if (token == "pdfcreator") {
0069             return QString::fromUtf8(info->GetCreator().GetStringUtf8().c_str());
0070         } else if (token == "pdfkeywords") {
0071             return QString::fromUtf8(info->GetKeywords().GetStringUtf8().c_str());
0072         } else if (token == "pdfsubject") {
0073             return QString::fromUtf8(info->GetSubject().GetStringUtf8().c_str());
0074         } else if (token == "pdftitle") {
0075             return QString::fromUtf8(info->GetTitle().GetStringUtf8().c_str());
0076         } else if (token == "pdfproducer") {
0077             return QString::fromUtf8(info->GetProducer().GetStringUtf8().c_str());
0078         } else if (token == "pdfpages") {
0079             return QString::number(doc.GetPageCount());
0080         }
0081     } catch (PdfError &error) {
0082         return QString::fromUtf8(error.what());
0083     }
0084 
0085     return QString("");
0086 }