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