~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

Viewing changes to src/settingsz3950.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
 
#ifdef HAVE_CONFIG_H
21
 
#include <config.h>
22
 
#endif
23
 
 
24
 
#ifdef HAVE_YAZ
25
 
 
26
 
#include <qlayout.h>
27
 
#include <qtimer.h>
28
 
#include <qheader.h>
29
 
#include <qlabel.h>
30
 
#include <qspinbox.h>
31
 
#include <qregexp.h>
32
 
 
33
 
#include <klistview.h>
34
 
#include <kpushbutton.h>
35
 
#include <kiconloader.h>
36
 
#include <kdialogbase.h>
37
 
#include <kmessagebox.h>
38
 
#include <klocale.h>
39
 
#include <kcombobox.h>
40
 
#include <klineedit.h>
41
 
 
42
 
#include "settingsz3950.h"
43
 
 
44
 
namespace KBibTeX
45
 
{
46
 
 
47
 
    ServerListViewItem::ServerListViewItem( KListView *list, const QString &_id, Settings::Z3950Server& _server, bool _newItem )
48
 
            : KListViewItem( list, _server.name, _server.database ), server( _server ), id( _id ), newItem( _newItem )
49
 
    {
50
 
        // nothing
51
 
    }
52
 
 
53
 
    ServerListViewItem::ServerListViewItem( KListViewItem *item, const QString &_id, Settings::Z3950Server& _server, bool _newItem )
54
 
            : KListViewItem( item, _server.name, _server.database ), server( _server ), id( _id ), newItem( _newItem )
55
 
    {
56
 
        // nothing
57
 
    }
58
 
 
59
 
    void ServerListViewItem::refresh( )
60
 
    {
61
 
        setText( 0, server.name );
62
 
        setText( 1, server.database );
63
 
    }
64
 
 
65
 
    SettingsZ3950Edit::SettingsZ3950Edit( QString &id, Settings::Z3950Server& _server, QWidget *parent, const char *name )
66
 
            : QWidget( parent, name ), m_id( id ), m_server( _server ), m_lineEditId( NULL )
67
 
    {
68
 
        QGridLayout *layout = new QGridLayout( this, 10, 2 , 0, KDialog::spacingHint() );
69
 
        QLabel *label = NULL;
70
 
 
71
 
        label = new QLabel( i18n( "Name:" ), this );
72
 
        layout->addWidget( label, 1, 0 );
73
 
        m_lineEditName = new KLineEdit( m_server.name, this );
74
 
        layout->addWidget( m_lineEditName, 1, 1 );
75
 
        label->setBuddy( m_lineEditName );
76
 
 
77
 
        label = new QLabel( i18n( "Database:" ), this );
78
 
        layout->addWidget( label, 2, 0 );
79
 
        m_lineEditDatabase = new KLineEdit( m_server.database, this );
80
 
        layout->addWidget( m_lineEditDatabase, 2, 1 );
81
 
        label->setBuddy( m_lineEditDatabase );
82
 
 
83
 
        label = new QLabel( i18n( "Host:" ), this );
84
 
        layout->addWidget( label, 3, 0 );
85
 
        m_lineEditHost = new KLineEdit( m_server.host, this );
86
 
        layout->addWidget( m_lineEditHost, 3, 1 );
87
 
        label->setBuddy( m_lineEditHost );
88
 
 
89
 
        label = new QLabel( i18n( "Port:" ), this );
90
 
        layout->addWidget( label, 4, 0 );
91
 
        m_spinBoxPort = new QSpinBox( this );
92
 
        m_spinBoxPort->setMinValue( 1 );
93
 
        m_spinBoxPort->setMaxValue( 65535 );
94
 
        m_spinBoxPort->setValue( m_server.port );
95
 
        layout->addWidget( m_spinBoxPort, 4, 1 );
96
 
        label->setBuddy( m_spinBoxPort );
97
 
 
98
 
        label = new QLabel( i18n( "User:" ), this );
99
 
        layout->addWidget( label, 5, 0 );
100
 
        m_lineEditUser = new KLineEdit( m_server.user, this );
101
 
        layout->addWidget( m_lineEditUser, 5, 1 );
102
 
        label->setBuddy( m_lineEditUser );
103
 
 
104
 
        label = new QLabel( i18n( "Password:" ), this );
105
 
        layout->addWidget( label, 6, 0 );
106
 
        m_lineEditPassword = new KLineEdit( m_server.password, this );
107
 
        layout->addWidget( m_lineEditPassword, 6, 1 );
108
 
        label->setBuddy( m_lineEditPassword );
109
 
 
110
 
        label = new QLabel( i18n( "Syntax:" ), this );
111
 
        layout->addWidget( label, 7, 0 );
112
 
        m_comboBoxSyntax = new KComboBox( true, this );
113
 
        layout->addWidget( m_comboBoxSyntax, 7, 1 );
114
 
        label->setBuddy( m_comboBoxSyntax );
115
 
        m_comboBoxSyntax->insertItem( "grs-1" );
116
 
        m_comboBoxSyntax->insertItem( "marc21" );
117
 
        m_comboBoxSyntax->insertItem( "mods" );
118
 
        m_comboBoxSyntax->insertItem( "unimarc" );
119
 
        m_comboBoxSyntax->insertItem( "usmarc" );
120
 
        m_comboBoxSyntax->setCurrentText( m_server.syntax );
121
 
 
122
 
        label = new QLabel( i18n( "Locale:" ), this );
123
 
        layout->addWidget( label, 8, 0 );
124
 
        m_comboBoxLocale = new KComboBox( true, this );
125
 
        m_comboBoxLocale->setCurrentText( m_server.locale );
126
 
        layout->addWidget( m_comboBoxLocale, 8, 1 );
127
 
        label->setBuddy( m_comboBoxLocale );
128
 
 
129
 
        label = new QLabel( i18n( "Charset:" ), this );
130
 
        layout->addWidget( label, 9, 0 );
131
 
        m_comboBoxCharset = new KComboBox( true, this );
132
 
        layout->addWidget( m_comboBoxCharset, 9, 1 );
133
 
        label->setBuddy( m_comboBoxCharset );
134
 
        m_comboBoxCharset->insertItem( "iso-5426" );
135
 
        m_comboBoxCharset->insertItem( "iso-8859-1" );
136
 
        m_comboBoxCharset->insertItem( "marc8" );
137
 
        m_comboBoxCharset->insertItem( "marc-8" );
138
 
        m_comboBoxCharset->insertItem( "utf-8" );
139
 
        m_comboBoxCharset->setCurrentText( m_server.charset );
140
 
    }
141
 
 
142
 
    SettingsZ3950Edit::~SettingsZ3950Edit()
143
 
    {
144
 
        // nothing
145
 
    }
146
 
 
147
 
    int SettingsZ3950Edit::execute( QWidget *parent, QString &id, Settings::Z3950Server &server )
148
 
    {
149
 
        KDialogBase *dlg = new KDialogBase( parent, "SettingsZ3950Edit", true, i18n( "Edit Z39.50 Server" ), KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok, false );
150
 
        SettingsZ3950Edit *edit = new SettingsZ3950Edit( id, server, dlg, "SettingsZ3950Edit" );
151
 
        dlg->setMainWidget( edit );
152
 
        connect( dlg, SIGNAL( apply() ), edit, SLOT( slotApply() ) );
153
 
        connect( dlg, SIGNAL( okClicked() ), edit, SLOT( slotApply() ) );
154
 
 
155
 
        return dlg->exec();
156
 
    }
157
 
 
158
 
    void SettingsZ3950Edit::slotApply()
159
 
    {
160
 
        m_server.charset = m_comboBoxCharset->currentText();
161
 
        m_server.database = m_lineEditDatabase->text();
162
 
        m_server.host = m_lineEditHost->text();
163
 
        m_server.locale = m_comboBoxLocale->currentText();
164
 
        m_server.name = m_lineEditName->text();
165
 
        m_server.syntax = m_comboBoxSyntax->currentText();
166
 
        m_server.user = m_lineEditUser->text();
167
 
        m_server.password = m_lineEditPassword->text();
168
 
        m_server.port = m_spinBoxPort->value();
169
 
 
170
 
        if ( m_id.isEmpty() )
171
 
        {
172
 
            m_id = QString( m_server.name ).lower().replace( QRegExp( "[^a-z0-9]" ), "" );
173
 
            qDebug( "Setting id to %s", m_id.latin1() );
174
 
        }
175
 
    }
176
 
 
177
 
    SettingsZ3950::SettingsZ3950( QWidget *parent, const char *name )
178
 
            : QWidget( parent, name )
179
 
    {
180
 
        setupGUI();
181
 
    }
182
 
 
183
 
    SettingsZ3950::~SettingsZ3950()
184
 
    {
185
 
        // nothing
186
 
    }
187
 
 
188
 
    void SettingsZ3950::applyData()
189
 
    {
190
 
        Settings *settings = Settings::self();
191
 
        settings->z3950_ServerList.clear();
192
 
 
193
 
        for ( QListViewItemIterator it( m_listServers ); it.current(); ++it )
194
 
        {
195
 
            ServerListViewItem *item = dynamic_cast<ServerListViewItem*>( *it );
196
 
            settings->z3950_ServerList[item->id] = item->server;
197
 
        }
198
 
    }
199
 
 
200
 
    void SettingsZ3950::readData()
201
 
    {
202
 
        Settings *settings = Settings::self();
203
 
        m_listServers->clear();
204
 
 
205
 
        for ( QMap<QString, Settings::Z3950Server>::Iterator it = settings->z3950_ServerList.begin(); it != settings-> z3950_ServerList.end(); ++it )
206
 
        {
207
 
            ServerListViewItem *item = new ServerListViewItem( m_listServers, it.key(), it.data(), false );
208
 
            item->setPixmap( 0, SmallIcon( "server" ) );
209
 
        }
210
 
 
211
 
        updateGUI();
212
 
    }
213
 
 
214
 
    void SettingsZ3950::slotNewServer()
215
 
    {
216
 
        Settings::Z3950Server server;
217
 
        server.port = 2100;
218
 
        ServerListViewItem * item = new ServerListViewItem( m_listServers, "", server, true );
219
 
        item->setPixmap( 0, SmallIcon( "server" ) );
220
 
        m_listServers->setSelected( item, TRUE );
221
 
        QTimer::singleShot( 100, this, SLOT( slotEditServer() ) );
222
 
    }
223
 
 
224
 
    void SettingsZ3950::slotEditServer()
225
 
    {
226
 
        ServerListViewItem * item = static_cast<ServerListViewItem*>( m_listServers->selectedItem() );
227
 
        if ( item != NULL )
228
 
        {
229
 
            if ( SettingsZ3950Edit::execute( this, item->id, item->server ) == QDialog::Accepted )
230
 
            {
231
 
                item->refresh();
232
 
                emit configChanged();
233
 
            }
234
 
            else if ( item->newItem )
235
 
            {
236
 
                delete item;
237
 
                updateGUI();
238
 
            }
239
 
        }
240
 
    }
241
 
 
242
 
    void SettingsZ3950::slotDeleteServer()
243
 
    {
244
 
        delete m_listServers->selectedItem();
245
 
        emit configChanged();
246
 
        updateGUI();
247
 
    }
248
 
 
249
 
    void SettingsZ3950::slotMoveUpServer()
250
 
    {
251
 
        ServerListViewItem * item = dynamic_cast<ServerListViewItem*>( m_listServers->selectedItem() );
252
 
        ServerListViewItem *itemAbove = NULL;
253
 
        if ( item != NULL && ( itemAbove = dynamic_cast<ServerListViewItem*>( item->itemAbove() ) ) != NULL )
254
 
        {
255
 
            Settings::Z3950Server server = item->server;
256
 
            item->server = itemAbove->server;
257
 
            itemAbove->server = server;
258
 
            QString id = item->id;
259
 
            item->id = itemAbove->id;
260
 
            itemAbove->id = id;
261
 
            for ( int i = 0; i < 2; ++i )
262
 
            {
263
 
                QString swap = item->text( i );
264
 
                item->setText( i, itemAbove->text( i ) );
265
 
                itemAbove->setText( i, swap );
266
 
            }
267
 
            m_listServers->setCurrentItem( itemAbove );
268
 
            m_listServers->ensureItemVisible( itemAbove );
269
 
        }
270
 
    }
271
 
 
272
 
    void SettingsZ3950::slotMoveDownServer()
273
 
    {
274
 
        ServerListViewItem * item = dynamic_cast<ServerListViewItem*>( m_listServers->selectedItem() );
275
 
        ServerListViewItem *itemBelow = NULL;
276
 
        if ( item != NULL && ( itemBelow = dynamic_cast<ServerListViewItem*>( item->itemBelow() ) ) != NULL )
277
 
        {
278
 
            Settings::Z3950Server server = item->server;
279
 
            item->server = itemBelow->server;
280
 
            itemBelow->server = server;
281
 
            QString id = item->id;
282
 
            item->id = itemBelow->id;
283
 
            itemBelow->id = id;
284
 
            for ( int i = 0; i < 2; ++i )
285
 
            {
286
 
                QString swap = item->text( i );
287
 
                item->setText( i, itemBelow->text( i ) );
288
 
                itemBelow->setText( i, swap );
289
 
            }
290
 
            m_listServers->setCurrentItem( itemBelow );
291
 
            m_listServers->ensureItemVisible( itemBelow );
292
 
        }
293
 
    }
294
 
 
295
 
    void SettingsZ3950::slotResetToDefault()
296
 
    {
297
 
        if ( KMessageBox::warningContinueCancel( this, i18n( "All Z39.50 server configurations will be reset to defaults." ), i18n( "Reset to Default" ), KGuiItem( i18n( "Reset" ), "reload" ) ) == KMessageBox::Continue )
298
 
        {
299
 
            Settings *settings = Settings::self();
300
 
            settings->z3950clearAll();
301
 
            settings->z3950loadDefault();
302
 
            readData();
303
 
        }
304
 
    }
305
 
 
306
 
    void SettingsZ3950::updateGUI()
307
 
    {
308
 
        QListViewItem *item = m_listServers->selectedItem();
309
 
        bool selected = item != NULL;
310
 
        m_buttonEditServer->setEnabled( selected );
311
 
        m_buttonDeleteServer->setEnabled( selected );
312
 
        m_buttonMoveDownServer->setEnabled( selected && item->itemBelow() != NULL );
313
 
        m_buttonMoveUpServer->setEnabled( selected && item->itemAbove() != NULL );
314
 
    }
315
 
 
316
 
    void SettingsZ3950::setupGUI()
317
 
    {
318
 
        QGridLayout * gridLayout = new QGridLayout( this, 7, 2, 0, KDialog::spacingHint(), "gridLayout" );
319
 
        gridLayout->setRowStretch( 5, 1 );
320
 
        gridLayout->setColStretch( 0, 1 );
321
 
 
322
 
        m_listServers = new KListView( this );
323
 
        m_listServers->setSorting( -1, FALSE );
324
 
        m_listServers->addColumn( i18n( "z3950 server", "Name" ) );
325
 
        m_listServers->addColumn( i18n( "z3950 server", "Database" ) );
326
 
        m_listServers->header()->setClickEnabled( FALSE );
327
 
        m_listServers->setFullWidth( true );
328
 
        m_listServers->setAllColumnsShowFocus( true );
329
 
        gridLayout->addMultiCellWidget( m_listServers, 0, 7, 0, 0 );
330
 
        connect( m_listServers, SIGNAL( selectionChanged() ), this, SLOT( updateGUI() ) );
331
 
        connect( m_listServers, SIGNAL( currentChanged( QListViewItem * ) ), this, SLOT( updateGUI() ) );
332
 
        connect( m_listServers, SIGNAL( doubleClicked( QListViewItem*, const QPoint &, int ) ), this, SLOT( slotEditServer() ) );
333
 
 
334
 
        m_buttonNewServer = new KPushButton( i18n( "z3950 server",  "New" ), this );
335
 
        m_buttonNewServer->setIconSet( QIconSet( SmallIcon( "add" ) ) );
336
 
        gridLayout->addWidget( m_buttonNewServer, 0, 1 );
337
 
        connect( m_buttonNewServer, SIGNAL( clicked() ), this, SLOT( slotNewServer() ) );
338
 
        m_buttonEditServer = new KPushButton( i18n( "z3950 server", "Edit" ), this );
339
 
        m_buttonEditServer->setIconSet( QIconSet( SmallIcon( "edit" ) ) );
340
 
        gridLayout->addWidget( m_buttonEditServer, 1, 1 );
341
 
        connect( m_buttonEditServer, SIGNAL( clicked() ), this, SLOT( slotEditServer() ) );
342
 
        m_buttonDeleteServer = new KPushButton( i18n( "z3950 server", "Delete" ), this );
343
 
        m_buttonDeleteServer->setIconSet( QIconSet( SmallIcon( "editdelete" ) ) );
344
 
        gridLayout->addWidget( m_buttonDeleteServer, 2, 1 );
345
 
        connect( m_buttonDeleteServer, SIGNAL( clicked() ), this, SLOT( slotDeleteServer() ) );
346
 
        m_buttonMoveUpServer = new KPushButton( i18n( "z3950 server", "Up" ), this );
347
 
        m_buttonMoveUpServer->setIconSet( QIconSet( SmallIcon( "up" ) ) );
348
 
        gridLayout->addWidget( m_buttonMoveUpServer, 3, 1 );
349
 
        connect( m_buttonMoveUpServer, SIGNAL( clicked() ), this, SLOT( slotMoveUpServer() ) );
350
 
        m_buttonMoveDownServer = new KPushButton( i18n( "z3950 server", "Down" ), this );
351
 
        m_buttonMoveDownServer->setIconSet( QIconSet( SmallIcon( "down" ) ) );
352
 
        gridLayout->addWidget( m_buttonMoveDownServer, 4, 1 );
353
 
        connect( m_buttonMoveDownServer, SIGNAL( clicked() ), this, SLOT( slotMoveDownServer() ) );
354
 
 
355
 
        m_buttonResetToDefault = new KPushButton( i18n( "z3950 server", "Reset" ), this );
356
 
        m_buttonResetToDefault->setIconSet( QIconSet( SmallIcon( "reload" ) ) );
357
 
        gridLayout->addWidget( m_buttonResetToDefault, 6, 1 );
358
 
        connect( m_buttonResetToDefault, SIGNAL( clicked() ), this, SLOT( slotResetToDefault() ) );
359
 
    }
360
 
 
361
 
}
362
 
#include "settingsz3950.moc"
363
 
 
364
 
#endif // HAVE_YAZ