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 "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>
32 :
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;
45 :
46 17 : OFormattedFieldWrapper::OFormattedFieldWrapper(const Reference<XComponentContext>& _rxFactory)
47 17 : :m_xContext(_rxFactory)
48 : {
49 17 : }
50 :
51 17 : InterfaceRef OFormattedFieldWrapper::createFormattedFieldWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext>& _rxFactory, bool bActAsFormatted)
52 : {
53 17 : OFormattedFieldWrapper *pRef = new OFormattedFieldWrapper(_rxFactory);
54 :
55 17 : if (bActAsFormatted)
56 : {
57 : // instantiate an FormattedModel
58 : // (instantiate it directly ..., as the OFormattedModel isn't
59 : // registered for any service names anymore)
60 16 : OFormattedModel* pModel = new OFormattedModel(pRef->m_xContext);
61 : InterfaceRef xFormattedModel(
62 16 : static_cast<XWeak*>(pModel), css::uno::UNO_QUERY);
63 :
64 16 : pRef->m_xAggregate = Reference<XAggregation> (xFormattedModel, UNO_QUERY);
65 : OSL_ENSURE(pRef->m_xAggregate.is(), "the OFormattedModel didn't have an XAggregation interface !");
66 :
67 : // _before_ setting the delegator, give it to the member references
68 16 : pRef->m_xFormattedPart.set(xFormattedModel, css::uno::UNO_QUERY);
69 16 : pRef->m_pEditPart = rtl::Reference< OEditModel >(new OEditModel(pRef->m_xContext));
70 : }
71 :
72 17 : osl_atomic_increment(&pRef->m_refCount);
73 :
74 17 : if (pRef->m_xAggregate.is())
75 : { // has to be in it's own block because of the temporary variable created by *this
76 16 : pRef->m_xAggregate->setDelegator(static_cast<XWeak*>(pRef));
77 : }
78 :
79 17 : InterfaceRef xRef(*pRef);
80 17 : osl_atomic_decrement(&pRef->m_refCount);
81 :
82 17 : return xRef;
83 : }
84 :
85 0 : Reference< XCloneable > SAL_CALL OFormattedFieldWrapper::createClone() throw (RuntimeException, std::exception)
86 : {
87 0 : ensureAggregate();
88 :
89 0 : rtl::Reference< OFormattedFieldWrapper > xRef(new OFormattedFieldWrapper(m_xContext));
90 :
91 0 : Reference< XCloneable > xCloneAccess;
92 0 : query_aggregation( m_xAggregate, xCloneAccess );
93 :
94 : // clone the aggregate
95 0 : if ( xCloneAccess.is() )
96 : {
97 0 : Reference< XCloneable > xClone = xCloneAccess->createClone();
98 0 : xRef->m_xAggregate = Reference< XAggregation >(xClone, UNO_QUERY);
99 : OSL_ENSURE(xRef->m_xAggregate.is(), "invalid aggregate cloned !");
100 :
101 0 : xRef->m_xFormattedPart.set(
102 0 : Reference< XInterface >(xClone.get()), css::uno::UNO_QUERY);
103 :
104 0 : if ( m_pEditPart.is() )
105 : {
106 0 : xRef->m_pEditPart = rtl::Reference< OEditModel >( new OEditModel(m_pEditPart.get(), m_xContext));
107 0 : }
108 : }
109 : else
110 : { // the clone source does not yet have an aggregate -> we don't yet need one, too
111 : }
112 :
113 0 : if ( xRef->m_xAggregate.is() )
114 : { // has to be in it's own block because of the temporary variable created by *this
115 0 : xRef->m_xAggregate->setDelegator(static_cast< XWeak* >(xRef.get()));
116 : }
117 :
118 0 : return xRef.get();
119 : }
120 :
121 51 : OFormattedFieldWrapper::~OFormattedFieldWrapper()
122 : {
123 : // release the aggregated object (if any)
124 17 : if (m_xAggregate.is())
125 17 : m_xAggregate->setDelegator(InterfaceRef ());
126 :
127 34 : }
128 :
129 591 : Any SAL_CALL OFormattedFieldWrapper::queryAggregation(const Type& _rType) throw (RuntimeException, std::exception)
130 : {
131 591 : Any aReturn;
132 :
133 591 : 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 0 : ensureAggregate();
137 0 : if (m_xAggregate.is())
138 0 : aReturn = m_xAggregate->queryAggregation(_rType);
139 : }
140 :
141 591 : if (!aReturn.hasValue())
142 : {
143 591 : aReturn = OFormattedFieldWrapper_Base::queryAggregation(_rType);
144 :
145 591 : 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 2 : ensureAggregate();
149 : }
150 :
151 591 : if (!aReturn.hasValue())
152 : {
153 948 : aReturn = ::cppu::queryInterface( _rType,
154 : static_cast< XPersistObject* >( this ),
155 : static_cast< XCloneable* >( this )
156 474 : );
157 :
158 474 : if (!aReturn.hasValue())
159 : {
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 474 : ensureAggregate();
164 474 : if (m_xAggregate.is())
165 474 : aReturn = m_xAggregate->queryAggregation(_rType);
166 : }
167 : }
168 : }
169 :
170 591 : return aReturn;
171 : }
172 :
173 0 : OUString SAL_CALL OFormattedFieldWrapper::getServiceName() throw(RuntimeException, std::exception)
174 : {
175 : // return the old compatibility name for an EditModel
176 0 : return OUString(FRM_COMPONENT_EDIT);
177 : }
178 :
179 1 : OUString SAL_CALL OFormattedFieldWrapper::getImplementationName( ) throw (RuntimeException, std::exception)
180 : {
181 1 : return OUString("com.sun.star.comp.forms.OFormattedFieldWrapper_ForcedFormatted");
182 : }
183 :
184 1 : sal_Bool SAL_CALL OFormattedFieldWrapper::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
185 : {
186 1 : return cppu::supportsService(this, _rServiceName);
187 : }
188 :
189 2 : Sequence< OUString > SAL_CALL OFormattedFieldWrapper::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
190 : {
191 : DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::getSupportedServiceNames: should never have made it 'til here without an aggregate!");
192 2 : Reference< XServiceInfo > xSI;
193 2 : m_xAggregate->queryAggregation(cppu::UnoType<XServiceInfo>::get()) >>= xSI;
194 2 : return xSI->getSupportedServiceNames();
195 : }
196 :
197 0 : void SAL_CALL OFormattedFieldWrapper::write(const Reference<XObjectOutputStream>& _rxOutStream) throw( IOException, RuntimeException, std::exception )
198 : {
199 : // can't write myself
200 0 : ensureAggregate();
201 :
202 : // if we act as real edit field, we can simple forward this write request
203 0 : if (!m_xFormattedPart.is())
204 : {
205 0 : Reference<XPersistObject> xAggregatePersistence;
206 0 : 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 0 : if (xAggregatePersistence.is())
210 0 : xAggregatePersistence->write(_rxOutStream);
211 0 : return;
212 : }
213 :
214 : // else we have to write an edit part first
215 : OSL_ENSURE(m_pEditPart.is(), "OFormattedFieldWrapper::write : formatted part without edit part ?");
216 0 : if ( !m_pEditPart.is() )
217 0 : throw RuntimeException( OUString(), *this );
218 :
219 : // for this we transfer the current props of the formatted part to the edit part
220 0 : Reference<XPropertySet> xFormatProps(m_xFormattedPart, UNO_QUERY);
221 : Reference<XPropertySet> xEditProps(
222 0 : static_cast<XWeak*>(m_pEditPart.get()), css::uno::UNO_QUERY);
223 :
224 0 : Locale aAppLanguage = Application::GetSettings().GetUILanguageTag().getLocale();
225 0 : dbtools::TransferFormComponentProperties(xFormatProps, xEditProps, aAppLanguage);
226 :
227 : // then write the edit part, after switching to "fake mode"
228 0 : m_pEditPart->enableFormattedWriteFake();
229 0 : m_pEditPart->write(_rxOutStream);
230 0 : m_pEditPart->disableFormattedWriteFake();
231 :
232 : // and finally write the formatted part we're really interested in
233 0 : m_xFormattedPart->write(_rxOutStream);
234 : }
235 :
236 1 : void SAL_CALL OFormattedFieldWrapper::read(const Reference<XObjectInputStream>& _rxInStream) throw( IOException, RuntimeException, std::exception )
237 : {
238 1 : SolarMutexGuard g;
239 1 : if (m_xAggregate.is())
240 : { // we alread did a decision if we're an EditModel or a FormattedModel
241 :
242 : // if we act as formatted, we have to read the edit part first
243 0 : if (m_xFormattedPart.is())
244 : {
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 0 : Reference<XMarkableStream> xInMarkable(_rxInStream, UNO_QUERY);
252 : DBG_ASSERT(xInMarkable.is(), "OFormattedFieldWrapper::read : can only work with markable streams !");
253 0 : sal_Int32 nBeforeEditPart = xInMarkable->createMark();
254 :
255 0 : 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 0 : if (!m_pEditPart->lastReadWasFormattedFake())
259 : { // case a), written with a version without the edit part fake, so seek to the start position, again
260 0 : xInMarkable->jumpToMark(nBeforeEditPart);
261 : }
262 0 : xInMarkable->deleteMark(nBeforeEditPart);
263 : }
264 :
265 0 : Reference<XPersistObject> xAggregatePersistence;
266 0 : 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 ...
269 :
270 0 : if (xAggregatePersistence.is())
271 0 : xAggregatePersistence->read(_rxInStream);
272 1 : return;
273 : }
274 :
275 : // we have to decide from the data within the stream whether we should
276 : // be an EditModel or a FormattedModel
277 :
278 : {
279 : // let an OEditModel do the reading
280 1 : rtl::Reference< OEditModel > pBasicReader(new OEditModel(m_xContext));
281 1 : pBasicReader->read(_rxInStream);
282 :
283 : // was it really an edit model ?
284 1 : if (!pBasicReader->lastReadWasFormattedFake())
285 : {
286 : // yes -> all fine
287 1 : m_xAggregate = Reference< XAggregation >( pBasicReader.get() );
288 : }
289 : else
290 : { // no -> substitute it with a formatted model
291 : // let the formmatted model do the reading
292 0 : m_xFormattedPart = Reference< XPersistObject >(new OFormattedModel(m_xContext));
293 0 : m_xFormattedPart->read(_rxInStream);
294 0 : m_pEditPart = pBasicReader;
295 0 : m_xAggregate = Reference< XAggregation >( m_xFormattedPart, UNO_QUERY );
296 1 : }
297 : }
298 :
299 : // do the aggregation
300 1 : osl_atomic_increment(&m_refCount);
301 1 : if (m_xAggregate.is())
302 : { // has to be in it's own block because of the temporary variable created by *this
303 1 : m_xAggregate->setDelegator(static_cast<XWeak*>(this));
304 : }
305 1 : osl_atomic_decrement(&m_refCount);
306 : }
307 :
308 476 : void OFormattedFieldWrapper::ensureAggregate()
309 : {
310 476 : if (m_xAggregate.is())
311 952 : return;
312 :
313 : {
314 : // instantiate an EditModel (the only place where we are allowed to decide that we're an FormattedModel
315 : // is in ::read)
316 0 : InterfaceRef xEditModel = m_xContext->getServiceManager()->createInstanceWithContext(FRM_SUN_COMPONENT_TEXTFIELD, m_xContext);
317 0 : if (!xEditModel.is())
318 : {
319 : // arghhh ... instantiate it directly ... it's dirty, but we really need this aggregate
320 0 : OEditModel* pModel = new OEditModel(m_xContext);
321 0 : xEditModel.set(static_cast<XWeak*>(pModel), css::uno::UNO_QUERY);
322 : }
323 :
324 0 : m_xAggregate = Reference<XAggregation> (xEditModel, UNO_QUERY);
325 : DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::ensureAggregate : the OEditModel didn't have an XAggregation interface !");
326 :
327 : {
328 0 : Reference< XServiceInfo > xSI(m_xAggregate, UNO_QUERY);
329 0 : if (!xSI.is())
330 : {
331 : OSL_FAIL("OFormattedFieldWrapper::ensureAggregate: the aggregate has no XServiceInfo!");
332 0 : m_xAggregate.clear();
333 0 : }
334 0 : }
335 : }
336 :
337 0 : osl_atomic_increment(&m_refCount);
338 0 : if (m_xAggregate.is())
339 : { // has to be in it's own block because of the temporary variable created by *this
340 0 : m_xAggregate->setDelegator(static_cast<XWeak*>(this));
341 : }
342 0 : osl_atomic_decrement(&m_refCount);
343 : }
344 :
345 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
346 1 : com_sun_star_form_OFormattedFieldWrapper_get_implementation(::com::sun::star::uno::XComponentContext* component,
347 : ::com::sun::star::uno::Sequence<css::uno::Any> const &)
348 : {
349 : css::uno::Reference<css::uno::XInterface> inst(
350 1 : OFormattedFieldWrapper::createFormattedFieldWrapper(component, false));
351 1 : inst->acquire();
352 1 : return inst.get();
353 : }
354 :
355 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
356 16 : 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 &)
358 : {
359 : css::uno::Reference<css::uno::XInterface> inst(
360 16 : OFormattedFieldWrapper::createFormattedFieldWrapper(component, true));
361 16 : inst->acquire();
362 16 : return inst.get();
363 : }
364 :
365 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|