"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/c2bUpdateMetadata.cpp" (12 Feb 2021, 7166 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 "c2bUpdateMetadata.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 "c2bUpdateMetadata.h"
8
9 #include "c2b.h"
10 #include "c2bSettings.h"
11 #include "c2bUtils.h"
12
13 #include <QPushButton>
14 #include <QTimer>
15
16
17 c2bUpdateMetadata::c2bUpdateMetadata(QWidget* parentw) : QDialog(parentw)
18 {
19 ui.setupUi(this);
20 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
21 setWindowTitle(tr("Documents Updating Log - cb2Bib"));
22 ui.Log->setFont(c2bSettingsP->c2bMonoFont);
23 ui.Log->setLineWrapMode(QPlainTextEdit::NoWrap);
24 _buttonAbort = new QPushButton(tr("Abort"));
25 ui.buttonBox->addButton(_buttonAbort, QDialogButtonBox::ActionRole);
26 connect(_buttonAbort, SIGNAL(clicked()), this, SLOT(aborted()));
27 connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
28 resize(c2bSettingsP->value("c2bLogWidget/size", size()).toSize());
29 _bpP = c2b::bibParser();
30 }
31
32 c2bUpdateMetadata::~c2bUpdateMetadata()
33 {
34 c2bSettingsP->setValue("c2bLogWidget/size", size());
35 }
36
37
38 void c2bUpdateMetadata::update(const QString& fn, const QString& bibtex)
39 {
40 ui.Log->clear();
41 _fn = fn;
42 _bibtex = bibtex;
43 QTimer::singleShot(500, this, SLOT(update()));
44 QDialog::exec();
45 }
46
47 void c2bUpdateMetadata::update()
48 {
49 ui.Log->appendPlainText(tr("[cb2bib] Updating documents metadata...\n"));
50 const QString exiftool_bin(c2bSettingsP->fileName("cb2Bib/ExifToolBin"));
51 if (exiftool_bin.isEmpty())
52 {
53 ui.Log->appendPlainText(tr("[cb2bib] ExifTool location has not been specified."));
54 return;
55 }
56 const QFileInfo exiftool_bin_fi(exiftool_bin);
57 if (exiftool_bin_fi.isAbsolute())
58 if (!exiftool_bin_fi.exists())
59 {
60 ui.Log->appendPlainText(tr("[cb2bib] ExifTool file %1 does not exist.").arg(exiftool_bin));
61 return;
62 }
63 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
64 ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(false);
65 _buttonAbort->setEnabled(true);
66 _buttonAbort->setFocus();
67 _aborted = false;
68 ui.Log->appendPlainText(tr("[cb2bib] Processing file %1...\n").arg(_fn));
69
70 int doc_counter(0);
71 int error_counter(0);
72 int updated_counter(0);
73 metadataParser* mp = c2b::metaParser();
74 bibReference ref;
75 _bpP->initReferenceParsing(_fn, _bpP->bibliographicFields(), &ref);
76 while (_bpP->referencesIn(_bibtex, &ref) && !_aborted)
77 {
78 QCoreApplication::processEvents();
79 const QString file(ref.value("file"));
80 if (file.isEmpty())
81 {
82 ui.Log->appendPlainText(tr("[cb2bib] %1: No file in reference.").arg(ref.citeidName));
83 continue;
84 }
85 QFileInfo fi(file);
86 if (!fi.exists())
87 {
88 ui.Log->appendPlainText(tr("[cb2bib] %1: Warning: File %2 does not exist.").arg(ref.citeidName, file));
89 continue;
90 }
91 ++doc_counter;
92 bibReference mref;
93 const bool has_reference(mp->metadata(file, &mref));
94 if (has_reference)
95 if (!needsUpdating(ref, mref))
96 continue;
97 if (!fi.isWritable())
98 {
99 ++error_counter;
100 ui.Log->appendPlainText(tr("[cb2bib] %1: Error: File %2 is not writable.").arg(ref.citeidName, file));
101 continue;
102 }
103 QString error_str;
104 if (mp->insertMetadata(ref, file, &error_str, has_reference))
105 {
106 if (mp->metadata(file, &mref))
107 if (!needsUpdating(ref, mref))
108 {
109 ++updated_counter;
110 ui.Log->appendPlainText(tr("[cb2bib] %1: File %2 successfully updated.").arg(ref.citeidName, file));
111 continue;
112 }
113 ++error_counter;
114 ui.Log->appendPlainText(
115 tr("[cb2bib] %1: Warning: File %2 was not properly updated.").arg(ref.citeidName, file));
116 writeDifferences(ref, mref);
117 }
118 else
119 {
120 ++error_counter;
121 ui.Log->appendPlainText(tr("[cb2bib] %1: Error: File %2 could not be updated.").arg(ref.citeidName, file));
122 ui.Log->appendPlainText(tr("[exiftool] '%1'.").arg(error_str));
123 }
124 QCoreApplication::processEvents();
125 }
126
127 QApplication::restoreOverrideCursor();
128 ui.buttonBox->button(QDialogButtonBox::Close)->setEnabled(true);
129 ui.buttonBox->button(QDialogButtonBox::Close)->setFocus();
130 _buttonAbort->setEnabled(false);
131
132 ui.Log->appendPlainText(tr("\n[cb2bib] Checked %1 documents.").arg(doc_counter));
133 if (doc_counter > 0 && updated_counter == 0 && error_counter == 0)
134 ui.Log->appendPlainText(tr("[cb2bib] Documents Metadata was up to date."));
135 else
136 ui.Log->appendPlainText(tr("[cb2bib] Updated %1 documents.").arg(updated_counter));
137 if (error_counter > 0)
138 ui.Log->appendPlainText(tr("[cb2bib] Found %1 errors.").arg(error_counter));
139 c2b::showMessage(tr("Updated %1 documents.").arg(updated_counter));
140 }
141
142 bool c2bUpdateMetadata::needsUpdating(const bibReference& ref, const bibReference& mref)
143 {
144 if (ref.typeName != mref.typeName)
145 return true;
146 const QStringList& bibliographicFields = _bpP->bibliographicFields();
147 for (int i = 0; i < bibliographicFields.count(); ++i)
148 {
149 const QString key(bibliographicFields.at(i));
150 if (key == "file")
151 continue;
152 else if (key == "id")
153 continue;
154 QString value(ref.value(key));
155 c2bUtils::fullBibToC2b(value);
156 if (key == "title" || key == "booktitle")
157 c2bUtils::cleanTitle(value);
158 if (value != mref.value(key))
159 return true;
160 }
161 return false;
162 }
163
164 void c2bUpdateMetadata::writeDifferences(const bibReference& ref, const bibReference& mref)
165 {
166 const QString diff(" [Key] '%1'\n [In Reference] '%2'\n [In Document] '%3'");
167 if (ref.typeName != mref.typeName)
168 ui.Log->appendPlainText(diff.arg("type", ref.typeName, mref.typeName));
169 const QStringList& bibliographicFields = _bpP->bibliographicFields();
170 for (int i = 0; i < bibliographicFields.count(); ++i)
171 {
172 const QString key(bibliographicFields.at(i));
173 if (key == "file")
174 continue;
175 else if (key == "id")
176 continue;
177 QString value(ref.value(key));
178 c2bUtils::fullBibToC2b(value);
179 if (key == "title" || key == "booktitle")
180 c2bUtils::cleanTitle(value);
181 if (value != mref.value(key))
182 ui.Log->appendPlainText(diff.arg(key, value, mref.value(key)));
183 }
184 }
185
186 void c2bUpdateMetadata::aborted()
187 {
188 _aborted = true;
189 _buttonAbort->setEnabled(false);
190 ui.Log->appendPlainText(tr("\n\n[cb2bib] Aborted.\n"));
191 }
192
193 void c2bUpdateMetadata::help()
194 {
195 c2bUtils::displayHelp("https://www.molspaces.com/cb2bib/doc/bibeditor/#update-documents-metadata");
196 }