1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
22 #include "FormattedField.hxx"
23 #include <services.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <cppuhelper/queryinterface.hxx>
26 #include <connectivity/dbtools.hxx>
27 #include <tools/debug.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/settings.hxx>
30 #include <i18nlangtag/languagetag.hxx>
31 #include <com/sun/star/io/XMarkableStream.hpp>
33 using namespace comphelper
;
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::form
;
42 using namespace ::com::sun::star::awt
;
43 using namespace ::com::sun::star::io
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::util
;
47 OFormattedFieldWrapper::OFormattedFieldWrapper(const Reference
<XComponentContext
>& _rxFactory
,
48 OUString
const & implementationName
)
49 :m_xContext(_rxFactory
)
50 ,m_implementationName(implementationName
)
54 css::uno::Reference
<css::uno::XInterface
> OFormattedFieldWrapper::createFormattedFieldWrapper(const css::uno::Reference
< css::uno::XComponentContext
>& _rxFactory
, bool bActAsFormatted
, OUString
const & implementationName
)
56 rtl::Reference
<OFormattedFieldWrapper
> pRef
= new OFormattedFieldWrapper(_rxFactory
,
61 // instantiate a FormattedModel
62 // (instantiate it directly ..., as the OFormattedModel isn't
63 // registered for any service names anymore)
64 rtl::Reference
<OFormattedModel
> pModel
= new OFormattedModel(pRef
->m_xContext
);
66 pRef
->m_xAggregate
= pModel
;
67 OSL_ENSURE(pRef
->m_xAggregate
.is(), "the OFormattedModel didn't have an XAggregation interface !");
69 // _before_ setting the delegator, give it to the member references
70 pRef
->m_xFormattedPart
= pModel
;
71 pRef
->m_pEditPart
.set(new OEditModel(pRef
->m_xContext
));
74 if (pRef
->m_xAggregate
.is())
75 { // has to be in its own block because of the temporary variable created by *this
76 pRef
->m_xAggregate
->setDelegator(static_cast<XWeak
*>(pRef
.get()));
79 css::uno::Reference
<css::uno::XInterface
> xRef(*pRef
);
84 Reference
< XCloneable
> SAL_CALL
OFormattedFieldWrapper::createClone()
88 rtl::Reference
< OFormattedFieldWrapper
> xRef(new OFormattedFieldWrapper(m_xContext
,
89 m_implementationName
));
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
.set(xClone
, UNO_QUERY
);
99 OSL_ENSURE(xRef
->m_xAggregate
.is(), "invalid aggregate cloned !");
101 xRef
->m_xFormattedPart
.set(
102 Reference
< XInterface
>(xClone
), css::uno::UNO_QUERY
);
104 if ( m_pEditPart
.is() )
106 xRef
->m_pEditPart
.set( new OEditModel(m_pEditPart
.get(), m_xContext
) );
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 its own block because of the temporary variable created by *this
115 xRef
->m_xAggregate
->setDelegator(static_cast< XWeak
* >(xRef
.get()));
121 OFormattedFieldWrapper::~OFormattedFieldWrapper()
123 // release the aggregated object (if any)
124 if (m_xAggregate
.is())
125 m_xAggregate
->setDelegator(css::uno::Reference
<css::uno::XInterface
> ());
129 Any SAL_CALL
OFormattedFieldWrapper::queryAggregation(const Type
& _rType
)
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
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
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.
164 if (m_xAggregate
.is())
165 aReturn
= m_xAggregate
->queryAggregation(_rType
);
173 OUString SAL_CALL
OFormattedFieldWrapper::getServiceName()
175 // return the old compatibility name for an EditModel
176 return FRM_COMPONENT_EDIT
;
179 OUString SAL_CALL
OFormattedFieldWrapper::getImplementationName( )
181 return m_implementationName
;
184 sal_Bool SAL_CALL
OFormattedFieldWrapper::supportsService( const OUString
& _rServiceName
)
186 return cppu::supportsService(this, _rServiceName
);
189 Sequence
< OUString
> SAL_CALL
OFormattedFieldWrapper::getSupportedServiceNames( )
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
)
199 // can't write myself
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
);
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
= m_pEditPart
;
223 Locale aAppLanguage
= Application::GetSettings().GetUILanguageTag().getLocale();
224 dbtools::TransferFormComponentProperties(xFormatProps
, xEditProps
, aAppLanguage
);
226 // then write the edit part, after switching to "fake mode"
227 m_pEditPart
->enableFormattedWriteFake();
228 m_pEditPart
->write(_rxOutStream
);
229 m_pEditPart
->disableFormattedWriteFake();
231 // and finally write the formatted part we're really interested in
232 m_xFormattedPart
->write(_rxOutStream
);
235 void SAL_CALL
OFormattedFieldWrapper::read(const Reference
<XObjectInputStream
>& _rxInStream
)
238 if (m_xAggregate
.is())
239 { // we already made a decision if we're an EditModel or a FormattedModel
241 // if we act as formatted, we have to read the edit part first
242 if (m_xFormattedPart
.is())
244 // two possible cases:
245 // a) the stuff was written by a version which didn't work with an Edit header (all intermediate
246 // versions >5.1 && <=568)
247 // b) it was written by a version using edit headers
248 // as we can distinguish a) from b) only after we have read the edit part, we need to remember the
250 Reference
<XMarkableStream
> xInMarkable(_rxInStream
, UNO_QUERY
);
251 DBG_ASSERT(xInMarkable
.is(), "OFormattedFieldWrapper::read : can only work with markable streams !");
252 sal_Int32 nBeforeEditPart
= xInMarkable
->createMark();
254 m_pEditPart
->read(_rxInStream
);
255 // this only works because an edit model can read the stuff written by a formatted model
256 // (maybe with some assertions) , but not vice versa
257 if (!m_pEditPart
->lastReadWasFormattedFake())
258 { // case a), written with a version without the edit part fake, so seek to the start position, again
259 xInMarkable
->jumpToMark(nBeforeEditPart
);
261 xInMarkable
->deleteMark(nBeforeEditPart
);
264 Reference
<XPersistObject
> xAggregatePersistence
;
265 query_aggregation(m_xAggregate
, xAggregatePersistence
);
266 DBG_ASSERT(xAggregatePersistence
.is(), "OFormattedFieldWrapper::read : don't know how to handle this : can't read !");
267 // oops ... We gave an XPersistObject interface to the caller but now we aren't an XPersistObject ...
269 if (xAggregatePersistence
.is())
270 xAggregatePersistence
->read(_rxInStream
);
274 // we have to decide from the data within the stream whether we should
275 // be an EditModel or a FormattedModel
278 // let an OEditModel do the reading
279 rtl::Reference
< OEditModel
> pBasicReader(new OEditModel(m_xContext
));
280 pBasicReader
->read(_rxInStream
);
282 // was it really an edit model ?
283 if (!pBasicReader
->lastReadWasFormattedFake())
286 m_xAggregate
= pBasicReader
;
289 { // no -> substitute it with a formatted model
290 // let the formatted model do the reading
291 m_xFormattedPart
.set(new OFormattedModel(m_xContext
));
292 m_xFormattedPart
->read(_rxInStream
);
293 m_pEditPart
= pBasicReader
;
294 m_xAggregate
.set( m_xFormattedPart
, UNO_QUERY
);
298 // do the aggregation
299 osl_atomic_increment(&m_refCount
);
300 if (m_xAggregate
.is())
301 { // has to be in its own block because of the temporary variable created by *this
302 m_xAggregate
->setDelegator(static_cast<XWeak
*>(this));
304 osl_atomic_decrement(&m_refCount
);
307 void OFormattedFieldWrapper::ensureAggregate()
309 if (m_xAggregate
.is())
313 // instantiate an EditModel (the only place where we are allowed to decide that we're a FormattedModel
315 css::uno::Reference
<css::uno::XInterface
> xEditModel
= m_xContext
->getServiceManager()->createInstanceWithContext(FRM_SUN_COMPONENT_TEXTFIELD
, m_xContext
);
316 if (!xEditModel
.is())
318 // arghhh... instantiate it directly... it's dirty, but we really need this aggregate
319 rtl::Reference
<OEditModel
> pModel
= new OEditModel(m_xContext
);
320 xEditModel
.set(static_cast<XWeak
*>(pModel
.get()), css::uno::UNO_QUERY
);
323 m_xAggregate
.set(xEditModel
, UNO_QUERY
);
324 DBG_ASSERT(m_xAggregate
.is(), "OFormattedFieldWrapper::ensureAggregate : the OEditModel didn't have an XAggregation interface !");
327 Reference
< XServiceInfo
> xSI(m_xAggregate
, UNO_QUERY
);
330 OSL_FAIL("OFormattedFieldWrapper::ensureAggregate: the aggregate has no XServiceInfo!");
331 m_xAggregate
.clear();
336 osl_atomic_increment(&m_refCount
);
337 if (m_xAggregate
.is())
338 { // has to be in its own block because of the temporary variable created by *this
339 m_xAggregate
->setDelegator(static_cast<XWeak
*>(this));
341 osl_atomic_decrement(&m_refCount
);
344 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
345 com_sun_star_form_OFormattedFieldWrapper_get_implementation(css::uno::XComponentContext
* component
,
346 css::uno::Sequence
<css::uno::Any
> const &)
348 css::uno::Reference
<css::uno::XInterface
> inst(
349 OFormattedFieldWrapper::createFormattedFieldWrapper(
350 component
, false, "com.sun.star.form.OFormattedFieldWrapper"));
355 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
356 com_sun_star_comp_forms_OFormattedFieldWrapper_ForcedFormatted_get_implementation(css::uno::XComponentContext
* component
,
357 css::uno::Sequence
<css::uno::Any
> const &)
359 css::uno::Reference
<css::uno::XInterface
> inst(
360 OFormattedFieldWrapper::createFormattedFieldWrapper(
361 component
, true, "com.sun.star.comp.forms.OFormattedFieldWrapper_ForcedFormatted"));
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */