File: c2bUpdateMetadata.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (193 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bUpdateMetadata.h"

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

#include <QPushButton>
#include <QTimer>


c2bUpdateMetadata::c2bUpdateMetadata(QWidget* parentw) : QDialog(parentw)
{
    ui.setupUi(this);
    setWindowTitle(tr("Documents Updating Log - cb2Bib"));
    ui.Log->setFont(c2bSettingsP->c2bMonoFont);
    ui.Log->setLineWrapMode(QPlainTextEdit::NoWrap);
    _buttonAbort = new QPushButton(tr("Abort"));
    ui.buttonBox->addButton(_buttonAbort, QDialogButtonBox::ActionRole);
    connect(_buttonAbort, SIGNAL(clicked()), this, SLOT(aborted()));
    connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
    resize(c2bSettingsP->value("c2bLogWidget/size", size()).toSize());
    _bpP = c2b::bibParser();
}

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


void c2bUpdateMetadata::update(const QString& fn, const QString& bibtex)
{
    ui.Log->clear();
    _fn = fn;
    _bibtex = bibtex;
    QTimer::singleShot(500, this, SLOT(update()));
    QDialog::exec();
}

void c2bUpdateMetadata::update()
{
    ui.Log->appendPlainText(tr("[cb2bib] Updating documents metadata...\n"));
    const QString exiftool_bin(c2bSettingsP->fileName("cb2Bib/ExifToolBin"));
    if (exiftool_bin.isEmpty())
    {
        ui.Log->appendPlainText(tr("[cb2bib] ExifTool location has not been specified."));
        return ;
    }
    const QFileInfo exiftool_bin_fi(exiftool_bin);
    if (exiftool_bin_fi.isAbsolute())
        if (!exiftool_bin_fi.exists())
        {
            ui.Log->appendPlainText(tr("[cb2bib] ExifTool file %1 does not exist.").arg(exiftool_bin));
            return;
        }
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(false);
    _buttonAbort->setEnabled(true);
    _buttonAbort->setFocus();
    _aborted = false;
    ui.Log->appendPlainText(tr("[cb2bib] Processing file %1...\n").arg(_fn));

    int doc_counter(0);
    int error_counter(0);
    int updated_counter(0);
    metadataParser* mp = c2b::metaParser();
    bibReference ref;
    _bpP->initReferenceParsing(_fn, _bpP->bibliographicFields(), &ref);
    while (_bpP->referencesIn(_bibtex, &ref) && !_aborted)
    {
        QCoreApplication::processEvents();
        const QString file(ref.value("file"));
        if (file.isEmpty())
        {
            ui.Log->appendPlainText(tr("[cb2bib] %1: No file in reference.").arg(ref.citeidName));
            continue;
        }
        QFileInfo fi(file);
        if (!fi.exists())
        {
            ui.Log->appendPlainText(tr("[cb2bib] %1: Warning: File %2 does not exist.").arg(ref.citeidName).arg(file));
            continue;
        }
        ++doc_counter;
        bibReference mref;
        if (mp->metadata(file, &mref))
            if (!needsUpdating(ref, mref))
                continue;
        if (!fi.isWritable())
        {
            ++error_counter;
            ui.Log->appendPlainText(tr("[cb2bib] %1: Error: File %2 is not writable.").arg(ref.citeidName).arg(file));
            continue;
        }
        QString error_str;
        if (mp->insertMetadata(ref, file, &error_str))
        {
            if (mp->metadata(file, &mref))
                if (!needsUpdating(ref, mref))
                {
                    ++updated_counter;
                    ui.Log->appendPlainText(tr("[cb2bib] %1: File %2 successfully updated.").arg(ref.citeidName).arg(file));
                    continue;
                }
            ++error_counter;
            ui.Log->appendPlainText(tr("[cb2bib] %1: Warning: File %2 was not properly updated.").arg(ref.citeidName).arg(file));
            writeDifferences(ref, mref);
        }
        else
        {
            ++error_counter;
            ui.Log->appendPlainText(tr("[cb2bib] %1: Error: File %2 could not be updated.").arg(ref.citeidName).arg(file));
            ui.Log->appendPlainText(tr("[exiftool] '%1'.").arg(error_str));
        }
        QCoreApplication::processEvents();
    }

    QApplication::restoreOverrideCursor();
    ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
    ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
    _buttonAbort->setEnabled(false);

    ui.Log->appendPlainText(tr("\n[cb2bib] Checked %1 documents.").arg(doc_counter));
    if (doc_counter > 0 && updated_counter == 0 && error_counter == 0)
        ui.Log->appendPlainText(tr("[cb2bib] Documents Metadata was up to date."));
    else
        ui.Log->appendPlainText(tr("[cb2bib] Updated %1 documents.").arg(updated_counter));
    if (error_counter > 0)
        ui.Log->appendPlainText(tr("[cb2bib] Found %1 errors.").arg(error_counter));
    c2b::showMessage(tr("Updated %1 documents.").arg(updated_counter));
}

bool c2bUpdateMetadata::needsUpdating(const bibReference& ref, const bibReference& mref)
{
    if (ref.typeName != mref.typeName)
        return true;
    const QStringList& bibliographicFields = _bpP->bibliographicFields();
    for (int i = 0; i < bibliographicFields.count(); ++i)
    {
        const QString key(bibliographicFields.at(i));
        if (key == "file")
            continue;
        else if (key == "id")
            continue;
        QString value(ref.value(key));
        c2bUtils::fullBibToC2b(value);
        if (key == "title" || key == "booktitle")
            c2bUtils::cleanTitle(value);
        if (value != mref.value(key))
            return true;
    }
    return false;
}

void c2bUpdateMetadata::writeDifferences(const bibReference& ref, const bibReference& mref)
{
    QString diff("  [Key] '%1'\n  [In Reference] '%2'\n  [In Document]  '%3'");
    if (ref.typeName != mref.typeName)
        ui.Log->appendPlainText(diff.arg("type").arg(ref.typeName).arg(mref.typeName));
    const QStringList& bibliographicFields = _bpP->bibliographicFields();
    for (int i = 0; i < bibliographicFields.count(); ++i)
    {
        const QString key(bibliographicFields.at(i));
        if (key == "file")
            continue;
        else if (key == "id")
            continue;
        QString value(ref.value(key));
        c2bUtils::fullBibToC2b(value);
        if (key == "title" || key == "booktitle")
            c2bUtils::cleanTitle(value);
        if (value != mref.value(key))
            ui.Log->appendPlainText(diff.arg(key).arg(value).arg(mref.value(key)));
    }
}

void c2bUpdateMetadata::aborted()
{
    _aborted = true;
    _buttonAbort->setEnabled(false);
    ui.Log->appendPlainText(tr("\n\n[cb2bib] Aborted.\n"));
}

void c2bUpdateMetadata::help()
{
    c2bUtils::displayHelp("http://www.molspaces.com/d_cb2bib-c2beditor.php#update_metadata");
}