~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

Viewing changes to src/webquerysciencedirect.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
 
 *   fischer@unix-ag.uni-kl.de                                             *
4
 
 *                                                                         *
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.                                   *
9
 
 *                                                                         *
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.                          *
14
 
 *                                                                         *
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
 
 ***************************************************************************/
20
 
#include <qlayout.h>
21
 
#include <qlabel.h>
22
 
#include <qspinbox.h>
23
 
#include <qbuffer.h>
24
 
#include <qfile.h>
25
 
 
26
 
#include <kpushbutton.h>
27
 
#include <klocale.h>
28
 
#include <kcombobox.h>
29
 
#include <kiconloader.h>
30
 
#include <kio/jobclasses.h>
31
 
#include <kio/job.h>
32
 
#include <kio/netaccess.h>
33
 
 
34
 
#include <settings.h>
35
 
#include <fileimporterris.h>
36
 
#include "webquerysciencedirect.h"
37
 
 
38
 
namespace KBibTeX
39
 
{
40
 
    WebQueryScienceDirectWidget::WebQueryScienceDirectWidget( QWidget *parent, const char *name )
41
 
            : WebQueryWidget( parent, name )
42
 
    {
43
 
        init();
44
 
 
45
 
        QString allValues;
46
 
        Settings *settings = Settings::self();
47
 
        QString value = settings->getWebQueryDefault( "ScienceDirect_title" );
48
 
        value = value == QString::null ? "" : value;
49
 
        lineEditQuery->setText( value );
50
 
        allValues += value;
51
 
        value = settings->getWebQueryDefault( "ScienceDirect_author" );
52
 
        value = value == QString::null ? "" : value;
53
 
        lineEditAuthor->setText( value );
54
 
        allValues += value;
55
 
        value = settings->getWebQueryDefault( "ScienceDirect_journal" );
56
 
        value = value == QString::null ? "" : value;
57
 
        lineEditJournal->setText( value );
58
 
        allValues += value;
59
 
        value = settings->getWebQueryDefault( "ScienceDirect_volume" );
60
 
        value = value == QString::null ? "" : value;
61
 
        lineEditVolume->setText( value );
62
 
        allValues += value;
63
 
        value = settings->getWebQueryDefault( "ScienceDirect_issue" );
64
 
        value = value == QString::null ? "" : value;
65
 
        lineEditIssue->setText( value );
66
 
        allValues += value;
67
 
        value = settings->getWebQueryDefault( "ScienceDirect_page" );
68
 
        value = value == QString::null ? "" : value;
69
 
        lineEditPage->setText( value );
70
 
        allValues += value;
71
 
 
72
 
        slotTextChanged( allValues, true );
73
 
    }
74
 
 
75
 
    void WebQueryScienceDirectWidget::init()
76
 
    {
77
 
        QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
78
 
 
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& ) ) );
95
 
 
96
 
 
97
 
        hLayout = new QHBoxLayout( );
98
 
        vLayout->addLayout( hLayout );
99
 
 
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& ) ) );
114
 
 
115
 
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
116
 
 
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& ) ) );
131
 
 
132
 
        hLayout = new QHBoxLayout( );
133
 
        vLayout->addLayout( hLayout );
134
 
 
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() ) );
142
 
 
143
 
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
144
 
 
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() ) );
152
 
 
153
 
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
154
 
 
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() ) );
162
 
 
163
 
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
164
 
 
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 );
173
 
    }
174
 
 
175
 
    void WebQueryScienceDirectWidget::slotTextChangedSD()
176
 
    {
177
 
        QString text = lineEditQuery->text().stripWhiteSpace().append( lineEditAuthor->text().stripWhiteSpace() ).append( lineEditJournal->text().stripWhiteSpace() ).replace( '$', "" );
178
 
        emit enableSearch( !text.isEmpty() );
179
 
    }
180
 
 
181
 
    WebQueryScienceDirect::WebQueryScienceDirect( QWidget* parent ): WebQuery( parent )
182
 
    {
183
 
        m_importer = new BibTeX::FileImporterBibTeX( FALSE );
184
 
        m_importer->setIgnoreComments( TRUE );
185
 
        m_widget = new WebQueryScienceDirectWidget( parent );
186
 
    }
