Debian Codegraph @

toolkit/source/controls/table/cellvalueconversion.cxx

raw
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 "cellvalueconversion.hxx"
21
22 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
23 #include <com/sun/star/util/NumberFormatter.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/util/Date.hpp>
26 #include <com/sun/star/util/Time.hpp>
27 #include <com/sun/star/util/DateTime.hpp>
28 #include <com/sun/star/util/XNumberFormatTypes.hpp>
29 #include <com/sun/star/util/NumberFormat.hpp>
30 #include <sal/log.hxx>
31 #include <tools/date.hxx>
32 #include <tools/time.hxx>
33 #include <comphelper/diagnose_ex.hxx>
34 #include <tools/long.hxx>
35 #include <unotools/syslocale.hxx>
36 #include <i18nlangtag/languagetag.hxx>
37 #include <comphelper/processfactory.hxx>
38
39 #include <limits>
40 #include <memory>
41
42 namespace svt
43 {
44 using namespace ::com::sun::star::uno;
45 using ::com::sun::star::util::XNumberFormatter;
46 using ::com::sun::star::util::NumberFormatter;
47 using ::com::sun::star::util::XNumberFormatsSupplier;
48 using ::com::sun::star::util::NumberFormatsSupplier;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::lang::Locale;
51 using ::com::sun::star::util::DateTime;
52 using ::com::sun::star::util::XNumberFormatTypes;
53
54 namespace NumberFormat = ::com::sun::star::util::NumberFormat;
55
56 //= helper
57
58 namespace
59 {
60 double lcl_convertDateToDays(sal_uInt16 const i_day, sal_uInt16 const i_month,
61 sal_Int16 const i_year)
62 {
63 tools::Long const nNullDateDays = ::Date::DateToDays(30, 12, 1899);
64 tools::Long const nValueDateDays = ::Date::DateToDays(i_day, i_month, i_year);
65
66 return nValueDateDays - nNullDateDays;
67 }
68
69 double lcl_convertTimeToDays(tools::Long const i_hours, tools::Long const i_minutes,
70 tools::Long const i_seconds, tools::Long const i_100thSeconds)
71 {
72 return tools::Time(i_hours, i_minutes, i_seconds, i_100thSeconds).GetTimeInDays();
73 }
74 }
75
76 //= StandardFormatNormalizer
77
78 class StandardFormatNormalizer
79 {
80 public:
81 virtual ~StandardFormatNormalizer() = default;
82
83 /** converts the given <code>Any</code> into a <code>double</code> value to be fed into a number formatter
84 */
85 virtual double convertToDouble(css::uno::Any const& i_value) const = 0;
86
87 /** returns the format key to be used for formatting values
88 */
89 sal_Int32 getFormatKey() const { return m_nFormatKey; }
90
91 protected:
92 StandardFormatNormalizer(css::uno::Reference<css::util::XNumberFormatter> const& i_formatter,
93 ::sal_Int32 const i_numberFormatType);
94
95 private:
96 ::sal_Int32 m_nFormatKey;
97 };
98
99 StandardFormatNormalizer::StandardFormatNormalizer(Reference<XNumberFormatter> const& i_formatter,
100 ::sal_Int32 const i_numberFormatType)
101 : m_nFormatKey(0)
102 {
103 try
104 {
105 ENSURE_OR_THROW(i_formatter.is(), "StandardFormatNormalizer: no formatter!");
106 Reference<XNumberFormatsSupplier> const xSupplier(i_formatter->getNumberFormatsSupplier(),
107 UNO_SET_THROW);
108 Reference<XNumberFormatTypes> const xTypes(xSupplier->getNumberFormats(), UNO_QUERY_THROW);
109 m_nFormatKey = xTypes->getStandardFormat(i_numberFormatType,
110 SvtSysLocale().GetLanguageTag().getLocale());
111 }
112 catch (const Exception&)
113 {
114 DBG_UNHANDLED_EXCEPTION("svtools.table");
115 }
116 }
117
118 //= DoubleNormalization
119
120 namespace
121 {
122 class DoubleNormalization : public StandardFormatNormalizer
123 {
124 public:
125 explicit DoubleNormalization(Reference<XNumberFormatter> const& i_formatter)
126 : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
127 {
128 }
129
130 virtual double convertToDouble(Any const& i_value) const override
131 {
132 double returnValue = std::numeric_limits<double>::quiet_NaN();
133 OSL_VERIFY(i_value >>= returnValue);
134 return returnValue;
135 }
136 };
137
138 //= IntegerNormalization
139
140 class IntegerNormalization : public StandardFormatNormalizer
141 {
142 public:
143 explicit IntegerNormalization(Reference<XNumberFormatter> const& i_formatter)
144 : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
145 {
146 }
147
148 virtual double convertToDouble(Any const& i_value) const override
149 {
150 sal_Int64 value(0);
151 OSL_VERIFY(i_value >>= value);
152 return value;
153 }
154 };
155
156 //= BooleanNormalization
157
158 class BooleanNormalization : public StandardFormatNormalizer
159 {
160 public:
161 explicit BooleanNormalization(Reference<XNumberFormatter> const& i_formatter)
162 : StandardFormatNormalizer(i_formatter, NumberFormat::LOGICAL)
163 {
164 }
165
166 virtual double convertToDouble(Any const& i_value) const override
167 {
168 bool value(false);
169 OSL_VERIFY(i_value >>= value);
170 return value ? 1 : 0;
171 }
172 };
173
174 //= DateTimeNormalization
175
176 class DateTimeNormalization : public StandardFormatNormalizer
177 {
178 public:
179 explicit DateTimeNormalization(Reference<XNumberFormatter> const& i_formatter)
180 : StandardFormatNormalizer(i_formatter, NumberFormat::DATETIME)
181 {
182 }
183
184 virtual double convertToDouble(Any const& i_value) const override
185 {
186 double returnValue = std::numeric_limits<double>::quiet_NaN();
187
188 // extract actual UNO value
189 DateTime aDateTimeValue;
190 ENSURE_OR_RETURN(i_value >>= aDateTimeValue, "allowed for DateTime values only",
191 returnValue);
192
193 // date part
194 returnValue
195 = lcl_convertDateToDays(aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year);
196
197 // time part
198 returnValue += lcl_convertTimeToDays(aDateTimeValue.Hours, aDateTimeValue.Minutes,
199 aDateTimeValue.Seconds, aDateTimeValue.NanoSeconds);
200
201 // done
202 return returnValue;
203 }
204 };
205
206 //= DateNormalization
207
208 class DateNormalization : public StandardFormatNormalizer
209 {
210 public:
211 explicit DateNormalization(Reference<XNumberFormatter> const& i_formatter)
212 : StandardFormatNormalizer(i_formatter, NumberFormat::DATE)
213 {
214 }
215
216 virtual double convertToDouble(Any const& i_value) const override
217 {
218 double returnValue = std::numeric_limits<double>::quiet_NaN();
219
220 // extract
221 css::util::Date aDateValue;
222 ENSURE_OR_RETURN(i_value >>= aDateValue, "allowed for Date values only", returnValue);
223
224 // convert
225 returnValue = lcl_convertDateToDays(aDateValue.Day, aDateValue.Month, aDateValue.Year);
226
227 // done
228 return returnValue;
229 }
230 };
231
232 //= TimeNormalization
233
234 class TimeNormalization : public StandardFormatNormalizer
235 {
236 public:
237 explicit TimeNormalization(Reference<XNumberFormatter> const& i_formatter)
238 : StandardFormatNormalizer(i_formatter, NumberFormat::TIME)
239 {
240 }
241
242 virtual double convertToDouble(Any const& i_value) const override
243 {
244 double returnValue = std::numeric_limits<double>::quiet_NaN();
245
246 // extract
247 css::util::Time aTimeValue;
248 ENSURE_OR_RETURN(i_value >>= aTimeValue, "allowed for tools::Time values only",
249 returnValue);
250
251 // convert
252 returnValue += lcl_convertTimeToDays(aTimeValue.Hours, aTimeValue.Minutes,
253 aTimeValue.Seconds, aTimeValue.NanoSeconds);
254
255 // done
256 return returnValue;
257 }
258 };
259 }
260
261 //= operations
262
263 bool CellValueConversion::ensureNumberFormatter()
264 {
265 if (bAttemptedFormatterCreation)
266 return xNumberFormatter.is();
267 bAttemptedFormatterCreation = true;
268
269 try
270 {
271 const Reference<XComponentContext>& xContext = ::comphelper::getProcessComponentContext();
272 // a number formatter
273 Reference<XNumberFormatter> const xFormatter(NumberFormatter::create(xContext),
274 UNO_QUERY_THROW);
275
276 // a supplier of number formats
277 Locale aLocale = SvtSysLocale().GetLanguageTag().getLocale();
278
279 Reference<XNumberFormatsSupplier> const xSupplier
280 = NumberFormatsSupplier::createWithLocale(xContext, aLocale);
281
282 // ensure a NullDate we will assume later on
283 css::util::Date const aNullDate(30, 12, 1899);
284 Reference<XPropertySet> const xFormatSettings(xSupplier->getNumberFormatSettings(),
285 UNO_SET_THROW);
286 xFormatSettings->setPropertyValue(u"NullDate"_ustr, Any(aNullDate));
287
288 // knit
289 xFormatter->attachNumberFormatsSupplier(xSupplier);
290
291 // done
292 xNumberFormatter = xFormatter;
293 }
294 catch (const Exception&)
295 {
296 DBG_UNHANDLED_EXCEPTION("svtools.table");
297 }
298
299 return xNumberFormatter.is();
300 }
301
302 const StandardFormatNormalizer* CellValueConversion::getValueNormalizer(Type const& i_valueType)
303 {
304 auto [pos, inserted] = aNormalizers.try_emplace(i_valueType.getTypeName());
305 if (inserted)
306 {
307 // never encountered this type before
308 OUString const sTypeName(i_valueType.getTypeName());
309 TypeClass const eTypeClass = i_valueType.getTypeClass();
310
311 if (sTypeName == ::cppu::UnoType<DateTime>::get().getTypeName())
312 {
313 pos->second = std::make_unique<DateTimeNormalization>(xNumberFormatter);
314 }
315 else if (sTypeName == ::cppu::UnoType<css::util::Date>::get().getTypeName())
316 {
317 pos->second = std::make_unique<DateNormalization>(xNumberFormatter);
318 }
319 else if (sTypeName == ::cppu::UnoType<css::util::Time>::get().getTypeName())
320 {
321 pos->second = std::make_unique<TimeNormalization>(xNumberFormatter);
322 }
323 else if (sTypeName == ::cppu::UnoType<sal_Bool>::get().getTypeName())
324 {
325 pos->second = std::make_unique<BooleanNormalization>(xNumberFormatter);
326 }
327 else if (sTypeName == ::cppu::UnoType<double>::get().getTypeName()
328 || sTypeName == ::cppu::UnoType<float>::get().getTypeName())
329 {
330 pos->second = std::make_unique<DoubleNormalization>(xNumberFormatter);
331 }
332 else if ((eTypeClass == TypeClass_BYTE) || (eTypeClass == TypeClass_SHORT)
333 || (eTypeClass == TypeClass_UNSIGNED_SHORT) || (eTypeClass == TypeClass_LONG)
334 || (eTypeClass == TypeClass_UNSIGNED_LONG) || (eTypeClass == TypeClass_HYPER))
335 {
336 pos->second = std::make_unique<IntegerNormalization>(xNumberFormatter);
337 }
338 else
339 {
340 SAL_WARN("svtools.table", "unsupported type '" << sTypeName << "'!");
341 }
342 }
343
344 return pos->second.get();
345 }
346
347 //= CellValueConversion
348
349 CellValueConversion::CellValueConversion()
350 : xNumberFormatter()
351 , bAttemptedFormatterCreation(false)
352 , aNormalizers()
353 {
354 }
355
356 CellValueConversion::~CellValueConversion() {}
357
358 OUString CellValueConversion::convertToString(const Any& i_value)
359 {
360 OUString sStringValue;
361 if (!i_value.hasValue())
362 return sStringValue;
363
364 if (!(i_value >>= sStringValue))
365 {
366 if (ensureNumberFormatter())
367 {
368 if (auto* pNormalizer = getValueNormalizer(i_value.getValueType()))
369 {
370 try
371 {
372 double const formatterCompliantValue = pNormalizer->convertToDouble(i_value);
373 sal_Int32 const formatKey = pNormalizer->getFormatKey();
374 sStringValue = xNumberFormatter->convertNumberToString(formatKey,
375 formatterCompliantValue);
376 }
377 catch (const Exception&)
378 {
379 DBG_UNHANDLED_EXCEPTION("svtools.table");
380 }
381 }
382 }
383 }
384
385 return sStringValue;
386 }
387
388 } // namespace svt
389
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */