LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / forms / source / component / CheckBox.cxx
blob7718b7d74504116d0a3c175ae3af90c411141fc9
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 "CheckBox.hxx"
21 #include <property.hxx>
22 #include <services.hxx>
23 #include <comphelper/basicio.hxx>
24 #include <com/sun/star/form/FormComponentType.hpp>
26 namespace frm
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::sdb;
30 using namespace ::com::sun::star::sdbc;
31 using namespace ::com::sun::star::beans;
32 using namespace ::com::sun::star::container;
33 using namespace ::com::sun::star::form;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::io;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::form::binding;
40 OCheckBoxControl::OCheckBoxControl(const Reference<XComponentContext>& _rxFactory)
41 :OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
46 css::uno::Sequence<OUString> SAL_CALL OCheckBoxControl::getSupportedServiceNames()
48 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
49 aSupported.realloc(aSupported.getLength() + 2);
51 OUString* pArray = aSupported.getArray();
52 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_CHECKBOX;
53 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_CHECKBOX;
54 return aSupported;
58 //= OCheckBoxModel
60 OCheckBoxModel::OCheckBoxModel(const Reference<XComponentContext>& _rxFactory)
61 :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX )
62 // use the old control name for compytibility reasons
65 m_nClassId = FormComponentType::CHECKBOX;
66 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
70 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
71 :OReferenceValueComponent( _pOriginal, _rxFactory )
76 OCheckBoxModel::~OCheckBoxModel()
81 css::uno::Reference< css::util::XCloneable > SAL_CALL OCheckBoxModel::createClone()
83 rtl::Reference<OCheckBoxModel> pClone = new OCheckBoxModel(this, getContext());
84 pClone->clonedFrom(this);
85 return pClone;
89 // XServiceInfo
91 css::uno::Sequence<OUString> SAL_CALL OCheckBoxModel::getSupportedServiceNames()
93 css::uno::Sequence<OUString> aSupported = OReferenceValueComponent::getSupportedServiceNames();
95 sal_Int32 nOldLen = aSupported.getLength();
96 aSupported.realloc( nOldLen + 9 );
97 OUString* pStoreTo = aSupported.getArray() + nOldLen;
99 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
100 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
101 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
103 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
104 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
106 *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
107 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
108 *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
110 *pStoreTo++ = FRM_COMPONENT_CHECKBOX;
112 return aSupported;
116 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
118 OReferenceValueComponent::describeFixedProperties( _rProps );
119 sal_Int32 nOldCount = _rProps.getLength();
120 _rProps.realloc( nOldCount + 1);
121 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
122 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
123 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
127 OUString SAL_CALL OCheckBoxModel::getServiceName()
129 return FRM_COMPONENT_CHECKBOX; // old (non-sun) name for compatibility !
133 void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
135 OReferenceValueComponent::write(_rxOutStream);
137 // Version
138 _rxOutStream->writeShort(0x0003);
139 // Properties
140 _rxOutStream << getReferenceValue();
141 _rxOutStream << static_cast<sal_Int16>(getDefaultChecked());
142 writeHelpTextCompatibly(_rxOutStream);
143 // from version 0x0003 : common properties
144 writeCommonProperties(_rxOutStream);
148 void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream)
150 OReferenceValueComponent::read(_rxInStream);
151 osl::MutexGuard aGuard(m_aMutex);
153 // Version
154 sal_uInt16 nVersion = _rxInStream->readShort();
156 OUString sReferenceValue;
157 sal_Int16 nDefaultChecked( 0 );
158 switch ( nVersion )
160 case 0x0001:
161 _rxInStream >> sReferenceValue;
162 nDefaultChecked = _rxInStream->readShort();
163 break;
164 case 0x0002:
165 _rxInStream >> sReferenceValue;
166 _rxInStream >> nDefaultChecked;
167 readHelpTextCompatibly( _rxInStream );
168 break;
169 case 0x0003:
170 _rxInStream >> sReferenceValue;
171 _rxInStream >> nDefaultChecked;
172 readHelpTextCompatibly(_rxInStream);
173 readCommonProperties(_rxInStream);
174 break;
175 default:
176 OSL_FAIL("OCheckBoxModel::read : unknown version !");
177 defaultCommonProperties();
178 break;
180 setReferenceValue( sReferenceValue );
181 setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
183 // After reading in, display the default values
184 if ( !getControlSource().isEmpty() )
185 // (not if we don't have a control source - the "State" property acts like it is persistent, then
186 resetNoBroadcast();
189 bool OCheckBoxModel::DbUseBool()
191 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
195 Any OCheckBoxModel::translateDbColumnToControlValue()
197 Any aValue;
200 // Set value in ControlModel
201 bool bValue = bool(); // avoid warning
202 if(DbUseBool())
204 bValue = m_xColumn->getBoolean();
206 else
208 const OUString sVal(m_xColumn->getString());
209 if (sVal == getReferenceValue())
210 bValue = true;
211 else if (sVal == getNoCheckReferenceValue())
212 bValue = false;
213 else
214 aValue <<= static_cast<sal_Int16>(getDefaultChecked());
216 if ( m_xColumn->wasNull() )
218 bool bTriState = true;
219 if ( m_xAggregateSet.is() )
220 m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
221 aValue <<= static_cast<sal_Int16>( bTriState ? TRISTATE_INDET : getDefaultChecked() );
223 else if ( !aValue.hasValue() )
225 // Since above either bValue is initialised, either aValue.hasValue(),
226 // bValue cannot be used uninitialised here.
227 // But GCC does not see/understand that, which breaks -Werror builds,
228 // so we explicitly default-initialise it.
229 aValue <<= static_cast<sal_Int16>( bValue ? TRISTATE_TRUE : TRISTATE_FALSE );
232 return aValue;
236 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
238 OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
239 if ( m_xColumnUpdate.is() )
241 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
244 sal_Int16 nValue = TRISTATE_INDET;
245 aControlValue >>= nValue;
246 switch (nValue)
248 case TRISTATE_INDET:
249 m_xColumnUpdate->updateNull();
250 break;
251 case TRISTATE_TRUE:
252 if (DbUseBool())
253 m_xColumnUpdate->updateBoolean( true );
254 else
255 m_xColumnUpdate->updateString( getReferenceValue() );
256 break;
257 case TRISTATE_FALSE:
258 if (DbUseBool())
259 m_xColumnUpdate->updateBoolean( false );
260 else
261 m_xColumnUpdate->updateString( getNoCheckReferenceValue() );
262 break;
263 default:
264 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
267 catch(const Exception&)
269 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
272 return true;
277 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
278 com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext* component,
279 css::uno::Sequence<css::uno::Any> const &)
281 return cppu::acquire(new frm::OCheckBoxModel(component));
284 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
285 com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext* component,
286 css::uno::Sequence<css::uno::Any> const &)
288 return cppu::acquire(new frm::OCheckBoxControl(component));
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */