summaryrefslogtreecommitdiffstats
path: root/kexi/widget/kexidbconnectionwidget.cpp
blob: f3a9f0ad7e69849ba8026c0c633af420cb42284b (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
/* This file is part of the KDE project
   Copyright (C) 2005 Jaroslaw Staniek <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kexidbconnectionwidget.h"
#include "kexidbconnectionwidgetdetailsbase.h"

#include <kexi.h>
#include <kexiguimsghandler.h>
#include <kexidb/connection.h>
#include <kexidb/utils.h>
#include "kexidbdrivercombobox.h"

#include <kdebug.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <knuminput.h>
#include <kpassdlg.h>
#include <kurlrequester.h>
#include <ktextedit.h>
#include <kprogress.h>

#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqbuttongroup.h>
#include <tqwidgetstack.h>
#include <tqlayout.h>
#include <tqvbox.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>
#include <tqthread.h>
#include <tqradiobutton.h>

//! Templorary hides db list
//! @todo reenable this when implemented
#define NO_LOAD_DB_LIST

// @internal
class KexiDBConnectionWidget::Private
{
	public:
		Private()
		 : connectionOnly(false)
		{
		}

		KPushButton *btnSaveChanges, *btnTestConnection;
		bool connectionOnly : 1;
};

//---------

KexiDBConnectionWidget::KexiDBConnectionWidget( TQWidget* parent,  const char* name )
 : KexiDBConnectionWidgetBase( parent, name )
 , d(new Private())
{
	iconLabel->setPixmap(DesktopIcon("network"));

	TQVBoxLayout *driversComboLyr = new TQVBoxLayout(frmEngine);
	m_driversCombo = new KexiDBDriverComboBox(frmEngine, Kexi::driverManager().driversInfo(), 
		KexiDBDriverComboBox::ShowServerDrivers);
	lblEngine->setBuddy( m_driversCombo );
	lblEngine->setFocusProxy( m_driversCombo );
	driversComboLyr->addWidget( m_driversCombo );

#ifdef NO_LOAD_DB_LIST
	btnLoadDBList->hide();
#endif
	btnLoadDBList->setIconSet(SmallIconSet("reload"));
	TQToolTip::add(btnLoadDBList, i18n("Load database list from the server"));
	TQWhatsThis::add(btnLoadDBList, 
		i18n("Loads database list from the server, so you can select one using the \"Name\" combo box."));

	TQHBoxLayout *hbox = new TQHBoxLayout(frmBottom);
	hbox->addStretch(2);
	d->btnSaveChanges = new KPushButton(KGuiItem(i18n("Save Changes"), "document-save", 
		i18n("Save all changes made to this connection information"),
		i18n("Save all changes made to this connection information. You can later reuse this information.")), 
		frmBottom, "savechanges");
	hbox->addWidget( d->btnSaveChanges );
	hbox->addSpacing( KDialogBase::spacingHint() );
	TQWidget::setTabOrder(titleEdit, d->btnSaveChanges);
	d->btnSaveChanges->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	d->btnTestConnection = new KPushButton(KGuiItem(i18n("&Test Connection"), "", 
		i18n("Test database connection"), 
		i18n("Tests database connection. You can ensure that valid connection information is provided.")), 
		frmBottom, "testConnection");
	hbox->addWidget( d->btnTestConnection );
	TQWidget::setTabOrder(d->btnSaveChanges, d->btnTestConnection);
	d->btnTestConnection->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	connect( locationBGrp, TQ_SIGNAL(clicked(int)), this, TQ_SLOT(slotLocationBGrpClicked(int)) );
	connect( chkPortDefault, TQ_SIGNAL(toggled(bool)), this , TQ_SLOT(slotCBToggled(bool)) );
	connect( btnLoadDBList, TQ_SIGNAL(clicked()), this, TQ_SIGNAL(loadDBList()) );
	connect( d->btnSaveChanges, TQ_SIGNAL(clicked()), this, TQ_SIGNAL(saveChanges()) );
}

KexiDBConnectionWidget::~KexiDBConnectionWidget()
{
	delete d;
}

bool KexiDBConnectionWidget::connectionOnly() const
{ return d->connectionOnly; }

void KexiDBConnectionWidget::setDataInternal(const KexiProjectData& data, bool connectionOnly, 
	const TQString& shortcutFileName)
{
	m_data = data;
	d->connectionOnly = connectionOnly;

	if (d->connectionOnly) {
		nameLabel->hide();
		nameCombo->hide();
		btnLoadDBList->hide();
		dbGroupBox->setTitle(i18n("Database Connection"));
	}
	else {
		nameLabel->show();
		nameCombo->show();
#ifndef NO_LOAD_DB_LIST
		btnLoadDBList->show();
#endif
		nameCombo->setCurrentText(m_data.databaseName());
		dbGroupBox->setTitle(i18n("Database"));
	}
//! @todo what if there's no such driver name?
	m_driversCombo->setDriverName(m_data.connectionData()->driverName);
	hostEdit->setText(m_data.connectionData()->hostName);
	locationBGrp->setButton( m_data.connectionData()->hostName.isEmpty() ? 0 : 1 );
	slotLocationBGrpClicked( locationBGrp->selectedId() );
	if (m_data.connectionData()->port!=0) {
		chkPortDefault->setChecked(false);
		customPortEdit->setValue(m_data.connectionData()->port);
	}
	else {
		chkPortDefault->setChecked(true);
/* @todo default port # instead of 0 */
		customPortEdit->setValue(0);
	}
	userEdit->setText(m_data.connectionData()->userName);
	passwordEdit->setText(m_data.connectionData()->password);
	if (d->connectionOnly)
		titleEdit->setText(m_data.connectionData()->caption);
	else
		titleEdit->setText(m_data.caption());

	if (shortcutFileName.isEmpty()) {
		d->btnSaveChanges->hide();
//		chkSavePassword->hide();
	}
	else {
		if (!TQFileInfo(shortcutFileName).isWritable()) {
			d->btnSaveChanges->setEnabled(false);
		}
	}
//	chkSavePassword->setChecked(!m_data.connectionData()->password.isEmpty());
	chkSavePassword->setChecked(m_data.connectionData()->savePassword);
	adjustSize();
}

void KexiDBConnectionWidget::setData(const KexiProjectData& data, const TQString& shortcutFileName)
{
	setDataInternal(data, false /*!connectionOnly*/, shortcutFileName);
}

void KexiDBConnectionWidget::setData(const KexiDB::ConnectionData& data, const TQString& shortcutFileName)
{
	KexiProjectData pdata(data);
	setDataInternal(pdata, true /*connectionOnly*/, shortcutFileName);
}

KPushButton* KexiDBConnectionWidget::saveChangesButton() const
{
	return d->btnSaveChanges;
}

KPushButton* KexiDBConnectionWidget::testConnectionButton() const
{
	return d->btnTestConnection;
}

KexiProjectData KexiDBConnectionWidget::data()
{
	return m_data;
}

void KexiDBConnectionWidget::slotLocationBGrpClicked(int id)
{
	if (id != 0 && id != 1) //only support local/remove radio buttons
		return;
	hostLbl->setEnabled(id==1);
	hostEdit->setEnabled(id==1);
}

void KexiDBConnectionWidget::slotCBToggled(bool on)
{
	if (sender()==chkPortDefault) {
		customPortEdit->setEnabled(!on);
	}
//	else if (sender()==chkSocketDefault) {
//		customSocketEdit->setEnabled(!on);
//	}
}

//-----------

