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 .
20 #include "controlwizard.hxx"
21 #include <tools/debug.hxx>
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 #include <com/sun/star/sdb/DatabaseContext.hpp>
25 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
26 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
27 #include <com/sun/star/container/XChild.hpp>
28 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
31 #include <com/sun/star/drawing/XDrawView.hpp>
32 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
33 #include <com/sun/star/sdb/CommandType.hpp>
34 #include <com/sun/star/sdbc/SQLWarning.hpp>
35 #include <com/sun/star/sdb/SQLContext.hpp>
36 #include <com/sun/star/task/InteractionHandler.hpp>
37 #include <comphelper/types.hxx>
38 #include <connectivity/dbtools.hxx>
39 #include <comphelper/interaction.hxx>
40 #include <vcl/stdtext.hxx>
41 #include <connectivity/conncleanup.hxx>
42 #include <com/sun/star/sdbc/DataType.hpp>
43 #include <tools/urlobj.hxx>
44 #include <vcl/layout.hxx>
45 #include <osl/diagnose.h>
47 #define WINDOW_SIZE_X 240
48 #define WINDOW_SIZE_Y 185
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::awt
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::sdb
;
56 using namespace ::com::sun::star::sdbc
;
57 using namespace ::com::sun::star::sdbcx
;
58 using namespace ::com::sun::star::beans
;
59 using namespace ::com::sun::star::container
;
60 using namespace ::com::sun::star::drawing
;
61 using namespace ::com::sun::star::frame
;
62 using namespace ::com::sun::star::sheet
;
63 using namespace ::com::sun::star::form
;
64 using namespace ::com::sun::star::task
;
65 using namespace ::svt
;
66 using namespace ::comphelper
;
67 using namespace ::dbtools
;
69 struct OAccessRegulator
71 friend class OControlWizardPage
;
74 OAccessRegulator() { }
77 OControlWizardPage::OControlWizardPage( OControlWizard
* _pParent
, const OString
& rID
, const OUString
& rUIXMLDescription
)
78 :OControlWizardPage_Base( _pParent
, rID
, rUIXMLDescription
)
79 ,m_pFormDatasourceLabel(nullptr)
80 ,m_pFormDatasource(nullptr)
81 ,m_pFormContentTypeLabel(nullptr)
82 ,m_pFormContentType(nullptr)
83 ,m_pFormTableLabel(nullptr)
84 ,m_pFormTable(nullptr)
88 OControlWizardPage::~OControlWizardPage()
93 void OControlWizardPage::dispose()
95 m_pFormDatasourceLabel
.clear();
96 m_pFormDatasource
.clear();
97 m_pFormContentTypeLabel
.clear();
98 m_pFormContentType
.clear();
99 m_pFormTableLabel
.clear();
100 m_pFormTable
.clear();
101 OControlWizardPage_Base::dispose();
104 OControlWizard
* OControlWizardPage::getDialog()
106 return static_cast< OControlWizard
* >(GetParent());
109 const OControlWizard
* OControlWizardPage::getDialog() const
111 return static_cast< OControlWizard
* >(GetParent());
115 bool OControlWizardPage::updateContext()
117 return getDialog()->updateContext(OAccessRegulator());
121 Reference
< XConnection
> OControlWizardPage::getFormConnection() const
123 return getDialog()->getFormConnection(OAccessRegulator());
127 void OControlWizardPage::setFormConnection( const Reference
< XConnection
>& _rxConn
, bool _bAutoDispose
)
129 getDialog()->setFormConnection( OAccessRegulator(), _rxConn
, _bAutoDispose
);
133 const OControlWizardContext
& OControlWizardPage::getContext()
135 return getDialog()->getContext();
139 void OControlWizardPage::fillListBox(ListBox
& _rList
, const Sequence
< OUString
>& _rItems
)
142 const OUString
* pItems
= _rItems
.getConstArray();
143 const OUString
* pEnd
= pItems
+ _rItems
.getLength();
144 ::svt::WizardTypes::WizardState nPos
;
145 sal_Int32 nIndex
= 0;
146 for (;pItems
< pEnd
; ++pItems
, ++nIndex
)
148 nPos
= _rList
.InsertEntry(*pItems
);
149 _rList
.SetEntryData(nPos
, reinterpret_cast<void*>(nIndex
));
154 void OControlWizardPage::fillListBox(ComboBox
& _rList
, const Sequence
< OUString
>& _rItems
)
157 const OUString
* pItems
= _rItems
.getConstArray();
158 const OUString
* pEnd
= pItems
+ _rItems
.getLength();
159 ::svt::WizardTypes::WizardState nPos
;
160 for (;pItems
< pEnd
; ++pItems
)
162 nPos
= _rList
.InsertEntry(*pItems
);
163 _rList
.SetEntryData(nPos
, nullptr);
168 void OControlWizardPage::enableFormDatasourceDisplay()
170 if (m_pFormContentType
)
174 VclFrame
*_pFrame
= get
<VclFrame
>("sourceframe");
176 get(m_pFormContentType
,"contenttype");
177 get(m_pFormContentTypeLabel
,"contenttypelabel");
178 get(m_pFormDatasource
, "datasource");
179 get(m_pFormDatasourceLabel
, "datasourcelabel");
180 get(m_pFormTable
,"formtable");
181 get(m_pFormTableLabel
,"formtablelabel");
183 const OControlWizardContext
& rContext
= getContext();
184 if ( rContext
.bEmbedded
)
186 m_pFormDatasourceLabel
->Hide();
187 m_pFormDatasource
->Hide();
188 m_pFormContentTypeLabel
->SetPosPixel(m_pFormDatasourceLabel
->GetPosPixel());
189 m_pFormContentType
->SetPosPixel(m_pFormDatasource
->GetPosPixel());
190 m_pFormTableLabel
->SetPosPixel(::Point(m_pFormDatasourceLabel
->GetPosPixel().X(),m_pFormTableLabel
->GetPosPixel().Y()));
191 m_pFormTable
->SetPosPixel(::Point(m_pFormDatasource
->GetPosPixel().X(),m_pFormTable
->GetPosPixel().Y()));
195 void OControlWizardPage::initializePage()
197 if (m_pFormDatasource
&& m_pFormContentTypeLabel
&& m_pFormTable
)
199 const OControlWizardContext
& rContext
= getContext();
200 OUString sDataSource
;
202 sal_Int32 nCommandType
= CommandType::COMMAND
;
205 rContext
.xForm
->getPropertyValue("DataSourceName") >>= sDataSource
;
206 rContext
.xForm
->getPropertyValue("Command") >>= sCommand
;
207 rContext
.xForm
->getPropertyValue("CommandType") >>= nCommandType
;
209 catch(const Exception
&)
211 OSL_FAIL("OControlWizardPage::initializePage: caught an exception!");
214 INetURLObject
aURL( sDataSource
);
215 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
216 sDataSource
= aURL
.GetName(INetURLObject::DecodeMechanism::WithCharset
);
217 m_pFormDatasource
->SetText(sDataSource
);
218 m_pFormTable
->SetText(sCommand
);
220 const char* pCommandTypeResourceId
= nullptr;
221 switch (nCommandType
)
223 case CommandType::TABLE
:
224 pCommandTypeResourceId
= RID_STR_TYPE_TABLE
;
227 case CommandType::QUERY
:
228 pCommandTypeResourceId
= RID_STR_TYPE_QUERY
;
232 pCommandTypeResourceId
= RID_STR_TYPE_COMMAND
;
235 m_pFormContentType
->SetText(compmodule::ModuleRes(pCommandTypeResourceId
));
238 OControlWizardPage_Base::initializePage();
241 OControlWizard::OControlWizard( vcl::Window
* _pParent
,
242 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
243 :OWizardMachine(_pParent
, WizardButtonFlags::CANCEL
| WizardButtonFlags::PREVIOUS
| WizardButtonFlags::NEXT
| WizardButtonFlags::FINISH
)
244 ,m_xContext(_rxContext
)
246 m_aContext
.xObjectModel
= _rxObjectModel
;
249 SetPageSizePixel(LogicToPixel(::Size(WINDOW_SIZE_X
, WINDOW_SIZE_Y
), MapMode(MapUnit::MapAppFont
)));
250 defaultButton(WizardButtonFlags::NEXT
);
251 enableButtons(WizardButtonFlags::FINISH
, false);
254 OControlWizard::~OControlWizard()
258 short OControlWizard::Execute()
260 // get the class id of the control we're dealing with
261 sal_Int16 nClassId
= FormComponentType::CONTROL
;
264 getContext().xObjectModel
->getPropertyValue("ClassId") >>= nClassId
;
266 catch(const Exception
&)
268 OSL_FAIL("OControlWizard::activate: could not obtain the class id!");
270 if (!approveControl(nClassId
))
272 // TODO: MessageBox or exception
278 return OControlWizard_Base::Execute();
282 void OControlWizard::implDetermineShape()
284 Reference
< XIndexAccess
> xPageObjects(m_aContext
.xDrawPage
, UNO_QUERY
);
285 DBG_ASSERT(xPageObjects
.is(), "OControlWizard::implDetermineShape: invalid page!");
287 // for comparing the model
288 Reference
< XControlModel
> xModelCompare(m_aContext
.xObjectModel
, UNO_QUERY
);
290 if (xPageObjects
.is())
292 // loop through all objects of the page
293 sal_Int32 nObjects
= xPageObjects
->getCount();
294 Reference
< XControlShape
> xControlShape
;
295 Reference
< XControlModel
> xControlModel
;
296 for (sal_Int32 i
=0; i
<nObjects
; ++i
)
298 if (xPageObjects
->getByIndex(i
) >>= xControlShape
)
299 { // it _is_ a control shape
300 xControlModel
= xControlShape
->getControl();
301 DBG_ASSERT(xControlModel
.is(), "OControlWizard::implDetermineShape: control shape without model!");
302 if (xModelCompare
.get() == xControlModel
.get())
304 m_aContext
.xObjectShape
= xControlShape
;
313 void OControlWizard::implDetermineForm()
315 Reference
< XChild
> xModelAsChild(m_aContext
.xObjectModel
, UNO_QUERY
);
316 Reference
< XInterface
> xControlParent
;
317 if (xModelAsChild
.is())
318 xControlParent
= xModelAsChild
->getParent();
320 m_aContext
.xForm
.set(xControlParent
, UNO_QUERY
);
321 m_aContext
.xRowSet
.set(xControlParent
, UNO_QUERY
);
322 DBG_ASSERT(m_aContext
.xForm
.is() && m_aContext
.xRowSet
.is(),
323 "OControlWizard::implDetermineForm: missing some interfaces of the control parent!");
328 void OControlWizard::implDeterminePage()
332 // get the document model
333 Reference
< XChild
> xControlAsChild(m_aContext
.xObjectModel
, UNO_QUERY
);
334 Reference
< XChild
> xModelSearch(xControlAsChild
->getParent(), UNO_QUERY
);
336 Reference
< XModel
> xModel(xModelSearch
, UNO_QUERY
);
337 while (xModelSearch
.is() && !xModel
.is())
339 xModelSearch
.set(xModelSearch
->getParent(), UNO_QUERY
);
340 xModel
.set(xModelSearch
, UNO_QUERY
);
343 Reference
< XDrawPage
> xPage
;
346 m_aContext
.xDocumentModel
= xModel
;
348 Reference
< XDrawPageSupplier
> xPageSupp(xModel
, UNO_QUERY
);
350 { // it's a document with only one page -> Writer
351 xPage
= xPageSupp
->getDrawPage();
355 // get the controller currently working on this model
356 Reference
< XController
> xController
= xModel
->getCurrentController();
357 DBG_ASSERT(xController
.is(), "OControlWizard::implDeterminePage: no current controller!");
359 // maybe it's a spreadsheet
360 Reference
< XSpreadsheetView
> xView(xController
, UNO_QUERY
);
363 Reference
< XSpreadsheet
> xSheet
= xView
->getActiveSheet();
364 xPageSupp
.set(xSheet
, UNO_QUERY
);
365 DBG_ASSERT(xPageSupp
.is(), "OControlWizard::implDeterminePage: a spreadsheet which is no page supplier!");
367 xPage
= xPageSupp
->getDrawPage();
370 { // can be a draw/impress doc only
371 Reference
< XDrawView
> xDrawView(xController
, UNO_QUERY
);
372 DBG_ASSERT(xDrawView
.is(), "OControlWizard::implDeterminePage: no alternatives left ... can't determine the page!");
374 xPage
= xDrawView
->getCurrentPage();
380 DBG_ASSERT(xPage
.is(), "OControlWizard::implDeterminePage: can't determine the page (no model)!");
382 m_aContext
.xDrawPage
= xPage
;
384 catch(const Exception
&)
386 OSL_FAIL("OControlWizard::implDeterminePage: caught an exception!");
391 void OControlWizard::implGetDSContext()
395 DBG_ASSERT(m_xContext
.is(), "OControlWizard::implGetDSContext: invalid service factory!");
397 m_aContext
.xDatasourceContext
= DatabaseContext::create(m_xContext
);
399 catch(const Exception
&)
401 OSL_FAIL("OControlWizard::implGetDSContext: invalid database context!");
406 Reference
< XConnection
> OControlWizard::getFormConnection(const OAccessRegulator
&) const
408 return getFormConnection();
411 Reference
< XConnection
> OControlWizard::getFormConnection() const
413 Reference
< XConnection
> xConn
;
416 if ( !::dbtools::isEmbeddedInDatabase(m_aContext
.xForm
,xConn
) )
417 m_aContext
.xForm
->getPropertyValue("ActiveConnection") >>= xConn
;
419 catch(const Exception
&)
421 OSL_FAIL("OControlWizard::getFormConnection: caught an exception!");
427 void OControlWizard::setFormConnection( const OAccessRegulator
& _rAccess
, const Reference
< XConnection
>& _rxConn
, bool _bAutoDispose
)
431 Reference
< XConnection
> xOldConn
= getFormConnection(_rAccess
);
432 if (xOldConn
.get() == _rxConn
.get())
435 disposeComponent(xOldConn
);
437 // set the new connection
440 // for this, use an AutoDisposer (so the conn is cleaned up when the form dies or gets another connection)
441 Reference
< XRowSet
> xFormRowSet( m_aContext
.xForm
, UNO_QUERY
);
442 rtl::Reference
<OAutoConnectionDisposer
> pAutoDispose
= new OAutoConnectionDisposer( xFormRowSet
, _rxConn
);
446 m_aContext
.xForm
->setPropertyValue("ActiveConnection", makeAny( _rxConn
) );
449 catch(const Exception
&)
451 OSL_FAIL("OControlWizard::setFormConnection: caught an exception!");
456 bool OControlWizard::updateContext(const OAccessRegulator
&)
458 return initContext();
461 Reference
< XInteractionHandler
> OControlWizard::getInteractionHandler(weld::Window
* _pWindow
) const
463 Reference
< XInteractionHandler
> xHandler
;
466 xHandler
.set( InteractionHandler::createWithParent(m_xContext
, nullptr), UNO_QUERY_THROW
);
468 catch(const Exception
&) { }
471 const OUString
sInteractionHandlerServiceName("com.sun.star.task.InteractionHandler");
472 ShowServiceNotAvailableError(_pWindow
, sInteractionHandlerServiceName
, true);
477 bool OControlWizard::initContext()
479 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OGroupBoxWizard::initContext: have no control model to work with!");
480 if (!m_aContext
.xObjectModel
.is())
484 m_aContext
.xForm
.clear();
485 m_aContext
.xRowSet
.clear();
486 m_aContext
.xDocumentModel
.clear();
487 m_aContext
.xDrawPage
.clear();
488 m_aContext
.xObjectShape
.clear();
489 m_aContext
.aFieldNames
.realloc(0);
491 m_aContext
.xObjectContainer
.clear();
492 m_aContext
.aTypes
.clear();
493 m_aContext
.bEmbedded
= false;
496 Reference
< XPreparedStatement
> xStatement
;
499 // get the datasource context
502 // first, determine the form the control belongs to
505 // need the page, too
508 // the shape of the control
509 implDetermineShape();
511 // get the columns of the object the settings refer to
512 Reference
< XNameAccess
> xColumns
;
514 if (m_aContext
.xForm
.is())
516 // collect some properties of the form
517 OUString sObjectName
= ::comphelper::getString(m_aContext
.xForm
->getPropertyValue("Command"));
518 sal_Int32 nObjectType
= ::comphelper::getINT32(m_aContext
.xForm
->getPropertyValue("CommandType"));
520 // calculate the connection the rowset is working with
521 Reference
< XConnection
> xConnection
;
522 m_aContext
.bEmbedded
= ::dbtools::isEmbeddedInDatabase( m_aContext
.xForm
, xConnection
);
523 if ( !m_aContext
.bEmbedded
)
524 xConnection
= ::dbtools::connectRowset( m_aContext
.xRowSet
, m_xContext
);
527 if (xConnection
.is())
533 Reference
< XTablesSupplier
> xSupplyTables(xConnection
, UNO_QUERY
);
534 if (xSupplyTables
.is() && xSupplyTables
->getTables().is() && xSupplyTables
->getTables()->hasByName(sObjectName
))
536 Reference
< XColumnsSupplier
> xSupplyColumns
;
537 m_aContext
.xObjectContainer
= xSupplyTables
->getTables();
538 m_aContext
.xObjectContainer
->getByName(sObjectName
) >>= xSupplyColumns
;
539 DBG_ASSERT(xSupplyColumns
.is(), "OControlWizard::initContext: invalid table columns!");
540 xColumns
= xSupplyColumns
->getColumns();
546 Reference
< XQueriesSupplier
> xSupplyQueries(xConnection
, UNO_QUERY
);
547 if (xSupplyQueries
.is() && xSupplyQueries
->getQueries().is() && xSupplyQueries
->getQueries()->hasByName(sObjectName
))
549 Reference
< XColumnsSupplier
> xSupplyColumns
;
550 m_aContext
.xObjectContainer
= xSupplyQueries
->getQueries();
551 m_aContext
.xObjectContainer
->getByName(sObjectName
) >>= xSupplyColumns
;
552 DBG_ASSERT(xSupplyColumns
.is(), "OControlWizard::initContext: invalid query columns!");
553 xColumns
= xSupplyColumns
->getColumns();
559 xStatement
= xConnection
->prepareStatement(sObjectName
);
561 // not interested in any results, only in the fields
562 Reference
< XPropertySet
> xStatementProps(xStatement
, UNO_QUERY
);
563 xStatementProps
->setPropertyValue("MaxRows", makeAny(sal_Int32(0)));
565 // TODO: think about handling local SQLExceptions here ...
566 Reference
< XColumnsSupplier
> xSupplyCols(xStatement
->executeQuery(), UNO_QUERY
);
567 if (xSupplyCols
.is())
568 xColumns
= xSupplyCols
->getColumns();
576 m_aContext
.aFieldNames
= xColumns
->getElementNames();
577 const OUString
* pBegin
= m_aContext
.aFieldNames
.getConstArray();
578 const OUString
* pEnd
= pBegin
+ m_aContext
.aFieldNames
.getLength();
579 for(;pBegin
!= pEnd
;++pBegin
)
581 sal_Int32 nFieldType
= DataType::OTHER
;
584 Reference
< XPropertySet
> xColumn
;
585 xColumns
->getByName(*pBegin
) >>= xColumn
;
586 xColumn
->getPropertyValue("Type") >>= nFieldType
;
588 catch(const Exception
&)
590 OSL_FAIL("OControlWizard::initContext: unexpected exception while gathering column information!");
592 m_aContext
.aTypes
.emplace(*pBegin
,nFieldType
);
596 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
597 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
598 catch(const SQLException
& e
) { aSQLException
<<= e
; }
599 catch(const Exception
&)
601 OSL_FAIL("OControlWizard::initContext: could not retrieve the control context (caught an exception)!");
604 ::comphelper::disposeComponent(xStatement
);
606 if (aSQLException
.hasValue())
607 { // an SQLException (or derivee) was thrown ...
609 // prepend an extra SQLContext explaining what we were doing
611 aContext
.Message
= compmodule::ModuleRes(RID_STR_COULDNOTOPENTABLE
);
612 aContext
.NextException
= aSQLException
;
614 // create an interaction handler to display this exception
615 Reference
< XInteractionHandler
> xHandler
= getInteractionHandler(GetFrameWeld());
616 if ( !xHandler
.is() )
619 Reference
< XInteractionRequest
> xRequest
= new OInteractionRequest(makeAny(aContext
));
622 xHandler
->handle(xRequest
);
624 catch(const Exception
&) { }
628 return m_aContext
.aFieldNames
.hasElements();
632 void OControlWizard::commitControlSettings(OControlWizardSettings
const * _pSettings
)
634 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OControlWizard::commitControlSettings: have no control model to work with!");
635 if (!m_aContext
.xObjectModel
.is())
638 // the only thing we have at the moment is the label
641 Reference
< XPropertySetInfo
> xInfo
= m_aContext
.xObjectModel
->getPropertySetInfo();
642 if (xInfo
.is() && xInfo
->hasPropertyByName("Label"))
644 OUString
sControlLabel(_pSettings
->sControlLabel
);
645 m_aContext
.xObjectModel
->setPropertyValue(
647 makeAny(sControlLabel
)
651 catch(const Exception
&)
653 OSL_FAIL("OControlWizard::commitControlSettings: could not commit the basic control settings!");
658 void OControlWizard::initControlSettings(OControlWizardSettings
* _pSettings
)
660 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OControlWizard::initControlSettings: have no control model to work with!");
661 if (!m_aContext
.xObjectModel
.is())
664 // initialize some settings from the control model give
667 OUString
sLabelPropertyName("Label");
668 Reference
< XPropertySetInfo
> xInfo
= m_aContext
.xObjectModel
->getPropertySetInfo();
669 if (xInfo
.is() && xInfo
->hasPropertyByName(sLabelPropertyName
))
671 OUString sControlLabel
;
672 m_aContext
.xObjectModel
->getPropertyValue(sLabelPropertyName
) >>= sControlLabel
;
673 _pSettings
->sControlLabel
= sControlLabel
;
676 catch(const Exception
&)
678 OSL_FAIL("OControlWizard::initControlSettings: could not retrieve the basic control settings!");
683 bool OControlWizard::needDatasourceSelection()
686 return !getContext().aFieldNames
.hasElements();
687 // if we got fields, the data source is valid ...
694 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */