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