"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bPostprocess.cpp" (12 Feb 2021, 4740 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 "c2bPostprocess.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 "c2bPostprocess.h"
    8 
    9 #include "c2b.h"
   10 #include "c2bSettings.h"
   11 #include "c2bUtils.h"
   12 
   13 #include <QDir>
   14 #include <QMessageBox>
   15 #include <QProcess>
   16 #include <QPushButton>
   17 
   18 
   19 c2bPostprocess::c2bPostprocess(const QString& fn, QWidget* parentw) : QDialog(parentw)
   20 {
   21     ui.setupUi(this);
   22     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
   23     setWindowTitle(tr("BibTeX Postprocessing Log - cb2Bib"));
   24     setAttribute(Qt::WA_DeleteOnClose);
   25     _buttonOpenFile = new QPushButton(tr("&Open File"));
   26     ui.buttonBox->addButton(_buttonOpenFile, QDialogButtonBox::ActionRole);
   27     _buttonOpenFile->setAutoDefault(true);
   28     _buttonOpenFile->setDefault(true);
   29     _buttonOpenFile->setEnabled(false);
   30     ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(false);
   31     connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
   32     connect(_buttonOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
   33     c2bSettings* settings(c2bSettingsP);
   34     resize(settings->value("c2bLogWidget/size", size()).toSize());
   35     PPBIBTEX_BIN = settings->fileName("cb2Bib/PostprocessBibTeXBin");
   36     PPBIBTEX_ARG = settings->value("cb2Bib/PostprocessBibTeXArg").toString();
   37     PPBIBTEX_EXT = settings->value("cb2Bib/PostprocessBibTeXExt").toString();
   38 
   39     QFile f(fn);
   40     if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
   41     {
   42         QMessageBox::warning(parentw, tr("Warning - cb2Bib"),
   43                              tr("Unable to open the file %1 for reading.\nError: '%2'.").arg(fn, f.errorString()),
   44                              QMessageBox::Ok);
   45         ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
   46         ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
   47         ui.Log->appendPlainText(tr("BibTeX Post Processing: Erroneous termination.\n"));
   48     }
   49     else
   50     {
   51         f.close();
   52         _bib = fn;
   53         QFileInfo fi(fn);
   54         _out = QDir::toNativeSeparators(fi.absolutePath() + '/' + fi.baseName() + '.' + PPBIBTEX_EXT);
   55         QString cargs(PPBIBTEX_ARG);
   56         cargs.replace("%finput", _bib);
   57         cargs.replace("%foutput", _out);
   58         ui.Log->appendPlainText(tr("BibTeX Post Processing Command:\n%1 %2\n").arg(PPBIBTEX_BIN, cargs));
   59         PProcessFile();
   60     }
   61 }
   62 
   63 c2bPostprocess::~c2bPostprocess()
   64 {
   65     c2bSettingsP->setValue("c2bLogWidget/size", size());
   66 }
   67 
   68 
   69 void c2bPostprocess::PProcessFile()
   70 {
   71     // Postprocessing BibTeX
   72     const QStringList arglist(PPBIBTEX_ARG.split(' ', QString::SkipEmptyParts));
   73     QStringList args;
   74     for (int i = 0; i < arglist.count(); i++)
   75     {
   76         if (arglist.at(i) == "%finput")
   77             args.append(_bib);
   78         else if (arglist.at(i) == "%foutput")
   79             args.append(_out);
   80         else
   81             args.append(arglist.at(i));
   82     }
   83     _ppBibTeX = new QProcess(this);
   84     connect(_ppBibTeX, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(conversionEnded()));
   85     _ppBibTeX->start(PPBIBTEX_BIN, args);
   86     if (!_ppBibTeX->waitForStarted())
   87     {
   88         ui.Log->appendPlainText(tr("Failed to call %1.").arg(PPBIBTEX_BIN));
   89         ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
   90         ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
   91         delete _ppBibTeX;
   92     }
   93 }
   94 
   95 void c2bPostprocess::conversionEnded()
   96 {
   97     addToLog();
   98     ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
   99     if (QFileInfo(_out).isReadable())
  100     {
  101         _buttonOpenFile->setEnabled(true);
  102         _buttonOpenFile->setFocus();
  103     }
  104     else
  105     {
  106         ui.buttonBox->button(QDialogButtonBox::Close)->setAutoDefault(true);
  107         ui.buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
  108         ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
  109     }
  110     ui.Log->appendPlainText(tr("\n\nBibTeX Post Processing Command Ended."));
  111     delete _ppBibTeX;
  112 }
  113 
  114 void c2bPostprocess::addToLog()
  115 {
  116     QString log(QString::fromUtf8(_ppBibTeX->readAllStandardOutput().trimmed()));
  117     if (!log.isEmpty())
  118         ui.Log->appendPlainText(log);
  119     log = QString::fromUtf8(_ppBibTeX->readAllStandardError().trimmed());
  120     if (!log.isEmpty())
  121         ui.Log->appendPlainText(log);
  122 }
  123 
  124 void c2bPostprocess::openFile()
  125 {
  126     c2bUtils::openFile(_out, this);
  127     close();
  128 }
  129 
  130 void c2bPostprocess::help()
  131 {
  132     c2bUtils::displayHelp("https://www.molspaces.com/cb2bib/doc/overview/#additional-keyboard-functionality");
  133 }