LCOV - code coverage report
Current view: top level - forms/source/component - Date.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 85 131 64.9 %
Date: 2015-06-13 12:38:46 Functions: 20 26 76.9 %
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 "Date.hxx"
      21             : #include "services.hxx"
      22             : #include <tools/date.hxx>
      23             : #include <connectivity/dbconversion.hxx>
      24             : #include <com/sun/star/sdbc/DataType.hpp>
      25             : #include <comphelper/processfactory.hxx>
      26             : 
      27             : using namespace dbtools;
      28             : 
      29             : namespace frm
      30             : {
      31             : 
      32             : 
      33             : using namespace ::com::sun::star;
      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::util;
      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             : 
      46             : 
      47          14 : ODateControl::ODateControl(const Reference<XComponentContext>& _rxFactory)
      48          14 :                :OBoundControl(_rxFactory, VCL_CONTROL_DATEFIELD)
      49             : {
      50          14 : }
      51             : 
      52             : 
      53           0 : Sequence<Type> ODateControl::_getTypes()
      54             : {
      55           0 :     return OBoundControl::_getTypes();
      56             : }
      57             : 
      58             : 
      59           1 : StringSequence SAL_CALL ODateControl::getSupportedServiceNames() throw(std::exception)
      60             : {
      61           1 :     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
      62           1 :     aSupported.realloc(aSupported.getLength() + 2);
      63             : 
      64           1 :     OUString*pArray = aSupported.getArray();
      65           1 :     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD;
      66           1 :     pArray[aSupported.getLength()-2] = STARDIV_ONE_FORM_CONTROL_DATEFIELD;
      67           1 :     return aSupported;
      68             : }
      69             : 
      70             : 
      71           0 : Sequence<Type> ODateModel::_getTypes()
      72             : {
      73           0 :     return OEditBaseModel::_getTypes();
      74             : }
      75             : 
      76             : 
      77          35 : ODateModel::ODateModel(const Reference<XComponentContext>& _rxFactory)
      78             :     : OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_DATEFIELD,
      79             :         FRM_SUN_CONTROL_DATEFIELD, true, true)
      80             :     // use the old control name for compytibility reasons
      81             :     , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
      82          35 :     , m_bDateTimeField(false)
      83             : {
      84          35 :     m_nClassId = FormComponentType::DATEFIELD;
      85          35 :     initValueProperty( PROPERTY_DATE, PROPERTY_ID_DATE );
      86             : 
      87          35 :     setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
      88             : 
      89          35 :     osl_atomic_increment( &m_refCount );
      90             :     try
      91             :     {
      92          35 :         if ( m_xAggregateSet.is() )
      93          35 :             m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, makeAny(util::Date(1, 1, 1800)) );
      94             :     }
      95           0 :     catch( const Exception& )
      96             :     {
      97             :         OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
      98             :     }
      99          35 :     osl_atomic_decrement( &m_refCount );
     100          35 : }
     101             : 
     102             : 
     103           2 : ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
     104             :     : OEditBaseModel(_pOriginal, _rxFactory)
     105             :     , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
     106           2 :     , m_bDateTimeField(false)
     107             : {
     108           2 :     setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
     109           2 : }
     110             : 
     111             : 
     112         105 : ODateModel::~ODateModel( )
     113             : {
     114          35 :     setAggregateSet(Reference< XFastPropertySet >(), -1);
     115          70 : }
     116             : 
     117             : // XCloneable
     118             : 
     119           2 : IMPLEMENT_DEFAULT_CLONING( ODateModel )
     120             : 
     121             : // XServiceInfo
     122             : 
     123          14 : StringSequence SAL_CALL ODateModel::getSupportedServiceNames() throw(std::exception)
     124             : {
     125          14 :     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
     126             : 
     127          14 :     sal_Int32 nOldLen = aSupported.getLength();
     128          14 :     aSupported.realloc( nOldLen + 9 );
     129          14 :     OUString* pStoreTo = aSupported.getArray() + nOldLen;
     130             : 
     131          14 :     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
     132          14 :     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
     133          14 :     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
     134             : 
     135          14 :     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
     136          14 :     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
     137             : 
     138          14 :     *pStoreTo++ = FRM_SUN_COMPONENT_DATEFIELD;
     139          14 :     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD;
     140          14 :     *pStoreTo++ = BINDABLE_DATABASE_DATE_FIELD;
     141             : 
     142          14 :     *pStoreTo++ = FRM_COMPONENT_DATEFIELD;
     143             : 
     144          14 :     return aSupported;
     145             : }
     146             : 
     147             : 
     148           1 : OUString SAL_CALL ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     149             : {
     150           1 :     return OUString(FRM_COMPONENT_DATEFIELD); // old (non-sun) name for compatibility !
     151             : }
     152             : 
     153             : // XPropertySet
     154             : 
     155          39 : void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
     156             : {
     157          39 :     BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
     158          39 :         DECL_PROP3(DEFAULT_DATE,            util::Date,             BOUND, MAYBEDEFAULT, MAYBEVOID);
     159          39 :         DECL_PROP1(TABINDEX,                sal_Int16,              BOUND);
     160          39 :         DECL_PROP1(FORMATKEY,               sal_Int32,              TRANSIENT);
     161          39 :         DECL_IFACE_PROP2(FORMATSSUPPLIER,   XNumberFormatsSupplier, READONLY, TRANSIENT);
     162             :     END_DESCRIBE_PROPERTIES();
     163          39 : }
     164             : 
     165             : 
     166        5744 : void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
     167             : {
     168        5744 :     switch (_nHandle)
     169             :     {
     170             :         case PROPERTY_ID_FORMATKEY:
     171          40 :             getFormatKeyPropertyValue(_rValue);
     172          40 :             break;
     173             :         case PROPERTY_ID_FORMATSSUPPLIER:
     174          39 :             _rValue <<= getFormatsSupplier();
     175          39 :             break;
     176             :         default:
     177        5665 :             OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
     178        5665 :             break;
     179             :     }
     180        5744 : }
     181             : 
     182             : 
     183         500 : sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
     184             :         sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
     185             : {
     186         500 :     if (PROPERTY_ID_FORMATKEY == _nHandle)
     187           1 :         return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
     188             :     else
     189         499 :         return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
     190             : }
     191             : 
     192             : 
     193         118 : void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception, std::exception)
     194             : {
     195         118 :     if (PROPERTY_ID_FORMATKEY == _nHandle)
     196           0 :         setFormatKeyPropertyValue(_rValue);
     197             :     else
     198         118 :         OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
     199         118 : }
     200             : 
     201             : // XLoadListener
     202             : 
     203           0 : void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
     204             : {
     205           0 :     OBoundControlModel::onConnectedDbColumn( _rxForm );
     206           0 :     Reference<XPropertySet> xField = getField();
     207           0 :     if (xField.is())
     208             :     {
     209           0 :         m_bDateTimeField = false;
     210             :         try
     211             :         {
     212           0 :             sal_Int32 nFieldType = 0;
     213           0 :             xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
     214           0 :             m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
     215             :         }
     216           0 :         catch(const Exception&)
     217             :         {
     218             :         }
     219           0 :     }
     220           0 : }
     221             : 
     222             : 
     223           0 : bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
     224             : {
     225           0 :     Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
     226           0 :     if ( !compare( aControlValue, m_aSaveValue ) )
     227             :     {
     228           0 :         if ( !aControlValue.hasValue() )
     229           0 :             m_xColumnUpdate->updateNull();
     230             :         else
     231             :         {
     232             :             try
     233             :             {
     234           0 :                 util::Date aDate;
     235           0 :                 if ( !( aControlValue >>= aDate ) )
     236             :                 {
     237           0 :                     sal_Int32 nAsInt(0);
     238           0 :                     aControlValue >>= nAsInt;
     239           0 :                     aDate = DBTypeConversion::toDate(nAsInt);
     240             :                 }
     241             : 
     242           0 :                 if ( !m_bDateTimeField )
     243           0 :                     m_xColumnUpdate->updateDate( aDate );
     244             :                 else
     245             :                 {
     246           0 :                     util::DateTime aDateTime = m_xColumn->getTimestamp();
     247           0 :                     aDateTime.Day = aDate.Day;
     248           0 :                     aDateTime.Month = aDate.Month;
     249           0 :                     aDateTime.Year = aDate.Year;
     250           0 :                     m_xColumnUpdate->updateTimestamp( aDateTime );
     251             :                 }
     252             :             }
     253           0 :             catch(const Exception&)
     254             :             {
     255           0 :                 return false;
     256             :             }
     257             :         }
     258           0 :         m_aSaveValue = aControlValue;
     259             :     }
     260           0 :     return true;
     261             : }
     262             : 
     263             : 
     264           0 : Any ODateModel::translateControlValueToExternalValue( ) const
     265             : {
     266           0 :     return getControlValue();
     267             : }
     268             : 
     269             : 
     270           2 : Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
     271             : {
     272           2 :     return _rExternalValue;
     273             : }
     274             : 
     275             : 
     276           6 : Any ODateModel::translateControlValueToValidatableValue( ) const
     277             : {
     278           6 :     return getControlValue();
     279             : }
     280             : 
     281             : 
     282           0 : Any ODateModel::translateDbColumnToControlValue()
     283             : {
     284           0 :     util::Date aDate = m_xColumn->getDate();
     285           0 :     if (m_xColumn->wasNull())
     286           0 :         m_aSaveValue.clear();
     287             :     else
     288           0 :         m_aSaveValue <<= aDate;
     289             : 
     290           0 :     return m_aSaveValue;
     291             : }
     292             : 
     293             : 
     294           5 : Any ODateModel::getDefaultForReset() const
     295             : {
     296           5 :     return m_aDefault;
     297             : }
     298             : 
     299             : 
     300           5 : void ODateModel::resetNoBroadcast()
     301             : {
     302           5 :     OEditBaseModel::resetNoBroadcast();
     303           5 :     m_aSaveValue.clear();
     304           5 : }
     305             : 
     306             : 
     307           4 : Sequence< Type > ODateModel::getSupportedBindingTypes()
     308             : {
     309           4 :     return Sequence< Type >( & cppu::UnoType<util::Date>::get(), 1 );
     310             : }
     311             : 
     312             : }   // namespace frm
     313             : 
     314             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     315          35 : com_sun_star_form_ODateModel_get_implementation(::com::sun::star::uno::XComponentContext* component,
     316             :         ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     317             : {
     318          35 :     return cppu::acquire(new frm::ODateModel(component));
     319             : }
     320             : 
     321             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     322          14 : com_sun_star_form_ODateControl_get_implementation(::com::sun::star::uno::XComponentContext* component,
     323             :         ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     324             : {
     325          14 :     return cppu::acquire(new frm::ODateControl(component));
     326             : }
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11