1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <core_resource.hxx>
21 #include <dbwiz.hxx>
22 #include <dbu_dlg.hxx>
23 #include <strings.hrc>
24 #include <strings.hxx>
25 #include <dsitems.hxx>
26 #include "dsnItem.hxx"
27 #include <svl/stritem.hxx>
28 #include <svl/eitem.hxx>
29 #include <svl/intitem.hxx>
30 #include <stringconstants.hxx>
31 #include "adminpages.hxx"
32 #include "generalpage.hxx"
33 #include <stringlistitem.hxx>
34 #include <unotools/confignode.hxx>
35 #include "ConnectionPage.hxx"
36 #include "DriverSettings.hxx"
37 #include "DbAdminImpl.hxx"
38 #include <helpids.h>
39
40 namespace dbaui
41 {
42 using namespace svt;
43 using namespace com::sun::star::uno;
44 using namespace com::sun::star::sdbc;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::util;
47 using namespace com::sun::star::beans;
48 using namespace com::sun::star::container;
49
50 #define START_PAGE 0
51 #define CONNECTION_PAGE 1
52 #define ADDITIONAL_PAGE_DBASE 2
53 #define ADDITIONAL_PAGE_FLAT 3
54 #define ADDITIONAL_PAGE_LDAP 4
55 //5 was ADDITIONAL_PAGE_ADABAS
56 #define ADDITIONAL_PAGE_MYSQL_JDBC 6
57 #define ADDITIONAL_PAGE_MYSQL_ODBC 7
58 #define ADDITIONAL_PAGE_ORACLE_JDBC 8
59 #define ADDITIONAL_PAGE_ADO 9
60 #define ADDITIONAL_PAGE_ODBC 10
61 #define ADDITIONAL_USERDEFINED 11
62 #define ADDITIONAL_PAGE_MYSQL_NATIVE 12
63
64 // ODbTypeWizDialog
ODbTypeWizDialog(weld::Window * _pParent,SfxItemSet const * _pItems,const Reference<XComponentContext> & _rxORB,const css::uno::Any & _aDataSourceName)65 ODbTypeWizDialog::ODbTypeWizDialog(weld::Window* _pParent, SfxItemSet const * _pItems,
66 const Reference< XComponentContext >& _rxORB, const css::uno::Any& _aDataSourceName)
67 : WizardMachine(_pParent, WizardButtonFlags::NEXT | WizardButtonFlags::PREVIOUS | WizardButtonFlags::FINISH | WizardButtonFlags::CANCEL | WizardButtonFlags::HELP )
68 {
69 m_pImpl.reset(new ODbDataSourceAdministrationHelper(_rxORB, m_xAssistant.get(), _pParent, this));
70 m_pImpl->setDataSourceOrName(_aDataSourceName);
71 Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
72 m_pOutSet.reset(new SfxItemSet( *_pItems->GetPool(), _pItems->GetRanges() ));
73
74 m_pImpl->translateProperties(xDatasource, *m_pOutSet);
75 m_eType = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(*m_pOutSet);
76
77 defaultButton(WizardButtonFlags::NEXT);
78 enableButtons(WizardButtonFlags::FINISH, false);
79 enableAutomaticNextButtonState();
80
81 m_xPrevPage->set_help_id(HID_DBWIZ_PREVIOUS);
82 m_xNextPage->set_help_id(HID_DBWIZ_NEXT);
83 m_xCancel->set_help_id(HID_DBWIZ_CANCEL);
84 m_xFinish->set_help_id(HID_DBWIZ_FINISH);
85 // no local resources needed anymore
86
87 const DbuTypeCollectionItem& rCollectionItem = dynamic_cast<const DbuTypeCollectionItem&>(*_pItems->GetItem(DSID_TYPECOLLECTION));
88 m_pCollection = rCollectionItem.getCollection();
89
90 ActivatePage();
91 setTitleBase(DBA_RES(STR_DATABASE_TYPE_CHANGE));
92
93 m_xAssistant->set_current_page(0);
94 }
95
~ODbTypeWizDialog()96 ODbTypeWizDialog::~ODbTypeWizDialog()
97 {
98 }
99
IMPL_LINK(ODbTypeWizDialog,OnTypeSelected,OGeneralPage &,_rTabPage,void)100 IMPL_LINK(ODbTypeWizDialog, OnTypeSelected, OGeneralPage&, _rTabPage, void)
101 {
102 m_eType = _rTabPage.GetSelectedType();
103 const bool bURLRequired = m_pCollection->isConnectionUrlRequired(m_eType);
104 enableButtons(WizardButtonFlags::NEXT,bURLRequired);
105 enableButtons(WizardButtonFlags::FINISH,!bURLRequired);
106 }
107
determineNextState(WizardState _nCurrentState) const108 WizardState ODbTypeWizDialog::determineNextState( WizardState _nCurrentState ) const
109 {
110 WizardState nNextState = WZS_INVALID_STATE;
111 switch(_nCurrentState)
112 {
113 case START_PAGE:
114 switch(m_pCollection->determineType(m_eType))
115 {
116 case ::dbaccess::DST_MOZILLA:
117 case ::dbaccess::DST_OUTLOOK:
118 case ::dbaccess::DST_OUTLOOKEXP:
119 case ::dbaccess::DST_EVOLUTION:
120 case ::dbaccess::DST_EVOLUTION_GROUPWISE:
121 case ::dbaccess::DST_EVOLUTION_LDAP:
122 case ::dbaccess::DST_KAB:
123 case ::dbaccess::DST_MACAB:
124 nNextState = WZS_INVALID_STATE;
125 break;
126 case ::dbaccess::DST_MYSQL_NATIVE:
127 nNextState = ADDITIONAL_PAGE_MYSQL_NATIVE;
128 break;
129 default:
130 nNextState = CONNECTION_PAGE;
131 break;
132 }
133 break;
134 case CONNECTION_PAGE:
135 switch(m_pCollection->determineType(m_eType))
136 {
137 case ::dbaccess::DST_MOZILLA:
138 case ::dbaccess::DST_THUNDERBIRD:
139 case ::dbaccess::DST_OUTLOOK:
140 case ::dbaccess::DST_OUTLOOKEXP:
141 case ::dbaccess::DST_EVOLUTION:
142 case ::dbaccess::DST_EVOLUTION_GROUPWISE:
143 case ::dbaccess::DST_EVOLUTION_LDAP:
144 case ::dbaccess::DST_KAB:
145 case ::dbaccess::DST_MACAB:
146 case ::dbaccess::DST_MSACCESS:
147 case ::dbaccess::DST_MSACCESS_2007:
148 case ::dbaccess::DST_JDBC:
149 case ::dbaccess::DST_CALC:
150 case ::dbaccess::DST_WRITER:
151 nNextState = WZS_INVALID_STATE;
152 break;
153 case ::dbaccess::DST_DBASE:
154 nNextState = ADDITIONAL_PAGE_DBASE;
155 break;
156 case ::dbaccess::DST_FLAT:
157 nNextState = ADDITIONAL_PAGE_FLAT;
158 break;
159 case ::dbaccess::DST_LDAP:
160 nNextState = ADDITIONAL_PAGE_LDAP;
161 break;
162 case ::dbaccess::DST_MYSQL_JDBC:
163 nNextState = ADDITIONAL_PAGE_MYSQL_JDBC;
164 break;
165 case ::dbaccess::DST_MYSQL_ODBC:
166 nNextState = ADDITIONAL_PAGE_MYSQL_ODBC;
167 break;
168 case ::dbaccess::DST_ORACLE_JDBC:
169 nNextState = ADDITIONAL_PAGE_ORACLE_JDBC;
170 break;
171 case ::dbaccess::DST_ADO:
172 nNextState = ADDITIONAL_PAGE_ADO;
173 break;
174 case ::dbaccess::DST_ODBC:
175 nNextState = ADDITIONAL_PAGE_ODBC;
176 break;
177 default:
178 nNextState = WZS_INVALID_STATE;
179 break;
180 }
181 break;
182 }
183
184 return nNextState;
185 }
186
getOutputSet() const187 const SfxItemSet* ODbTypeWizDialog::getOutputSet() const
188 {
189 return m_pOutSet.get();
190 }
191
getWriteOutputSet()192 SfxItemSet* ODbTypeWizDialog::getWriteOutputSet()
193 {
194 return m_pOutSet.get();
195 }
196
createConnection()197 std::pair< Reference<XConnection>,bool> ODbTypeWizDialog::createConnection()
198 {
199 return m_pImpl->createConnection();
200 }
201
getORB() const202 Reference< XComponentContext > ODbTypeWizDialog::getORB() const
203 {
204 return m_pImpl->getORB();
205 }
206
getDriver()207 Reference< XDriver > ODbTypeWizDialog::getDriver()
208 {
209 return m_pImpl->getDriver();
210 }
211
getDatasourceType(const SfxItemSet & _rSet) const212 OUString ODbTypeWizDialog::getDatasourceType(const SfxItemSet& _rSet) const
213 {
214 return dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(_rSet);
215 }
216
clearPassword()217 void ODbTypeWizDialog::clearPassword()
218 {
219 m_pImpl->clearPassword();
220 }
221
createPage(WizardState _nState)222 std::unique_ptr<BuilderPage> ODbTypeWizDialog::createPage(WizardState _nState)
223 {
224 const char* pStringId = STR_PAGETITLE_ADVANCED;
225 std::unique_ptr<BuilderPage> xPage;
226
227 OString sIdent(OString::number(_nState));
228 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
229
230 switch(_nState)
231 {
232 case START_PAGE: // start state
233 {
234 xPage = std::make_unique<OGeneralPageDialog>(pPageContainer, this, *m_pOutSet);
235 OGeneralPage* pGeneralPage = static_cast<OGeneralPage*>(xPage.get());
236 pGeneralPage->SetTypeSelectHandler( LINK( this, ODbTypeWizDialog, OnTypeSelected));
237 pStringId = STR_PAGETITLE_GENERAL;
238 }
239 break;
240 case CONNECTION_PAGE:
241 xPage = OConnectionTabPage::Create(pPageContainer, this, m_pOutSet.get());
242 pStringId = STR_PAGETITLE_CONNECTION;
243 break;
244
245 case ADDITIONAL_PAGE_DBASE:
246 xPage = ODriversSettings::CreateDbase(pPageContainer, this, m_pOutSet.get());
247 break;
248 case ADDITIONAL_PAGE_FLAT:
249 xPage = ODriversSettings::CreateText(pPageContainer, this, m_pOutSet.get());
250 break;
251 case ADDITIONAL_PAGE_LDAP:
252 xPage = ODriversSettings::CreateLDAP(pPageContainer, this, m_pOutSet.get());
253 break;
254 case ADDITIONAL_PAGE_MYSQL_JDBC:
255 xPage = ODriversSettings::CreateMySQLJDBC(pPageContainer, this, m_pOutSet.get());
256 break;
257 case ADDITIONAL_PAGE_MYSQL_NATIVE:
258 xPage = ODriversSettings::CreateMySQLNATIVE(pPageContainer, this, m_pOutSet.get());
259 break;
260 case ADDITIONAL_PAGE_MYSQL_ODBC:
261 xPage = ODriversSettings::CreateMySQLODBC(pPageContainer, this, m_pOutSet.get());
262 break;
263 case ADDITIONAL_PAGE_ORACLE_JDBC:
264 xPage = ODriversSettings::CreateOracleJDBC(pPageContainer, this, m_pOutSet.get());
265 break;
266 case ADDITIONAL_PAGE_ADO:
267 xPage = ODriversSettings::CreateAdo(pPageContainer, this, m_pOutSet.get());
268 break;
269 case ADDITIONAL_PAGE_ODBC:
270 xPage = ODriversSettings::CreateODBC(pPageContainer, this, m_pOutSet.get());
271 break;
272 case ADDITIONAL_USERDEFINED:
273 xPage = ODriversSettings::CreateUser(pPageContainer, this, m_pOutSet.get());
274 break;
275 default:
276 OSL_FAIL("Wrong state!");
277 break;
278 }
279
280 // register ourself as modified listener
281 if ( xPage )
282 {
283 static_cast<OGenericAdministrationPage*>(xPage.get())->SetServiceFactory( m_pImpl->getORB() );
284 static_cast<OGenericAdministrationPage*>(xPage.get())->SetAdminDialog(this,this);
285 m_xAssistant->set_page_title(sIdent, DBA_RES(pStringId));
286 defaultButton( _nState == START_PAGE ? WizardButtonFlags::NEXT : WizardButtonFlags::FINISH );
287 enableButtons( WizardButtonFlags::FINISH, _nState != START_PAGE);
288 }
289 return xPage;
290 }
291
leaveState(WizardState _nState)292 bool ODbTypeWizDialog::leaveState(WizardState _nState)
293 {
294 SfxTabPage* pPage = static_cast<SfxTabPage*>(WizardMachine::GetPage(_nState));
295 if ( pPage )
296 pPage->FillItemSet(m_pOutSet.get());
297 return true;
298 }
299
setTitle(const OUString & _sTitle)300 void ODbTypeWizDialog::setTitle(const OUString& _sTitle)
301 {
302 m_xAssistant->set_title(_sTitle);
303 }
304
enableConfirmSettings(bool _bEnable)305 void ODbTypeWizDialog::enableConfirmSettings( bool _bEnable )
306 {
307 enableButtons( WizardButtonFlags::FINISH, _bEnable );
308 // TODO:
309 // this is hacky. At the moment, this method is used in only one case.
310 // As soon as it is to be used more wide-spread, we should find a proper concept
311 // for enabling both the Next and Finish buttons, depending on the current page state.
312 // Plus, the concept must also care for the case where those pages are embedded into
313 // anormal tab dialog.
314 }
315
saveDatasource()316 void ODbTypeWizDialog::saveDatasource()
317 {
318 SfxTabPage* pPage = static_cast<SfxTabPage*>(WizardMachine::GetPage(getCurrentState()));
319 if ( pPage )
320 pPage->FillItemSet(m_pOutSet.get());
321
322 OUString sOldURL;
323 if ( m_pImpl->getCurrentDataSource().is() )
324 m_pImpl->getCurrentDataSource()->getPropertyValue(PROPERTY_URL) >>= sOldURL;
325 DataSourceInfoConverter::convert( getORB(), m_pCollection,sOldURL,m_eType,m_pImpl->getCurrentDataSource());
326 }
327
getPageController(BuilderPage * pCurrentPage) const328 vcl::IWizardPageController* ODbTypeWizDialog::getPageController(BuilderPage* pCurrentPage) const
329 {
330 OGenericAdministrationPage* pPage = static_cast<OGenericAdministrationPage*>(pCurrentPage);
331 return pPage;
332 }
333
onFinish()334 bool ODbTypeWizDialog::onFinish()
335 {
336 saveDatasource();
337 return m_pImpl->saveChanges(*m_pOutSet) && WizardMachine::onFinish();
338 }
339
340 } // namespace dbaui
341
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
343