187
 
 
188
 
    WebQueryScienceDirect::~WebQueryScienceDirect()
189
 
    {
190
 
        delete m_widget;
191
 
        delete m_importer;
192
 
    }
193
 
 
194
 
    QString WebQueryScienceDirect::title()
195
 
    {
196
 
        return i18n( "ScienceDirect" );
197
 
    }
198
 
 
199
 
    QString WebQueryScienceDirect::disclaimer()
200
 
    {
201
 
        return i18n( "About ScienceDirect" );
202
 
    }
203
 
 
204
 
    QString WebQueryScienceDirect::disclaimerURL()
205
 
    {
206
 
        return "http://info.sciencedirect.com/";
207
 
    }
208
 
 
209
 
    WebQueryWidget *WebQueryScienceDirect::widget()
210
 
    {
211
 
        return m_widget;
212
 
    }
213
 
 
214
 
    void WebQueryScienceDirect::query()
215
 
    {
216
 
        WebQuery::query();
217
 
 
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() );
224
 
 
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" );
231
 
 
232
 
        if ( tak.isEmpty() && author.isEmpty() && journal.isEmpty() )
233
 
        {
234
 
            setEndSearch( WebQuery::statusInvalidQuery );
235
 
            return;
236
 
        }
237
 
 
238
 
        setNumStages( 4 );
239
 
 
240
 
        if ( !getStartPage() ) return;
241
 
 
242
 
        if ( !getResultPage( tak, author, journal, volume, issue, page ) ) return;
243
 
 
244
 
        if ( !getArticleListPage() ) return;
245
 
 
246
 
        if ( !getRISFile() ) return;
247
 
    }
248
 
 
249
 
    bool  WebQueryScienceDirect::getStartPage()
250
 
    {
251
 
        QString startPage = downloadHTML( KURL( "http://www.sciencedirect.com/" ) );
252
 
        if ( m_aborted )
253
 
        {
254
 
            setEndSearch( WebQuery::statusAborted );
255
 
            return false;
256
 
        }
257
 
        else if ( startPage == QString::null )
258
 
        {
259
 
            setEndSearch( WebQuery::statusError );
260
 
            return false;
261
 
        }
262
 
 
263
 
        int pos = startPage.find( "<input type=\"hidden\" name=\"_acct\" value=\"" );
264
 
        if ( pos < 0 )
265
 
        {
266
 
            setEndSearch( WebQuery::statusError );
267
 
            return false;
268
 
        }
269
 
        m_account = startPage.mid( pos + 41, startPage.find( "\"", pos + 43 ) - pos - 41 );
270
 
        pos = startPage.find( "<input type=\"hidden\" name=\"md5\" value=\"" );
271
 
        if ( pos < 0 )
272
 
        {
273
 
            setEndSearch( WebQuery::statusError );
274
 
            return false;
275
 
        }
276
 
        m_md5 = startPage.mid( pos + 39, startPage.find( "\"", pos + 41 ) - pos - 39 );
277
 
 
278
 
        return true;
279
 
    }
280
 
 
281
 
    bool WebQueryScienceDirect::getResultPage( const QString &tak, const QString &author, const QString &journal, const QString &volume, const QString &issue, const QString &page )
282
 
    {
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 );
285
 
        if ( m_aborted )
286
 
        {
287
 
            setEndSearch( WebQuery::statusAborted );
288
 
            return false;
289
 
        }
290
 
        else if ( resultPage == QString::null )
291
 
        {
292
 
            setEndSearch( WebQuery::statusError );
293
 
            return false;
294
 
        }
295
 
 
296
 
        int pos = resultPage.find( "<input type=\"hidden\" name=\"_ArticleListID\" value=" );
297
 
        if ( pos < 0 )
298
 
        {
299
 
            setEndSearch( WebQuery::statusError );
300
 
            return false;
301
 
        }
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 );
305
 
        if ( pos < 0 )
306
 
        {
307
 
            setEndSearch( WebQuery::statusError );
308
 
            return false;
309
 
        }
310
 
        m_md5 = resultPage.mid( pos + 39, 32 );
