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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/***************************************************************************
copyright : (C) 2007 by Robby Stephenson
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "crossreffetcher.h"
#include "messagehandler.h"
#include "../translators/xslthandler.h"
#include "../translators/tellicoimporter.h"
#include "../tellico_kernel.h"
#include "../tellico_utils.h"
#include "../collection.h"
#include "../entry.h"
#include "../core/netaccess.h"
#include "../imagefactory.h"
#include "../tellico_debug.h"
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <klineedit.h>
#include <kactivelabel.h>
#include <qlabel.h>
#include <qwhatsthis.h>
#include <qlayout.h>
#include <qfile.h>
// #define CROSSREF_TEST
#define CROSSREF_USE_UNIXREF
namespace {
static const char* CROSSREF_BASE_URL = "http://www.crossref.org/openurl/?url_ver=Z39.88-2004&noredirect=true";
}
using Tellico::Fetch::CrossRefFetcher;
CrossRefFetcher::CrossRefFetcher(QObject* parent_)
: Fetcher(parent_), m_xsltHandler(0), m_job(0), m_started(false) {
}
CrossRefFetcher::~CrossRefFetcher() {
delete m_xsltHandler;
m_xsltHandler = 0;
}
QString CrossRefFetcher::defaultName() {
return QString::fromLatin1("CrossRef");
}
QString CrossRefFetcher::source() const {
return m_name.isEmpty() ? defaultName() : m_name;
}
bool CrossRefFetcher::canFetch(int type) const {
return type == Data::Collection::Bibtex;
}
void CrossRefFetcher::readConfigHook(const KConfigGroup& config_) {
QString s = config_.readEntry("User");
if(!s.isEmpty()) {
m_user = s;
}
s = config_.readEntry("Password");
if(!s.isEmpty()) {
m_password = s;
}
}
void CrossRefFetcher::search(FetchKey key_, const QString& value_) {
m_key = key_;
m_value = value_.stripWhiteSpace();
m_started = true;
if(m_user.isEmpty() || m_password.isEmpty()) {
message(i18n("%1 requires a username and password.").arg(source()), MessageHandler::Warning);
stop();
return;
}
if(!canFetch(Kernel::self()->collectionType())) {
message(i18n("%1 does not allow searching for this collection type.").arg(source()), MessageHandler::Warning);
stop();
return;
}
m_data.truncate(0);
// myDebug() << "CrossRefFetcher::search() - value = " << value_ << endl;
KURL u = searchURL(m_key, m_value);
if(u.isEmpty()) {
stop();
return;
}
m_job = KIO::get(u, false, false);
connect(m_job, SIGNAL(data(KIO::Job*, const QByteArray&)),
SLOT(slotData(KIO::Job*, const QByteArray&)));
connect(m_job, SIGNAL(result(KIO::Job*)),
SLOT(slotComplete(KIO::Job*)));
}
void CrossRefFetcher::stop() {
if(!m_started) {
return;
}
// myDebug() << "CrossRefFetcher::stop()" << endl;
if(m_job) {
m_job->kill();
m_job = 0;
}
m_data.truncate(0);
m_started = false;
emit signalDone(this);
}
void CrossRefFetcher::slotData(KIO::Job*, const QByteArray& data_) {
QDataStream stream(m_data, IO_WriteOnly | IO_Append);
stream.writeRawBytes(data_.data(), data_.size());
}
void CrossRefFetcher::slotComplete(KIO::Job* job_) {
// myDebug() << "CrossRefFetcher::slotComplete()" << endl;
// since the fetch is done, don't worry about holding the job pointer
m_job = 0;
if(job_->error()) {
job_->showErrorDialog(Kernel::self()->widget());
stop();
return;
}
if(m_data.isEmpty()) {
myDebug() << "CrossRefFetcher::slotComplete() - no data" << endl;
stop();
return;
}
#if 0
kdWarning() << "Remove debug from crossreffetcher.cpp" << endl;
QFile f(QString::fromLatin1("/tmp/test.xml"));
if(f.open(IO_WriteOnly)) {
QTextStream t(&f);
t.setEncoding(QTextStream::UnicodeUTF8);
t << QCString(m_data, m_data.size()+1);
}
f.close();
#endif
if(!m_xsltHandler) {
initXSLTHandler();
if(!m_xsltHandler) { // probably an error somewhere in the stylesheet loading
stop();
return;
}
}
// assume result is always utf-8
QString str = m_xsltHandler->applyStylesheet(QString::fromUtf8(m_data, m_data.size()));
Import::TellicoImporter imp(str);
Data::CollPtr coll = imp.collection();
if(!coll) {
myDebug() << "CrossRefFetcher::slotComplete() - no valid result" << endl;
stop();
return;
}
Data::EntryVec entries = coll->entries();
for(Data::EntryVec::Iterator entry = entries.begin(); entry != entries.end(); ++entry) {
if(!m_started) {
// might get aborted
break;
}
QString desc = entry->field(QString::fromLatin1("author"))
+ QChar('/') + entry->field(QString::fromLatin1("publisher"));
if(!entry->field(QString::fromLatin1("year")).isEmpty()) {
desc += QChar('/') + entry->field(QString::fromLatin1("year"));
}
SearchResult* r = new SearchResult(this, entry->title(), desc, entry->field(QString::fromLatin1("isbn")));
m_entries.insert(r->uid, Data::EntryPtr(entry));
emit signalResultFound(r);
}
stop(); // required
}
Tellico::Data::EntryPtr CrossRefFetcher::fetchEntry(uint uid_) {
Data::EntryPtr entry = m_entries[uid_];
// if URL but no cover image, fetch it
if(!entry->field(QString::fromLatin1("url")).isEmpty()) {
Data::CollPtr coll = entry->collection();
Data::FieldPtr field = coll->fieldByName(QString::fromLatin1("cover"));
if(!field && !coll->imageFields().isEmpty()) {
field = coll->imageFields().front();
} else if(!field) {
field = new Data::Field(QString::fromLatin1("cover"), i18n("Front Cover"), Data::Field::Image);
coll->addField(field);
}
if(entry->field(field).isEmpty()) {
QPixmap pix = NetAccess::filePreview(entry->field(QString::fromLatin1("url")));
if(!pix.isNull()) {
QString id = ImageFactory::addImage(pix, QString::fromLatin1("PNG"));
if(!id.isEmpty()) {
entry->setField(field, id);
}
}
}
}
return entry;
}
void CrossRefFetcher::initXSLTHandler() {
#ifdef CROSSREF_USE_UNIXREF
QString xsltfile = locate("appdata", QString::fromLatin1("unixref2tellico.xsl"));
#else
QString xsltfile = locate("appdata", QString::fromLatin1("crossref2tellico.xsl"));
#endif
if(xsltfile.isEmpty()) {
kdWarning() << "CrossRefFetcher::initXSLTHandler() - can not locate xslt file." << endl;
return;
}
KURL u;
u.setPath(xsltfile);
delete m_xsltHandler;
m_xsltHandler = new XSLTHandler(u);
if(!m_xsltHandler->isValid()) {
kdWarning() << "CrossRefFetcher::initXSLTHandler() - error in crossref2tellico.xsl." << endl;
delete m_xsltHandler;
m_xsltHandler = 0;
return;
}
}
KURL CrossRefFetcher::searchURL(FetchKey key_, const QString& value_) const {
KURL u(QString::fromLatin1(CROSSREF_BASE_URL));
#ifdef CROSSREF_USE_UNIXREF
u.addQueryItem(QString::fromLatin1("format"), QString::fromLatin1("unixref"));
#endif
u.addQueryItem(QString::fromLatin1("req_dat"), QString::fromLatin1("ourl_%1:%2").arg(m_user, m_password));
switch(key_) {
case DOI:
u.addQueryItem(QString::fromLatin1("rft_id"), QString::fromLatin1("info:doi/%1").arg(value_));
break;
default:
kdWarning() << "CrossRefFetcher::search() - key not recognized: " << m_key << endl;
return KURL();
}
#ifdef CROSSREF_TEST
u = KURL::fromPathOrURL(QString::fromLatin1("/home/robby/crossref.xml"));
#endif
myDebug() << "CrossRefFetcher::search() - url: " << u.url() << endl;
return u;
}
void CrossRefFetcher::updateEntry(Data::EntryPtr entry_) {
QString doi = entry_->field(QString::fromLatin1("doi"));
if(!doi.isEmpty()) {
search(Fetch::DOI, doi);
return;
}
#if 0
// optimistically try searching for title and rely on Collection::sameEntry() to figure things out
QString t = entry_->field(QString::fromLatin1("title"));
if(!t.isEmpty()) {
m_limit = 10; // raise limit so more possibility of match
search(Fetch::Title, t);
return;
}
#endif
myDebug() << "CrossRefFetcher::updateEntry() - insufficient info to search" << endl;
emit signalDone(this); // always need to emit this if not continuing with the search
}
void CrossRefFetcher::updateEntrySynchronous(Data::EntryPtr entry) {
if(!entry) {
return;
}
if(m_user.isEmpty() || m_password.isEmpty()) {
myDebug() << "CrossRefFetcher::updateEntrySynchronous() - username and password is required" << endl;
return;
}
QString doi = entry->field(QString::fromLatin1("doi"));
if(doi.isEmpty()) {
return;
}
KURL u = searchURL(DOI, doi);
QString xml = FileHandler::readTextFile(u, true, true);
if(xml.isEmpty()) {
return;
}
if(!m_xsltHandler) {
initXSLTHandler();
if(!m_xsltHandler) { // probably an error somewhere in the stylesheet loading
return;
}
}
// assume result is always utf-8
QString str = m_xsltHandler->applyStylesheet(xml);
Import::TellicoImporter imp(str);
Data::CollPtr coll = imp.collection();
if(coll && coll->entryCount() > 0) {
myLog() << "CrossRefFetcher::updateEntrySynchronous() - found DOI result, merging" << endl;
Data::Collection::mergeEntry(entry, coll->entries().front(), false /*overwrite*/);
}
}
Tellico::Fetch::ConfigWidget* CrossRefFetcher::configWidget(QWidget* parent_) const {
return new CrossRefFetcher::ConfigWidget(parent_, this);
}
CrossRefFetcher::ConfigWidget::ConfigWidget(QWidget* parent_, const CrossRefFetcher* fetcher_)
: Fetch::ConfigWidget(parent_) {
QGridLayout* l = new QGridLayout(optionsWidget(), 4, 2);
l->setSpacing(4);
l->setColStretch(1, 10);
int row = 0;
KActiveLabel* al = new KActiveLabel(i18n("CrossRef requires an account for access. "
"Please read the terms and conditions and "
"<a href='http://www.crossref.org/requestaccount/'>"
"request an account</a>. Enter your OpenURL "
"account information below."),
optionsWidget());
++row;
l->addMultiCellWidget(al, row, row, 0, 1);
// richtext gets weird with size
al->setMinimumWidth(al->sizeHint().width());
QLabel* label = new QLabel(i18n("&Username: "), optionsWidget());
l->addWidget(label, ++row, 0);
m_userEdit = new KLineEdit(optionsWidget());
connect(m_userEdit, SIGNAL(textChanged(const QString&)), SLOT(slotSetModified()));
l->addWidget(m_userEdit, row, 1);
QString w = i18n("A username and password is required to access the CrossRef service. The password is "
"stored as plain text in the Tellico configuration file.");
QWhatsThis::add(label, w);
QWhatsThis::add(m_userEdit, w);
label->setBuddy(m_userEdit);
label = new QLabel(i18n("&Password: "), optionsWidget());
l->addWidget(label, ++row, 0);
m_passEdit = new KLineEdit(optionsWidget());
connect(m_passEdit, SIGNAL(textChanged(const QString&)), SLOT(slotSetModified()));
l->addWidget(m_passEdit, row, 1);
QWhatsThis::add(label, w);
QWhatsThis::add(m_passEdit, w);
label->setBuddy(m_passEdit);
if(fetcher_) {
m_userEdit->setText(fetcher_->m_user);
m_passEdit->setText(fetcher_->m_password);
}
}
void CrossRefFetcher::ConfigWidget::saveConfig(KConfigGroup& config_) {
QString s = m_userEdit->text();
config_.writeEntry("User", s);
s = m_passEdit->text();
config_.writeEntry("Password", s);
slotSetModified(false);
}
QString CrossRefFetcher::ConfigWidget::preferredName() const {
return CrossRefFetcher::defaultName();
}
#include "crossreffetcher.moc"
|