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