fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dbaccess / source / ui / dlg / dbwizsetup.cxx
blobc71fe8871088df205056b2c507ffa972b37694a2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "dbwizsetup.hxx"
21 #include "dsmeta.hxx"
22 #include "DBSetupConnectionPages.hxx"
23 #include "dbu_dlg.hrc"
24 #include "dsitems.hxx"
25 #include "dsnItem.hxx"
27 #include <unotools/pathoptions.hxx>
28 #include <svl/stritem.hxx>
29 #include <svl/eitem.hxx>
30 #include <svl/intitem.hxx>
31 #include <vcl/msgbox.hxx>
32 #include "dbustrings.hrc"
33 #include "adminpages.hxx"
34 #include <sfx2/docfilt.hxx>
35 #include <unotools/ucbhelper.hxx>
36 #include "generalpage.hxx"
37 #include "localresaccess.hxx"
38 #include "stringlistitem.hxx"
39 #include "propertysetitem.hxx"
40 #include <unotools/confignode.hxx>
41 #include "DbAdminImpl.hxx"
42 #include "dbaccess_helpid.hrc"
43 #include "ConnectionPageSetup.hxx"
44 #include "UITools.hxx"
45 #include <dbaccess/AsynchronousLink.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <cppuhelper/exc_hlp.hxx>
49 #include <com/sun/star/frame/XStorable.hpp>
50 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
51 #include <com/sun/star/sdbc/XDataSource.hpp>
52 #include <com/sun/star/container/XNameAccess.hpp>
53 #include <com/sun/star/sdb/DatabaseContext.hpp>
54 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
55 #include <com/sun/star/frame/Desktop.hpp>
56 #include <com/sun/star/frame/FrameSearchFlag.hpp>
57 #include <com/sun/star/frame/XComponentLoader.hpp>
58 #include <com/sun/star/frame/XModel.hpp>
59 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
60 #include <com/sun/star/lang/XInitialization.hpp>
61 #include <com/sun/star/sdb/CommandType.hpp>
62 #include <com/sun/star/ucb/InteractiveIOException.hpp>
63 #include <com/sun/star/io/IOException.hpp>
64 #include <com/sun/star/frame/XTerminateListener.hpp>
65 #include <com/sun/star/sdbc/XDriverAccess.hpp>
66 #include <com/sun/star/document/MacroExecMode.hpp>
67 #include <com/sun/star/ucb/IOErrorCode.hpp>
68 #include <com/sun/star/task/InteractionHandler.hpp>
69 #include <com/sun/star/task/XInteractionHandler2.hpp>
70 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
72 #include <svl/filenotation.hxx>
73 #include <comphelper/interaction.hxx>
74 #include <comphelper/namedvaluecollection.hxx>
75 #include <comphelper/processfactory.hxx>
76 #include <comphelper/sequenceashashmap.hxx>
77 #include <tools/diagnose_ex.h>
78 #include <osl/diagnose.h>
79 #include <connectivity/DriversConfig.hxx>
81 namespace dbaui
83 using namespace dbtools;
84 using namespace svt;
85 using namespace com::sun::star;
86 using namespace com::sun::star::uno;
87 using namespace com::sun::star::sdbc;
88 using namespace com::sun::star::sdbcx;
89 using namespace com::sun::star::task;
90 using namespace com::sun::star::lang;
91 using namespace com::sun::star::io;
92 using namespace com::sun::star::util;
93 using namespace com::sun::star::beans;
94 using namespace com::sun::star::container;
95 using namespace com::sun::star::frame;
96 using namespace com::sun::star::ucb;
97 using namespace ::com::sun::star::sdb;
98 using namespace ::com::sun::star::document;
99 using namespace ::comphelper;
100 using namespace ::cppu;
102 // ODbTypeWizDialogSetup
103 ODbTypeWizDialogSetup::ODbTypeWizDialogSetup(vcl::Window* _pParent
104 ,SfxItemSet* _pItems
105 ,const Reference< XComponentContext >& _rxORB
106 ,const ::com::sun::star::uno::Any& _aDataSourceName
108 :svt::RoadmapWizard( _pParent, WizardButtonFlags::NEXT | WizardButtonFlags::PREVIOUS | WizardButtonFlags::FINISH | WizardButtonFlags::CANCEL | WizardButtonFlags::HELP )
110 , m_pOutSet(NULL)
111 , m_bResetting(false)
112 , m_bApplied(false)
113 , m_bUIEnabled( true )
114 , m_bIsConnectable( false)
115 , m_sRM_IntroText( ModuleRes( STR_PAGETITLE_INTROPAGE ) )
116 , m_sRM_dBaseText( ModuleRes( STR_PAGETITLE_DBASE ) )
117 , m_sRM_TextText( ModuleRes( STR_PAGETITLE_TEXT ) )
118 , m_sRM_MSAccessText( ModuleRes( STR_PAGETITLE_MSACCESS ) )
119 , m_sRM_LDAPText( ModuleRes( STR_PAGETITLE_LDAP ) )
120 , m_sRM_ADOText( ModuleRes( STR_PAGETITLE_ADO ) )
121 , m_sRM_JDBCText( ModuleRes( STR_PAGETITLE_JDBC ) )
122 , m_sRM_MySQLNativePageTitle( ModuleRes( STR_PAGETITLE_MYSQL_NATIVE ) )
123 , m_sRM_OracleText( ModuleRes( STR_PAGETITLE_ORACLE ) )
124 , m_sRM_MySQLText( ModuleRes( STR_PAGETITLE_MYSQL ) )
125 , m_sRM_ODBCText( ModuleRes( STR_PAGETITLE_ODBC ) )
126 , m_sRM_SpreadSheetText( ModuleRes( STR_PAGETITLE_SPREADSHEET ) )
127 , m_sRM_AuthentificationText( ModuleRes( STR_PAGETITLE_AUTHENTIFICATION ) )
128 , m_sRM_FinalText( ModuleRes( STR_PAGETITLE_FINAL ) )
129 , m_sWorkPath( SvtPathOptions().GetWorkPath() )
130 , m_pGeneralPage( NULL )
131 , m_pMySQLIntroPage( NULL )
132 , m_pFinalPage( NULL )
133 , m_pCollection( NULL )
135 // no local resources needed anymore
136 // extract the datasource type collection from the item set
137 const DbuTypeCollectionItem* pCollectionItem = PTR_CAST(DbuTypeCollectionItem, _pItems->GetItem(DSID_TYPECOLLECTION));
138 if (pCollectionItem)
139 m_pCollection = pCollectionItem->getCollection();
141 OSL_ENSURE(m_pCollection, "ODbTypeWizDialogSetup::ODbTypeWizDialogSetup : really need a DSN type collection !");
143 m_pImpl.reset(new ODbDataSourceAdministrationHelper(_rxORB,this,this));
144 m_pImpl->setDataSourceOrName(_aDataSourceName);
145 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
146 m_pOutSet = new SfxItemSet( *_pItems->GetPool(), _pItems->GetRanges() );
148 m_pImpl->translateProperties(xDatasource, *m_pOutSet);
150 SetPageSizePixel(LogicToPixel(::Size(WIZARD_PAGE_X, WIZARD_PAGE_Y), MAP_APPFONT));
151 defaultButton(WizardButtonFlags::NEXT);
152 enableButtons(WizardButtonFlags::FINISH, true);
153 enableAutomaticNextButtonState();
155 ::dbaccess::ODsnTypeCollection::TypeIterator aIter = m_pCollection->begin();
156 ::dbaccess::ODsnTypeCollection::TypeIterator aEnd = m_pCollection->end();
157 for(PathId i = 1;aIter != aEnd;++aIter,++i)
159 const OUString sURLPrefix = aIter.getURLPrefix();
160 svt::RoadmapWizardTypes::WizardPath aPath;
161 aPath.push_back(PAGE_DBSETUPWIZARD_INTRO);
162 m_pCollection->fillPageIds(sURLPrefix,aPath);
163 aPath.push_back(PAGE_DBSETUPWIZARD_AUTHENTIFICATION);
164 aPath.push_back(PAGE_DBSETUPWIZARD_FINAL);
166 declareAuthDepPath(sURLPrefix,i,aPath);
169 svt::RoadmapWizardTypes::WizardPath aPath;
170 aPath.push_back(PAGE_DBSETUPWIZARD_INTRO);
171 declarePath( static_cast<PathId>(m_pCollection->size()+1), aPath);
173 m_pPrevPage->SetHelpId(HID_DBWIZ_PREVIOUS);
174 m_pNextPage->SetHelpId(HID_DBWIZ_NEXT);
175 m_pCancel->SetHelpId(HID_DBWIZ_CANCEL);
176 m_pFinish->SetHelpId(HID_DBWIZ_FINISH);
177 m_pHelp->SetUniqueId(UID_DBWIZ_HELP);
178 SetRoadmapInteractive( true );
179 ActivatePage();
180 setTitleBase(ModuleRes(STR_DBWIZARDTITLE));
183 void ODbTypeWizDialogSetup::declareAuthDepPath( const OUString& _sURL, PathId _nPathId, const svt::RoadmapWizardTypes::WizardPath& _rPaths)
185 bool bHasAuthentication = DataSourceMetaData::getAuthentication( _sURL ) != AuthNone;
187 // collect the elements of the path
188 WizardPath aPath;
190 svt::RoadmapWizardTypes::WizardPath::const_iterator aIter = _rPaths.begin();
191 svt::RoadmapWizardTypes::WizardPath::const_iterator aEnd = _rPaths.end();
192 for(;aIter != aEnd;++aIter)
194 if ( bHasAuthentication || ( *aIter != PAGE_DBSETUPWIZARD_AUTHENTIFICATION ) )
195 aPath.push_back( *aIter );
198 // call base method
199 ::svt::RoadmapWizard::declarePath( _nPathId, aPath );
202 OUString ODbTypeWizDialogSetup::getStateDisplayName( WizardState _nState ) const
204 OUString sRoadmapItem;
205 switch( _nState )
207 case PAGE_DBSETUPWIZARD_INTRO:
208 sRoadmapItem = m_sRM_IntroText;
209 break;
211 case PAGE_DBSETUPWIZARD_DBASE:
212 sRoadmapItem = m_sRM_dBaseText;
213 break;
214 case PAGE_DBSETUPWIZARD_ADO:
215 sRoadmapItem = m_sRM_ADOText;
216 break;
217 case PAGE_DBSETUPWIZARD_TEXT:
218 sRoadmapItem = m_sRM_TextText;
219 break;
220 case PAGE_DBSETUPWIZARD_MSACCESS:
221 sRoadmapItem = m_sRM_MSAccessText;
222 break;
223 case PAGE_DBSETUPWIZARD_LDAP:
224 sRoadmapItem = m_sRM_LDAPText;
225 break;
226 case PAGE_DBSETUPWIZARD_JDBC:
227 sRoadmapItem = m_sRM_JDBCText;
228 break;
229 case PAGE_DBSETUPWIZARD_ORACLE:
230 sRoadmapItem = m_sRM_OracleText;
231 break;
232 case PAGE_DBSETUPWIZARD_MYSQL_INTRO:
233 sRoadmapItem = m_sRM_MySQLText;
234 break;
235 case PAGE_DBSETUPWIZARD_MYSQL_JDBC:
236 sRoadmapItem = m_sRM_JDBCText;
237 break;
238 case PAGE_DBSETUPWIZARD_MYSQL_NATIVE:
239 sRoadmapItem = m_sRM_MySQLNativePageTitle;
240 break;
241 case PAGE_DBSETUPWIZARD_MYSQL_ODBC:
242 sRoadmapItem = m_sRM_ODBCText;
243 break;
244 case PAGE_DBSETUPWIZARD_ODBC:
245 sRoadmapItem = m_sRM_ODBCText;
246 break;
247 case PAGE_DBSETUPWIZARD_SPREADSHEET:
248 sRoadmapItem = m_sRM_SpreadSheetText;
249 break;
250 case PAGE_DBSETUPWIZARD_AUTHENTIFICATION:
251 sRoadmapItem = m_sRM_AuthentificationText;
252 break;
253 case PAGE_DBSETUPWIZARD_USERDEFINED:
254 sRoadmapItem = ModuleRes(STR_PAGETITLE_CONNECTION);
255 break;
256 case PAGE_DBSETUPWIZARD_FINAL:
257 sRoadmapItem = m_sRM_FinalText;
258 break;
259 default:
260 break;
262 return sRoadmapItem;
265 ODbTypeWizDialogSetup::~ODbTypeWizDialogSetup()
267 disposeOnce();
270 void ODbTypeWizDialogSetup::dispose()
272 delete m_pOutSet;
273 m_pOutSet = NULL;
274 m_pGeneralPage.clear();
275 m_pMySQLIntroPage.clear();
276 m_pFinalPage.clear();
277 svt::RoadmapWizard::dispose();
280 IMPL_LINK(ODbTypeWizDialogSetup, OnTypeSelected, OGeneralPage*, /*_pTabPage*/)
282 activateDatabasePath();
283 return 1L;
286 void lcl_removeUnused(const ::comphelper::NamedValueCollection& _aOld,const ::comphelper::NamedValueCollection& _aNew,::comphelper::NamedValueCollection& _rDSInfo)
288 _rDSInfo.merge(_aNew,true);
289 uno::Sequence< beans::NamedValue > aOldValues = _aOld.getNamedValues();
290 const beans::NamedValue* pIter = aOldValues.getConstArray();
291 const beans::NamedValue* pEnd = pIter + aOldValues.getLength();
292 for(;pIter != pEnd;++pIter)
294 if ( !_aNew.has(pIter->Name) )
296 _rDSInfo.remove(pIter->Name);
301 void DataSourceInfoConverter::convert(const Reference<XComponentContext> & xContext, const ::dbaccess::ODsnTypeCollection* _pCollection,const OUString& _sOldURLPrefix,const OUString& _sNewURLPrefix,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _xDatasource)
303 if ( _pCollection->getPrefix(_sOldURLPrefix) == _pCollection->getPrefix(_sNewURLPrefix) )
304 return ;
305 uno::Sequence< beans::PropertyValue> aInfo;
306 _xDatasource->getPropertyValue(PROPERTY_INFO) >>= aInfo;
307 ::comphelper::NamedValueCollection aDS(aInfo);
309 ::connectivity::DriversConfig aDriverConfig(xContext);
311 const ::comphelper::NamedValueCollection& aOldProperties = aDriverConfig.getProperties(_sOldURLPrefix);
312 const ::comphelper::NamedValueCollection& aNewProperties = aDriverConfig.getProperties(_sNewURLPrefix);
313 lcl_removeUnused(aOldProperties,aNewProperties,aDS);
315 aDS >>= aInfo;
316 _xDatasource->setPropertyValue(PROPERTY_INFO,uno::makeAny(aInfo));
319 void ODbTypeWizDialogSetup::activateDatabasePath()
321 switch ( m_pGeneralPage->GetDatabaseCreationMode() )
323 case OGeneralPageWizard::eCreateNew:
325 sal_Int32 nCreateNewDBIndex = m_pCollection->getIndexOf( m_pGeneralPage->GetSelectedType() );
326 if ( nCreateNewDBIndex == -1 )
327 nCreateNewDBIndex = m_pCollection->getIndexOf( OUString("sdbc:dbase:") );
328 OSL_ENSURE( nCreateNewDBIndex != -1, "ODbTypeWizDialogSetup::activateDatabasePath: the GeneralPage should have prevented this!" );
329 activatePath( static_cast< PathId >( nCreateNewDBIndex + 1 ), true );
331 enableState(PAGE_DBSETUPWIZARD_FINAL, true );
332 enableButtons( WizardButtonFlags::FINISH, true);
334 break;
335 case OGeneralPageWizard::eConnectExternal:
337 OUString sOld = m_sURL;
338 m_sURL = m_pGeneralPage->GetSelectedType();
339 DataSourceInfoConverter::convert(getORB(), m_pCollection,sOld,m_sURL,m_pImpl->getCurrentDataSource());
340 ::dbaccess::DATASOURCE_TYPE eType = VerifyDataSourceType(m_pCollection->determineType(m_sURL));
341 if (eType == ::dbaccess::DST_UNKNOWN)
342 eType = m_pCollection->determineType(m_sOldURL);
344 activatePath( static_cast<PathId>(m_pCollection->getIndexOf(m_sURL) + 1), true);
345 updateTypeDependentStates();
347 break;
348 case OGeneralPageWizard::eOpenExisting:
350 activatePath( static_cast<PathId>(m_pCollection->size() + 1), true );
351 enableButtons( WizardButtonFlags::FINISH, !m_pGeneralPage->GetSelectedDocument().sURL.isEmpty() );
353 break;
354 default:
355 OSL_FAIL( "ODbTypeWizDialogSetup::activateDatabasePath: unknown creation mode!" );
358 enableButtons( WizardButtonFlags::NEXT, m_pGeneralPage->GetDatabaseCreationMode() != OGeneralPageWizard::eOpenExisting );
359 // TODO: this should go into the base class. Point is, we activate a path whose *last*
360 // step is also the current one. The base class should automatically disable
361 // the Next button in such a case. However, not for this patch ...
364 void ODbTypeWizDialogSetup::updateTypeDependentStates()
366 bool bDoEnable = false;
367 bool bIsConnectionRequired = IsConnectionUrlRequired();
368 if (!bIsConnectionRequired)
370 bDoEnable = true;
372 else if ( m_sURL == m_sOldURL )
374 bDoEnable = m_bIsConnectable;
376 enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION, bDoEnable);
377 enableState(PAGE_DBSETUPWIZARD_FINAL, bDoEnable );
378 enableButtons( WizardButtonFlags::FINISH, bDoEnable);
381 bool ODbTypeWizDialogSetup::IsConnectionUrlRequired()
383 return m_pCollection->isConnectionUrlRequired(m_sURL);
386 void ODbTypeWizDialogSetup::resetPages(const Reference< XPropertySet >& _rxDatasource)
388 // remove all items which relate to indirect properties from the input set
389 // (without this, the following may happen: select an arbitrary data source where some indirect properties
390 // are set. Select another data source of the same type, where the indirect props are not set (yet). Then,
391 // the indirect property values of the first ds are shown in the second ds ...)
392 const ODbDataSourceAdministrationHelper::MapInt2String& rMap = m_pImpl->getIndirectProperties();
393 for ( ODbDataSourceAdministrationHelper::MapInt2String::const_iterator aIndirect = rMap.begin();
394 aIndirect != rMap.end();
395 ++aIndirect
397 getWriteOutputSet()->ClearItem( (sal_uInt16)aIndirect->first );
399 // extract all relevant data from the property set of the data source
400 m_pImpl->translateProperties(_rxDatasource, *getWriteOutputSet());
403 const SfxItemSet* ODbTypeWizDialogSetup::getOutputSet() const
405 return m_pOutSet;
408 SfxItemSet* ODbTypeWizDialogSetup::getWriteOutputSet()
410 return m_pOutSet;
413 ::std::pair< Reference<XConnection>,sal_Bool> ODbTypeWizDialogSetup::createConnection()
415 return m_pImpl->createConnection();
418 Reference< XComponentContext > ODbTypeWizDialogSetup::getORB() const
420 return m_pImpl->getORB();
423 Reference< XDriver > ODbTypeWizDialogSetup::getDriver()
425 return m_pImpl->getDriver();
428 ::dbaccess::DATASOURCE_TYPE ODbTypeWizDialogSetup::VerifyDataSourceType(const ::dbaccess::DATASOURCE_TYPE _DatabaseType) const
430 ::dbaccess::DATASOURCE_TYPE LocDatabaseType = _DatabaseType;
431 if ((LocDatabaseType == ::dbaccess::DST_MYSQL_JDBC) || (LocDatabaseType == ::dbaccess::DST_MYSQL_ODBC) || (LocDatabaseType == ::dbaccess::DST_MYSQL_NATIVE))
433 if (m_pMySQLIntroPage != nullptr)
435 switch( m_pMySQLIntroPage->getMySQLMode() )
437 case OMySQLIntroPageSetup::VIA_JDBC:
438 return ::dbaccess::DST_MYSQL_JDBC;
439 case OMySQLIntroPageSetup::VIA_NATIVE:
440 return ::dbaccess::DST_MYSQL_NATIVE;
441 case OMySQLIntroPageSetup::VIA_ODBC:
442 return ::dbaccess::DST_MYSQL_ODBC;
446 return LocDatabaseType;
449 OUString ODbTypeWizDialogSetup::getDatasourceType(const SfxItemSet& _rSet) const
451 OUString sRet = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(_rSet);
452 if (m_pMySQLIntroPage != nullptr && m_pMySQLIntroPage->IsVisible() )
454 switch( m_pMySQLIntroPage->getMySQLMode() )
456 case OMySQLIntroPageSetup::VIA_JDBC:
457 sRet = "sdbc:mysql:jdbc:";
458 break;
459 case OMySQLIntroPageSetup::VIA_NATIVE:
460 sRet = "sdbc:mysql:mysqlc:";
461 break;
462 case OMySQLIntroPageSetup::VIA_ODBC:
463 sRet = "sdbc:mysql:odbc:";
464 break;
467 return sRet;
470 void ODbTypeWizDialogSetup::clearPassword()
472 m_pImpl->clearPassword();
475 VclPtr<TabPage> ODbTypeWizDialogSetup::createPage(WizardState _nState)
477 VclPtr<SfxTabPage> pFirstPage;
478 VclPtr<OGenericAdministrationPage> pPage;
479 switch(_nState)
481 case PAGE_DBSETUPWIZARD_INTRO:
482 pFirstPage = VclPtr<OGeneralPageWizard>::Create(this,*m_pOutSet);
483 pPage = static_cast<OGenericAdministrationPage*> (pFirstPage.get());
484 m_pGeneralPage = static_cast<OGeneralPageWizard*>(pFirstPage.get());
485 m_pGeneralPage->SetTypeSelectHandler(LINK(this, ODbTypeWizDialogSetup, OnTypeSelected));
486 m_pGeneralPage->SetCreationModeHandler(LINK( this, ODbTypeWizDialogSetup, OnChangeCreationMode ) );
487 m_pGeneralPage->SetDocumentSelectionHandler(LINK( this, ODbTypeWizDialogSetup, OnRecentDocumentSelected ) );
488 m_pGeneralPage->SetChooseDocumentHandler(LINK( this, ODbTypeWizDialogSetup, OnSingleDocumentChosen ) );
489 break;
491 case PAGE_DBSETUPWIZARD_DBASE:
492 pPage = OConnectionTabPageSetup::CreateDbaseTabPage(this,*m_pOutSet);
493 break;
495 case PAGE_DBSETUPWIZARD_ADO:
496 pPage = OConnectionTabPageSetup::CreateADOTabPage( this, *m_pOutSet);
497 break;
499 case PAGE_DBSETUPWIZARD_TEXT:
500 pPage = OTextConnectionPageSetup::CreateTextTabPage(this,*m_pOutSet);
501 break;
503 case PAGE_DBSETUPWIZARD_ODBC:
504 pPage = OConnectionTabPageSetup::CreateODBCTabPage( this, *m_pOutSet);
505 break;
507 case PAGE_DBSETUPWIZARD_JDBC:
508 pPage = OJDBCConnectionPageSetup::CreateJDBCTabPage( this, *m_pOutSet);
509 break;
511 case PAGE_DBSETUPWIZARD_MYSQL_ODBC:
512 m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix(OUString("sdbc:mysql:odbc:"))));
513 pPage = OConnectionTabPageSetup::CreateODBCTabPage( this, *m_pOutSet);
514 break;
516 case PAGE_DBSETUPWIZARD_MYSQL_JDBC:
517 m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix(OUString("sdbc:mysql:jdbc:"))));
518 pPage = OGeneralSpecialJDBCConnectionPageSetup::CreateMySQLJDBCTabPage( this, *m_pOutSet);
519 break;
520 case PAGE_DBSETUPWIZARD_MYSQL_NATIVE:
521 m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix(OUString("sdbc:mysql:mysqlc:"))));
522 pPage = MySQLNativeSetupPage::Create( this, *m_pOutSet);
523 break;
525 case PAGE_DBSETUPWIZARD_ORACLE:
526 pPage = OGeneralSpecialJDBCConnectionPageSetup::CreateOracleJDBCTabPage( this, *m_pOutSet);
527 break;
529 case PAGE_DBSETUPWIZARD_LDAP:
530 pPage = OLDAPConnectionPageSetup::CreateLDAPTabPage(this,*m_pOutSet);
531 break;
533 case PAGE_DBSETUPWIZARD_SPREADSHEET: /// first user defined driver
534 pPage = OSpreadSheetConnectionPageSetup::CreateSpreadSheetTabPage(this,*m_pOutSet);
535 break;
537 case PAGE_DBSETUPWIZARD_MSACCESS:
538 pPage = OConnectionTabPageSetup::CreateMSAccessTabPage(this,*m_pOutSet);
539 break;
540 case PAGE_DBSETUPWIZARD_MYSQL_INTRO:
541 m_pMySQLIntroPage = OMySQLIntroPageSetup::CreateMySQLIntroTabPage(this,*m_pOutSet);
542 m_pMySQLIntroPage->SetClickHdl(LINK( this, ODbTypeWizDialogSetup, ImplClickHdl ) );
543 pPage = m_pMySQLIntroPage;
544 break;
546 case PAGE_DBSETUPWIZARD_AUTHENTIFICATION:
547 pPage = OAuthentificationPageSetup::CreateAuthentificationTabPage(this,*m_pOutSet);
548 break;
550 case PAGE_DBSETUPWIZARD_USERDEFINED:
551 pPage = OConnectionTabPageSetup::CreateUserDefinedTabPage(this,*m_pOutSet);
552 break;
554 case PAGE_DBSETUPWIZARD_FINAL:
555 pPage = OFinalDBPageSetup::CreateFinalDBTabPageSetup(this,*m_pOutSet);
556 m_pFinalPage = static_cast<OFinalDBPageSetup*> (pPage.get());
557 break;
560 if ( pPage )
562 if ((_nState != PAGE_DBSETUPWIZARD_INTRO) && (_nState != PAGE_DBSETUPWIZARD_AUTHENTIFICATION))
564 pPage->SetModifiedHandler(LINK( this, ODbTypeWizDialogSetup, ImplModifiedHdl ) );
567 pPage->SetServiceFactory( m_pImpl->getORB() );
568 pPage->SetAdminDialog(this, this);
570 defaultButton( _nState == PAGE_DBSETUPWIZARD_FINAL ? WizardButtonFlags::FINISH : WizardButtonFlags::NEXT );
571 enableButtons( WizardButtonFlags::FINISH, _nState == PAGE_DBSETUPWIZARD_FINAL );
572 enableButtons( WizardButtonFlags::NEXT, _nState != PAGE_DBSETUPWIZARD_FINAL );
573 pPage->Show();
575 return pPage;
578 IMPL_LINK_TYPED(ODbTypeWizDialogSetup, ImplModifiedHdl, OGenericAdministrationPage const *, _pConnectionPageSetup, void)
580 m_bIsConnectable = _pConnectionPageSetup->GetRoadmapStateValue( );
581 enableState(PAGE_DBSETUPWIZARD_FINAL, m_bIsConnectable);
582 enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION, m_bIsConnectable);
583 if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL)
584 enableButtons( WizardButtonFlags::FINISH, true);
585 else
586 enableButtons( WizardButtonFlags::FINISH, m_bIsConnectable);
587 enableButtons( WizardButtonFlags::NEXT, m_bIsConnectable && (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL));
590 IMPL_LINK_TYPED(ODbTypeWizDialogSetup, ImplClickHdl, OMySQLIntroPageSetup*, _pMySQLIntroPageSetup, void)
592 OUString sURLPrefix;
593 switch( _pMySQLIntroPageSetup->getMySQLMode() )
595 case OMySQLIntroPageSetup::VIA_ODBC:
596 sURLPrefix = "sdbc:mysql:odbc:";
597 break;
598 case OMySQLIntroPageSetup::VIA_JDBC:
599 sURLPrefix = "sdbc:mysql:jdbc:";
600 break;
601 case OMySQLIntroPageSetup::VIA_NATIVE:
602 sURLPrefix = "sdbc:mysql:mysqlc:";
603 break;
605 activatePath( static_cast<PathId>(m_pCollection->getIndexOf(sURLPrefix) + 1), true);
608 IMPL_LINK_NOARG_TYPED(ODbTypeWizDialogSetup, OnChangeCreationMode, OGeneralPageWizard*, void)
610 activateDatabasePath();
613 IMPL_LINK(ODbTypeWizDialogSetup, OnRecentDocumentSelected, OGeneralPageWizard*, /*_pGeneralPage*/)
615 enableButtons( WizardButtonFlags::FINISH, !m_pGeneralPage->GetSelectedDocument().sURL.isEmpty() );
616 return 0L;
619 IMPL_LINK(ODbTypeWizDialogSetup, OnSingleDocumentChosen, OGeneralPageWizard*, /*_pGeneralPage*/)
621 if ( prepareLeaveCurrentState( eFinish ) )
622 onFinish();
623 return 0L;
626 void ODbTypeWizDialogSetup::enterState(WizardState _nState)
628 m_sURL = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(*m_pOutSet);
629 RoadmapWizard::enterState(_nState);
630 switch(_nState)
632 case PAGE_DBSETUPWIZARD_INTRO:
633 m_sOldURL = m_sURL;
634 break;
635 case PAGE_DBSETUPWIZARD_FINAL:
636 enableButtons( WizardButtonFlags::FINISH, true);
637 if ( m_pFinalPage )
638 m_pFinalPage->enableTableWizardCheckBox(m_pCollection->supportsTableCreation(m_sURL));
639 break;
643 bool ODbTypeWizDialogSetup::saveDatasource()
645 SfxTabPage* pPage = static_cast<SfxTabPage*>(WizardDialog::GetPage(getCurrentState()));
646 if ( pPage )
647 pPage->FillItemSet(m_pOutSet);
648 return true;
651 bool ODbTypeWizDialogSetup::leaveState(WizardState _nState)
653 if (_nState == PAGE_DBSETUPWIZARD_MYSQL_INTRO)
654 return true;
655 if ( _nState == PAGE_DBSETUPWIZARD_INTRO && m_sURL != m_sOldURL )
657 resetPages(m_pImpl->getCurrentDataSource());
659 SfxTabPage* pPage = static_cast<SfxTabPage*>(WizardDialog::GetPage(_nState));
660 return pPage && pPage->DeactivatePage(m_pOutSet) != 0;
663 void ODbTypeWizDialogSetup::setTitle(const OUString& /*_sTitle*/)
665 OSL_FAIL( "ODbTypeWizDialogSetup::setTitle: not implemented!" );
666 // why?
669 void ODbTypeWizDialogSetup::enableConfirmSettings( bool _bEnable )
671 (void)_bEnable;
674 namespace
676 bool lcl_handle( const Reference< XInteractionHandler2 >& _rxHandler, const Any& _rRequest )
678 OInteractionRequest* pRequest = new OInteractionRequest( _rRequest );
679 Reference < XInteractionRequest > xRequest( pRequest );
680 OInteractionAbort* pAbort = new OInteractionAbort;
681 pRequest->addContinuation( pAbort );
683 return _rxHandler->handleInteractionRequest( xRequest );
687 bool ODbTypeWizDialogSetup::SaveDatabaseDocument()
689 Reference< XInteractionHandler2 > xHandler( InteractionHandler::createWithParent(getORB(), 0) );
692 if (callSaveAsDialog())
694 m_pImpl->saveChanges(*m_pOutSet);
695 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
696 Reference< XModel > xModel( getDataSourceOrModel( xDatasource ), UNO_QUERY_THROW );
697 Reference< XStorable > xStore( xModel, UNO_QUERY_THROW );
699 if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eCreateNew )
700 CreateDatabase();
702 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
703 aArgs.put( "Overwrite", true );
704 aArgs.put( "InteractionHandler", xHandler );
705 aArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
707 OUString sPath = ODbDataSourceAdministrationHelper::getDocumentUrl( *m_pOutSet );
708 xStore->storeAsURL( sPath, aArgs.getPropertyValues() );
710 if ( !m_pFinalPage || m_pFinalPage->IsDatabaseDocumentToBeRegistered() )
711 RegisterDataSourceByLocation( sPath );
713 return true;
716 catch ( const Exception& e )
718 Any aError = ::cppu::getCaughtException();
719 if ( xHandler.is() )
721 if ( !lcl_handle( xHandler, aError ) )
723 InteractiveIOException aRequest;
724 aRequest.Classification = InteractionClassification_ERROR;
725 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() ) )
726 // assume savint the document faile
727 aRequest.Code = IOErrorCode_CANT_WRITE;
728 else
729 aRequest.Code = IOErrorCode_GENERAL;
730 aRequest.Message = e.Message;
731 aRequest.Context = e.Context;
732 lcl_handle( xHandler, makeAny( aRequest ) );
736 return false;
739 bool ODbTypeWizDialogSetup::IsDatabaseDocumentToBeOpened() const
741 if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
742 return true;
744 if ( m_pFinalPage != nullptr )
745 return m_pFinalPage->IsDatabaseDocumentToBeOpened();
747 return true;
750 bool ODbTypeWizDialogSetup::IsTableWizardToBeStarted() const
752 if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
753 return false;
755 if ( m_pFinalPage != nullptr )
756 return m_pFinalPage->IsTableWizardToBeStarted();
758 return false;
761 void ODbTypeWizDialogSetup::CreateDatabase()
763 OUString sUrl;
764 OUString eType = m_pGeneralPage->GetSelectedType();
765 if ( dbaccess::ODsnTypeCollection::isEmbeddedDatabase(eType) )
767 sUrl = eType;
768 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
769 OSL_ENSURE(xDatasource.is(),"DataSource is null!");
770 if ( xDatasource.is() )
771 xDatasource->setPropertyValue( PROPERTY_INFO, makeAny( m_pCollection->getDefaultDBSettings( eType ) ) );
772 m_pImpl->translateProperties(xDatasource,*m_pOutSet);
774 else if ( m_pCollection->isFileSystemBased(eType) )
776 Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
777 INetURLObject aDBPathURL(m_sWorkPath);
778 aDBPathURL.Append(m_aDocURL.getBase());
779 createUniqueFolderName(&aDBPathURL);
780 OUString sPrefix = eType;
781 sUrl = aDBPathURL.GetMainURL( INetURLObject::NO_DECODE);
782 xSimpleFileAccess->createFolder(sUrl);
783 sUrl = sPrefix.concat(sUrl);
785 m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, sUrl));
786 m_pImpl->saveChanges(*m_pOutSet);
789 void ODbTypeWizDialogSetup::RegisterDataSourceByLocation(const OUString& _sPath)
791 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
792 Reference< XDatabaseContext > xDatabaseContext( DatabaseContext::create(getORB()) );
793 INetURLObject aURL( _sPath );
794 OUString sFilename = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
795 OUString sDatabaseName = ::dbtools::createUniqueName(xDatabaseContext, sFilename, false);
796 xDatabaseContext->registerObject(sDatabaseName, xDatasource);
799 bool ODbTypeWizDialogSetup::callSaveAsDialog()
801 bool bRet = false;
802 ::sfx2::FileDialogHelper aFileDlg(
803 ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION,
804 0, this);
805 const SfxFilter* pFilter = getStandardDatabaseFilter();
806 if ( pFilter )
808 INetURLObject aWorkURL( m_sWorkPath );
809 aFileDlg.SetDisplayFolder( aWorkURL.GetMainURL( INetURLObject::NO_DECODE ));
811 OUString sDefaultName = ModuleRes( STR_DATABASEDEFAULTNAME );
812 OUString sExtension = pFilter->GetDefaultExtension();
813 sDefaultName += sExtension.replaceAt( 0, 1, OUString() );
814 aWorkURL.Append( sDefaultName );
815 sDefaultName = createUniqueFileName( aWorkURL );
816 aFileDlg.SetFileName( sDefaultName );
818 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
819 aFileDlg.SetCurrentFilter(pFilter->GetUIName());
821 if ( aFileDlg.Execute() == ERRCODE_NONE )
823 m_aDocURL = INetURLObject(aFileDlg.GetPath());
825 if( m_aDocURL.GetProtocol() != INetProtocol::NotValid )
827 OUString sFileName = m_aDocURL.GetMainURL( INetURLObject::NO_DECODE );
828 if ( ::utl::UCBContentHelper::IsDocument(sFileName) )
829 ::utl::UCBContentHelper::Kill(sFileName);
830 m_pOutSet->Put(SfxStringItem(DSID_DOCUMENT_URL, sFileName));
831 bRet = true;
834 return bRet;
837 void ODbTypeWizDialogSetup::createUniqueFolderName(INetURLObject* pURL)
839 Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
840 :: OUString sLastSegmentName = pURL->getName();
841 bool bFolderExists = true;
842 sal_Int32 i = 1;
843 while (bFolderExists)
845 bFolderExists = xSimpleFileAccess->isFolder(pURL->GetMainURL( INetURLObject::NO_DECODE ));
846 if (bFolderExists)
848 i++;
849 pURL->setName(sLastSegmentName.concat(OUString::number(i)));
854 OUString ODbTypeWizDialogSetup::createUniqueFileName(const INetURLObject& _rURL)
856 Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
857 OUString BaseName = _rURL.getBase();
859 bool bElementExists = true;
861 INetURLObject aExistenceCheck( _rURL );
862 for ( sal_Int32 i = 1; bElementExists; )
864 bElementExists = xSimpleFileAccess->exists( aExistenceCheck.GetMainURL( INetURLObject::NO_DECODE ) );
865 if ( bElementExists )
867 aExistenceCheck.setBase( BaseName.concat( OUString::number( i ) ) );
868 ++i;
871 return aExistenceCheck.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
873 IWizardPageController* ODbTypeWizDialogSetup::getPageController( TabPage* _pCurrentPage ) const
875 OGenericAdministrationPage* pPage = static_cast<OGenericAdministrationPage*>(_pCurrentPage);
876 return pPage;
879 namespace
881 typedef ::cppu::WeakImplHelper1 < XTerminateListener
882 > AsyncLoader_Base;
883 class AsyncLoader : public AsyncLoader_Base
885 private:
886 Reference< XComponentLoader > m_xFrameLoader;
887 Reference< XDesktop2 > m_xDesktop;
888 Reference< XInteractionHandler2 > m_xInteractionHandler;
889 OUString m_sURL;
890 OAsynchronousLink m_aAsyncCaller;
892 public:
893 AsyncLoader( const Reference< XComponentContext >& _rxORB, const OUString& _rURL );
895 void doLoadAsync();
897 // XTerminateListener
898 virtual void SAL_CALL queryTermination( const com::sun::star::lang::EventObject& Event ) throw (TerminationVetoException, RuntimeException, std::exception) SAL_OVERRIDE;
899 virtual void SAL_CALL notifyTermination( const com::sun::star::lang::EventObject& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
900 // XEventListener
901 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
903 private:
904 DECL_LINK( OnOpenDocument, void* );
907 AsyncLoader::AsyncLoader( const Reference< XComponentContext >& _rxORB, const OUString& _rURL )
908 :m_sURL( _rURL )
909 ,m_aAsyncCaller( LINK( this, AsyncLoader, OnOpenDocument ) )
913 m_xDesktop.set( Desktop::create(_rxORB) );
914 m_xFrameLoader.set( m_xDesktop, UNO_QUERY_THROW );
915 m_xInteractionHandler = InteractionHandler::createWithParent(_rxORB, 0);
917 catch( const Exception& )
919 DBG_UNHANDLED_EXCEPTION();
923 void AsyncLoader::doLoadAsync()
925 OSL_ENSURE( !m_aAsyncCaller.IsRunning(), "AsyncLoader:doLoadAsync: already running!" );
927 acquire();
930 if ( m_xDesktop.is() )
931 m_xDesktop->addTerminateListener( this );
933 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
935 m_aAsyncCaller.Call( NULL );
938 IMPL_LINK_NOARG( AsyncLoader, OnOpenDocument )
942 if ( m_xFrameLoader.is() )
944 ::comphelper::NamedValueCollection aLoadArgs;
945 aLoadArgs.put( "InteractionHandler", m_xInteractionHandler );
946 aLoadArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
948 Sequence< PropertyValue > aLoadArgPV;
949 aLoadArgs >>= aLoadArgPV;
951 m_xFrameLoader->loadComponentFromURL( m_sURL,
952 OUString( "_default" ),
953 FrameSearchFlag::ALL,
954 aLoadArgPV
958 catch( const Exception& )
960 // do not assert.
961 // Such an exception happens for instance of the to-be-loaded document does not exist anymore.
966 if ( m_xDesktop.is() )
967 m_xDesktop->removeTerminateListener( this );
969 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
971 release();
972 return 0L;
975 void SAL_CALL AsyncLoader::queryTermination( const com::sun::star::lang::EventObject& /*Event*/ ) throw (TerminationVetoException, RuntimeException, std::exception)
977 throw TerminationVetoException();
980 void SAL_CALL AsyncLoader::notifyTermination( const com::sun::star::lang::EventObject& /*Event*/ ) throw (RuntimeException, std::exception)
983 void SAL_CALL AsyncLoader::disposing( const com::sun::star::lang::EventObject& /*Source*/ ) throw (RuntimeException, std::exception)
988 bool ODbTypeWizDialogSetup::onFinish()
990 if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
992 // we're not going to re-use the XModel we have - since the document the user
993 // wants us to load could be a non-database document. Instead, we asynchronously
994 // open the selected document. Thus, the wizard's return value is RET_CANCEL,
995 // which means to not continue loading the database document
996 if ( !OWizardMachine::Finish( RET_CANCEL ) )
997 return false;
1001 AsyncLoader* pAsyncLoader = new AsyncLoader( getORB(), m_pGeneralPage->GetSelectedDocument().sURL );
1002 ::rtl::Reference< AsyncLoader > xKeepAlive( pAsyncLoader );
1003 pAsyncLoader->doLoadAsync();
1005 catch( const Exception& )
1007 DBG_UNHANDLED_EXCEPTION();
1010 return true;
1013 if (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL)
1015 skipUntil(PAGE_DBSETUPWIZARD_FINAL);
1017 if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL)
1018 return SaveDatabaseDocument() && OWizardMachine::onFinish();
1019 else
1021 enableButtons( WizardButtonFlags::FINISH, false );
1022 return false;
1026 } // namespace dbaui
1028 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */