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

« back to all changes in this revision

Viewing changes to src/webquerybibsonomy.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 <qapplication.h>
21
 
#include <qfile.h>
22
 
#include <qregexp.h>
23
 
#include <qstring.h>
24
 
#include <qspinbox.h>
25
 
 
26
 
#include <klocale.h>
27
 
#include <klineedit.h>
28
 
#include <kmessagebox.h>
29
 
#include <kio/netaccess.h>
30
 
#include <kurl.h>
31
 
 
32
 
#include <settings.h>
33
 
#include <fileimporterbibtex.h>
34
 
#include "webquerybibsonomy.h"
35
 
 
36
 
namespace KBibTeX
37
 
{
38
 
    WebQueryBibSonomyWidget::WebQueryBibSonomyWidget( QWidget *parent, const char *name )
39
 
            : WebQueryWidget( parent, name )
40
 
    {
41
 
        init();
42
 
 
43
 
        Settings *settings = Settings::self();
44
 
        QString value = settings->getWebQueryDefault( "BibSonomy" );
45
 
        value = value == QString::null ? "" : value;
46
 
        lineEditQuery->setText( value );
47
 
        slotTextChanged( value, true );
48
 
    }
49
 
 
50
 
 
51
 
    WebQueryBibSonomy::WebQueryBibSonomy( QWidget* parent ): WebQuery( parent )
52
 
    {
53
 
        m_widget = new WebQueryBibSonomyWidget( parent );
54
 
    }
55
 
 
56
 
    WebQueryBibSonomy::~WebQueryBibSonomy()
57
 
    {
58
 
        delete m_widget;
59
 
    }
60
 
 
61
 
    QString WebQueryBibSonomy::title()
62
 
    {
63
 
        return i18n( "BibSonomy" );
64
 
    }
65
 
 
66
 
    QString WebQueryBibSonomy::disclaimer()
67
 
    {
68
 
        return i18n( "BibSonomy bookmark sharing system" );
69
 
    }
70
 
 
71
 
    QString WebQueryBibSonomy::disclaimerURL()
72
 
    {
73
 
        return "http://www.bibsonomy.org/help/about/";
74
 
    }
75
 
 
76
 
    WebQueryWidget *WebQueryBibSonomy::widget()
77
 
    {
78
 
        return m_widget;
79
 
    }
80
 
 
81
 
    void WebQueryBibSonomy::query()
82
 
    {
83
 
        WebQuery::query();
84
 
        Settings *settings = Settings::self();
85
 
        settings->setWebQueryDefault( "BibSonomy", m_widget->lineEditQuery->text() );
86
 
 
87
 
        setNumStages( 1 );
88
 
 
89
 
        QString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
90
 
        if ( searchTerm.isEmpty() )
91
 
        {
92
 
            setEndSearch( WebQuery::statusInvalidQuery );
93
 
            return;
94
 
        }
95
 
 
96
 
        int numberOfResults = m_widget->spinBoxMaxHits->value();
97
 
 
98
 
        KURL url = KURL( QString( "http://www.bibsonomy.org/bib/search/%2?items=%1" ).arg( numberOfResults ) .arg( searchTerm.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" ) ) );
99
 
 
100
 
        BibTeX::File *bibFile = downloadBibTeXFile( url );
101
 
        if ( bibFile != NULL && !m_aborted )
102
 
        {
103
 
            for ( BibTeX::File::ElementList::iterator it = bibFile->begin(); it != bibFile->end(); ++it )
104
 
            {
105
 
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
106
 
                if ( entry != NULL )
107
 
                    emit foundEntry( entry, false );
108
 
            }
109
 
            setEndSearch( WebQuery::statusSuccess );
110
 
        }
111
 
        else if ( !m_aborted )
112
 
        {
113
 
            QString message = KIO::NetAccess::lastErrorString();
114
 
            if ( message.isEmpty() )
115
 
                message.prepend( '\n' );
116
 
            message.prepend( QString( i18n( "Querying database '%1' failed." ) ).arg( title() ) );
117
 
            KMessageBox::error( m_parent, message );
118
 
            setEndSearch( WebQuery::statusError );
119
 
        }
120
 
        else
121
 
            setEndSearch( WebQuery::statusAborted );
122
 
 
123
 
        if ( bibFile != NULL )
124
 
            delete bibFile;
125
 
    }
126
 
}