bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / FormattedFieldWrapper.cxx
blob963ab91a59cfaf5c86ceeb2ed2aec03c4c38a8c4
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 "FormattedFieldWrapper.hxx"
21 #include "Edit.hxx"
22 #include "FormattedField.hxx"
23 #include "EditBase.hxx"
24 #include "services.hxx"
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <cppuhelper/queryinterface.hxx>
28 #include <connectivity/dbtools.hxx>
29 #include <tools/debug.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
33 using namespace frm;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::sdb;
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::sdbcx;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::container;
40 using namespace ::com::sun::star::form;
41 using namespace ::com::sun::star::awt;
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::util;
46 OFormattedFieldWrapper::OFormattedFieldWrapper(const Reference<XComponentContext>& _rxFactory)
47 :m_xContext(_rxFactory)
51 InterfaceRef OFormattedFieldWrapper::createFormattedFieldWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext>& _rxFactory, bool bActAsFormatted)
53 OFormattedFieldWrapper *pRef = new OFormattedFieldWrapper(_rxFactory);
55 if (bActAsFormatted)
57 // instantiate an FormattedModel
58 // (instantiate it directly ..., as the OFormattedModel isn't
59 // registered for any service names anymore)
60 OFormattedModel* pModel = new OFormattedModel(pRef->m_xContext);
61 InterfaceRef xFormattedModel(
62 static_cast<XWeak*>(pModel), css::uno::UNO_QUERY);
64 pRef->m_xAggregate = Reference<XAggregation> (xFormattedModel, UNO_QUERY);
65 OSL_ENSURE(pRef->m_xAggregate.is(), "the OFormattedModel didn't have an XAggregation interface !");
67 // _before_ setting the delegator, give it to the member references
68 pRef->m_xFormattedPart.set(xFormattedModel, css::uno::UNO_QUERY);
69 pRef->m_pEditPart = rtl::Reference< OEditModel >(new OEditModel(pRef->m_xContext));
72 osl_atomic_increment(&pRef->m_refCount);
74 if (pRef->m_xAggregate.is())
75 { // has to be in it's own block because of the temporary variable created by *this
76 pRef->m_xAggregate->setDelegator(static_cast<XWeak*>(pRef));
79 InterfaceRef xRef(*pRef);
80 osl_atomic_decrement(&pRef->m_refCount);
82 return xRef;
85 Reference< XCloneable > SAL_CALL OFormattedFieldWrapper::createClone() throw (RuntimeException, std::exception)
87 ensureAggregate();
89 rtl::Reference< OFormattedFieldWrapper > xRef(new OFormattedFieldWrapper(m_xContext));
91 Reference< XCloneable > xCloneAccess;
92 query_aggregation( m_xAggregate, xCloneAccess );
94 // clone the aggregate
95 if ( xCloneAccess.is() )
97 Reference< XCloneable > xClone = xCloneAccess->createClone();
98 xRef->m_xAggregate = Reference< XAggregation >(xClone, UNO_QUERY);
99 OSL_ENSURE(xRef->m_xAggregate.is(), "invalid aggregate cloned !");
101 xRef->m_xFormattedPart.set(
102 Reference< XInterface >(xClone.get()), css::uno::UNO_QUERY);
104 if ( m_pEditPart.is() )
106 xRef->m_pEditPart = rtl::Reference< OEditModel >( new OEditModel(m_pEditPart.get(), m_xContext));
109 else
110 { // the clone source does not yet have an aggregate -> we don't yet need one, too
113 if ( xRef->m_xAggregate.is() )
114 { // has to be in it's own block because of the temporary variable created by *this
115 xRef->m_xAggregate->setDelegator(static_cast< XWeak* >(xRef.get()));
118 return xRef.get();
121 OFormattedFieldWrapper::~OFormattedFieldWrapper()
123 // release the aggregated object (if any)
124 if (m_xAggregate.is())
125 m_xAggregate->setDelegator(InterfaceRef ());
129 Any SAL_CALL OFormattedFieldWrapper::queryAggregation(const Type& _rType) throw (RuntimeException, std::exception)
131 Any aReturn;
133 if (_rType.equals( cppu::UnoType<XTypeProvider>::get() ) )
134 { // a XTypeProvider interface needs a working aggregate - we don't want to give the type provider
135 // of our base class (OFormattedFieldWrapper_Base) to the caller as it supplies nearly nothing
136 ensureAggregate();
137 if (m_xAggregate.is())
138 aReturn = m_xAggregate->queryAggregation(_rType);
141 if (!aReturn.hasValue())
143 aReturn = OFormattedFieldWrapper_Base::queryAggregation(_rType);
145 if ((_rType.equals( cppu::UnoType<XServiceInfo>::get() ) ) && aReturn.hasValue())
146 { // somebody requested an XServiceInfo interface and our base class provided it
147 // check our aggregate if it has one, too
148 ensureAggregate();
151 if (!aReturn.hasValue())
153 aReturn = ::cppu::queryInterface( _rType,
154 static_cast< XPersistObject* >( this ),
155 static_cast< XCloneable* >( this )
158 if (!aReturn.hasValue())
160 // somebody requests an interface other than the basics (XInterface) and other than
161 // the two we can supply without an aggregate. So ensure
162 // the aggregate exists.
163 ensureAggregate();
164 if (m_xAggregate.is())
165 aReturn = m_xAggregate->queryAggregation(_rType);
170 return aReturn;
173 OUString SAL_CALL OFormattedFieldWrapper::getServiceName() throw(RuntimeException, std::exception)
175 // return the old compatibility name for an EditModel
176 return OUString(FRM_COMPONENT_EDIT);
179 OUString SAL_CALL OFormattedFieldWrapper::getImplementationName( ) throw (RuntimeException, std::exception)
181 return OUString("com.sun.star.comp.forms.OFormattedFieldWrapper_ForcedFormatted");
184 sal_Bool SAL_CALL OFormattedFieldWrapper::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
186 return cppu::supportsService(this, _rServiceName);
189 Sequence< OUString > SAL_CALL OFormattedFieldWrapper::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
191 DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::getSupportedServiceNames: should never have made it 'til here without an aggregate!");
192 Reference< XServiceInfo > xSI;
193 m_xAggregate->queryAggregation(cppu::UnoType<XServiceInfo>::get()) >>= xSI;
194 return xSI->getSupportedServiceNames();
197 void SAL_CALL OFormattedFieldWrapper::write(const Reference<XObjectOutputStream>& _rxOutStream) throw( IOException, RuntimeException, std::exception )
199 // can't write myself
200 ensureAggregate();
202 // if we act as real edit field, we can simple forward this write request
203 if (!m_xFormattedPart.is())
205 Reference<XPersistObject> xAggregatePersistence;
206 query_aggregation(m_xAggregate, xAggregatePersistence);
207 DBG_ASSERT(xAggregatePersistence.is(), "OFormattedFieldWrapper::write : don't know how to handle this : can't write !");
208 // oops ... We gave an XPersistObject interface to the caller but now we aren't an XPersistObject ...
209 if (xAggregatePersistence.is())
210 xAggregatePersistence->write(_rxOutStream);
211 return;
214 // else we have to write an edit part first
215 OSL_ENSURE(m_pEditPart.is(), "OFormattedFieldWrapper::write : formatted part without edit part ?");
216 if ( !m_pEditPart.is() )
217 throw RuntimeException( OUString(), *this );
219 // for this we transfer the current props of the formatted part to the edit part
220 Reference<XPropertySet> xFormatProps(m_xFormattedPart, UNO_QUERY);
221 Reference<XPropertySet> xEditProps(
222 static_cast<XWeak*>(m_pEditPart.get()), css::uno::UNO_QUERY);
224 Locale aAppLanguage = Application::GetSettings().GetUILanguageTag().getLocale();
225 dbtools::TransferFormComponentProperties(xFormatProps, xEditProps, aAppLanguage);
227 // then write the edit part, after switching to "fake mode"
228 m_pEditPart->enableFormattedWriteFake();
229 m_pEditPart->write(_rxOutStream);
230 m_pEditPart->disableFormattedWriteFake();
232 // and finally write the formatted part we're really interested in
233 m_xFormattedPart->write(_rxOutStream);
236 void SAL_CALL OFormattedFieldWrapper::read(const Reference<XObjectInputStream>& _rxInStream) throw( IOException, RuntimeException, std::exception )
238 SolarMutexGuard g;
239 if (m_xAggregate.is())
240 { // we alread did a decision if we're an EditModel or a FormattedModel
242 // if we act as formatted, we have to read the edit part first
243 if (m_xFormattedPart.is())
245 // two possible cases:
246 // a) the stuff was written by a version which didn't work with an Edit header (all intermediate
247 // versions >5.1 && <=568)
248 // b) it was written by a version using edit headers
249 // as we can distinguish a) from b) only after we have read the edit part, we need to remember the
250 // position
251 Reference<XMarkableStream> xInMarkable(_rxInStream, UNO_QUERY);
252 DBG_ASSERT(xInMarkable.is(), "OFormattedFieldWrapper::read : can only work with markable streams !");
253 sal_Int32 nBeforeEditPart = xInMarkable->createMark();
255 m_pEditPart->read(_rxInStream);
256 // this only works because an edit model can read the stuff written by a formatted model
257 // (maybe with some assertions) , but not vice versa
258 if (!m_pEditPart->lastReadWasFormattedFake())
259 { // case a), written with a version without the edit part fake, so seek to the start position, again
260 xInMarkable->jumpToMark(nBeforeEditPart);
262 xInMarkable->deleteMark(nBeforeEditPart);
265 Reference<XPersistObject> xAggregatePersistence;
266 query_aggregation(m_xAggregate, xAggregatePersistence);
267 DBG_ASSERT(xAggregatePersistence.is(), "OFormattedFieldWrapper::read : don't know how to handle this : can't read !");
268 // oops ... We gave an XPersistObject interface to the caller but now we aren't an XPersistObject ...
270 if (xAggregatePersistence.is())
271 xAggregatePersistence->read(_rxInStream);
272 return;
275 // we have to decide from the data within the stream whether we should
276 // be an EditModel or a FormattedModel
279 // let an OEditModel do the reading
280 rtl::Reference< OEditModel > pBasicReader(new OEditModel(m_xContext));
281 pBasicReader->read(_rxInStream);
283 // was it really an edit model ?
284 if (!pBasicReader->lastReadWasFormattedFake())
286 // yes -> all fine
287 m_xAggregate = Reference< XAggregation >( pBasicReader.get() );
289 else
290 { // no -> substitute it with a formatted model
291 // let the formmatted model do the reading
292 m_xFormattedPart = Reference< XPersistObject >(new OFormattedModel(m_xContext));
293 m_xFormattedPart->read(_rxInStream);
294 m_pEditPart = pBasicReader;
295 m_xAggregate = Reference< XAggregation >( m_xFormattedPart, UNO_QUERY );
299 // do the aggregation
300 osl_atomic_increment(&m_refCount);
301 if (m_xAggregate.is())
302 { // has to be in it's own block because of the temporary variable created by *this
303 m_xAggregate->setDelegator(static_cast<XWeak*>(this));
305 osl_atomic_decrement(&m_refCount);
308 void OFormattedFieldWrapper::ensureAggregate()
310 if (m_xAggregate.is())
311 return;
314 // instantiate an EditModel (the only place where we are allowed to decide that we're an FormattedModel
315 // is in ::read)
316 InterfaceRef xEditModel = m_xContext->getServiceManager()->createInstanceWithContext(FRM_SUN_COMPONENT_TEXTFIELD, m_xContext);
317 if (!xEditModel.is())
319 // arghhh ... instantiate it directly ... it's dirty, but we really need this aggregate
320 OEditModel* pModel = new OEditModel(m_xContext);
321 xEditModel.set(static_cast<XWeak*>(pModel), css::uno::UNO_QUERY);
324 m_xAggregate = Reference<XAggregation> (xEditModel, UNO_QUERY);
325 DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::ensureAggregate : the OEditModel didn't have an XAggregation interface !");
328 Reference< XServiceInfo > xSI(m_xAggregate, UNO_QUERY);
329 if (!xSI.is())
331 OSL_FAIL("OFormattedFieldWrapper::ensureAggregate: the aggregate has no XServiceInfo!");
332 m_xAggregate.clear();
337 osl_atomic_increment(&m_refCount);
338 if (m_xAggregate.is())
339 { // has to be in it's own block because of the temporary variable created by *this
340 m_xAggregate->setDelegator(static_cast<XWeak*>(this));
342 osl_atomic_decrement(&m_refCount);
345 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
346 com_sun_star_form_OFormattedFieldWrapper_get_implementation(::com::sun::star::uno::XComponentContext* component,
347 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
349 css::uno::Reference<css::uno::XInterface> inst(
350 OFormattedFieldWrapper::createFormattedFieldWrapper(component, false));
351 inst->acquire();
352 return inst.get();
355 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
356 com_sun_star_comp_forms_OFormattedFieldWrapper_ForcedFormatted_get_implementation(::com::sun::star::uno::XComponentContext* component,
357 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
359 css::uno::Reference<css::uno::XInterface> inst(
360 OFormattedFieldWrapper::createFormattedFieldWrapper(component, true));
361 inst->acquire();
362 return inst.get();
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */