1
/**********************************************************************
2
** Copyright (C) 2000 Trolltech AS. All rights reserved.
4
** This file is part of Qt Designer.
6
** This file may be distributed and/or modified under the terms of the
7
** GNU General Public License version 2 as published by the Free Software
8
** Foundation and appearing in the file LICENSE.GPL included in the
9
** packaging of this file.
11
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12
** licenses may use this file in accordance with the Qt Commercial License
13
** Agreement provided with the Software.
15
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
** See http://www.trolltech.com/gpl/ for GPL licensing information.
19
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20
** information about Qt Commercial License Agreements.
22
** Contact info@trolltech.com if any conditions of this licensing are
25
**********************************************************************/
27
#include "dbconnectionsimpl.h"
29
#include <qgroupbox.h>
33
#include <qcombobox.h>
35
#include <klineedit.h>
36
#include <qpushbutton.h>
37
#include <qsqldatabase.h>
38
#include <qmessagebox.h>
39
#include <qapplication.h>
40
#include "mainwindow.h"
41
#include "asciivalidator.h"
45
static bool blockChanges = FALSE;
48
* Constructs a DatabaseConnectionsEditor which is a child of 'parent', with the
49
* name 'name' and widget flags set to 'f'
51
* The dialog will by default be modeless, unless you set 'modal' to
52
* TRUE to construct a modal dialog.
54
DatabaseConnectionsEditor::DatabaseConnectionsEditor( Project *pro, QWidget* parent, const char* name, bool modal, WFlags fl )
55
: DatabaseConnectionBase( parent, name, modal, fl ), project( pro )
57
connect( buttonHelp, SIGNAL( clicked() ), MainWindow::self, SLOT( showDialogHelp() ) );
58
connectionWidget = new DatabaseConnectionWidget( grp );
59
grpLayout->addWidget( connectionWidget, 0, 0 );
61
QPtrList<DatabaseConnection> lst = project->databaseConnections();
62
for ( DatabaseConnection *conn = lst.first(); conn; conn = lst.next() )
63
listConnections->insertItem( conn->name() );
64
connectionWidget->comboDriver->insertStringList( QSqlDatabase::drivers() );
66
connectionWidget->editName->setValidator( new AsciiValidator( connectionWidget->editName ) );
70
DatabaseConnectionsEditor::~DatabaseConnectionsEditor()
74
void DatabaseConnectionsEditor::deleteConnection()
76
if ( listConnections->currentItem() == -1 )
78
project->removeDatabaseConnection( listConnections->currentText() );
79
delete listConnections->item( listConnections->currentItem() );
80
if ( listConnections->count() ) {
81
listConnections->setCurrentItem( 0 );
82
currentConnectionChanged( listConnections->currentText() );
86
project->saveConnections();
89
void DatabaseConnectionsEditor::newConnection()
93
QString n( "(default)" );
94
if ( project->databaseConnection( n ) ) {
97
while ( project->databaseConnection( n + QString::number( i ) ) )
99
n = n + QString::number( i );
101
connectionWidget->editName->setText( n );
102
listConnections->clearSelection();
103
buttonConnect->setDefault( TRUE );
104
connectionWidget->editName->setFocus();
105
blockChanges = FALSE;
108
void DatabaseConnectionsEditor::doConnect()
111
if ( listConnections->currentItem() == -1 ||
112
!listConnections->item( listConnections->currentItem() )->isSelected() ) { // new connection
113
// ### do error checking for duplicated connection names
114
DatabaseConnection *conn = new DatabaseConnection( project );
115
conn->setName( connectionWidget->editName->text() );
116
conn->setDriver( connectionWidget->comboDriver->lineEdit()->text() );
117
conn->setDatabase( connectionWidget->editDatabase->text() );
118
conn->setUsername( connectionWidget->editUsername->text() );
119
conn->setPassword( connectionWidget->editPassword->text() );
120
conn->setHostname( connectionWidget->editHostname->text() );
121
conn->setPort( connectionWidget->editPort->value() );
122
if ( conn->refreshCatalog() ) {
123
project->addDatabaseConnection( conn );
124
listConnections->insertItem( conn->name() );
125
listConnections->setCurrentItem( listConnections->count() - 1 );
126
project->saveConnections();
128
QMessageBox::warning( MainWindow::self, i18n( "Connection" ),
129
i18n( "Could not connect to the database.\n"
130
"Please ensure that the database server is running "
131
"and that all the connection information is correct.\n"
132
"[ " + conn->lastError() + " ]" ) );
135
} else { // sync // ### should this do something else? right now it just overwrites all info about the connection...
136
DatabaseConnection *conn = project->databaseConnection( listConnections->currentText() );
137
conn->setName( connectionWidget->editName->text() );
138
conn->setDriver( connectionWidget->comboDriver->lineEdit()->text() );
139
conn->setDatabase( connectionWidget->editDatabase->text() );
140
conn->setUsername( connectionWidget->editUsername->text() );
141
conn->setPassword( connectionWidget->editPassword->text() );
142
conn->setHostname( connectionWidget->editHostname->text() );
143
conn->setPort( connectionWidget->editPort->value() );
144
conn->refreshCatalog();
145
project->saveConnections();
150
void DatabaseConnectionsEditor::currentConnectionChanged( const QString &s )
153
DatabaseConnection *conn = project->databaseConnection( s );
155
enableAll( conn != 0 );
156
connectionWidget->editName->setEnabled( FALSE );
157
blockChanges = FALSE;
161
connectionWidget->editName->setText( conn->name() );
162
blockChanges = FALSE;
163
connectionWidget->comboDriver->lineEdit()->setText( conn->driver() );
164
connectionWidget->editDatabase->setText( conn->database() );
165
connectionWidget->editUsername->setText( conn->username() );
166
connectionWidget->editPassword->setText( conn->password() );
167
connectionWidget->editHostname->setText( conn->hostname() );
168
connectionWidget->editPort->setValue( conn->port() );
172
void DatabaseConnectionsEditor::connectionNameChanged( const QString &s )
174
if ( listConnections->currentItem() == 0 || blockChanges )
176
listConnections->changeItem( s, listConnections->currentItem() );
179
void DatabaseConnectionsEditor::enableAll( bool b )
181
connectionWidget->editName->setEnabled( b );
182
connectionWidget->editName->setText( "" );
183
connectionWidget->comboDriver->setEnabled( b );
184
connectionWidget->comboDriver->lineEdit()->setText( "" );
185
connectionWidget->editDatabase->setEnabled( b );
186
connectionWidget->editDatabase->setText( "" );
187
connectionWidget->editUsername->setEnabled( b );
188
connectionWidget->editUsername->setText( "" );
189
connectionWidget->editPassword->setEnabled( b );
190
connectionWidget->editPassword->setText( "" );
191
connectionWidget->editHostname->setEnabled( b );
192
connectionWidget->editHostname->setText( "" );
193
connectionWidget->editPort->setEnabled( b );
194
connectionWidget->editPort->setValue( -1 );
195
buttonConnect->setEnabled( b );