KexiDBConnectionTabWidget::KexiDBConnectionTabWidget( TQWidget* parent, const char* name )
 : KTabWidget( parent, name )
{
	mainWidget = new KexiDBConnectionWidget( this, "mainWidget" );
	mainWidget->layout()->setMargin(KDialog::marginHint());
	addTab( mainWidget, i18n("Parameters") );

//	TQVBox *page2 = new TQVBox(this);
//	page2->setMargin(KDialog::marginHint());
//	page2->setSpacing(KDialog::spacingHint());
//	TQLabel *lbl = new TQLabel(i18n("&Description:"), page2);
//	m_descriptionEdit = new KTextEdit(page2);
//	lbl->setBuddy(m_descriptionEdit);
	detailsWidget = new KexiDBConnectionWidgetDetailsBase(this, "detailsWidget");
	addTab( detailsWidget, i18n("Details") );

	connect( mainWidget->testConnectionButton(), TQ_SIGNAL(clicked()), this, TQ_SLOT(slotTestConnection()) );
}

KexiDBConnectionTabWidget::~KexiDBConnectionTabWidget()
{
}

void KexiDBConnectionTabWidget::setData(const KexiProjectData& data, const TQString& shortcutFileName)
{
	mainWidget->setData( data, shortcutFileName );
	detailsWidget->chkUseSocket->setChecked( data.constConnectionData()->useLocalSocketFile );
	detailsWidget->customSocketEdit->setURL( data.constConnectionData()->localSocketFileName );
	detailsWidget->customSocketEdit->setEnabled( detailsWidget->chkUseSocket->isChecked() );
	detailsWidget->chkSocketDefault->setChecked( data.constConnectionData()->localSocketFileName.isEmpty() );
	detailsWidget->chkSocketDefault->setEnabled( detailsWidget->chkUseSocket->isChecked() );
	detailsWidget->descriptionEdit->setText( data.description() );
}

void KexiDBConnectionTabWidget::setData(const KexiDB::ConnectionData& data, 
	const TQString& shortcutFileName)
{
	mainWidget->setData( data, shortcutFileName );
	detailsWidget->chkUseSocket->setChecked( data.useLocalSocketFile );
	detailsWidget->customSocketEdit->setURL( data.localSocketFileName );
	detailsWidget->customSocketEdit->setEnabled( detailsWidget->chkUseSocket->isChecked() );
	detailsWidget->chkSocketDefault->setChecked( data.localSocketFileName.isEmpty() );
	detailsWidget->chkSocketDefault->setEnabled( detailsWidget->chkUseSocket->isChecked() );
	detailsWidget->descriptionEdit->setText( data.description );
}

KexiProjectData KexiDBConnectionTabWidget::currentProjectData()
{
	KexiProjectData data;

//! @todo check if that's database of connection shortcut. Now we're assuming db shortcut only!

	// collect data from the form's fields
//	if (d->isDatabaseShortcut) {
		if (mainWidget->connectionOnly()) {
			data.connectionData()->caption = mainWidget->titleEdit->text();
			data.setCaption( TQString() );
			data.connectionData()->description = detailsWidget->descriptionEdit->text();
			data.setDatabaseName( TQString() );
		}
		else {
			data.connectionData()->caption = TQString(); /* connection name is not specified... */
			data.setCaption( mainWidget->titleEdit->text() );
			data.setDescription( detailsWidget->descriptionEdit->text() );
			data.setDatabaseName( mainWidget->nameCombo->currentText() );
		}
//	}
/*	else {
		data.setCaption( TQString() );
		data.connectionData()->connName = config.readEntry("caption");
		data.setDescription( TQString() );
		data.connectionData()->description = config.readEntry("comment");
		data.setDatabaseName( TQString() );
	}*/
	data.connectionData()->driverName = mainWidget->driversCombo()->selectedDriverName();

/*	if (data.connectionData()->driverName.isEmpty()) {
		//ERR: "No valid "engine" field specified for %1 section" group
		return false;
	}*/
	data.connectionData()->hostName = 
		(mainWidget->remotehostRBtn->isChecked()/*remote*/) ? mainWidget->hostEdit->text()
		: TQString();
	data.connectionData()->port = mainWidget->chkPortDefault->isChecked() 
		? 0 : mainWidget->customPortEdit->value();
	data.connectionData()->localSocketFileName = detailsWidget->chkSocketDefault->isChecked() 
		? TQString() : detailsWidget->customSocketEdit->url();
	data.connectionData()->useLocalSocketFile = detailsWidget->chkUseSocket->isChecked();
//UNSAFE!!!!
	data.connectionData()->userName = mainWidget->userEdit->text();
	data.connectionData()->password = mainWidget->passwordEdit->text();
	data.connectionData()->savePassword = mainWidget->chkSavePassword->isChecked();
/* @todo add "options=", eg. as string list? */
	return data;
}

bool KexiDBConnectionTabWidget::savePasswordOptionSelected() const
{
	return mainWidget->chkSavePassword->isChecked();
}




void KexiDBConnectionTabWidget::slotTestConnection()
{
	KexiGUIMessageHandler msgHandler;
	KexiDB::connectionTestDialog(this, *currentProjectData().connectionData(), 
		msgHandler);
}

//--------

//! @todo set proper help ctxt ID

KexiDBConnectionDialog::KexiDBConnectionDialog(const KexiProjectData& data, 
	const TQString& shortcutFileName, const KGuiItem& acceptButtonGuiItem)
 : KDialogBase(0, "dlg", true, i18n("Open Database"), 
	KDialogBase::User1|KDialogBase::Cancel|KDialogBase::Help,
	KDialogBase::User1, false, 
	acceptButtonGuiItem.text().isEmpty() 
		? KGuiItem(i18n("&Open"), "document-open", i18n("Open Database Connection")) 
		: acceptButtonGuiItem
	)
{
	tabWidget = new KexiDBConnectionTabWidget(this, "tabWidget");
	tabWidget->setData(data, shortcutFileName);
	init();
}

KexiDBConnectionDialog::KexiDBConnectionDialog(const KexiDB::ConnectionData& data, 
	const TQString& shortcutFileName, const KGuiItem& acceptButtonGuiItem)
 : KDialogBase(0, "dlg", true, i18n("Connect to a Database Server"), 
	KDialogBase::User1|KDialogBase::Cancel|KDialogBase::Help,
	KDialogBase::User1, false, 
	acceptButtonGuiItem.text().isEmpty() 
		? KGuiItem(i18n("&Open"), "document-open", i18n("Open Database Connection"))
		: acceptButtonGuiItem
	)
{
	tabWidget = new KexiDBConnectionTabWidget(this, "tabWidget");
	tabWidget->setData(data, shortcutFileName);
	init();
}

KexiDBConnectionDialog::~KexiDBConnectionDialog()
{
}

void KexiDBConnectionDialog::init()
{
	connect( this, TQ_SIGNAL(user1Clicked()), this, TQ_SLOT(accept()));
	setMainWidget(tabWidget);
	connect(tabWidget->mainWidget, TQ_SIGNAL(saveChanges()), this, TQ_SIGNAL(saveChanges()));
	connect(tabWidget, TQ_SIGNAL(testConnection()), this, TQ_SIGNAL(testConnection()));

	adjustSize();
	resize(width(), tabWidget->height());
	if (tabWidget->mainWidget->connectionOnly())
		tabWidget->mainWidget->driversCombo()->setFocus();
	else if (tabWidget->mainWidget->nameCombo->currentText().isEmpty())
		tabWidget->mainWidget->nameCombo->setFocus();
	else if (tabWidget->mainWidget->userEdit->text().isEmpty())
		tabWidget->mainWidget->userEdit->setFocus();
	else if (tabWidget->mainWidget->passwordEdit->text().isEmpty())
		tabWidget->mainWidget->passwordEdit->setFocus();
	else //back
		tabWidget->mainWidget->nameCombo->setFocus();
}

KexiProjectData KexiDBConnectionDialog::currentProjectData()
{ return tabWidget->currentProjectData(); }

bool KexiDBConnectionDialog::savePasswordOptionSelected() const
{ return tabWidget->savePasswordOptionSelected(); }

KexiDBConnectionWidget* KexiDBConnectionDialog::mainWidget() const
{ return tabWidget->mainWidget; }

KexiDBConnectionWidgetDetailsBase* KexiDBConnectionDialog::detailsWidget() const
{ return tabWidget->detailsWidget; }

#include "kexidbconnectionwidget.moc"