LibreOffice Module i18npool (master) 1
calendarImpl.cxx
Go to the documentation of this file.
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 <calendarImpl.hxx>
22#include <localedata.hxx>
25
26#include <com/sun/star/uno/XComponentContext.hpp>
27
28using namespace ::com::sun::star::uno;
29using namespace ::com::sun::star::i18n;
30
31namespace i18npool {
32
34{
35}
36
37CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
38{
39 if (!m_xContext.is())
40 throw RuntimeException("CalendarImpl::CalendarImpl: empty m_xContext");
41}
42
44{
45}
46
47void SAL_CALL
48CalendarImpl::loadDefaultCalendarTZ( const css::lang::Locale& rLocale, const OUString& rTimeZone )
49{
50 const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
51 auto pCal = std::find_if(xC.begin(), xC.end(), [](const Calendar2& rCal) { return rCal.Default; });
52 if (pCal == xC.end())
53 throw RuntimeException("CalendarImpl::loadDefaultCalendarTZ: no default calendar found for this locale");
54 loadCalendarTZ(pCal->Name, rLocale, rTimeZone);
55}
56
57void SAL_CALL
58CalendarImpl::loadCalendarTZ( const OUString& uniqueID, const css::lang::Locale& rLocale, const OUString& rTimeZone )
59{
60 Reference < XCalendar4 > xOldCalendar( xCalendar ); // backup
61 const OUString aCacheID( uniqueID + "_" + rTimeZone);
62 bool bTimeZone = true;
63 sal_Int32 i;
64
65 for (i = 0; i < sal::static_int_cast<sal_Int32>(lookupTable.size()); i++) {
66 lookupTableItem &listItem = lookupTable[i];
67 if (aCacheID == listItem.m_aCacheID) {
68 xCalendar = listItem.xCalendar;
69 break;
70 }
71 }
72
73 if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
74 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
75 "com.sun.star.i18n.Calendar_" + uniqueID, m_xContext);
76
77 if ( ! xI.is() ) {
78 // check if the calendar is defined in localedata, load gregorian calendar service.
79 const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
80 if (std::any_of(xC.begin(), xC.end(), [&uniqueID](const Calendar2& rCal) { return uniqueID == rCal.Name; }))
81 xI = m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext);
82 }
83
84 if ( !xI.is() )
85 throw RuntimeException("CalendarImpl::loadCalendarTZ: no calendar found for this locale");
86 xCalendar.set(xI, UNO_QUERY);
87
88 if (!rTimeZone.isEmpty())
89 {
90 /* XXX NOTE: currently (2019-06-19) calendar implementations derive
91 * from Calendar_gregorian, even Hijri and Jewish. If that should
92 * change in future this should be adapted. */
93 Calendar_gregorian* pCal = dynamic_cast<Calendar_gregorian*>(xCalendar.get());
94 bTimeZone = (pCal && pCal->setTimeZone(rTimeZone));
95 }
96
97 lookupTable.emplace_back( aCacheID, xCalendar );
98 }
99
100 if ( !xCalendar.is() )
101 {
102 xCalendar = xOldCalendar;
103 throw RuntimeException("CalendarImpl::loadCalendarTZ: no calendar found for this locale, should use old one?");
104 }
105
106 try
107 {
108 xCalendar->loadCalendar(uniqueID, rLocale);
109 }
110 catch ( Exception& )
111 { // restore previous calendar and re-throw
112 xCalendar = xOldCalendar;
113 throw;
114 }
115
116 if (!bTimeZone)
117 // The calendar is usable but is not in the expected time zone.
118 throw RuntimeException("CalendarImpl::loadCalendarTZ: the calendar is usable but is not in the expected time zone");
119}
120
121Calendar2 SAL_CALL
123{
124 if (!xCalendar.is())
125 throw RuntimeException("CalendarImpl::getLoadedCalendar2: no calendar");
126 return xCalendar->getLoadedCalendar2();
127}
128
129::css::i18n::Calendar SAL_CALL
131{
132 if (!xCalendar.is())
133 throw RuntimeException("CalendarImpl::getLoadedCalendar: no calendar");
134 return xCalendar->getLoadedCalendar();
135}
136
137Sequence< OUString > SAL_CALL
138CalendarImpl::getAllCalendars( const css::lang::Locale& rLocale )
139{
140 const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
141 Sequence< OUString > xSeq( xC.getLength() );
142 std::transform(xC.begin(), xC.end(), xSeq.getArray(),
143 [](const Calendar2& rCal) { return rCal.Name; });
144 return xSeq;
145}
146
147void SAL_CALL
148CalendarImpl::setDateTime( double fTimeInDays )
149{
150 if (!xCalendar.is())
151 throw RuntimeException("CalendarImpl::setDateTime: no calendar");
152 xCalendar->setDateTime( fTimeInDays );
153}
154
155double SAL_CALL
157{
158 if (!xCalendar.is())
159 throw RuntimeException("CalendarImpl::getDateTime: no calendar");
160 return xCalendar->getDateTime();
161}
162
163void SAL_CALL
165{
166 if (!xCalendar.is())
167 throw RuntimeException("CalendarImpl::setLocalDateTime: no calendar");
168 xCalendar->setLocalDateTime( fTimeInDays );
169}
170
171double SAL_CALL
173{
174 if (!xCalendar.is())
175 throw RuntimeException("CalendarImpl::getLocalDateTime: no calendar");
176 return xCalendar->getLocalDateTime();
177}
178
179void SAL_CALL CalendarImpl::loadDefaultCalendar( const css::lang::Locale& rLocale )
180{
181 loadDefaultCalendarTZ( rLocale, OUString());
182}
183
184void SAL_CALL CalendarImpl::loadCalendar( const OUString& uniqueID, const css::lang::Locale& rLocale )
185{
186 loadCalendarTZ( uniqueID, rLocale, OUString());
187}
188
189OUString SAL_CALL
191{
192 if (!xCalendar.is())
193 throw RuntimeException("CalendarImpl::getUniqueID: no calendar");
194 return xCalendar->getUniqueID();
195}
196
197void SAL_CALL
198CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value )
199{
200 if (!xCalendar.is())
201 throw RuntimeException("CalendarImpl::setValue: no calendar");
202 xCalendar->setValue( fieldIndex, value );
203}
204
205sal_Int16 SAL_CALL
206CalendarImpl::getValue( sal_Int16 fieldIndex )
207{
208 if (!xCalendar.is())
209 throw RuntimeException("CalendarImpl::getValue: no calendar");
210 return xCalendar->getValue( fieldIndex );
211}
212
213void SAL_CALL
214CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount )
215{
216 if (!xCalendar.is())
217 throw RuntimeException("CalendarImpl::addValue: no calendar");
218 xCalendar->addValue( fieldIndex, amount);
219}
220
221sal_Int16 SAL_CALL
223{
224 if (!xCalendar.is())
225 throw RuntimeException("CalendarImpl::getFirstDayOfWeek: no calendar");
226 return xCalendar->getFirstDayOfWeek();
227}
228
229void SAL_CALL
231{
232 if (!xCalendar.is())
233 throw RuntimeException("CalendarImpl::setFirstDayOfWeek: no calendar");
234 xCalendar->setFirstDayOfWeek(day);
235}
236
237void SAL_CALL
239{
240 if (!xCalendar.is())
241 throw RuntimeException("CalendarImpl::setMinimumNumberOfDaysForFirstWeek: no calendar");
242 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
243}
244
245sal_Int16 SAL_CALL
247{
248 if (!xCalendar.is())
249 throw RuntimeException("CalendarImpl::getMinimumNumberOfDaysForFirstWeek: no calendar");
250 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
251}
252
253
254OUString SAL_CALL
255CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
256{
257 if (!xCalendar.is())
258 throw RuntimeException("CalendarImpl::getDisplayName: no calendar");
259 return xCalendar->getDisplayName( displayIndex, idx, nameType );
260}
261
262sal_Int16 SAL_CALL
264{
265 if (!xCalendar.is())
266 throw RuntimeException("CalendarImpl::setDisplayName: no calendar");
267 return xCalendar->getNumberOfMonthsInYear();
268}
269
270
271sal_Int16 SAL_CALL
273{
274 if (!xCalendar.is())
275 throw RuntimeException("CalendarImpl::getNumberOfDaysInWeek: no calendar");
276 return xCalendar->getNumberOfDaysInWeek();
277}
278
279
280Sequence< CalendarItem > SAL_CALL
282{
283 if (!xCalendar.is())
284 throw RuntimeException("CalendarImpl::setNumberOfDaysInWeek: no calendar");
285 return xCalendar->getDays();
286}
287
288
289Sequence< CalendarItem > SAL_CALL
291{
292 if (!xCalendar.is())
293 throw RuntimeException("CalendarImpl::getMonths: no calendar");
294 return xCalendar->getMonths();
295}
296
297
298Sequence< CalendarItem2 > SAL_CALL
300{
301 if (!xCalendar.is())
302 throw RuntimeException("CalendarImpl::getDays2: no calendar");
303 return xCalendar->getDays2();
304}
305
306
307Sequence< CalendarItem2 > SAL_CALL
309{
310 if (!xCalendar.is())
311 throw RuntimeException("CalendarImpl::getMonths2: no calendar");
312 return xCalendar->getMonths2();
313}
314
315
316Sequence< CalendarItem2 > SAL_CALL
318{
319 if (!xCalendar.is())
320 throw RuntimeException("CalendarImpl::getGenitiveMonths2: no calendar");
321 return xCalendar->getGenitiveMonths2();
322}
323
324
325Sequence< CalendarItem2 > SAL_CALL
327{
328 if (!xCalendar.is())
329 throw RuntimeException("CalendarImpl::getPartitiveMonths2: no calendar");
330 return xCalendar->getPartitiveMonths2();
331}
332
333
334sal_Bool SAL_CALL
336{
337 if (!xCalendar.is())
338 throw RuntimeException("CalendarImpl::isValid: no calendar");
339 return xCalendar->isValid();
340}
341
342OUString SAL_CALL
343CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
344{
345 if (!xCalendar.is())
346 throw RuntimeException("CalendarImpl::getDisplayString: no calendar");
347 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
348}
349
350OUString SAL_CALL
352{
353 return "com.sun.star.i18n.CalendarImpl";
354}
355
356sal_Bool SAL_CALL
357CalendarImpl::supportsService(const OUString& rServiceName)
358{
359 return cppu::supportsService(this, rServiceName);
360}
361
362Sequence< OUString > SAL_CALL
364{
365 return { "com.sun.star.i18n.LocaleCalendar", "com.sun.star.i18n.LocaleCalendar2" };
366}
367
368}
369
370/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
sal_Int32 day
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual sal_Int16 SAL_CALL getMinimumNumberOfDaysForFirstWeek() override
virtual css::uno::Sequence< OUString > SAL_CALL getAllCalendars(const css::lang::Locale &rLocale) override
std::vector< lookupTableItem > lookupTable
css::uno::Reference< css::i18n::XCalendar4 > xCalendar
virtual OUString SAL_CALL getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) override
virtual sal_Bool SAL_CALL isValid() override
virtual double SAL_CALL getDateTime() override
virtual css::uno::Sequence< css::i18n::CalendarItem2 > SAL_CALL getDays2() override
virtual void SAL_CALL setFirstDayOfWeek(sal_Int16 nDay) override
virtual void SAL_CALL addValue(sal_Int16 nFieldIndex, sal_Int32 nAmount) override
virtual void SAL_CALL loadDefaultCalendar(const css::lang::Locale &rLocale) override
virtual double SAL_CALL getLocalDateTime() override
virtual css::uno::Sequence< css::i18n::CalendarItem > SAL_CALL getDays() override
virtual OUString SAL_CALL getUniqueID() override
virtual OUString SAL_CALL getImplementationName() override
virtual css::i18n::Calendar2 SAL_CALL getLoadedCalendar2() override
virtual css::i18n::Calendar SAL_CALL getLoadedCalendar() override
virtual ~CalendarImpl() override
Destructor.
virtual void SAL_CALL loadCalendar(const OUString &uniqueID, const css::lang::Locale &rLocale) override
virtual void SAL_CALL setMinimumNumberOfDaysForFirstWeek(sal_Int16 nDays) override
virtual void SAL_CALL setLocalDateTime(double TimeInDays) override
virtual sal_Int16 SAL_CALL getNumberOfDaysInWeek() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL loadDefaultCalendarTZ(const css::lang::Locale &rLocale, const OUString &rTimeZone) override
virtual sal_Int16 SAL_CALL getFirstDayOfWeek() override
virtual void SAL_CALL loadCalendarTZ(const OUString &uniqueID, const css::lang::Locale &rLocale, const OUString &rTimeZone) override
virtual void SAL_CALL setValue(sal_Int16 nFieldIndex, sal_Int16 nValue) override
virtual css::uno::Sequence< css::i18n::CalendarItem2 > SAL_CALL getPartitiveMonths2() override
virtual css::uno::Sequence< css::i18n::CalendarItem2 > SAL_CALL getGenitiveMonths2() override
virtual sal_Int16 SAL_CALL getValue(sal_Int16 nFieldIndex) override
virtual css::uno::Sequence< css::i18n::CalendarItem2 > SAL_CALL getMonths2() override
virtual css::uno::Sequence< css::i18n::CalendarItem > SAL_CALL getMonths() override
virtual OUString SAL_CALL getDisplayString(sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode) override
virtual void SAL_CALL setDateTime(double fTimeInDays) override
virtual sal_Int16 SAL_CALL getNumberOfMonthsInYear() override
bool setTimeZone(const OUString &rTimeZone)
static rtl::Reference< LocaleDataImpl > get()
Definition: localedata.hxx:77
Any value
const sal_uInt16 idx[]
@ Exception
Reference< XComponentContext > getProcessComponentContext()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
Constant values shared between i18npool and, for example, the number formatter.
css::uno::Reference< css::i18n::XCalendar4 > xCalendar
unsigned char sal_Bool