~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/webquerycitebase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2009-06-15 12:41:19 UTC
  • mfrom: (1.1.3 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090615124119-eoojhlvkmw3o9tkm
Tags: 0.2.2-1
* New upstream version (Closes: #487301, #507837, #518959, #520392).
* Bumped Standards-Version to 3.8.1. No changes necessary.
* Added 'DM-Upload-Allowed: yes'.
* Added missing '${misc:Depends}'.
* Support for per-file encoding settings (Closes: #514356)
* Upgrade debhelper compatibility to version 5.
* Update debian/copyright for 2009 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *   Copyright (C) 2004-2005 by Thomas Fischer                             *
 
2
 *   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
3
 *   fischer@unix-ag.uni-kl.de                                             *
4
4
 *                                                                         *
5
5
 *   This program is free software; you can redistribute it and/or modify  *
20
20
#include <qapplication.h>
21
21
#include <qfile.h>
22
22
#include <qstring.h>
 
23
#include <qspinbox.h>
23
24
 
 
25
#include <klineedit.h>
24
26
#include <klocale.h>
25
27
#include <kmessagebox.h>
26
28
#include <kio/netaccess.h>
27
29
#include <kurl.h>
28
30
 
 
31
#include <settings.h>
29
32
#include <fileimporterbibtex.h>
30
33
#include "webquerycitebase.h"
31
34
 
32
35
namespace KBibTeX
33
36
{
 
37
    WebQueryCitebaseWidget::WebQueryCitebaseWidget( QWidget *parent, const char *name )
 
38
            : WebQueryWidget( parent, name )
 
39
    {
 
40
        init();
 
41
 
 
42
        Settings *settings = Settings::self();
 
43
        QString value = settings->getWebQueryDefault( "Citebase" );
 
44
        value = value == QString::null ? "" : value;
 
45
        lineEditQuery->setText( value );
 
46
        slotTextChanged( value, true );
 
47
    }
 
48
 
34
49
 
35
50
    WebQueryCitebase::WebQueryCitebase( QWidget* parent ): WebQuery( parent )
36
51
    {
37
 
        // nothing
 
52
        m_widget = new WebQueryCitebaseWidget( parent );
38
53
    }
39
54
 
40
55
    WebQueryCitebase::~WebQueryCitebase()
41
56
    {
42
 
        // nothing
 
57
        delete m_widget;
43
58
    }
44
59
 
45
60
    QString WebQueryCitebase::title()
49
64
 
50
65
    QString WebQueryCitebase::disclaimer()
51
66
    {
52
 
        return i18n( "Citebase Search is a semi-autonomous citation index" );
 
67
        return i18n( "Citebase citation index" );
53
68
    }
54
69
 
55
70
    QString WebQueryCitebase::disclaimerURL()
57
72
        return "http://www.citebase.org/";
58
73
    }
59
74
 
60
 
    void WebQueryCitebase::query( const QString& searchTerm, int numberOfResults )
61
 
    {
62
 
        WebQuery::query( searchTerm, numberOfResults );
63
 
        emit setTotalSteps( 1 );
64
 
 
65
 
        QString term = searchTerm;
66
 
        KURL url = KURL( QString( "http://www.citebase.org/search?submitted=Search&author=&maxrows=%2&yearfrom=&format=BibTeX&order=DESC&type=metadata&title=%1&publication=&yearuntil=&rank=paperimpact" ).arg( term.replace( "%", "%25" ).replace( " ", "+" ).replace( "?", "%3F" ).replace( "&", "%26" ) ).arg( numberOfResults ) );
67
 
        QString tmpFile;
68
 
        if ( KIO::NetAccess::download( url, tmpFile, m_parent ) && !m_aborted )
69
 
        {
70
 
            BibTeX::FileImporterBibTeX importer( FALSE );
71
 
            importer.setIgnoreComments( TRUE );
72
 
            QFile inputFile( tmpFile );
73
 
            inputFile.open( IO_ReadOnly );
74
 
            BibTeX::File *tmpBibFile = importer.load( &inputFile );
75
 
            inputFile.close();
76
 
            KIO::NetAccess::removeTempFile( tmpFile );
77
 
 
78
 
            emit setProgress( 1 );
79
 
 
 
75
    WebQueryWidget *WebQueryCitebase::widget()
 
76
    {
 
77
        return m_widget;
 
78
    }
 
79
 
 
80
    void WebQueryCitebase::query()
 
81
    {
 
82
        WebQuery::query();
 
83
        Settings *settings = Settings::self();
 
84
        settings->setWebQueryDefault( "Citebase", m_widget->lineEditQuery->text() );
 
85
 
 
86
        setNumStages( 1 );
 
87
        int numberOfResults = m_widget->spinBoxMaxHits->value();
 
88
 
 
89
        QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
 
90
        if ( searchTerm.isEmpty() )
 
91
        {
 
92
            setEndSearch( WebQuery::statusInvalidQuery );
 
93
            return;
 
94
        }
 
95
 
 
96
        KURL url = KURL( QString( "http://www.citebase.org/search?submitted=Search&author=&maxrows=%1&yearfrom=&format=BibTeX&order=DESC&type=metadata&title=%2&publication=&yearuntil=&rank=paperimpact" ).arg( numberOfResults ).arg( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ) );
 
97
        BibTeX::File *tmpBibFile = downloadBibTeXFile( url );
 
98
        if ( tmpBibFile != NULL && !m_aborted )
 
99
        {
80
100
            int count = numberOfResults;
81
101
            for ( BibTeX::File::ElementList::iterator it = tmpBibFile->begin(); count > 0 && it != tmpBibFile->end(); ++it )
82
102
            {
83
103
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
84
104
                if ( entry != NULL )
85
105
                {
86
 
                    emit foundEntry( new BibTeX::Entry( entry ) );
 
106
                    emit foundEntry( new BibTeX::Entry( entry ), false );
87
107
                    --count;
88
108
                }
89
109
            }
90
110
 
91
 
            delete tmpBibFile;
92
 
            emit endSearch( false );
 
111
            setEndSearch( WebQuery::statusSuccess );
93
112
        }
94
113
        else if ( !m_aborted )
95
114
        {
98
117
                message.prepend( '\n' );
99
118
            message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
100
119
            KMessageBox::error( m_parent, message );
101
 
            emit endSearch( true );
 
120
            setEndSearch( WebQuery::statusError );
102
121
        }
103
122
        else
104
 
            emit endSearch( false );
 
123
            setEndSearch( WebQuery::statusAborted );
 
124
 
 
125
        if ( tmpBibFile != NULL )
 
126
            delete tmpBibFile;
105
127
    }
106
128
}