kexi

kexidbconnectionwidget.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kexidbconnectionwidget.h"
21#include "kexidbconnectionwidgetdetailsbase.h"
22
23#include <kexi.h>
24#include <kexiguimsghandler.h>
25#include <kexidb/connection.h>
26#include <kexidb/utils.h>
27#include "kexidbdrivercombobox.h"
28
29#include <kdebug.h>
30#include <kiconloader.h>
31#include <klineedit.h>
32#include <knuminput.h>
33#include <kpassdlg.h>
34#include <kurlrequester.h>
35#include <ktextedit.h>
36#include <kprogress.h>
37
38#include <tqlabel.h>
39#include <tqcheckbox.h>
40#include <tqbuttongroup.h>
41#include <tqwidgetstack.h>
42#include <tqlayout.h>
43#include <tqvbox.h>
44#include <tqtooltip.h>
45#include <tqwhatsthis.h>
46#include <tqthread.h>
47#include <tqradiobutton.h>
48
51#define NO_LOAD_DB_LIST
52
53// @internal
54class KexiDBConnectionWidget::Private
55{
56 public:
57 Private()
58 : connectionOnly(false)
59 {
60 }
61
62 KPushButton *btnSaveChanges, *btnTestConnection;
63 bool connectionOnly : 1;
64};
65
66//---------
67
68KexiDBConnectionWidget::KexiDBConnectionWidget( TQWidget* parent, const char* name )
69 : KexiDBConnectionWidgetBase( parent, name )
70 , d(new Private())
71{
72 iconLabel->setPixmap(DesktopIcon("network"));
73
74 TQVBoxLayout *driversComboLyr = new TQVBoxLayout(frmEngine);
75 m_driversCombo = new KexiDBDriverComboBox(frmEngine, Kexi::driverManager().driversInfo(),
76 KexiDBDriverComboBox::ShowServerDrivers);
77 lblEngine->setBuddy( m_driversCombo );
78 lblEngine->setFocusProxy( m_driversCombo );
79 driversComboLyr->addWidget( m_driversCombo );
80
81#ifdef NO_LOAD_DB_LIST
82 btnLoadDBList->hide();
83#endif
84 btnLoadDBList->setIconSet(SmallIconSet("reload"));
85 TQToolTip::add(btnLoadDBList, i18n("Load database list from the server"));
86 TQWhatsThis::add(btnLoadDBList,
87 i18n("Loads database list from the server, so you can select one using the \"Name\" combo box."));
88
89 TQHBoxLayout *hbox = new TQHBoxLayout(frmBottom);
90 hbox->addStretch(2);
91 d->btnSaveChanges = new KPushButton(KGuiItem(i18n("Save Changes"), "document-save",
92 i18n("Save all changes made to this connection information"),
93 i18n("Save all changes made to this connection information. You can later reuse this information.")),
94 frmBottom, "savechanges");
95 hbox->addWidget( d->btnSaveChanges );
96 hbox->addSpacing( KDialogBase::spacingHint() );
97 TQWidget::setTabOrder(titleEdit, d->btnSaveChanges);
98 d->btnSaveChanges->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
99
100 d->btnTestConnection = new KPushButton(KGuiItem(i18n("&Test Connection"), "",
101 i18n("Test database connection"),
102 i18n("Tests database connection. You can ensure that valid connection information is provided.")),
103 frmBottom, "testConnection");
104 hbox->addWidget( d->btnTestConnection );
105 TQWidget::setTabOrder(d->btnSaveChanges, d->btnTestConnection);
106 d->btnTestConnection->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
107
108 connect( locationBGrp, TQ_SIGNAL(clicked(int)), this, TQ_SLOT(slotLocationBGrpClicked(int)) );
109 connect( chkPortDefault, TQ_SIGNAL(toggled(bool)), this , TQ_SLOT(slotCBToggled(bool)) );
110 connect( btnLoadDBList, TQ_SIGNAL(clicked()), this, TQ_SIGNAL(loadDBList()) );
111 connect( d->btnSaveChanges, TQ_SIGNAL(clicked()), this, TQ_SIGNAL(saveChanges()) );
112}
113
114KexiDBConnectionWidget::~KexiDBConnectionWidget()
115{
116 delete d;
117}
118
119bool KexiDBConnectionWidget::connectionOnly() const
120{ return d->connectionOnly; }
121
122void KexiDBConnectionWidget::setDataInternal(const KexiProjectData& data, bool connectionOnly,
123 const TQString& shortcutFileName)
124{
125 m_data = data;
126 d->connectionOnly = connectionOnly;
127
128 if (d->connectionOnly) {
129 nameLabel->hide();
130 nameCombo->hide();
131 btnLoadDBList->hide();
132 dbGroupBox->setTitle(i18n("Database Connection"));
133 }
134 else {
135 nameLabel->show();
136 nameCombo->show();
137#ifndef NO_LOAD_DB_LIST
138 btnLoadDBList->show();
139#endif
140 nameCombo->setCurrentText(m_data.databaseName());
141 dbGroupBox->setTitle(i18n("Database"));
142 }
144 m_driversCombo->setDriverName(m_data.connectionData()->driverName);
145 hostEdit->setText(m_data.connectionData()->hostName);
146 locationBGrp->setButton( m_data.connectionData()->hostName.isEmpty() ? 0 : 1 );
147 slotLocationBGrpClicked( locationBGrp->selectedId() );
148 if (m_data.connectionData()->port!=0) {
149 chkPortDefault->setChecked(false);
150 customPortEdit->setValue(m_data.connectionData()->port);
151 }
152 else {
153 chkPortDefault->setChecked(true);
154/* @todo default port # instead of 0 */
155 customPortEdit->setValue(0);
156 }
157 userEdit->setText(m_data.connectionData()->userName);
158 passwordEdit->setText(m_data.connectionData()->password);
159 if (d->connectionOnly)
160 titleEdit->setText(m_data.connectionData()->caption);
161 else
162 titleEdit->setText(m_data.caption());
163
164 if (shortcutFileName.isEmpty()) {
165 d->btnSaveChanges->hide();
166// chkSavePassword->hide();
167 }
168 else {
169 if (!TQFileInfo(shortcutFileName).isWritable()) {
170 d->btnSaveChanges->setEnabled(false);
171 }
172 }
173// chkSavePassword->setChecked(!m_data.connectionData()->password.isEmpty());
174 chkSavePassword->setChecked(m_data.connectionData()->savePassword);
175 adjustSize();
176}
177
178void KexiDBConnectionWidget::setData(const KexiProjectData& data, const TQString& shortcutFileName)
179{
180 setDataInternal(data, false , shortcutFileName);
181}
182
183void KexiDBConnectionWidget::setData(const KexiDB::ConnectionData& data, const TQString& shortcutFileName)
184{
185 KexiProjectData pdata(data);
186 setDataInternal(pdata, true /*connectionOnly*/, shortcutFileName);
187}
188
189KPushButton* KexiDBConnectionWidget::saveChangesButton() const
190{
191 return d->btnSaveChanges;
192}
193
194KPushButton* KexiDBConnectionWidget::testConnectionButton() const
195{
196 return d->btnTestConnection;
197}
198
199KexiProjectData KexiDBConnectionWidget::data()
200{
201 return m_data;
202}
203
204void KexiDBConnectionWidget::slotLocationBGrpClicked(int id)
205{
206 if (id != 0 && id != 1) //only support local/remove radio buttons
207 return;
208 hostLbl->setEnabled(id==1);
209 hostEdit->setEnabled(id==1);
210}
211
212void KexiDBConnectionWidget::slotCBToggled(bool on)
213{
214 if (sender()==chkPortDefault) {
215 customPortEdit->setEnabled(!on);
216 }
217// else if (sender()==chkSocketDefault) {
218// customSocketEdit->setEnabled(!on);
219// }
220}
221
222//-----------
223
224KexiDBConnectionTabWidget::KexiDBConnectionTabWidget( TQWidget* parent, const char* name )
225 : KTabWidget( parent, name )
226{
227 mainWidget = new KexiDBConnectionWidget( this, "mainWidget" );
228 mainWidget->layout()->setMargin(KDialog::marginHint());
229 addTab( mainWidget, i18n("Parameters") );
230
231// TQVBox *page2 = new TQVBox(this);
232// page2->setMargin(KDialog::marginHint());
233// page2->setSpacing(KDialog::spacingHint());
234// TQLabel *lbl = new TQLabel(i18n("&Description:"), page2);
235// m_descriptionEdit = new KTextEdit(page2);
236// lbl->setBuddy(m_descriptionEdit);
237 detailsWidget = new KexiDBConnectionWidgetDetailsBase(this, "detailsWidget");
238 addTab( detailsWidget, i18n("Details") );
239
240 connect( mainWidget->testConnectionButton(), TQ_SIGNAL(clicked()), this, TQ_SLOT(slotTestConnection()) );
241}
242
243KexiDBConnectionTabWidget::~KexiDBConnectionTabWidget()
244{
245}
246
247void KexiDBConnectionTabWidget::setData(const KexiProjectData& data, const TQString& shortcutFileName)
248{
249 mainWidget->setData( data, shortcutFileName );
250 detailsWidget->chkUseSocket->setChecked( data.constConnectionData()->useLocalSocketFile );
251 detailsWidget->customSocketEdit->setURL( data.constConnectionData()->localSocketFileName );
252 detailsWidget->customSocketEdit->setEnabled( detailsWidget->chkUseSocket->isChecked() );
253 detailsWidget->chkSocketDefault->setChecked( data.constConnectionData()->localSocketFileName.isEmpty() );
254 detailsWidget->chkSocketDefault->setEnabled( detailsWidget->chkUseSocket->isChecked() );
255 detailsWidget->descriptionEdit->setText( data.description() );
256}
257
258void KexiDBConnectionTabWidget::setData(const KexiDB::ConnectionData& data,
259 const TQString& shortcutFileName)
260{
261 mainWidget->setData( data, shortcutFileName );
262 detailsWidget->chkUseSocket->setChecked( data.useLocalSocketFile );
263 detailsWidget->customSocketEdit->setURL( data.localSocketFileName );
264 detailsWidget->customSocketEdit->setEnabled( detailsWidget->chkUseSocket->isChecked() );
265 detailsWidget->chkSocketDefault->setChecked( data.localSocketFileName.isEmpty() );
266 detailsWidget->chkSocketDefault->setEnabled( detailsWidget->chkUseSocket->isChecked() );
267 detailsWidget->descriptionEdit->setText( data.description );
268}
269
270KexiProjectData KexiDBConnectionTabWidget::currentProjectData()
271{
272 KexiProjectData data;
273
275
276 // collect data from the form's fields
277// if (d->isDatabaseShortcut) {
278 if (mainWidget->connectionOnly()) {
279 data.connectionData()->caption = mainWidget->titleEdit->text();
280 data.setCaption( TQString() );
281 data.connectionData()->description = detailsWidget->descriptionEdit->text();
282 data.setDatabaseName( TQString() );
283 }
284 else {
285 data.connectionData()->caption = TQString(); /* connection name is not specified... */
286 data.setCaption( mainWidget->titleEdit->text() );
287 data.setDescription( detailsWidget->descriptionEdit->text() );
288 data.setDatabaseName( mainWidget->nameCombo->currentText() );
289 }
290// }
291/* else {
292 data.setCaption( TQString() );
293 data.connectionData()->connName = config.readEntry("caption");
294 data.setDescription( TQString() );
295 data.connectionData()->description = config.readEntry("comment");
296 data.setDatabaseName( TQString() );
297 }*/
298 data.connectionData()->driverName = mainWidget->driversCombo()->selectedDriverName();
299
300/* if (data.connectionData()->driverName.isEmpty()) {
301 //ERR: "No valid "engine" field specified for %1 section" group
302 return false;
303 }*/
304 data.connectionData()->hostName =
305 (mainWidget->remotehostRBtn->isChecked()/*remote*/) ? mainWidget->hostEdit->text()
306 : TQString();
307 data.connectionData()->port = mainWidget->chkPortDefault->isChecked()
308 ? 0 : mainWidget->customPortEdit->value();
309 data.connectionData()->localSocketFileName = detailsWidget->chkSocketDefault->isChecked()
310 ? TQString() : detailsWidget->customSocketEdit->url();
311 data.connectionData()->useLocalSocketFile = detailsWidget->chkUseSocket->isChecked();
312//UNSAFE!!!!
313 data.connectionData()->userName = mainWidget->userEdit->text();
314 data.connectionData()->password = mainWidget->passwordEdit->text();
315 data.connectionData()->savePassword = mainWidget->chkSavePassword->isChecked();
316/* @todo add "options=", eg. as string list? */
317 return data;
318}
319
320bool KexiDBConnectionTabWidget::savePasswordOptionSelected() const
321{
322 return mainWidget->chkSavePassword->isChecked();
323}
324
325
326
327
328void KexiDBConnectionTabWidget::slotTestConnection()
329{
330 KexiGUIMessageHandler msgHandler;
331 KexiDB::connectionTestDialog(this, *currentProjectData().connectionData(),
332 msgHandler);
333}
334
335//--------
336
338
339KexiDBConnectionDialog::KexiDBConnectionDialog(const KexiProjectData& data,
340 const TQString& shortcutFileName, const KGuiItem& acceptButtonGuiItem)
341 : KDialogBase(0, "dlg", true, i18n("Open Database"),
342 KDialogBase::User1|KDialogBase::Cancel|KDialogBase::Help,
343 KDialogBase::User1, false,
344 acceptButtonGuiItem.text().isEmpty()
345 ? KGuiItem(i18n("&Open"), "document-open", i18n("Open Database Connection"))
346 : acceptButtonGuiItem
347 )
348{
349 tabWidget = new KexiDBConnectionTabWidget(this, "tabWidget");
350 tabWidget->setData(data, shortcutFileName);
351 init();
352}
353
354KexiDBConnectionDialog::KexiDBConnectionDialog(const KexiDB::ConnectionData& data,
355 const TQString& shortcutFileName, const KGuiItem& acceptButtonGuiItem)
356 : KDialogBase(0, "dlg", true, i18n("Connect to a Database Server"),
357 KDialogBase::User1|KDialogBase::Cancel|KDialogBase::Help,
358 KDialogBase::User1, false,
359 acceptButtonGuiItem.text().isEmpty()
360 ? KGuiItem(i18n("&Open"), "document-open", i18n("Open Database Connection"))
361 : acceptButtonGuiItem
362 )
363{
364 tabWidget = new KexiDBConnectionTabWidget(this, "tabWidget");
365 tabWidget->setData(data, shortcutFileName);
366 init();
367}
368
369KexiDBConnectionDialog::~KexiDBConnectionDialog()
370{
371}
372
373void KexiDBConnectionDialog::init()
374{
375 connect( this, TQ_SIGNAL(user1Clicked()), this, TQ_SLOT(accept()));
376 setMainWidget(tabWidget);
377 connect(tabWidget->mainWidget, TQ_SIGNAL(saveChanges()), this, TQ_SIGNAL(saveChanges()));
378 connect(tabWidget, TQ_SIGNAL(testConnection()), this, TQ_SIGNAL(testConnection()));
379
380 adjustSize();
381 resize(width(), tabWidget->height());
382 if (tabWidget->mainWidget->connectionOnly())
383 tabWidget->mainWidget->driversCombo()->setFocus();
384 else if (tabWidget->mainWidget->nameCombo->currentText().isEmpty())
385 tabWidget->mainWidget->nameCombo->setFocus();
386 else if (tabWidget->mainWidget->userEdit->text().isEmpty())
387 tabWidget->mainWidget->userEdit->setFocus();
388 else if (tabWidget->mainWidget->passwordEdit->text().isEmpty())
389 tabWidget->mainWidget->passwordEdit->setFocus();
390 else //back
391 tabWidget->mainWidget->nameCombo->setFocus();
392}
393
394KexiProjectData KexiDBConnectionDialog::currentProjectData()
395{ return tabWidget->currentProjectData(); }
396
397bool KexiDBConnectionDialog::savePasswordOptionSelected() const
398{ return tabWidget->savePasswordOptionSelected(); }
399
400KexiDBConnectionWidget* KexiDBConnectionDialog::mainWidget() const
401{ return tabWidget->mainWidget; }
402
403KexiDBConnectionWidgetDetailsBase* KexiDBConnectionDialog::detailsWidget() const
404{ return tabWidget->detailsWidget; }
405
406#include "kexidbconnectionwidget.moc"
407
Combo box widget for selecting a database driver.
bool savePassword
True if password should be saved to a file for the connection.
TQString caption
The caption of the connection.
TQString localSocketFileName
Name of local (named) socket file.
TQString description
The additional description for the connection.
TQString userName
Username used for the connection.
TQString password
Password used for the connection.
TQString driverName
the name of the driver that should be used to create a connection
unsigned short int port
Port used for the remote connection.
TQString hostName
Host name used for the remote connection.
bool useLocalSocketFile
True if local socket file should be used instead of TCP/IP port.
Database specific connection data, e.g. host, port.
Kexi project core data member.
KEXI_DB_EXPORT void connectionTestDialog(TQWidget *parent, const ConnectionData &data, MessageHandler &msgHandler)
KEXICORE_EXPORT KexiDB::DriverManager & driverManager()
shared driver manager
Definition kexi.cpp:91