1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
21
#include <qapplication.h>
24
#include <qcheckbox.h>
26
#include <kiconloader.h>
27
#include <kcombobox.h>
28
#include <kpushbutton.h>
30
#include <kmessagebox.h>
31
#include <klineedit.h>
32
#include <kio/netaccess.h>
36
#include <fileimporterbibtex.h>
37
#include "webqueryspireshep.h"
42
const QString WebQuerySpiresHep::mirrorNames[] = {i18n( "DESY (Germany)" ), i18n( "FNAL (U.S.A.)" ), i18n( "IHEP (Russia)" ), i18n( "Durham U (U.K.)" ), i18n( "SLAC (U.S.A.)" ), i18n( "YITP (Japan)" ), i18n( "LIPI (Indonesia)" )};
43
const QString WebQuerySpiresHep::mirrorURLs[] = {"http://www-library.desy.de/cgi-bin/spiface", "http://www-spires.fnal.gov/spires", "http://usparc.ihep.su/spires", "http://www-spires.dur.ac.uk/spires", "http://www.slac.stanford.edu/spires", "http://www.yukawa.kyoto-u.ac.jp/spires", "http://www.spires.lipi.go.id/spires"};
45
WebQuerySpiresHepWidget::WebQuerySpiresHepWidget( QWidget *parent, const char *name )
46
: WebQueryWidget( parent, name )
50
Settings *settings = Settings::self();
51
QString value = settings->getWebQueryDefault( "SpiresHep_query" );
52
value = value == QString::null ? "" : value;
53
lineEditQuery->setText( value );
54
slotTextChanged( value, true );
55
value = settings->getWebQueryDefault( "SpiresHep_mirror" );
56
value = value == QString::null || value.isEmpty() ? "0" : value;
57
comboBoxMirror->setCurrentItem( value.toInt() );
58
value = settings->getWebQueryDefault( "SpiresHep_type" );
59
value = value == QString::null || value.isEmpty() ? "0" : value;
60
comboBoxType->setCurrentItem( value.toInt() );
63
void WebQuerySpiresHepWidget::init()
65
QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
67
QHBoxLayout *hLayout = new QHBoxLayout( );
68
vLayout->addLayout( hLayout );
70
comboBoxMirror = new KComboBox( false, this );
71
hLayout->addWidget( comboBoxMirror );
72
hLayout->setStretchFactor( comboBoxMirror, 7 );
73
hLayout->addSpacing( KDialog::spacingHint() * 2 );
74
for ( unsigned int i = 0; i < sizeof( WebQuerySpiresHep::mirrorNames ) / sizeof( WebQuerySpiresHep::mirrorNames[0] ); ++i )
75
comboBoxMirror->insertItem( WebQuerySpiresHep::mirrorNames[i] );
76
comboBoxMirror->setCurrentItem( 0 );
78
QLabel *label = new QLabel( i18n( "Type:" ), this );
79
hLayout->addWidget( label );
80
hLayout->setStretchFactor( label, 1 );
81
comboBoxType = new KComboBox( false, this );
82
label->setBuddy( comboBoxType );
83
hLayout->addWidget( comboBoxType );
84
hLayout->setStretchFactor( comboBoxType, 5 );
85
comboBoxType->insertItem( i18n( "raw query" ) );
86
comboBoxType->insertItem( i18n( "author" ) );
87
comboBoxType->insertItem( i18n( "title" ) );
88
comboBoxType->insertItem( i18n( "journal" ) );
89
comboBoxType->insertItem( i18n( "EPrint number" ) );
90
comboBoxType->insertItem( i18n( "report number" ) );
91
comboBoxType->insertItem( i18n( "keywords" ) );
92
comboBoxType->setCurrentItem( 0 );
94
hLayout = new QHBoxLayout( );
95
vLayout->addLayout( hLayout );
96
KPushButton *clearSearchText = new KPushButton( this );
97
clearSearchText->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
98
hLayout->addWidget( clearSearchText );
99
label = new QLabel( i18n( "Search &term:" ), this );
100
hLayout->addWidget( label );
101
lineEditQuery = new KLineEdit( this );
102
hLayout->addWidget( lineEditQuery );
103
label->setBuddy( lineEditQuery );
104
connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery, SLOT( clear() ) );
105
connect( lineEditQuery, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChanged( const QString& ) ) );
106
hLayout->setStretchFactor( lineEditQuery, 4 );
107
KCompletion *completionQuery = lineEditQuery->completionObject();
109
m_checkBoxFetchAbstracts = new QCheckBox( i18n( "Include abstracts from arxiv.org if available" ), this );
110
vLayout->addWidget( m_checkBoxFetchAbstracts );
112
connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
113
connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
115
vLayout->addStretch( 1 );
118
WebQuerySpiresHep::WebQuerySpiresHep( QWidget* parent ): WebQuery( parent )
120
m_widget = new WebQuerySpiresHepWidget( parent );
123
WebQuerySpiresHep::~WebQuerySpiresHep()
128
QString WebQuerySpiresHep::title()
130
return i18n( "SPIRES-HEP" );
133
QString WebQuerySpiresHep::disclaimer()
135
return i18n( "SPIRES-HEP Search" );
138
QString WebQuerySpiresHep::disclaimerURL()
140
return "http://www.slac.stanford.edu/spires/";
143
WebQueryWidget *WebQuerySpiresHep::widget()
148
void WebQuerySpiresHep::query()
150
const int maxEntries = 25;
151
bool doFetchAbstracts = m_widget->m_checkBoxFetchAbstracts->isChecked();
152
int mirrorIdx = m_widget->comboBoxMirror->currentItem();
155
Settings *settings = Settings::self();
156
settings->setWebQueryDefault( "SpiresHep_query", m_widget->lineEditQuery->text() );
157
settings->setWebQueryDefault( "SpiresHep_mirror", QString::number( m_widget->comboBoxMirror->currentItem() ) );
158
settings->setWebQueryDefault( "SpiresHep_type", QString::number( m_widget->comboBoxType->currentItem() ) );
160
setNumStages( 1 + doFetchAbstracts ? maxEntries : 0 );
162
QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
163
if ( searchTerm.isEmpty() )
165
setEndSearch( WebQuery::statusInvalidQuery );
169
switch ( m_widget->comboBoxType->currentItem() )
171
case 1: searchTerm = searchTerm.prepend( "fin a " ); break;
172
case 2: searchTerm = searchTerm.prepend( "fin t " ); break;
173
case 3: searchTerm = searchTerm.prepend( "fin j " ); break;
174
case 4: searchTerm = searchTerm.prepend( "fin eprint " ); break;
175
case 5: searchTerm = searchTerm.prepend( "fin r" ); break;
176
case 6: searchTerm = searchTerm.prepend( "fin k" ); break;
180
KURL url = KURL( QString( "%1/find/hep/www?rawcmd=%2&FORMAT=WWWBRIEFBIBTEX&SEQUENCE=" ).arg( mirrorURLs[mirrorIdx] ).arg( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ) );
181
qDebug( "url=%s", url.prettyURL().latin1() );
183
BibTeX::File *tmpBibFile = downloadBibTeXFile( url );
184
if ( tmpBibFile != NULL && !m_aborted )
187
for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); count < maxEntries && it != tmpBibFile->end(); ++it, ++count )
189
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
190
if ( doFetchAbstracts )
192
fetchArxivAbstract( entry );
196
emit foundEntry( new BibTeX::Entry( entry ), false );
199
setEndSearch( WebQuery::statusSuccess );
201
else if ( !m_aborted )
203
QString message = KIO::NetAccess::lastErrorString();
204
if ( message.isEmpty() )
205
message.prepend( '\n' );
206
message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
207
KMessageBox::error( m_parent, message );
208
setEndSearch( WebQuery::statusError );
211
setEndSearch( WebQuery::statusAborted );
213
if ( tmpBibFile != NULL )
217
void WebQuerySpiresHep::fetchArxivAbstract( BibTeX::Entry *entry )
219
BibTeX::EntryField *field = NULL;
220
BibTeX::Value *value = NULL;
221
QString eprint = (( field = entry->getField( "eprint" ) ) ) && (( value = field->value() ) ) ? value->text() : QString::null;
222
QString archivePrefix = (( field = entry->getField( "archivePrefix" ) ) ) && (( value = field->value() ) ) ? value->text() : QString::null;
224
if ( archivePrefix == "arXiv" && !eprint.isNull() )
226
KURL absURL( QString( "http://arxiv.org/abs/" ).append( eprint ) );
227
QString abstract = download( absURL );
228
int p = abstract.find( "Abstract:</span>" );
231
int p2 = abstract.find( "</", p + 14 );
232
abstract = abstract.mid( p + 16, p2 - p - 16 ).simplifyWhiteSpace();
233
BibTeX::EntryField * field = new BibTeX::EntryField( BibTeX::EntryField::ftAbstract );
234
entry->addField( field );
235
field->setValue( new BibTeX::Value( abstract ) );
237
field = new BibTeX::EntryField( "pdf" );
238
entry->addField( field );
239
field->setValue( new BibTeX::Value( QString( "http://arxiv.org/pdf/" ).append( eprint ) ) );
241
field = new BibTeX::EntryField( BibTeX::EntryField::ftURL );
242
entry->addField( field );
243
field->setValue( new BibTeX::Value( QString( "http://arxiv.org/abs/" ).append( eprint ) ) );