Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / i18npool / source / calendar / calendarImpl.cxx
blob4fcaeba18224c7042037e52e34b77fa2de4e793d
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/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;
31 namespace i18npool {
33 CalendarImpl::CalendarImpl() : m_xContext(comphelper::getProcessComponentContext())
37 CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
39 if (!m_xContext.is())
40 throw RuntimeException(u"CalendarImpl::CalendarImpl: empty m_xContext"_ustr);
43 CalendarImpl::~CalendarImpl()
47 void SAL_CALL
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; });
52 if (pCal == xC.end())
53 throw RuntimeException(u"CalendarImpl::loadDefaultCalendarTZ: no default calendar found for this locale"_ustr);
54 loadCalendarTZ(pCal->Name, rLocale, rTimeZone);
57 void SAL_CALL
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;
63 sal_Int32 i;
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;
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);
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(u"com.sun.star.i18n.Calendar_gregorian"_ustr, m_xContext);
84 if ( !xI.is() )
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);
110 catch ( Exception& )
111 { // restore previous calendar and re-throw
112 xCalendar = std::move(xOldCalendar);
113 throw;
116 if (!bTimeZone)
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);
121 Calendar2 SAL_CALL
122 CalendarImpl::getLoadedCalendar2()
124 if (!xCalendar.is())
125 throw RuntimeException(u"CalendarImpl::getLoadedCalendar2: no calendar"_ustr);
126 return xCalendar->getLoadedCalendar2();
129 ::css::i18n::Calendar SAL_CALL
130 CalendarImpl::getLoadedCalendar()
132 if (!xCalendar.is())
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; });
144 return xSeq;
147 void SAL_CALL
148 CalendarImpl::setDateTime( double fTimeInDays )
150 if (!xCalendar.is())
151 throw RuntimeException(u"CalendarImpl::setDateTime: no calendar"_ustr);
152 xCalendar->setDateTime( fTimeInDays );
155 double SAL_CALL
156 CalendarImpl::getDateTime()
158 if (!xCalendar.is())
159 throw RuntimeException(u"CalendarImpl::getDateTime: no calendar"_ustr);
160 return xCalendar->getDateTime();
163 void SAL_CALL
164 CalendarImpl::setLocalDateTime( double fTimeInDays )
166 if (!xCalendar.is())
167 throw RuntimeException(u"CalendarImpl::setLocalDateTime: no calendar"_ustr);
168 xCalendar->setLocalDateTime( fTimeInDays );
171 double SAL_CALL
172 CalendarImpl::getLocalDateTime()
174 if (!xCalendar.is())
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());
189 OUString SAL_CALL
190 CalendarImpl::getUniqueID()
192 if (!xCalendar.is())
193 throw RuntimeException(u"CalendarImpl::getUniqueID: no calendar"_ustr);
194 return xCalendar->getUniqueID();
197 void SAL_CALL
198 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value )
200 if (!xCalendar.is())
201 throw RuntimeException(u"CalendarImpl::setValue: no calendar"_ustr);
202 xCalendar->setValue( fieldIndex, value );
205 sal_Int16 SAL_CALL
206 CalendarImpl::getValue( sal_Int16 fieldIndex )
208 if (!xCalendar.is())
209 throw RuntimeException(u"CalendarImpl::getValue: no calendar"_ustr);
210 return xCalendar->getValue( fieldIndex );
213 void SAL_CALL
214 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount )
216 if (!xCalendar.is())
217 throw RuntimeException(u"CalendarImpl::addValue: no calendar"_ustr);
218 xCalendar->addValue( fieldIndex, amount);
221 sal_Int16 SAL_CALL
222 CalendarImpl::getFirstDayOfWeek()
224 if (!xCalendar.is())
225 throw RuntimeException(u"CalendarImpl::getFirstDayOfWeek: no calendar"_ustr);
226 return xCalendar->getFirstDayOfWeek();
229 void SAL_CALL
230 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
232 if (!xCalendar.is())
233 throw RuntimeException(u"CalendarImpl::setFirstDayOfWeek: no calendar"_ustr);
234 xCalendar->setFirstDayOfWeek(day);
237 void SAL_CALL
238 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
240 if (!xCalendar.is())
241 throw RuntimeException(u"CalendarImpl::setMinimumNumberOfDaysForFirstWeek: no calendar"_ustr);
242 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
245 sal_Int16 SAL_CALL
246 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
248 if (!xCalendar.is())
249 throw RuntimeException(u"CalendarImpl::getMinimumNumberOfDaysForFirstWeek: no calendar"_ustr);
250 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
254 OUString SAL_CALL
255 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
257 if (!xCalendar.is())
258 throw RuntimeException(u"CalendarImpl::getDisplayName: no calendar"_ustr);
259 return xCalendar->getDisplayName( displayIndex, idx, nameType );
262 sal_Int16 SAL_CALL
263 CalendarImpl::getNumberOfMonthsInYear()
265 if (!xCalendar.is())
266 throw RuntimeException(u"CalendarImpl::setDisplayName: no calendar"_ustr);
267 return xCalendar->getNumberOfMonthsInYear();
271 sal_Int16 SAL_CALL
272 CalendarImpl::getNumberOfDaysInWeek()
274 if (!xCalendar.is())
275 throw RuntimeException(u"CalendarImpl::getNumberOfDaysInWeek: no calendar"_ustr);
276 return xCalendar->getNumberOfDaysInWeek();
280 Sequence< CalendarItem > SAL_CALL
281 CalendarImpl::getDays()
283 if (!xCalendar.is())
284 throw RuntimeException(u"CalendarImpl::setNumberOfDaysInWeek: no calendar"_ustr);
285 return xCalendar->getDays();
289 Sequence< CalendarItem > SAL_CALL
290 CalendarImpl::getMonths()
292 if (!xCalendar.is())
293 throw RuntimeException(u"CalendarImpl::getMonths: no calendar"_ustr);
294 return xCalendar->getMonths();
298 Sequence< CalendarItem2 > SAL_CALL
299 CalendarImpl::getDays2()
301 if (!xCalendar.is())
302 throw RuntimeException(u"CalendarImpl::getDays2: no calendar"_ustr);
303 return xCalendar->getDays2();
307 Sequence< CalendarItem2 > SAL_CALL
308 CalendarImpl::getMonths2()
310 if (!xCalendar.is())
311 throw RuntimeException(u"CalendarImpl::getMonths2: no calendar"_ustr);
312 return xCalendar->getMonths2();
316 Sequence< CalendarItem2 > SAL_CALL
317 CalendarImpl::getGenitiveMonths2()
319 if (!xCalendar.is())
320 throw RuntimeException(u"CalendarImpl::getGenitiveMonths2: no calendar"_ustr);
321 return xCalendar->getGenitiveMonths2();
325 Sequence< CalendarItem2 > SAL_CALL
326 CalendarImpl::getPartitiveMonths2()
328 if (!xCalendar.is())
329 throw RuntimeException(u"CalendarImpl::getPartitiveMonths2: no calendar"_ustr);
330 return xCalendar->getPartitiveMonths2();
334 sal_Bool SAL_CALL
335 CalendarImpl::isValid()
337 if (!xCalendar.is())
338 throw RuntimeException(u"CalendarImpl::isValid: no calendar"_ustr);
339 return xCalendar->isValid();
342 OUString SAL_CALL
343 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
345 if (!xCalendar.is())
346 throw RuntimeException(u"CalendarImpl::getDisplayString: no calendar"_ustr);
347 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
350 OUString SAL_CALL
351 CalendarImpl::getImplementationName()
353 return u"com.sun.star.i18n.CalendarImpl"_ustr;
356 sal_Bool SAL_CALL
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: */