build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / ui / browser / exsrcbrw.cxx
blob69db7d2dc0feb94fa9de9e2950692fe870ac48a9
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 "exsrcbrw.hxx"
21 #include "uiservices.hxx"
22 #include <com/sun/star/form/FormComponentType.hpp>
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/form/XGridColumnFactory.hpp>
25 #include <com/sun/star/form/XLoadable.hpp>
26 #include <com/sun/star/frame/FrameSearchFlag.hpp>
27 #include "formadapter.hxx"
28 #include <comphelper/processfactory.hxx>
29 #include "dbustrings.hrc"
30 #include "dbu_reghelper.hxx"
31 #include <o3tl/any.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <rtl/strbuf.hxx>
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::sdb;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::sdbcx;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::form;
43 using namespace ::com::sun::star::frame;
44 using namespace dbaui;
46 // SbaExternalSourceBrowser
47 extern "C" void SAL_CALL createRegistryInfo_OFormGridView()
49 static OMultiInstanceAutoRegistration< SbaExternalSourceBrowser > aAutoRegistration;
52 Any SAL_CALL SbaExternalSourceBrowser::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
54 Any aRet = SbaXDataBrowserController::queryInterface(_rType);
55 if(!aRet.hasValue())
56 aRet = ::cppu::queryInterface(_rType,
57 static_cast<css::util::XModifyBroadcaster*>(this),
58 static_cast<css::form::XLoadListener*>(this));
60 return aRet;
63 SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference< css::uno::XComponentContext >& _rM)
64 :SbaXDataBrowserController(_rM)
65 ,m_aModifyListeners(getMutex())
66 ,m_pDataSourceImpl(nullptr)
67 ,m_bInQueryDispatch( false )
72 SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
77 css::uno::Sequence<OUString> SAL_CALL SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException, std::exception)
79 return getSupportedServiceNames_Static();
82 OUString SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException)
84 return OUString("org.openoffice.comp.dbu.OFormGridView");
87 css::uno::Sequence<OUString> SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException)
89 css::uno::Sequence<OUString> aSupported { "com.sun.star.sdb.FormGridView" };
90 return aSupported;
93 Reference< XInterface > SAL_CALL SbaExternalSourceBrowser::Create(const Reference<XMultiServiceFactory >& _rxFactory)
95 return *(new SbaExternalSourceBrowser( comphelper::getComponentContext(_rxFactory)));
98 OUString SAL_CALL SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException, std::exception)
100 return getImplementationName_Static();
103 Reference< XRowSet > SbaExternalSourceBrowser::CreateForm()
105 m_pDataSourceImpl = new SbaXFormAdapter();
106 return m_pDataSourceImpl;
109 bool SbaExternalSourceBrowser::InitializeForm(const Reference< XPropertySet > & /*i_formProperties*/)
111 return true;
114 bool SbaExternalSourceBrowser::LoadForm()
116 // as we don't have a main form (yet), we have nothing to do
117 // we don't call FormLoaded, because this expects a working data source
118 return true;
121 void SbaExternalSourceBrowser::modified(const css::lang::EventObject& aEvent) throw( RuntimeException, std::exception )
123 SbaXDataBrowserController::modified(aEvent);
125 // multiplex this event to all my listeners
126 css::lang::EventObject aEvt(*this);
127 ::comphelper::OInterfaceIteratorHelper2 aIt(m_aModifyListeners);
128 while (aIt.hasMoreElements())
129 static_cast< css::util::XModifyListener*>(aIt.next())->modified(aEvt);
132 void SAL_CALL SbaExternalSourceBrowser::dispatch(const css::util::URL& aURL, const Sequence< css::beans::PropertyValue>& aArgs) throw(css::uno::RuntimeException, std::exception)
134 const css::beans::PropertyValue* pArguments = aArgs.getConstArray();
135 if ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
137 // search the argument describing the column to create
138 OUString sControlType;
139 sal_Int32 nControlPos = -1;
140 Sequence< css::beans::PropertyValue> aControlProps;
141 sal_uInt16 i;
142 for ( i = 0; i < aArgs.getLength(); ++i, ++pArguments )
144 if ( pArguments->Name == "ColumnType" )
146 auto s = o3tl::tryAccess<OUString>(pArguments->Value);
147 OSL_ENSURE(s, "invalid type for argument \"ColumnType\" !");
148 if (s)
149 sControlType = *s;
151 else if ( pArguments->Name == "ColumnPosition" )
153 auto n = o3tl::tryAccess<sal_Int16>(pArguments->Value);
154 OSL_ENSURE(n, "invalid type for argument \"ColumnPosition\" !");
155 if (n)
156 nControlPos = *n;
158 else if ( pArguments->Name == "ColumnProperties" )
160 auto s = o3tl::tryAccess<Sequence<css::beans::PropertyValue>>(
161 pArguments->Value);
162 OSL_ENSURE(s, "invalid type for argument \"ColumnProperties\" !");
163 if (s)
164 aControlProps = *s;
166 else
167 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << pArguments->Name << ") !");
169 if (sControlType.isEmpty())
171 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
172 sControlType = "TextField";
174 OSL_ENSURE(aControlProps.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
176 // create the col
177 Reference< css::form::XGridColumnFactory > xColFactory(getControlModel(), UNO_QUERY);
178 Reference< css::beans::XPropertySet > xNewCol = xColFactory->createColumn(sControlType);
179 Reference< XPropertySetInfo > xNewColProperties;
180 if (xNewCol.is())
181 xNewColProperties = xNewCol->getPropertySetInfo();
182 // set its properties
183 if (xNewColProperties.is())
185 const css::beans::PropertyValue* pControlProps = aControlProps.getConstArray();
186 for (i=0; i<aControlProps.getLength(); ++i, ++pControlProps)
190 if (xNewColProperties->hasPropertyByName(pControlProps->Name))
191 xNewCol->setPropertyValue(pControlProps->Name, pControlProps->Value);
193 catch (const Exception&)
195 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << pControlProps->Name << ")!");
200 // correct the position
201 Reference< css::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
203 if (nControlPos > xColContainer->getCount())
204 nControlPos = xColContainer->getCount();
205 if (nControlPos < 0)
206 nControlPos = 0;
208 // append the column
209 xColContainer->insertByIndex(nControlPos, makeAny(xNewCol));
211 else if ( aURL.Complete == ".uno:FormSlots/ClearView" )
213 ClearView();
215 else if ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
217 if (!m_pDataSourceImpl)
218 return;
220 Reference< XRowSet > xMasterForm;
221 // search the arguments for the master form
222 for (sal_Int32 i=0; i<aArgs.getLength(); ++i, ++pArguments)
224 if ( (pArguments->Name == "MasterForm") && (pArguments->Value.getValueTypeClass() == TypeClass_INTERFACE) )
226 xMasterForm.set(pArguments->Value, UNO_QUERY);
227 break;
230 if (!xMasterForm.is())
232 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
233 return;
236 Attach(xMasterForm);
238 else
239 SbaXDataBrowserController::dispatch(aURL, aArgs);
242 Reference< css::frame::XDispatch > SAL_CALL SbaExternalSourceBrowser::queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags) throw( RuntimeException, std::exception )
244 Reference< css::frame::XDispatch > xReturn;
245 if (m_bInQueryDispatch)
246 return xReturn;
248 m_bInQueryDispatch = true;
250 if ( ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
251 // attach a new external form
252 || ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
253 // add a column to the grid
254 || ( aURL.Complete == ".uno:FormSlots/ClearView" )
255 // clear the grid
257 xReturn = static_cast<css::frame::XDispatch*>(this);
259 if ( !xReturn.is()
260 && ( (aURL.Complete == ".uno:FormSlots/moveToFirst" ) || (aURL.Complete == ".uno:FormSlots/moveToPrev" )
261 || (aURL.Complete == ".uno:FormSlots/moveToNext" ) || (aURL.Complete == ".uno:FormSlots/moveToLast" )
262 || (aURL.Complete == ".uno:FormSlots/moveToNew" ) || (aURL.Complete == ".uno:FormSlots/undoRecord" )
266 OSL_ENSURE(aURL.Mark.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the css::util::URL shouldn't have a mark !");
267 css::util::URL aNewUrl = aURL;
269 // split the css::util::URL
270 OSL_ENSURE( m_xUrlTransformer.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
271 if ( m_xUrlTransformer.is() )
272 m_xUrlTransformer->parseStrict( aNewUrl );
274 // set a new mark
275 aNewUrl.Mark = "DB/FormGridView";
276 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
277 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
279 if (m_xUrlTransformer.is())
280 m_xUrlTransformer->assemble(aNewUrl);
282 Reference< XDispatchProvider > xFrameDispatcher( getFrame(), UNO_QUERY );
283 if (xFrameDispatcher.is())
284 xReturn = xFrameDispatcher->queryDispatch(aNewUrl, aTargetFrameName, FrameSearchFlag::PARENT);
288 if (!xReturn.is())
289 xReturn = SbaXDataBrowserController::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
291 m_bInQueryDispatch = false;
292 return xReturn;
295 void SAL_CALL SbaExternalSourceBrowser::disposing()
297 // say our modify listeners goodbye
298 css::lang::EventObject aEvt;
299 aEvt.Source = static_cast<XWeak*>(this);
300 m_aModifyListeners.disposeAndClear(aEvt);
302 stopListening();
304 SbaXDataBrowserController::disposing();
307 void SAL_CALL SbaExternalSourceBrowser::addModifyListener(const Reference< css::util::XModifyListener > & aListener) throw( RuntimeException, std::exception )
309 m_aModifyListeners.addInterface(aListener);
312 void SAL_CALL SbaExternalSourceBrowser::removeModifyListener(const Reference< css::util::XModifyListener > & aListener) throw( RuntimeException, std::exception )
314 m_aModifyListeners.removeInterface(aListener);
317 void SAL_CALL SbaExternalSourceBrowser::unloading(const css::lang::EventObject& aEvent) throw( RuntimeException, std::exception )
319 if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == aEvent.Source))
321 ClearView();
324 SbaXDataBrowserController::unloading(aEvent);
327 void SbaExternalSourceBrowser::Attach(const Reference< XRowSet > & xMaster)
329 Any aOldPos;
330 bool bWasInsertRow = false;
331 bool bBeforeFirst = true;
332 bool bAfterLast = true;
333 Reference< XRowLocate > xCursor(xMaster, UNO_QUERY);
334 Reference< XPropertySet > xMasterProps(xMaster, UNO_QUERY);
338 // switch the control to design mode
339 if (getBrowserView() && getBrowserView()->getGridControl().is())
340 getBrowserView()->getGridControl()->setDesignMode(true);
342 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
343 // restore the old position
344 if (xCursor.is() && xMaster.is())
346 bBeforeFirst = xMaster->isBeforeFirst();
347 bAfterLast = xMaster->isAfterLast();
348 if(!bBeforeFirst && !bAfterLast)
349 aOldPos = xCursor->getBookmark();
352 if (xMasterProps.is())
353 xMasterProps->getPropertyValue(PROPERTY_ISNEW) >>= bWasInsertRow;
355 catch( const Exception& )
357 DBG_UNHANDLED_EXCEPTION();
360 onStartLoading( Reference< XLoadable >( xMaster, UNO_QUERY ) );
362 stopListening();
363 m_pDataSourceImpl->AttachForm(xMaster);
364 startListening();
366 if (xMaster.is())
368 // at this point we have to reset the formatter for the new form
369 initFormatter();
370 // assume that the master form is already loaded
371 #if OSL_DEBUG_LEVEL > 0
373 Reference< XLoadable > xLoadable( xMaster, UNO_QUERY );
374 OSL_ENSURE( xLoadable.is() && xLoadable->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
376 #endif
378 LoadFinished(true);
380 Reference< XResultSetUpdate > xUpdate(xMaster, UNO_QUERY);
383 if (bWasInsertRow && xUpdate.is())
384 xUpdate->moveToInsertRow();
385 else if (xCursor.is() && aOldPos.hasValue())
386 xCursor->moveToBookmark(aOldPos);
387 else if(bBeforeFirst && xMaster.is())
388 xMaster->beforeFirst();
389 else if(bAfterLast && xMaster.is())
390 xMaster->afterLast();
392 catch(Exception&)
394 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
400 void SbaExternalSourceBrowser::ClearView()
402 // set a new (empty) datasource
403 Attach(Reference< XRowSet > ());
405 // clear all cols in the grid
406 Reference< css::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
407 while (xColContainer->getCount() > 0)
408 xColContainer->removeByIndex(0);
411 void SAL_CALL SbaExternalSourceBrowser::disposing(const css::lang::EventObject& Source) throw( RuntimeException, std::exception )
413 if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == Source.Source))
415 ClearView();
418 SbaXDataBrowserController::disposing(Source);
421 void SbaExternalSourceBrowser::startListening()
423 if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
425 Reference< css::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
426 xLoadable->addLoadListener(static_cast<css::form::XLoadListener*>(this));
430 void SbaExternalSourceBrowser::stopListening()
432 if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
434 Reference< css::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
435 xLoadable->removeLoadListener(static_cast<css::form::XLoadListener*>(this));
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */