summaryrefslogtreecommitdiffstats
path: root/kbarcode/sqltables.cpp
blob: 9c4aec7199c2be98a582b0180409ac5c9a339d16 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/***************************************************************************
                          sqltables.cpp  -  description
                             -------------------
    begin                : Son Dez 29 2002
    copyright            : (C) 2002 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "sqltables.h"
#include "definition.h"

// TQt includes
#include <tqcheckbox.h>
#include <tqfile.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqmap.h>
#include <tqprogressdialog.h>
#include <tqsqldatabase.h>

// KDE includes
#include <tdeapplication.h>
#include <kcombobox.h>
#include <tdeconfig.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>

TQMap<TQString,SqlDescription*> drivers;

class MySqlDescription : public SqlDescription {
    public:
        virtual const TQString initdb( TQString ) const { return "mysql"; }
        virtual const TQString autoIncrement() const { return "int(11) NOT NULL auto_increment"; }
        virtual const TQString showColumns( const TQString & table ) const { return "SHOW COLUMNS FROM " + table; }
};

class PostgreSQLDescription : public SqlDescription {
    public:
        virtual const TQString initdb( TQString ) const { return "template1"; }
        virtual const TQString autoIncrement() const { return "serial NOT NULL"; }
        virtual const TQString showColumns( const TQString & table ) const {
            return "select a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), "
                   "a.attnotnull, a.atthasdef, a.attnum, substring(d.adsrc for 128) "
                   "from pg_catalog.pg_attribute a "
                   "inner join pg_catalog.pg_class c on c.oid = a.attrelid "
                   "left join pg_catalog.pg_attrdef d on d.adrelid = c.oid and a.attnum = "
                   "d.adnum "
                   "where a.attnum > 0 and not a.attisdropped and c.relname ~ '^" + table + "$';";
            }
};

/** should work for ODBC, too */
class SQLiteDescription : public SqlDescription {
    public:
        virtual const TQString initdb( TQString db = TQString() ) const { return db; }
        virtual const TQString autoIncrement() const { return "serial NOT NULL"; }
        virtual const TQString showColumns( const TQString & table ) const { return "SHOW COLUMNS FROM " + table; }
};

SqlTables::SqlTables( TQObject* parent )
    : TQObject( parent, "sqltables" )
{
    drivers.insert( "QMYSQL3", new MySqlDescription() );
    drivers.insert( "QPSQL7", new PostgreSQLDescription() );
    /* The same driver plugin is used for TQDBC and SQLite */
    drivers.insert( "QSQLITE", new SQLiteDescription() );
    drivers.insert( "QODBC3", new SQLiteDescription() );

    drivers.insert( "TQMYSQL3", new MySqlDescription() );
    drivers.insert( "TQPSQL7", new PostgreSQLDescription() );
    /* The same driver plugin is used for TQDBC and SQLite */
    drivers.insert( "TQSQLITE", new SQLiteDescription() );
    drivers.insert( "TQODBC3", new SQLiteDescription() );

    db = NULL;
    connected = false;
    loadConfig();

    connect( kapp, TQT_SIGNAL( aboutToQuit() ), this, TQT_SLOT( saveConfig() ) );
}

SqlTables::~SqlTables()
{
}

SqlTables* SqlTables::instance = NULL;

SqlTables* SqlTables::getInstance()
{
    if( !instance )
        instance = new SqlTables();

    return instance;
}

const SqlDescription* SqlTables::driver() const
{
    return drivers[sqldata.driver];
}
bool SqlTables::connectMySQL()
{
    db = TQSqlDatabase::addDatabase( sqldata.driver );

    db->setDatabaseName( sqldata.database );
    db->setUserName( sqldata.username );
    db->setPassword( sqldata.password );
    db->setHostName( sqldata.hostname );

    if( !db->open() )
        KMessageBox::error( 0, i18n("<qt>Unable to open database: ") + sqldata.database + "<br>" +
              db->lastError().databaseText() + "</qt>");

    connected = db->open();
    if( connected ) {
        updateTables();
        emit connectedSQL();
        // be sure that we have a clean list of producers
        Definition::updateProducer();
    }

    return connected;
}

bool SqlTables::newTables()
{
    return newTables( sqldata.username, sqldata.password, sqldata.hostname, sqldata.database, sqldata.driver );
}

bool SqlTables::newTables( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver )
{
    if( KMessageBox::warningContinueCancel( 0,
                     i18n("We are going to re-create the tables '") +
                          TQString( TABLE_BASIC "', '" TABLE_CUSTOMER "', '"
                          TABLE_CUSTOMER_TEXT) +i18n("' and '") + TQString(TABLE_LABEL_DEF "'")) ==  KMessageBox::Cancel )
        return false;

    if( !drivers[driver] )
        return false;

    TQSqlDatabase*dbase = TQSqlDatabase::addDatabase(driver, drivers[driver]->initdb( database ) );
    dbase->setDatabaseName( drivers[driver]->initdb( database ) );
    dbase->setUserName( username );
    dbase->setPassword( password );
    dbase->setHostName( hostname );

    if(dbase->open()) {

        if ((driver != "QSQLITE") && (driver != "TQSQLITE"))
        {
            bool found = false;
            TQSqlQuery existing("SHOW DATABASES LIKE '" + database + "';");
            while( existing.next() )
                found = true;

            TQSqlQuery firstquery( NULL, dbase );
            if( !found && !firstquery.exec("CREATE DATABASE " + database + ";")) {
                if( KMessageBox::warningContinueCancel( 0, i18n("<qt>Can't create database ")+ database + i18n("<br>You can continue if the database exists already.</qt>")
                    + firstquery.lastError().databaseText() ) == KMessageBox::Cancel ) {
                    dbase->close();
                    TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
                    return false;
                }
            }
        }
        dbase->close();
        TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));

        // The database is created, now connect to the one specified by the user
        dbase = TQSqlDatabase::addDatabase(driver, database );
        dbase->setDatabaseName( database );
        dbase->setUserName( username );
        dbase->setPassword( password );
        dbase->setHostName( hostname );
        if(!dbase->open() || !dbase->isOpen()) {
            KMessageBox::error( 0, i18n("KBarcode could not create the required database. Please create it manually.") + dbase->lastError().databaseText() );
            TQSqlDatabase::removeDatabase( database );
            return false;
        }


        TQSqlQuery query( NULL, dbase );

        // barcode_basic
        query.exec("DROP TABLE " TABLE_BASIC );
        exec( &query, "CREATE TABLE " TABLE_BASIC " ("
                   "    uid " + drivers[driver]->autoIncrement() + ","
                   "    article_no varchar(50) DEFAULT NULL,"
                   "    article_desc varchar(50) DEFAULT NULL,"
                   "    barcode_no TEXT DEFAULT NULL,"
                   "    encoding_type varchar(50) DEFAULT NULL,"
                   "    field0 varchar(50) DEFAULT NULL,"
                   "    field1 varchar(50) DEFAULT NULL,"
                   "    field2 varchar(50) DEFAULT NULL,"
                   "    field3 varchar(50) DEFAULT NULL,"
                   "    field4 varchar(50) DEFAULT NULL,"
                   "    field5 varchar(50) DEFAULT NULL,"
                   "    field6 varchar(50) DEFAULT NULL,"
                   "    field7 varchar(50) DEFAULT NULL,"
                   "    field8 varchar(50) DEFAULT NULL,"
                   "    field9 varchar(50) DEFAULT NULL,"
                   "    PRIMARY KEY  (uid)"
                   ");" );

        // customer
        query.exec("DROP TABLE " TABLE_CUSTOMER );
        exec( &query, "CREATE TABLE " TABLE_CUSTOMER " ("
                   "    uid " + drivers[driver]->autoIncrement() + " ,"
                   "    customer_no varchar(20) DEFAULT NULL,"
                   "    customer_name varchar(20) DEFAULT NULL,"
                   "    PRIMARY KEY  (uid)"
                   ");" );

        // customer_text
        query.exec("DROP TABLE " TABLE_CUSTOMER_TEXT );
        exec( &query, "CREATE TABLE " TABLE_CUSTOMER_TEXT " ("
                   "    uid " + drivers[driver]->autoIncrement() + ","
                   "    customer_no varchar(20) DEFAULT NULL,"
                   "    encoding_type varchar(50) DEFAULT NULL,"
                   "    article_no varchar(50) DEFAULT NULL,"
                   "    article_no_customer varchar(50) NULL,"
                   "    barcode_no TEXT DEFAULT NULL,"
                   "    line0 varchar(50) DEFAULT NULL,"
                   "    line1 varchar(50) DEFAULT NULL,"
                   "    line2 varchar(50) DEFAULT NULL,"
                   "    line3 varchar(50) DEFAULT NULL,"
                   "    line4 varchar(50) DEFAULT NULL,"
                   "    line5 varchar(50) DEFAULT NULL,"
                   "    line6 varchar(50) DEFAULT NULL,"
                   "    line7 varchar(50) DEFAULT NULL,"
                   "    line8 varchar(50) DEFAULT NULL,"
                   "    line9 varchar(50) DEFAULT NULL,"
                   "    PRIMARY KEY  (uid)"
                   ");" );

        // label_def
        query.exec("DROP TABLE " TABLE_LABEL_DEF );
        exec( &query, "CREATE TABLE " TABLE_LABEL_DEF " ("
                   "    label_no " + drivers[driver]->autoIncrement() + ","
                   "    manufacture varchar(255) DEFAULT NULL,"
                   "    type varchar(255) DEFAULT NULL,"
                   "    paper char(1) DEFAULT NULL,"
                   "    gap_top NUMERIC(10,4) NULL,"
                   "    gap_left NUMERIC(10,4) NULL,"
                   "    height NUMERIC(10,4) NULL,"
                   "    width NUMERIC(10,4) NULL,"
                   "    gap_v NUMERIC(10,4) NULL,"
                   "    gap_h NUMERIC(10,4) NULL,"
                   "    number_h int DEFAULT NULL," //smalint(6)
                   "    number_v int DEFAULT NULL," //smalint(6)
                   "    paper_type varchar(30) DEFAULT NULL,"
                   "    compatibility varchar(10) DEFAULT NULL," // keep compatibility with older versions, was "remark text"
                   "    PRIMARY KEY  (label_no)"
                   ");" );

        dbase->close();
        TQSqlDatabase::removeDatabase( database );
        KMessageBox::information( 0, i18n("Created table ")+database+i18n(" successfully!") );
    } else {
        dbase->close();
        TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
        KMessageBox::sorry( 0, i18n("Can't connect to database.") );
        return false;
    }

    return true;
}

