Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / forms / source / misc / limitedformats.cxx
blobb7a0d5abcbd6fa83c505c555b7532610672f7c78
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 <limitedformats.hxx>
21 #include <osl/diagnose.h>
22 #include <comphelper/types.hxx>
23 #include <comphelper/extract.hxx>
24 #include <com/sun/star/form/FormComponentType.hpp>
25 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
28 namespace frm
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::util;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::form;
36 using namespace ::com::sun::star::beans;
38 sal_Int32 OLimitedFormats::s_nInstanceCount(0);
39 ::osl::Mutex OLimitedFormats::s_aMutex;
40 Reference< XNumberFormatsSupplier > OLimitedFormats::s_xStandardFormats;
43 //=
45 namespace {
47 enum LocaleType
49 ltEnglishUS,
50 ltGerman,
51 ltSystem
56 static const Locale& getLocale(LocaleType _eType)
58 static const Locale s_aEnglishUS( "en", "us", OUString() );
59 static const Locale s_aGerman( "de", "DE", OUString() );
60 static const Locale s_aSystem( "", "", "" );
62 switch (_eType)
64 case ltEnglishUS:
65 return s_aEnglishUS;
67 case ltGerman:
68 return s_aGerman;
70 case ltSystem:
71 return s_aSystem;
74 OSL_FAIL("getLocale: invalid enum value!");
75 return s_aSystem;
78 namespace {
80 struct FormatEntry
82 const char* pDescription;
83 sal_Int32 nKey;
84 LocaleType eLocale;
89 static FormatEntry* lcl_getFormatTable(sal_Int16 nTableId)
91 switch (nTableId)
93 case FormComponentType::TIMEFIELD:
95 static FormatEntry s_aFormats[] = {
96 { "HH:MM", -1, ltEnglishUS },
97 { "HH:MM:SS", -1, ltEnglishUS },
98 { "HH:MM AM/PM", -1, ltEnglishUS },
99 { "HH:MM:SS AM/PM", -1, ltEnglishUS },
100 { nullptr, -1, ltSystem }
102 return s_aFormats;
104 case FormComponentType::DATEFIELD:
106 static FormatEntry s_aFormats[] = {
107 { "T-M-JJ", -1, ltGerman },
108 { "TT-MM-JJ", -1, ltGerman },
109 { "TT-MM-JJJJ", -1, ltGerman },
110 { "NNNNT. MMMM JJJJ", -1, ltGerman },
112 { "DD/MM/YY", -1, ltEnglishUS },
113 { "MM/DD/YY", -1, ltEnglishUS },
114 { "YY/MM/DD", -1, ltEnglishUS },
115 { "DD/MM/YYYY", -1, ltEnglishUS },
116 { "MM/DD/YYYY", -1, ltEnglishUS },
117 { "YYYY/MM/DD", -1, ltEnglishUS },
119 { "JJ-MM-TT", -1, ltGerman },
120 { "JJJJ-MM-TT", -1, ltGerman },
122 { nullptr, -1, ltSystem }
124 return s_aFormats;
128 OSL_FAIL("lcl_getFormatTable: invalid id!");
129 return nullptr;
132 OLimitedFormats::OLimitedFormats(const Reference< XComponentContext >& _rxContext, const sal_Int16 _nClassId)
133 :m_nFormatEnumPropertyHandle(-1)
134 ,m_nTableId(_nClassId)
136 OSL_ENSURE(_rxContext.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
137 acquireSupplier(_rxContext);
138 ensureTableInitialized(m_nTableId);
142 OLimitedFormats::~OLimitedFormats()
144 releaseSupplier();
148 void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId)
150 FormatEntry* pFormatTable = lcl_getFormatTable(_nTableId);
151 if (-1 != pFormatTable->nKey)
152 return;
154 ::osl::MutexGuard aGuard(s_aMutex);
155 if (-1 != pFormatTable->nKey)
156 return;
158 // initialize the keys
159 Reference<XNumberFormats> xStandardFormats;
160 if (s_xStandardFormats.is())
161 xStandardFormats = s_xStandardFormats->getNumberFormats();
162 OSL_ENSURE(xStandardFormats.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
164 if (!xStandardFormats.is())
165 return;
167 // loop through the table
168 FormatEntry* pLoopFormats = pFormatTable;
169 while (pLoopFormats->pDescription)
171 // get the key for the description
172 pLoopFormats->nKey = xStandardFormats->queryKey(
173 OUString::createFromAscii(pLoopFormats->pDescription),
174 getLocale(pLoopFormats->eLocale),
175 false
178 if (-1 == pLoopFormats->nKey)
180 pLoopFormats->nKey = xStandardFormats->addNew(
181 OUString::createFromAscii(pLoopFormats->pDescription),
182 getLocale(pLoopFormats->eLocale)
184 #ifdef DBG_UTIL
187 xStandardFormats->getByKey(pLoopFormats->nKey);
189 catch(const Exception&)
191 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
193 #endif
196 // next
197 ++pLoopFormats;
202 void OLimitedFormats::clearTable(const sal_Int16 _nTableId)
204 ::osl::MutexGuard aGuard(s_aMutex);
205 FormatEntry* pFormats = lcl_getFormatTable(_nTableId);
206 FormatEntry* pResetLoop = pFormats;
207 while (pResetLoop->pDescription)
209 pResetLoop->nKey = -1;
210 ++pResetLoop;
215 void OLimitedFormats::setAggregateSet(const Reference< XFastPropertySet >& _rxAggregate, sal_Int32 _nOriginalPropertyHandle)
217 // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
218 OSL_ENSURE(!m_xAggregate.is() || !_rxAggregate.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
219 OSL_ENSURE(_rxAggregate.is() || m_xAggregate.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
221 m_xAggregate = _rxAggregate;
222 m_nFormatEnumPropertyHandle = _nOriginalPropertyHandle;
223 #ifdef DBG_UTIL
224 if (m_xAggregate.is())
228 m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
230 catch(const Exception&)
232 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
235 #endif
239 void OLimitedFormats::getFormatKeyPropertyValue( Any& _rValue ) const
241 _rValue.clear();
243 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
244 if (!m_xAggregate.is())
245 return;
247 // get the aggregate's enum property value
248 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
249 sal_Int32 nValue = -1;
250 ::cppu::enum2int(nValue, aEnumPropertyValue);
252 // get the translation table
253 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
255 // seek to the nValue'th entry
256 sal_Int32 nLookup = 0;
257 for ( ;
258 (nullptr != pFormats->pDescription) && (nLookup < nValue);
259 ++pFormats, ++nLookup
262 OSL_ENSURE(nullptr != pFormats->pDescription, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
263 if (pFormats->pDescription)
264 _rValue <<= pFormats->nKey;
266 // TODO: should use a standard format for the control type we're working for
270 bool OLimitedFormats::convertFormatKeyPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rNewValue)
272 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
274 if (!m_xAggregate)
275 return false;
277 // the new format key to set
278 sal_Int32 nNewFormat = 0;
279 if (!(_rNewValue >>= nNewFormat))
280 throw IllegalArgumentException();
282 // get the old (enum) value from the aggregate
283 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
284 sal_Int32 nOldEnumValue = -1;
285 ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
287 // get the translation table
288 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
290 _rOldValue.clear();
291 _rConvertedValue.clear();
293 // look for the entry with the given format key
294 sal_Int32 nTablePosition = 0;
295 for ( ;
296 (nullptr != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
297 ++pFormats, ++nTablePosition
300 if (nTablePosition == nOldEnumValue)
301 _rOldValue <<= pFormats->nKey;
304 bool bFoundIt = (nullptr != pFormats->pDescription);
305 bool bModified = false;
306 if (bFoundIt)
308 _rConvertedValue <<= static_cast<sal_Int16>(nTablePosition);
309 bModified = nTablePosition != nOldEnumValue;
312 if (!_rOldValue.hasValue())
313 { // did not reach the end of the table (means we found nNewFormat)
314 // -> go to the end to ensure that _rOldValue is set
315 while (pFormats->pDescription)
317 if (nTablePosition == nOldEnumValue)
319 _rOldValue <<= pFormats->nKey;
320 break;
323 ++pFormats;
324 ++nTablePosition;
328 OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
330 if (!bFoundIt)
331 { // somebody gave us a format which we can't translate
332 throw IllegalArgumentException("This control supports only a very limited number of formats.", nullptr, 2);
335 return bModified;
339 void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
341 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
343 if (m_xAggregate.is())
344 { // this is to be called after convertFormatKeyPropertyValue, where
345 // we translated the format key into an enum value.
346 // So now we can simply forward this enum value to our aggregate
347 m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
352 void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
354 ::osl::MutexGuard aGuard(s_aMutex);
355 if (1 == ++s_nInstanceCount)
356 { // create the standard formatter
357 s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
362 void OLimitedFormats::releaseSupplier()
364 ::osl::MutexGuard aGuard(s_aMutex);
365 if (0 == --s_nInstanceCount)
367 ::comphelper::disposeComponent(s_xStandardFormats);
368 s_xStandardFormats = nullptr;
370 clearTable(FormComponentType::TIMEFIELD);
371 clearTable(FormComponentType::DATEFIELD);
376 } // namespace frm
379 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */