~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/webqueryieeexplore.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2009-06-15 12:41:19 UTC
  • mfrom: (1.1.3 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090615124119-eoojhlvkmw3o9tkm
Tags: 0.2.2-1
* New upstream version (Closes: #487301, #507837, #518959, #520392).
* Bumped Standards-Version to 3.8.1. No changes necessary.
* Added 'DM-Upload-Allowed: yes'.
* Added missing '${misc:Depends}'.
* Support for per-file encoding settings (Closes: #514356)
* Upgrade debhelper compatibility to version 5.
* Update debian/copyright for 2009 release.

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 <qspinbox.h>
 
21
#include <qfile.h>
 
22
 
 
23
#include <kio/jobclasses.h>
 
24
#include <kio/job.h>
 
25
#include <kio/netaccess.h>
 
26
#include <klineedit.h>
 
27
#include <kurl.h>
 
28
#include <klocale.h>
 
29
#include <kmessagebox.h>
 
30
 
 
31
#include <settings.h>
 
32
#include <entryfield.h>
 
33
#include <file.h>
 
34
#include <fileimporterbibtex.h>
 
35
#include "webqueryieeexplore.h"
 
36
 
 
37
#define min(a,b) ((a)>(b)?(b):(a))
 
38
 
 
39
namespace KBibTeX
 
40
{
 
41
    WebQueryIEEExploreWidget::WebQueryIEEExploreWidget( QWidget *parent, const char *name )
 
42
            : WebQueryWidget( parent, name )
 
43
    {
 
44
        init();
 
45
 
 
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 );
 
51
    }
 
52
 
 
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" )
 
54
    {
 
55
        m_widget = new WebQueryIEEExploreWidget( parent );
 
56
        m_bibtexImporter = new BibTeX::FileImporterBibTeX( false );
 
57
    }
 
58
 
 
59
    WebQueryIEEExplore::~WebQueryIEEExplore()
 
60
    {
 
61
        delete m_bibtexImporter;
 
62
        delete m_widget;
 
63
    }
 
64
 
 
65
    QString WebQueryIEEExplore::title()
 
66
    {
 
67
        return i18n( "IEEExplore" );
 
68
    }
 
69
 
 
70
    QString WebQueryIEEExplore::disclaimer()
 
71
    {
 
72
        return i18n( "What is IEEE Xplore?" );
 
73
    }
 
74
 
 
75
    QString WebQueryIEEExplore::disclaimerURL()
 
76
    {
 
77
        return "http://ieeexplore.ieee.org/guide/g_oview_faq.jsp";
 
78
    }
 
79
 
 
80
    WebQueryWidget *WebQueryIEEExplore::widget()
 
81
    {
 
82
        return m_widget;
 
83
    }
 
84
 
 
85
    void WebQueryIEEExplore::query()
 
86
    {
 
87
        WebQuery::query();
 
88
        Settings *settings = Settings::self();
 
89
        settings->setWebQueryDefault( "IEEE", m_widget->lineEditQuery->text() );
 
90
 
 
91
        m_arnumList.clear();
 
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() )
 
96
        {
 
97
            setEndSearch( WebQuery::statusInvalidQuery );
 
98
            return;
 
99
        }
 
100
 
 
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" ) );
 
103
 
 
104
        QString completeText = downloadHTML( url );
 
105
        if ( completeText != QString::null && !m_aborted )
 
106
        {
 
107
            if ( completeText.find( "You have entered an invalid search" ) > -1 )
 
108
            {
 
109
                KMessageBox::information( m_widget, i18n( "You have entered an invalid search." ), i18n( "Search Error" ) );
 
110
                setEndSearch( WebQuery::statusInvalidQuery );
 
111
                return;
 
112
            }
 
113
            else if ( completeText.find( "No results" ) > -1 )
 
114
            {
 
115
                setEndSearch( WebQuery::statusSuccess );
 
116
                return;
 
117
            }
 
118
 
 
119
            m_numberOfMatches.search( completeText );
 
120
            bool ok;
 
121
            m_numMatches = m_numberOfMatches.cap( 1 ).toInt( &ok );
 
122
            if ( !ok ) m_numMatches = 0;
 
123
 
 
124
            if ( m_numMatches <= 0 )
 
125
            {
 
126
                setEndSearch( WebQuery::statusSuccess );
 
127
                return;
 
128
            }
 
129
 
 
130
            m_findArNumber.search( completeText );
 
131
            for ( int i = 0; i < m_numMatches; ++i )
 
132
            {
 
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 );
 
137
 
 
138
                m_findArNumber.search( completeText,  m_findArNumber.pos( 1 ) + 1 );
 
139
            }
 
140
 
 
141
            if ( !m_arnumList.isEmpty() )
 
142
            {
 
143
                m_hitCounter = 1;
 
144
                fetchNext();
 
145
            }
 
146
            else
 
147
            {
 
148
                setEndSearch( WebQuery::statusSuccess );
 
149
            }
 
150
        }
 
151
        else if ( !m_aborted )
 
152
        {
 
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 );
 
159
        }
 
160
        else
 
161
        {
 
162
            setEndSearch( WebQuery::statusSuccess );
 
163
        }
 
164
    }
 
165
 
 
166
    void WebQueryIEEExplore::fetchNext()
 
167
    {
 
168
        if ( m_arnumList.isEmpty() ) return;
 
169
 
 
170
        int arnum = m_arnumList.first();
 
171
        m_arnumList.remove( m_arnumList.begin() );
 
172
 
 
173
        m_incomingData = "";
 
174
        QString data = "dlSelect=cite&fileFormate=BibTex&arnumber=%3Carnumber%3E" + QString::number( arnum ) + "%3C%2Farnumber%3E&Submit=Download";
 
175
 
 
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 * ) ) );
 
180
    }
 
181
 
 
182
    void WebQueryIEEExplore::slotData( KIO::Job *, const QByteArray &data )
 
183
    {
 
184
        if ( data.size() > 0 )
 
185
            m_incomingData.append( QCString( data, data.size() + 1 ) );
 
186
    }
 
187
 
 
188
    void WebQueryIEEExplore::slotResult( KIO::Job *job )
 
189
    {
 
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})$" );
 
191
        if ( job->error() )
 
192
        {
 
193
            job->showErrorDialog();
 
194
            return;
 
195
        }
 
196
 
 
197
        enterNextStage();
 
198
        m_incomingData.replace( "<br>", "" );
 
199
 
 
200
        BibTeX::File *bibtexFile = m_bibtexImporter->load( m_incomingData );
 
201
        if ( bibtexFile != NULL )
 
202
        {
 
203
            for ( BibTeX::File::ElementList::iterator it = bibtexFile->begin(); it != bibtexFile->end(); ++it )
 
204
            {
 
205
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
 
206
                if ( entry != NULL )
 
207
                {
 
208
                    fixDate( entry );
 
209
                    emit foundEntry( new BibTeX::Entry( entry ), false );
 
210
                }
 
211
            }
 
212
            delete bibtexFile;
 
213
        }
 
214
 
 
215
        if ( !m_arnumList.isEmpty() )
 
216
            fetchNext();
 
217
        else
 
218
        {
 
219
            setEndSearch( WebQuery::statusSuccess );
 
220
        }
 
221
    }
 
222
 
 
223
    void WebQueryIEEExplore::fixDate( BibTeX::Entry *entry )
 
224
    {
 
225
        m_date.search( entry->getField( BibTeX::EntryField::ftYear )->value()->text() );
 
226
 
 
227
        if ( !m_date.cap( 3 ).isNull() && !m_date.cap( 3 ).isEmpty() )
 
228
        {
 
229
            BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftYear );
 
230
            if ( field == NULL )
 
231
            {
 
232
                field = new BibTeX::EntryField( BibTeX::EntryField::ftJournal );
 
233
                entry->addField( field );
 
234
            }
 
235
            field->setValue( new BibTeX::Value( m_date.cap( 3 ), true ) );
 
236
        }
 
237
 
 
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 )
 
240
        {
 
241
            BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftMonth );
 
242
            if ( field == NULL )
 
243
            {
 
244
                field = new BibTeX::EntryField( BibTeX::EntryField::ftJournal );
 
245
                entry->addField( field );
 
246
            }
 
247
 
 
248
            field->setValue( new BibTeX::Value( newMonth, true ) );
 
249
 
 
250
            newMonth = QString::null;
 
251
            if ( !m_date.cap( 2 ).isNull() && !m_date.cap( 2 ).isEmpty() && ( newMonth = parseMonth( m_date.cap( 2 ) ) ) != QString::null )
 
252
            {
 
253
                field->value()->items.append( new BibTeX::PlainText( "/" ) );
 
254
                field->value()->items.append( new BibTeX::MacroKey( newMonth ) );
 
255
            }
 
256
        }
 
257
    }
 
258
 
 
259
    QString WebQueryIEEExplore::parseMonth( const QString &month )
 
260
    {
 
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;
 
264
    }
 
265
 
 
266
}
 
267
#include "webqueryieeexplore.moc"