void SqlTables::importLabelDef()
{
    if( KMessageBox::warningContinueCancel( 0, i18n("We are going to delete the complete table: " ) + TABLE_LABEL_DEF ) ==
        KMessageBox::Cancel )
        return;

    TQSqlQuery query( TQString(), db );
    exec( &query, "delete from " TABLE_LABEL_DEF );

    TQString f = locateLocal( "data", "kbarcode/labeldefinitions.sql" );
    if( !TQFile::exists( f ) ) {
        TDEConfig* config = kapp->config();
        config->setGroup( "Definitions" );
        f = config->readEntry( "defpath", locate( "data", "kbarcode/labeldefinitions.sql" ) );
    }

    importData( f, db );

    Definition::updateProducer();
}

void SqlTables::importExampleData()
{
    if( KMessageBox::warningContinueCancel( 0,
        i18n("We are going to delete the complete tables: " ) +  TQString( TABLE_BASIC ", " TABLE_CUSTOMER ", " TABLE_CUSTOMER_TEXT ) ) ==
        KMessageBox::Cancel )
        return;

    importData( locate("appdata", "exampledata.sql"), db );
}

void SqlTables::importData( const TQString & filename, TQSqlDatabase* db )
{
    if( !db ) {
        tqDebug("Can't import data, dabase not open!");
        return;
    }

    if( filename.isEmpty() || !db->isOpen() ) // quick escape
    {
        KMessageBox::error( NULL, i18n("Data file for import not found. Continuing without importing data. Please check your KBarcode installation.") );
        return;
    }

    TQFile data( filename);
    TQProgressDialog* dlg = new TQProgressDialog( i18n("SQL import progress:"),  TQString(), data.size(), 0, "dlg", true );

    if( data.open( IO_ReadOnly ) ) {
        TQString s;
        TQSqlQuery query( TQString(), db );
        while( data.readLine( s, 1024 ) != -1 )
            if( !s.isEmpty() ) {
                dlg->setProgress( dlg->progress() + s.length() );
                exec( &query, s );
            }
    } else
        KMessageBox::sorry( 0, i18n("Can't open the data file containing the label definitions.") );

    dlg->close( true );
    data.close();
}

void SqlTables::exec( TQSqlQuery* query, const TQString & text )
{
    if( !query->exec( text ) )
        KMessageBox::sorry( 0, i18n("<qt>Can't execute command:<br><b>") + text + "</b><br></qt>" + query->lastError().databaseText(), "sqlexecerror" );
}

void SqlTables::loadConfig()
{
    TDEConfig* config = kapp->config();

    config->setGroup("SQL");
    sqldata.username = config->readEntry("username", "root");
    sqldata.password = config->readEntry("password", "" );
    sqldata.hostname = config->readEntry("hostname", "localhost" );
    sqldata.database = config->readEntry("database", "kbarcode" );
    sqldata.driver = config->readEntry("driver", "TQMYSQL3" );
    sqldata.autoconnect = config->readBoolEntry("autoconnect", false );
}

void SqlTables::saveConfig()
{
    TDEConfig* config = kapp->config();

    config->setGroup("SQL");
    config->writeEntry("username", sqldata.username );
    config->writeEntry("password", sqldata.password );
    config->writeEntry("hostname", sqldata.hostname );
    config->writeEntry("database", sqldata.database );
    config->writeEntry("driver", sqldata.driver );
    config->writeEntry("autoconnect", sqldata.autoconnect );

    config->sync();
}

void SqlTables::updateTables()
{
    /* Older versions of KBarcode did only
     * support MySQL. As a reason, databases
     * do not need to be updated for other DBs
     * as they have already been created with the most
     * recent database structures.
     */
    if ((sqldata.driver != "QMYSQL3") && (sqldata.driver != "TQMYSQL3"))
        return;

    bool changed = false;
    // check for field0 - field9 (1.4.0)
    TQStringList fields;
    for( unsigned int i = 0; i < 10; i++ )
        fields.append( TQString("field%1").arg( i )  );

    TQSqlQuery query("SHOW FIELDS FROM " TABLE_BASIC );
    while ( query.next() )
        if( fields.grep( query.value( 0 ).toString(), false ).count() ) {
            fields.remove( query.value( 0 ).toString() );
        }

    if( fields.count() ) {
        TQSqlQuery q;
        for( unsigned int i = 0; i < 10; i++ )
            q.exec("ALTER TABLE " TABLE_BASIC " ADD " + fields[i] + " varchar(50)");

        tqDebug("changing fields");
        changed = true;
    }

    // change barcode_no from varchar to TEXT (1.5.0)
    TQSqlQuery query2("SHOW FIELDS FROM " TABLE_BASIC );
    while( query2.next() )
        if( query2.value( 0 ).toString() == "barcode_no" && query2.value(1) == "varchar(50)" ) {
            query2.exec( "ALTER TABLE " TABLE_BASIC " CHANGE barcode_no barcode_no TEXT" );
            changed = true;
        }

    // change barcode_no from varchar to TEXT (1.5.0)
    TQSqlQuery query3("SHOW FIELDS FROM " TABLE_CUSTOMER_TEXT );
    while( query3.next() )
        if( query3.value( 0 ).toString() == "barcode_no" && query3.value(1) == "varchar(50)" ) {
            query3.exec( "ALTER TABLE " TABLE_CUSTOMER_TEXT " CHANGE barcode_no barcode_no TEXT" );
            changed = true;
        }

    // change NUMERIC DEFAULT TO NUMERIC(10,4) (1.6.1)
    TQStringList update;
    update << "gap_top" << "gap_left" << "height" << "width" << "gap_v" << "gap_h";
    TQSqlQuery query4("SHOW FIELDS FROM " TABLE_LABEL_DEF );
    while( query4.next() )
        if( update.contains( query4.value( 0 ).toString() ) && query4.value(1).toString() == "decimal(10,0)" ) {
            TQSqlQuery q;
            q.exec( TQString("ALTER TABLE " TABLE_LABEL_DEF " CHANGE ") +
                    query4.value( 0 ).toString() + " " + query4.value( 0 ).toString() + " NUMERIC(10,4)" );
            changed = true;
        }

    if( changed )
        KMessageBox::information( 0, i18n("The SQL tables of KBarcode have changed since the last version. "
                                          "KBarcode updated them without any loss of data."  ) );
}

bool SqlTables::testSettings( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver )
{
    TQSqlDatabase* db = TQSqlDatabase::addDatabase( driver );
    if( !drivers[driver] )
      return false;

    db->setDatabaseName( database );
    db->setUserName( username );
    db->setPassword( password );
    db->setHostName( hostname );

    if( !db->open() ) 
    {
        TQSqlDatabase::removeDatabase( database );
    }
    else 
    {
        KMessageBox::information( 0, i18n("Connected successfully to your database") );
        db->close();
        TQSqlDatabase::removeDatabase( database );
        return true;
    }

    db = TQSqlDatabase::addDatabase( driver );
    
    db->setDatabaseName( drivers[driver]->initdb( database ) );

    db->setUserName( username );
    db->setPassword( password );
    db->setHostName( hostname );

    if( !db->open() ) {
        KMessageBox::error( 0, i18n("<qt>Connection failed:<br>") + database + "<br>" +
              db->lastError().databaseText() + "</qt>" );
        TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
        return false;
    } else {
        KMessageBox::information( 0, i18n("Connected successfully to your database") );
        db->close();
        TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
        return true;
    }

}

const TQString SqlTables::getBarcodeMaxLength( const TQString & name )
{
    if( SqlTables::isConnected() )
    {
        TQSqlQuery query("select uid, (length(barcode_no)) as LEN from " TABLE_BASIC
            " where encoding_type = '" + name +"' ORDER by LEN DESC LIMIT 1" );
        
        while( query.next() ) {
            TQSqlQuery queryuid("select barcode_no from barcode_basic where uid = '"
                + query.value( 0 ).toString() + "'" );
            while( queryuid.next() )
                if(!queryuid.value( 0 ).toString().isEmpty())
                    return queryuid.value( 0 ).toString();
        }
        
        TQSqlQuery query1("select uid, (length(barcode_no)) as LEN from " TABLE_CUSTOMER_TEXT 
                        " where encoding_type = '" + name +"' ORDER by LEN DESC LIMIT 1" );
        
        while( query1.next() ) {
            TQSqlQuery queryuid("select barcode_no from customer_text where uid = '"
                + query1.value( 0 ).toString() + "'" );
            while( queryuid.next() )
                if(!queryuid.value( 0 ).toString().isEmpty())
                    return queryuid.value( 0 ).toString();
        }
    }
    
    return "1234567";        
}

