Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18npool / source / calendar / calendarImpl.cxx
blob2cb6595604899e4075eca3799fcede3abd9fa5c9
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 <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;
31 namespace i18npool {
33 #define ERROR RuntimeException()
35 CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
39 CalendarImpl::~CalendarImpl()
43 void SAL_CALL
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; });
48 if (pCal == xC.end())
49 throw ERROR;
50 loadCalendarTZ(pCal->Name, rLocale, rTimeZone);
53 void SAL_CALL
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;
59 sal_Int32 i;
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;
65 break;
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);
73 if ( ! xI.is() ) {
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);
80 if ( !xI.is() )
81 throw ERROR;
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;
99 throw ERROR;
104 xCalendar->loadCalendar(uniqueID, rLocale);
106 catch ( Exception& )
107 { // restore previous calendar and re-throw
108 xCalendar = xOldCalendar;
109 throw;
112 if (!bTimeZone)
113 // The calendar is usable but is not in the expected time zone.
114 throw ERROR;
117 Calendar2 SAL_CALL
118 CalendarImpl::getLoadedCalendar2()
120 if (!xCalendar.is())
121 throw ERROR;
122 return xCalendar->getLoadedCalendar2();
125 ::css::i18n::Calendar SAL_CALL
126 CalendarImpl::getLoadedCalendar()
128 if (!xCalendar.is())
129 throw ERROR;
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; });
140 return xSeq;
143 void SAL_CALL
144 CalendarImpl::setDateTime( double fTimeInDays )
146 if (!xCalendar.is())
147 throw ERROR;
148 xCalendar->setDateTime( fTimeInDays );
151 double SAL_CALL
152 CalendarImpl::getDateTime()
154 if (!xCalendar.is())
155 throw ERROR;
156 return xCalendar->getDateTime();
159 void SAL_CALL
160 CalendarImpl::setLocalDateTime( double fTimeInDays )
162 if (!xCalendar.is())
163 throw ERROR;
164 xCalendar->setLocalDateTime( fTimeInDays );
167 double SAL_CALL
168 CalendarImpl::getLocalDateTime()
170 if (!xCalendar.is())
171 throw ERROR;
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());
185 OUString SAL_CALL
186 CalendarImpl::getUniqueID()
188 if (!xCalendar.is())
189 throw ERROR;
190 return xCalendar->getUniqueID();
193 void SAL_CALL
194 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value )
196 if (!xCalendar.is())
197 throw ERROR;
198 xCalendar->setValue( fieldIndex, value );
201 sal_Int16 SAL_CALL
202 CalendarImpl::getValue( sal_Int16 fieldIndex )
204 if (!xCalendar.is())
205 throw ERROR;
206 return xCalendar->getValue( fieldIndex );
209 void SAL_CALL
210 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount )
212 if (!xCalendar.is())
213 throw ERROR;
214 xCalendar->addValue( fieldIndex, amount);
217 sal_Int16 SAL_CALL
218 CalendarImpl::getFirstDayOfWeek()
220 if (!xCalendar.is())
221 throw ERROR;
222 return xCalendar->getFirstDayOfWeek();
225 void SAL_CALL
226 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
228 if (!xCalendar.is())
229 throw ERROR;
230 xCalendar->setFirstDayOfWeek(day);
233 void SAL_CALL
234 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
236 if (!xCalendar.is())
237 throw ERROR;
238 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
241 sal_Int16 SAL_CALL
242 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
244 if (!xCalendar.is())
245 throw ERROR;
246 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
250 OUString SAL_CALL
251 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
253 if (!xCalendar.is())
254 throw ERROR;
255 return xCalendar->getDisplayName( displayIndex, idx, nameType );
258 sal_Int16 SAL_CALL
259 CalendarImpl::getNumberOfMonthsInYear()
261 if (!xCalendar.is())
262 throw ERROR;
263 return xCalendar->getNumberOfMonthsInYear();
267 sal_Int16 SAL_CALL
268 CalendarImpl::getNumberOfDaysInWeek()
270 if (!xCalendar.is())
271 throw ERROR;
272 return xCalendar->getNumberOfDaysInWeek();
276 Sequence< CalendarItem > SAL_CALL
277 CalendarImpl::getDays()
279 if (!xCalendar.is())
280 throw ERROR;
281 return xCalendar->getDays();
285 Sequence< CalendarItem > SAL_CALL
286 CalendarImpl::getMonths()
288 if (!xCalendar.is())
289 throw ERROR;
290 return xCalendar->getMonths();
294 Sequence< CalendarItem2 > SAL_CALL
295 CalendarImpl::getDays2()
297 if (!xCalendar.is())
298 throw ERROR;
299 return xCalendar->getDays2();
303 Sequence< CalendarItem2 > SAL_CALL
304 CalendarImpl::getMonths2()
306 if (!xCalendar.is())
307 throw ERROR;
308 return xCalendar->getMonths2();
312 Sequence< CalendarItem2 > SAL_CALL
313 CalendarImpl::getGenitiveMonths2()
315 if (!xCalendar.is())
316 throw ERROR;
317 return xCalendar->getGenitiveMonths2();
321 Sequence< CalendarItem2 > SAL_CALL
322 CalendarImpl::getPartitiveMonths2()
324 if (!xCalendar.is())
325 throw ERROR;
326 return xCalendar->getPartitiveMonths2();
330 sal_Bool SAL_CALL
331 CalendarImpl::isValid()
333 if (!xCalendar.is())
334 throw ERROR;
335 return xCalendar->isValid();
338 OUString SAL_CALL
339 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
341 if (!xCalendar.is())
342 throw ERROR;
343 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
346 OUString SAL_CALL
347 CalendarImpl::getImplementationName()
349 return "com.sun.star.i18n.CalendarImpl";
352 sal_Bool SAL_CALL
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: */