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/sequence.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 #define ERROR RuntimeException()
35 CalendarImpl::CalendarImpl(const Reference
< XComponentContext
> &rxContext
) : m_xContext(rxContext
)
39 CalendarImpl::~CalendarImpl()
44 CalendarImpl::loadDefaultCalendarTZ( const css::lang::Locale
& rLocale
, const OUString
& rTimeZone
)
46 const Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
47 auto pCal
= std::find_if(xC
.begin(), xC
.end(), [](const Calendar2
& rCal
) { return rCal
.Default
; });
50 loadCalendarTZ(pCal
->Name
, rLocale
, rTimeZone
);
54 CalendarImpl::loadCalendarTZ( const OUString
& uniqueID
, const css::lang::Locale
& rLocale
, const OUString
& rTimeZone
)
56 Reference
< XCalendar4
> xOldCalendar( xCalendar
); // backup
57 const OUString
aCacheID( uniqueID
+ "_" + rTimeZone
);
58 bool bTimeZone
= true;
61 for (i
= 0; i
< sal::static_int_cast
<sal_Int32
>(lookupTable
.size()); i
++) {
62 lookupTableItem
&listItem
= lookupTable
[i
];
63 if (aCacheID
== listItem
.m_aCacheID
) {
64 xCalendar
= listItem
.xCalendar
;
69 if (i
>= sal::static_int_cast
<sal_Int32
>(lookupTable
.size())) {
70 Reference
< XInterface
> xI
= m_xContext
->getServiceManager()->createInstanceWithContext(
71 "com.sun.star.i18n.Calendar_" + uniqueID
, m_xContext
);
74 // check if the calendar is defined in localedata, load gregorian calendar service.
75 const Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
76 if (std::any_of(xC
.begin(), xC
.end(), [&uniqueID
](const Calendar2
& rCal
) { return uniqueID
== rCal
.Name
; }))
77 xI
= m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext
);
82 xCalendar
.set(xI
, UNO_QUERY
);
84 if (!rTimeZone
.isEmpty())
86 /* XXX NOTE: currently (2019-06-19) calendar implementations derive
87 * from Calendar_gregorian, even Hijri and Jewish. If that should
88 * change in future this should be adapted. */
89 Calendar_gregorian
* pCal
= dynamic_cast<Calendar_gregorian
*>(xCalendar
.get());
90 bTimeZone
= (pCal
&& pCal
->setTimeZone(rTimeZone
));
93 lookupTable
.emplace_back( aCacheID
, xCalendar
);
96 if ( !xCalendar
.is() )
98 xCalendar
= xOldCalendar
;
104 xCalendar
->loadCalendar(uniqueID
, rLocale
);
107 { // restore previous calendar and re-throw
108 xCalendar
= xOldCalendar
;
113 // The calendar is usable but is not in the expected time zone.
118 CalendarImpl::getLoadedCalendar2()
122 return xCalendar
->getLoadedCalendar2();
125 ::css::i18n::Calendar SAL_CALL
126 CalendarImpl::getLoadedCalendar()
130 return xCalendar
->getLoadedCalendar();
133 Sequence
< OUString
> SAL_CALL
134 CalendarImpl::getAllCalendars( const css::lang::Locale
& rLocale
)
136 const Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
137 Sequence
< OUString
> xSeq( xC
.getLength() );
138 std::transform(xC
.begin(), xC
.end(), xSeq
.begin(),
139 [](const Calendar2
& rCal
) { return rCal
.Name
; });
144 CalendarImpl::setDateTime( double fTimeInDays
)
148 xCalendar
->setDateTime( fTimeInDays
);
152 CalendarImpl::getDateTime()
156 return xCalendar
->getDateTime();
160 CalendarImpl::setLocalDateTime( double fTimeInDays
)
164 xCalendar
->setLocalDateTime( fTimeInDays
);
168 CalendarImpl::getLocalDateTime()
172 return xCalendar
->getLocalDateTime();
175 void SAL_CALL
CalendarImpl::loadDefaultCalendar( const css::lang::Locale
& rLocale
)
177 loadDefaultCalendarTZ( rLocale
, OUString());
180 void SAL_CALL
CalendarImpl::loadCalendar( const OUString
& uniqueID
, const css::lang::Locale
& rLocale
)
182 loadCalendarTZ( uniqueID
, rLocale
, OUString());
186 CalendarImpl::getUniqueID()
190 return xCalendar
->getUniqueID();
194 CalendarImpl::setValue( sal_Int16 fieldIndex
, sal_Int16 value
)
198 xCalendar
->setValue( fieldIndex
, value
);
202 CalendarImpl::getValue( sal_Int16 fieldIndex
)
206 return xCalendar
->getValue( fieldIndex
);
210 CalendarImpl::addValue( sal_Int16 fieldIndex
, sal_Int32 amount
)
214 xCalendar
->addValue( fieldIndex
, amount
);
218 CalendarImpl::getFirstDayOfWeek()
222 return xCalendar
->getFirstDayOfWeek();
226 CalendarImpl::setFirstDayOfWeek( sal_Int16 day
)
230 xCalendar
->setFirstDayOfWeek(day
);
234 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
)
238 xCalendar
->setMinimumNumberOfDaysForFirstWeek(days
);
242 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
246 return xCalendar
->getMinimumNumberOfDaysForFirstWeek();
251 CalendarImpl::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
)
255 return xCalendar
->getDisplayName( displayIndex
, idx
, nameType
);
259 CalendarImpl::getNumberOfMonthsInYear()
263 return xCalendar
->getNumberOfMonthsInYear();
268 CalendarImpl::getNumberOfDaysInWeek()
272 return xCalendar
->getNumberOfDaysInWeek();
276 Sequence
< CalendarItem
> SAL_CALL
277 CalendarImpl::getDays()
281 return xCalendar
->getDays();
285 Sequence
< CalendarItem
> SAL_CALL
286 CalendarImpl::getMonths()
290 return xCalendar
->getMonths();
294 Sequence
< CalendarItem2
> SAL_CALL
295 CalendarImpl::getDays2()
299 return xCalendar
->getDays2();
303 Sequence
< CalendarItem2
> SAL_CALL
304 CalendarImpl::getMonths2()
308 return xCalendar
->getMonths2();
312 Sequence
< CalendarItem2
> SAL_CALL
313 CalendarImpl::getGenitiveMonths2()
317 return xCalendar
->getGenitiveMonths2();
321 Sequence
< CalendarItem2
> SAL_CALL
322 CalendarImpl::getPartitiveMonths2()
326 return xCalendar
->getPartitiveMonths2();
331 CalendarImpl::isValid()
335 return xCalendar
->isValid();
339 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
343 return xCalendar
->getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
347 CalendarImpl::getImplementationName()
349 return "com.sun.star.i18n.CalendarImpl";
353 CalendarImpl::supportsService(const OUString
& rServiceName
)
355 return cppu::supportsService(this, rServiceName
);
358 Sequence
< OUString
> SAL_CALL
359 CalendarImpl::getSupportedServiceNames()
361 return { "com.sun.star.i18n.LocaleCalendar", "com.sun.star.i18n.LocaleCalendar2" };
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */