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 "Date.hxx"
21 : #include "services.hxx"
22 : #include <tools/date.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::util;
41 : using namespace ::com::sun::star::container;
42 : using namespace ::com::sun::star::form;
43 : using namespace ::com::sun::star::awt;
44 : using namespace ::com::sun::star::io;
45 : using namespace ::com::sun::star::lang;
46 :
47 :
48 18 : ODateControl::ODateControl(const Reference<XComponentContext>& _rxFactory)
49 18 : :OBoundControl(_rxFactory, VCL_CONTROL_DATEFIELD)
50 : {
51 18 : }
52 :
53 :
54 18 : InterfaceRef SAL_CALL ODateControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
55 : {
56 18 : return *(new ODateControl( comphelper::getComponentContext(_rxFactory) ));
57 : }
58 :
59 :
60 0 : Sequence<Type> ODateControl::_getTypes()
61 : {
62 0 : return OBoundControl::_getTypes();
63 : }
64 :
65 :
66 0 : StringSequence SAL_CALL ODateControl::getSupportedServiceNames() throw(std::exception)
67 : {
68 0 : StringSequence aSupported = OBoundControl::getSupportedServiceNames();
69 0 : aSupported.realloc(aSupported.getLength() + 1);
70 :
71 0 : OUString*pArray = aSupported.getArray();
72 0 : pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD;
73 0 : return aSupported;
74 : }
75 :
76 68 : InterfaceRef SAL_CALL ODateModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
77 : {
78 68 : return *(new ODateModel( comphelper::getComponentContext(_rxFactory) ));
79 : }
80 :
81 :
82 0 : Sequence<Type> ODateModel::_getTypes()
83 : {
84 0 : return OEditBaseModel::_getTypes();
85 : }
86 :
87 :
88 68 : ODateModel::ODateModel(const Reference<XComponentContext>& _rxFactory)
89 : : OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_DATEFIELD,
90 : FRM_SUN_CONTROL_DATEFIELD, true, true)
91 : // use the old control name for compytibility reasons
92 : , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
93 68 : , m_bDateTimeField(false)
94 : {
95 68 : m_nClassId = FormComponentType::DATEFIELD;
96 68 : initValueProperty( PROPERTY_DATE, PROPERTY_ID_DATE );
97 :
98 68 : setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
99 :
100 68 : osl_atomic_increment( &m_refCount );
101 : try
102 : {
103 68 : if ( m_xAggregateSet.is() )
104 68 : m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, makeAny(util::Date(1, 1, 1800)) );
105 : }
106 0 : catch( const Exception& )
107 : {
108 : OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
109 : }
110 68 : osl_atomic_decrement( &m_refCount );
111 68 : }
112 :
113 :
114 4 : ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
115 : : OEditBaseModel(_pOriginal, _rxFactory)
116 : , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
117 4 : , m_bDateTimeField(false)
118 : {
119 4 : setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
120 4 : }
121 :
122 :
123 204 : ODateModel::~ODateModel( )
124 : {
125 68 : setAggregateSet(Reference< XFastPropertySet >(), -1);
126 136 : }
127 :
128 : // XCloneable
129 :
130 4 : IMPLEMENT_DEFAULT_CLONING( ODateModel )
131 :
132 : // XServiceInfo
133 :
134 26 : StringSequence SAL_CALL ODateModel::getSupportedServiceNames() throw(std::exception)
135 : {
136 26 : StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
137 :
138 26 : sal_Int32 nOldLen = aSupported.getLength();
139 26 : aSupported.realloc( nOldLen + 8 );
140 26 : OUString* pStoreTo = aSupported.getArray() + nOldLen;
141 :
142 26 : *pStoreTo++ = BINDABLE_CONTROL_MODEL;
143 26 : *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
144 26 : *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
145 :
146 26 : *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
147 26 : *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
148 :
149 26 : *pStoreTo++ = FRM_SUN_COMPONENT_DATEFIELD;
150 26 : *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD;
151 26 : *pStoreTo++ = BINDABLE_DATABASE_DATE_FIELD;
152 :
153 26 : return aSupported;
154 : }
155 :
156 :
157 2 : OUString SAL_CALL ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
158 : {
159 2 : return OUString(FRM_COMPONENT_DATEFIELD); // old (non-sun) name for compatibility !
160 : }
161 :
162 : // XPropertySet
163 :
164 76 : void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
165 : {
166 76 : BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
167 76 : DECL_PROP3(DEFAULT_DATE, util::Date, BOUND, MAYBEDEFAULT, MAYBEVOID);
168 76 : DECL_PROP1(TABINDEX, sal_Int16, BOUND);
169 76 : DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
170 76 : DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
171 : END_DESCRIBE_PROPERTIES();
172 76 : }
173 :
174 :
175 10540 : void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
176 : {
177 10540 : switch (_nHandle)
178 : {
179 : case PROPERTY_ID_FORMATKEY:
180 72 : getFormatKeyPropertyValue(_rValue);
181 72 : break;
182 : case PROPERTY_ID_FORMATSSUPPLIER:
183 70 : _rValue <<= getFormatsSupplier();
184 70 : break;
185 : default:
186 10398 : OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
187 10398 : break;
188 : }
189 10540 : }
190 :
191 :
192 970 : sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
193 : sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
194 : {
195 970 : if (PROPERTY_ID_FORMATKEY == _nHandle)
196 2 : return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
197 : else
198 968 : return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
199 : }
200 :
201 :
202 206 : void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception, std::exception)
203 : {
204 206 : if (PROPERTY_ID_FORMATKEY == _nHandle)
205 0 : setFormatKeyPropertyValue(_rValue);
206 : else
207 206 : OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
208 206 : }
209 :
210 : // XLoadListener
211 :
212 0 : void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
213 : {
214 0 : OBoundControlModel::onConnectedDbColumn( _rxForm );
215 0 : Reference<XPropertySet> xField = getField();
216 0 : if (xField.is())
217 : {
218 0 : m_bDateTimeField = false;
219 : try
220 : {
221 0 : sal_Int32 nFieldType = 0;
222 0 : xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
223 0 : m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
224 : }
225 0 : catch(const Exception&)
226 : {
227 : }
228 0 : }
229 0 : }
230 :
231 :
232 0 : bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
233 : {
234 0 : Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
235 0 : if ( !compare( aControlValue, m_aSaveValue ) )
236 : {
237 0 : if ( !aControlValue.hasValue() )
238 0 : m_xColumnUpdate->updateNull();
239 : else
240 : {
241 : try
242 : {
243 0 : util::Date aDate;
244 0 : if ( !( aControlValue >>= aDate ) )
245 : {
246 0 : sal_Int32 nAsInt(0);
247 0 : aControlValue >>= nAsInt;
248 0 : aDate = DBTypeConversion::toDate(nAsInt);
249 : }
250 :
251 0 : if ( !m_bDateTimeField )
252 0 : m_xColumnUpdate->updateDate( aDate );
253 : else
254 : {
255 0 : util::DateTime aDateTime = m_xColumn->getTimestamp();
256 0 : aDateTime.Day = aDate.Day;
257 0 : aDateTime.Month = aDate.Month;
258 0 : aDateTime.Year = aDate.Year;
259 0 : m_xColumnUpdate->updateTimestamp( aDateTime );
260 : }
261 : }
262 0 : catch(const Exception&)
263 : {
264 0 : return false;
265 : }
266 : }
267 0 : m_aSaveValue = aControlValue;
268 : }
269 0 : return true;
270 : }
271 :
272 :
273 0 : Any ODateModel::translateControlValueToExternalValue( ) const
274 : {
275 0 : return getControlValue();
276 : }
277 :
278 :
279 4 : Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
280 : {
281 4 : return _rExternalValue;
282 : }
283 :
284 :
285 12 : Any ODateModel::translateControlValueToValidatableValue( ) const
286 : {
287 12 : return getControlValue();
288 : }
289 :
290 :
291 0 : Any ODateModel::translateDbColumnToControlValue()
292 : {
293 0 : util::Date aDate = m_xColumn->getDate();
294 0 : if (m_xColumn->wasNull())
295 0 : m_aSaveValue.clear();
296 : else
297 0 : m_aSaveValue <<= aDate;
298 :
299 0 : return m_aSaveValue;
300 : }
301 :
302 :
303 10 : Any ODateModel::getDefaultForReset() const
304 : {
305 10 : return m_aDefault;
306 : }
307 :
308 :
309 10 : void ODateModel::resetNoBroadcast()
310 : {
311 10 : OEditBaseModel::resetNoBroadcast();
312 10 : m_aSaveValue.clear();
313 10 : }
314 :
315 :
316 8 : Sequence< Type > ODateModel::getSupportedBindingTypes()
317 : {
318 8 : return Sequence< Type >( & cppu::UnoType<util::Date>::get(), 1 );
319 : }
320 :
321 :
322 : } // namespace frm
323 :
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|