LCOV - code coverage report
Current view: top level - forms/source/component - Edit.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 195 319 61.1 %
Date: 2014-11-03 Functions: 36 48 75.0 %
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             : 
      21             : #include "Edit.hxx"
      22             : 
      23             : #include <com/sun/star/uno/Type.hxx>
      24             : #include <com/sun/star/awt/XWindow.hpp>
      25             : #include <com/sun/star/container/XIndexAccess.hpp>
      26             : #include <com/sun/star/form/XSubmit.hpp>
      27             : #include <com/sun/star/util/NumberFormat.hpp>
      28             : #include <com/sun/star/sdbc/DataType.hpp>
      29             : #include <com/sun/star/awt/XVclWindowPeer.hpp>
      30             : 
      31             : #include <vcl/svapp.hxx>
      32             : #include <vcl/keycodes.hxx>
      33             : #include <tools/wintypes.hxx>
      34             : 
      35             : #include <connectivity/dbtools.hxx>
      36             : #include <connectivity/formattedcolumnvalue.hxx>
      37             : #include <connectivity/dbconversion.hxx>
      38             : 
      39             : #include <tools/diagnose_ex.h>
      40             : 
      41             : #include <comphelper/container.hxx>
      42             : #include <comphelper/numbers.hxx>
      43             : #include <comphelper/processfactory.hxx>
      44             : 
      45             : using namespace dbtools;
      46             : 
      47             : namespace frm
      48             : {
      49             : using namespace ::com::sun::star::uno;
      50             : using namespace ::com::sun::star::sdb;
      51             : using namespace ::com::sun::star::sdbc;
      52             : using namespace ::com::sun::star::sdbcx;
      53             : using namespace ::com::sun::star::beans;
      54             : using namespace ::com::sun::star::container;
      55             : using namespace ::com::sun::star::form;
      56             : using namespace ::com::sun::star::awt;
      57             : using namespace ::com::sun::star::io;
      58             : using namespace ::com::sun::star::lang;
      59             : using namespace ::com::sun::star::util;
      60             : using namespace ::com::sun::star::form::binding;
      61             : 
      62             : 
      63         116 : InterfaceRef SAL_CALL OEditControl_CreateInstance(const Reference< XMultiServiceFactory > & _rxFactory)
      64             : {
      65         116 :     return *(new OEditControl( comphelper::getComponentContext(_rxFactory) ));
      66             : }
      67             : 
      68             : 
      69           0 : Sequence<Type> OEditControl::_getTypes()
      70             : {
      71           0 :     static Sequence<Type> aTypes;
      72           0 :     if (!aTypes.getLength())
      73             :     {
      74             :         // my two base classes
      75           0 :         aTypes = concatSequences(OBoundControl::_getTypes(), OEditControl_BASE::getTypes());
      76             :     }
      77           0 :     return aTypes;
      78             : }
      79             : 
      80             : 
      81        3898 : Any SAL_CALL OEditControl::queryAggregation(const Type& _rType) throw (RuntimeException, std::exception)
      82             : {
      83        3898 :     Any aReturn = OBoundControl::queryAggregation(_rType);
      84        3898 :     if (!aReturn.hasValue())
      85         230 :         aReturn = OEditControl_BASE::queryInterface(_rType);
      86             : 
      87        3898 :     return aReturn;
      88             : }
      89             : 
      90             : 
      91         116 : OEditControl::OEditControl(const Reference<XComponentContext>& _rxFactory)
      92             :                :OBoundControl( _rxFactory, FRM_SUN_CONTROL_RICHTEXTCONTROL )
      93             :                ,m_aChangeListeners(m_aMutex)
      94         116 :                ,m_nKeyEvent( 0 )
      95             : {
      96             : 
      97         116 :     increment(m_refCount);
      98             :     {
      99         116 :         Reference<XWindow>  xComp;
     100         116 :         if (query_aggregation(m_xAggregate, xComp))
     101             :         {
     102         116 :             xComp->addFocusListener(this);
     103         116 :             xComp->addKeyListener(this);
     104         116 :         }
     105             :     }
     106         116 :     decrement(m_refCount);
     107         116 : }
     108             : 
     109             : 
     110         246 : OEditControl::~OEditControl()
     111             : {
     112          82 :     if( m_nKeyEvent )
     113           0 :         Application::RemoveUserEvent( m_nKeyEvent );
     114             : 
     115          82 :     if (!OComponentHelper::rBHelper.bDisposed)
     116             :     {
     117           0 :         acquire();
     118           0 :         dispose();
     119             :     }
     120             : 
     121         164 : }
     122             : 
     123             : // XChangeBroadcaster
     124             : 
     125           0 : void OEditControl::addChangeListener(const Reference<XChangeListener>& l) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     126             : {
     127           0 :     m_aChangeListeners.addInterface( l );
     128           0 : }
     129             : 
     130             : 
     131           0 : void OEditControl::removeChangeListener(const Reference<XChangeListener>& l) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     132             : {
     133           0 :     m_aChangeListeners.removeInterface( l );
     134           0 : }
     135             : 
     136             : // OComponentHelper
     137             : 
     138          82 : void OEditControl::disposing()
     139             : {
     140          82 :     OBoundControl::disposing();
     141             : 
     142          82 :     EventObject aEvt(static_cast<XWeak*>(this));
     143          82 :     m_aChangeListeners.disposeAndClear(aEvt);
     144          82 : }
     145             : 
     146             : // XServiceInfo
     147             : 
     148           0 : StringSequence  OEditControl::getSupportedServiceNames() throw(std::exception)
     149             : {
     150           0 :     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
     151           0 :     aSupported.realloc(aSupported.getLength() + 1);
     152             : 
     153           0 :     OUString*pArray = aSupported.getArray();
     154           0 :     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_TEXTFIELD;
     155           0 :     return aSupported;
     156             : }
     157             : 
     158             : // XEventListener
     159             : 
     160         272 : void OEditControl::disposing(const EventObject& Source) throw( RuntimeException, std::exception )
     161             : {
     162         272 :     OBoundControl::disposing(Source);
     163         272 : }
     164             : 
     165             : // XFocusListener
     166             : 
     167           0 : void OEditControl::focusGained( const FocusEvent& /*e*/ ) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     168             : {
     169           0 :     Reference<XPropertySet>  xSet(getModel(), UNO_QUERY);
     170           0 :     if (xSet.is())
     171           0 :         xSet->getPropertyValue( PROPERTY_TEXT ) >>= m_aHtmlChangeValue;
     172           0 : }
     173             : 
     174             : 
     175           0 : void OEditControl::focusLost( const FocusEvent& /*e*/ ) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     176             : {
     177           0 :     Reference<XPropertySet>  xSet(getModel(), UNO_QUERY);
     178           0 :     if (xSet.is())
     179             :     {
     180           0 :         OUString sNewHtmlChangeValue;
     181           0 :         xSet->getPropertyValue( PROPERTY_TEXT ) >>= sNewHtmlChangeValue;
     182           0 :         if( sNewHtmlChangeValue != m_aHtmlChangeValue )
     183             :         {
     184           0 :             EventObject aEvt( *this );
     185           0 :             m_aChangeListeners.notifyEach( &XChangeListener::changed, aEvt );
     186           0 :         }
     187           0 :     }
     188           0 : }
     189             : 
     190             : // XKeyListener
     191             : 
     192           0 : void OEditControl::keyPressed(const ::com::sun::star::awt::KeyEvent& e) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     193             : {
     194           0 :     if( e.KeyCode != KEY_RETURN || e.Modifiers != 0 )
     195           0 :         return;
     196             : 
     197             :     // Is the Control in a form with a submit URL?
     198           0 :     Reference<XPropertySet>  xSet(getModel(), UNO_QUERY);
     199           0 :     if( !xSet.is() )
     200           0 :         return;
     201             : 
     202             :     // Not for multiline edits
     203           0 :     Any aTmp( xSet->getPropertyValue(PROPERTY_MULTILINE));
     204           0 :     if ((aTmp.getValueType().equals(::getBooleanCppuType())) && getBOOL(aTmp))
     205           0 :         return;
     206             : 
     207           0 :     Reference<XFormComponent>  xFComp(xSet, UNO_QUERY);
     208           0 :     InterfaceRef  xParent = xFComp->getParent();
     209           0 :     if( !xParent.is() )
     210           0 :         return;
     211             : 
     212           0 :     Reference<XPropertySet>  xFormSet(xParent, UNO_QUERY);
     213           0 :     if( !xFormSet.is() )
     214           0 :         return;
     215             : 
     216           0 :     aTmp = xFormSet->getPropertyValue( PROPERTY_TARGET_URL );
     217           0 :     if (!aTmp.getValueType().equals(cppu::UnoType<OUString>::get()) ||
     218           0 :         getString(aTmp).isEmpty() )
     219           0 :         return;
     220             : 
     221           0 :     Reference<XIndexAccess>  xElements(xParent, UNO_QUERY);
     222           0 :     sal_Int32 nCount = xElements->getCount();
     223           0 :     if( nCount > 1 )
     224             :     {
     225           0 :         Reference<XPropertySet>  xFCSet;
     226           0 :         for( sal_Int32 nIndex=0; nIndex < nCount; nIndex++ )
     227             :         {
     228             :             //  Any aElement(xElements->getByIndex(nIndex));
     229           0 :             xElements->getByIndex(nIndex) >>= xFCSet;
     230             :             OSL_ENSURE(xFCSet.is(),"OEditControl::keyPressed: No XPropertySet!");
     231             : 
     232           0 :             if (hasProperty(PROPERTY_CLASSID, xFCSet) &&
     233           0 :                 getINT16(xFCSet->getPropertyValue(PROPERTY_CLASSID)) == FormComponentType::TEXTFIELD)
     234             :             {
     235             :                 // Found another Edit -> then do not submit!
     236           0 :                 if (xFCSet != xSet)
     237           0 :                     return;
     238             :             }
     239           0 :         }
     240             :     }
     241             : 
     242             :     // Because we're still in the header, trigger submit asynchronously
     243           0 :     if( m_nKeyEvent )
     244           0 :         Application::RemoveUserEvent( m_nKeyEvent );
     245           0 :     m_nKeyEvent = Application::PostUserEvent( LINK(this, OEditControl,OnKeyPressed) );
     246             : }
     247             : 
     248             : 
     249           0 : void OEditControl::keyReleased(const ::com::sun::star::awt::KeyEvent& /*e*/) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     250             : {
     251           0 : }
     252             : 
     253             : 
     254           0 : IMPL_LINK(OEditControl, OnKeyPressed, void*, /*EMPTYARG*/)
     255             : {
     256           0 :     m_nKeyEvent = 0;
     257             : 
     258           0 :     Reference<XFormComponent>  xFComp(getModel(), UNO_QUERY);
     259           0 :     InterfaceRef  xParent = xFComp->getParent();
     260           0 :     Reference<XSubmit>  xSubmit(xParent, UNO_QUERY);
     261           0 :     if (xSubmit.is())
     262           0 :         xSubmit->submit( Reference<XControl>(), ::com::sun::star::awt::MouseEvent() );
     263           0 :     return 0L;
     264             : }
     265             : 
     266             : 
     267         168 : void SAL_CALL OEditControl::createPeer( const Reference< XToolkit>& _rxToolkit, const Reference< XWindowPeer>& _rxParent ) throw ( RuntimeException, std::exception )
     268             : {
     269         168 :     OBoundControl::createPeer(_rxToolkit, _rxParent);
     270         168 : }
     271             : 
     272         176 : InterfaceRef SAL_CALL OEditModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
     273             : {
     274         176 :     return *(new OEditModel( comphelper::getComponentContext(_rxFactory) ));
     275             : }
     276             : 
     277             : 
     278           0 : Sequence<Type> OEditModel::_getTypes()
     279             : {
     280           0 :     return OEditBaseModel::_getTypes();
     281             : }
     282             : 
     283             : 
     284             : 
     285         208 : OEditModel::OEditModel(const Reference<XComponentContext>& _rxFactory)
     286             :     :OEditBaseModel( _rxFactory, FRM_SUN_COMPONENT_RICHTEXTCONTROL, FRM_SUN_CONTROL_TEXTFIELD, true, true )
     287             :     ,m_bMaxTextLenModified(false)
     288         208 :     ,m_bWritingFormattedFake(false)
     289             : {
     290             : 
     291         208 :     m_nClassId = FormComponentType::TEXTFIELD;
     292         208 :     initValueProperty( PROPERTY_TEXT, PROPERTY_ID_TEXT );
     293         208 : }
     294             : 
     295             : 
     296           4 : OEditModel::OEditModel( const OEditModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
     297             :     :OEditBaseModel( _pOriginal, _rxFactory )
     298             :     ,m_bMaxTextLenModified(false)
     299           4 :     ,m_bWritingFormattedFake(false)
     300             : {
     301             : 
     302             :     // Note that most of the properties are not clone from the original object:
     303             :     // Things as the format key, it's type, and such, depend on the field being part of a loaded form
     304             :     // (they're initialized in onConnectedDbColumn). Even if the original object _is_ part of such a form, we ourself
     305             :     // certainly aren't, so these members are defaulted. If we're inserted into a form which is already loaded,
     306             :     // they will be set to new values, anyway ....
     307           4 : }
     308             : 
     309             : 
     310         588 : OEditModel::~OEditModel()
     311             : {
     312         196 :     if (!OComponentHelper::rBHelper.bDisposed)
     313             :     {
     314           0 :         acquire();
     315           0 :         dispose();
     316             :     }
     317             : 
     318         392 : }
     319             : 
     320             : 
     321           4 : IMPLEMENT_DEFAULT_CLONING( OEditModel )
     322             : 
     323             : 
     324         196 : void OEditModel::disposing()
     325             : {
     326         196 :     OEditBaseModel::disposing();
     327         196 :     m_pValueFormatter.reset();
     328         196 : }
     329             : 
     330             : // XPersistObject
     331             : 
     332           2 : OUString SAL_CALL OEditModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     333             : {
     334           2 :     return OUString(FRM_COMPONENT_EDIT);  // old (non-sun) name for compatibility !
     335             : }
     336             : 
     337             : // XServiceInfo
     338             : 
     339         104 : StringSequence SAL_CALL OEditModel::getSupportedServiceNames() throw(std::exception)
     340             : {
     341         104 :     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
     342             : 
     343         104 :     sal_Int32 nOldLen = aSupported.getLength();
     344         104 :     aSupported.realloc( nOldLen + 8 );
     345         104 :     OUString* pStoreTo = aSupported.getArray() + nOldLen;
     346             : 
     347         104 :     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
     348         104 :     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
     349         104 :     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
     350             : 
     351         104 :     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
     352         104 :     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
     353             : 
     354         104 :     *pStoreTo++ = FRM_SUN_COMPONENT_TEXTFIELD;
     355         104 :     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TEXTFIELD;
     356         104 :     *pStoreTo++ = BINDABLE_DATABASE_TEXT_FIELD;
     357             : 
     358         104 :     return aSupported;
     359             : }
     360             : 
     361             : // XPropertySet
     362       27284 : void SAL_CALL OEditModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle ) const
     363             : {
     364       27284 :     if ( PROPERTY_ID_PERSISTENCE_MAXTEXTLENGTH == nHandle )
     365             :     {
     366         178 :         if ( m_bMaxTextLenModified )
     367           0 :             rValue <<= sal_Int16(0);
     368         178 :         else if ( m_xAggregateSet.is() )
     369         178 :             rValue = m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN);
     370             :     }
     371             :     else
     372             :     {
     373       27106 :         OEditBaseModel::getFastPropertyValue(rValue, nHandle );
     374             :     }
     375       27284 : }
     376             : 
     377             : 
     378         214 : void OEditModel::describeFixedProperties( Sequence< Property >& _rProps ) const
     379             : {
     380         214 :     BEGIN_DESCRIBE_PROPERTIES( 5, OEditBaseModel )
     381         214 :         DECL_PROP2(PERSISTENCE_MAXTEXTLENGTH,sal_Int16,         READONLY, TRANSIENT);
     382         214 :         DECL_PROP2(DEFAULT_TEXT,        OUString,        BOUND, MAYBEDEFAULT);
     383         214 :         DECL_BOOL_PROP1(EMPTY_IS_NULL,                          BOUND);
     384         214 :         DECL_PROP1(TABINDEX,            sal_Int16,              BOUND);
     385         214 :         DECL_BOOL_PROP2(FILTERPROPOSAL,                         BOUND, MAYBEDEFAULT);
     386             :     END_DESCRIBE_PROPERTIES();
     387         214 : }
     388             : 
     389             : 
     390         214 : void OEditModel::describeAggregateProperties( Sequence< Property >& _rAggregateProps ) const
     391             : {
     392         214 :     OEditBaseModel::describeAggregateProperties( _rAggregateProps );
     393             : 
     394             :     // our aggregate is a rich text model, which also derives from OControlModel, as
     395             :     // do we, so we need to remove some duplicate properties
     396         214 :     RemoveProperty( _rAggregateProps, PROPERTY_TABINDEX );
     397         214 :     RemoveProperty( _rAggregateProps, PROPERTY_CLASSID );
     398         214 :     RemoveProperty( _rAggregateProps, PROPERTY_NAME );
     399         214 :     RemoveProperty( _rAggregateProps, PROPERTY_TAG );
     400         214 :     RemoveProperty( _rAggregateProps, PROPERTY_NATIVE_LOOK );
     401             : 
     402         214 : }
     403             : 
     404             : 
     405          44 : bool OEditModel::implActsAsRichText( ) const
     406             : {
     407          44 :     bool bActAsRichText = false;
     408          44 :     if ( m_xAggregateSet.is() )
     409             :     {
     410          44 :         OSL_VERIFY( m_xAggregateSet->getPropertyValue( PROPERTY_RICH_TEXT ) >>= bActAsRichText );
     411             :     }
     412          44 :     return bActAsRichText;
     413             : }
     414             : 
     415             : 
     416          10 : void SAL_CALL OEditModel::reset(  ) throw(RuntimeException, std::exception)
     417             : {
     418             :     // no reset if we currently act as rich text control
     419          10 :     if ( implActsAsRichText() )
     420          10 :         return;
     421             : 
     422          10 :     OEditBaseModel::reset();
     423             : }
     424             : 
     425             : 
     426             : namespace
     427             : {
     428           8 :     void lcl_transferProperties( const Reference< XPropertySet >& _rxSource, const Reference< XPropertySet >& _rxDest )
     429             :     {
     430             :         try
     431             :         {
     432           8 :             Reference< XPropertySetInfo > xSourceInfo;
     433           8 :             if ( _rxSource.is() )
     434           8 :                 xSourceInfo = _rxSource->getPropertySetInfo();
     435             : 
     436          16 :             Reference< XPropertySetInfo > xDestInfo;
     437           8 :             if ( _rxDest.is() )
     438           8 :                 xDestInfo = _rxDest->getPropertySetInfo();
     439             : 
     440           8 :             if ( !xSourceInfo.is() || !xDestInfo.is() )
     441             :             {
     442             :                 OSL_FAIL( "lcl_transferProperties: invalid property set(s)!" );
     443           8 :                 return;
     444             :             }
     445             : 
     446          16 :             Sequence< Property > aSourceProps( xSourceInfo->getProperties() );
     447           8 :             const Property* pSourceProps = aSourceProps.getConstArray();
     448           8 :             const Property* pSourcePropsEnd = aSourceProps.getConstArray() + aSourceProps.getLength();
     449         684 :             while ( pSourceProps != pSourcePropsEnd )
     450             :             {
     451         668 :                 if ( !xDestInfo->hasPropertyByName( pSourceProps->Name ) )
     452             :                 {
     453         308 :                     ++pSourceProps;
     454         616 :                     continue;
     455             :                 }
     456             : 
     457         360 :                 Property aDestProp( xDestInfo->getPropertyByName( pSourceProps->Name ) );
     458         360 :                 if ( 0 != ( aDestProp.Attributes & PropertyAttribute::READONLY ) )
     459             :                 {
     460           0 :                     ++pSourceProps;
     461           0 :                     continue;
     462             :                 }
     463             : 
     464             :                 try
     465             :                 {
     466         360 :                     _rxDest->setPropertyValue( pSourceProps->Name, _rxSource->getPropertyValue( pSourceProps->Name ) );
     467             :                 }
     468           0 :                 catch(const IllegalArgumentException& e)
     469             :                 {
     470             : #if OSL_DEBUG_LEVEL > 0
     471             :                     OString sMessage( "could not transfer the property named '" );
     472             :                     sMessage += OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US );
     473             :                     sMessage += OString( "'." );
     474             :                     if ( !e.Message.isEmpty() )
     475             :                     {
     476             :                         sMessage += OString( "\n\nMessage:\n" );
     477             :                         sMessage += OString( e.Message.getStr(), e.Message.getLength(), RTL_TEXTENCODING_ASCII_US );
     478             :                     }
     479             :                     OSL_FAIL( sMessage.getStr() );
     480             : #else
     481             :                     (void)e;
     482             : #endif
     483             :                 }
     484             : 
     485         360 :                 ++pSourceProps;
     486         368 :             }
     487             :         }
     488           0 :         catch( const Exception& )
     489             :         {
     490             :             DBG_UNHANDLED_EXCEPTION();
     491             :         }
     492             :     }
     493             : }
     494             : 
     495             : 
     496           4 : void OEditModel::writeAggregate( const Reference< XObjectOutputStream >& _rxOutStream ) const
     497             : {
     498             :     // we need to fake the writing of our aggregate. Since #i24387#, we have another aggregate,
     499             :     // but for compatibility, we need to use an "old" aggregate for writing and reading
     500             : 
     501             :     Reference< XPropertySet > xFakedAggregate(
     502           8 :         getContext()->getServiceManager()->createInstanceWithContext( OUString(VCL_CONTROLMODEL_EDIT), getContext() ),
     503             :         UNO_QUERY
     504           4 :     );
     505             :     OSL_ENSURE( xFakedAggregate.is(), "OEditModel::writeAggregate: could not create an old EditControlModel!" );
     506           4 :     if ( !xFakedAggregate.is() )
     507           4 :         return;
     508             : 
     509           4 :     lcl_transferProperties( m_xAggregateSet, xFakedAggregate );
     510             : 
     511           8 :     Reference< XPersistObject > xFakedPersist( xFakedAggregate, UNO_QUERY );
     512             :     OSL_ENSURE( xFakedPersist.is(), "OEditModel::writeAggregate: no XPersistObject!" );
     513           4 :     if ( xFakedPersist.is() )
     514           8 :         xFakedPersist->write( _rxOutStream );
     515             : }
     516             : 
     517             : 
     518           4 : void OEditModel::readAggregate( const Reference< XObjectInputStream >& _rxInStream )
     519             : {
     520             :     // we need to fake the reading of our aggregate. Since #i24387#, we have another aggregate,
     521             :     // but for compatibility, we need to use an "old" aggregate for writing and reading
     522             : 
     523             :     Reference< XPropertySet > xFakedAggregate(
     524           8 :         getContext()->getServiceManager()->createInstanceWithContext( OUString(VCL_CONTROLMODEL_EDIT), getContext() ),
     525             :         UNO_QUERY
     526           4 :     );
     527           8 :     Reference< XPersistObject > xFakedPersist( xFakedAggregate, UNO_QUERY );
     528             :     OSL_ENSURE( xFakedPersist.is(), "OEditModel::readAggregate: no XPersistObject, or no faked aggregate at all!" );
     529           4 :     if ( xFakedPersist.is() )
     530             :     {
     531           4 :         xFakedPersist->read( _rxInStream );
     532           4 :         lcl_transferProperties( xFakedAggregate, m_xAggregateSet );
     533           4 :     }
     534           4 : }
     535             : 
     536             : 
     537           4 : void OEditModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     538             : {
     539           4 :     Any aCurrentText;
     540           4 :     sal_Int16 nOldTextLen = 0;
     541             :     // Am I loaded at the moment and did I switch MaxTextLen temporarily?
     542           4 :     if ( m_bMaxTextLenModified )
     543             :     {   // -> for the duration of saving, make my aggregated model believe the old TextLen
     544             : 
     545             :         // before doing this we have to save the current text value of the aggregate, as this may be affected by resetting the text len
     546           0 :         aCurrentText = m_xAggregateSet->getPropertyValue(PROPERTY_TEXT);
     547             : 
     548           0 :         m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN) >>= nOldTextLen;
     549           0 :         m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, makeAny((sal_Int16)0));
     550             :     }
     551             : 
     552           4 :     OEditBaseModel::write(_rxOutStream);
     553             : 
     554           4 :     if ( m_bMaxTextLenModified )
     555             :     {   // Reset again
     556           0 :         m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, makeAny(nOldTextLen));
     557             :         // and reset the text
     558             :         // First we set it to an empty string : Without this the second setPropertyValue would not do anything as it thinks
     559             :         // we aren't changing the prop (it didn't notify the - implicite - change of the text prop while setting the max text len)
     560             :         // This seems to be a bug with in toolkit's EditControl-implementation.
     561           0 :         m_xAggregateSet->setPropertyValue(PROPERTY_TEXT, makeAny(OUString()));
     562           0 :         m_xAggregateSet->setPropertyValue(PROPERTY_TEXT, aCurrentText);
     563           4 :     }
     564           4 : }
     565             : 
     566             : 
     567           4 : void OEditModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     568             : {
     569           4 :     OEditBaseModel::read(_rxInStream);
     570             : 
     571             :     // Some versions (5.1 'til about 552) wrote a wrong DefaultControl-property value which is unknown
     572             :     // to older versions (5.0).
     573             :     // correct this ...
     574           4 :     if (m_xAggregateSet.is())
     575             :     {
     576           4 :         Any aDefaultControl = m_xAggregateSet->getPropertyValue(PROPERTY_DEFAULTCONTROL);
     577          12 :         if  (   (aDefaultControl.getValueType().getTypeClass() == TypeClass_STRING)
     578          16 :             &&  (getString(aDefaultControl) == STARDIV_ONE_FORM_CONTROL_TEXTFIELD )
     579             :             )
     580             :         {
     581           0 :             m_xAggregateSet->setPropertyValue( PROPERTY_DEFAULTCONTROL, makeAny( OUString(STARDIV_ONE_FORM_CONTROL_EDIT) ) );
     582             :             // Older as well as current versions should understand this : the former knew only the STARDIV_ONE_FORM_CONTROL_EDIT,
     583             :             // the latter are registered for both STARDIV_ONE_FORM_CONTROL_EDIT and STARDIV_ONE_FORM_CONTROL_TEXTFIELD.
     584           4 :         }
     585             :     }
     586           4 : }
     587             : 
     588             : 
     589           4 : sal_uInt16 OEditModel::getPersistenceFlags() const
     590             : {
     591           4 :     sal_uInt16 nFlags = OEditBaseModel::getPersistenceFlags();
     592             : 
     593           4 :     if (m_bWritingFormattedFake)
     594           0 :         nFlags |= PF_FAKE_FORMATTED_FIELD;
     595             : 
     596           4 :     return nFlags;
     597             : }
     598             : 
     599             : 
     600          34 : void OEditModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
     601             : {
     602          34 :     Reference< XPropertySet > xField = getField();
     603          34 :     if ( xField.is() )
     604             :     {
     605          34 :         m_pValueFormatter.reset( new ::dbtools::FormattedColumnValue( getContext(), Reference< XRowSet >( _rxForm, UNO_QUERY ), xField ) );
     606             : 
     607          34 :         if ( m_pValueFormatter->getKeyType() != NumberFormat::SCIENTIFIC )
     608             :         {
     609          34 :             m_bMaxTextLenModified = getINT16(m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN)) != 0;
     610          34 :             if ( !m_bMaxTextLenModified )
     611             :             {
     612          34 :                 sal_Int32 nFieldLen = 0;
     613          34 :                 xField->getPropertyValue("Precision") >>= nFieldLen;
     614             : 
     615          34 :                 if (nFieldLen && nFieldLen <= USHRT_MAX)
     616             :                 {
     617           0 :                     Any aVal;
     618           0 :                     aVal <<= (sal_Int16)nFieldLen;
     619           0 :                     m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, aVal);
     620             : 
     621           0 :                     m_bMaxTextLenModified = true;
     622             :                 }
     623             :             }
     624             :             else
     625           0 :                 m_bMaxTextLenModified = false; // to get sure that the text len won't be set in unloaded
     626             :         }
     627          34 :     }
     628          34 : }
     629             : 
     630             : 
     631          34 : void OEditModel::onDisconnectedDbColumn()
     632             : {
     633          34 :     OEditBaseModel::onDisconnectedDbColumn();
     634             : 
     635          34 :     m_pValueFormatter.reset();
     636             : 
     637          34 :     if ( hasField() && m_bMaxTextLenModified )
     638             :     {
     639           0 :         Any aVal;
     640           0 :         aVal <<= (sal_Int16)0;  // Only if it was 0, I switched it in onConnectedDbColumn
     641           0 :         m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, aVal);
     642           0 :         m_bMaxTextLenModified = false;
     643             :     }
     644          34 : }
     645             : 
     646             : 
     647          34 : bool OEditModel::approveDbColumnType( sal_Int32 _nColumnType )
     648             : {
     649             :     // if we act as rich text currently, we do not allow binding to a database column
     650          34 :     if ( implActsAsRichText() )
     651           0 :         return false;
     652             : 
     653          34 :     return OEditBaseModel::approveDbColumnType( _nColumnType );
     654             : }
     655             : 
     656             : 
     657          30 : void OEditModel::resetNoBroadcast()
     658             : {
     659          30 :     OEditBaseModel::resetNoBroadcast();
     660          30 : }
     661             : 
     662             : 
     663           0 : bool OEditModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
     664             : {
     665           0 :     Any aNewValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
     666             : 
     667           0 :     OUString sNewValue;
     668           0 :     aNewValue >>= sNewValue;
     669             : 
     670           0 :     if  (   !aNewValue.hasValue()
     671           0 :         ||  (   sNewValue.isEmpty()         // an empty string
     672           0 :             &&  m_bEmptyIsNull              // which should be interpreted as NULL
     673             :             )
     674             :         )
     675             :     {
     676           0 :         m_xColumnUpdate->updateNull();
     677             :     }
     678             :     else
     679             :     {
     680             :         OSL_PRECOND( m_pValueFormatter.get(), "OEditModel::commitControlValueToDbColumn: no value formatter!" );
     681             :         try
     682             :         {
     683           0 :             if ( m_pValueFormatter.get() )
     684             :             {
     685           0 :                 if ( !m_pValueFormatter->setFormattedValue( sNewValue ) )
     686           0 :                     return false;
     687             :             }
     688             :             else
     689           0 :                 m_xColumnUpdate->updateString( sNewValue );
     690             :         }
     691           0 :         catch ( const Exception& )
     692             :         {
     693           0 :             return false;
     694             :         }
     695             :     }
     696             : 
     697           0 :     return true;
     698             : }
     699             : 
     700             : 
     701          34 : Any OEditModel::translateDbColumnToControlValue()
     702             : {
     703             :     OSL_PRECOND( m_pValueFormatter.get(), "OEditModel::translateDbColumnToControlValue: no value formatter!" );
     704          34 :     Any aRet;
     705          34 :     if ( m_pValueFormatter.get() )
     706             :     {
     707          34 :         OUString sValue( m_pValueFormatter->getFormattedValue() );
     708          68 :         if  (   sValue.isEmpty()
     709          26 :             &&  m_pValueFormatter->getColumn().is()
     710          60 :             &&  m_pValueFormatter->getColumn()->wasNull()
     711             :             )
     712             :         {
     713             :         }
     714             :         else
     715             :         {
     716             :             // #i2817# OJ
     717          34 :             sal_uInt16 nMaxTextLen = getINT16( m_xAggregateSet->getPropertyValue( PROPERTY_MAXTEXTLEN ) );
     718          34 :             if ( nMaxTextLen && sValue.getLength() > nMaxTextLen )
     719             :             {
     720           0 :                 sal_Int32 nDiff = sValue.getLength() - nMaxTextLen;
     721           0 :                 sValue = sValue.replaceAt( nMaxTextLen, nDiff, OUString() );
     722             :             }
     723             : 
     724          34 :             aRet <<= sValue;
     725          34 :         }
     726             :     }
     727             : 
     728          34 :     return aRet.hasValue() ? aRet : makeAny( OUString() );
     729             : }
     730             : 
     731             : 
     732          30 : Any OEditModel::getDefaultForReset() const
     733             : {
     734          30 :     return makeAny( m_aDefaultText );
     735             : }
     736             : 
     737             : 
     738         192 : }
     739             : 
     740             : 
     741             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10