summaryrefslogtreecommitdiffstats
path: root/src/webqueryz3950.cpp
blob: a405ee35597d0e05fe4664bd8c8f9b730cb6370c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/***************************************************************************
 *   Copyright (C) 2004-2009 by Thomas Fischer                             *
 *   [email protected]                                             *
 *                                                                         *
 *   based on code from or inspired by                                     *
 *   - Tellico by Robby Stephenson                                         *
 *   - kbib by Thach Nguyen                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *                                                                         *
 *   This code is based on code from Tellico 1.2.10                        *
 *                      by Robby Stephenson <[email protected]>          *
 *   Tellico is released under the GNU GPL 2, too.                         *
 *                      http://www.periapsis.org/tellico/                  *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <tqfile.h>
#include <tqapplication.h>
#include <tqspinbox.h>
#include <tqlayout.h>
#include <tqlabel.h>

#include <tdelocale.h>
#include <kstandarddirs.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <tdeio/netaccess.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <kurl.h>

#include "fileimporterbibutils.h"
#include "settings.h"
#include "webqueryz3950.h"

namespace KBibTeX
{
    const TQString WebQueryZ3950Widget::attributesI18N[] = { i18n( "any" ), i18n( "title" ), i18n( "author" ), i18n( "publisher" )};

    WebQueryZ3950Widget::WebQueryZ3950Widget( TQWidget *parent, const char *name )
            : WebQueryWidget( parent, name ), lineEditQuery2( NULL )
    {
        init();

        Settings *settings = Settings::self();
        TQString value = settings->getWebQueryDefault( "Z3950_server" );
        value = value == TQString::null || value.isEmpty() ? "0" : value;
        comboBoxServers->setCurrentItem( value.toInt() );
        value = settings->getWebQueryDefault( "Z3950_query1" );
        value = value == TQString::null ? "" : value;
        lineEditQuery->setText( value );
        slotTextChanged( value, true );
        value = settings->getWebQueryDefault( "Z3950_attr1" );
        value = value == TQString::null ? "1" : value;
        comboBoxInAttribute->setCurrentItem( value.toInt() );
        value = settings->getWebQueryDefault( "Z3950_query2" );
        lineEditQuery2->setText( value == TQString::null ? "" : value );
        value = settings->getWebQueryDefault( "Z3950_attr2" );
        value = value == TQString::null ? "1" : value;
        comboBoxInAttribute2->setCurrentItem( value.toInt() );
        value = settings->getWebQueryDefault( "Z3950_booleanOp" );
        value = value == TQString::null || value.isEmpty() ? "0" : value;
        comboBoxBooleanOp->setCurrentItem( value.toInt() );
    }

    void WebQueryZ3950Widget::init()
    {
        TQVBoxLayout *vLayout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );

        TQHBoxLayout *hLayout = new TQHBoxLayout( );
        vLayout->addLayout( hLayout );

        TQLabel *label = new TQLabel( i18n( "Server:" ), this );
        hLayout->setStretchFactor( label, 1 );
        hLayout->addWidget( label );
        comboBoxServers = new KComboBox( false, this );
        hLayout->addWidget( comboBoxServers );
        hLayout->setStretchFactor( comboBoxServers, 7 );
        label->setBuddy( comboBoxServers );
        hLayout->addSpacing( KDialog::spacingHint() * 2 );
        label = new TQLabel( i18n( "&Number of results:" ), this );
        hLayout->addWidget( label );
        hLayout->setStretchFactor( label, 1 );
        spinBoxMaxHits = new TQSpinBox( 1, 50, 1, this );
        spinBoxMaxHits->setValue( 10 );
        hLayout->addWidget( spinBoxMaxHits );
        hLayout->setStretchFactor( spinBoxMaxHits, 3 );
        label->setBuddy( spinBoxMaxHits );

        TQGridLayout *layout = new TQGridLayout( vLayout, 2, 6, KDialog::spacingHint() );

        KPushButton *clearSearchText = new KPushButton( this );
        clearSearchText->setIconSet( TQIconSet( SmallIcon( "locationbar_erase" ) ) );
        layout->addWidget( clearSearchText, 0, 1 );
        label = new TQLabel( i18n( "Search term 1:" ), this );
        layout->addWidget( label, 0, 2 );
        lineEditQuery = new KLineEdit( this );
        layout->addWidget( lineEditQuery, 0, 3 );
        label->setBuddy( lineEditQuery );
        connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery, SLOT( clear() ) );
        connect( lineEditQuery, SIGNAL( textChanged( const TQString& ) ), this, SLOT( slotTextChanged( const TQString& ) ) );
        TDECompletion *completionQuery = lineEditQuery->completionObject();
        connect( lineEditQuery, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
        connect( lineEditQuery, SIGNAL( returnPressed( const TQString& ) ), completionQuery, SLOT( addItem( const TQString& ) ) );
        label = new TQLabel( i18n( "Search in:" ), this );
        layout->addWidget( label, 0, 4 );
        comboBoxInAttribute = new KComboBox( false, this );
        layout->addWidget( comboBoxInAttribute, 0, 5 );
        label->setBuddy( comboBoxInAttribute );

        comboBoxBooleanOp = new KComboBox( false, this );
        layout->addWidget( comboBoxBooleanOp, 1, 0 );

        clearSearchText = new KPushButton( this );
        clearSearchText->setIconSet( TQIconSet( SmallIcon( "locationbar_erase" ) ) );
        layout->addWidget( clearSearchText, 1, 1 );
        label = new TQLabel( i18n( "Search term 2:" ), this );
        layout->addWidget( label, 1, 2 );
        lineEditQuery2 = new KLineEdit( this );
        layout->addWidget( lineEditQuery2, 1, 3 );
        label->setBuddy( lineEditQuery2 );
        connect( clearSearchText, SIGNAL( clicked() ), lineEditQuery2, SLOT( clear() ) );
        completionQuery = lineEditQuery->completionObject();
        connect( lineEditQuery2, SIGNAL( returnPressed() ), this, SIGNAL( startSearch() ) );
        connect( lineEditQuery2, SIGNAL( returnPressed( const TQString& ) ), completionQuery, SLOT( addItem( const TQString& ) ) );
        label = new TQLabel( i18n( "Search in:" ), this );
        layout->addWidget( label, 1, 4 );
        comboBoxInAttribute2 = new KComboBox( false, this );
        layout->addWidget( comboBoxInAttribute2, 1, 5 );
        label->setBuddy( comboBoxInAttribute2 );

#ifdef HAVE_YAZ
        Settings *settings = Settings::self();
        for ( TQMap<TQString, Settings::Z3950Server>::Iterator it = settings->z3950_ServerList.begin(); it != settings->z3950_ServerList.end(); ++it )
            comboBoxServers->insertItem( it.data().name );
#endif // HAVE_YAZ
        for ( unsigned int i = 0; i < sizeof( attributesI18N ) / sizeof( attributesI18N[0] ); ++i )
        {
            comboBoxInAttribute->insertItem( attributesI18N[i] );
            comboBoxInAttribute2->insertItem( attributesI18N[i] );
        }
        comboBoxBooleanOp->insertItem( i18n( "and" ) );
        comboBoxBooleanOp->insertItem( i18n( "or" ) );
    }

    WebQueryZ3950::WebQueryZ3950( TQWidget* parent ) : WebQuery( parent )
#ifdef HAVE_YAZ
            , m_marc21transformer( NULL ), m_unimarctransformer( NULL ), m_modsImporter( NULL ), m_conn( NULL )
#endif // HAVE_YAZ
    {
        m_widget = new WebQueryZ3950Widget( parent );
    }

    WebQueryZ3950::~WebQueryZ3950()
    {
#ifdef HAVE_YAZ
        if ( m_modsImporter != NULL ) delete m_modsImporter;
        if ( m_marc21transformer != NULL ) delete m_marc21transformer;
        if ( m_unimarctransformer != NULL ) delete m_unimarctransformer;
        if ( m_conn != NULL ) delete m_conn;
#endif // HAVE_YAZ
//         delete m_widget; FIXME Why does delete fail here?
    }

    TQString WebQueryZ3950::title()
    {
        return i18n( "Z39.50" );
    }

    TQString WebQueryZ3950::disclaimer()
    {
        return i18n( "Z39.50" );
    }

    TQString WebQueryZ3950::disclaimerURL()
    {
        return "http://[127.0.0.1]/";
    }

    WebQueryWidget *WebQueryZ3950::widget()
    {
        return m_widget;
    }

    void WebQueryZ3950::query()
    {
        WebQuery::query();

        Settings *settings = Settings::self();
        settings->setWebQueryDefault( "Z3950_server", TQString::number( m_widget->comboBoxServers->currentItem() ) );
        settings->setWebQueryDefault( "Z3950_query1", m_widget->lineEditQuery->text() );
        settings->setWebQueryDefault( "Z3950_attr1", TQString::number( m_widget->comboBoxInAttribute->currentItem() ) );
        settings->setWebQueryDefault( "Z3950_query2", m_widget->lineEditQuery2->text() );
        settings->setWebQueryDefault( "Z3950_attr2", TQString::number( m_widget->comboBoxInAttribute2->currentItem() ) );
        settings->setWebQueryDefault( "Z3950_booleanOp", TQString::number( m_widget->comboBoxBooleanOp->currentItem() ) );

#ifdef HAVE_YAZ

        TQString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace();
        if ( searchTerm.isEmpty() )
        {
            setNumStages( 1 );
            setEndSearch( WebQuery::statusError );
            return;
        }

        TQString query = queryClause( searchTerm, m_widget->comboBoxInAttribute->currentItem() );

        searchTerm = m_widget->lineEditQuery2->text().stripWhiteSpace();
        if ( !searchTerm.isEmpty() )
        {
            if ( m_widget->comboBoxBooleanOp->currentItem() == 0 )
                query = query.prepend( "@and " );
            else
                query = query.prepend( "@or " );
            query = query.append( queryClause( searchTerm, m_widget->comboBoxInAttribute2->currentItem() ) );
        }

        kdDebug() << "query = " << query << endl;

        m_conn = NULL;
        settings = Settings::self();
        for ( TQMap<TQString, Settings::Z3950Server>::Iterator it = settings->z3950_ServerList.begin(); m_conn == NULL && it != settings->z3950_ServerList.end(); ++it )
        {
            if ( it.data().name.compare( m_widget->comboBoxServers->currentText() ) == 0 )
            {
                m_syntax = it.data().syntax;
                m_conn = new KBibTeX::Z3950Connection( this, it.data().host, it.data().port, it.data().database, it.data().charset, m_syntax, "f" );
                m_conn->setUserPassword( it.data().user, it.data().password );
            }
        }

        if ( m_conn != NULL )
        {
            setNumStages( m_widget->spinBoxMaxHits->value() );
            m_started = true;

            m_conn->setQuery( query, m_widget->spinBoxMaxHits->value() );
            m_modsList.clear();
            m_hitCounter = 0;
            m_conn->start();
            kdDebug() << "WebQueryZ3950::query: started" << endl;
        }
        else
#else // HAVE_YAZ
        kdDebug() << "HAVE_YAZ not defined" << endl;
#endif // HAVE_YAZ
        {
            setNumStages( 1 );
            setEndSearch( WebQuery::statusSuccess );
        }
    }

    void WebQueryZ3950::cancelQuery()
    {
#ifdef HAVE_YAZ
        if ( m_started && m_conn != NULL )
        {
            m_started = false;
            m_conn->abort();
            m_conn->wait( 10000 );
            setEndSearch( WebQuery::statusError );
        }
#endif // HAVE_YAZ
    }

#ifdef HAVE_YAZ
    TQString WebQueryZ3950::queryClause( const TQString& text, int field )
    {
        TQString result = "@attr 1=";
        switch ( field )
        {
        case 1: result.append( "4" ); break;
        case 2: result.append( "1003" ); break;
        case 3: result.append( "1006" ); break;
        case 4: result.append( "1016" ); break;
        default: result.append( "1018" ); break;
        }
        result.append( " @attr 2=3 \"" ).append( text ).append( "\" " );

        return result;
    }
#endif // HAVE_YAZ

    void WebQueryZ3950::customEvent( TQCustomEvent* event )
    {
        kdDebug() << "WebQueryZ3950::customEvent of type " << event->type() << endl;

#ifdef HAVE_YAZ
        if ( !m_conn )
        {
            return;
        }

        if ( event->type() == KBibTeX::Z3950ResultFound::uid() )
        {
            KBibTeX::Z3950ResultFound* e = static_cast<KBibTeX::Z3950ResultFound*>( event );
            kdDebug() << "Z3950ResultFound: " << e->result().left( 24 ) << " [..] " << e->result().right( 24 ) << endl;
            storeResult( e->result(), m_syntax );
            m_hitCounter++;
            enterNextStage();
        }

        else if ( event->type() == KBibTeX::Z3950ConnectionDone::uid() )
        {
            KBibTeX::Z3950ConnectionDone* e = static_cast<KBibTeX::Z3950ConnectionDone*>( event );
            kdDebug() << "Z3950ConnectionDone: " << e->message() << " (" << e->messageType() << ")" << endl;
            if ( e->messageType() > -1 )
                KMessageBox::error( m_widget, TQString( i18n( "The server returned the following message:\n\n%1" ) ).arg( e->message() ), i18n( "Error querying Z39.50 server" ) );

            m_started = false;
            if ( m_conn != NULL )
                m_conn->wait();

            if ( !m_aborted )
            {
                evalStoredResults();
                setEndSearch( e->messageType() > -1 ? WebQuery::statusError : WebQuery::statusSuccess );
            }
        }

        tqApp->processEvents();
#endif // HAVE_YAZ
    }

#ifdef HAVE_YAZ
    void WebQueryZ3950::storeResult( const TQString& resultText, const TQString& syntax )
    {
        if ( resultText.isEmpty() ) return;
        TQString convertedResultText = TQString::null;

        if ( syntax == "mods" )
            convertedResultText = resultText;
        else if ( syntax == "usmarc" || syntax == "marc21" )
        {
            if ( m_marc21transformer == NULL ) m_marc21transformer = new BibTeX::XSLTransform( TDEGlobal::dirs()->findResource( "data", "kbibtexpart/xslt/MARC21slim2MODS3.xsl" ) );
            convertedResultText = m_marc21transformer->transform( resultText );
        }
        else if ( syntax == "unimarc" )
        {
            if ( m_unimarctransformer == NULL ) m_unimarctransformer = new BibTeX::XSLTransform( TDEGlobal::dirs()->findResource( "data", "kbibtexpart/xslt/UNIMARC2MODS3.xsl" ) );
            convertedResultText = m_unimarctransformer->transform( resultText );
        }

        m_modsList.append( convertedResultText );
    }

    void WebQueryZ3950::evalStoredResults()
    {
        if ( m_modsImporter == NULL )
            m_modsImporter = new BibTeX::FileImporterBibUtils( BibTeX::File::formatMODS );

        for ( TQStringList::Iterator it = m_modsList.begin(); it != m_modsList.end();++it )
        {
            BibTeX::File *bibtexFile = m_modsImporter->load( *it );
            if ( bibtexFile != NULL )
            {
                for ( BibTeX::File::ElementList::iterator it = bibtexFile->begin(); it != bibtexFile->end(); ++it )
                {
                    BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
                    if ( entry != NULL )
                    {
                        BibTeX::Entry *newEntry = new BibTeX::Entry( entry );
                        kdDebug() << "entry= " << newEntry->text() << endl;
                        emit foundEntry( newEntry, false );
                    }
                }

                delete bibtexFile;
            }
        }
    }
#endif // HAVE_YAZ

}
#include "webqueryz3950.moc"