~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/webqueryspireshep.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 <qfile.h>
21
 
#include <qapplication.h>
22
 
#include <qlayout.h>
23
 
#include <qlabel.h>
24
 
#include <qcheckbox.h>
25
 
 
26
 
#include <kiconloader.h>
27
 
#include <kcombobox.h>
28
 
#include <kpushbutton.h>
29
 
#include <klocale.h>
30
 
#include <kmessagebox.h>
31
 
#include <klineedit.h>
32
 
#include <kio/netaccess.h>
33
 
#include <kurl.h>
34
 
 
35
 
#include <settings.h>
36
 
#include <fileimporterbibtex.h>
37
 
#include "webqueryspireshep.h"
38
 
 
39
 
namespace KBibTeX
40
 
{
41
 
 
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"};
44
 
 
45
 
    WebQuerySpiresHepWidget::WebQuerySpiresHepWidget( QWidget *parent, const char *name )
46
 
            : WebQueryWidget( parent, name )
47
 
    {
48
 
        init();
49
 
 
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() );
61
 
    }
62
 
 
63
 
    void WebQuerySpiresHepWidget::init()
64
 
    {
65
 
        QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
66
 
 
67
 
        QHBoxLayout *hLayout = new QHBoxLayout( );
68
 
        vLayout->addLayout( hLayout );
69
 
 
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 );
77
 
 
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 );
93
 
 
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();
108
 
 
109
 
        m_checkBoxFetchAbstracts = new QCheckBox( i18n( "Include abstracts from arxiv.org if available" ), this );
110
 
        vLayout->addWidget( m_checkBoxFetchAbstracts );
111
 
 
112
 
        connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
113
 
        connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
114
 
 
115
 
        vLayout->addStretch( 1 );
116
 
    }
117
 
 
118
 
    WebQuerySpiresHep::WebQuerySpiresHep( QWidget* parent ): WebQuery( parent )
119
 
    {
120
 
        m_widget = new WebQuerySpiresHepWidget( parent );
121
 
    }
122
 
 
123
 
    WebQuerySpiresHep::~WebQuerySpiresHep()
124
 
    {
125
 
        delete m_widget;
126
 
    }
127
 
 
128
 
    QString WebQuerySpiresHep::title()
129
 
    {
130
 
        return i18n( "SPIRES-HEP" );
131
 
    }
132
 
 
133
 
    QString WebQuerySpiresHep::disclaimer()
134
 
    {
135
 
        return i18n( "SPIRES-HEP Search" );
136
 
    }
137
 
 
138
 
    QString WebQuerySpiresHep::disclaimerURL()
139
 
    {
140
 
        return "http://www.slac.stanford.edu/spires/";
141
 
    }
142
 
 
143
 
    WebQueryWidget *WebQuerySpiresHep::widget()
144
 
    {
145
 
        return m_widget;
146
 
    }
147
 
 
148
 
    void WebQuerySpiresHep::query()
149
 
    {
150
 
        const int maxEntries = 25;
151
 
        bool doFetchAbstracts = m_widget->m_checkBoxFetchAbstracts->isChecked();
152
 
        int mirrorIdx = m_widget->comboBoxMirror->currentItem();
153
 
 
154
 
        WebQuery::query();
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() ) );
159
 
 
160
 
        setNumStages( 1 + doFetchAbstracts ? maxEntries : 0 );
161
 
 
162
 
        QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
163
 
        if ( searchTerm.isEmpty() )
164
 
        {
165
 
            setEndSearch( WebQuery::statusInvalidQuery );
166
 
            return;
167
 
        }
168
 
 
169
 
        switch ( m_widget->comboBoxType->currentItem() )
170
 
        {
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;
177
 
        default: ;// nothing
178
 
        }
179
 
 
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() );
182
 
 
183
 
        BibTeX::File *tmpBibFile = downloadBibTeXFile( url );
184
 
        if ( tmpBibFile != NULL && !m_aborted )
185
 
        {
186
 
            int count = 0;
187
 
            for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); count < maxEntries && it != tmpBibFile->end(); ++it, ++count )
188
 
            {
189
 
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
190
 
                if ( doFetchAbstracts )
191
 
                {
192
 
                    fetchArxivAbstract( entry );
193
 
                    enterNextStage();
194
 
                }
195
 
                if ( entry != NULL )
196
 
                    emit foundEntry( new BibTeX::Entry( entry ), false );
197
 
            }
198
 
 
199
 
            setEndSearch( WebQuery::statusSuccess );
200
 
        }
201
 
        else if ( !m_aborted )
202
 
        {
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 );
209
 
        }
210
 
        else
211
 
            setEndSearch( WebQuery::statusAborted );
212
 
 
213
 
        if ( tmpBibFile != NULL )
214
 
            delete tmpBibFile;
215
 
    }
216
 
 
217
 
    void WebQuerySpiresHep::fetchArxivAbstract( BibTeX::Entry *entry )
218
 
    {
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;
223
 
 
224
 
        if ( archivePrefix == "arXiv" && !eprint.isNull() )
225
 
        {
226
 
            KURL absURL( QString( "http://arxiv.org/abs/" ).append( eprint ) );
227
 
            QString abstract = download( absURL );
228
 
            int p = abstract.find( "Abstract:</span>" );
229
 
            if ( p > -1 )
230
 
            {
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 ) );
236
 
 
237
 
                field = new BibTeX::EntryField( "pdf" );
238
 
                entry->addField( field );
239
 
                field->setValue( new BibTeX::Value( QString( "http://arxiv.org/pdf/" ).append( eprint ) ) );
240
 
 
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 ) ) );
244
 
            }
245
 
        }
246
 
    }
247
 
}