tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / extensions / source / dbpilots / controlwizard.cxx
blob9293f6add16cfbd1f0afa314a22f6a79b535bcdf
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 "controlwizard.hxx"
21 #include <tools/debug.hxx>
22 #include <comphelper/diagnose_ex.hxx>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
25 #include <com/sun/star/sdb/DatabaseContext.hpp>
26 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
27 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
28 #include <com/sun/star/container/XChild.hpp>
29 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
30 #include <com/sun/star/frame/XModel.hpp>
31 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
32 #include <com/sun/star/drawing/XDrawView.hpp>
33 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
34 #include <com/sun/star/sdb/CommandType.hpp>
35 #include <com/sun/star/sdbc/SQLWarning.hpp>
36 #include <com/sun/star/sdb/SQLContext.hpp>
37 #include <com/sun/star/task/InteractionHandler.hpp>
38 #include <comphelper/types.hxx>
39 #include <connectivity/dbtools.hxx>
40 #include <comphelper/interaction.hxx>
41 #include <vcl/stdtext.hxx>
42 #include <connectivity/conncleanup.hxx>
43 #include <com/sun/star/sdbc/DataType.hpp>
44 #include <tools/urlobj.hxx>
46 #define WIZARD_SIZE_X 60
47 #define WIZARD_SIZE_Y 23
49 namespace dbp
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::awt;
53 using namespace ::com::sun::star::sdb;
54 using namespace ::com::sun::star::sdbc;
55 using namespace ::com::sun::star::sdbcx;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::drawing;
59 using namespace ::com::sun::star::frame;
60 using namespace ::com::sun::star::sheet;
61 using namespace ::com::sun::star::form;
62 using namespace ::com::sun::star::task;
63 using namespace ::comphelper;
64 using namespace ::dbtools;
66 struct OAccessRegulator
68 friend class OControlWizardPage;
70 protected:
71 OAccessRegulator() { }
74 OControlWizardPage::OControlWizardPage(weld::Container* pPage, OControlWizard* pWizard, const OUString& rUIXMLDescription, const OUString& rID)
75 : OControlWizardPage_Base(pPage, pWizard, rUIXMLDescription, rID)
76 , m_pDialog(pWizard)
78 m_xContainer->set_size_request(m_xContainer->get_approximate_digit_width() * WIZARD_SIZE_X,
79 m_xContainer->get_text_height() * WIZARD_SIZE_Y);
82 OControlWizardPage::~OControlWizardPage()
86 OControlWizard* OControlWizardPage::getDialog()
88 return m_pDialog;
91 const OControlWizard* OControlWizardPage::getDialog() const
93 return m_pDialog;
96 bool OControlWizardPage::updateContext()
98 return m_pDialog->updateContext(OAccessRegulator());
101 Reference< XConnection > OControlWizardPage::getFormConnection() const
103 return m_pDialog->getFormConnection(OAccessRegulator());
106 void OControlWizardPage::setFormConnection( const Reference< XConnection >& _rxConn, bool _bAutoDispose )
108 m_pDialog->setFormConnection( OAccessRegulator(), _rxConn, _bAutoDispose );
111 const OControlWizardContext& OControlWizardPage::getContext() const
113 return m_pDialog->getContext();
116 void OControlWizardPage::fillListBox(weld::TreeView& _rList, const Sequence< OUString >& _rItems)
118 _rList.clear();
119 for (sal_Int32 nIndex = 0; nIndex < _rItems.getLength(); ++nIndex)
121 _rList.append(OUString::number(nIndex), _rItems[nIndex]);
125 void OControlWizardPage::fillListBox(weld::ComboBox& _rList, const Sequence< OUString >& _rItems)
127 _rList.clear();
128 for (auto& item : _rItems)
130 _rList.append_text(item);
134 void OControlWizardPage::enableFormDatasourceDisplay()
136 if (m_xFormContentType)
137 // nothing to do
138 return;
140 m_xFrame = m_xBuilder->weld_frame(u"sourceframe"_ustr);
141 m_xFrame->show();
142 m_xFormContentType = m_xBuilder->weld_label(u"contenttype"_ustr);
143 m_xFormContentTypeLabel = m_xBuilder->weld_label(u"contenttypelabel"_ustr);
144 m_xFormDatasource = m_xBuilder->weld_label(u"datasource"_ustr);
145 m_xFormDatasourceLabel = m_xBuilder->weld_label(u"datasourcelabel"_ustr);
146 m_xFormTable = m_xBuilder->weld_label(u"formtable"_ustr);
148 const OControlWizardContext& rContext = getContext();
149 if ( rContext.bEmbedded )
151 m_xFormDatasourceLabel->hide();
152 m_xFormDatasource->hide();
156 void OControlWizardPage::initializePage()
158 if (m_xFormDatasource && m_xFormContentTypeLabel && m_xFormTable)
160 const OControlWizardContext& rContext = getContext();
161 OUString sDataSource;
162 OUString sCommand;
163 sal_Int32 nCommandType = CommandType::COMMAND;
166 rContext.xForm->getPropertyValue(u"DataSourceName"_ustr) >>= sDataSource;
167 rContext.xForm->getPropertyValue(u"Command"_ustr) >>= sCommand;
168 rContext.xForm->getPropertyValue(u"CommandType"_ustr) >>= nCommandType;
170 catch(const Exception&)
172 TOOLS_WARN_EXCEPTION("extensions.dbpilots", "OControlWizardPage::initializePage");
175 INetURLObject aURL( sDataSource );
176 if( aURL.GetProtocol() != INetProtocol::NotValid )
177 sDataSource = aURL.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
178 m_xFormDatasource->set_label(sDataSource);
179 m_xFormTable->set_label(sCommand);
181 TranslateId pCommandTypeResourceId;
182 switch (nCommandType)
184 case CommandType::TABLE:
185 pCommandTypeResourceId = RID_STR_TYPE_TABLE;
186 break;
188 case CommandType::QUERY:
189 pCommandTypeResourceId = RID_STR_TYPE_QUERY;
190 break;
192 default:
193 pCommandTypeResourceId = RID_STR_TYPE_COMMAND;
194 break;
196 m_xFormContentType->set_label(compmodule::ModuleRes(pCommandTypeResourceId));
199 OControlWizardPage_Base::initializePage();
202 OControlWizard::OControlWizard(weld::Window* _pParent,
203 const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
204 : WizardMachine(_pParent, WizardButtonFlags::CANCEL | WizardButtonFlags::PREVIOUS | WizardButtonFlags::NEXT | WizardButtonFlags::FINISH)
205 , m_xContext(_rxContext)
207 m_aContext.xObjectModel = _rxObjectModel;
208 initContext();
210 defaultButton(WizardButtonFlags::NEXT);
211 enableButtons(WizardButtonFlags::FINISH, false);
214 OControlWizard::~OControlWizard()
218 short OControlWizard::run()
220 // get the class id of the control we're dealing with
221 sal_Int16 nClassId = FormComponentType::CONTROL;
224 getContext().xObjectModel->getPropertyValue(u"ClassId"_ustr) >>= nClassId;
226 catch(const Exception&)
228 OSL_FAIL("OControlWizard::activate: could not obtain the class id!");
230 if (!approveControl(nClassId))
232 // TODO: MessageBox or exception
233 return RET_CANCEL;
236 ActivatePage();
238 m_xAssistant->set_current_page(0);
240 return OControlWizard_Base::run();
243 void OControlWizard::implDetermineShape()
245 Reference< XIndexAccess > xPageObjects = m_aContext.xDrawPage;
246 DBG_ASSERT(xPageObjects.is(), "OControlWizard::implDetermineShape: invalid page!");
248 // for comparing the model
249 Reference< XControlModel > xModelCompare(m_aContext.xObjectModel, UNO_QUERY);
251 if (!xPageObjects.is())
252 return;
254 // loop through all objects of the page
255 sal_Int32 nObjects = xPageObjects->getCount();
256 Reference< XControlShape > xControlShape;
257 Reference< XControlModel > xControlModel;
258 for (sal_Int32 i=0; i<nObjects; ++i)
260 if (xPageObjects->getByIndex(i) >>= xControlShape)
261 { // it _is_ a control shape
262 xControlModel = xControlShape->getControl();
263 DBG_ASSERT(xControlModel.is(), "OControlWizard::implDetermineShape: control shape without model!");
264 if (xModelCompare.get() == xControlModel.get())
266 m_aContext.xObjectShape = xControlShape;
267 break;
274 void OControlWizard::implDetermineForm()
276 Reference< XChild > xModelAsChild(m_aContext.xObjectModel, UNO_QUERY);
277 Reference< XInterface > xControlParent;
278 if (xModelAsChild.is())
279 xControlParent = xModelAsChild->getParent();
281 m_aContext.xForm.set(xControlParent, UNO_QUERY);
282 m_aContext.xRowSet.set(xControlParent, UNO_QUERY);
283 DBG_ASSERT(m_aContext.xForm.is() && m_aContext.xRowSet.is(),
284 "OControlWizard::implDetermineForm: missing some interfaces of the control parent!");
289 void OControlWizard::implDeterminePage()
293 // get the document model
294 Reference< XChild > xControlAsChild(m_aContext.xObjectModel, UNO_QUERY);
295 Reference< XChild > xModelSearch(xControlAsChild->getParent(), UNO_QUERY);
297 Reference< XModel > xModel(xModelSearch, UNO_QUERY);
298 while (xModelSearch.is() && !xModel.is())
300 xModelSearch.set(xModelSearch->getParent(), UNO_QUERY);
301 xModel.set(xModelSearch, UNO_QUERY);
304 Reference< XDrawPage > xPage;
305 if (xModel.is())
307 m_aContext.xDocumentModel = xModel;
309 Reference< XDrawPageSupplier > xPageSupp(xModel, UNO_QUERY);
310 if (xPageSupp.is())
311 { // it's a document with only one page -> Writer
312 xPage = xPageSupp->getDrawPage();
314 else
316 // get the controller currently working on this model
317 Reference< XController > xController = xModel->getCurrentController();
318 DBG_ASSERT(xController.is(), "OControlWizard::implDeterminePage: no current controller!");
320 // maybe it's a spreadsheet
321 Reference< XSpreadsheetView > xView(xController, UNO_QUERY);
322 if (xView.is())
323 { // okay, it is one
324 Reference< XSpreadsheet > xSheet = xView->getActiveSheet();
325 xPageSupp.set(xSheet, UNO_QUERY);
326 DBG_ASSERT(xPageSupp.is(), "OControlWizard::implDeterminePage: a spreadsheet which is no page supplier!");
327 if (xPageSupp.is())
328 xPage = xPageSupp->getDrawPage();
330 else
331 { // can be a draw/impress doc only
332 Reference< XDrawView > xDrawView(xController, UNO_QUERY);
333 DBG_ASSERT(xDrawView.is(), "OControlWizard::implDeterminePage: no alternatives left ... can't determine the page!");
334 if (xDrawView.is())
335 xPage = xDrawView->getCurrentPage();
339 else
341 DBG_ASSERT(xPage.is(), "OControlWizard::implDeterminePage: can't determine the page (no model)!");
343 m_aContext.xDrawPage = std::move(xPage);
345 catch(const Exception&)
347 TOOLS_WARN_EXCEPTION("extensions.dbpilots", "OControlWizard::implDeterminePage");
352 void OControlWizard::implGetDSContext()
356 DBG_ASSERT(m_xContext.is(), "OControlWizard::implGetDSContext: invalid service factory!");
358 m_aContext.xDatasourceContext = DatabaseContext::create(m_xContext);
360 catch(const Exception&)
362 OSL_FAIL("OControlWizard::implGetDSContext: invalid database context!");
367 Reference< XConnection > OControlWizard::getFormConnection(const OAccessRegulator&) const
369 return getFormConnection();
372 Reference< XConnection > OControlWizard::getFormConnection() const
374 Reference< XConnection > xConn;
377 if ( !::dbtools::isEmbeddedInDatabase(m_aContext.xForm,xConn) )
378 m_aContext.xForm->getPropertyValue(u"ActiveConnection"_ustr) >>= xConn;
380 catch(const Exception&)
382 TOOLS_WARN_EXCEPTION("extensions.dbpilots", "OControlWizard::getFormConnection");
384 return xConn;
388 void OControlWizard::setFormConnection( const OAccessRegulator& _rAccess, const Reference< XConnection >& _rxConn, bool _bAutoDispose )
392 Reference< XConnection > xOldConn = getFormConnection(_rAccess);
393 if (xOldConn.get() == _rxConn.get())
394 return;
396 disposeComponent(xOldConn);
398 // set the new connection
399 if ( _bAutoDispose )
401 // for this, use an AutoDisposer (so the conn is cleaned up when the form dies or gets another connection)
402 Reference< XRowSet > xFormRowSet( m_aContext.xForm, UNO_QUERY );
403 new OAutoConnectionDisposer( xFormRowSet, _rxConn );
405 else
407 m_aContext.xForm->setPropertyValue(u"ActiveConnection"_ustr, Any( _rxConn ) );
410 catch(const Exception&)
412 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OControlWizard::setFormConnection");
417 bool OControlWizard::updateContext(const OAccessRegulator&)
419 return initContext();
422 Reference< XInteractionHandler > OControlWizard::getInteractionHandler(weld::Window* _pWindow) const
424 Reference< XInteractionHandler > xHandler;
427 xHandler.set( InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY_THROW );
429 catch(const Exception&) { }
430 if (!xHandler.is())
432 ShowServiceNotAvailableError(_pWindow, u"com.sun.star.task.InteractionHandler", true);
434 return xHandler;
437 bool OControlWizard::initContext()
439 DBG_ASSERT(m_aContext.xObjectModel.is(), "OGroupBoxWizard::initContext: have no control model to work with!");
440 if (!m_aContext.xObjectModel.is())
441 return false;
443 // reset the context
444 m_aContext.xForm.clear();
445 m_aContext.xRowSet.clear();
446 m_aContext.xDocumentModel.clear();
447 m_aContext.xDrawPage.clear();
448 m_aContext.xObjectShape.clear();
449 m_aContext.aFieldNames.realloc(0);
451 m_aContext.xObjectContainer.clear();
452 m_aContext.aTypes.clear();
453 m_aContext.bEmbedded = false;
455 Any aSQLException;
456 Reference< XPreparedStatement > xStatement;
459 // get the datasource context
460 implGetDSContext();
462 // first, determine the form the control belongs to
463 implDetermineForm();
465 // need the page, too
466 implDeterminePage();
468 // the shape of the control
469 implDetermineShape();
471 // get the columns of the object the settings refer to
472 Reference< XNameAccess > xColumns;
474 if (m_aContext.xForm.is())
476 // collect some properties of the form
477 OUString sObjectName = ::comphelper::getString(m_aContext.xForm->getPropertyValue(u"Command"_ustr));
478 sal_Int32 nObjectType = ::comphelper::getINT32(m_aContext.xForm->getPropertyValue(u"CommandType"_ustr));
480 // calculate the connection the rowset is working with
481 Reference< XConnection > xConnection;
482 m_aContext.bEmbedded = ::dbtools::isEmbeddedInDatabase( m_aContext.xForm, xConnection );
483 if ( !m_aContext.bEmbedded )
484 xConnection = ::dbtools::connectRowset( m_aContext.xRowSet, m_xContext, nullptr );
486 // get the fields
487 if (xConnection.is())
489 switch (nObjectType)
491 case 0:
493 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
494 if (xSupplyTables.is() && xSupplyTables->getTables().is() && xSupplyTables->getTables()->hasByName(sObjectName))
496 Reference< XColumnsSupplier > xSupplyColumns;
497 m_aContext.xObjectContainer = xSupplyTables->getTables();
498 m_aContext.xObjectContainer->getByName(sObjectName) >>= xSupplyColumns;
499 DBG_ASSERT(xSupplyColumns.is(), "OControlWizard::initContext: invalid table columns!");
500 xColumns = xSupplyColumns->getColumns();
503 break;
504 case 1:
506 Reference< XQueriesSupplier > xSupplyQueries(xConnection, UNO_QUERY);
507 if (xSupplyQueries.is() && xSupplyQueries->getQueries().is() && xSupplyQueries->getQueries()->hasByName(sObjectName))
509 Reference< XColumnsSupplier > xSupplyColumns;
510 m_aContext.xObjectContainer = xSupplyQueries->getQueries();
511 m_aContext.xObjectContainer->getByName(sObjectName) >>= xSupplyColumns;
512 DBG_ASSERT(xSupplyColumns.is(), "OControlWizard::initContext: invalid query columns!");
513 xColumns = xSupplyColumns->getColumns();
516 break;
517 default:
519 xStatement = xConnection->prepareStatement(sObjectName);
521 // not interested in any results, only in the fields
522 Reference< XPropertySet > xStatementProps(xStatement, UNO_QUERY);
523 xStatementProps->setPropertyValue(u"MaxRows"_ustr, Any(sal_Int32(0)));
525 // TODO: think about handling local SQLExceptions here ...
526 Reference< XColumnsSupplier > xSupplyCols(xStatement->executeQuery(), UNO_QUERY);
527 if (xSupplyCols.is())
528 xColumns = xSupplyCols->getColumns();
534 if (xColumns.is())
536 m_aContext.aFieldNames = xColumns->getElementNames();
537 for (auto& name : m_aContext.aFieldNames)
539 sal_Int32 nFieldType = DataType::OTHER;
542 Reference< XPropertySet > xColumn;
543 xColumns->getByName(name) >>= xColumn;
544 xColumn->getPropertyValue(u"Type"_ustr) >>= nFieldType;
546 catch(const Exception&)
548 TOOLS_WARN_EXCEPTION(
549 "extensions.dbpilots",
550 "unexpected exception while gathering column information!");
552 m_aContext.aTypes.emplace(name, nFieldType);
556 catch(const SQLContext& e) { aSQLException <<= e; }
557 catch(const SQLWarning& e) { aSQLException <<= e; }
558 catch(const SQLException& e) { aSQLException <<= e; }
559 catch(const Exception&)
561 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OControlWizard::initContext: could not retrieve the control context");
564 ::comphelper::disposeComponent(xStatement);
566 if (aSQLException.hasValue())
567 { // an SQLException (or derivee) was thrown ...
569 // prepend an extra SQLContext explaining what we were doing
570 SQLContext aContext(compmodule::ModuleRes(RID_STR_COULDNOTOPENTABLE), {}, {}, 0,
571 aSQLException, {});
573 // create an interaction handler to display this exception
574 Reference< XInteractionHandler > xHandler = getInteractionHandler(m_xAssistant.get());
575 if ( !xHandler.is() )
576 return false;
578 Reference< XInteractionRequest > xRequest = new OInteractionRequest(Any(aContext));
581 xHandler->handle(xRequest);
583 catch(const Exception&) { }
584 return false;
587 return m_aContext.aFieldNames.hasElements();
591 void OControlWizard::commitControlSettings(OControlWizardSettings const * _pSettings)
593 DBG_ASSERT(m_aContext.xObjectModel.is(), "OControlWizard::commitControlSettings: have no control model to work with!");
594 if (!m_aContext.xObjectModel.is())
595 return;
597 // the only thing we have at the moment is the label
600 Reference< XPropertySetInfo > xInfo = m_aContext.xObjectModel->getPropertySetInfo();
601 if (xInfo.is() && xInfo->hasPropertyByName(u"Label"_ustr))
603 OUString sControlLabel(_pSettings->sControlLabel);
604 m_aContext.xObjectModel->setPropertyValue(
605 u"Label"_ustr,
606 Any(sControlLabel)
610 catch(const Exception&)
612 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OControlWizard::commitControlSettings: could not commit the basic control settings!");
617 void OControlWizard::initControlSettings(OControlWizardSettings* _pSettings)
619 DBG_ASSERT(m_aContext.xObjectModel.is(), "OControlWizard::initControlSettings: have no control model to work with!");
620 if (!m_aContext.xObjectModel.is())
621 return;
623 // initialize some settings from the control model give
626 OUString sLabelPropertyName(u"Label"_ustr);
627 Reference< XPropertySetInfo > xInfo = m_aContext.xObjectModel->getPropertySetInfo();
628 if (xInfo.is() && xInfo->hasPropertyByName(sLabelPropertyName))
630 OUString sControlLabel;
631 m_aContext.xObjectModel->getPropertyValue(sLabelPropertyName) >>= sControlLabel;
632 _pSettings->sControlLabel = sControlLabel;
635 catch(const Exception&)
637 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OControlWizard::initControlSettings: could not retrieve the basic control settings!");
642 bool OControlWizard::needDatasourceSelection()
644 // lemme see ...
645 return !getContext().aFieldNames.hasElements();
646 // if we got fields, the data source is valid ...
650 } // namespace dbp
653 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */