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 "EditBase.hxx"
21 : #include "property.hxx"
22 : #include "property.hrc"
23 : #include "services.hxx"
24 : #include <tools/debug.hxx>
25 : #include <comphelper/basicio.hxx>
26 : #include <cppuhelper/queryinterface.hxx>
27 : #include "frm_resource.hxx"
28 : #include "frm_resource.hrc"
29 :
30 : //.........................................................................
31 : namespace frm
32 : {
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::sdb;
35 : using namespace ::com::sun::star::sdbc;
36 : using namespace ::com::sun::star::sdbcx;
37 : using namespace ::com::sun::star::beans;
38 : using namespace ::com::sun::star::container;
39 : using namespace ::com::sun::star::form;
40 : using namespace ::com::sun::star::awt;
41 : using namespace ::com::sun::star::io;
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::util;
44 :
45 : const sal_uInt16 DEFAULT_LONG = 0x0001;
46 : const sal_uInt16 DEFAULT_DOUBLE = 0x0002;
47 : const sal_uInt16 FILTERPROPOSAL = 0x0004;
48 :
49 : DBG_NAME( OEditBaseModel )
50 : //------------------------------------------------------------------
51 174 : OEditBaseModel::OEditBaseModel( const Reference< XComponentContext >& _rxFactory, const OUString& rUnoControlModelName,
52 : const OUString& rDefault, const sal_Bool _bSupportExternalBinding, const sal_Bool _bSupportsValidation )
53 : :OBoundControlModel( _rxFactory, rUnoControlModelName, rDefault, sal_True, _bSupportExternalBinding, _bSupportsValidation )
54 : ,m_nLastReadVersion(0)
55 : ,m_bEmptyIsNull(sal_True)
56 174 : ,m_bFilterProposal(sal_False)
57 : {
58 : DBG_CTOR( OEditBaseModel, NULL );
59 174 : }
60 :
61 : //------------------------------------------------------------------
62 8 : OEditBaseModel::OEditBaseModel( const OEditBaseModel* _pOriginal, const Reference< XComponentContext >& _rxFactory )
63 : :OBoundControlModel( _pOriginal, _rxFactory )
64 8 : ,m_nLastReadVersion(0)
65 : {
66 : DBG_CTOR( OEditBaseModel, NULL );
67 :
68 8 : m_bFilterProposal = _pOriginal->m_bFilterProposal;
69 8 : m_bEmptyIsNull = _pOriginal->m_bEmptyIsNull;
70 8 : m_aDefault = _pOriginal->m_aDefault;
71 8 : m_aDefaultText = _pOriginal->m_aDefaultText;
72 8 : }
73 :
74 : //------------------------------------------------------------------
75 172 : OEditBaseModel::~OEditBaseModel( )
76 : {
77 : DBG_DTOR( OEditBaseModel, NULL );
78 172 : }
79 :
80 : // XPersist
81 : //------------------------------------------------------------------------------
82 8 : void OEditBaseModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
83 : {
84 8 : OBoundControlModel::write(_rxOutStream);
85 :
86 : // Version
87 8 : sal_uInt16 nVersionId = 0x0005;
88 : DBG_ASSERT((getPersistenceFlags() & ~PF_SPECIAL_FLAGS) == 0,
89 : "OEditBaseModel::write : invalid special version flags !");
90 : // please don't use other flags, older versions can't interpret them !
91 :
92 8 : nVersionId |= getPersistenceFlags();
93 8 : _rxOutStream->writeShort(nVersionId);
94 :
95 : // Name
96 8 : _rxOutStream->writeShort(0); // obsolete
97 8 : _rxOutStream << m_aDefaultText;
98 :
99 : // Masking for any
100 8 : sal_uInt16 nAnyMask = 0;
101 8 : if (m_aDefault.getValueType().getTypeClass() == TypeClass_LONG)
102 0 : nAnyMask |= DEFAULT_LONG;
103 8 : else if (m_aDefault.getValueType().getTypeClass() == TypeClass_DOUBLE)
104 0 : nAnyMask |= DEFAULT_DOUBLE;
105 :
106 8 : if (m_bFilterProposal) // Don't save a value, because it's boolean
107 0 : nAnyMask |= FILTERPROPOSAL;
108 :
109 8 : _rxOutStream->writeBoolean(m_bEmptyIsNull);
110 8 : _rxOutStream->writeShort(nAnyMask);
111 :
112 8 : if ((nAnyMask & DEFAULT_LONG) == DEFAULT_LONG)
113 0 : _rxOutStream->writeLong(getINT32(m_aDefault));
114 8 : else if ((nAnyMask & DEFAULT_DOUBLE) == DEFAULT_DOUBLE)
115 0 : _rxOutStream->writeDouble(getDouble(m_aDefault));
116 :
117 : // since version 5 we write the help text
118 8 : writeHelpTextCompatibly(_rxOutStream);
119 : // (that's potentially bad : at the time I added the above line we had two derived classes : OEditModel and
120 : // OFormattedModel. The first one does not have an own version handling, so it can't write the help text itself,
121 : // the second one does it's own writing (reading) after calling our method, so normally we shouldn't write any
122 : // additional members as this is not compatible to older office versions.
123 : // We decided to place the writing of the help text here as it seems the less worse alternative. There is no delivered
124 : // office version including formatted controls (and thus the OFormattedModel), and the OFormattedModel::read seems
125 : // robust against this change (as it will read a wrong and unknown file version and thus set it's members to defaults).
126 :
127 8 : if ((nVersionId & PF_HANDLE_COMMON_PROPS) != 0)
128 8 : writeCommonEditProperties(_rxOutStream);
129 :
130 : // !!! properties common to all OEditBaseModel derived classes should be written in writeCommonEditProperties !!!
131 8 : }
132 :
133 : //------------------------------------------------------------------------------
134 8 : sal_uInt16 OEditBaseModel::getPersistenceFlags() const
135 : {
136 8 : return PF_HANDLE_COMMON_PROPS;
137 : }
138 :
139 : //------------------------------------------------------------------------------
140 8 : void OEditBaseModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
141 : {
142 8 : OBoundControlModel::read(_rxInStream);
143 8 : ::osl::MutexGuard aGuard(m_aMutex);
144 :
145 : // Version's own version number
146 8 : sal_uInt16 nVersion = _rxInStream->readShort();
147 8 : m_nLastReadVersion = nVersion;
148 :
149 8 : sal_Bool bHandleCommonProps = (nVersion & PF_HANDLE_COMMON_PROPS) != 0;
150 8 : nVersion = nVersion & ~PF_SPECIAL_FLAGS;
151 :
152 : // obsolete
153 8 : _rxInStream->readShort();
154 :
155 8 : _rxInStream >> m_aDefaultText;
156 :
157 8 : if (nVersion >= 0x0003)
158 : {
159 8 : m_bEmptyIsNull = _rxInStream->readBoolean();
160 :
161 8 : sal_uInt16 nAnyMask = _rxInStream->readShort();
162 8 : if ((nAnyMask & DEFAULT_LONG) == DEFAULT_LONG)
163 : {
164 0 : sal_Int32 nValue = _rxInStream->readLong();
165 0 : m_aDefault <<= (sal_Int32)nValue;
166 : }
167 8 : else if ((nAnyMask & DEFAULT_DOUBLE) == DEFAULT_DOUBLE)
168 : {
169 0 : double fValue = _rxInStream->readDouble();
170 0 : m_aDefault <<= (double)fValue;
171 : }
172 :
173 8 : if ((nAnyMask & FILTERPROPOSAL) == FILTERPROPOSAL)
174 0 : m_bFilterProposal = sal_True;
175 : }
176 :
177 8 : if (nVersion > 4)
178 8 : readHelpTextCompatibly(_rxInStream);
179 :
180 8 : if (bHandleCommonProps)
181 8 : readCommonEditProperties(_rxInStream);
182 :
183 : // After reading, display default values
184 8 : if ( !getControlSource().isEmpty() )
185 : // (not if we don't have a control source - the "State" property acts like it is persistent, then)
186 6 : resetNoBroadcast();
187 8 : };
188 :
189 : //------------------------------------------------------------------------------
190 0 : void OEditBaseModel::defaultCommonEditProperties()
191 : {
192 0 : OBoundControlModel::defaultCommonProperties();
193 : // no own common properties at the moment
194 0 : }
195 :
196 : //------------------------------------------------------------------------------
197 8 : void OEditBaseModel::readCommonEditProperties(const Reference<XObjectInputStream>& _rxInStream)
198 : {
199 8 : sal_Int32 nLen = _rxInStream->readLong();
200 :
201 8 : Reference<XMarkableStream> xMark(_rxInStream, UNO_QUERY);
202 : DBG_ASSERT(xMark.is(), "OBoundControlModel::readCommonProperties : can only work with markable streams !");
203 8 : sal_Int32 nMark = xMark->createMark();
204 :
205 : // read properties common to all OBoundControlModels
206 8 : OBoundControlModel::readCommonProperties(_rxInStream);
207 :
208 : // read properties common to all OEditBaseModels
209 :
210 : // skip the remaining bytes
211 8 : xMark->jumpToMark(nMark);
212 8 : _rxInStream->skipBytes(nLen);
213 8 : xMark->deleteMark(nMark);
214 8 : }
215 :
216 : //------------------------------------------------------------------------------
217 8 : void OEditBaseModel::writeCommonEditProperties(const Reference<XObjectOutputStream>& _rxOutStream)
218 : {
219 8 : Reference<XMarkableStream> xMark(_rxOutStream, UNO_QUERY);
220 : DBG_ASSERT(xMark.is(), "OEditBaseModel::writeCommonProperties : can only work with markable streams !");
221 8 : sal_Int32 nMark = xMark->createMark();
222 :
223 : // a placeholder where we will write the overall length (later in this method)
224 8 : sal_Int32 nLen = 0;
225 8 : _rxOutStream->writeLong(nLen);
226 :
227 : // write properties common to all OBoundControlModels
228 8 : OBoundControlModel::writeCommonProperties(_rxOutStream);
229 :
230 : // write properties common to all OEditBaseModels
231 :
232 : // close the block - write the correct length at the beginning
233 8 : nLen = xMark->offsetToMark(nMark) - sizeof(nLen);
234 8 : xMark->jumpToMark(nMark);
235 8 : _rxOutStream->writeLong(nLen);
236 8 : xMark->jumpToFurthest();
237 8 : xMark->deleteMark(nMark);
238 8 : }
239 :
240 : //------------------------------------------------------------------------------
241 34577 : void OEditBaseModel::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
242 : {
243 34577 : switch (nHandle)
244 : {
245 : case PROPERTY_ID_EMPTY_IS_NULL:
246 214 : rValue <<= (sal_Bool)m_bEmptyIsNull;
247 214 : break;
248 : case PROPERTY_ID_FILTERPROPOSAL:
249 210 : rValue <<= (sal_Bool)m_bFilterProposal;
250 210 : break;
251 : case PROPERTY_ID_DEFAULT_TEXT:
252 214 : rValue <<= m_aDefaultText;
253 214 : break;
254 : case PROPERTY_ID_DEFAULT_VALUE:
255 : case PROPERTY_ID_DEFAULT_DATE:
256 : case PROPERTY_ID_DEFAULT_TIME:
257 102 : rValue = m_aDefault;
258 102 : break;
259 : default:
260 33837 : OBoundControlModel::getFastPropertyValue(rValue, nHandle);
261 : }
262 34577 : }
263 :
264 : //------------------------------------------------------------------------------
265 3105 : sal_Bool OEditBaseModel::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue,
266 : sal_Int32 nHandle, const Any& rValue ) throw( IllegalArgumentException )
267 : {
268 3105 : sal_Bool bModified(sal_False);
269 3105 : switch (nHandle)
270 : {
271 : case PROPERTY_ID_EMPTY_IS_NULL:
272 102 : bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bEmptyIsNull);
273 102 : break;
274 : case PROPERTY_ID_FILTERPROPOSAL:
275 94 : bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bFilterProposal);
276 94 : break;
277 : case PROPERTY_ID_DEFAULT_TEXT:
278 99 : bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefaultText);
279 99 : break;
280 : case PROPERTY_ID_DEFAULT_VALUE:
281 0 : bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefault, ::getCppuType((const double*)0));
282 0 : break;
283 : case PROPERTY_ID_DEFAULT_DATE:
284 : case PROPERTY_ID_DEFAULT_TIME:
285 0 : bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aDefault, ::getCppuType((const sal_Int32*)0));
286 0 : break;
287 : default:
288 : bModified = OBoundControlModel::convertFastPropertyValue(
289 : rConvertedValue,
290 : rOldValue,
291 : nHandle,
292 2810 : rValue);
293 : }
294 3105 : return bModified;
295 : }
296 :
297 : //------------------------------------------------------------------------------
298 720 : void OEditBaseModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw ( ::com::sun::star::uno::Exception)
299 : {
300 720 : switch (nHandle)
301 : {
302 : case PROPERTY_ID_EMPTY_IS_NULL:
303 : DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
304 18 : m_bEmptyIsNull = getBOOL(rValue);
305 18 : break;
306 : case PROPERTY_ID_FILTERPROPOSAL:
307 : DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
308 16 : m_bFilterProposal = getBOOL(rValue);
309 16 : break;
310 : // Changing the default values causes a reset
311 : case PROPERTY_ID_DEFAULT_TEXT:
312 : DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_STRING, "invalid type" );
313 19 : rValue >>= m_aDefaultText;
314 19 : resetNoBroadcast();
315 19 : break;
316 : case PROPERTY_ID_DEFAULT_VALUE:
317 : case PROPERTY_ID_DEFAULT_DATE:
318 : case PROPERTY_ID_DEFAULT_TIME:
319 0 : m_aDefault = rValue;
320 0 : resetNoBroadcast();
321 0 : break;
322 : default:
323 667 : OBoundControlModel::setFastPropertyValue_NoBroadcast(nHandle, rValue );
324 : }
325 720 : }
326 :
327 : // XPropertyState
328 : //------------------------------------------------------------------------------
329 0 : Any OEditBaseModel::getPropertyDefaultByHandle( sal_Int32 nHandle ) const
330 : {
331 0 : switch (nHandle)
332 : {
333 : case PROPERTY_ID_DEFAULT_TEXT:
334 0 : return makeAny(OUString());
335 : case PROPERTY_ID_FILTERPROPOSAL:
336 0 : return Any(makeAny((sal_Bool)sal_False));
337 : case PROPERTY_ID_DEFAULT_VALUE:
338 : case PROPERTY_ID_DEFAULT_DATE:
339 : case PROPERTY_ID_DEFAULT_TIME:
340 0 : return Any();
341 : default:
342 0 : return OBoundControlModel::getPropertyDefaultByHandle(nHandle);
343 : }
344 : }
345 :
346 : //.........................................................................
347 : }
348 : //.........................................................................
349 :
350 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|