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

« back to all changes in this revision

Viewing changes to src/webquerycsb.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 <qlabel.h>
21
 
#include <qlayout.h>
22
 
#include <qregexp.h>
23
 
#include <qbuffer.h>
24
 
#include <qcstring.h>
25
 
#include <qiodevice.h>
26
 
#include <qspinbox.h>
27
 
#include <qcheckbox.h>
28
 
#include <qfile.h>
29
 
#include <qdatetime.h>
30
 
 
31
 
#include <klocale.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>
38
 
 
39
 
#include <settings.h>
40
 
#include "webquerycsb.h"
41
 
 
42
 
namespace KBibTeX
43
 
{
44
 
    WebQueryCSBWidget::WebQueryCSBWidget( QWidget *parent, const char *name )
45
 
            : WebQueryWidget( parent, name )
46
 
    {
47
 
        init();
48
 
 
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 );
72
 
    }
73
 
 
74
 
    void WebQueryCSBWidget::init()
75
 
    {
76
 
        QVBoxLayout *vLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
77
 
 
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& ) ) );
100
 
 
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 );
123
 
 
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 );
131
 
 
132
 
        hLayout = new QHBoxLayout( );
133
 
        vLayout->addLayout( hLayout );
134
 
        checkBoxOnlinePapersOnly = new QCheckBox( i18n( "Online papers only" ), this );
135
 
        hLayout->addWidget( checkBoxOnlinePapersOnly );
136
 
 
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" ) );
147
 
    }
148
 
 
149
 
    void WebQueryCSBWidget::slotYearCheckToggled()
150
 
    {
151
 
        bool yearEnabled = checkBoxYear->isChecked();
152
 
        comboBoxYear->setEnabled( yearEnabled );
153
 
        spinBoxYear->setEnabled( yearEnabled );
154
 
    }
155
 
 
156
 
    WebQueryCSB::WebQueryCSB( QWidget* parent ): WebQuery( parent )
157
 
    {
158
 
        m_importer = new BibTeX::FileImporterBibTeX( FALSE );
159
 
        m_importer->setIgnoreComments( TRUE );
160
 
        m_widget = new WebQueryCSBWidget( parent );
161
 
    }
162
 
 
163
 
    WebQueryCSB::~WebQueryCSB()
164
 
    {
165
 
        delete m_widget;
166
 
        delete m_importer;
167
 
    }
168
 
 
169
 
    QString WebQueryCSB::title()
170
 
    {
171
 
        return i18n( "Computer Science Bibliographies" );
172
 
    }
173
 
 
174
 
    QString WebQueryCSB::disclaimer()
175
 
    {
176
 
        return i18n( "Copyright for the Bibliography Collection" );
177
 
    }
178
 
 
179
 
    QString WebQueryCSB::disclaimerURL()
180
 
    {
181
 
        return "http://liinwww.ira.uka.de/bibliography/Copyright.html";
182
 
    }
183
 
 
184
 
    WebQueryWidget *WebQueryCSB::widget()
185
 
    {
186
 
        return m_widget;
187
 
    }
188
 
 
189
 
    void WebQueryCSB::query()
190
 
    {
191
 
        WebQuery::query();
192
 
 
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" );
201
 
 
202
 
        setNumStages( 1 );
203
 
 
204
 
        int numberOfResults = m_widget->spinBoxMaxHits->value();
205
 
        QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
206
 
 
207
 
        QString year;
208
 
        if ( m_widget->checkBoxYear->isChecked() )
209
 
        {
210
 
            switch ( m_widget->comboBoxYear->currentItem() )
211
 
            {
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;
215
 
            }
216
 
        }
217
 
 
218
 
        QString field;
219
 
        switch ( m_widget->comboBoxField->currentItem() )
220
 
        {
221
 
        case 1: field = "au"; break;
222
 
        case 2: field = "ti"; break;
223
 
        default: field = ""; break;
224
 
        }
225
 
 
226
 
        QString sortBy;
227
 
        switch ( m_widget->comboBoxSortBy->currentItem() )
228
 
        {
229
 
        case 1: sortBy = "&sort=score"; break;
230
 
        case 2: sortBy = "&sort=year"; break;
231
 
        default: sortBy = ""; break;
232
 
        }
233
 
 
234
 
        QString onlinePapersOnly = m_widget->checkBoxOnlinePapersOnly->isChecked() ? "&online=on" : "";
235
 
 
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 ) );
237
 
 
238
 
        QString rawText = download( url );
239
 
        if ( rawText != NULL && !m_aborted )
240
 
        {
241
 
            QRegExp removeXML( "<[^>]+>" );
242
 
            rawText.replace( removeXML, "" );
243
 
 
244
 
            BibTeX::FileImporterBibTeX importer( FALSE );
245
 
            importer.setIgnoreComments( TRUE );
246
 
            QBuffer buffer;
247
 
 
248
 
            buffer.open( IO_WriteOnly );
249
 
            QTextStream ts( &buffer );
250
 
            ts.setEncoding( QTextStream::UnicodeUTF8 );
251
 
            ts << rawText << endl;
252
 
            buffer.close();
253
 
 
254
 
            buffer.open( IO_ReadOnly );
255
 
            BibTeX::File *tmpBibFile = importer.load( &buffer );
256
 
            buffer.close();
257
 
 
258
 
            if ( tmpBibFile != NULL && tmpBibFile->count() > 0 )
259
 
            {
260
 
                for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); !m_aborted && it != tmpBibFile->end(); ++it )
261
 
                {
262
 
                    BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
263
 
                    if ( entry != NULL )
264
 
                        emit foundEntry( new BibTeX::Entry( entry ), false );
265
 
                }
266
 
 
267
 
                setEndSearch( WebQuery::statusSuccess );
268
 
            }
269
 
            else
270
 
                setEndSearch( WebQuery::statusError );
271
 
 
272
 
            if ( tmpBibFile != NULL )
273
 
                delete tmpBibFile;
274
 
        }
275
 
        else if ( !m_aborted )
276
 
        {
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 );
283
 
        }
284
 
        else
285
 
            setEndSearch( WebQuery::statusAborted );
286
 
    }
287
 
 
288
 
}
289
 
#include "webquerycsb.moc"