LCOV - code coverage report
Current view: top level - forms/source/component - FormattedFieldWrapper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 64 134 47.8 %
Date: 2014-11-03 Functions: 13 17 76.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10