tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / forms / source / component / Time.cxx
blobc6c590334fabde1766ae5ddb038ac74c00a106c7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "Time.hxx"
21 #include <property.hxx>
22 #include <services.hxx>
23 #include <connectivity/dbconversion.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/sdbc/DataType.hpp>
27 #include <com/sun/star/util/DateTime.hpp>
28 #include <com/sun/star/form/FormComponentType.hpp>
30 using namespace dbtools;
32 namespace frm
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::sdb;
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::form;
41 using namespace ::com::sun::star::util;
44 //=
46 OTimeControl::OTimeControl(const Reference<XComponentContext>& _rxFactory)
47 :OBoundControl(_rxFactory, VCL_CONTROL_TIMEFIELD)
52 Sequence<Type> OTimeControl::_getTypes()
54 return OBoundControl::_getTypes();
58 css::uno::Sequence<OUString> SAL_CALL OTimeControl::getSupportedServiceNames()
60 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
61 aSupported.realloc(aSupported.getLength() + 2);
63 OUString*pArray = aSupported.getArray();
64 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_TIMEFIELD;
65 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_TIMEFIELD;
66 return aSupported;
70 //= OTimeModel
72 // XServiceInfo
74 css::uno::Sequence<OUString> SAL_CALL OTimeModel::getSupportedServiceNames()
76 css::uno::Sequence<OUString> aSupported = OBoundControlModel::getSupportedServiceNames();
78 sal_Int32 nOldLen = aSupported.getLength();
79 aSupported.realloc( nOldLen + 9 );
80 OUString* pStoreTo = aSupported.getArray() + nOldLen;
82 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
83 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
84 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
86 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
87 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
89 *pStoreTo++ = FRM_SUN_COMPONENT_TIMEFIELD;
90 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TIMEFIELD;
91 *pStoreTo++ = BINDABLE_DATABASE_TIME_FIELD;
93 *pStoreTo++ = FRM_COMPONENT_TIMEFIELD;
95 return aSupported;
99 Sequence<Type> OTimeModel::_getTypes()
101 return OBoundControlModel::_getTypes();
105 OTimeModel::OTimeModel(const Reference<XComponentContext>& _rxFactory)
106 : OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_TIMEFIELD,
107 FRM_SUN_CONTROL_TIMEFIELD, true, true)
108 // use the old control name for compatibility reasons
109 , OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD)
110 , m_bDateTimeField(false)
112 m_nClassId = FormComponentType::TIMEFIELD;
113 initValueProperty( PROPERTY_TIME, PROPERTY_ID_TIME );
115 setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_TIMEFORMAT));
119 OTimeModel::OTimeModel(const OTimeModel* _pOriginal, const Reference<XComponentContext>& _rxFactory)
120 : OEditBaseModel(_pOriginal, _rxFactory)
121 , OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD)
122 , m_bDateTimeField(false)
124 setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_TIMEFORMAT ) );
128 OTimeModel::~OTimeModel( )
130 setAggregateSet(Reference< XFastPropertySet >(), -1);
133 // XCloneable
135 css::uno::Reference< css::util::XCloneable > SAL_CALL OTimeModel::createClone()
137 rtl::Reference<OTimeModel> pClone = new OTimeModel(this, getContext());
138 pClone->clonedFrom(this);
139 return pClone;
143 OUString SAL_CALL OTimeModel::getServiceName()
145 return FRM_COMPONENT_TIMEFIELD; // old (non-sun) name for compatibility !
148 // XPropertySet
150 void OTimeModel::describeFixedProperties( Sequence< Property >& _rProps ) const
152 OEditBaseModel::describeFixedProperties( _rProps );
153 sal_Int32 nOldCount = _rProps.getLength();
154 _rProps.realloc( nOldCount + 4);
155 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
156 *pProperties++ = css::beans::Property(PROPERTY_DEFAULT_TIME, PROPERTY_ID_DEFAULT_TIME, cppu::UnoType<util::Time>::get(), css::beans::PropertyAttribute::BOUND | css::beans::PropertyAttribute::MAYBEDEFAULT | css::beans::PropertyAttribute::MAYBEVOID);
157 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
158 *pProperties++ = css::beans::Property(PROPERTY_FORMATKEY, PROPERTY_ID_FORMATKEY, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::TRANSIENT);
159 *pProperties++ = css::beans::Property(PROPERTY_FORMATSSUPPLIER, PROPERTY_ID_FORMATSSUPPLIER, cppu::UnoType<XNumberFormatsSupplier>::get(),
160 css::beans::PropertyAttribute::READONLY | css::beans::PropertyAttribute::TRANSIENT);
161 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
165 void SAL_CALL OTimeModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
167 switch (_nHandle)
169 case PROPERTY_ID_FORMATKEY:
170 getFormatKeyPropertyValue(_rValue);
171 break;
172 case PROPERTY_ID_FORMATSSUPPLIER:
173 _rValue <<= getFormatsSupplier();
174 break;
175 default:
176 OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
177 break;
182 sal_Bool SAL_CALL OTimeModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
183 sal_Int32 _nHandle, const Any& _rValue )
185 if (PROPERTY_ID_FORMATKEY == _nHandle)
186 return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
187 else
188 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
192 void SAL_CALL OTimeModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue)
194 if (PROPERTY_ID_FORMATKEY == _nHandle)
195 setFormatKeyPropertyValue(_rValue);
196 else
197 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
200 // XLoadListener
202 void OTimeModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
204 OBoundControlModel::onConnectedDbColumn( _rxForm );
205 Reference<XPropertySet> xField = getField();
206 if (!xField.is())
207 return;
209 m_bDateTimeField = false;
212 sal_Int32 nFieldType = 0;
213 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
214 m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
216 catch(const Exception&)
222 bool OTimeModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
224 Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
225 if ( aControlValue == m_aSaveValue )
226 return true;
228 if ( !aControlValue.hasValue() )
229 m_xColumnUpdate->updateNull();
230 else
234 util::Time aTime;
235 if ( !( aControlValue >>= aTime ) )
237 sal_Int64 nAsInt(0);
238 aControlValue >>= nAsInt;
239 aTime = DBTypeConversion::toTime(nAsInt);
242 if (!m_bDateTimeField)
243 m_xColumnUpdate->updateTime(aTime);
244 else
246 util::DateTime aDateTime = m_xColumn->getTimestamp();
247 if (aDateTime.Year == 0 && aDateTime.Month == 0 && aDateTime.Day == 0)
248 aDateTime = css::util::DateTime(0,0,0,0,30,12,1899, false);
249 aDateTime.NanoSeconds = aTime.NanoSeconds;
250 aDateTime.Seconds = aTime.Seconds;
251 aDateTime.Minutes = aTime.Minutes;
252 aDateTime.Hours = aTime.Hours;
253 m_xColumnUpdate->updateTimestamp(aDateTime);
256 catch(const Exception&)
258 return false;
261 m_aSaveValue = std::move(aControlValue);
262 return true;
266 Any OTimeModel::translateControlValueToExternalValue( ) const
268 return getControlValue();
272 Any OTimeModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
274 return _rExternalValue;
278 Any OTimeModel::translateControlValueToValidatableValue( ) const
280 return getControlValue();
284 Any OTimeModel::translateDbColumnToControlValue()
286 util::Time aTime = m_xColumn->getTime();
287 if ( m_xColumn->wasNull() )
288 m_aSaveValue.clear();
289 else
290 m_aSaveValue <<= aTime;
292 return m_aSaveValue;
296 Any OTimeModel::getDefaultForReset() const
298 return m_aDefault;
302 void OTimeModel::resetNoBroadcast()
304 OEditBaseModel::resetNoBroadcast();
305 m_aSaveValue.clear();
309 Sequence< Type > OTimeModel::getSupportedBindingTypes()
311 return Sequence< Type >( & cppu::UnoType<util::Time>::get(), 1 );
314 } // namespace frm
316 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
317 com_sun_star_form_OTimeModel_get_implementation(css::uno::XComponentContext* component,
318 css::uno::Sequence<css::uno::Any> const &)
320 return cppu::acquire(new frm::OTimeModel(component));
323 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
324 com_sun_star_form_OTimeControl_get_implementation(css::uno::XComponentContext* component,
325 css::uno::Sequence<css::uno::Any> const &)
327 return cppu::acquire(new frm::OTimeControl(component));
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */