~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to kdevdesigner/designer/dbconnectionsimpl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**********************************************************************
2
 
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
3
 
**
4
 
** This file is part of Qt Designer.
5
 
**
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.
10
 
**
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.
14
 
**
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.
17
 
**
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.
21
 
**
22
 
** Contact info@trolltech.com if any conditions of this licensing are
23
 
** not clear to you.
24
 
**
25
 
**********************************************************************/
26
 
 
27
 
#include "dbconnectionsimpl.h"
28
 
#include <qptrlist.h>
29
 
#include <qgroupbox.h>
30
 
#include <qlayout.h>
31
 
#include "project.h"
32
 
#include <qlistbox.h>
33
 
#include <qcombobox.h>
34
 
#include <qspinbox.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"
42
 
 
43
 
#include <klocale.h>
44
 
 
45
 
static bool blockChanges = FALSE;
46
 
 
47
 
/*
48
 
 *  Constructs a DatabaseConnectionsEditor which is a child of 'parent', with the
49
 
 *  name 'name' and widget flags set to 'f'
50
 
 *
51
 
 *  The dialog will by default be modeless, unless you set 'modal' to
52
 
 *  TRUE to construct a modal dialog.
53
 
 */
54
 
DatabaseConnectionsEditor::DatabaseConnectionsEditor( Project *pro, QWidget* parent,  const char* name, bool modal, WFlags fl )
55
 
    : DatabaseConnectionBase( parent, name, modal, fl ), project( pro )
56
 
{
57
 
    connect( buttonHelp, SIGNAL( clicked() ), MainWindow::self, SLOT( showDialogHelp() ) );
58
 
    connectionWidget = new DatabaseConnectionWidget( grp );
59
 
    grpLayout->addWidget( connectionWidget, 0, 0 );
60
 
#ifndef QT_NO_SQL
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() );
65
 
#endif
66
 
    connectionWidget->editName->setValidator( new AsciiValidator( connectionWidget->editName ) );
67
 
    enableAll( FALSE );
68
 
}
69
 
 
70
 
DatabaseConnectionsEditor::~DatabaseConnectionsEditor()
71
 
{
72
 
}
73
 
 
74
 
void DatabaseConnectionsEditor::deleteConnection()
75
 
{
76
 
    if ( listConnections->currentItem() == -1 )
77
 
        return;
78
 
    project->removeDatabaseConnection( listConnections->currentText() );
79
 
    delete listConnections->item( listConnections->currentItem() );
80
 
    if ( listConnections->count() ) {
81
 
        listConnections->setCurrentItem( 0 );
82
 
        currentConnectionChanged( listConnections->currentText() );
83
 
    } else {
84
 
        enableAll( FALSE );
85
 
    }
86
 
    project->saveConnections();
87
 
}
88
 
 
89
 
void DatabaseConnectionsEditor::newConnection()
90
 
{
91
 
    blockChanges = TRUE;
92
 
    enableAll( TRUE );
93
 
    QString n( "(default)" );
94
 
    if ( project->databaseConnection( n ) ) {
95
 
        n = "connection";
96
 
        int i = 2;
97
 
        while ( project->databaseConnection( n + QString::number( i ) ) )
98
 
            ++i;
99
 
        n = n + QString::number( i );
100
 
    }
101
 
    connectionWidget->editName->setText( n );
102
 
    listConnections->clearSelection();
103
 
    buttonConnect->setDefault( TRUE );
104
 
    connectionWidget->editName->setFocus();
105
 
    blockChanges = FALSE;
106
 
}
107
 
 
108
 
void DatabaseConnectionsEditor::doConnect()
109
 
{
110
 
#ifndef QT_NO_SQL
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();
127
 
        } else {
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() + " ]" ) );
133
 
            delete conn;
134
 
        }
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();
146
 
    }
147
 
#endif
148
 
}
149
 
 
150
 
void DatabaseConnectionsEditor::currentConnectionChanged( const QString &s )
151
 
{
152
 
#ifndef QT_NO_SQL
153
 
    DatabaseConnection *conn = project->databaseConnection( s );
154
 
    blockChanges = TRUE;
155
 
    enableAll( conn != 0 );
156
 
    connectionWidget->editName->setEnabled( FALSE );
157
 
    blockChanges = FALSE;
158
 
    if ( !conn )
159
 
        return;
160
 
    blockChanges = TRUE;
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() );
169
 
#endif
170
 
}
171
 
 
172
 
void DatabaseConnectionsEditor::connectionNameChanged( const QString &s )
173
 
{
174
 
    if ( listConnections->currentItem() == 0 || blockChanges )
175
 
        return;
176
 
    listConnections->changeItem( s, listConnections->currentItem() );
177
 
}
178
 
 
179
 
void DatabaseConnectionsEditor::enableAll( bool b )
180
 
{
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 );
196
 
}