SqlWidget::SqlWidget( bool showlabel, TQWidget* parent, const char* name )
    : TQWidget( parent, name )
{
    TQVBoxLayout* layout = new TQVBoxLayout( this );

    TQGroupBox* groupDatabase = new TQGroupBox( this );
    groupDatabase->setTitle( i18n( "Database Settings" ) );
    groupDatabase->setColumnLayout(0, Qt::Vertical );
    groupDatabase->layout()->setSpacing( 6 );
    groupDatabase->layout()->setMargin( 11 );
    TQVBoxLayout* groupDatabaseLayout = new TQVBoxLayout( groupDatabase->layout() );
    groupDatabaseLayout->setAlignment( TQt::AlignTop );

    TQGridLayout* grid = new TQGridLayout( 2, 2 );

    TQLabel* label = new TQLabel( groupDatabase );
    label->setText( i18n("Username :") );
    grid->addWidget( label, 0, 0 );

    m_username = new KLineEdit( groupDatabase );
    grid->addWidget( m_username, 0, 1 );

    label = new TQLabel( groupDatabase );
    label->setText( i18n("Password :") );
    grid->addWidget( label, 1, 0 );

    m_password = new KLineEdit( groupDatabase );
    m_password->setEchoMode( KLineEdit::Password );
    grid->addWidget( m_password, 1, 1 );

    label = new TQLabel( groupDatabase );
    label->setText( i18n("Database :") );
    grid->addWidget( label, 2, 0 );

    m_database = new KLineEdit( "kbarcode", groupDatabase );
    grid->addWidget( m_database, 2, 1 );

    label = new TQLabel( groupDatabase );
    label->setText( i18n("Host :") );
    grid->addWidget( label, 3, 0 );

    m_hostname = new KLineEdit( "localhost", groupDatabase );
    grid->addWidget( m_hostname, 3, 1 );

    label = new TQLabel( groupDatabase );
    label->setText( i18n("Driver :") );
    grid->addWidget( label, 4, 0 );

    m_driver = new KComboBox( false, groupDatabase );
    TQStringList drList = TQSqlDatabase::drivers();
    TQStringList::Iterator it = drList.begin();
    while( it != drList.end() ) {
        m_driver->insertItem( *it );
        ++it;
    }
    grid->addWidget( m_driver, 4, 1 );
    groupDatabaseLayout->addLayout( grid );

    m_autoconnect = new TQCheckBox( i18n("&Autoconnect on program start"), groupDatabase );
    groupDatabaseLayout->addWidget( m_autoconnect );

    buttonTest = new KPushButton( groupDatabase, "buttonTest" );
    buttonTest->setText( i18n( "&Test Settings" ) );

    groupDatabaseLayout->addWidget( buttonTest );
    if( showlabel )
        groupDatabaseLayout->addWidget( new TQLabel( i18n("<b>You have to test your database settings before you can procede.</b>"), groupDatabase ) );

    layout->add( groupDatabase );

    connect( buttonTest, TQT_SIGNAL( clicked() ), this, TQT_SLOT( testSettings() ) );

    const mysqldata & sqldata = SqlTables::getInstance()->getData();
    m_username->setText( sqldata.username );
    m_password->setText( sqldata.password );
    m_hostname->setText( sqldata.hostname );
    m_database->setText( sqldata.database );
    m_autoconnect->setChecked( sqldata.autoconnect );
    for( int i = 0; i < m_driver->count(); i++ )
        if( m_driver->text(i) == sqldata.driver )
            m_driver->setCurrentItem( i );
}

SqlWidget::~SqlWidget()
{
}

void SqlWidget::save( bool usedb )
{
    mysqldata sqldata = SqlTables::getInstance()->getData();
    sqldata.username = m_username->text();
    sqldata.password = m_password->text();
    sqldata.hostname = m_hostname ->text();
    sqldata.database = m_database->text();
    sqldata.driver = m_driver->currentText();
    sqldata.autoconnect = ( usedb ? m_autoconnect->isChecked() : false );
    SqlTables::getInstance()->setData( sqldata );
}

void SqlWidget::testSettings()
{
    emit databaseWorking(
    SqlTables::getInstance()->testSettings( m_username->text(), m_password->text(),
                                            m_hostname->text(), m_database->text(),
                                            m_driver->currentText() ) );
}

const TQString SqlWidget::username() const
{
    return m_username->text();
}

const TQString SqlWidget::driver() const
{
    return m_driver->currentText();
}

const TQString SqlWidget::database() const
{
    return m_database->text();
}

const TQString SqlWidget::hostname() const
{
    return m_hostname->text();
}

const TQString SqlWidget::password() const
{
    return m_password->text();
}

bool SqlWidget::autoconnect() const
{
    return m_autoconnect->isChecked();
}

int SqlWidget::driverCount() const
{
    return m_driver->count();
}


#include "sqltables.moc"