"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/bibExtractor.cpp" (12 Feb 2021, 8554 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bibExtractor.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "bibExtractor.h"
    8 
    9 #include "bibParser.h"
   10 #include "cb2bib_utilities.h"
   11 #include "document.h"
   12 #include "metadataParser.h"
   13 #include "network.h"
   14 #include "networkQuery.h"
   15 #include "settings.h"
   16 
   17 #include <QEventLoop>
   18 
   19 
   20 bibExtractor::bibExtractor(QObject* parento) : QObject(parento), cout(*(new QTextStream(stdout)))
   21 {
   22     _settingsP = settings::instance();
   23     _bpP = new bibParser(this);
   24     _mpP = new metadataParser(_bpP, this);
   25     _networkQP = new networkQuery(_bpP, this);
   26     init();
   27 }
   28 
   29 bibExtractor::bibExtractor(bibParser* bp, metadataParser* mp, networkQuery* nq, QObject* parento)
   30     : QObject(parento), cout(*(new QTextStream(stdout))), _bpP(bp), _mpP(mp), _networkQP(nq)
   31 {
   32     _settingsP = settings::instance();
   33     Q_ASSERT_X(_bpP, "bibExtractor", "bibParser was not instantiated");
   34     Q_ASSERT_X(_networkQP, "bibExtractor", "networkQuery was not instantiated");
   35     init();
   36 }
   37 
   38 bibExtractor::~bibExtractor()
   39 {
   40     delete &cout;
   41 }
   42 
   43 
   44 void bibExtractor::init()
   45 {
   46     connect(_networkQP, SIGNAL(queryEnded(bool,QString,QString)), this, SLOT(queryEnded(bool,QString,QString)),
   47             Qt::QueuedConnection);
   48     connect(_networkQP->networkPtr(), SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this,
   49             SLOT(proxyLogMessage()));
   50     _event_loop = new QEventLoop(this);
   51 }
   52 
   53 /**
   54     Extracts a bibliographic reference from files input_fns and writes it to the
   55     BibTeX file output_fn
   56 */
   57 int bibExtractor::extract(const QStringList& input_fns, const QString& output_fn, const InputType& type)
   58 {
   59     _input_type = type;
   60     QString bibtex;
   61     for (int i = 0; i < input_fns.count(); ++i)
   62     {
   63         const QString& input_fn = input_fns.at(i);
   64         if (!QFileInfo::exists(input_fn))
   65         {
   66             logMessage(tr("Error: File %1 does not exist.").arg(input_fn));
   67             continue;
   68         }
   69         logMessage(tr("Processing %1...").arg(input_fn));
   70         if (_input_type == Document)
   71         {
   72             _document_fn = input_fn;
   73             const QString text(documentToString(input_fn));
   74             extract(text);
   75         }
   76         else
   77         {
   78             _document_fn.clear();
   79             const QString text(c2bUtils::fileToString(input_fn));
   80             extract(text);
   81         }
   82         if (_settingsP->value("cb2Bib/MovePdf").toBool() && !_document_fn.isEmpty() && isReliable())
   83         {
   84             const QString file(_bpP->documentFilename(_document_fn, output_fn));
   85             const QString target_doc_fn(
   86                 c2bUtils::documentAbsoluteName(_settingsP->value("cb2Bib/RelativePdfDirectory").toBool(), output_fn,
   87                                                _settingsP->fileName("cb2Bib/PdfDirectory"), file));
   88             const QString doc_dirname(QFileInfo(target_doc_fn).absolutePath());
   89             QDir doc_dir(doc_dirname);
   90             if (!doc_dir.exists())
   91                 if (!doc_dir.mkpath(doc_dirname))
   92                     logMessage(tr("Error: Unable to create directory %1.").arg(doc_dirname));
   93             if (doc_dir.exists())
   94             {
   95                 const network::Action action(network::actionType(_settingsP->value("cb2Bib/CopyMovePdf").toString()));
   96                 logMessage(tr("Copying/moving to %1...").arg(target_doc_fn));
   97                 _networkQP->networkPtr()->getFile(_document_fn, target_doc_fn, action, this, SLOT(copyMoveEnded(bool)));
   98                 _event_loop->exec();
   99                 if (_is_copymove_ok)
  100                 {
  101                     if (_settingsP->value("cb2Bib/InsertMetadata").toBool())
  102                     {
  103                         QString mp_error;
  104                         if (!_mpP->insertMetadata(_bpP->currentReference(), target_doc_fn, &mp_error))
  105                         {
  106                             logMessage(tr("Warning: Unable to insert metadata to file %1.").arg(target_doc_fn));
  107                             logMessage(mp_error);
  108                         }
  109                     }
  110                     _document_fn = file;
  111                 }
  112                 else
  113                 {
  114                     if (_settingsP->cl_sloppy)
  115                     {
  116                         logMessage(tr("Warning: Skipping file field %1.").arg(_document_fn));
  117                         _document_fn.clear();
  118                     }
  119                     else
  120                         _bpP->clearCurrentReference();
  121                 }
  122             }
  123         }
  124         if (isReliable())
  125         {
  126             if (!_document_fn.isEmpty())
  127                 _bpP->setField("file", _document_fn);
  128             bibtex += _bpP->toBibTeX();
  129         }
  130         else
  131             logMessage(tr("Warning: Skip writing reference for file %1.").arg(input_fn));
  132         logMessage(QString());
  133     }
  134     if (bibtex.isEmpty())
  135         logMessage(tr("Warning: No reference extracted."));
  136     else
  137     {
  138         logMessage(tr("Writing %1...").arg(output_fn));
  139         if (c2bUtils::stringToFile(bibtex, output_fn))
  140             return 0;
  141         else
  142             logMessage(tr("Error: Unable to write file %1.").arg(output_fn));
  143     }
  144     return 1;
  145 }
  146 
  147 /**
  148     Extracts a bibliographic reference from input string text writes it to the
  149     output string bibtex
  150 */
  151 int bibExtractor::extract(const QString& text, QString* bibtex)
  152 {
  153     _document_fn.clear();
  154     _input_type = PlainText;
  155     extract(text);
  156     *bibtex = _bpP->toBibTeX();
  157     if (_bpP->isAutoRecognized())
  158         return 0;
  159     else if (_bpP->fieldCount() > 0)
  160         return 1;
  161     else
  162         return 2;
  163 }
  164 
  165 void bibExtractor::extract(const QString& text)
  166 {
  167     _extract(text);
  168     if (_settingsP->value("cb2Bib/AutomaticQuery").toBool())
  169         if (!_bpP->autoRecognizedString().contains("BibTeX") && _bpP->fieldCount() > 0)
  170         {
  171             logMessage(tr("Submitting query..."));
  172             _settingsP->setValue("networkQuery/isSupervised", false);
  173             _networkQP->submitQuery(_bpP->currentReference(), text, _input_type == PlainText);
  174             _event_loop->exec();
  175             _settingsP->setValue("networkQuery/isSupervised", true);
  176         }
  177     _bpP->setCiteID();
  178 }
  179 
  180 void bibExtractor::_extract(const QString& text)
  181 {
  182     _bpP->clearCurrentReference();
  183     _bpP->setReferenceType("unknown");
  184     if (text.trimmed().isEmpty())
  185     {
  186         logMessage(tr("Warning: Empty input string."));
  187         return;
  188     }
  189     QString out_text, out_tagtext;
  190     if (_settingsP->value("cb2Bib/PreparserAutomatic").toBool())
  191         _bpP->preparse(text, &out_text);
  192     _bpP->parse(text, &out_text, &out_tagtext);
  193     if (_bpP->fieldCount() > 0)
  194         _bpP->setReferenceType("article");
  195     logMessage(_bpP->autoRecognizedString());
  196 }
  197 
  198 void bibExtractor::queryEnded(bool succeeded, const QString& targetPDF, const QString& targetBib)
  199 {
  200     if (succeeded)
  201     {
  202         _extract(targetBib);
  203         if (_input_type == PlainText && _bpP->isAutoRecognized() && !targetPDF.isEmpty())
  204             _document_fn = targetPDF;
  205     }
  206     _event_loop->exit();
  207 }
  208 
  209 void bibExtractor::copyMoveEnded(bool ok)
  210 {
  211     _is_copymove_ok = ok;
  212     if (!_is_copymove_ok)
  213         logMessage(tr("Error: Copy/move failed. %1").arg(_networkQP->networkPtr()->errorString()));
  214     _event_loop->exit();
  215 }
  216 
  217 void bibExtractor::proxyLogMessage() const
  218 {
  219     _networkQP->networkPtr()->cancelDownload();
  220     logMessage(tr("Warning: Proxy authentication dialog is disabled in command line mode."));
  221 }
  222 
  223 void bibExtractor::logMessage(const QString& ms) const
  224 {
  225     cout << "[cb2bib] " << ms << endl;
  226 }
  227 
  228 bool bibExtractor::isReliable() const
  229 {
  230     if (_settingsP->cl_sloppy)
  231         return true;
  232     else
  233         return _bpP->isAutoRecognized();
  234 }
  235 
  236 QString bibExtractor::documentToString(const QString& fn) const
  237 {
  238     document doc(fn, document::FirstPage);
  239     const QString text(doc.toString());
  240     if (!doc.errorString().isEmpty())
  241         logMessage("Error: " + doc.errorString());
  242     if (_settingsP->value("cb2Bib/AddMetadata").toBool())
  243     {
  244         // Add metadata to document text
  245         const QString metadata(_mpP->metadata(fn));
  246         if (_settingsP->value("cb2Bib/PreAppendMetadata").toString() == "prepend")
  247             return metadata + text;
  248         else
  249             return text + '\n' + metadata;
  250     }
  251     else
  252         return text;
  253 }