1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "dbwizsetup.hxx"
23 #include "DBSetupConnectionPages.hxx"
24 #include "dbadminsetup.hrc"
25 #include "dbu_dlg.hrc"
26 #include "dsitems.hxx"
27 #include "dsnItem.hxx"
29 #include <unotools/pathoptions.hxx>
30 #include <svl/stritem.hxx>
31 #include <svl/eitem.hxx>
32 #include <svl/intitem.hxx>
33 #include <vcl/msgbox.hxx>
34 #include "dbustrings.hrc"
35 #include "adminpages.hxx"
36 #include <sfx2/docfilt.hxx>
37 #include <unotools/ucbhelper.hxx>
38 #include "generalpage.hxx"
39 #include "localresaccess.hxx"
40 #include "stringlistitem.hxx"
41 #include "propertysetitem.hxx"
42 #include <unotools/confignode.hxx>
43 #include "DbAdminImpl.hxx"
44 #include "dbaccess_helpid.hrc"
45 #include "ConnectionPageSetup.hxx"
46 #include "UITools.hxx"
47 #include "dbadmin.hrc"
48 #include <dbaccess/AsyncronousLink.hxx>
49 #include <sfx2/filedlghelper.hxx>
50 #include <cppuhelper/exc_hlp.hxx>
52 #include <com/sun/star/frame/XStorable.hpp>
53 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
54 #include <com/sun/star/sdbc/XDataSource.hpp>
55 #include <com/sun/star/container/XNameAccess.hpp>
56 #include <com/sun/star/sdb/DatabaseContext.hpp>
57 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
58 #include <com/sun/star/frame/Desktop.hpp>
59 #include <com/sun/star/frame/FrameSearchFlag.hpp>
60 #include <com/sun/star/frame/XComponentLoader.hpp>
61 #include <com/sun/star/frame/XModel.hpp>
62 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
63 #include <com/sun/star/task/XJobExecutor.hpp>
64 #include <com/sun/star/lang/XInitialization.hpp>
65 #include <com/sun/star/sdb/CommandType.hpp>
66 #include <com/sun/star/ucb/InteractiveIOException.hpp>
67 #include <com/sun/star/io/IOException.hpp>
68 #include <com/sun/star/frame/XTerminateListener.hpp>
69 #include <com/sun/star/sdbc/XDriverAccess.hpp>
70 #include <com/sun/star/document/MacroExecMode.hpp>
71 #include <com/sun/star/ucb/IOErrorCode.hpp>
72 #include <com/sun/star/task/InteractionHandler.hpp>
73 #include <com/sun/star/task/XInteractionHandler2.hpp>
74 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
78 #include <svl/filenotation.hxx>
79 #include <comphelper/interaction.hxx>
80 #include <comphelper/namedvaluecollection.hxx>
81 #include <comphelper/processfactory.hxx>
82 #include <comphelper/sequenceashashmap.hxx>
83 #include <tools/diagnose_ex.h>
84 #include <osl/diagnose.h>
85 #include <connectivity/DriversConfig.hxx>
90 //.........................................................................
93 //.........................................................................
94 using namespace dbtools
;
96 using namespace com::sun::star
;
97 using namespace com::sun::star::uno
;
98 using namespace com::sun::star::sdbc
;
99 using namespace com::sun::star::sdbcx
;
100 using namespace com::sun::star::task
;
101 using namespace com::sun::star::lang
;
102 using namespace com::sun::star::io
;
103 using namespace com::sun::star::util
;
104 using namespace com::sun::star::beans
;
105 using namespace com::sun::star::container
;
106 using namespace com::sun::star::frame
;
107 using namespace com::sun::star::ucb
;
108 using namespace ::com::sun::star::sdb
;
109 using namespace ::com::sun::star::document
;
110 using namespace ::comphelper
;
111 using namespace ::cppu
;
113 DBG_NAME(ODbTypeWizDialogSetup
)
114 //=========================================================================
115 //= ODbTypeWizDialogSetup
116 //=========================================================================
117 //-------------------------------------------------------------------------
118 ODbTypeWizDialogSetup::ODbTypeWizDialogSetup(Window
* _pParent
120 ,const Reference
< XComponentContext
>& _rxORB
121 ,const ::com::sun::star::uno::Any
& _aDataSourceName
123 :svt::RoadmapWizard( _pParent
, ModuleRes(DLG_DATABASE_WIZARD
),
124 WZB_NEXT
| WZB_PREVIOUS
| WZB_FINISH
| WZB_CANCEL
| WZB_HELP
)
127 , m_bResetting(sal_False
)
128 , m_bApplied(sal_False
)
129 , m_bUIEnabled( sal_True
)
130 , m_bIsConnectable( sal_False
)
131 , m_sRM_IntroText( ModuleRes( STR_PAGETITLE_INTROPAGE
) )
132 , m_sRM_dBaseText( ModuleRes( STR_PAGETITLE_DBASE
) )
133 , m_sRM_TextText( ModuleRes( STR_PAGETITLE_TEXT
) )
134 , m_sRM_MSAccessText( ModuleRes( STR_PAGETITLE_MSACCESS
) )
135 , m_sRM_LDAPText( ModuleRes( STR_PAGETITLE_LDAP
) )
136 , m_sRM_ADOText( ModuleRes( STR_PAGETITLE_ADO
) )
137 , m_sRM_JDBCText( ModuleRes( STR_PAGETITLE_JDBC
) )
138 , m_sRM_MySQLNativePageTitle( ModuleRes( STR_PAGETITLE_MYSQL_NATIVE
) )
139 , m_sRM_OracleText( ModuleRes( STR_PAGETITLE_ORACLE
) )
140 , m_sRM_MySQLText( ModuleRes( STR_PAGETITLE_MYSQL
) )
141 , m_sRM_ODBCText( ModuleRes( STR_PAGETITLE_ODBC
) )
142 , m_sRM_SpreadSheetText( ModuleRes( STR_PAGETITLE_SPREADSHEET
) )
143 , m_sRM_AuthentificationText( ModuleRes( STR_PAGETITLE_AUTHENTIFICATION
) )
144 , m_sRM_FinalText( ModuleRes( STR_PAGETITLE_FINAL
) )
145 , m_sWorkPath( SvtPathOptions().GetWorkPath() )
146 , m_pGeneralPage( NULL
)
147 , m_pMySQLIntroPage( NULL
)
148 , m_pFinalPage( NULL
)
149 , m_pCollection( NULL
)
151 DBG_CTOR(ODbTypeWizDialogSetup
,NULL
);
152 // no local resources needed anymore
153 // extract the datasource type collection from the item set
154 DbuTypeCollectionItem
* pCollectionItem
= PTR_CAST(DbuTypeCollectionItem
, _pItems
->GetItem(DSID_TYPECOLLECTION
));
156 m_pCollection
= pCollectionItem
->getCollection();
158 OSL_ENSURE(m_pCollection
, "ODbTypeWizDialogSetup::ODbTypeWizDialogSetup : really need a DSN type collection !");
162 m_pImpl
= ::std::auto_ptr
<ODbDataSourceAdministrationHelper
>(new ODbDataSourceAdministrationHelper(_rxORB
,this,this));
163 m_pImpl
->setDataSourceOrName(_aDataSourceName
);
164 Reference
< XPropertySet
> xDatasource
= m_pImpl
->getCurrentDataSource();
165 m_pOutSet
= new SfxItemSet( *_pItems
->GetPool(), _pItems
->GetRanges() );
167 m_pImpl
->translateProperties(xDatasource
, *m_pOutSet
);
169 SetPageSizePixel(LogicToPixel(::Size(WIZARD_PAGE_X
, WIZARD_PAGE_Y
), MAP_APPFONT
));
170 ShowButtonFixedLine(sal_True
);
171 defaultButton(WZB_NEXT
);
172 enableButtons(WZB_FINISH
, sal_True
);
173 enableAutomaticNextButtonState();
175 ::dbaccess::ODsnTypeCollection::TypeIterator aIter
= m_pCollection
->begin();
176 ::dbaccess::ODsnTypeCollection::TypeIterator aEnd
= m_pCollection
->end();
177 for(PathId i
= 1;aIter
!= aEnd
;++aIter
,++i
)
179 const OUString sURLPrefix
= aIter
.getURLPrefix();
180 svt::RoadmapWizardTypes::WizardPath aPath
;
181 aPath
.push_back(PAGE_DBSETUPWIZARD_INTRO
);
182 m_pCollection
->fillPageIds(sURLPrefix
,aPath
);
183 aPath
.push_back(PAGE_DBSETUPWIZARD_AUTHENTIFICATION
);
184 aPath
.push_back(PAGE_DBSETUPWIZARD_FINAL
);
186 declareAuthDepPath(sURLPrefix
,i
,aPath
);
189 svt::RoadmapWizardTypes::WizardPath aPath
;
190 aPath
.push_back(PAGE_DBSETUPWIZARD_INTRO
);
191 declarePath( static_cast<PathId
>(m_pCollection
->size()+1), aPath
);
193 m_pPrevPage
->SetHelpId(HID_DBWIZ_PREVIOUS
);
194 m_pNextPage
->SetHelpId(HID_DBWIZ_NEXT
);
195 m_pCancel
->SetHelpId(HID_DBWIZ_CANCEL
);
196 m_pFinish
->SetHelpId(HID_DBWIZ_FINISH
);
197 m_pHelp
->SetUniqueId(UID_DBWIZ_HELP
);
198 SetRoadmapInteractive( sal_True
);
202 void ODbTypeWizDialogSetup::declareAuthDepPath( const OUString
& _sURL
, PathId _nPathId
, const svt::RoadmapWizardTypes::WizardPath
& _rPaths
)
204 bool bHasAuthentication
= DataSourceMetaData::getAuthentication( _sURL
) != AuthNone
;
206 // collect the elements of the path
209 svt::RoadmapWizardTypes::WizardPath::const_iterator aIter
= _rPaths
.begin();
210 svt::RoadmapWizardTypes::WizardPath::const_iterator aEnd
= _rPaths
.end();
211 for(;aIter
!= aEnd
;++aIter
)
213 if ( bHasAuthentication
|| ( *aIter
!= PAGE_DBSETUPWIZARD_AUTHENTIFICATION
) )
214 aPath
.push_back( *aIter
);
218 ::svt::RoadmapWizard::declarePath( _nPathId
, aPath
);
221 String
ODbTypeWizDialogSetup::getStateDisplayName( WizardState _nState
) const
226 case PAGE_DBSETUPWIZARD_INTRO
:
227 sRoadmapItem
= m_sRM_IntroText
;
230 case PAGE_DBSETUPWIZARD_DBASE
:
231 sRoadmapItem
= m_sRM_dBaseText
;
233 case PAGE_DBSETUPWIZARD_ADO
:
234 sRoadmapItem
= m_sRM_ADOText
;
236 case PAGE_DBSETUPWIZARD_TEXT
:
237 sRoadmapItem
= m_sRM_TextText
;
239 case PAGE_DBSETUPWIZARD_MSACCESS
:
240 sRoadmapItem
= m_sRM_MSAccessText
;
242 case PAGE_DBSETUPWIZARD_LDAP
:
243 sRoadmapItem
= m_sRM_LDAPText
;
245 case PAGE_DBSETUPWIZARD_JDBC
:
246 sRoadmapItem
= m_sRM_JDBCText
;
248 case PAGE_DBSETUPWIZARD_ORACLE
:
249 sRoadmapItem
= m_sRM_OracleText
;
251 case PAGE_DBSETUPWIZARD_MYSQL_INTRO
:
252 sRoadmapItem
= m_sRM_MySQLText
;
254 case PAGE_DBSETUPWIZARD_MYSQL_JDBC
:
255 sRoadmapItem
= m_sRM_JDBCText
;
257 case PAGE_DBSETUPWIZARD_MYSQL_NATIVE
:
258 sRoadmapItem
= m_sRM_MySQLNativePageTitle
;
260 case PAGE_DBSETUPWIZARD_MYSQL_ODBC
:
261 sRoadmapItem
= m_sRM_ODBCText
;
263 case PAGE_DBSETUPWIZARD_ODBC
:
264 sRoadmapItem
= m_sRM_ODBCText
;
266 case PAGE_DBSETUPWIZARD_SPREADSHEET
:
267 sRoadmapItem
= m_sRM_SpreadSheetText
;
269 case PAGE_DBSETUPWIZARD_AUTHENTIFICATION
:
270 sRoadmapItem
= m_sRM_AuthentificationText
;
272 case PAGE_DBSETUPWIZARD_USERDEFINED
:
274 LocalResourceAccess
aDummy(DLG_DATABASE_ADMINISTRATION
, RSC_TABDIALOG
);
275 sRoadmapItem
= String(ModuleRes(STR_PAGETITLE_CONNECTION
));
278 case PAGE_DBSETUPWIZARD_FINAL
:
279 sRoadmapItem
= m_sRM_FinalText
;
287 //-------------------------------------------------------------------------
288 ODbTypeWizDialogSetup::~ODbTypeWizDialogSetup()
291 DBG_DTOR(ODbTypeWizDialogSetup
,NULL
);
294 //-------------------------------------------------------------------------
295 IMPL_LINK(ODbTypeWizDialogSetup
, OnTypeSelected
, OGeneralPage
*, /*_pTabPage*/)
297 activateDatabasePath();
301 void lcl_removeUnused(const ::comphelper::NamedValueCollection
& _aOld
,const ::comphelper::NamedValueCollection
& _aNew
,::comphelper::NamedValueCollection
& _rDSInfo
)
303 _rDSInfo
.merge(_aNew
,true);
304 uno::Sequence
< beans::NamedValue
> aOldValues
= _aOld
.getNamedValues();
305 const beans::NamedValue
* pIter
= aOldValues
.getConstArray();
306 const beans::NamedValue
* pEnd
= pIter
+ aOldValues
.getLength();
307 for(;pIter
!= pEnd
;++pIter
)
309 if ( !_aNew
.has(pIter
->Name
) )
311 _rDSInfo
.remove(pIter
->Name
);
315 // -----------------------------------------------------------------------------
316 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
)
318 if ( _pCollection
->getPrefix(_sOldURLPrefix
) == _pCollection
->getPrefix(_sNewURLPrefix
) )
320 uno::Sequence
< beans::PropertyValue
> aInfo
;
321 _xDatasource
->getPropertyValue(PROPERTY_INFO
) >>= aInfo
;
322 ::comphelper::NamedValueCollection
aDS(aInfo
);
324 ::connectivity::DriversConfig
aDriverConfig(xContext
);
326 const ::comphelper::NamedValueCollection
& aOldProperties
= aDriverConfig
.getProperties(_sOldURLPrefix
);
327 const ::comphelper::NamedValueCollection
& aNewProperties
= aDriverConfig
.getProperties(_sNewURLPrefix
);
328 lcl_removeUnused(aOldProperties
,aNewProperties
,aDS
);
331 _xDatasource
->setPropertyValue(PROPERTY_INFO
,uno::makeAny(aInfo
));
333 //-------------------------------------------------------------------------
334 void ODbTypeWizDialogSetup::activateDatabasePath()
336 switch ( m_pGeneralPage
->GetDatabaseCreationMode() )
338 case OGeneralPageWizard::eCreateNew
:
340 sal_Int32 nCreateNewDBIndex
= m_pCollection
->getIndexOf( m_pCollection
->getEmbeddedDatabase() );
341 if ( nCreateNewDBIndex
== -1 )
342 nCreateNewDBIndex
= m_pCollection
->getIndexOf( OUString("sdbc:dbase:") );
343 OSL_ENSURE( nCreateNewDBIndex
!= -1, "ODbTypeWizDialogSetup::activateDatabasePath: the GeneralPage should have prevented this!" );
344 activatePath( static_cast< PathId
>( nCreateNewDBIndex
+ 1 ), sal_True
);
346 enableState(PAGE_DBSETUPWIZARD_FINAL
, sal_True
);
347 enableButtons( WZB_FINISH
, sal_True
);
350 case OGeneralPageWizard::eConnectExternal
:
352 OUString sOld
= m_sURL
;
353 m_sURL
= m_pGeneralPage
->GetSelectedType();
354 DataSourceInfoConverter::convert(getORB(), m_pCollection
,sOld
,m_sURL
,m_pImpl
->getCurrentDataSource());
355 ::dbaccess::DATASOURCE_TYPE eType
= VerifyDataSourceType(m_pCollection
->determineType(m_sURL
));
356 if (eType
== ::dbaccess::DST_UNKNOWN
)
357 eType
= m_pCollection
->determineType(m_sOldURL
);
359 activatePath( static_cast<PathId
>(m_pCollection
->getIndexOf(m_sURL
) + 1), sal_True
);
360 updateTypeDependentStates();
363 case OGeneralPageWizard::eOpenExisting
:
365 activatePath( static_cast<PathId
>(m_pCollection
->size() + 1), sal_True
);
366 enableButtons( WZB_FINISH
, m_pGeneralPage
->GetSelectedDocument().sURL
.Len() != 0 );
370 OSL_FAIL( "ODbTypeWizDialogSetup::activateDatabasePath: unknown creation mode!" );
373 enableButtons( WZB_NEXT
, m_pGeneralPage
->GetDatabaseCreationMode() != OGeneralPageWizard::eOpenExisting
);
374 // TODO: this should go into the base class. Point is, we activate a path whose *last*
375 // step is also the current one. The base class should automatically disable
376 // the Next button in such a case. However, not for this patch ...
379 //-------------------------------------------------------------------------
380 void ODbTypeWizDialogSetup::updateTypeDependentStates()
382 sal_Bool bDoEnable
= sal_False
;
383 sal_Bool bIsConnectionRequired
= IsConnectionUrlRequired();
384 if (!bIsConnectionRequired
)
386 bDoEnable
= sal_True
;
388 else if ( m_sURL
== m_sOldURL
)
390 bDoEnable
= m_bIsConnectable
;
392 enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION
, bDoEnable
);
393 enableState(PAGE_DBSETUPWIZARD_FINAL
, bDoEnable
);
394 enableButtons( WZB_FINISH
, bDoEnable
);
398 //-------------------------------------------------------------------------
399 sal_Bool
ODbTypeWizDialogSetup::IsConnectionUrlRequired()
401 return m_pCollection
->isConnectionUrlRequired(m_sURL
);
404 //-------------------------------------------------------------------------
405 void ODbTypeWizDialogSetup::resetPages(const Reference
< XPropertySet
>& _rxDatasource
)
407 // remove all items which relate to indirect properties from the input set
408 // (without this, the following may happen: select an arbitrary data source where some indirect properties
409 // are set. Select another data source of the same type, where the indirect props are not set (yet). Then,
410 // the indirect property values of the first ds are shown in the second ds ...)
411 const ODbDataSourceAdministrationHelper::MapInt2String
& rMap
= m_pImpl
->getIndirectProperties();
412 for ( ODbDataSourceAdministrationHelper::ConstMapInt2StringIterator aIndirect
= rMap
.begin();
413 aIndirect
!= rMap
.end();
416 getWriteOutputSet()->ClearItem( (sal_uInt16
)aIndirect
->first
);
418 // extract all relevant data from the property set of the data source
419 m_pImpl
->translateProperties(_rxDatasource
, *getWriteOutputSet());
421 // -----------------------------------------------------------------------------
422 const SfxItemSet
* ODbTypeWizDialogSetup::getOutputSet() const
426 // -----------------------------------------------------------------------------
427 SfxItemSet
* ODbTypeWizDialogSetup::getWriteOutputSet()
431 // -----------------------------------------------------------------------------
432 ::std::pair
< Reference
<XConnection
>,sal_Bool
> ODbTypeWizDialogSetup::createConnection()
434 return m_pImpl
->createConnection();
436 // -----------------------------------------------------------------------------
437 Reference
< XComponentContext
> ODbTypeWizDialogSetup::getORB() const
439 return m_pImpl
->getORB();
441 // -----------------------------------------------------------------------------
442 Reference
< XDriver
> ODbTypeWizDialogSetup::getDriver()
444 return m_pImpl
->getDriver();
448 ::dbaccess::DATASOURCE_TYPE
ODbTypeWizDialogSetup::VerifyDataSourceType(const ::dbaccess::DATASOURCE_TYPE _DatabaseType
) const
450 ::dbaccess::DATASOURCE_TYPE LocDatabaseType
= _DatabaseType
;
451 if ((LocDatabaseType
== ::dbaccess::DST_MYSQL_JDBC
) || (LocDatabaseType
== ::dbaccess::DST_MYSQL_ODBC
) || (LocDatabaseType
== ::dbaccess::DST_MYSQL_NATIVE
))
453 if (m_pMySQLIntroPage
!= NULL
)
455 switch( m_pMySQLIntroPage
->getMySQLMode() )
457 case OMySQLIntroPageSetup::VIA_JDBC
:
458 return ::dbaccess::DST_MYSQL_JDBC
;
459 case OMySQLIntroPageSetup::VIA_NATIVE
:
460 return ::dbaccess::DST_MYSQL_NATIVE
;
461 case OMySQLIntroPageSetup::VIA_ODBC
:
462 return ::dbaccess::DST_MYSQL_ODBC
;
466 return LocDatabaseType
;
471 // -----------------------------------------------------------------------------
472 OUString
ODbTypeWizDialogSetup::getDatasourceType(const SfxItemSet
& _rSet
) const
474 OUString sRet
= m_pImpl
->getDatasourceType(_rSet
);
475 if (m_pMySQLIntroPage
!= NULL
&& m_pMySQLIntroPage
->IsVisible() )
477 switch( m_pMySQLIntroPage
->getMySQLMode() )
479 case OMySQLIntroPageSetup::VIA_JDBC
:
480 sRet
= OUString("sdbc:mysql:jdbc:");
482 case OMySQLIntroPageSetup::VIA_NATIVE
:
483 sRet
= OUString("sdbc:mysql:mysqlc:");
485 case OMySQLIntroPageSetup::VIA_ODBC
:
486 sRet
= OUString("sdbc:mysql:odbc:");
493 // -----------------------------------------------------------------------------
494 void ODbTypeWizDialogSetup::clearPassword()
496 m_pImpl
->clearPassword();
499 // -----------------------------------------------------------------------------
500 TabPage
* ODbTypeWizDialogSetup::createPage(WizardState _nState
)
502 SfxTabPage
* pFirstPage
;
503 OGenericAdministrationPage
* pPage
= NULL
;
506 case PAGE_DBSETUPWIZARD_INTRO
:
507 pFirstPage
= new OGeneralPageWizard(this,*m_pOutSet
);
508 pPage
= static_cast<OGenericAdministrationPage
*> (pFirstPage
);
509 m_pGeneralPage
= static_cast<OGeneralPageWizard
*>(pFirstPage
);
510 m_pGeneralPage
->SetTypeSelectHandler(LINK(this, ODbTypeWizDialogSetup
, OnTypeSelected
));
511 m_pGeneralPage
->SetCreationModeHandler(LINK( this, ODbTypeWizDialogSetup
, OnChangeCreationMode
) );
512 m_pGeneralPage
->SetDocumentSelectionHandler(LINK( this, ODbTypeWizDialogSetup
, OnRecentDocumentSelected
) );
513 m_pGeneralPage
->SetChooseDocumentHandler(LINK( this, ODbTypeWizDialogSetup
, OnSingleDocumentChosen
) );
516 case PAGE_DBSETUPWIZARD_DBASE
:
517 pPage
= OConnectionTabPageSetup::CreateDbaseTabPage(this,*m_pOutSet
);
520 case PAGE_DBSETUPWIZARD_ADO
:
521 pPage
= OConnectionTabPageSetup::CreateADOTabPage( this, *m_pOutSet
);
524 case PAGE_DBSETUPWIZARD_TEXT
:
525 pPage
= OTextConnectionPageSetup::CreateTextTabPage(this,*m_pOutSet
);
528 case PAGE_DBSETUPWIZARD_ODBC
:
529 pPage
= OConnectionTabPageSetup::CreateODBCTabPage( this, *m_pOutSet
);
532 case PAGE_DBSETUPWIZARD_JDBC
:
533 pPage
= OJDBCConnectionPageSetup::CreateJDBCTabPage( this, *m_pOutSet
);
536 case PAGE_DBSETUPWIZARD_MYSQL_ODBC
:
537 m_pOutSet
->Put(SfxStringItem(DSID_CONNECTURL
, m_pCollection
->getPrefix(OUString("sdbc:mysql:odbc:"))));
538 pPage
= OConnectionTabPageSetup::CreateODBCTabPage( this, *m_pOutSet
);
541 case PAGE_DBSETUPWIZARD_MYSQL_JDBC
:
542 m_pOutSet
->Put(SfxStringItem(DSID_CONNECTURL
, m_pCollection
->getPrefix(OUString("sdbc:mysql:jdbc:"))));
543 pPage
= OGeneralSpecialJDBCConnectionPageSetup::CreateMySQLJDBCTabPage( this, *m_pOutSet
);
545 case PAGE_DBSETUPWIZARD_MYSQL_NATIVE
:
546 m_pOutSet
->Put(SfxStringItem(DSID_CONNECTURL
, m_pCollection
->getPrefix(OUString("sdbc:mysql:mysqlc:"))));
547 pPage
= MySQLNativeSetupPage::Create( this, *m_pOutSet
);
550 case PAGE_DBSETUPWIZARD_ORACLE
:
551 pPage
= OGeneralSpecialJDBCConnectionPageSetup::CreateOracleJDBCTabPage( this, *m_pOutSet
);
554 case PAGE_DBSETUPWIZARD_LDAP
:
555 pPage
= OLDAPConnectionPageSetup::CreateLDAPTabPage(this,*m_pOutSet
);
558 case PAGE_DBSETUPWIZARD_SPREADSHEET
: /// first user defined driver
559 pPage
= OSpreadSheetConnectionPageSetup::CreateSpreadSheetTabPage(this,*m_pOutSet
);
562 case PAGE_DBSETUPWIZARD_MSACCESS
:
563 pPage
= OConnectionTabPageSetup::CreateMSAccessTabPage(this,*m_pOutSet
);
565 case PAGE_DBSETUPWIZARD_MYSQL_INTRO
:
566 m_pMySQLIntroPage
= OMySQLIntroPageSetup::CreateMySQLIntroTabPage(this,*m_pOutSet
);
567 m_pMySQLIntroPage
->SetClickHdl(LINK( this, ODbTypeWizDialogSetup
, ImplClickHdl
) );
568 pPage
= m_pMySQLIntroPage
;
571 case PAGE_DBSETUPWIZARD_AUTHENTIFICATION
:
572 pPage
= OAuthentificationPageSetup::CreateAuthentificationTabPage(this,*m_pOutSet
);
575 case PAGE_DBSETUPWIZARD_USERDEFINED
:
576 pPage
= OConnectionTabPageSetup::CreateUserDefinedTabPage(this,*m_pOutSet
);
579 case PAGE_DBSETUPWIZARD_FINAL
:
580 pPage
= OFinalDBPageSetup::CreateFinalDBTabPageSetup(this,*m_pOutSet
);
581 m_pFinalPage
= static_cast<OFinalDBPageSetup
*> (pPage
);
587 if ((_nState
!= PAGE_DBSETUPWIZARD_INTRO
) && (_nState
!= PAGE_DBSETUPWIZARD_AUTHENTIFICATION
))
589 pPage
->SetModifiedHandler(LINK( this, ODbTypeWizDialogSetup
, ImplModifiedHdl
) );
592 pPage
->SetServiceFactory( m_pImpl
->getORB() );
593 pPage
->SetAdminDialog(this, this);
595 defaultButton( _nState
== PAGE_DBSETUPWIZARD_FINAL
? WZB_FINISH
: WZB_NEXT
);
596 enableButtons( WZB_FINISH
, _nState
== PAGE_DBSETUPWIZARD_FINAL
);
597 enableButtons( WZB_NEXT
, _nState
== PAGE_DBSETUPWIZARD_FINAL
? sal_False
: sal_True
);
604 IMPL_LINK(ODbTypeWizDialogSetup
, ImplModifiedHdl
, OGenericAdministrationPage
*, _pConnectionPageSetup
)
606 m_bIsConnectable
= _pConnectionPageSetup
->GetRoadmapStateValue( );
607 enableState(PAGE_DBSETUPWIZARD_FINAL
, m_bIsConnectable
);
608 enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION
, m_bIsConnectable
);
609 if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL
)
610 enableButtons( WZB_FINISH
, sal_True
);
612 enableButtons( WZB_FINISH
, m_bIsConnectable
);
613 enableButtons( WZB_NEXT
, m_bIsConnectable
&& (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL
));
618 // -----------------------------------------------------------------------------
619 IMPL_LINK(ODbTypeWizDialogSetup
, ImplClickHdl
, OMySQLIntroPageSetup
*, _pMySQLIntroPageSetup
)
622 switch( _pMySQLIntroPageSetup
->getMySQLMode() )
624 case OMySQLIntroPageSetup::VIA_ODBC
:
625 sURLPrefix
= OUString("sdbc:mysql:odbc:");
627 case OMySQLIntroPageSetup::VIA_JDBC
:
628 sURLPrefix
= OUString("sdbc:mysql:jdbc:");
630 case OMySQLIntroPageSetup::VIA_NATIVE
:
631 sURLPrefix
= OUString("sdbc:mysql:mysqlc:");
634 activatePath( static_cast<PathId
>(m_pCollection
->getIndexOf(sURLPrefix
) + 1), sal_True
);
638 // -----------------------------------------------------------------------------
639 IMPL_LINK(ODbTypeWizDialogSetup
, OnChangeCreationMode
, OGeneralPageWizard
*, /*_pGeneralPage*/)
641 activateDatabasePath();
645 // -----------------------------------------------------------------------------
646 IMPL_LINK(ODbTypeWizDialogSetup
, OnRecentDocumentSelected
, OGeneralPageWizard
*, /*_pGeneralPage*/)
648 enableButtons( WZB_FINISH
, m_pGeneralPage
->GetSelectedDocument().sURL
.Len() != 0 );
652 // -----------------------------------------------------------------------------
653 IMPL_LINK(ODbTypeWizDialogSetup
, OnSingleDocumentChosen
, OGeneralPageWizard
*, /*_pGeneralPage*/)
655 if ( prepareLeaveCurrentState( eFinish
) )
660 // -----------------------------------------------------------------------------
661 void ODbTypeWizDialogSetup::enterState(WizardState _nState
)
663 m_sURL
= m_pImpl
->getDatasourceType(*m_pOutSet
);
664 RoadmapWizard::enterState(_nState
);
667 case PAGE_DBSETUPWIZARD_INTRO
:
670 case PAGE_DBSETUPWIZARD_FINAL
:
671 enableButtons( WZB_FINISH
, sal_True
);
673 m_pFinalPage
->enableTableWizardCheckBox(m_pCollection
->supportsTableCreation(m_sURL
));
678 //-------------------------------------------------------------------------
679 sal_Bool
ODbTypeWizDialogSetup::saveDatasource()
681 SfxTabPage
* pPage
= static_cast<SfxTabPage
*>(WizardDialog::GetPage(getCurrentState()));
683 pPage
->FillItemSet(*m_pOutSet
);
688 // -----------------------------------------------------------------------------
689 sal_Bool
ODbTypeWizDialogSetup::leaveState(WizardState _nState
)
691 if (_nState
== PAGE_DBSETUPWIZARD_MYSQL_INTRO
)
693 if ( _nState
== PAGE_DBSETUPWIZARD_INTRO
&& m_sURL
!= m_sOldURL
)
695 resetPages(m_pImpl
->getCurrentDataSource());
697 SfxTabPage
* pPage
= static_cast<SfxTabPage
*>(WizardDialog::GetPage(_nState
));
698 return pPage
&& pPage
->DeactivatePage(m_pOutSet
) != 0;
701 // -----------------------------------------------------------------------------
702 void ODbTypeWizDialogSetup::setTitle(const OUString
& /*_sTitle*/)
704 OSL_FAIL( "ODbTypeWizDialogSetup::setTitle: not implemented!" );
708 //-------------------------------------------------------------------------
709 void ODbTypeWizDialogSetup::enableConfirmSettings( bool _bEnable
)
714 //-------------------------------------------------------------------------
717 bool lcl_handle( const Reference
< XInteractionHandler2
>& _rxHandler
, const Any
& _rRequest
)
719 OInteractionRequest
* pRequest
= new OInteractionRequest( _rRequest
);
720 Reference
< XInteractionRequest
> xRequest( pRequest
);
721 OInteractionAbort
* pAbort
= new OInteractionAbort
;
722 pRequest
->addContinuation( pAbort
);
724 return _rxHandler
->handleInteractionRequest( xRequest
);
728 //-------------------------------------------------------------------------
729 sal_Bool
ODbTypeWizDialogSetup::SaveDatabaseDocument()
731 Reference
< XInteractionHandler2
> xHandler( InteractionHandler::createWithParent(getORB(), 0) );
734 if (callSaveAsDialog() == sal_True
)
736 m_pImpl
->saveChanges(*m_pOutSet
);
737 Reference
< XPropertySet
> xDatasource
= m_pImpl
->getCurrentDataSource();
738 Reference
< XModel
> xModel( getDataSourceOrModel( xDatasource
), UNO_QUERY_THROW
);
739 Reference
< XStorable
> xStore( xModel
, UNO_QUERY_THROW
);
741 if ( m_pGeneralPage
->GetDatabaseCreationMode() == OGeneralPageWizard::eCreateNew
)
744 ::comphelper::NamedValueCollection
aArgs( xModel
->getArgs() );
745 aArgs
.put( "Overwrite", sal_Bool( sal_True
) );
746 aArgs
.put( "InteractionHandler", xHandler
);
747 aArgs
.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG
);
749 OUString sPath
= m_pImpl
->getDocumentUrl( *m_pOutSet
);
750 xStore
->storeAsURL( sPath
, aArgs
.getPropertyValues() );
752 if ( !m_pFinalPage
|| m_pFinalPage
->IsDatabaseDocumentToBeRegistered() )
753 RegisterDataSourceByLocation( sPath
);
758 catch ( const Exception
& e
)
760 Any aError
= ::cppu::getCaughtException();
763 if ( !lcl_handle( xHandler
, aError
) )
765 InteractiveIOException aRequest
;
766 aRequest
.Classification
= InteractionClassification_ERROR
;
767 if ( aError
.isExtractableTo( ::cppu::UnoType
< IOException
>::get() ) )
768 // assume savint the document faile
769 aRequest
.Code
= IOErrorCode_CANT_WRITE
;
771 aRequest
.Code
= IOErrorCode_GENERAL
;
772 aRequest
.Message
= e
.Message
;
773 aRequest
.Context
= e
.Context
;
774 lcl_handle( xHandler
, makeAny( aRequest
) );
780 // ------------------------------------------------------------------------
781 sal_Bool
ODbTypeWizDialogSetup::IsDatabaseDocumentToBeOpened() const
783 if ( m_pGeneralPage
->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting
)
786 if ( m_pFinalPage
!= NULL
)
787 return m_pFinalPage
->IsDatabaseDocumentToBeOpened();
792 // ------------------------------------------------------------------------
793 sal_Bool
ODbTypeWizDialogSetup::IsTableWizardToBeStarted() const
795 if ( m_pGeneralPage
->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting
)
798 if ( m_pFinalPage
!= NULL
)
799 return m_pFinalPage
->IsTableWizardToBeStarted();
804 //-------------------------------------------------------------------------
805 OUString
ODbTypeWizDialogSetup::getDefaultDatabaseType() const
807 OUString sEmbeddedURL
= m_pCollection
->getEmbeddedDatabase();
808 ::connectivity::DriversConfig
aDriverConfig(getORB());
811 if ( aDriverConfig
.getDriverFactoryName(sEmbeddedURL
).isEmpty() || !m_pImpl
->getDriver(sEmbeddedURL
).is() )
812 sEmbeddedURL
= OUString("sdbc:dbase:");
814 catch(const Exception
&)
816 sEmbeddedURL
= OUString("sdbc:dbase:");
822 //-------------------------------------------------------------------------
823 void ODbTypeWizDialogSetup::CreateDatabase()
826 OUString eType
= getDefaultDatabaseType();
827 if ( m_pCollection
->isEmbeddedDatabase(eType
) )
830 Reference
< XPropertySet
> xDatasource
= m_pImpl
->getCurrentDataSource();
831 OSL_ENSURE(xDatasource
.is(),"DataSource is null!");
832 if ( xDatasource
.is() )
833 xDatasource
->setPropertyValue( PROPERTY_INFO
, makeAny( m_pCollection
->getDefaultDBSettings( eType
) ) );
834 m_pImpl
->translateProperties(xDatasource
,*m_pOutSet
);
836 else if ( m_pCollection
->isFileSystemBased(eType
) )
838 Reference
< XSimpleFileAccess3
> xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
839 INetURLObject
aDBPathURL(m_sWorkPath
);
840 aDBPathURL
.Append(m_aDocURL
.getBase());
841 createUniqueFolderName(&aDBPathURL
);
842 OUString sPrefix
= eType
;
843 sUrl
= aDBPathURL
.GetMainURL( INetURLObject::NO_DECODE
);
844 xSimpleFileAccess
->createFolder(sUrl
);
845 sUrl
= sPrefix
.concat(sUrl
);
847 m_pOutSet
->Put(SfxStringItem(DSID_CONNECTURL
, sUrl
));
848 m_pImpl
->saveChanges(*m_pOutSet
);
851 //-------------------------------------------------------------------------
852 void ODbTypeWizDialogSetup::RegisterDataSourceByLocation(const OUString
& _sPath
)
854 Reference
< XPropertySet
> xDatasource
= m_pImpl
->getCurrentDataSource();
855 Reference
< XDatabaseContext
> xDatabaseContext( DatabaseContext::create(getORB()) );
856 Reference
< XNameAccess
> xNameAccessDatabaseContext(xDatabaseContext
, UNO_QUERY_THROW
);
857 INetURLObject
aURL( _sPath
);
858 OUString sFilename
= aURL
.getBase( INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
);
859 OUString sDatabaseName
= ::dbtools::createUniqueName(xNameAccessDatabaseContext
, sFilename
,sal_False
);
860 xDatabaseContext
->registerObject(sDatabaseName
, xDatasource
);
864 //-------------------------------------------------------------------------
865 sal_Bool
ODbTypeWizDialogSetup::callSaveAsDialog()
867 sal_Bool bRet
= sal_False
;
868 ::sfx2::FileDialogHelper
aFileDlg(
869 ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION
,
871 const SfxFilter
* pFilter
= getStandardDatabaseFilter();
874 INetURLObject
aWorkURL( m_sWorkPath
);
875 aFileDlg
.SetDisplayFolder( aWorkURL
.GetMainURL( INetURLObject::NO_DECODE
));
877 OUString sDefaultName
= String( ModuleRes( STR_DATABASEDEFAULTNAME
) );
878 OUString sExtension
= pFilter
->GetDefaultExtension();
879 sDefaultName
+= sExtension
.replaceAt( 0, 1, OUString() );
880 aWorkURL
.Append( sDefaultName
);
881 sDefaultName
= createUniqueFileName( aWorkURL
);
882 aFileDlg
.SetFileName( sDefaultName
);
884 aFileDlg
.AddFilter(pFilter
->GetUIName(),pFilter
->GetDefaultExtension());
885 aFileDlg
.SetCurrentFilter(pFilter
->GetUIName());
887 if ( aFileDlg
.Execute() == ERRCODE_NONE
)
889 m_aDocURL
= INetURLObject(aFileDlg
.GetPath());
891 if( m_aDocURL
.GetProtocol() != INET_PROT_NOT_VALID
)
893 OUString sFileName
= m_aDocURL
.GetMainURL( INetURLObject::NO_DECODE
);
894 if ( ::utl::UCBContentHelper::IsDocument(sFileName
) )
895 ::utl::UCBContentHelper::Kill(sFileName
);
896 m_pOutSet
->Put(SfxStringItem(DSID_DOCUMENT_URL
, sFileName
));
903 //-------------------------------------------------------------------------
904 void ODbTypeWizDialogSetup::createUniqueFolderName(INetURLObject
* pURL
)
906 Reference
< XSimpleFileAccess3
> xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
907 :: OUString sLastSegmentName
= pURL
->getName();
908 sal_Bool bFolderExists
= sal_True
;
910 while (bFolderExists
== sal_True
)
912 bFolderExists
= xSimpleFileAccess
->isFolder(pURL
->GetMainURL( INetURLObject::NO_DECODE
));
913 if (bFolderExists
== sal_True
)
916 pURL
->setName(sLastSegmentName
.concat(OUString::valueOf(i
)));
921 //-------------------------------------------------------------------------
922 String
ODbTypeWizDialogSetup::createUniqueFileName(const INetURLObject
& _rURL
)
924 Reference
< XSimpleFileAccess3
> xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
925 OUString BaseName
= _rURL
.getBase();
927 sal_Bool bElementExists
= sal_True
;
929 INetURLObject
aExistenceCheck( _rURL
);
930 for ( sal_Int32 i
= 1; bElementExists
; )
932 bElementExists
= xSimpleFileAccess
->exists( aExistenceCheck
.GetMainURL( INetURLObject::NO_DECODE
) );
933 if ( bElementExists
)
935 aExistenceCheck
.setBase( BaseName
.concat( OUString::valueOf( i
) ) );
939 return aExistenceCheck
.getName( INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
);
941 // -----------------------------------------------------------------------------
942 IWizardPageController
* ODbTypeWizDialogSetup::getPageController( TabPage
* _pCurrentPage
) const
944 OGenericAdministrationPage
* pPage
= static_cast<OGenericAdministrationPage
*>(_pCurrentPage
);
948 // -----------------------------------------------------------------------------
951 // .............................................................................
952 typedef ::cppu::WeakImplHelper1
< XTerminateListener
954 class AsyncLoader
: public AsyncLoader_Base
957 Reference
< XComponentLoader
> m_xFrameLoader
;
958 Reference
< XDesktop2
> m_xDesktop
;
959 Reference
< XInteractionHandler2
> m_xInteractionHandler
;
961 OAsyncronousLink m_aAsyncCaller
;
964 AsyncLoader( const Reference
< XComponentContext
>& _rxORB
, const OUString
& _rURL
);
968 // XTerminateListener
969 virtual void SAL_CALL
queryTermination( const com::sun::star::lang::EventObject
& Event
) throw (TerminationVetoException
, RuntimeException
);
970 virtual void SAL_CALL
notifyTermination( const com::sun::star::lang::EventObject
& Event
) throw (RuntimeException
);
972 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
);
975 DECL_LINK( OnOpenDocument
, void* );
978 // .............................................................................
979 AsyncLoader::AsyncLoader( const Reference
< XComponentContext
>& _rxORB
, const OUString
& _rURL
)
981 ,m_aAsyncCaller( LINK( this, AsyncLoader
, OnOpenDocument
) )
985 m_xDesktop
.set( Desktop::create(_rxORB
) );
986 m_xFrameLoader
.set( m_xDesktop
, UNO_QUERY_THROW
);
987 m_xInteractionHandler
= InteractionHandler::createWithParent(_rxORB
, 0);
989 catch( const Exception
& )
991 DBG_UNHANDLED_EXCEPTION();
995 // .............................................................................
996 void AsyncLoader::doLoadAsync()
998 OSL_ENSURE( !m_aAsyncCaller
.IsRunning(), "AsyncLoader:doLoadAsync: already running!" );
1003 if ( m_xDesktop
.is() )
1004 m_xDesktop
->addTerminateListener( this );
1006 catch( const Exception
& ) { DBG_UNHANDLED_EXCEPTION(); }
1008 m_aAsyncCaller
.Call( NULL
);
1011 // .............................................................................
1012 IMPL_LINK( AsyncLoader
, OnOpenDocument
, void*, /*_pEmptyArg*/ )
1016 if ( m_xFrameLoader
.is() )
1018 ::comphelper::NamedValueCollection aLoadArgs
;
1019 aLoadArgs
.put( "InteractionHandler", m_xInteractionHandler
);
1020 aLoadArgs
.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG
);
1022 Sequence
< PropertyValue
> aLoadArgPV
;
1023 aLoadArgs
>>= aLoadArgPV
;
1025 m_xFrameLoader
->loadComponentFromURL( m_sURL
,
1026 OUString( "_default" ),
1027 FrameSearchFlag::ALL
,
1032 catch( const Exception
& )
1035 // Such an exception happens for instance of the to-be-loaded document does not exist anymore.
1040 if ( m_xDesktop
.is() )
1041 m_xDesktop
->removeTerminateListener( this );
1043 catch( const Exception
& ) { DBG_UNHANDLED_EXCEPTION(); }
1049 // .............................................................................
1050 void SAL_CALL
AsyncLoader::queryTermination( const com::sun::star::lang::EventObject
& /*Event*/ ) throw (TerminationVetoException
, RuntimeException
)
1052 throw TerminationVetoException();
1055 // .............................................................................
1056 void SAL_CALL
AsyncLoader::notifyTermination( const com::sun::star::lang::EventObject
& /*Event*/ ) throw (RuntimeException
)
1059 // .............................................................................
1060 void SAL_CALL
AsyncLoader::disposing( const com::sun::star::lang::EventObject
& /*Source*/ ) throw (RuntimeException
)
1065 // -----------------------------------------------------------------------------
1066 sal_Bool
ODbTypeWizDialogSetup::onFinish()
1068 if ( m_pGeneralPage
->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting
)
1070 // we're not going to re-use the XModel we have - since the document the user
1071 // wants us to load could be a non-database document. Instead, we asynchronously
1072 // open the selected document. Thus, the wizard's return value is RET_CANCEL,
1073 // which means to not continue loading the database document
1074 if ( !OWizardMachine::Finnish( RET_CANCEL
) )
1077 Reference
< XComponentLoader
> xFrameLoader
;
1080 AsyncLoader
* pAsyncLoader
= new AsyncLoader( getORB(), m_pGeneralPage
->GetSelectedDocument().sURL
);
1081 ::rtl::Reference
< AsyncLoader
> xKeepAlive( pAsyncLoader
);
1082 pAsyncLoader
->doLoadAsync();
1084 catch( const Exception
& )
1086 DBG_UNHANDLED_EXCEPTION();
1092 if (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL
)
1094 skipUntil(PAGE_DBSETUPWIZARD_FINAL
);
1096 if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL
)
1097 return SaveDatabaseDocument() ? OWizardMachine::onFinish() : sal_False
;
1100 enableButtons( WZB_FINISH
, sal_False
);
1105 //.........................................................................
1106 } // namespace dbaui
1107 //.........................................................................
1109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */