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
***************************************************************************/
21
#include <qapplication.h>
24
#include <qcheckbox.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>
38
#include <fileimporterbibtex.h>
39
#include "webquerydblp.h"
43
WebQueryDBLPWidget::WebQueryDBLPWidget( QWidget *parent, const char *name )
44
: WebQueryWidget( parent, name )
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" );
58
void WebQueryDBLPWidget::init()
60
QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
62
QHBoxLayout *hLayout = new QHBoxLayout( );
63
vLayout->addLayout( hLayout );
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();
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 );
86
hLayout = new QHBoxLayout( );
87
vLayout->addLayout( hLayout );
89
checkBoxKeepEntriesSeparate = new QCheckBox( i18n( "Do not merge corresponding entries" ), this );
90
hLayout->addWidget( checkBoxKeepEntriesSeparate );
92
vLayout->addStretch( 0 );
94
connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
95
connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
98
WebQueryDBLP::WebQueryDBLP( QWidget* parent ): WebQuery( parent )
100
m_widget = new WebQueryDBLPWidget( parent );
103
WebQueryDBLP::~WebQueryDBLP()
108
QString WebQueryDBLP::title()
110
return i18n( "DBLP" );
113
QString WebQueryDBLP::disclaimer()
115
return i18n( "DBLP - Copyright" );
118
QString WebQueryDBLP::disclaimerURL()
120
return "http://www.informatik.uni-trier.de/~ley/db/copyright.html";
123
WebQueryWidget *WebQueryDBLP::widget()
128
void WebQueryDBLP::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" );
135
int numberOfResults = m_widget->spinBoxMaxHits->value();
136
setNumStages( numberOfResults + 1 );
138
QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
139
if ( searchTerm.isEmpty() )
141
setEndSearch( WebQuery::statusInvalidQuery );
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" ) ) );
147
QString completeText = download( url );
148
if ( completeText != QString::null && !m_aborted )
150
QRegExp findBibTeXurl( "<a href=\"(http://dblp.uni-trier.de/rec/bibtex/[^\"]+)\"" );
151
findBibTeXurl.search( completeText );
152
int pos = findBibTeXurl.pos( 1 );
154
int maxCount = numberOfResults;
155
while ( !m_aborted && pos > -1 && ( maxCount-- ) > 0 )
157
KURL keyUrl = KURL( findBibTeXurl.cap( 1 ) );
158
BibTeX::File *tmpBibFile = downloadBibTeXFile( keyUrl );
159
if ( tmpBibFile != NULL && !m_aborted )
161
if ( tmpBibFile->count() == 2 )
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() ) );
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 ) )
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 );
177
emit foundEntry( new BibTeX::Entry( firstEntry ), false );
178
emit foundEntry( new BibTeX::Entry( secondEntry ), m_widget->checkBoxKeepEntriesSeparate->isChecked() );
182
for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); it != tmpBibFile->end(); ++it )
184
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
186
emit foundEntry( new BibTeX::Entry( entry ), false );
190
if ( tmpBibFile != NULL )
193
findBibTeXurl.search( completeText, pos + 1 );
194
pos = findBibTeXurl.pos( 1 );
197
setEndSearch( WebQuery::statusSuccess );
199
else if ( !m_aborted )
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 );
209
setEndSearch( WebQuery::statusAborted );