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
***************************************************************************/
25
#include <qiodevice.h>
27
#include <qcheckbox.h>
29
#include <qdatetime.h>
32
#include <kmessagebox.h>
33
#include <kio/netaccess.h>
34
#include <klineedit.h>
35
#include <kcombobox.h>
36
#include <kpushbutton.h>
37
#include <kiconloader.h>
40
#include "webquerycsb.h"
44
WebQueryCSBWidget::WebQueryCSBWidget( QWidget *parent, const char *name )
45
: WebQueryWidget( parent, name )
49
Settings *settings = Settings::self();
50
QString value = settings->getWebQueryDefault( "CSB_query" );
51
value = value == QString::null ? "" : value;
52
lineEditQuery->setText( value );
53
value = settings->getWebQueryDefault( "CSB_field" );
54
value = value == QString::null || value.isEmpty() ? "0" : value;
55
comboBoxField->setCurrentItem( value.toInt() );
56
value = settings->getWebQueryDefault( "CSB_yearCheck" );
57
value = value == QString::null || value.isEmpty() ? "0" : value;
58
checkBoxYear->setChecked( value == "1" );
59
value = settings->getWebQueryDefault( "CSB_yearType" );
60
value = value == QString::null || value.isEmpty() ? "0" : value;
61
comboBoxYear->setCurrentItem( value.toInt() );
62
value = settings->getWebQueryDefault( "CSB_yearValue" );
63
value = value == QString::null ? "2000" : value;
64
spinBoxYear->setValue( value.toInt() );
65
value = settings->getWebQueryDefault( "CSB_sortBy" );
66
value = value == QString::null || value.isEmpty() ? "0" : value;
67
comboBoxSortBy->setCurrentItem( value.toInt() );
68
value = settings->getWebQueryDefault( "CSB_onlinePapersOnly" );
69
value = value == QString::null || value.isEmpty() ? "0" : value;
70
checkBoxOnlinePapersOnly->setChecked( value == "1" );
71
slotTextChanged( value, true );
74
void WebQueryCSBWidget::init()
76
QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
78
QHBoxLayout *hLayout = new QHBoxLayout( );
79
vLayout->addLayout( hLayout );
80
KPushButton *clearSearchText = new KPushButton( this );
81
clearSearchText->setIconSet( QIconSet( SmallIcon( "locationbar_erase" ) ) );
82
hLayout->addWidget( clearSearchText );
83
QLabel *label = new QLabel( i18n( "Search &term:" ), this );
84
hLayout->addWidget( label );
85
lineEditQuery = new KLineEdit( this );
86
KCompletion *completionQuery = lineEditQuery->completionObject();
87
hLayout->addWidget( lineEditQuery );
88
label->setBuddy( lineEditQuery );
89
hLayout->addSpacing( KDialog::spacingHint() * 2 );
90
label = new QLabel( i18n( "Here, \"Field\" refers to a parameter for the CSB search (author, title, any)", "&Field:" ), this );
91
hLayout->addWidget( label );
92
comboBoxField = new KComboBox( false, this );
93
hLayout->addWidget( comboBoxField );
94
label->setBuddy( comboBoxField );
95
connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery, SLOT( clear() ) );
96
connect( lineEditQuery, SIGNAL( textChanged( const QString& ) ), this, SLOT( slotTextChanged( const QString& ) ) );
97
hLayout->setStretchFactor( lineEditQuery, 5 );
98
connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
99
connect( lineEditQuery, SIGNAL( returnPressed( const QString& ) ), completionQuery, SLOT( addItem( const QString& ) ) );
101
hLayout = new QHBoxLayout( );
102
vLayout->addLayout( hLayout );
103
label = new QLabel( i18n( "&Number of results:" ), this );
104
hLayout->addWidget( label );
105
spinBoxMaxHits = new QSpinBox( 1, 500, 1, this );
106
spinBoxMaxHits->setValue( 10 );
107
hLayout->setStretchFactor( spinBoxMaxHits, 3 );
108
hLayout->addWidget( spinBoxMaxHits );
109
label->setBuddy( spinBoxMaxHits );
110
hLayout->addSpacing( KDialog::spacingHint() * 2 );
111
checkBoxYear = new QCheckBox( i18n( "&Year:" ), this );
112
hLayout->setStretchFactor( checkBoxYear, 1 );
113
checkBoxYear->setChecked( true );
114
hLayout->addWidget( checkBoxYear );
115
connect( checkBoxYear, SIGNAL( toggled( bool ) ), this, SLOT( slotYearCheckToggled() ) );
116
comboBoxYear = new KComboBox( false, this );
117
hLayout->setStretchFactor( comboBoxYear, 3 );
118
hLayout->addWidget( comboBoxYear );
119
spinBoxYear = new QSpinBox( 1800, 2100, 1, this );
120
spinBoxYear->setValue( QDate::currentDate().year() );
121
hLayout->setStretchFactor( spinBoxYear, 6 );
122
hLayout->addWidget( spinBoxYear );
124
hLayout->addSpacing( KDialog::spacingHint() * 2 );
125
label = new QLabel( i18n( "Sort &by:" ), this );
126
hLayout->addWidget( label );
127
comboBoxSortBy = new KComboBox( false, this );
128
hLayout->setStretchFactor( comboBoxSortBy, 8 );
129
hLayout->addWidget( comboBoxSortBy );
130
label->setBuddy( comboBoxSortBy );
132
hLayout = new QHBoxLayout( );
133
vLayout->addLayout( hLayout );
134
checkBoxOnlinePapersOnly = new QCheckBox( i18n( "Online papers only" ), this );
135
hLayout->addWidget( checkBoxOnlinePapersOnly );
137
comboBoxField->insertItem( i18n( "any" ) );
138
comboBoxField->insertItem( i18n( "author" ) );
139
comboBoxField->insertItem( i18n( "title" ) );
140
comboBoxYear->insertItem( i18n( "exact" ) );
141
comboBoxYear->insertItem( i18n( "until" ) );
142
comboBoxYear->insertItem( i18n( "from" ) );
143
comboBoxYear->setCurrentItem( 1 );
144
comboBoxSortBy->insertItem( i18n( "none" ) );
145
comboBoxSortBy->insertItem( i18n( "score" ) );
146
comboBoxSortBy->insertItem( i18n( "year" ) );
149
void WebQueryCSBWidget::slotYearCheckToggled()
151
bool yearEnabled = checkBoxYear->isChecked();
152
comboBoxYear->setEnabled( yearEnabled );
153
spinBoxYear->setEnabled( yearEnabled );
156
WebQueryCSB::WebQueryCSB( QWidget* parent ): WebQuery( parent )
158
m_importer = new BibTeX::FileImporterBibTeX( FALSE );
159
m_importer->setIgnoreComments( TRUE );
160
m_widget = new WebQueryCSBWidget( parent );
163
WebQueryCSB::~WebQueryCSB()
169
QString WebQueryCSB::title()
171
return i18n( "Computer Science Bibliographies" );
174
QString WebQueryCSB::disclaimer()
176
return i18n( "Copyright for the Bibliography Collection" );
179
QString WebQueryCSB::disclaimerURL()
181
return "http://liinwww.ira.uka.de/bibliography/Copyright.html";
184
WebQueryWidget *WebQueryCSB::widget()
189
void WebQueryCSB::query()
193
Settings *settings = Settings::self();
194
settings->setWebQueryDefault( "CSB_query", m_widget->lineEditQuery->text() );
195
settings->setWebQueryDefault( "CSB_field", QString::number( m_widget->comboBoxField->currentItem() ) );
196
settings->setWebQueryDefault( "CSB_yearCheck", m_widget->checkBoxYear->isChecked() ? "1" : "0" );
197
settings->setWebQueryDefault( "CSB_yearType", QString::number( m_widget->comboBoxYear->currentItem() ) );
198
settings->setWebQueryDefault( "CSB_yearValue", QString::number( m_widget->spinBoxYear->value() ) );
199
settings->setWebQueryDefault( "CSB_sortBy", QString::number( m_widget->comboBoxSortBy->currentItem() ) );
200
settings->setWebQueryDefault( "CSB_onlinePapersOnly", m_widget->checkBoxOnlinePapersOnly->isChecked() ? "1" : "0" );
204
int numberOfResults = m_widget->spinBoxMaxHits->value();
205
QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
208
if ( m_widget->checkBoxYear->isChecked() )
210
switch ( m_widget->comboBoxYear->currentItem() )
212
case 1: year = QString( "before=%1" ).arg( m_widget->spinBoxYear->value() ); break;
213
case 2: year = QString( "since=%1" ).arg( m_widget->spinBoxYear->value() ); break;
214
default: year = QString( "year=%1" ).arg( m_widget->spinBoxYear->value() ); break;
219
switch ( m_widget->comboBoxField->currentItem() )
221
case 1: field = "au"; break;
222
case 2: field = "ti"; break;
223
default: field = ""; break;
227
switch ( m_widget->comboBoxSortBy->currentItem() )
229
case 1: sortBy = "&sort=score"; break;
230
case 2: sortBy = "&sort=year"; break;
231
default: sortBy = ""; break;
234
QString onlinePapersOnly = m_widget->checkBoxOnlinePapersOnly->isChecked() ? "&online=on" : "";
236
KURL url = KURL( QString( "http://liinwww.ira.uka.de/csbib?query=%4&results=bibtex&maxnum=%1&%2&field=%3" ).arg( numberOfResults ).arg( year ).arg( field ).arg( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ).append( onlinePapersOnly ).append( sortBy ) );
238
QString rawText = download( url );
239
if ( rawText != NULL && !m_aborted )
241
QRegExp removeXML( "<[^>]+>" );
242
rawText.replace( removeXML, "" );
244
BibTeX::FileImporterBibTeX importer( FALSE );
245
importer.setIgnoreComments( TRUE );
248
buffer.open( IO_WriteOnly );
249
QTextStream ts( &buffer );
250
ts.setEncoding( QTextStream::UnicodeUTF8 );
251
ts << rawText << endl;
254
buffer.open( IO_ReadOnly );
255
BibTeX::File *tmpBibFile = importer.load( &buffer );
258
if ( tmpBibFile != NULL && tmpBibFile->count() > 0 )
260
for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); !m_aborted && it != tmpBibFile->end(); ++it )
262
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
264
emit foundEntry( new BibTeX::Entry( entry ), false );
267
setEndSearch( WebQuery::statusSuccess );
270
setEndSearch( WebQuery::statusError );
272
if ( tmpBibFile != NULL )
275
else if ( !m_aborted )
277
QString message = KIO::NetAccess::lastErrorString();
278
if ( message.isEmpty() )
279
message.prepend( '\n' );
280
message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
281
KMessageBox::error( m_parent, message );
282
setEndSearch( WebQuery::statusError );
285
setEndSearch( WebQuery::statusAborted );
289
#include "webquerycsb.moc"