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