311
 
        pos = resultPage.find( "_userid=" );
312
 
        if ( pos < 0 )
313
 
        {
314
 
            setEndSearch( WebQuery::statusError );
315
 
            return false;
316
 
        }
317
 
        m_userid = resultPage.mid( pos + 8, resultPage.find( "&", pos + 9 ) - pos - 8 );
318
 
 
319
 
        return true;
320
 
    }
321
 
 
322
 
    bool WebQueryScienceDirect::getArticleListPage()
323
 
    {
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 );
326
 
        if ( m_aborted )
327
 
        {
328
 
            setEndSearch( WebQuery::statusAborted );
329
 
            return false;
330
 
        }
331
 
        else if ( articleListPage == QString::null )
332
 
        {
333
 
            setEndSearch( WebQuery::statusError );
334
 
            return false;
335
 
        }
336
 
        else if ( articleListPage.find( "subscription does not entitle" ) >= 0 )
337
 
        {
338
 
            qWarning( "Your subscription does not entitle you to access the download feature of ScienceDirect" );
339
 
            setEndSearch( WebQuery::statusInsufficientPermissions );
340
 
            return false;
341
 
        }
342
 
 
343
 
        int pos = articleListPage.find( "<input type=hidden name=md5 value=" );
344
 
        if ( pos < 0 )
345
 
        {
346
 
            setEndSearch( WebQuery::statusError );
347
 
            return false;
348
 
        }
349
 
        m_md5 = articleListPage.mid( pos + 34, articleListPage.find( ">", pos + 36 ) - pos - 34 );
350
 
        pos = articleListPage.find( "<input type=hidden name=_ArticleListID value=" );
351
 
        if ( pos < 0 )
352
 
        {
353
 
            setEndSearch( WebQuery::statusError );
354
 
            return false;
355
 
        }
356
 
        m_articleListID = articleListPage.mid( pos + 45, articleListPage.find( ">", pos + 46 ) - pos - 45 );
357
 
 
358
 
        return true;
359
 
    }
360
 
 
361
 
    bool WebQueryScienceDirect::getRISFile()
362
 
    {
363
 
        m_incomingData = "";
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 * ) ) );
370
 
 
371
 
        return true;
372
 
    }
373
 
 
374
 
    void WebQueryScienceDirect::slotData( KIO::Job *, const QByteArray &data )
375
 
    {
376
 
        if ( data.size() > 0 )
377
 
            m_incomingData.append( QCString( data, data.size() + 1 ) );
378
 
    }
379
 
 
380
 
    void WebQueryScienceDirect::slotResult( KIO::Job *job )
381
 
    {
382
 
        if ( job->error() )
383
 
        {
384
 
            setEndSearch( WebQuery::statusError );
385
 
            return;
386
 
        }
387
 
 
388
 
        BibTeX::FileImporterRIS importer;
389
 
        QBuffer buffer;
390
 
 
391
 
        buffer.open( IO_WriteOnly );
392
 
        QTextStream ts( &buffer );
393
 
        ts.setEncoding( QTextStream::UnicodeUTF8 );
394
 
        ts << m_incomingData << endl;
395
 
        buffer.close();
396
 
 
397
 
        buffer.open( IO_ReadOnly );
398
 
        BibTeX::File *tmpBibFile = importer.load( &buffer );
399
 
        buffer.close();
400
 
 
401
 
        if ( tmpBibFile != NULL )
402
 
        {
403
 
            int count = m_widget->spinBoxMaxHits->value();
404
 
            for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); count > 0 && it != tmpBibFile->end(); ++it )
405
 
            {
406
 
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
407
 
                if ( entry != NULL )
408
 
                {
409
 
                    emit foundEntry( new BibTeX::Entry( entry ), false );
410
 
                    --count;
411
 
                }
412
 
            }
413
 
 
414
 
            delete tmpBibFile;
415
 
        }
416
 
        else
417
 
        {
418
 
            setEndSearch( WebQuery::statusError );
419
 
            return;
420
 
        }
421
 
 
422
 
        setEndSearch( WebQuery::statusSuccess );
423
 
    }
424
 
}
425
 
#include "webquerysciencedirect.moc"