kexi

KexiNewProjectWizard.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 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 "KexiNewProjectWizard.h"
21
22#include "KexiConnSelector.h"
23#include "KexiConnSelectorBase.h"
24#include "KexiNewPrjTypeSelector.h"
25#include "KexiOpenExistingFile.h"
26#include "KexiDBTitlePage.h"
27#include "KexiServerDBNamePage.h"
28#include "KexiProjectSelector.h"
29#include "kexi.h"
30
31#include <kexiutils/identifier.h>
32#include <kexiutils/utils.h>
33#include <kexiguimsghandler.h>
34
35#include <tdeapplication.h>
36#include <kiconloader.h>
37#include <kmimetype.h>
38#include <tdelocale.h>
39#include <kdebug.h>
40#include <tdeconfig.h>
41#include <tdelistview.h>
42#include <kurlcombobox.h>
43#include <tdemessagebox.h>
44#include <klineedit.h>
45
46#include <tqobjectlist.h>
47#include <tqlabel.h>
48#include <tqpushbutton.h>
49#include <tqlayout.h>
50#include <tqcheckbox.h>
51#include <tqheader.h>
52
54class KexiNewProjectWizardPrivate
55{
56 public:
57 KexiNewProjectWizardPrivate()
58 {
59 le_dbname_txtchanged_disable = false;
60 le_dbname_autofill = true;
61// conndata_to_show = 0;
62// project_set_to_show = 0;
63 }
64 ~KexiNewProjectWizardPrivate()
65 {
66// delete conndata_to_show;
67// delete project_set_to_show;
68 delete msgHandler;
69 }
70// TDEListView *lv_types;
71 TDEListViewItem *lvi_file, *lvi_server;
72 TQString chk_file_txt, chk_server_txt;
73
74 TQString server_db_name_dblist_lbl_txt;
75
76 //for displaying db list of the selected conn.
77 TQGuardedPtr<KexiDB::ConnectionData> conndata_to_show;
78 KexiProjectSet *project_set_to_show;
79
80 KexiGUIMessageHandler* msgHandler;
81
82 bool le_dbname_txtchanged_disable : 1;
83 bool le_dbname_autofill : 1;
84};
85
86KexiNewProjectWizard::KexiNewProjectWizard(KexiDBConnectionSet& conn_set,
87 TQWidget *parent, const char *name, bool modal, WFlags f)
88: KWizard(parent, name, modal, f)
89, d(new KexiNewProjectWizardPrivate() )
90{
91 d->msgHandler = new KexiGUIMessageHandler(this);
92 setIcon( DesktopIcon("document-new") );
93 setCaption( i18n("Creating New Project") );
94 finishButton()->setText(i18n("Create"));
95
96 //page: type selector
97 m_prjtype_sel = new KexiNewPrjTypeSelector(this, "KexiNewPrjTypeSelector");
98// lv_types = new TDEListView(m_prjtype_sel, "types listview");
99// m_prjtype_sel->lv_types->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Minimum, 0, 2));
100 m_prjtype_sel->lv_types->setShadeSortColumn(false);
101 m_prjtype_sel->lv_types->header()->hide();
102 m_prjtype_sel->lv_types->setSorting(-1);
103 m_prjtype_sel->lv_types->setAlternateBackground(TQColor()); //disable altering
104 m_prjtype_sel->lv_types->setItemMargin( KDialogBase::marginHint() );
105 TQString none;
106 d->lvi_file = new TDEListViewItem( m_prjtype_sel->lv_types, i18n("New Project Stored in File") );
107 d->lvi_file->setPixmap(0,
108 TDEGlobal::iconLoader()->loadIcon( KMimeType::mimeType(
109 KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0), TDEIcon::Desktop
110 )
111 );
112 d->lvi_file->setMultiLinesEnabled( true );
113 d->lvi_server = new TDEListViewItem( m_prjtype_sel->lv_types, d->lvi_file,
114 i18n("New Project Stored on Database Server") );
115 d->lvi_server->setPixmap(0, DesktopIcon("network") );
116 d->lvi_server->setMultiLinesEnabled( true );
117// m_prjtype_sel->lv_types->resize(d->m_prjtype_sel->lv_types->width(), d->lvi_file->height()*3);
118 m_prjtype_sel->lv_types->setFocus();
119// TQString txt_dns = i18n("Don't show me this question again.");
120// d->chk_file_txt = m_prjtype_sel->chk_always->text() +"\n"+txt_dns;
121// d->chk_server_txt = i18n("Always &use database server for creating new projects.")
122// +"\n"+txt_dns;
123
124 connect(m_prjtype_sel->lv_types,TQ_SIGNAL(executed(TQListViewItem*)),this,TQ_SLOT(slotLvTypesExecuted(TQListViewItem*)));
125 connect(m_prjtype_sel->lv_types,TQ_SIGNAL(returnPressed(TQListViewItem*)),this,TQ_SLOT(slotLvTypesExecuted(TQListViewItem*)));
126 connect(m_prjtype_sel->lv_types,TQ_SIGNAL(selectionChanged( TQListViewItem*)),this,TQ_SLOT(slotLvTypesSelected(TQListViewItem*)));
127
128// static_cast<TQVBoxLayout*>(m_prjtype_sel->layout())->insertWidget(1,d->m_prjtype_sel->lv_types);
129// static_cast<TQVBoxLayout*>(m_prjtype_sel->layout())->insertStretch(3,1);
130// updateGeometry();
131
132 addPage(m_prjtype_sel, i18n("Select Storage Method"));
133// d->m_prjtype_sel->lv_types->setMinimumHeight(TQMAX(d->lvi_file->height(),d->lvi_server->height())+25);
134
135 //page: db title
136 m_db_title = new KexiDBTitlePage(TQString(), this, "KexiDBTitlePage");
137 addPage(m_db_title, i18n("Select Project's Caption"));
138
139 //page: connection selector
140 m_conn_sel_widget = new TQWidget(this);
141 TQVBoxLayout* conn_sel_lyr = new TQVBoxLayout(m_conn_sel_widget);
142 TQLabel *conn_sel_label = new TQLabel(i18n("Enter a new Kexi project's file name:"), m_conn_sel_widget);
143 conn_sel_label->setAlignment(TQt::AlignAuto|TQt::AlignTop|TQt::WordBreak);
144 conn_sel_lyr->addWidget( conn_sel_label );
145 conn_sel_lyr->addSpacing(KDialogBase::spacingHint());
146
147 m_conn_sel = new KexiConnSelectorWidget(conn_set, ":OpenExistingOrCreateNewProject",
148 m_conn_sel_widget, "KexiConnSelectorWidget");
149 conn_sel_lyr->addWidget( m_conn_sel );
150
151 //"Select database server connection"
152// m_conn_sel->m_file->btn_advanced->hide();
153// m_conn_sel->m_file->label->hide();
154//TODO m_conn_sel->m_file->lbl->setText( i18n("Enter a new Kexi project's file name:") );
155 m_conn_sel->hideHelpers();
156
157 m_conn_sel->m_remote->label->setText(
158 i18n("Select database server's connection you wish to use to create a new Kexi project. "
159 "<p>Here you may also add, edit or remove connections from the list."));
160// m_conn_sel->m_remote->label_back->hide();
161// m_conn_sel->m_remote->btn_back->hide();
162
163 m_conn_sel->showSimpleConn();
164 //anyway, db files will be _saved_
165 m_conn_sel->m_fileDlg->setMode( KexiStartupFileDialog::SavingFileBasedDB );
166// m_conn_sel->m_fileDlg->setMode( KFile::LocalOnly | KFile::File );
167// m_conn_sel->m_fileDlg->setOperationMode( KFileDialog::Saving );
169// connect(m_conn_sel->m_fileDlg,TQ_SIGNAL(fileHighlighted(const TQString&)),this,TQ_SLOT(slotFileHighlighted(const TQString&)));
170 connect(m_conn_sel->m_fileDlg,TQ_SIGNAL(accepted()),this,TQ_SLOT(accept()));
171 m_conn_sel->showAdvancedConn();
172 connect(m_conn_sel,TQ_SIGNAL(connectionItemExecuted(ConnectionDataLVItem*)),
173 this,TQ_SLOT(next()));
174
175 addPage(m_conn_sel_widget, i18n("Select Project's Location"));
176
177 //page: server db name
178 m_server_db_name = new KexiServerDBNamePage(this, "KexiServerDBNamePage");
179 d->server_db_name_dblist_lbl_txt = i18n("Existing project databases on <b>%1</b> database server:");
180 connect(m_server_db_name->le_caption, TQ_SIGNAL(textChanged(const TQString&)),
181 this,TQ_SLOT(slotServerDBCaptionTxtChanged(const TQString&)));
182 connect(m_server_db_name->le_dbname, TQ_SIGNAL(textChanged(const TQString&)),
183 this,TQ_SLOT(slotServerDBNameTxtChanged(const TQString&)));
184 connect(m_server_db_name->le_caption, TQ_SIGNAL(returnPressed()),
185 this,TQ_SLOT(accept()));
186 connect(m_server_db_name->le_dbname, TQ_SIGNAL(returnPressed()),
187 this,TQ_SLOT(accept()));
188 m_server_db_name->le_caption->setText(i18n("New database"));
189 m_server_db_name->le_dbname->setValidator(new KexiUtils::IdentifierValidator(this, "id_val"));
190 m_project_selector = new KexiProjectSelectorWidget(
191 m_server_db_name->frm_dblist, "KexiProjectSelectorWidget", 0, false, false );
192 GLUE_WIDGET(m_project_selector, m_server_db_name->frm_dblist);
193 m_project_selector->setFocusPolicy(TQWidget::NoFocus);
194 m_project_selector->setSelectable(false);
195
196 addPage(m_server_db_name, i18n("Select Project's Caption & Database Name"));
197
198 setFinishEnabled(m_prjtype_sel,false);
199 setFinishEnabled(m_db_title,false);
200 setFinishEnabled(m_server_db_name,true);
201
202 //finish:
203 updateGeometry();
204 m_prjtype_sel->lv_types->setSelected(d->lvi_file, true);
205}
206
207KexiNewProjectWizard::~KexiNewProjectWizard()
208{
209 delete d;
210}
211
212void KexiNewProjectWizard::show()
213{
214 KDialog::centerOnScreen(this);
215 KWizard::show();
216}
217
218void KexiNewProjectWizard::slotLvTypesExecuted(TQListViewItem *)
219{
220 next();
221}
222
223void KexiNewProjectWizard::slotLvTypesSelected(TQListViewItem *item)
224{
225/* if (item==d->lvi_file) {
226 m_prjtype_sel->chk_always->setText(d->chk_file_txt);
227 }
228 else if (item==d->lvi_server) {
229 m_prjtype_sel->chk_always->setText(d->chk_server_txt);
230 }*/
231 setAppropriate( m_db_title, item==d->lvi_file );
232 setAppropriate( m_server_db_name, item==d->lvi_server );
233}
234
235void KexiNewProjectWizard::showPage(TQWidget *page)
236{
237 if (page==m_prjtype_sel) {//p 1
238 m_prjtype_sel->lv_types->setFocus();
239 m_prjtype_sel->lv_types->setCurrentItem(m_prjtype_sel->lv_types->currentItem());
240 } else if (page==m_db_title) {//p 2
241 if (m_db_title->le_caption->text().stripWhiteSpace().isEmpty())
242 m_db_title->le_caption->setText(i18n("New database"));
243 m_db_title->le_caption->selectAll();
244 m_db_title->le_caption->setFocus();
245 } else if (page==m_conn_sel_widget) {//p 3
246 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {
247 m_conn_sel->showSimpleConn();
248 TQString fn = KexiUtils::string2FileName( m_db_title->le_caption->text() );
249 if (!fn.endsWith(".kexi"))
250 fn += ".kexi";
251 m_conn_sel->m_fileDlg->setLocationText(fn);
252 setFinishEnabled(m_conn_sel_widget,true);
253 m_conn_sel->setFocus();
254 }
255 else {
256 m_conn_sel->showAdvancedConn();
257 setFinishEnabled(m_conn_sel_widget,false);
258 m_conn_sel->setFocus();
259 m_server_db_name->le_caption->selectAll();
260 }
261 } else if (page==m_server_db_name) {
262 if (m_conn_sel->selectedConnectionData()
263 && (static_cast<KexiDB::ConnectionData*>(d->conndata_to_show) != m_conn_sel->selectedConnectionData())) {
264 m_project_selector->setProjectSet(0);
265// delete d->project_set_to_show;
266 d->conndata_to_show = 0;
267 d->project_set_to_show = new KexiProjectSet(*m_conn_sel->selectedConnectionData(), d->msgHandler);
268 if (d->project_set_to_show->error()) {
269 delete d->project_set_to_show;
270 d->project_set_to_show = 0;
271 return;
272 }
273 d->conndata_to_show = m_conn_sel->selectedConnectionData();
274 //-refresh projects list
275 m_project_selector->setProjectSet( d->project_set_to_show );
276 }
277 }
278 KWizard::showPage(page);
279}
280
281void KexiNewProjectWizard::next()
282{
283 //let's check if move to next page is allowed:
284 if (currentPage()==m_db_title) { //pg 2
285 if (m_db_title->le_caption->text().stripWhiteSpace().isEmpty()) {
286 KMessageBox::information(this, i18n("Enter project caption."));
287 m_db_title->le_caption->setText("");
288 m_db_title->le_caption->setFocus();
289 return;
290 }
291 } else if (currentPage()==m_conn_sel_widget) {//p 3
292 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {
293 //test for db file selection
294 }
295 else {
296 //test for db conn selection
297 if (!m_conn_sel->selectedConnectionData()) {
298 KMessageBox::information(this, i18n("Select server connection for a new project."));
299 return;
300 }
301 m_project_selector->label->setText(
302 d->server_db_name_dblist_lbl_txt.arg(m_conn_sel->selectedConnectionData()->serverInfoString(false)) );
303 m_server_db_name->le_caption->setFocus();
304
305 }
306 }
307 KWizard::next();
308}
309
310void KexiNewProjectWizard::accept()
311{
312 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file) {//FILE:
313 //check if new db file name is ok
314 kdDebug() << "********** sender() " << sender()->className() << endl;
315 if (sender()==finishButton()) { /*(only if signal does not come from filedialog)*/
316 kdDebug() << "********** sender()==finishButton() ********" << endl;
317// if (!m_conn_sel->m_fileDlg->checkURL()) {
318 if (!m_conn_sel->m_fileDlg->checkFileName()) {
319 return;
320 }
321 }
322 } else {//SERVER:
323 //check if we have enough of data
324 if (m_server_db_name->le_caption->text().stripWhiteSpace().isEmpty()) {
325 KMessageBox::information(this, i18n("Enter project caption."));
326 m_server_db_name->le_caption->setText("");
327 m_server_db_name->le_caption->setFocus();
328 return;
329 }
330 TQString dbname = m_server_db_name->le_dbname->text().stripWhiteSpace();
331 if (dbname.isEmpty()) {
332 KMessageBox::information(this, i18n("Enter project's database name."));
333 m_server_db_name->le_dbname->setText("");
334 m_server_db_name->le_dbname->setFocus();
335 return;
336 }
337 //check for duplicated dbname
338 if (m_conn_sel->confirmOverwrites() && m_project_selector->projectSet() && m_project_selector->projectSet()
339 ->findProject( m_server_db_name->le_dbname->text() )) {
340 if (KMessageBox::Continue!=KMessageBox::warningContinueCancel( this, "<qt>"
341 +i18n("<b>A project with database name \"%1\" already exists</b>"
342 "<p>Do you want to delete it and create a new one?")
343 .arg( m_server_db_name->le_dbname->text() ), TQString(), KStdGuiItem::del(),
344 TQString(), KMessageBox::Notify|KMessageBox::Dangerous ))
345 {
346 m_server_db_name->le_dbname->setFocus();
347 return;
348 }
349 }
350 }
351
352 KWizard::accept();
353}
354
355void KexiNewProjectWizard::done(int r)
356{
357/* //save state (always, no matter if dialog is accepted or not)
358 TDEGlobal::config()->setGroup("Startup");
359 if (!m_prjtype_sel->chk_always->isChecked())
360 TDEGlobal::config()->deleteEntry("DefaultStorageForNewProjects");
361 else if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file)
362 TDEGlobal::config()->writeEntry("DefaultStorageForNewProjects","File");
363 else
364 TDEGlobal::config()->writeEntry("DefaultStorageForNewProjects","Server");*/
365
366 TDEGlobal::config()->sync();
367 KWizard::done(r);
368}
369
370TQString KexiNewProjectWizard::projectDBName() const
371{
372 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_server)
373 return m_server_db_name->le_dbname->text();
374 return m_conn_sel->selectedFileName();
375}
376
377TQString KexiNewProjectWizard::projectCaption() const
378{
379 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_server) {
380 return m_server_db_name->le_caption->text();
381 }
382 return m_db_title->le_caption->text();
383}
384
385KexiDB::ConnectionData* KexiNewProjectWizard::projectConnectionData() const
386{
387 if (m_prjtype_sel->lv_types->currentItem()==d->lvi_file)
388 return 0;
389 return m_conn_sel->selectedConnectionData();
390}
391
392void KexiNewProjectWizard::slotServerDBCaptionTxtChanged(const TQString &capt)
393{
394 if (m_server_db_name->le_dbname->text().isEmpty())
395 d->le_dbname_autofill=true;
396 if (d->le_dbname_autofill) {
397 d->le_dbname_txtchanged_disable = true;
398 TQString captionAsId = KexiUtils::string2Identifier(capt);
399 m_server_db_name->le_dbname->setText(captionAsId);
400 d->le_dbname_txtchanged_disable = false;
401 }
402}
403
404void KexiNewProjectWizard::slotServerDBNameTxtChanged(const TQString &)
405{
406 if (d->le_dbname_txtchanged_disable)
407 return;
408 d->le_dbname_autofill = false;
409}
410
413void KexiNewProjectWizard::setConfirmOverwrites(bool set)
414{
415 m_conn_sel->setConfirmOverwrites(set);
416}
417
418
419#include "KexiNewProjectWizard.moc"
420
A helper widget used to displaying a line edit with a label and layout.
Database specific connection data, e.g. host, port.
static TQString defaultFileBasedDriverMimeType()
Definition driver.cpp:137
Validates input for identifier name.
Definition identifier.h:49
KEXIUTILS_EXPORT TQString string2FileName(const TQString &s)
KEXIUTILS_EXPORT TQString string2Identifier(const TQString &s)