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

« back to all changes in this revision

Viewing changes to src/webquerydblp.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 <qspinbox.h>
23
 
#include <qregexp.h>
24
 
#include <qcheckbox.h>
25
 
#include <qlabel.h>
26
 
#include <qlayout.h>
27
 
 
28
 
#include <klocale.h>
29
 
#include <klineedit.h>
30
 
#include <kpushbutton.h>
31
 
#include <kiconloader.h>
32
 
#include <kcombobox.h>
33
 
#include <kmessagebox.h>
34
 
#include <kio/netaccess.h>
35
 
#include <kurl.h>
36
 
 
37
 
#include <settings.h>
38
 
#include <fileimporterbibtex.h>
39
 
#include "webquerydblp.h"
40
 
 
41
 
namespace KBibTeX
42
 
{
43
 
    WebQueryDBLPWidget::WebQueryDBLPWidget( QWidget *parent, const char *name )
44
 
            : WebQueryWidget( parent, name )
45
 
    {
46
 
        init();
47
 
 
48
 
        Settings *settings = Settings::self();
49
 
        QString value = settings->getWebQueryDefault( "DBLP_query" );
50
 
        value = value == QString::null ? "" : value;
51
 
        lineEditQuery->setText( value );
52
 
        slotTextChanged( value, true );
53
 
        value = settings->getWebQueryDefault( "DBLP_keepSeparate" );
54
 
        value = value == QString::null || value.isEmpty() ? "0" : value;
55
 
        checkBoxKeepEntriesSeparate->setChecked( value == "1" );
56
 
    }
57
 
 
58
 
    void WebQueryDBLPWidget::init()
59
 
    {
60
 
        QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
61
 
 
62
 
        QHBoxLayout *hLayout = new QHBoxLayout( );
63
 
        vLayout->addLayout( hLayout );
64
 
 
65
 
        KPushButton *clearSearchText = new KPushButton( this );
66
 
        clearSearchText->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
67
 
        hLayout->addWidget( clearSearchText );
68
 
        QLabel *label = new QLabel( i18n( "Search &term:" ), this );
69
 
        hLayout->addWidget( label );
70
 
        lineEditQuery = new KLineEdit( this );
71
 
        hLayout->addWidget( lineEditQuery );
72
 
        label->setBuddy( lineEditQuery );
73
 
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
74
 
        connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery, SLOT( clear() ) );
75
 
        connect( lineEditQuery, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChanged( const QString& ) ) );
76
 
        hLayout->setStretchFactor( lineEditQuery, 4 );
77
 
        KCompletion *completionQuery = lineEditQuery->completionObject();
78
 
 
79
 
        label = new QLabel( i18n( "&Number of results:" ), this );
80
 
        hLayout->addWidget( label );
81
 
        spinBoxMaxHits = new QSpinBox( 1, 250, 1, this );
82
 
        spinBoxMaxHits->setValue( 10 );
83
 
        hLayout->addWidget( spinBoxMaxHits );
84
 
        label->setBuddy( spinBoxMaxHits );
85
 
 
86
 
        hLayout = new QHBoxLayout( );
87
 
        vLayout->addLayout( hLayout );
88
 
 
89
 
        checkBoxKeepEntriesSeparate = new QCheckBox( i18n( "Do not merge corresponding entries" ), this );
90
 
        hLayout->addWidget( checkBoxKeepEntriesSeparate );
91
 
 
92
 
        vLayout->addStretch( 0 );
93
 
 
94
 
        connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
95
 
        connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
96
 
    }
97
 
 
98
 
    WebQueryDBLP::WebQueryDBLP( QWidget* parent ): WebQuery( parent )
99
 
    {
100
 
        m_widget = new WebQueryDBLPWidget( parent );
101
 
    }
102
 
 
103
 
    WebQueryDBLP::~WebQueryDBLP()
104
 
    {
105
 
        delete m_widget;
106
 
    }
107
 
 
108
 
    QString WebQueryDBLP::title()
109
 
    {
110
 
        return i18n( "DBLP" );
111
 
    }
112
 
 
113
 
    QString WebQueryDBLP::disclaimer()
114
 
    {
115
 
        return i18n( "DBLP - Copyright" );
116
 
    }
117
 
 
118
 
    QString WebQueryDBLP::disclaimerURL()
119
 
    {
120
 
        return "http://www.informatik.uni-trier.de/~ley/db/copyright.html";
121
 
    }
122
 
 
123
 
    WebQueryWidget *WebQueryDBLP::widget()
124
 
    {
125
 
        return m_widget;
126
 
    }
127
 
 
128
 
    void WebQueryDBLP::query()
129
 
    {
130
 
        WebQuery::query();
131
 
        Settings *settings = Settings::self();
132
 
        settings->setWebQueryDefault( "DBLP_query", m_widget->lineEditQuery->text() );
133
 
        settings->setWebQueryDefault( "DBLP_keepSeparate", m_widget->checkBoxKeepEntriesSeparate->isChecked() ? "1" : "0" );
134
 
 
135
 
        int numberOfResults = m_widget->spinBoxMaxHits->value();
136
 
        setNumStages( numberOfResults + 1 );
137
 
 
138
 
        QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
139
 
        if ( searchTerm.isEmpty() )
140
 
        {
141
 
            setEndSearch( WebQuery::statusInvalidQuery );
142
 
            return;
143
 
        }
144
 
 
145
 
        KURL url = KURL( QString( "http://dblp.l3s.de/?newsearch=1&q=%1&search_opt=all&synt_query_exp=full" ).arg( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ) );
146
 
 
147
 
        QString completeText = download( url );
148
 
        if ( completeText != QString::null && !m_aborted )
149
 
        {
150
 
            QRegExp findBibTeXurl( "<a href=\"(http://dblp.uni-trier.de/rec/bibtex/[^\"]+)\"" );
151
 
            findBibTeXurl.search( completeText );
152
 
            int pos = findBibTeXurl.pos( 1 );
153
 
 
154
 
            int maxCount = numberOfResults;
155
 
            while ( !m_aborted && pos > -1 && ( maxCount-- ) > 0 )
156
 
            {
157
 
                KURL keyUrl = KURL( findBibTeXurl.cap( 1 ) );
158
 
                BibTeX::File *tmpBibFile = downloadBibTeXFile( keyUrl );
159
 
                if ( tmpBibFile != NULL && !m_aborted )
160
 
                {
161
 
                    if ( tmpBibFile->count() == 2 )
162
 
                    {
163
 
                        /** if the data returned from DBLP contains only two entry, check if they are InCollection and Book. In this case, one is the conference article and the other is the conference proceedings. */
164
 
                        BibTeX::Entry *firstEntry = dynamic_cast<BibTeX::Entry*>( *tmpBibFile->begin() );
165
 
                        BibTeX::Entry *secondEntry = dynamic_cast<BibTeX::Entry*>( *( ++tmpBibFile->begin() ) );
166
 
 
167
 
                        if ( !m_widget->checkBoxKeepEntriesSeparate->isChecked() && firstEntry != NULL && secondEntry != NULL && ( firstEntry->entryType() == BibTeX::Entry::etInProceedings || firstEntry->entryType() == BibTeX::Entry::etInCollection ) && ( secondEntry->entryType() == BibTeX::Entry::etBook || secondEntry->entryType() == BibTeX::Entry::etProceedings ) )
168
 
                        {
169
 
                            /** We merge both conference article and proceedings, as otherwise 2*n entries will be shown in the search result and it is no longer clear, which conference and with article match. */
170
 
                            BibTeX::Entry *myEntry = tmpBibFile->completeReferencedFieldsConst( firstEntry );
171
 
                            myEntry->merge( secondEntry, BibTeX::Entry::msAddNew );
172
 
                            myEntry->deleteField( BibTeX::EntryField::ftCrossRef );
173
 
                            emit foundEntry( myEntry, false );
174
 
                        }
175
 
                        else
176
 
                        {
177
 
                            emit foundEntry( new BibTeX::Entry( firstEntry ), false );
178
 
                            emit foundEntry( new BibTeX::Entry( secondEntry ), m_widget->checkBoxKeepEntriesSeparate->isChecked() );
179
 
                        }
180
 
                    }
181
 
                    else
182
 
                        for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); it != tmpBibFile->end(); ++it )
183
 
                        {
184
 
                            BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
185
 
                            if ( entry != NULL )
186
 
                                emit foundEntry( new BibTeX::Entry( entry ), false );
187
 
                        }
188
 
                }
189
 
 
190
 
                if ( tmpBibFile != NULL )
191
 
                    delete tmpBibFile;
192
 
 
193
 
                findBibTeXurl.search( completeText, pos + 1 );
194
 
                pos = findBibTeXurl.pos( 1 );
195
 
            }
196
 
 
197
 
            setEndSearch( WebQuery::statusSuccess );
198
 
        }
199
 
        else if ( !m_aborted )
200
 
        {
201
 
            QString message = KIO::NetAccess::lastErrorString();
202
 
            if ( message.isEmpty() )
203
 
                message.prepend( '\n' );
204
 
            message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
205
 
            KMessageBox::error( m_parent, message );
206
 
            setEndSearch( WebQuery::statusError );
207
 
        }
208
 
        else
209
 
            setEndSearch( WebQuery::statusAborted );
210
 
    }
211
 
 
212
 
}