1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <calendarImpl.hxx>
21 #include <calendar_gregorian.hxx>
22 #include <localedata.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/uno/XComponentContext.hpp>
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::i18n
;
33 CalendarImpl::CalendarImpl() : m_xContext(comphelper::getProcessComponentContext())
37 CalendarImpl::CalendarImpl(const Reference
< XComponentContext
> &rxContext
) : m_xContext(rxContext
)
40 throw RuntimeException(u
"CalendarImpl::CalendarImpl: empty m_xContext"_ustr
);
43 CalendarImpl::~CalendarImpl()
48 CalendarImpl::loadDefaultCalendarTZ( const css::lang::Locale
& rLocale
, const OUString
& rTimeZone
)
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
; });
53 throw RuntimeException(u
"CalendarImpl::loadDefaultCalendarTZ: no default calendar found for this locale"_ustr
);
54 loadCalendarTZ(pCal
->Name
, rLocale
, rTimeZone
);
58 CalendarImpl::loadCalendarTZ( const OUString
& uniqueID
, const css::lang::Locale
& rLocale
, const OUString
& rTimeZone
)
60 Reference
< XCalendar4
> xOldCalendar( xCalendar
); // backup
61 const OUString
aCacheID( uniqueID
+ "_" + rTimeZone
);
62 bool bTimeZone
= true;
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
;
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
);
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(u
"com.sun.star.i18n.Calendar_gregorian"_ustr
, m_xContext
);
85 throw RuntimeException(u
"CalendarImpl::loadCalendarTZ: no calendar found for this locale"_ustr
);
86 xCalendar
.set(xI
, UNO_QUERY
);
88 if (!rTimeZone
.isEmpty())
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
));
97 lookupTable
.emplace_back( aCacheID
, xCalendar
);
100 if ( !xCalendar
.is() )
102 xCalendar
= xOldCalendar
;
103 throw RuntimeException(u
"CalendarImpl::loadCalendarTZ: no calendar found for this locale, should use old one?"_ustr
);
108 xCalendar
->loadCalendar(uniqueID
, rLocale
);
111 { // restore previous calendar and re-throw
112 xCalendar
= std::move(xOldCalendar
);
117 // The calendar is usable but is not in the expected time zone.
118 throw RuntimeException(u
"CalendarImpl::loadCalendarTZ: the calendar is usable but is not in the expected time zone"_ustr
);
122 CalendarImpl::getLoadedCalendar2()
125 throw RuntimeException(u
"CalendarImpl::getLoadedCalendar2: no calendar"_ustr
);
126 return xCalendar
->getLoadedCalendar2();
129 ::css::i18n::Calendar SAL_CALL
130 CalendarImpl::getLoadedCalendar()
133 throw RuntimeException(u
"CalendarImpl::getLoadedCalendar: no calendar"_ustr
);
134 return xCalendar
->getLoadedCalendar();
137 Sequence
< OUString
> SAL_CALL
138 CalendarImpl::getAllCalendars( const css::lang::Locale
& rLocale
)
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
; });
148 CalendarImpl::setDateTime( double fTimeInDays
)
151 throw RuntimeException(u
"CalendarImpl::setDateTime: no calendar"_ustr
);
152 xCalendar
->setDateTime( fTimeInDays
);
156 CalendarImpl::getDateTime()
159 throw RuntimeException(u
"CalendarImpl::getDateTime: no calendar"_ustr
);
160 return xCalendar
->getDateTime();
164 CalendarImpl::setLocalDateTime( double fTimeInDays
)
167 throw RuntimeException(u
"CalendarImpl::setLocalDateTime: no calendar"_ustr
);
168 xCalendar
->setLocalDateTime( fTimeInDays
);
172 CalendarImpl::getLocalDateTime()
175 throw RuntimeException(u
"CalendarImpl::getLocalDateTime: no calendar"_ustr
);
176 return xCalendar
->getLocalDateTime();
179 void SAL_CALL
CalendarImpl::loadDefaultCalendar( const css::lang::Locale
& rLocale
)
181 loadDefaultCalendarTZ( rLocale
, OUString());
184 void SAL_CALL
CalendarImpl::loadCalendar( const OUString
& uniqueID
, const css::lang::Locale
& rLocale
)
186 loadCalendarTZ( uniqueID
, rLocale
, OUString());
190 CalendarImpl::getUniqueID()
193 throw RuntimeException(u
"CalendarImpl::getUniqueID: no calendar"_ustr
);
194 return xCalendar
->getUniqueID();
198 CalendarImpl::setValue( sal_Int16 fieldIndex
, sal_Int16 value
)
201 throw RuntimeException(u
"CalendarImpl::setValue: no calendar"_ustr
);
202 xCalendar
->setValue( fieldIndex
, value
);
206 CalendarImpl::getValue( sal_Int16 fieldIndex
)
209 throw RuntimeException(u
"CalendarImpl::getValue: no calendar"_ustr
);
210 return xCalendar
->getValue( fieldIndex
);
214 CalendarImpl::addValue( sal_Int16 fieldIndex
, sal_Int32 amount
)
217 throw RuntimeException(u
"CalendarImpl::addValue: no calendar"_ustr
);
218 xCalendar
->addValue( fieldIndex
, amount
);
222 CalendarImpl::getFirstDayOfWeek()
225 throw RuntimeException(u
"CalendarImpl::getFirstDayOfWeek: no calendar"_ustr
);
226 return xCalendar
->getFirstDayOfWeek();
230 CalendarImpl::setFirstDayOfWeek( sal_Int16 day
)
233 throw RuntimeException(u
"CalendarImpl::setFirstDayOfWeek: no calendar"_ustr
);
234 xCalendar
->setFirstDayOfWeek(day
);
238 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
)
241 throw RuntimeException(u
"CalendarImpl::setMinimumNumberOfDaysForFirstWeek: no calendar"_ustr
);
242 xCalendar
->setMinimumNumberOfDaysForFirstWeek(days
);
246 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
249 throw RuntimeException(u
"CalendarImpl::getMinimumNumberOfDaysForFirstWeek: no calendar"_ustr
);
250 return xCalendar
->getMinimumNumberOfDaysForFirstWeek();
255 CalendarImpl::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
)
258 throw RuntimeException(u
"CalendarImpl::getDisplayName: no calendar"_ustr
);
259 return xCalendar
->getDisplayName( displayIndex
, idx
, nameType
);
263 CalendarImpl::getNumberOfMonthsInYear()
266 throw RuntimeException(u
"CalendarImpl::setDisplayName: no calendar"_ustr
);
267 return xCalendar
->getNumberOfMonthsInYear();
272 CalendarImpl::getNumberOfDaysInWeek()
275 throw RuntimeException(u
"CalendarImpl::getNumberOfDaysInWeek: no calendar"_ustr
);
276 return xCalendar
->getNumberOfDaysInWeek();
280 Sequence
< CalendarItem
> SAL_CALL
281 CalendarImpl::getDays()
284 throw RuntimeException(u
"CalendarImpl::setNumberOfDaysInWeek: no calendar"_ustr
);
285 return xCalendar
->getDays();
289 Sequence
< CalendarItem
> SAL_CALL
290 CalendarImpl::getMonths()
293 throw RuntimeException(u
"CalendarImpl::getMonths: no calendar"_ustr
);
294 return xCalendar
->getMonths();
298 Sequence
< CalendarItem2
> SAL_CALL
299 CalendarImpl::getDays2()
302 throw RuntimeException(u
"CalendarImpl::getDays2: no calendar"_ustr
);
303 return xCalendar
->getDays2();
307 Sequence
< CalendarItem2
> SAL_CALL
308 CalendarImpl::getMonths2()
311 throw RuntimeException(u
"CalendarImpl::getMonths2: no calendar"_ustr
);
312 return xCalendar
->getMonths2();
316 Sequence
< CalendarItem2
> SAL_CALL
317 CalendarImpl::getGenitiveMonths2()
320 throw RuntimeException(u
"CalendarImpl::getGenitiveMonths2: no calendar"_ustr
);
321 return xCalendar
->getGenitiveMonths2();
325 Sequence
< CalendarItem2
> SAL_CALL
326 CalendarImpl::getPartitiveMonths2()
329 throw RuntimeException(u
"CalendarImpl::getPartitiveMonths2: no calendar"_ustr
);
330 return xCalendar
->getPartitiveMonths2();
335 CalendarImpl::isValid()
338 throw RuntimeException(u
"CalendarImpl::isValid: no calendar"_ustr
);
339 return xCalendar
->isValid();
343 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
346 throw RuntimeException(u
"CalendarImpl::getDisplayString: no calendar"_ustr
);
347 return xCalendar
->getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
351 CalendarImpl::getImplementationName()
353 return u
"com.sun.star.i18n.CalendarImpl"_ustr
;
357 CalendarImpl::supportsService(const OUString
& rServiceName
)
359 return cppu::supportsService(this, rServiceName
);
362 Sequence
< OUString
> SAL_CALL
363 CalendarImpl::getSupportedServiceNames()
365 return { u
"com.sun.star.i18n.LocaleCalendar"_ustr
, u
"com.sun.star.i18n.LocaleCalendar2"_ustr
};
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */