1/*
2 Copyright (C) 2010 Marco Mentasti <marcomentasti@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "connectionwizard.h"
20#include "sqlmanager.h"
21
22#include <klocale.h>
23#include <kcombobox.h>
24#include <kmessagebox.h>
25#include <klineedit.h>
26#include <kurlrequester.h>
27#include <kdebug.h>
28#include <knuminput.h>
29
30#include <qformlayout.h>
31#include <qsqldatabase.h>
32#include <qsqlerror.h>
33
34ConnectionWizard::ConnectionWizard ( SQLManager *manager, Connection *conn, QWidget *parent, Qt::WindowFlags flags )
35: QWizard(parent, flags)
36, m_manager(manager)
37, m_connection(conn)
38{
39 setWindowTitle(i18nc("@title:window", "Connection Wizard"));
40
41 setPage(Page_Driver, new ConnectionDriverPage);
42 setPage(Page_Standard_Server, new ConnectionStandardServerPage);
43 setPage(Page_SQLite_Server, new ConnectionSQLiteServerPage);
44 setPage(Page_Save, new ConnectionSavePage);
45}
46
47ConnectionWizard::~ConnectionWizard()
48{
49}
50
51ConnectionDriverPage::ConnectionDriverPage ( QWidget *parent)
52: QWizardPage(parent)
53{
54 setTitle(i18nc("@title Wizard page title", "Database Driver"));
55 setSubTitle(i18nc("@title Wizard page subtitle", "Select the database driver"));
56
57 QFormLayout *layout = new QFormLayout();
58
59 driverComboBox = new KComboBox();
60 driverComboBox->addItems(QSqlDatabase::drivers());
61
62 layout->addRow(i18nc("@label:listbox", "Database driver:"), driverComboBox);
63
64 setLayout(layout);
65
66 registerField("driver", driverComboBox, "currentText");
67}
68
69
70void ConnectionDriverPage::initializePage()
71{
72 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
73 Connection *c = wiz->connection();
74
75 if (!c->driver.isEmpty())
76 driverComboBox->setCurrentItem(c->driver);
77}
78
79int ConnectionDriverPage::nextId() const
80{
81 if (driverComboBox->currentText().contains("QSQLITE"))
82 return ConnectionWizard::Page_SQLite_Server;
83 else
84 return ConnectionWizard::Page_Standard_Server;
85}
86
87ConnectionStandardServerPage::ConnectionStandardServerPage ( QWidget *parent )
88: QWizardPage(parent)
89{
90 setTitle(i18nc("@title Wizard page title", "Connection Parameters"));
91 setSubTitle(i18nc("@title Wizard page subtitle", "Please enter connection parameters"));
92
93 QFormLayout *layout = new QFormLayout();
94
95 hostnameLineEdit = new KLineEdit();
96 usernameLineEdit = new KLineEdit();
97 passwordLineEdit = new KLineEdit();
98 databaseLineEdit = new KLineEdit();
99 optionsLineEdit = new KLineEdit();
100 portSpinBox = new KIntSpinBox();
101
102 portSpinBox->setMaximum(65535);
103 portSpinBox->setSpecialValueText(i18nc("@item Spinbox special value", "Default"));
104 portSpinBox->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
105
106 passwordLineEdit->setPasswordMode(true);
107
108 layout->addRow(i18nc("@label:textbox", "Hostname:"), hostnameLineEdit);
109 layout->addRow(i18nc("@label:textbox", "Username:"), usernameLineEdit);
110 layout->addRow(i18nc("@label:textbox", "Password:"), passwordLineEdit);
111 layout->addRow(i18nc("@label:spinbox", "Port:"), portSpinBox);
112 layout->addRow(i18nc("@label:textbox", "Database name:"), databaseLineEdit);
113 layout->addRow(i18nc("@label:textbox", "Connection options:"), optionsLineEdit);
114
115 setLayout(layout);
116
117 registerField("hostname*", hostnameLineEdit);
118 registerField("username" , usernameLineEdit);
119 registerField("password" , passwordLineEdit);
120 registerField("database" , databaseLineEdit);
121 registerField("stdOptions" , optionsLineEdit);
122 registerField("port" , portSpinBox);
123}
124
125
126ConnectionStandardServerPage::~ConnectionStandardServerPage()
127{
128}
129
130
131void ConnectionStandardServerPage::initializePage()
132{
133 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
134 Connection *c = wiz->connection();
135
136 hostnameLineEdit->setText("localhost");
137
138 if (c->driver == field("driver").toString())
139 {
140 hostnameLineEdit->setText(c->hostname);
141 usernameLineEdit->setText(c->username);
142 passwordLineEdit->setText(c->password);
143 databaseLineEdit->setText(c->database);
144 optionsLineEdit->setText(c->options);
145 portSpinBox->setValue(c->port);
146 }
147
148 hostnameLineEdit->selectAll();
149}
150
151
152bool ConnectionStandardServerPage::validatePage()
153{
154 Connection c;
155
156 c.driver = field("driver").toString();
157 c.hostname = field("hostname").toString();
158 c.username = field("username").toString();
159 c.password = field("password").toString();
160 c.database = field("database").toString();
161 c.options = field("stdOptions").toString();
162 c.port = field("port").toInt();
163
164 QSqlError e;
165
166 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
167
168 if (!wiz->manager()->testConnection(c, e))
169 {
170 KMessageBox::error(this, i18n("Unable to connect to database.") + "\n" + e.text());
171 return false;
172 }
173
174 return true;
175}
176
177int ConnectionStandardServerPage::nextId() const
178{
179 return ConnectionWizard::Page_Save;
180}
181
182ConnectionSQLiteServerPage::ConnectionSQLiteServerPage ( QWidget *parent)
183: QWizardPage(parent)
184{
185 setTitle(i18nc("@title Wizard page title", "Connection Parameters"));
186 setSubTitle(i18nc("@title Wizard page subtitle", "Please enter the SQLite database file path.\nIf the file does not exist, a new database will be created."));
187
188 QFormLayout *layout = new QFormLayout();
189
190 pathUrlRequester = new KUrlRequester(this);
191 optionsLineEdit = new KLineEdit();
192
193 pathUrlRequester->setMode( KFile::File);
194 pathUrlRequester->setFilter("*.db *.sqlite|" + i18n("Database files") + "\n*|" + i18n("All files"));
195
196 layout->addRow(i18nc("@label:textbox", "Path:"), pathUrlRequester);
197 layout->addRow(i18nc("@label:textbox", "Connection options:"), optionsLineEdit);
198
199 setLayout(layout);
200
201 registerField("path*", pathUrlRequester->lineEdit());
202 registerField("sqliteOptions", optionsLineEdit);
203}
204
205
206void ConnectionSQLiteServerPage::initializePage()
207{
208 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
209 Connection *c = wiz->connection();
210
211 if (c->driver == field("driver").toString())
212 {
213 pathUrlRequester->lineEdit()->setText(c->database);
214 optionsLineEdit->setText(c->options);
215 }
216}
217
218bool ConnectionSQLiteServerPage::validatePage()
219{
220 Connection c;
221
222 c.driver = field("driver").toString();
223 c.database = field("path").toString();
224 c.options = field("sqliteOptions").toString();
225
226 QSqlError e;
227
228 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
229
230 if (!wiz->manager()->testConnection(c, e))
231 {
232 KMessageBox::error(this, i18nc("@info", "Unable to connect to database.<nl/><message>%1</message>", e.text()));
233 return false;
234 }
235
236 return true;
237}
238
239int ConnectionSQLiteServerPage::nextId() const
240{
241 return ConnectionWizard::Page_Save;
242}
243
244ConnectionSavePage::ConnectionSavePage ( QWidget *parent)
245: QWizardPage(parent)
246{
247 setTitle(i18nc("@title Wizard page title", "Connection Name"));
248 setSubTitle(i18nc("@title Wizard page subtitle", "Enter a unique connection name"));
249
250 QFormLayout *layout = new QFormLayout();
251
252 connectionNameLineEdit = new KLineEdit();
253
254 layout->addRow(i18nc("@label:textbox", "Connection name:"), connectionNameLineEdit);
255
256 setLayout(layout);
257
258 registerField("connectionName*", connectionNameLineEdit);
259}
260
261void ConnectionSavePage::initializePage()
262{
263 QString name;
264
265 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
266 Connection *c = wiz->connection();
267
268 if (!c->name.isEmpty())
269 {
270 name = c->name;
271 }
272 else if (field("driver").toString().contains("QSQLITE"))
273 {
274 /// TODO: use db file basename
275 name = "SQLite";
276
277 for (int i = 1; QSqlDatabase::contains(name); i++)
278 name = QString("%1%2").arg("SQLite").arg(i);
279 }
280 else
281 {
282 name = QString("%1 on %2").arg(field("database").toString()).arg(field("hostname").toString()).simplified();
283
284 for (int i = 1; QSqlDatabase::contains(name); i++)
285 name = QString("%1 on %2 (%3)").arg(field("database").toString()).arg(field("hostname").toString()).arg(i).simplified();
286 }
287
288 connectionNameLineEdit->setText(name);
289 connectionNameLineEdit->selectAll();
290}
291
292
293bool ConnectionSavePage::validatePage()
294{
295 QString name = field("connectionName").toString().simplified();
296
297 ConnectionWizard *wiz = static_cast<ConnectionWizard*>(wizard());
298 Connection *c = wiz->connection();
299
300 c->name = name;
301 c->driver = field("driver").toString();
302
303 if (field("driver").toString().contains("QSQLITE"))
304 {
305 c->database = field("path").toString();
306 c->options = field("sqliteOptions").toString();
307 }
308 else
309 {
310 c->hostname = field("hostname").toString();
311 c->username = field("username").toString();
312 c->password = field("password").toString();
313 c->database = field("database").toString();
314 c->options = field("stdOptions").toString();
315 c->port = field("port").toInt();
316 }
317
318 return true;
319}
320
321int ConnectionSavePage::nextId() const
322{
323 return -1;
324}
325