bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / Time.cxx
blobe09359a36a2dc724a912ac43fae9640333e07169
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 <tools/time.hxx>
22 #include <connectivity/dbconversion.hxx>
23 #include <com/sun/star/sdbc/DataType.hpp>
24 #include <comphelper/processfactory.hxx>
26 using namespace dbtools;
28 namespace frm
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::sdb;
34 using namespace ::com::sun::star::sdbc;
35 using namespace ::com::sun::star::sdbcx;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::form;
39 using namespace ::com::sun::star::util;
40 using namespace ::com::sun::star::awt;
41 using namespace ::com::sun::star::io;
42 using namespace ::com::sun::star::lang;
45 //=
47 OTimeControl::OTimeControl(const Reference<XComponentContext>& _rxFactory)
48 :OBoundControl(_rxFactory, VCL_CONTROL_TIMEFIELD)
53 Sequence<Type> OTimeControl::_getTypes()
55 return OBoundControl::_getTypes();
59 StringSequence SAL_CALL OTimeControl::getSupportedServiceNames() throw(std::exception)
61 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
62 aSupported.realloc(aSupported.getLength() + 2);
64 OUString*pArray = aSupported.getArray();
65 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_TIMEFIELD;
66 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_TIMEFIELD;
67 return aSupported;
71 //= OTimeModel
73 // XServiceInfo
75 StringSequence SAL_CALL OTimeModel::getSupportedServiceNames() throw(std::exception)
77 StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
79 sal_Int32 nOldLen = aSupported.getLength();
80 aSupported.realloc( nOldLen + 9 );
81 OUString* pStoreTo = aSupported.getArray() + nOldLen;
83 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
84 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
85 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
87 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
88 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
90 *pStoreTo++ = FRM_SUN_COMPONENT_TIMEFIELD;
91 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TIMEFIELD;
92 *pStoreTo++ = BINDABLE_DATABASE_TIME_FIELD;
94 *pStoreTo++ = FRM_COMPONENT_TIMEFIELD;
96 return aSupported;
100 Sequence<Type> OTimeModel::_getTypes()
102 return OBoundControlModel::_getTypes();
107 OTimeModel::OTimeModel(const Reference<XComponentContext>& _rxFactory)
108 : OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_TIMEFIELD,
109 FRM_SUN_CONTROL_TIMEFIELD, true, true)
110 // use the old control name for compatibility reasons
111 , OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD)
112 , m_bDateTimeField(false)
114 m_nClassId = FormComponentType::TIMEFIELD;
115 initValueProperty( PROPERTY_TIME, PROPERTY_ID_TIME );
117 setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_TIMEFORMAT));
121 OTimeModel::OTimeModel(const OTimeModel* _pOriginal, const Reference<XComponentContext>& _rxFactory)
122 : OEditBaseModel(_pOriginal, _rxFactory)
123 , OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD)
124 , m_bDateTimeField(false)
126 setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_TIMEFORMAT ) );
130 OTimeModel::~OTimeModel( )
132 setAggregateSet(Reference< XFastPropertySet >(), -1);
135 // XCloneable
137 IMPLEMENT_DEFAULT_CLONING( OTimeModel )
140 OUString SAL_CALL OTimeModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
142 return OUString(FRM_COMPONENT_TIMEFIELD); // old (non-sun) name for compatibility !
145 // XPropertySet
147 void OTimeModel::describeFixedProperties( Sequence< Property >& _rProps ) const
149 BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
150 DECL_PROP3(DEFAULT_TIME, util::Time, BOUND, MAYBEDEFAULT, MAYBEVOID);
151 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
152 DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
153 DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
154 END_DESCRIBE_PROPERTIES();
158 void SAL_CALL OTimeModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
160 switch (_nHandle)
162 case PROPERTY_ID_FORMATKEY:
163 getFormatKeyPropertyValue(_rValue);
164 break;
165 case PROPERTY_ID_FORMATSSUPPLIER:
166 _rValue <<= getFormatsSupplier();
167 break;
168 default:
169 OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
170 break;
175 sal_Bool SAL_CALL OTimeModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
176 sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
178 if (PROPERTY_ID_FORMATKEY == _nHandle)
179 return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
180 else
181 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
185 void SAL_CALL OTimeModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception, std::exception)
187 if (PROPERTY_ID_FORMATKEY == _nHandle)
188 setFormatKeyPropertyValue(_rValue);
189 else
190 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
193 // XLoadListener
195 void OTimeModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
197 OBoundControlModel::onConnectedDbColumn( _rxForm );
198 Reference<XPropertySet> xField = getField();
199 if (xField.is())
201 m_bDateTimeField = false;
204 sal_Int32 nFieldType = 0;
205 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
206 m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
208 catch(const Exception&)
215 bool OTimeModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
217 Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
218 if ( !compare( aControlValue, m_aSaveValue ) )
220 if ( !aControlValue.hasValue() )
221 m_xColumnUpdate->updateNull();
222 else
226 util::Time aTime;
227 if ( !( aControlValue >>= aTime ) )
229 sal_Int64 nAsInt(0);
230 aControlValue >>= nAsInt;
231 aTime = DBTypeConversion::toTime(nAsInt);
234 if (!m_bDateTimeField)
235 m_xColumnUpdate->updateTime(aTime);
236 else
238 util::DateTime aDateTime = m_xColumn->getTimestamp();
239 aDateTime.NanoSeconds = aTime.NanoSeconds;
240 aDateTime.Seconds = aTime.Seconds;
241 aDateTime.Minutes = aTime.Minutes;
242 aDateTime.Hours = aTime.Hours;
243 m_xColumnUpdate->updateTimestamp(aDateTime);
246 catch(const Exception&)
248 return false;
251 m_aSaveValue = aControlValue;
253 return true;
257 Any OTimeModel::translateControlValueToExternalValue( ) const
259 return getControlValue();
263 Any OTimeModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
265 return _rExternalValue;
269 Any OTimeModel::translateControlValueToValidatableValue( ) const
271 return getControlValue();
275 Any OTimeModel::translateDbColumnToControlValue()
277 util::Time aTime = m_xColumn->getTime();
278 if ( m_xColumn->wasNull() )
279 m_aSaveValue.clear();
280 else
281 m_aSaveValue <<= aTime;
283 return m_aSaveValue;
287 Any OTimeModel::getDefaultForReset() const
289 return m_aDefault;
293 void OTimeModel::resetNoBroadcast()
295 OEditBaseModel::resetNoBroadcast();
296 m_aSaveValue.clear();
300 Sequence< Type > OTimeModel::getSupportedBindingTypes()
302 return Sequence< Type >( & cppu::UnoType<util::Time>::get(), 1 );
305 } // namespace frm
307 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
308 com_sun_star_form_OTimeModel_get_implementation(::com::sun::star::uno::XComponentContext* component,
309 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
311 return cppu::acquire(new frm::OTimeModel(component));
314 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
315 com_sun_star_form_OTimeControl_get_implementation(::com::sun::star::uno::XComponentContext* component,
316 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
318 return cppu::acquire(new frm::OTimeControl(component));
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */