1*24acc546SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*24acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*24acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*24acc546SAndrew Rist * distributed with this work for additional information 6*24acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*24acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*24acc546SAndrew Rist * "License"); you may not use this file except in compliance 9*24acc546SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*24acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*24acc546SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*24acc546SAndrew Rist * software distributed under the License is distributed on an 15*24acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*24acc546SAndrew Rist * KIND, either express or implied. See the License for the 17*24acc546SAndrew Rist * specific language governing permissions and limitations 18*24acc546SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*24acc546SAndrew Rist *************************************************************/ 21*24acc546SAndrew Rist 22*24acc546SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_forms.hxx" 26cdf0e10cSrcweir #include "Time.hxx" 27cdf0e10cSrcweir #include <tools/debug.hxx> 28cdf0e10cSrcweir #include <tools/time.hxx> 29cdf0e10cSrcweir #include <connectivity/dbconversion.hxx> 30cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp> 31cdf0e10cSrcweir 32cdf0e10cSrcweir using namespace dbtools; 33cdf0e10cSrcweir 34cdf0e10cSrcweir //......................................................................... 35cdf0e10cSrcweir namespace frm 36cdf0e10cSrcweir { 37cdf0e10cSrcweir //......................................................................... 38cdf0e10cSrcweir 39cdf0e10cSrcweir using namespace ::com::sun::star; 40cdf0e10cSrcweir using namespace ::com::sun::star::uno; 41cdf0e10cSrcweir using namespace ::com::sun::star::sdb; 42cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 43cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx; 44cdf0e10cSrcweir using namespace ::com::sun::star::beans; 45cdf0e10cSrcweir using namespace ::com::sun::star::container; 46cdf0e10cSrcweir using namespace ::com::sun::star::form; 47cdf0e10cSrcweir using namespace ::com::sun::star::util; 48cdf0e10cSrcweir using namespace ::com::sun::star::awt; 49cdf0e10cSrcweir using namespace ::com::sun::star::io; 50cdf0e10cSrcweir using namespace ::com::sun::star::lang; 51cdf0e10cSrcweir 52cdf0e10cSrcweir //================================================================== 53cdf0e10cSrcweir //= 54cdf0e10cSrcweir //================================================================== 55cdf0e10cSrcweir 56cdf0e10cSrcweir //================================================================== 57cdf0e10cSrcweir //= OTimeControl 58cdf0e10cSrcweir //================================================================== 59cdf0e10cSrcweir //------------------------------------------------------------------ 60cdf0e10cSrcweir OTimeControl::OTimeControl(const Reference<XMultiServiceFactory>& _rxFactory) 61cdf0e10cSrcweir :OBoundControl(_rxFactory, VCL_CONTROL_TIMEFIELD) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir //------------------------------------------------------------------ 66cdf0e10cSrcweir InterfaceRef SAL_CALL OTimeControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir return *(new OTimeControl(_rxFactory)); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir //------------------------------------------------------------------------------ 72cdf0e10cSrcweir Sequence<Type> OTimeControl::_getTypes() 73cdf0e10cSrcweir { 74cdf0e10cSrcweir return OBoundControl::_getTypes(); 75cdf0e10cSrcweir } 76cdf0e10cSrcweir 77cdf0e10cSrcweir //------------------------------------------------------------------------------ 78cdf0e10cSrcweir StringSequence SAL_CALL OTimeControl::getSupportedServiceNames() throw() 79cdf0e10cSrcweir { 80cdf0e10cSrcweir StringSequence aSupported = OBoundControl::getSupportedServiceNames(); 81cdf0e10cSrcweir aSupported.realloc(aSupported.getLength() + 1); 82cdf0e10cSrcweir 83cdf0e10cSrcweir ::rtl::OUString*pArray = aSupported.getArray(); 84cdf0e10cSrcweir pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_TIMEFIELD; 85cdf0e10cSrcweir return aSupported; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir //================================================================== 89cdf0e10cSrcweir //= OTimeModel 90cdf0e10cSrcweir //================================================================== 91cdf0e10cSrcweir //------------------------------------------------------------------ 92cdf0e10cSrcweir InterfaceRef SAL_CALL OTimeModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir return *(new OTimeModel(_rxFactory)); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir // XServiceInfo 98cdf0e10cSrcweir //------------------------------------------------------------------------------ 99cdf0e10cSrcweir StringSequence SAL_CALL OTimeModel::getSupportedServiceNames() throw() 100cdf0e10cSrcweir { 101cdf0e10cSrcweir StringSequence aSupported = OBoundControlModel::getSupportedServiceNames(); 102cdf0e10cSrcweir 103cdf0e10cSrcweir sal_Int32 nOldLen = aSupported.getLength(); 104cdf0e10cSrcweir aSupported.realloc( nOldLen + 8 ); 105cdf0e10cSrcweir ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen; 106cdf0e10cSrcweir 107cdf0e10cSrcweir *pStoreTo++ = BINDABLE_CONTROL_MODEL; 108cdf0e10cSrcweir *pStoreTo++ = DATA_AWARE_CONTROL_MODEL; 109cdf0e10cSrcweir *pStoreTo++ = VALIDATABLE_CONTROL_MODEL; 110cdf0e10cSrcweir 111cdf0e10cSrcweir *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL; 112cdf0e10cSrcweir *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL; 113cdf0e10cSrcweir 114cdf0e10cSrcweir *pStoreTo++ = FRM_SUN_COMPONENT_TIMEFIELD; 115cdf0e10cSrcweir *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TIMEFIELD; 116cdf0e10cSrcweir *pStoreTo++ = BINDABLE_DATABASE_TIME_FIELD; 117cdf0e10cSrcweir 118cdf0e10cSrcweir return aSupported; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir //------------------------------------------------------------------------------ 122cdf0e10cSrcweir Sequence<Type> OTimeModel::_getTypes() 123cdf0e10cSrcweir { 124cdf0e10cSrcweir return OBoundControlModel::_getTypes(); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir //------------------------------------------------------------------ 128cdf0e10cSrcweir DBG_NAME( OTimeModel ) 129cdf0e10cSrcweir //------------------------------------------------------------------ 130cdf0e10cSrcweir OTimeModel::OTimeModel(const Reference<XMultiServiceFactory>& _rxFactory) 131cdf0e10cSrcweir :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_TIMEFIELD, FRM_SUN_CONTROL_TIMEFIELD, sal_True, sal_True ) 132cdf0e10cSrcweir // use the old control name for compytibility reasons 133cdf0e10cSrcweir ,OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir DBG_CTOR( OTimeModel, NULL ); 136cdf0e10cSrcweir 137cdf0e10cSrcweir m_nClassId = FormComponentType::TIMEFIELD; 138cdf0e10cSrcweir initValueProperty( PROPERTY_TIME, PROPERTY_ID_TIME ); 139cdf0e10cSrcweir 140cdf0e10cSrcweir setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_TIMEFORMAT)); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir //------------------------------------------------------------------------------ 144cdf0e10cSrcweir OTimeModel::OTimeModel( const OTimeModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory ) 145cdf0e10cSrcweir :OEditBaseModel( _pOriginal, _rxFactory ) 146cdf0e10cSrcweir ,OLimitedFormats( _rxFactory, FormComponentType::TIMEFIELD ) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir DBG_CTOR( OTimeModel, NULL ); 149cdf0e10cSrcweir 150cdf0e10cSrcweir setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_TIMEFORMAT ) ); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir //------------------------------------------------------------------------------ 154cdf0e10cSrcweir OTimeModel::~OTimeModel( ) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir setAggregateSet(Reference< XFastPropertySet >(), -1); 157cdf0e10cSrcweir DBG_DTOR( OTimeModel, NULL ); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir // XCloneable 161cdf0e10cSrcweir //------------------------------------------------------------------------------ 162cdf0e10cSrcweir IMPLEMENT_DEFAULT_CLONING( OTimeModel ) 163cdf0e10cSrcweir 164cdf0e10cSrcweir //------------------------------------------------------------------------------ 165cdf0e10cSrcweir ::rtl::OUString SAL_CALL OTimeModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir return FRM_COMPONENT_TIMEFIELD; // old (non-sun) name for compatibility ! 168cdf0e10cSrcweir } 169cdf0e10cSrcweir 170cdf0e10cSrcweir // XPropertySet 171cdf0e10cSrcweir //------------------------------------------------------------------------------ 172cdf0e10cSrcweir void OTimeModel::describeFixedProperties( Sequence< Property >& _rProps ) const 173cdf0e10cSrcweir { 174cdf0e10cSrcweir BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel ) 175cdf0e10cSrcweir DECL_PROP3(DEFAULT_TIME, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID); 176cdf0e10cSrcweir DECL_PROP1(TABINDEX, sal_Int16, BOUND); 177cdf0e10cSrcweir DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT); 178cdf0e10cSrcweir DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT); 179cdf0e10cSrcweir END_DESCRIBE_PROPERTIES(); 180cdf0e10cSrcweir } 181cdf0e10cSrcweir 182cdf0e10cSrcweir //------------------------------------------------------------------------------ 183cdf0e10cSrcweir void SAL_CALL OTimeModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const 184cdf0e10cSrcweir { 185cdf0e10cSrcweir switch (_nHandle) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir case PROPERTY_ID_FORMATKEY: 188cdf0e10cSrcweir getFormatKeyPropertyValue(_rValue); 189cdf0e10cSrcweir break; 190cdf0e10cSrcweir case PROPERTY_ID_FORMATSSUPPLIER: 191cdf0e10cSrcweir _rValue <<= getFormatsSupplier(); 192cdf0e10cSrcweir break; 193cdf0e10cSrcweir default: 194cdf0e10cSrcweir OEditBaseModel::getFastPropertyValue(_rValue, _nHandle); 195cdf0e10cSrcweir break; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir //------------------------------------------------------------------------------ 200cdf0e10cSrcweir sal_Bool SAL_CALL OTimeModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue, 201cdf0e10cSrcweir sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir if (PROPERTY_ID_FORMATKEY == _nHandle) 204cdf0e10cSrcweir return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue); 205cdf0e10cSrcweir else 206cdf0e10cSrcweir return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue ); 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir //------------------------------------------------------------------------------ 210cdf0e10cSrcweir void SAL_CALL OTimeModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception) 211cdf0e10cSrcweir { 212cdf0e10cSrcweir if (PROPERTY_ID_FORMATKEY == _nHandle) 213cdf0e10cSrcweir setFormatKeyPropertyValue(_rValue); 214cdf0e10cSrcweir else 215cdf0e10cSrcweir OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir 218cdf0e10cSrcweir // XLoadListener 219cdf0e10cSrcweir //------------------------------------------------------------------------------ 220cdf0e10cSrcweir void OTimeModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm ) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir OBoundControlModel::onConnectedDbColumn( _rxForm ); 223cdf0e10cSrcweir Reference<XPropertySet> xField = getField(); 224cdf0e10cSrcweir if (xField.is()) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir m_bDateTimeField = sal_False; 227cdf0e10cSrcweir try 228cdf0e10cSrcweir { 229cdf0e10cSrcweir sal_Int32 nFieldType = 0; 230cdf0e10cSrcweir xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType; 231cdf0e10cSrcweir m_bDateTimeField = (nFieldType == DataType::TIMESTAMP); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir catch(Exception&) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir } 236cdf0e10cSrcweir } 237cdf0e10cSrcweir } 238cdf0e10cSrcweir 239cdf0e10cSrcweir //------------------------------------------------------------------------------ 240cdf0e10cSrcweir sal_Bool OTimeModel::commitControlValueToDbColumn( bool /*_bPostReset*/ ) 241cdf0e10cSrcweir { 242cdf0e10cSrcweir Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) ); 243cdf0e10cSrcweir if ( !compare( aControlValue, m_aSaveValue ) ) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir if ( !aControlValue.hasValue() ) 246cdf0e10cSrcweir m_xColumnUpdate->updateNull(); 247cdf0e10cSrcweir else 248cdf0e10cSrcweir { 249cdf0e10cSrcweir try 250cdf0e10cSrcweir { 251cdf0e10cSrcweir util::Time aTime; 252cdf0e10cSrcweir if ( !( aControlValue >>= aTime ) ) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir sal_Int32 nAsInt(0); 255cdf0e10cSrcweir aControlValue >>= nAsInt; 256cdf0e10cSrcweir aTime = DBTypeConversion::toTime(nAsInt); 257cdf0e10cSrcweir } 258cdf0e10cSrcweir 259cdf0e10cSrcweir if (!m_bDateTimeField) 260cdf0e10cSrcweir m_xColumnUpdate->updateTime(aTime); 261cdf0e10cSrcweir else 262cdf0e10cSrcweir { 263cdf0e10cSrcweir util::DateTime aDateTime = m_xColumn->getTimestamp(); 264cdf0e10cSrcweir aDateTime.HundredthSeconds = aTime.HundredthSeconds; 265cdf0e10cSrcweir aDateTime.Seconds = aTime.Seconds; 266cdf0e10cSrcweir aDateTime.Minutes = aTime.Minutes; 267cdf0e10cSrcweir aDateTime.Hours = aTime.Hours; 268cdf0e10cSrcweir m_xColumnUpdate->updateTimestamp(aDateTime); 269cdf0e10cSrcweir } 270cdf0e10cSrcweir } 271cdf0e10cSrcweir catch(Exception&) 272cdf0e10cSrcweir { 273cdf0e10cSrcweir return sal_False; 274cdf0e10cSrcweir } 275cdf0e10cSrcweir } 276cdf0e10cSrcweir m_aSaveValue = aControlValue; 277cdf0e10cSrcweir } 278cdf0e10cSrcweir return sal_True; 279cdf0e10cSrcweir } 280cdf0e10cSrcweir 281cdf0e10cSrcweir //------------------------------------------------------------------------------ 282cdf0e10cSrcweir void OTimeModel::impl_translateControlValueToUNOTime( Any& _rUNOValue ) const 283cdf0e10cSrcweir { 284cdf0e10cSrcweir _rUNOValue = getControlValue(); 285cdf0e10cSrcweir if ( _rUNOValue.hasValue() ) 286cdf0e10cSrcweir { 287cdf0e10cSrcweir sal_Int32 nTime = 0; 288cdf0e10cSrcweir OSL_VERIFY( _rUNOValue >>= nTime ); 289cdf0e10cSrcweir if ( nTime == ::Time( 99, 99, 99 ).GetTime() ) 290cdf0e10cSrcweir // "invalid time" in VCL is different from "invalid time" in UNO 291cdf0e10cSrcweir _rUNOValue.clear(); 292cdf0e10cSrcweir else 293cdf0e10cSrcweir _rUNOValue <<= DBTypeConversion::toTime( nTime ); 294cdf0e10cSrcweir } 295cdf0e10cSrcweir } 296cdf0e10cSrcweir 297cdf0e10cSrcweir //------------------------------------------------------------------------------ 298cdf0e10cSrcweir Any OTimeModel::translateControlValueToExternalValue( ) const 299cdf0e10cSrcweir { 300cdf0e10cSrcweir Any aExternalValue; 301cdf0e10cSrcweir impl_translateControlValueToUNOTime( aExternalValue ); 302cdf0e10cSrcweir return aExternalValue; 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir //------------------------------------------------------------------------------ 306cdf0e10cSrcweir Any OTimeModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const 307cdf0e10cSrcweir { 308cdf0e10cSrcweir Any aControlValue; 309cdf0e10cSrcweir if ( _rExternalValue.hasValue() ) 310cdf0e10cSrcweir { 311cdf0e10cSrcweir util::Time aTime; 312cdf0e10cSrcweir OSL_VERIFY( _rExternalValue >>= aTime ); 313cdf0e10cSrcweir aControlValue <<= DBTypeConversion::toINT32( aTime ); 314cdf0e10cSrcweir } 315cdf0e10cSrcweir return aControlValue; 316cdf0e10cSrcweir } 317cdf0e10cSrcweir 318cdf0e10cSrcweir //------------------------------------------------------------------------------ 319cdf0e10cSrcweir Any OTimeModel::translateControlValueToValidatableValue( ) const 320cdf0e10cSrcweir { 321cdf0e10cSrcweir Any aValidatableValue; 322cdf0e10cSrcweir impl_translateControlValueToUNOTime( aValidatableValue ); 323cdf0e10cSrcweir return aValidatableValue; 324cdf0e10cSrcweir } 325cdf0e10cSrcweir 326cdf0e10cSrcweir //------------------------------------------------------------------------------ 327cdf0e10cSrcweir Any OTimeModel::translateDbColumnToControlValue() 328cdf0e10cSrcweir { 329cdf0e10cSrcweir util::Time aTime = m_xColumn->getTime(); 330cdf0e10cSrcweir if ( m_xColumn->wasNull() ) 331cdf0e10cSrcweir m_aSaveValue.clear(); 332cdf0e10cSrcweir else 333cdf0e10cSrcweir // the aggregated set expects an Int32 as value ... 334cdf0e10cSrcweir m_aSaveValue <<= DBTypeConversion::toINT32( aTime ); 335cdf0e10cSrcweir 336cdf0e10cSrcweir return m_aSaveValue; 337cdf0e10cSrcweir } 338cdf0e10cSrcweir 339cdf0e10cSrcweir //------------------------------------------------------------------------------ 340cdf0e10cSrcweir Any OTimeModel::getDefaultForReset() const 341cdf0e10cSrcweir { 342cdf0e10cSrcweir return m_aDefault; 343cdf0e10cSrcweir } 344cdf0e10cSrcweir 345cdf0e10cSrcweir //------------------------------------------------------------------------------ 346cdf0e10cSrcweir void OTimeModel::resetNoBroadcast() 347cdf0e10cSrcweir { 348cdf0e10cSrcweir OEditBaseModel::resetNoBroadcast(); 349cdf0e10cSrcweir m_aSaveValue.clear(); 350cdf0e10cSrcweir } 351cdf0e10cSrcweir 352cdf0e10cSrcweir //------------------------------------------------------------------------------ 353cdf0e10cSrcweir Sequence< Type > OTimeModel::getSupportedBindingTypes() 354cdf0e10cSrcweir { 355cdf0e10cSrcweir return Sequence< Type >( &::getCppuType( static_cast< util::Time* >( NULL ) ), 1 ); 356cdf0e10cSrcweir } 357cdf0e10cSrcweir 358cdf0e10cSrcweir //......................................................................... 359cdf0e10cSrcweir } // namespace frm 360cdf0e10cSrcweir //......................................................................... 361cdf0e10cSrcweir 362