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>
46 #define WINDOW_SIZE_X 240
47 #define WINDOW_SIZE_Y 185
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::awt
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::com::sun::star::sdb
;
55 using namespace ::com::sun::star::sdbc
;
56 using namespace ::com::sun::star::sdbcx
;
57 using namespace ::com::sun::star::beans
;
58 using namespace ::com::sun::star::container
;
59 using namespace ::com::sun::star::drawing
;
60 using namespace ::com::sun::star::frame
;
61 using namespace ::com::sun::star::sheet
;
62 using namespace ::com::sun::star::form
;
63 using namespace ::com::sun::star::task
;
64 using namespace ::svt
;
65 using namespace ::comphelper
;
66 using namespace ::dbtools
;
68 struct OAccessRegulator
70 friend class OControlWizardPage
;
73 OAccessRegulator() { }
76 OControlWizardPage::OControlWizardPage( OControlWizard
* _pParent
, const OString
& rID
, const OUString
& rUIXMLDescription
)
77 :OControlWizardPage_Base( _pParent
, rID
, rUIXMLDescription
)
78 ,m_pFormDatasourceLabel(nullptr)
79 ,m_pFormDatasource(nullptr)
80 ,m_pFormContentTypeLabel(nullptr)
81 ,m_pFormContentType(nullptr)
82 ,m_pFormTableLabel(nullptr)
83 ,m_pFormTable(nullptr)
87 OControlWizardPage::~OControlWizardPage()
92 void OControlWizardPage::dispose()
94 m_pFormDatasourceLabel
.clear();
95 m_pFormDatasource
.clear();
96 m_pFormContentTypeLabel
.clear();
97 m_pFormContentType
.clear();
98 m_pFormTableLabel
.clear();
100 OControlWizardPage_Base::dispose();
103 OControlWizard
* OControlWizardPage::getDialog()
105 return static_cast< OControlWizard
* >(GetParent());
108 const OControlWizard
* OControlWizardPage::getDialog() const
110 return static_cast< OControlWizard
* >(GetParent());
114 bool OControlWizardPage::updateContext()
116 return getDialog()->updateContext(OAccessRegulator());
120 Reference
< XConnection
> OControlWizardPage::getFormConnection() const
122 return getDialog()->getFormConnection(OAccessRegulator());
126 void OControlWizardPage::setFormConnection( const Reference
< XConnection
>& _rxConn
, bool _bAutoDispose
)
128 getDialog()->setFormConnection( OAccessRegulator(), _rxConn
, _bAutoDispose
);
132 const OControlWizardContext
& OControlWizardPage::getContext()
134 return getDialog()->getContext();
138 void OControlWizardPage::fillListBox(ListBox
& _rList
, const Sequence
< OUString
>& _rItems
)
141 const OUString
* pItems
= _rItems
.getConstArray();
142 const OUString
* pEnd
= pItems
+ _rItems
.getLength();
143 ::svt::WizardTypes::WizardState nPos
;
144 sal_Int32 nIndex
= 0;
145 for (;pItems
< pEnd
; ++pItems
, ++nIndex
)
147 nPos
= _rList
.InsertEntry(*pItems
);
148 _rList
.SetEntryData(nPos
, reinterpret_cast<void*>(nIndex
));
153 void OControlWizardPage::fillListBox(ComboBox
& _rList
, const Sequence
< OUString
>& _rItems
)
156 const OUString
* pItems
= _rItems
.getConstArray();
157 const OUString
* pEnd
= pItems
+ _rItems
.getLength();
158 ::svt::WizardTypes::WizardState nPos
;
159 for (;pItems
< pEnd
; ++pItems
)
161 nPos
= _rList
.InsertEntry(*pItems
);
162 _rList
.SetEntryData(nPos
, reinterpret_cast<void*>(0));
167 void OControlWizardPage::enableFormDatasourceDisplay()
169 if (m_pFormContentType
)
173 VclFrame
*_pFrame
= get
<VclFrame
>("sourceframe");
175 get(m_pFormContentType
,"contenttype");
176 get(m_pFormContentTypeLabel
,"contenttypelabel");
177 get(m_pFormDatasource
, "datasource");
178 get(m_pFormDatasourceLabel
, "datasourcelabel");
179 get(m_pFormTable
,"formtable");
180 get(m_pFormTableLabel
,"formtablelabel");
182 const OControlWizardContext
& rContext
= getContext();
183 if ( rContext
.bEmbedded
)
185 m_pFormDatasourceLabel
->Hide();
186 m_pFormDatasource
->Hide();
187 m_pFormContentTypeLabel
->SetPosPixel(m_pFormDatasourceLabel
->GetPosPixel());
188 m_pFormContentType
->SetPosPixel(m_pFormDatasource
->GetPosPixel());
189 m_pFormTableLabel
->SetPosPixel(::Point(m_pFormDatasourceLabel
->GetPosPixel().X(),m_pFormTableLabel
->GetPosPixel().Y()));
190 m_pFormTable
->SetPosPixel(::Point(m_pFormDatasource
->GetPosPixel().X(),m_pFormTable
->GetPosPixel().Y()));
194 void OControlWizardPage::initializePage()
196 if (m_pFormDatasource
&& m_pFormContentTypeLabel
&& m_pFormTable
)
198 const OControlWizardContext
& rContext
= getContext();
199 OUString sDataSource
;
201 sal_Int32 nCommandType
= CommandType::COMMAND
;
204 rContext
.xForm
->getPropertyValue("DataSourceName") >>= sDataSource
;
205 rContext
.xForm
->getPropertyValue("Command") >>= sCommand
;
206 rContext
.xForm
->getPropertyValue("CommandType") >>= nCommandType
;
208 catch(const Exception
&)
210 OSL_FAIL("OControlWizardPage::initializePage: caught an exception!");
213 INetURLObject
aURL( sDataSource
);
214 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
215 sDataSource
= aURL
.GetName(INetURLObject::DecodeMechanism::WithCharset
);
216 m_pFormDatasource
->SetText(sDataSource
);
217 m_pFormTable
->SetText(sCommand
);
219 const char* pCommandTypeResourceId
= nullptr;
220 switch (nCommandType
)
222 case CommandType::TABLE
:
223 pCommandTypeResourceId
= RID_STR_TYPE_TABLE
;
226 case CommandType::QUERY
:
227 pCommandTypeResourceId
= RID_STR_TYPE_QUERY
;
231 pCommandTypeResourceId
= RID_STR_TYPE_COMMAND
;
234 m_pFormContentType
->SetText(compmodule::ModuleRes(pCommandTypeResourceId
));
237 OControlWizardPage_Base::initializePage();
240 OControlWizard::OControlWizard( vcl::Window
* _pParent
,
241 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
242 :OWizardMachine(_pParent
, WizardButtonFlags::CANCEL
| WizardButtonFlags::PREVIOUS
| WizardButtonFlags::NEXT
| WizardButtonFlags::FINISH
)
243 ,m_xContext(_rxContext
)
245 m_aContext
.xObjectModel
= _rxObjectModel
;
248 SetPageSizePixel(LogicToPixel(::Size(WINDOW_SIZE_X
, WINDOW_SIZE_Y
), MapMode(MapUnit::MapAppFont
)));
249 defaultButton(WizardButtonFlags::NEXT
);
250 enableButtons(WizardButtonFlags::FINISH
, false);
253 OControlWizard::~OControlWizard()
257 short OControlWizard::Execute()
259 // get the class id of the control we're dealing with
260 sal_Int16 nClassId
= FormComponentType::CONTROL
;
263 getContext().xObjectModel
->getPropertyValue("ClassId") >>= nClassId
;
265 catch(const Exception
&)
267 OSL_FAIL("OControlWizard::activate: could not obtain the class id!");
269 if (!approveControl(nClassId
))
271 // TODO: MessageBox or exception
277 return OControlWizard_Base::Execute();
281 void OControlWizard::implDetermineShape()
283 Reference
< XIndexAccess
> xPageObjects(m_aContext
.xDrawPage
, UNO_QUERY
);
284 DBG_ASSERT(xPageObjects
.is(), "OControlWizard::implDetermineShape: invalid page!");
286 // for comparing the model
287 Reference
< XControlModel
> xModelCompare(m_aContext
.xObjectModel
, UNO_QUERY
);
289 if (xPageObjects
.is())
291 // loop through all objects of the page
292 sal_Int32 nObjects
= xPageObjects
->getCount();
293 Reference
< XControlShape
> xControlShape
;
294 Reference
< XControlModel
> xControlModel
;
295 for (sal_Int32 i
=0; i
<nObjects
; ++i
)
297 if (xPageObjects
->getByIndex(i
) >>= xControlShape
)
298 { // it _is_ a control shape
299 xControlModel
= xControlShape
->getControl();
300 DBG_ASSERT(xControlModel
.is(), "OControlWizard::implDetermineShape: control shape without model!");
301 if (xModelCompare
.get() == xControlModel
.get())
303 m_aContext
.xObjectShape
= xControlShape
;
312 void OControlWizard::implDetermineForm()
314 Reference
< XChild
> xModelAsChild(m_aContext
.xObjectModel
, UNO_QUERY
);
315 Reference
< XInterface
> xControlParent
;
316 if (xModelAsChild
.is())
317 xControlParent
= xModelAsChild
->getParent();
319 m_aContext
.xForm
.set(xControlParent
, UNO_QUERY
);
320 m_aContext
.xRowSet
.set(xControlParent
, UNO_QUERY
);
321 DBG_ASSERT(m_aContext
.xForm
.is() && m_aContext
.xRowSet
.is(),
322 "OControlWizard::implDetermineForm: missing some interfaces of the control parent!");
327 void OControlWizard::implDeterminePage()
331 // get the document model
332 Reference
< XChild
> xControlAsChild(m_aContext
.xObjectModel
, UNO_QUERY
);
333 Reference
< XChild
> xModelSearch(xControlAsChild
->getParent(), UNO_QUERY
);
335 Reference
< XModel
> xModel(xModelSearch
, UNO_QUERY
);
336 while (xModelSearch
.is() && !xModel
.is())
338 xModelSearch
.set(xModelSearch
->getParent(), UNO_QUERY
);
339 xModel
.set(xModelSearch
, UNO_QUERY
);
342 Reference
< XDrawPage
> xPage
;
345 m_aContext
.xDocumentModel
= xModel
;
347 Reference
< XDrawPageSupplier
> xPageSupp(xModel
, UNO_QUERY
);
349 { // it's a document with only one page -> Writer
350 xPage
= xPageSupp
->getDrawPage();
354 // get the controller currently working on this model
355 Reference
< XController
> xController
= xModel
->getCurrentController();
356 DBG_ASSERT(xController
.is(), "OControlWizard::implDeterminePage: no current controller!");
358 // maybe it's a spreadsheet
359 Reference
< XSpreadsheetView
> xView(xController
, UNO_QUERY
);
362 Reference
< XSpreadsheet
> xSheet
= xView
->getActiveSheet();
363 xPageSupp
.set(xSheet
, UNO_QUERY
);
364 DBG_ASSERT(xPageSupp
.is(), "OControlWizard::implDeterminePage: a spreadsheet which is no page supplier!");
366 xPage
= xPageSupp
->getDrawPage();
369 { // can be a draw/impress doc only
370 Reference
< XDrawView
> xDrawView(xController
, UNO_QUERY
);
371 DBG_ASSERT(xDrawView
.is(), "OControlWizard::implDeterminePage: no alternatives left ... can't determine the page!");
373 xPage
= xDrawView
->getCurrentPage();
379 DBG_ASSERT(xPage
.is(), "OControlWizard::implDeterminePage: can't determine the page (no model)!");
381 m_aContext
.xDrawPage
= xPage
;
383 catch(const Exception
&)
385 OSL_FAIL("OControlWizard::implDeterminePage: caught an exception!");
390 void OControlWizard::implGetDSContext()
394 DBG_ASSERT(m_xContext
.is(), "OControlWizard::implGetDSContext: invalid service factory!");
396 m_aContext
.xDatasourceContext
= DatabaseContext::create(m_xContext
);
398 catch(const Exception
&)
400 OSL_FAIL("OControlWizard::implGetDSContext: invalid database context!");
405 Reference
< XConnection
> OControlWizard::getFormConnection(const OAccessRegulator
&) const
407 return getFormConnection();
410 Reference
< XConnection
> OControlWizard::getFormConnection() const
412 Reference
< XConnection
> xConn
;
415 if ( !::dbtools::isEmbeddedInDatabase(m_aContext
.xForm
,xConn
) )
416 m_aContext
.xForm
->getPropertyValue("ActiveConnection") >>= xConn
;
418 catch(const Exception
&)
420 OSL_FAIL("OControlWizard::getFormConnection: caught an exception!");
426 void OControlWizard::setFormConnection( const OAccessRegulator
& _rAccess
, const Reference
< XConnection
>& _rxConn
, bool _bAutoDispose
)
430 Reference
< XConnection
> xOldConn
= getFormConnection(_rAccess
);
431 if (xOldConn
.get() == _rxConn
.get())
434 disposeComponent(xOldConn
);
436 // set the new connection
439 // for this, use a AutoDisposer (so the conn is cleaned up when the form dies or gets another connection)
440 Reference
< XRowSet
> xFormRowSet( m_aContext
.xForm
, UNO_QUERY
);
441 rtl::Reference
<OAutoConnectionDisposer
> pAutoDispose
= new OAutoConnectionDisposer( xFormRowSet
, _rxConn
);
445 m_aContext
.xForm
->setPropertyValue("ActiveConnection", makeAny( _rxConn
) );
448 catch(const Exception
&)
450 OSL_FAIL("OControlWizard::setFormConnection: caught an exception!");
455 bool OControlWizard::updateContext(const OAccessRegulator
&)
457 return initContext();
460 Reference
< XInteractionHandler
> OControlWizard::getInteractionHandler(weld::Window
* _pWindow
) const
462 Reference
< XInteractionHandler
> xHandler
;
465 xHandler
.set( InteractionHandler::createWithParent(m_xContext
, nullptr), UNO_QUERY_THROW
);
467 catch(const Exception
&) { }
470 const OUString
sInteractionHandlerServiceName("com.sun.star.task.InteractionHandler");
471 ShowServiceNotAvailableError(_pWindow
, sInteractionHandlerServiceName
, true);
476 bool OControlWizard::initContext()
478 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OGroupBoxWizard::initContext: have no control model to work with!");
479 if (!m_aContext
.xObjectModel
.is())
483 m_aContext
.xForm
.clear();
484 m_aContext
.xRowSet
.clear();
485 m_aContext
.xDocumentModel
.clear();
486 m_aContext
.xDrawPage
.clear();
487 m_aContext
.xObjectShape
.clear();
488 m_aContext
.aFieldNames
.realloc(0);
490 m_aContext
.xObjectContainer
.clear();
491 m_aContext
.aTypes
.clear();
492 m_aContext
.bEmbedded
= false;
495 Reference
< XPreparedStatement
> xStatement
;
498 // get the datasource context
501 // first, determine the form the control belongs to
504 // need the page, too
507 // the shape of the control
508 implDetermineShape();
510 // get the columns of the object the settings refer to
511 Reference
< XNameAccess
> xColumns
;
513 if (m_aContext
.xForm
.is())
515 // collect some properties of the form
516 OUString sObjectName
= ::comphelper::getString(m_aContext
.xForm
->getPropertyValue("Command"));
517 sal_Int32 nObjectType
= ::comphelper::getINT32(m_aContext
.xForm
->getPropertyValue("CommandType"));
519 // calculate the connection the rowset is working with
520 Reference
< XConnection
> xConnection
;
521 m_aContext
.bEmbedded
= ::dbtools::isEmbeddedInDatabase( m_aContext
.xForm
, xConnection
);
522 if ( !m_aContext
.bEmbedded
)
523 xConnection
= ::dbtools::connectRowset( m_aContext
.xRowSet
, m_xContext
);
526 if (xConnection
.is())
532 Reference
< XTablesSupplier
> xSupplyTables(xConnection
, UNO_QUERY
);
533 if (xSupplyTables
.is() && xSupplyTables
->getTables().is() && xSupplyTables
->getTables()->hasByName(sObjectName
))
535 Reference
< XColumnsSupplier
> xSupplyColumns
;
536 m_aContext
.xObjectContainer
= xSupplyTables
->getTables();
537 m_aContext
.xObjectContainer
->getByName(sObjectName
) >>= xSupplyColumns
;
538 DBG_ASSERT(xSupplyColumns
.is(), "OControlWizard::initContext: invalid table columns!");
539 xColumns
= xSupplyColumns
->getColumns();
545 Reference
< XQueriesSupplier
> xSupplyQueries(xConnection
, UNO_QUERY
);
546 if (xSupplyQueries
.is() && xSupplyQueries
->getQueries().is() && xSupplyQueries
->getQueries()->hasByName(sObjectName
))
548 Reference
< XColumnsSupplier
> xSupplyColumns
;
549 m_aContext
.xObjectContainer
= xSupplyQueries
->getQueries();
550 m_aContext
.xObjectContainer
->getByName(sObjectName
) >>= xSupplyColumns
;
551 DBG_ASSERT(xSupplyColumns
.is(), "OControlWizard::initContext: invalid query columns!");
552 xColumns
= xSupplyColumns
->getColumns();
558 xStatement
= xConnection
->prepareStatement(sObjectName
);
560 // not interested in any results, only in the fields
561 Reference
< XPropertySet
> xStatementProps(xStatement
, UNO_QUERY
);
562 xStatementProps
->setPropertyValue("MaxRows", makeAny(sal_Int32(0)));
564 // TODO: think about handling local SQLExceptions here ...
565 Reference
< XColumnsSupplier
> xSupplyCols(xStatement
->executeQuery(), UNO_QUERY
);
566 if (xSupplyCols
.is())
567 xColumns
= xSupplyCols
->getColumns();
575 m_aContext
.aFieldNames
= xColumns
->getElementNames();
576 const OUString
* pBegin
= m_aContext
.aFieldNames
.getConstArray();
577 const OUString
* pEnd
= pBegin
+ m_aContext
.aFieldNames
.getLength();
578 for(;pBegin
!= pEnd
;++pBegin
)
580 sal_Int32 nFieldType
= DataType::OTHER
;
583 Reference
< XPropertySet
> xColumn
;
584 xColumns
->getByName(*pBegin
) >>= xColumn
;
585 xColumn
->getPropertyValue("Type") >>= nFieldType
;
587 catch(const Exception
&)
589 OSL_FAIL("OControlWizard::initContext: unexpected exception while gathering column information!");
591 m_aContext
.aTypes
.emplace(*pBegin
,nFieldType
);
595 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
596 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
597 catch(const SQLException
& e
) { aSQLException
<<= e
; }
598 catch(const Exception
&)
600 OSL_FAIL("OControlWizard::initContext: could not retrieve the control context (caught an exception)!");
603 ::comphelper::disposeComponent(xStatement
);
605 if (aSQLException
.hasValue())
606 { // an SQLException (or derivee) was thrown ...
608 // prepend an extra SQLContext explaining what we were doing
610 aContext
.Message
= compmodule::ModuleRes(RID_STR_COULDNOTOPENTABLE
);
611 aContext
.NextException
= aSQLException
;
613 // create an interaction handler to display this exception
614 Reference
< XInteractionHandler
> xHandler
= getInteractionHandler(GetFrameWeld());
615 if ( !xHandler
.is() )
618 Reference
< XInteractionRequest
> xRequest
= new OInteractionRequest(makeAny(aContext
));
621 xHandler
->handle(xRequest
);
623 catch(const Exception
&) { }
627 return 0 != m_aContext
.aFieldNames
.getLength();
631 void OControlWizard::commitControlSettings(OControlWizardSettings
const * _pSettings
)
633 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OControlWizard::commitControlSettings: have no control model to work with!");
634 if (!m_aContext
.xObjectModel
.is())
637 // the only thing we have at the moment is the label
640 Reference
< XPropertySetInfo
> xInfo
= m_aContext
.xObjectModel
->getPropertySetInfo();
641 if (xInfo
.is() && xInfo
->hasPropertyByName("Label"))
643 OUString
sControlLabel(_pSettings
->sControlLabel
);
644 m_aContext
.xObjectModel
->setPropertyValue(
646 makeAny(sControlLabel
)
650 catch(const Exception
&)
652 OSL_FAIL("OControlWizard::commitControlSettings: could not commit the basic control settings!");
657 void OControlWizard::initControlSettings(OControlWizardSettings
* _pSettings
)
659 DBG_ASSERT(m_aContext
.xObjectModel
.is(), "OControlWizard::initControlSettings: have no control model to work with!");
660 if (!m_aContext
.xObjectModel
.is())
663 // initialize some settings from the control model give
666 OUString
sLabelPropertyName("Label");
667 Reference
< XPropertySetInfo
> xInfo
= m_aContext
.xObjectModel
->getPropertySetInfo();
668 if (xInfo
.is() && xInfo
->hasPropertyByName(sLabelPropertyName
))
670 OUString sControlLabel
;
671 m_aContext
.xObjectModel
->getPropertyValue(sLabelPropertyName
) >>= sControlLabel
;
672 _pSettings
->sControlLabel
= sControlLabel
;
675 catch(const Exception
&)
677 OSL_FAIL("OControlWizard::initControlSettings: could not retrieve the basic control settings!");
682 bool OControlWizard::needDatasourceSelection()
685 return (0 == getContext().aFieldNames
.getLength());
686 // if we got fields, the data source is valid ...
693 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */