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
***************************************************************************/
23
#include <kio/jobclasses.h>
25
#include <kio/netaccess.h>
26
#include <klineedit.h>
29
#include <kmessagebox.h>
32
#include <entryfield.h>
34
#include <fileimporterbibtex.h>
35
#include "webqueryieeexplore.h"
37
#define min(a,b) ((a)>(b)?(b):(a))
41
WebQueryIEEExploreWidget::WebQueryIEEExploreWidget( QWidget *parent, const char *name )
42
: WebQueryWidget( parent, name )
46
Settings *settings = Settings::self();
47
QString value = settings->getWebQueryDefault( "IEEE" );
48
value = value == QString::null ? "" : value;
49
lineEditQuery->setText( value );
50
slotTextChanged( value, true );
53
WebQueryIEEExplore::WebQueryIEEExplore( QWidget* parent ): WebQuery( parent ), m_numberOfMatches( "Your search matched <strong>(\\d+)</strong> of" ), m_findArNumber( "srchabstract.jsp\\?arnumber=(\\d+)" ), m_date( "(?:([A-Z][a-z]{2,3})(?:(?:/|-\\d+\\s+)([A-Z][a-z]{2,3}))?)?\\.?\\s*(\\d{4})$" ), m_risURL( "http://ieeexplore.ieee.org/xpls/citationAct" )
55
m_widget = new WebQueryIEEExploreWidget( parent );
56
m_bibtexImporter = new BibTeX::FileImporterBibTeX( false );
59
WebQueryIEEExplore::~WebQueryIEEExplore()
61
delete m_bibtexImporter;
65
QString WebQueryIEEExplore::title()
67
return i18n( "IEEExplore" );
70
QString WebQueryIEEExplore::disclaimer()
72
return i18n( "What is IEEE Xplore?" );
75
QString WebQueryIEEExplore::disclaimerURL()
77
return "http://ieeexplore.ieee.org/guide/g_oview_faq.jsp";
80
WebQueryWidget *WebQueryIEEExplore::widget()
85
void WebQueryIEEExplore::query()
88
Settings *settings = Settings::self();
89
settings->setWebQueryDefault( "IEEE", m_widget->lineEditQuery->text() );
92
m_numMaxHits = min( 50, m_widget->spinBoxMaxHits->value() );
93
setNumStages( m_numMaxHits + 1 );
94
QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
95
if ( searchTerm.isEmpty() )
97
setEndSearch( WebQuery::statusInvalidQuery );
101
QString category = "metadata";
102
KURL url = KURL( QString( "http://ieeexplore.ieee.org/search/freesearchresult.jsp?queryText=" ).append( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ).append( "+%3Cin%3E+" ).append( category ).append( "&ResultCount=" ).append( QString::number( m_numMaxHits ) ).append( "&ResultStart=0" ) );
104
QString completeText = downloadHTML( url );
105
if ( completeText != QString::null && !m_aborted )
107
if ( completeText.find( "You have entered an invalid search" ) > -1 )
109
KMessageBox::information( m_widget, i18n( "You have entered an invalid search." ), i18n( "Search Error" ) );
110
setEndSearch( WebQuery::statusInvalidQuery );
113
else if ( completeText.find( "No results" ) > -1 )
115
setEndSearch( WebQuery::statusSuccess );
119
m_numberOfMatches.search( completeText );
121
m_numMatches = m_numberOfMatches.cap( 1 ).toInt( &ok );
122
if ( !ok ) m_numMatches = 0;
124
if ( m_numMatches <= 0 )
126
setEndSearch( WebQuery::statusSuccess );
130
m_findArNumber.search( completeText );
131
for ( int i = 0; i < m_numMatches; ++i )
133
if ( m_findArNumber.cap( 1 ).isEmpty() ) break;
134
int arnum = m_findArNumber.cap( 1 ).toInt( &ok );
135
if ( !ok || arnum <= 0 ) break;
136
m_arnumList.append( arnum );
138
m_findArNumber.search( completeText, m_findArNumber.pos( 1 ) + 1 );
141
if ( !m_arnumList.isEmpty() )
148
setEndSearch( WebQuery::statusSuccess );
151
else if ( !m_aborted )
153
QString message = KIO::NetAccess::lastErrorString();
154
if ( message.isEmpty() )
155
message.prepend( '\n' );
156
message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
157
KMessageBox::error( m_parent, message );
158
setEndSearch( WebQuery::statusError );
162
setEndSearch( WebQuery::statusSuccess );
166
void WebQueryIEEExplore::fetchNext()
168
if ( m_arnumList.isEmpty() ) return;
170
int arnum = m_arnumList.first();
171
m_arnumList.remove( m_arnumList.begin() );
174
QString data = "dlSelect=cite&fileFormate=BibTex&arnumber=%3Carnumber%3E" + QString::number( arnum ) + "%3C%2Farnumber%3E&Submit=Download";
176
KIO::TransferJob *job = KIO::http_post( m_risURL, data.utf8(), false );
177
job->addMetaData( "content-type", "Content-Type: application/x-www-form-urlencoded" );
178
connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ), this, SLOT( slotData( KIO::Job *, const QByteArray & ) ) );
179
connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotResult( KIO::Job * ) ) );
182
void WebQueryIEEExplore::slotData( KIO::Job *, const QByteArray &data )
184
if ( data.size() > 0 )
185
m_incomingData.append( QCString( data, data.size() + 1 ) );
188
void WebQueryIEEExplore::slotResult( KIO::Job *job )
190
QRegExp m_date( "^(((\\d{1,2}(-\\d{1,2}))\\s+)?(([A-Z][a-z]{2,3})(/([A-Z][a-z]{2,3}))?)\\.?\\s+)?(\\d{4})$" );
193
job->showErrorDialog();
198
m_incomingData.replace( "<br>", "" );
200
BibTeX::File *bibtexFile = m_bibtexImporter->load( m_incomingData );
201
if ( bibtexFile != NULL )
203
for ( BibTeX::File::ElementList::iterator it = bibtexFile->begin(); it != bibtexFile->end(); ++it )
205
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
209
emit foundEntry( new BibTeX::Entry( entry ), false );
215
if ( !m_arnumList.isEmpty() )
219
setEndSearch( WebQuery::statusSuccess );
223
void WebQueryIEEExplore::fixDate( BibTeX::Entry *entry )
225
m_date.search( entry->getField( BibTeX::EntryField::ftYear )->value()->text() );
227
if ( !m_date.cap( 3 ).isNull() && !m_date.cap( 3 ).isEmpty() )
229
BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftYear );
232
field = new BibTeX::EntryField( BibTeX::EntryField::ftJournal );
233
entry->addField( field );
235
field->setValue( new BibTeX::Value( m_date.cap( 3 ), true ) );
238
QString newMonth = QString::null;
239
if ( !m_date.cap( 1 ).isNull() && !m_date.cap( 1 ).isEmpty() && ( newMonth = parseMonth( m_date.cap( 1 ) ) ) != QString::null )
241
BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftMonth );
244
field = new BibTeX::EntryField( BibTeX::EntryField::ftJournal );
245
entry->addField( field );
248
field->setValue( new BibTeX::Value( newMonth, true ) );
250
newMonth = QString::null;
251
if ( !m_date.cap( 2 ).isNull() && !m_date.cap( 2 ).isEmpty() && ( newMonth = parseMonth( m_date.cap( 2 ) ) ) != QString::null )
253
field->value()->items.append( new BibTeX::PlainText( "/" ) );
254
field->value()->items.append( new BibTeX::MacroKey( newMonth ) );
259
QString WebQueryIEEExplore::parseMonth( const QString &month )
261
for ( unsigned int i = 0; i < sizeof( BibTeX::MonthsTriple ) / sizeof( BibTeX::MonthsTriple[0] ); ++i )
262
if ( month.startsWith( BibTeX::MonthsTriple[i], false ) ) return BibTeX::MonthsTriple[i];
263
return QString::null;
267
#include "webqueryieeexplore.moc"