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
***************************************************************************/
26
#include <kpushbutton.h>
28
#include <kcombobox.h>
29
#include <kiconloader.h>
30
#include <kio/jobclasses.h>
32
#include <kio/netaccess.h>
35
#include <fileimporterris.h>
36
#include "webquerysciencedirect.h"
40
WebQueryScienceDirectWidget::WebQueryScienceDirectWidget( QWidget *parent, const char *name )
41
: WebQueryWidget( parent, name )
46
Settings *settings = Settings::self();
47
QString value = settings->getWebQueryDefault( "ScienceDirect_title" );
48
value = value == QString::null ? "" : value;
49
lineEditQuery->setText( value );
51
value = settings->getWebQueryDefault( "ScienceDirect_author" );
52
value = value == QString::null ? "" : value;
53
lineEditAuthor->setText( value );
55
value = settings->getWebQueryDefault( "ScienceDirect_journal" );
56
value = value == QString::null ? "" : value;
57
lineEditJournal->setText( value );
59
value = settings->getWebQueryDefault( "ScienceDirect_volume" );
60
value = value == QString::null ? "" : value;
61
lineEditVolume->setText( value );
63
value = settings->getWebQueryDefault( "ScienceDirect_issue" );
64
value = value == QString::null ? "" : value;
65
lineEditIssue->setText( value );
67
value = settings->getWebQueryDefault( "ScienceDirect_page" );
68
value = value == QString::null ? "" : value;
69
lineEditPage->setText( value );
72
slotTextChanged( allValues, true );
75
void WebQueryScienceDirectWidget::init()
77
QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
79
QHBoxLayout *hLayout = new QHBoxLayout( );
80
vLayout->addLayout( hLayout );
81
KPushButton *clearSearchText = new KPushButton( this );
82
clearSearchText->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
83
hLayout->addWidget( clearSearchText );
84
QLabel *label = new QLabel( i18n( "Title, abstract, keywords:" ), this );
85
hLayout->addWidget( label );
86
lineEditQuery = new KLineEdit( this );
87
KCompletion *completionQuery = lineEditQuery->completionObject();
88
hLayout->addWidget( lineEditQuery );
89
label->setBuddy( lineEditQuery );
90
connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery, SLOT( clear() ) );
91
connect( lineEditQuery, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChangedSD( ) ) );
92
hLayout->setStretchFactor( lineEditQuery, 7 );
93
connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
94
connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
97
hLayout = new QHBoxLayout( );
98
vLayout->addLayout( hLayout );
100
KPushButton *clearAuthor = new KPushButton( this );
101
clearAuthor->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
102
hLayout->addWidget( clearAuthor );
103
label = new QLabel( i18n( "Author:" ), this );
104
hLayout->addWidget( label );
105
lineEditAuthor = new KLineEdit( this );
106
completionQuery = lineEditAuthor->completionObject();
107
hLayout->addWidget( lineEditAuthor );
108
label->setBuddy( lineEditAuthor );
109
connect( clearAuthor, SIGNAL( clicked() ), lineEditAuthor, SLOT( clear() ) );
110
connect( lineEditAuthor, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChangedSD( ) ) );
111
hLayout->setStretchFactor( lineEditAuthor, 7 );
112
connect( lineEditAuthor, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
113
connect( lineEditAuthor, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
115
hLayout->addSpacing( KDialog::spacingHint() * 2 );
117
KPushButton *clearJournal = new KPushButton( this );
118
clearJournal->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
119
hLayout->addWidget( clearJournal );
120
label = new QLabel( i18n( "Journal/book title:" ), this );
121
hLayout->addWidget( label );
122
lineEditJournal = new KLineEdit( this );
123
completionQuery = lineEditJournal->completionObject();
124
hLayout->addWidget( lineEditJournal );
125
label->setBuddy( lineEditJournal );
126
connect( clearJournal, SIGNAL( clicked() ), lineEditJournal, SLOT( clear() ) );
127
connect( lineEditJournal, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChangedSD( ) ) );
128
hLayout->setStretchFactor( lineEditJournal, 4 );
129
connect( lineEditJournal, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
130
connect( lineEditJournal, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
132
hLayout = new QHBoxLayout( );
133
vLayout->addLayout( hLayout );
135
label = new QLabel( i18n( "Volume:" ), this );
136
hLayout->addWidget( label );
137
lineEditVolume = new KLineEdit( this );
138
hLayout->addWidget( lineEditVolume );
139
hLayout->setStretchFactor( lineEditVolume, 1 );
140
label->setBuddy( lineEditVolume );
141
connect( lineEditVolume, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
143
hLayout->addSpacing( KDialog::spacingHint() * 2 );
145
label = new QLabel( i18n( "Issue:" ), this );
146
hLayout->addWidget( label );
147
lineEditIssue = new KLineEdit( this );
148
hLayout->addWidget( lineEditIssue );
149
label->setBuddy( lineEditIssue );
150
hLayout->setStretchFactor( lineEditIssue, 1 );
151
connect( lineEditIssue, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
153
hLayout->addSpacing( KDialog::spacingHint() * 2 );
155
label = new QLabel( i18n( "Page:" ), this );
156
hLayout->addWidget( label );
157
lineEditPage = new KLineEdit( this );
158
hLayout->addWidget( lineEditPage );
159
hLayout->setStretchFactor( lineEditPage, 1 );
160
label->setBuddy( lineEditPage );
161
connect( lineEditPage, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
163
hLayout->addSpacing( KDialog::spacingHint() * 2 );
165
label = new QLabel( i18n( "&Number of results:" ), this );
166
hLayout->addWidget( label );
167
spinBoxMaxHits = new QSpinBox( 1, 100, 1, this );
168
spinBoxMaxHits->setValue( 10 );
169
hLayout->addWidget( spinBoxMaxHits );
170
hLayout->setStretchFactor( spinBoxMaxHits, 1 );
171
label->setBuddy( spinBoxMaxHits );
172
hLayout->addStretch( 5 );
175
void WebQueryScienceDirectWidget::slotTextChangedSD()
177
QString text = lineEditQuery->text().stripWhiteSpace().append( lineEditAuthor->text().stripWhiteSpace() ).append( lineEditJournal->text().stripWhiteSpace() ).replace( '$', "" );
178
emit enableSearch( !text.isEmpty() );
181
WebQueryScienceDirect::WebQueryScienceDirect( QWidget* parent ): WebQuery( parent )
183
m_importer = new BibTeX::FileImporterBibTeX( FALSE );
184
m_importer->setIgnoreComments( TRUE );
185
m_widget = new WebQueryScienceDirectWidget( parent );
188
WebQueryScienceDirect::~WebQueryScienceDirect()
194
QString WebQueryScienceDirect::title()
196
return i18n( "ScienceDirect" );
199
QString WebQueryScienceDirect::disclaimer()
201
return i18n( "About ScienceDirect" );
204
QString WebQueryScienceDirect::disclaimerURL()
206
return "http://info.sciencedirect.com/";
209
WebQueryWidget *WebQueryScienceDirect::widget()
214
void WebQueryScienceDirect::query()
218
Settings *settings = Settings::self();
219
settings->setWebQueryDefault( "ScienceDirect_author", m_widget->lineEditAuthor->text() );
220
settings->setWebQueryDefault( "ScienceDirect_journal", m_widget->lineEditJournal->text() );
221
settings->setWebQueryDefault( "ScienceDirect_volume", m_widget->lineEditVolume->text() );
222
settings->setWebQueryDefault( "ScienceDirect_issue", m_widget->lineEditIssue->text() );
223
settings->setWebQueryDefault( "ScienceDirect_page", m_widget->lineEditPage->text() );
225
QString tak = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
226
QString author = m_widget->lineEditAuthor->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
227
QString journal = m_widget->lineEditJournal->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
228
QString volume = m_widget->lineEditVolume->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
229
QString issue = m_widget->lineEditIssue->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
230
QString page = m_widget->lineEditPage->text().stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
232
if ( tak.isEmpty() && author.isEmpty() && journal.isEmpty() )
234
setEndSearch( WebQuery::statusInvalidQuery );
240
if ( !getStartPage() ) return;
242
if ( !getResultPage( tak, author, journal, volume, issue, page ) ) return;
244
if ( !getArticleListPage() ) return;
246
if ( !getRISFile() ) return;
249
bool WebQueryScienceDirect::getStartPage()
251
QString startPage = downloadHTML( KURL( "http://www.sciencedirect.com/" ) );
254
setEndSearch( WebQuery::statusAborted );
257
else if ( startPage == QString::null )
259
setEndSearch( WebQuery::statusError );
263
int pos = startPage.find( "<input type=\"hidden\" name=\"_acct\" value=\"" );
266
setEndSearch( WebQuery::statusError );
269
m_account = startPage.mid( pos + 41, startPage.find( "\"", pos + 43 ) - pos - 41 );
270
pos = startPage.find( "<input type=\"hidden\" name=\"md5\" value=\"" );
273
setEndSearch( WebQuery::statusError );
276
m_md5 = startPage.mid( pos + 39, startPage.find( "\"", pos + 41 ) - pos - 39 );
281
bool WebQueryScienceDirect::getResultPage( const QString &tak, const QString &author, const QString &journal, const QString &volume, const QString &issue, const QString &page )
283
KURL url = KURL( QString( "http://www.sciencedirect.com/science?_ob=QuickSearchURL&_method=submitForm&qs_tak=" ).append( tak ).append( "&qs_author=" ).append( author ).append( "&qs_title=" ).append( journal ).append( "&qs_vol=" ).append( volume ).append( "&qs_issue=" ).append( issue ).append( "&qs_pages=" ).append( page ).append( "&_acct=" ).append( m_account ).append( "&md5=" ).append( m_md5 ).append( "&x=0&y=0&=Submit" ) );
284
QString resultPage = downloadHTML( url );
287
setEndSearch( WebQuery::statusAborted );
290
else if ( resultPage == QString::null )
292
setEndSearch( WebQuery::statusError );
296
int pos = resultPage.find( "<input type=\"hidden\" name=\"_ArticleListID\" value=" );
299
setEndSearch( WebQuery::statusError );
302
m_articleListID = resultPage.mid( pos + 49, resultPage.find( ">", pos + 50 ) - pos - 49 );
303
pos = resultPage.find( "<input type=\"hidden\" name=\"md5\" value=\"" );
304
pos = resultPage.find( "<input type=\"hidden\" name=\"md5\" value=\"" , pos + 2 );
307
setEndSearch( WebQuery::statusError );
310
m_md5 = resultPage.mid( pos + 39, 32 );
311
pos = resultPage.find( "_userid=" );
314
setEndSearch( WebQuery::statusError );
317
m_userid = resultPage.mid( pos + 8, resultPage.find( "&", pos + 9 ) - pos - 8 );
322
bool WebQueryScienceDirect::getArticleListPage()
324
KURL url = KURL( QString( "http://www.sciencedirect.com/science?_ob=ArticleListURL&_method=tag&refSource=search&_st=13&_chunk=0&NEXT_LIST=1&view=c&md5=%1&_ArticleListID=%2&sisr_search=&sisrterm=&export=Export+Citations&count=%3" ).arg( m_md5 ).arg( m_articleListID ).arg( m_widget->spinBoxMaxHits->value() ) );
325
QString articleListPage = downloadHTML( url );
328
setEndSearch( WebQuery::statusAborted );
331
else if ( articleListPage == QString::null )
333
setEndSearch( WebQuery::statusError );
336
else if ( articleListPage.find( "subscription does not entitle" ) >= 0 )
338
qWarning( "Your subscription does not entitle you to access the download feature of ScienceDirect" );
339
setEndSearch( WebQuery::statusInsufficientPermissions );
343
int pos = articleListPage.find( "<input type=hidden name=md5 value=" );
346
setEndSearch( WebQuery::statusError );
349
m_md5 = articleListPage.mid( pos + 34, articleListPage.find( ">", pos + 36 ) - pos - 34 );
350
pos = articleListPage.find( "<input type=hidden name=_ArticleListID value=" );
353
setEndSearch( WebQuery::statusError );
356
m_articleListID = articleListPage.mid( pos + 45, articleListPage.find( ">", pos + 46 ) - pos - 45 );
361
bool WebQueryScienceDirect::getRISFile()
364
QString data = QString( "_ob=DownloadURL&_method=finish&_acct=%1&_userid=%2&_ArticleListID=%3&count=10&md5=%4&JAVASCRIPT_ON=&format=cite&citation-type=RIS&RETURN_URL=http://www.sciencedirect.com/science/home" ).arg( m_account ).arg( m_userid ).arg( m_articleListID ).arg( m_md5 );
365
KURL url = KURL( "http://www.sciencedirect.com/science" );
366
KIO::TransferJob *job = KIO::http_post( url, data.utf8(), false );
367
job->addMetaData( "content-type", "Content-Type: application/x-www-form-urlencoded" );
368
connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ), this, SLOT( slotData( KIO::Job *, const QByteArray & ) ) );
369
connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotResult( KIO::Job * ) ) );
374
void WebQueryScienceDirect::slotData( KIO::Job *, const QByteArray &data )
376
if ( data.size() > 0 )
377
m_incomingData.append( QCString( data, data.size() + 1 ) );
380
void WebQueryScienceDirect::slotResult( KIO::Job *job )
384
setEndSearch( WebQuery::statusError );
388
BibTeX::FileImporterRIS importer;
391
buffer.open( IO_WriteOnly );
392
QTextStream ts( &buffer );
393
ts.setEncoding( QTextStream::UnicodeUTF8 );
394
ts << m_incomingData << endl;
397
buffer.open( IO_ReadOnly );
398
BibTeX::File *tmpBibFile = importer.load( &buffer );
401
if ( tmpBibFile != NULL )
403
int count = m_widget->spinBoxMaxHits->value();
404
for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); count > 0 && it != tmpBibFile->end(); ++it )
406
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
409
emit foundEntry( new BibTeX::Entry( entry ), false );
418
setEndSearch( WebQuery::statusError );
422
setEndSearch( WebQuery::statusSuccess );
425
#include "webquerysciencedirect.moc"