LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / forms / source / misc / limitedformats.cxx
blobf49b8617ff2cf95eb6e47cec618745eb3682adbd
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.is())
276 // the new format key to set
277 sal_Int32 nNewFormat = 0;
278 if (!(_rNewValue >>= nNewFormat))
279 throw IllegalArgumentException();
281 // get the old (enum) value from the aggregate
282 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
283 sal_Int32 nOldEnumValue = -1;
284 ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
286 // get the translation table
287 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
289 _rOldValue.clear();
290 _rConvertedValue.clear();
292 // look for the entry with the given format key
293 sal_Int32 nTablePosition = 0;
294 for ( ;
295 (nullptr != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
296 ++pFormats, ++nTablePosition
299 if (nTablePosition == nOldEnumValue)
300 _rOldValue <<= pFormats->nKey;
303 bool bFoundIt = (nullptr != pFormats->pDescription);
304 bool bModified = false;
305 if (bFoundIt)
307 _rConvertedValue <<= static_cast<sal_Int16>(nTablePosition);
308 bModified = nTablePosition != nOldEnumValue;
311 if (!_rOldValue.hasValue())
312 { // did not reach the end of the table (means we found nNewFormat)
313 // -> go to the end to ensure that _rOldValue is set
314 while (pFormats->pDescription)
316 if (nTablePosition == nOldEnumValue)
318 _rOldValue <<= pFormats->nKey;
319 break;
322 ++pFormats;
323 ++nTablePosition;
327 OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
329 if (!bFoundIt)
330 { // somebody gave us a format which we can't translate
331 throw IllegalArgumentException("This control supports only a very limited number of formats.", nullptr, 2);
334 return bModified;
337 return false;
341 void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
343 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
345 if (m_xAggregate.is())
346 { // this is to be called after convertFormatKeyPropertyValue, where
347 // we translated the format key into an enum value.
348 // So now we can simply forward this enum value to our aggregate
349 m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
354 void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
356 ::osl::MutexGuard aGuard(s_aMutex);
357 if (1 == ++s_nInstanceCount)
358 { // create the standard formatter
359 s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
364 void OLimitedFormats::releaseSupplier()
366 ::osl::MutexGuard aGuard(s_aMutex);
367 if (0 == --s_nInstanceCount)
369 ::comphelper::disposeComponent(s_xStandardFormats);
370 s_xStandardFormats = nullptr;
372 clearTable(FormComponentType::TIMEFIELD);
373 clearTable(FormComponentType::DATEFIELD);
378 } // namespace frm
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */