LibreOffice Module dbaccess (master) 1
exsrcbrw.cxx
Go to the documentation of this file.
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 <exsrcbrw.hxx>
21#include <com/sun/star/util/XURLTransformer.hpp>
22#include <com/sun/star/form/XGridColumnFactory.hpp>
23#include <com/sun/star/form/XLoadable.hpp>
24#include <com/sun/star/frame/FrameSearchFlag.hpp>
25#include <formadapter.hxx>
26#include <strings.hxx>
27#include <o3tl/any.hxx>
29#include <sal/log.hxx>
30
31using namespace ::com::sun::star::uno;
32using namespace ::com::sun::star::sdb;
33using namespace ::com::sun::star::sdbc;
34using namespace ::com::sun::star::sdbcx;
35using namespace ::com::sun::star::beans;
36using namespace ::com::sun::star::container;
37using namespace ::com::sun::star::lang;
38using namespace ::com::sun::star::form;
39using namespace ::com::sun::star::frame;
40using namespace dbaui;
41
42// SbaExternalSourceBrowser
43extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
45 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
46{
47 return cppu::acquire(new SbaExternalSourceBrowser(context));
48}
49
50Any SAL_CALL SbaExternalSourceBrowser::queryInterface(const Type& _rType)
51{
53 if(!aRet.hasValue())
54 aRet = ::cppu::queryInterface(_rType,
55 static_cast<css::util::XModifyBroadcaster*>(this),
56 static_cast<css::form::XLoadListener*>(this));
57
58 return aRet;
59}
60
63 ,m_aModifyListeners(getMutex())
64 ,m_bInQueryDispatch( false )
65{
66
67}
68
70{
71}
72
73css::uno::Sequence<OUString> SAL_CALL SbaExternalSourceBrowser::getSupportedServiceNames()
74{
75 return { "com.sun.star.sdb.FormGridView" };
76}
77
79{
80 return "org.openoffice.comp.dbu.OFormGridView";
81}
82
84{
86 return m_pDataSourceImpl;
87}
88
90{
91 return true;
92}
93
95{
96 // as we don't have a main form (yet), we have nothing to do
97 // we don't call FormLoaded, because this expects a working data source
98 return true;
99}
100
101void SbaExternalSourceBrowser::modified(const css::lang::EventObject& aEvent)
102{
104
105 // multiplex this event to all my listeners
106 css::lang::EventObject aEvt(*this);
107 m_aModifyListeners.notifyEach( &css::util::XModifyListener::modified, aEvt );
108}
109
110void SAL_CALL SbaExternalSourceBrowser::dispatch(const css::util::URL& aURL, const Sequence< css::beans::PropertyValue>& aArgs)
111{
112 if ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
113 {
114 // search the argument describing the column to create
115 OUString sControlType;
116 sal_Int32 nControlPos = -1;
118 for ( const css::beans::PropertyValue& rArgument : aArgs )
119 {
120 if ( rArgument.Name == "ColumnType" )
121 {
122 auto s = o3tl::tryAccess<OUString>(rArgument.Value);
123 OSL_ENSURE(s, "invalid type for argument \"ColumnType\" !");
124 if (s)
125 sControlType = *s;
126 }
127 else if ( rArgument.Name == "ColumnPosition" )
128 {
129 auto n = o3tl::tryAccess<sal_Int16>(rArgument.Value);
130 OSL_ENSURE(n, "invalid type for argument \"ColumnPosition\" !");
131 if (n)
132 nControlPos = *n;
133 }
134 else if ( rArgument.Name == "ColumnProperties" )
135 {
136 auto s = o3tl::tryAccess<Sequence<css::beans::PropertyValue>>(
137 rArgument.Value);
138 OSL_ENSURE(s, "invalid type for argument \"ColumnProperties\" !");
139 if (s)
140 aControlProps = *s;
141 }
142 else
143 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << rArgument.Name << ") !");
144 }
145 if (sControlType.isEmpty())
146 {
147 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
148 sControlType = "TextField";
149 }
150 OSL_ENSURE(aControlProps.hasElements(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
151
152 // create the col
154 Reference< css::beans::XPropertySet > xNewCol = xColFactory->createColumn(sControlType);
155 Reference< XPropertySetInfo > xNewColProperties;
156 if (xNewCol.is())
157 xNewColProperties = xNewCol->getPropertySetInfo();
158 // set its properties
159 if (xNewColProperties.is())
160 {
161 for (const css::beans::PropertyValue& rControlProp : std::as_const(aControlProps))
162 {
163 try
164 {
165 if (xNewColProperties->hasPropertyByName(rControlProp.Name))
166 xNewCol->setPropertyValue(rControlProp.Name, rControlProp.Value);
167 }
168 catch (const Exception&)
169 {
170 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << rControlProp.Name << ")!");
171 }
172 }
173 }
174
175 // correct the position
177
178 if (nControlPos > xColContainer->getCount())
179 nControlPos = xColContainer->getCount();
180 if (nControlPos < 0)
181 nControlPos = 0;
182
183 // append the column
184 xColContainer->insertByIndex(nControlPos, Any(xNewCol));
185 }
186 else if ( aURL.Complete == ".uno:FormSlots/ClearView" )
187 {
188 ClearView();
189 }
190 else if ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
191 {
193 return;
194
195 Reference< XRowSet > xMasterForm;
196 // search the arguments for the master form
197 for (const css::beans::PropertyValue& rArgument : aArgs)
198 {
199 if ( (rArgument.Name == "MasterForm") && (rArgument.Value.getValueTypeClass() == TypeClass_INTERFACE) )
200 {
201 xMasterForm.set(rArgument.Value, UNO_QUERY);
202 break;
203 }
204 }
205 if (!xMasterForm.is())
206 {
207 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
208 return;
209 }
210
211 Attach(xMasterForm);
212 }
213 else
214 SbaXDataBrowserController::dispatch(aURL, aArgs);
215}
216
217Reference< css::frame::XDispatch > SAL_CALL SbaExternalSourceBrowser::queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags)
218{
221 return xReturn;
222
223 m_bInQueryDispatch = true;
224
225 if ( ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
226 // attach a new external form
227 || ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
228 // add a column to the grid
229 || ( aURL.Complete == ".uno:FormSlots/ClearView" )
230 // clear the grid
231 )
232 xReturn = static_cast<css::frame::XDispatch*>(this);
233
234 if ( !xReturn.is()
235 && ( (aURL.Complete == ".uno:FormSlots/moveToFirst" ) || (aURL.Complete == ".uno:FormSlots/moveToPrev" )
236 || (aURL.Complete == ".uno:FormSlots/moveToNext" ) || (aURL.Complete == ".uno:FormSlots/moveToLast" )
237 || (aURL.Complete == ".uno:FormSlots/moveToNew" ) || (aURL.Complete == ".uno:FormSlots/undoRecord" )
238 )
239 )
240 {
241 OSL_ENSURE(aURL.Mark.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the css::util::URL shouldn't have a mark !");
242 css::util::URL aNewUrl = aURL;
243
244 // split the css::util::URL
245 OSL_ENSURE( m_xUrlTransformer.is(), "SbaExternalSourceBrowser::queryDispatch : could not create a URLTransformer !" );
246 if ( m_xUrlTransformer.is() )
247 m_xUrlTransformer->parseStrict( aNewUrl );
248
249 // set a new mark
250 aNewUrl.Mark = "DB/FormGridView";
251 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
252 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
253
254 if (m_xUrlTransformer.is())
255 m_xUrlTransformer->assemble(aNewUrl);
256
257 Reference< XDispatchProvider > xFrameDispatcher( getFrame(), UNO_QUERY );
258 if (xFrameDispatcher.is())
259 xReturn = xFrameDispatcher->queryDispatch(aNewUrl, aTargetFrameName, FrameSearchFlag::PARENT);
260
261 }
262
263 if (!xReturn.is())
264 xReturn = SbaXDataBrowserController::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
265
266 m_bInQueryDispatch = false;
267 return xReturn;
268}
269
271{
272 // say our modify listeners goodbye
273 css::lang::EventObject aEvt;
274 aEvt.Source = static_cast<XWeak*>(this);
276
278
280}
281
283{
285}
286
288{
290}
291
292void SAL_CALL SbaExternalSourceBrowser::unloading(const css::lang::EventObject& aEvent)
293{
294 if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == aEvent.Source))
295 {
296 ClearView();
297 }
298
300}
301
303{
304 Any aOldPos;
305 bool bWasInsertRow = false;
306 bool bBeforeFirst = true;
307 bool bAfterLast = true;
308 Reference< XRowLocate > xCursor(xMaster, UNO_QUERY);
309 Reference< XPropertySet > xMasterProps(xMaster, UNO_QUERY);
310
311 try
312 {
313 // switch the control to design mode
314 if (getBrowserView() && getBrowserView()->getGridControl().is())
315 getBrowserView()->getGridControl()->setDesignMode(true);
316
317 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
318 // restore the old position
319 if (xCursor.is() && xMaster.is())
320 {
321 bBeforeFirst = xMaster->isBeforeFirst();
322 bAfterLast = xMaster->isAfterLast();
323 if(!bBeforeFirst && !bAfterLast)
324 aOldPos = xCursor->getBookmark();
325 }
326
327 if (xMasterProps.is())
328 xMasterProps->getPropertyValue(PROPERTY_ISNEW) >>= bWasInsertRow;
329 }
330 catch( const Exception& )
331 {
332 DBG_UNHANDLED_EXCEPTION("dbaccess");
333 }
334
335 onStartLoading( Reference< XLoadable >( xMaster, UNO_QUERY ) );
336
338 m_pDataSourceImpl->AttachForm(xMaster);
340
341 if (!xMaster.is())
342 return;
343
344 // at this point we have to reset the formatter for the new form
346 // assume that the master form is already loaded
347#if OSL_DEBUG_LEVEL > 0
348 {
349 Reference< XLoadable > xLoadable( xMaster, UNO_QUERY );
350 OSL_ENSURE( xLoadable.is() && xLoadable->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
351 }
352#endif
353
354 LoadFinished(true);
355
356 Reference< XResultSetUpdate > xUpdate(xMaster, UNO_QUERY);
357 try
358 {
359 if (bWasInsertRow && xUpdate.is())
360 xUpdate->moveToInsertRow();
361 else if (xCursor.is() && aOldPos.hasValue())
362 xCursor->moveToBookmark(aOldPos);
363 else if(bBeforeFirst && xMaster.is())
364 xMaster->beforeFirst();
365 else if(bAfterLast && xMaster.is())
366 xMaster->afterLast();
367 }
368 catch(Exception&)
369 {
370 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
371 }
372}
373
375{
376 // set a new (empty) datasource
378
379 // clear all cols in the grid
381 while (xColContainer->getCount() > 0)
382 xColContainer->removeByIndex(0);
383}
384
385void SAL_CALL SbaExternalSourceBrowser::disposing(const css::lang::EventObject& Source)
386{
387 if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == Source.Source))
388 {
389 ClearView();
390 }
391
393}
394
396{
397 if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
398 {
399 Reference< css::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
400 xLoadable->addLoadListener(static_cast<css::form::XLoadListener*>(this));
401 }
402}
403
405{
406 if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
407 {
408 Reference< css::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
409 xLoadable->removeLoadListener(static_cast<css::form::XLoadListener*>(this));
410 }
411}
412
413/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: exsrcbrw.cxx:73
virtual void SAL_CALL dispatch(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &aArgs) override
Definition: exsrcbrw.cxx:110
virtual void SAL_CALL unloading(const css::lang::EventObject &aEvent) override
Definition: exsrcbrw.cxx:292
virtual ~SbaExternalSourceBrowser() override
Definition: exsrcbrw.cxx:69
virtual void SAL_CALL modified(const css::lang::EventObject &aEvent) override
Definition: exsrcbrw.cxx:101
void Attach(const css::uno::Reference< css::sdbc::XRowSet > &xMaster)
Definition: exsrcbrw.cxx:302
virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL &aURL, const OUString &aTargetFrameName, sal_Int32 nSearchFlags) override
Definition: exsrcbrw.cxx:217
virtual bool InitializeForm(const css::uno::Reference< css::beans::XPropertySet > &i_formProperties) override
Definition: exsrcbrw.cxx:89
virtual void SAL_CALL addModifyListener(const css::uno::Reference< css::util::XModifyListener > &aListener) override
Definition: exsrcbrw.cxx:282
virtual void SAL_CALL disposing() override
Definition: exsrcbrw.cxx:270
virtual void SAL_CALL removeModifyListener(const css::uno::Reference< css::util::XModifyListener > &aListener) override
Definition: exsrcbrw.cxx:287
virtual css::uno::Reference< css::sdbc::XRowSet > CreateForm() override
Definition: exsrcbrw.cxx:83
SbaExternalSourceBrowser(const css::uno::Reference< css::uno::XComponentContext > &_rM)
Definition: exsrcbrw.cxx:61
virtual OUString SAL_CALL getImplementationName() override
Definition: exsrcbrw.cxx:78
::comphelper::OInterfaceContainerHelper3< css::util::XModifyListener > m_aModifyListeners
Definition: exsrcbrw.hxx:36
rtl::Reference< SbaXFormAdapter > m_pDataSourceImpl
Definition: exsrcbrw.hxx:38
virtual bool LoadForm() override
Definition: exsrcbrw.cxx:94
void onStartLoading(const css::uno::Reference< css::form::XLoadable > &_rxLoadable)
Definition: brwctrlr.cxx:565
virtual void LoadFinished(bool bWasSynch)
Definition: brwctrlr.cxx:2426
virtual void SAL_CALL disposing() override
Definition: brwctrlr.cxx:1118
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &_rType) override
Definition: brwctrlr.cxx:491
UnoDataBrowserView * getBrowserView() const
Definition: brwctrlr.hxx:138
virtual void SAL_CALL unloading(const css::lang::EventObject &aEvent) override
Definition: brwctrlr.cxx:2483
css::uno::Reference< css::awt::XControlModel > getControlModel() const
Definition: brwctrlr.hxx:125
virtual void SAL_CALL modified(const css::lang::EventObject &aEvent) override
Definition: brwctrlr.cxx:1072
const css::uno::Reference< css::awt::XControl > & getGridControl() const
Definition: brwview.hxx:51
#define DBG_UNHANDLED_EXCEPTION(...)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * org_openoffice_comp_dbu_OFormGridView_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: exsrcbrw.cxx:44
URL aURL
Definition: intercept.cxx:87
sal_Int64 n
#define SAL_WARN(area, stream)
@ Exception
Type
::osl::Mutex & getMutex()
detail::Optional< sal_Int16 >::type tryAccess< sal_Int16 >(css::uno::Any const &any)
constexpr OUStringLiteral PROPERTY_ISNEW(u"IsNew")