c2bPostprocess.cpp 4.64 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2018 by Pere Constans
3
 *   constans@molspaces.com
4
 *   cb2Bib version 1.9.9. Licensed under the GNU GPL version 3.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bPostprocess.h"

#include "c2b.h"
#include "c2bSettings.h"
#include "c2bUtils.h"

#include <QDir>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>


c2bPostprocess::c2bPostprocess(const QString& fn, QWidget* parentw) : QDialog(parentw)
{
    ui.setupUi(this);
22
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
23 24
    setWindowTitle(tr("BibTeX Postprocessing Log - cb2Bib"));
    setAttribute(Qt::WA_DeleteOnClose);
25 26 27 28 29
    _buttonOpenFile = new QPushButton(tr("&Open File"));
    ui.buttonBox->addButton(_buttonOpenFile, QDialogButtonBox::ActionRole);
    _buttonOpenFile->setAutoDefault(true);
    _buttonOpenFile->setDefault(true);
    _buttonOpenFile->setEnabled(false);
30 31
    ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(false);
    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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    PPBIBTEX_ARG = settings->value("cb2Bib/PostprocessBibTeXArg").toString();
    PPBIBTEX_EXT = settings->value("cb2Bib/PostprocessBibTeXExt").toString();

    QFile f(fn);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::warning(parentw, tr("Warning - cb2Bib"),
                             tr("Unable to open the file %1 for reading.\nError: '%2'.").
                             arg(fn).arg(f.errorString()), QMessageBox::Ok);
        ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
        ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
        ui.Log->appendPlainText(tr("BibTeX Post Processing: Erroneous termination.\n"));
    }
    else
    {
        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 57
        cargs.replace("%finput", _bib);
        cargs.replace("%foutput", _out);
58 59 60 61 62 63 64 65 66 67 68 69 70 71
        ui.Log->appendPlainText(tr("BibTeX Post Processing Command:\n%1 %2\n").arg(PPBIBTEX_BIN).arg(cargs));
        PProcessFile();
    }
}

c2bPostprocess::~c2bPostprocess()
{
    c2bSettingsP->setValue("c2bLogWidget/size", size());
}


void c2bPostprocess::PProcessFile()
{
    // 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 81 82
        else
            args.append(arglist.at(i));
    }
83 84 85 86
    _ppBibTeX = new QProcess(this);
    connect(_ppBibTeX, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(conversionEnded()));
    _ppBibTeX->start(PPBIBTEX_BIN, args);
    if (!_ppBibTeX->waitForStarted())
87 88 89 90
    {
        ui.Log->appendPlainText(tr("Failed to call %1.").arg(PPBIBTEX_BIN));
        ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
        ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
91
        delete _ppBibTeX;
92 93 94 95 96 97 98
    }
}

void c2bPostprocess::conversionEnded()
{
    addToLog();
    ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
99 100 101 102 103 104 105 106 107 108 109
    if (QFileInfo(_out).isReadable())
    {
        _buttonOpenFile->setEnabled(true);
        _buttonOpenFile->setFocus();
    }
    else
    {
        ui.buttonBox->button(QDialogButtonBox::Close)->setAutoDefault(true);
        ui.buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
        ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
    }
110
    ui.Log->appendPlainText(tr("\n\nBibTeX Post Processing Command Ended."));
111
    delete _ppBibTeX;
112 113 114 115
}

void c2bPostprocess::addToLog()
{
116
    QString log(QString::fromUtf8(_ppBibTeX->readAllStandardOutput().trimmed()));
117 118
    if (!log.isEmpty())
        ui.Log->appendPlainText(log);
119
    log = QString::fromUtf8(_ppBibTeX->readAllStandardError().trimmed());
120 121 122 123 124 125
    if (!log.isEmpty())
        ui.Log->appendPlainText(log);
}

void c2bPostprocess::openFile()
{
126
    c2bUtils::openFile(_out, this);
127 128 129 130 131
    close();
}

void c2bPostprocess::help()
{
132
    c2bUtils::displayHelp("https://www.molspaces.com/cb2bib/doc/overview/#additional-keyboard-functionality");
133
}