Line data Source code
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/form/FormComponentType.hpp>
22 : #include <com/sun/star/util/XURLTransformer.hpp>
23 : #include <com/sun/star/form/XGridColumnFactory.hpp>
24 : #include <com/sun/star/form/XLoadable.hpp>
25 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
26 : #include "formadapter.hxx"
27 : #include <comphelper/processfactory.hxx>
28 : #include "dbustrings.hrc"
29 : #include "dbu_reghelper.hxx"
30 : #include <tools/diagnose_ex.h>
31 : #include <rtl/strbuf.hxx>
32 :
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::sdb;
35 : using namespace ::com::sun::star::sdbc;
36 : using namespace ::com::sun::star::sdbcx;
37 : using namespace ::com::sun::star::beans;
38 : using namespace ::com::sun::star::container;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::form;
41 : using namespace ::com::sun::star::frame;
42 : using namespace dbaui;
43 :
44 : // SbaExternalSourceBrowser
45 0 : extern "C" void SAL_CALL createRegistryInfo_OFormGridView()
46 : {
47 0 : static OMultiInstanceAutoRegistration< SbaExternalSourceBrowser > aAutoRegistration;
48 0 : }
49 :
50 0 : Any SAL_CALL SbaExternalSourceBrowser::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
51 : {
52 0 : Any aRet = SbaXDataBrowserController::queryInterface(_rType);
53 0 : if(!aRet.hasValue())
54 0 : aRet = ::cppu::queryInterface(_rType,
55 : (::com::sun::star::util::XModifyBroadcaster*)this,
56 0 : (::com::sun::star::form::XLoadListener*)this);
57 :
58 0 : return aRet;
59 : }
60 :
61 0 : SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference< ::com::sun::star::uno::XComponentContext >& _rM)
62 : :SbaXDataBrowserController(_rM)
63 0 : ,m_aModifyListeners(getMutex())
64 : ,m_pDataSourceImpl(NULL)
65 0 : ,m_bInQueryDispatch( sal_False )
66 : {
67 :
68 0 : }
69 :
70 0 : SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
71 : {
72 :
73 0 : }
74 :
75 0 : ::comphelper::StringSequence SAL_CALL SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException, std::exception)
76 : {
77 0 : return getSupportedServiceNames_Static();
78 : }
79 :
80 0 : OUString SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException)
81 : {
82 0 : return OUString("org.openoffice.comp.dbu.OFormGridView");
83 : }
84 :
85 0 : ::comphelper::StringSequence SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException)
86 : {
87 0 : ::comphelper::StringSequence aSupported(1);
88 0 : aSupported[0] = "com.sun.star.sdb.FormGridView";
89 0 : return aSupported;
90 : }
91 :
92 0 : Reference< XInterface > SAL_CALL SbaExternalSourceBrowser::Create(const Reference<XMultiServiceFactory >& _rxFactory)
93 : {
94 0 : return *(new SbaExternalSourceBrowser( comphelper::getComponentContext(_rxFactory)));
95 : }
96 :
97 0 : OUString SAL_CALL SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException, std::exception)
98 : {
99 0 : return getImplementationName_Static();
100 : }
101 :
102 0 : Reference< XRowSet > SbaExternalSourceBrowser::CreateForm()
103 : {
104 0 : m_pDataSourceImpl = new SbaXFormAdapter();
105 0 : return m_pDataSourceImpl;
106 : }
107 :
108 0 : sal_Bool SbaExternalSourceBrowser::InitializeForm(const Reference< XPropertySet > & /*i_formProperties*/)
109 : {
110 0 : return sal_True;
111 : }
112 :
113 0 : sal_Bool SbaExternalSourceBrowser::LoadForm()
114 : {
115 : // as we don't have a main form (yet), we have nothing to do
116 : // we don't call FormLoaded, because this expects a working data source
117 0 : return sal_True;
118 : }
119 :
120 0 : void SbaExternalSourceBrowser::modified(const ::com::sun::star::lang::EventObject& aEvent) throw( RuntimeException, std::exception )
121 : {
122 0 : SbaXDataBrowserController::modified(aEvent);
123 :
124 : // multiplex this event to all my listeners
125 0 : ::com::sun::star::lang::EventObject aEvt(*this);
126 0 : ::cppu::OInterfaceIteratorHelper aIt(m_aModifyListeners);
127 0 : while (aIt.hasMoreElements())
128 0 : ((::com::sun::star::util::XModifyListener*)aIt.next())->modified(aEvt);
129 0 : }
130 :
131 0 : void SAL_CALL SbaExternalSourceBrowser::dispatch(const ::com::sun::star::util::URL& aURL, const Sequence< ::com::sun::star::beans::PropertyValue>& aArgs) throw(::com::sun::star::uno::RuntimeException, std::exception)
132 : {
133 0 : const ::com::sun::star::beans::PropertyValue* pArguments = aArgs.getConstArray();
134 0 : if ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
135 : {
136 : // search the argument describing the column to create
137 0 : OUString sControlType;
138 0 : sal_Int32 nControlPos = -1;
139 0 : Sequence< ::com::sun::star::beans::PropertyValue> aControlProps;
140 : sal_uInt16 i;
141 0 : for ( i = 0; i < aArgs.getLength(); ++i, ++pArguments )
142 : {
143 0 : if ( pArguments->Name == "ColumnType" )
144 : {
145 0 : sal_Bool bCorrectType = pArguments->Value.getValueType().equals(::getCppuType((const OUString*)0));
146 : OSL_ENSURE(bCorrectType, "invalid type for argument \"ColumnType\" !");
147 0 : if (bCorrectType)
148 0 : sControlType = ::comphelper::getString(pArguments->Value);
149 : }
150 0 : else if ( pArguments->Name == "ColumnPosition" )
151 : {
152 0 : sal_Bool bCorrectType = pArguments->Value.getValueType().equals(::getCppuType((const sal_Int16*)0));
153 : OSL_ENSURE(bCorrectType, "invalid type for argument \"ColumnPosition\" !");
154 0 : if (bCorrectType)
155 0 : nControlPos = ::comphelper::getINT16(pArguments->Value);
156 : }
157 0 : else if ( pArguments->Name == "ColumnProperties" )
158 : {
159 0 : sal_Bool bCorrectType = pArguments->Value.getValueType().equals(::getCppuType((const Sequence< ::com::sun::star::beans::PropertyValue>*)0));
160 : OSL_ENSURE(bCorrectType, "invalid type for argument \"ColumnProperties\" !");
161 0 : if (bCorrectType)
162 0 : aControlProps = *(Sequence< ::com::sun::star::beans::PropertyValue>*)pArguments->Value.getValue();
163 : }
164 : else
165 : SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << pArguments->Name << ") !");
166 : }
167 0 : if (sControlType.isEmpty())
168 : {
169 : SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
170 0 : sControlType = "TextField";
171 : }
172 : OSL_ENSURE(aControlProps.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
173 :
174 : // create the col
175 0 : Reference< ::com::sun::star::form::XGridColumnFactory > xColFactory(getControlModel(), UNO_QUERY);
176 0 : Reference< ::com::sun::star::beans::XPropertySet > xNewCol = xColFactory->createColumn(sControlType);
177 0 : Reference< XPropertySetInfo > xNewColProperties;
178 0 : if (xNewCol.is())
179 0 : xNewColProperties = xNewCol->getPropertySetInfo();
180 : // set its properties
181 0 : if (xNewColProperties.is())
182 : {
183 0 : const ::com::sun::star::beans::PropertyValue* pControlProps = aControlProps.getConstArray();
184 0 : for (i=0; i<aControlProps.getLength(); ++i, ++pControlProps)
185 : {
186 : try
187 : {
188 0 : if (xNewColProperties->hasPropertyByName(pControlProps->Name))
189 0 : xNewCol->setPropertyValue(pControlProps->Name, pControlProps->Value);
190 : }
191 0 : catch (const Exception&)
192 : {
193 : SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << pControlProps->Name << ")!");
194 : }
195 : }
196 : }
197 :
198 : // correct the position
199 0 : Reference< ::com::sun::star::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
200 :
201 0 : if (nControlPos > xColContainer->getCount())
202 0 : nControlPos = xColContainer->getCount();
203 0 : if (nControlPos < 0)
204 0 : nControlPos = 0;
205 :
206 : // append the column
207 0 : xColContainer->insertByIndex(nControlPos, makeAny(xNewCol));
208 : }
209 0 : else if ( aURL.Complete == ".uno:FormSlots/ClearView" )
210 : {
211 0 : ClearView();
212 : }
213 0 : else if ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
214 : {
215 0 : if (!m_pDataSourceImpl)
216 0 : return;
217 :
218 0 : Reference< XRowSet > xMasterForm;
219 : // search the arguments for the master form
220 0 : for (sal_uInt16 i=0; i<aArgs.getLength(); ++i, ++pArguments)
221 : {
222 0 : if ( (pArguments->Name == "MasterForm") && (pArguments->Value.getValueTypeClass() == TypeClass_INTERFACE) )
223 : {
224 0 : xMasterForm = Reference< XRowSet > (*(Reference< XInterface > *)pArguments->Value.getValue(), UNO_QUERY);
225 0 : break;
226 : }
227 : }
228 0 : if (!xMasterForm.is())
229 : {
230 : SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
231 0 : return;
232 : }
233 :
234 0 : Attach(xMasterForm);
235 : }
236 : else
237 0 : SbaXDataBrowserController::dispatch(aURL, aArgs);
238 : }
239 :
240 0 : Reference< ::com::sun::star::frame::XDispatch > SAL_CALL SbaExternalSourceBrowser::queryDispatch(const ::com::sun::star::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags) throw( RuntimeException, std::exception )
241 : {
242 0 : Reference< ::com::sun::star::frame::XDispatch > xReturn;
243 0 : if (m_bInQueryDispatch)
244 0 : return xReturn;
245 :
246 0 : m_bInQueryDispatch = sal_True;
247 :
248 0 : if ( ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
249 : // attach a new external form
250 0 : || ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
251 : // add a column to the grid
252 0 : || ( aURL.Complete == ".uno:FormSlots/ClearView" )
253 : // clear the grid
254 : )
255 0 : xReturn = (::com::sun::star::frame::XDispatch*)this;
256 :
257 0 : if ( !xReturn.is()
258 0 : && ( (aURL.Complete == ".uno:FormSlots/moveToFirst" ) || (aURL.Complete == ".uno:FormSlots/moveToPrev" )
259 0 : || (aURL.Complete == ".uno:FormSlots/moveToNext" ) || (aURL.Complete == ".uno:FormSlots/moveToLast" )
260 0 : || (aURL.Complete == ".uno:FormSlots/moveToNew" ) || (aURL.Complete == ".uno:FormSlots/undoRecord" )
261 : )
262 : )
263 : {
264 : OSL_ENSURE(aURL.Mark.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the ::com::sun::star::util::URL shouldn't have a mark !");
265 0 : ::com::sun::star::util::URL aNewUrl = aURL;
266 :
267 : // split the ::com::sun::star::util::URL
268 : OSL_ENSURE( m_xUrlTransformer.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
269 0 : if ( m_xUrlTransformer.is() )
270 0 : m_xUrlTransformer->parseStrict( aNewUrl );
271 :
272 : // set a new mark
273 0 : aNewUrl.Mark = "DB/FormGridView";
274 : // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
275 : // frame, so we use "FormGridView" as mark that a dispatch request came from this view
276 :
277 0 : if (m_xUrlTransformer.is())
278 0 : m_xUrlTransformer->assemble(aNewUrl);
279 :
280 0 : Reference< XDispatchProvider > xFrameDispatcher( getFrame(), UNO_QUERY );
281 0 : if (xFrameDispatcher.is())
282 0 : xReturn = xFrameDispatcher->queryDispatch(aNewUrl, aTargetFrameName, FrameSearchFlag::PARENT);
283 :
284 : }
285 :
286 0 : if (!xReturn.is())
287 0 : xReturn = SbaXDataBrowserController::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
288 :
289 0 : m_bInQueryDispatch = sal_False;
290 0 : return xReturn;
291 : }
292 :
293 0 : void SAL_CALL SbaExternalSourceBrowser::disposing()
294 : {
295 : // say our modify listeners goodbye
296 0 : ::com::sun::star::lang::EventObject aEvt;
297 0 : aEvt.Source = (XWeak*) this;
298 0 : m_aModifyListeners.disposeAndClear(aEvt);
299 :
300 0 : stopListening();
301 :
302 0 : SbaXDataBrowserController::disposing();
303 0 : }
304 :
305 0 : void SAL_CALL SbaExternalSourceBrowser::addModifyListener(const Reference< ::com::sun::star::util::XModifyListener > & aListener) throw( RuntimeException, std::exception )
306 : {
307 0 : m_aModifyListeners.addInterface(aListener);
308 0 : }
309 :
310 0 : void SAL_CALL SbaExternalSourceBrowser::removeModifyListener(const Reference< ::com::sun::star::util::XModifyListener > & aListener) throw( RuntimeException, std::exception )
311 : {
312 0 : m_aModifyListeners.removeInterface(aListener);
313 0 : }
314 :
315 0 : void SAL_CALL SbaExternalSourceBrowser::unloading(const ::com::sun::star::lang::EventObject& aEvent) throw( RuntimeException, std::exception )
316 : {
317 0 : if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == aEvent.Source))
318 : {
319 0 : ClearView();
320 : }
321 :
322 0 : SbaXDataBrowserController::unloading(aEvent);
323 0 : }
324 :
325 0 : void SbaExternalSourceBrowser::Attach(const Reference< XRowSet > & xMaster)
326 : {
327 0 : Any aOldPos;
328 0 : sal_Bool bWasInsertRow = sal_False;
329 0 : sal_Bool bBeforeFirst = sal_True;
330 0 : sal_Bool bAfterLast = sal_True;
331 0 : Reference< XRowLocate > xCursor(xMaster, UNO_QUERY);
332 0 : Reference< XPropertySet > xMasterProps(xMaster, UNO_QUERY);
333 :
334 : try
335 : {
336 : // switch the control to design mode
337 0 : if (getBrowserView() && getBrowserView()->getGridControl().is())
338 0 : getBrowserView()->getGridControl()->setDesignMode(sal_True);
339 :
340 : // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
341 : // restore the old position
342 0 : if (xCursor.is() && xMaster.is())
343 : {
344 0 : bBeforeFirst = xMaster->isBeforeFirst();
345 0 : bAfterLast = xMaster->isAfterLast();
346 0 : if(!bBeforeFirst && !bAfterLast)
347 0 : aOldPos = xCursor->getBookmark();
348 : }
349 :
350 0 : if (xMasterProps.is())
351 0 : xMasterProps->getPropertyValue(PROPERTY_ISNEW) >>= bWasInsertRow;
352 : }
353 0 : catch( const Exception& )
354 : {
355 : DBG_UNHANDLED_EXCEPTION();
356 : }
357 :
358 0 : onStartLoading( Reference< XLoadable >( xMaster, UNO_QUERY ) );
359 :
360 0 : stopListening();
361 0 : m_pDataSourceImpl->AttachForm(xMaster);
362 0 : startListening();
363 :
364 0 : if (xMaster.is())
365 : {
366 : // at this point we have to reset the formatter for the new form
367 0 : initFormatter();
368 : // assume that the master form is already loaded
369 : #if OSL_DEBUG_LEVEL > 0
370 : {
371 : Reference< XLoadable > xLoadable( xMaster, UNO_QUERY );
372 : OSL_ENSURE( xLoadable.is() && xLoadable->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
373 : }
374 : #endif
375 :
376 0 : LoadFinished(sal_True);
377 :
378 0 : Reference< XResultSetUpdate > xUpdate(xMaster, UNO_QUERY);
379 : try
380 : {
381 0 : if (bWasInsertRow && xUpdate.is())
382 0 : xUpdate->moveToInsertRow();
383 0 : else if (xCursor.is() && aOldPos.hasValue())
384 0 : xCursor->moveToBookmark(aOldPos);
385 0 : else if(bBeforeFirst && xMaster.is())
386 0 : xMaster->beforeFirst();
387 0 : else if(bAfterLast && xMaster.is())
388 0 : xMaster->afterLast();
389 : }
390 0 : catch(Exception&)
391 : {
392 : SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
393 0 : }
394 :
395 0 : }
396 0 : }
397 :
398 0 : void SbaExternalSourceBrowser::ClearView()
399 : {
400 : // set a new (empty) datasource
401 0 : Attach(Reference< XRowSet > ());
402 :
403 : // clear all cols in the grid
404 0 : Reference< ::com::sun::star::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
405 0 : while (xColContainer->getCount() > 0)
406 0 : xColContainer->removeByIndex(0);
407 0 : }
408 :
409 0 : void SAL_CALL SbaExternalSourceBrowser::disposing(const ::com::sun::star::lang::EventObject& Source) throw( RuntimeException, std::exception )
410 : {
411 0 : if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == Source.Source))
412 : {
413 0 : ClearView();
414 : }
415 :
416 0 : SbaXDataBrowserController::disposing(Source);
417 0 : }
418 :
419 0 : void SbaExternalSourceBrowser::startListening()
420 : {
421 0 : if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
422 : {
423 0 : Reference< ::com::sun::star::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
424 0 : xLoadable->addLoadListener((::com::sun::star::form::XLoadListener*)this);
425 : }
426 0 : }
427 :
428 0 : void SbaExternalSourceBrowser::stopListening()
429 : {
430 0 : if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
431 : {
432 0 : Reference< ::com::sun::star::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
433 0 : xLoadable->removeLoadListener((::com::sun::star::form::XLoadListener*)this);
434 : }
435 0 : }
436 :
437 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|