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>
21 #include <calendar_gregorian.hxx>
22 #include <localedata.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25
26 #include <com/sun/star/uno/XComponentContext.hpp>
27
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::i18n;
30
31 namespace i18npool {
32
CalendarImpl()33 CalendarImpl::CalendarImpl() : m_xContext(comphelper::getProcessComponentContext())
34 {
35 }
36
CalendarImpl(const Reference<XComponentContext> & rxContext)37 CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
38 {
39 if (!m_xContext.is())
40 throw RuntimeException("CalendarImpl::CalendarImpl: empty m_xContext");
41 }
42
~CalendarImpl()43 CalendarImpl::~CalendarImpl()
44 {
45 }
46
47 void SAL_CALL
loadDefaultCalendarTZ(const css::lang::Locale & rLocale,const OUString & rTimeZone)48 CalendarImpl::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
57 void SAL_CALL
loadCalendarTZ(const OUString & uniqueID,const css::lang::Locale & rLocale,const OUString & rTimeZone)58 CalendarImpl::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
121 Calendar2 SAL_CALL
getLoadedCalendar2()122 CalendarImpl::getLoadedCalendar2()
123 {
124 if (!xCalendar.is())
125 throw RuntimeException("CalendarImpl::getLoadedCalendar2: no calendar");
126 return xCalendar->getLoadedCalendar2();
127 }
128
129 ::css::i18n::Calendar SAL_CALL
getLoadedCalendar()130 CalendarImpl::getLoadedCalendar()
131 {
132 if (!xCalendar.is())
133 throw RuntimeException("CalendarImpl::getLoadedCalendar: no calendar");
134 return xCalendar->getLoadedCalendar();
135 }
136
137 Sequence< OUString > SAL_CALL
getAllCalendars(const css::lang::Locale & rLocale)138 CalendarImpl::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.begin(),
143 [](const Calendar2& rCal) { return rCal.Name; });
144 return xSeq;
145 }
146
147 void SAL_CALL
setDateTime(double fTimeInDays)148 CalendarImpl::setDateTime( double fTimeInDays )
149 {
150 if (!xCalendar.is())
151 throw RuntimeException("CalendarImpl::setDateTime: no calendar");
152 xCalendar->setDateTime( fTimeInDays );
153 }
154
155 double SAL_CALL
getDateTime()156 CalendarImpl::getDateTime()
157 {
158 if (!xCalendar.is())
159 throw RuntimeException("CalendarImpl::getDateTime: no calendar");
160 return xCalendar->getDateTime();
161 }
162
163 void SAL_CALL
setLocalDateTime(double fTimeInDays)164 CalendarImpl::setLocalDateTime( double fTimeInDays )
165 {
166 if (!xCalendar.is())
167 throw RuntimeException("CalendarImpl::setLocalDateTime: no calendar");
168 xCalendar->setLocalDateTime( fTimeInDays );
169 }
170
171 double SAL_CALL
getLocalDateTime()172 CalendarImpl::getLocalDateTime()
173 {
174 if (!xCalendar.is())
175 throw RuntimeException("CalendarImpl::getLocalDateTime: no calendar");
176 return xCalendar->getLocalDateTime();
177 }
178
loadDefaultCalendar(const css::lang::Locale & rLocale)179 void SAL_CALL CalendarImpl::loadDefaultCalendar( const css::lang::Locale& rLocale )
180 {
181 loadDefaultCalendarTZ( rLocale, OUString());
182 }
183
loadCalendar(const OUString & uniqueID,const css::lang::Locale & rLocale)184 void SAL_CALL CalendarImpl::loadCalendar( const OUString& uniqueID, const css::lang::Locale& rLocale )
185 {
186 loadCalendarTZ( uniqueID, rLocale, OUString());
187 }
188
189 OUString SAL_CALL
getUniqueID()190 CalendarImpl::getUniqueID()
191 {
192 if (!xCalendar.is())
193 throw RuntimeException("CalendarImpl::getUniqueID: no calendar");
194 return xCalendar->getUniqueID();
195 }
196
197 void SAL_CALL
setValue(sal_Int16 fieldIndex,sal_Int16 value)198 CalendarImpl::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
205 sal_Int16 SAL_CALL
getValue(sal_Int16 fieldIndex)206 CalendarImpl::getValue( sal_Int16 fieldIndex )
207 {
208 if (!xCalendar.is())
209 throw RuntimeException("CalendarImpl::getValue: no calendar");
210 return xCalendar->getValue( fieldIndex );
211 }
212
213 void SAL_CALL
addValue(sal_Int16 fieldIndex,sal_Int32 amount)214 CalendarImpl::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
221 sal_Int16 SAL_CALL
getFirstDayOfWeek()222 CalendarImpl::getFirstDayOfWeek()
223 {
224 if (!xCalendar.is())
225 throw RuntimeException("CalendarImpl::getFirstDayOfWeek: no calendar");
226 return xCalendar->getFirstDayOfWeek();
227 }
228
229 void SAL_CALL
setFirstDayOfWeek(sal_Int16 day)230 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
231 {
232 if (!xCalendar.is())
233 throw RuntimeException("CalendarImpl::setFirstDayOfWeek: no calendar");
234 xCalendar->setFirstDayOfWeek(day);
235 }
236
237 void SAL_CALL
setMinimumNumberOfDaysForFirstWeek(sal_Int16 days)238 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
239 {
240 if (!xCalendar.is())
241 throw RuntimeException("CalendarImpl::setMinimumNumberOfDaysForFirstWeek: no calendar");
242 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
243 }
244
245 sal_Int16 SAL_CALL
getMinimumNumberOfDaysForFirstWeek()246 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
247 {
248 if (!xCalendar.is())
249 throw RuntimeException("CalendarImpl::getMinimumNumberOfDaysForFirstWeek: no calendar");
250 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
251 }
252
253
254 OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)255 CalendarImpl::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
262 sal_Int16 SAL_CALL
getNumberOfMonthsInYear()263 CalendarImpl::getNumberOfMonthsInYear()
264 {
265 if (!xCalendar.is())
266 throw RuntimeException("CalendarImpl::setDisplayName: no calendar");
267 return xCalendar->getNumberOfMonthsInYear();
268 }
269
270
271 sal_Int16 SAL_CALL
getNumberOfDaysInWeek()272 CalendarImpl::getNumberOfDaysInWeek()
273 {
274 if (!xCalendar.is())
275 throw RuntimeException("CalendarImpl::getNumberOfDaysInWeek: no calendar");
276 return xCalendar->getNumberOfDaysInWeek();
277 }
278
279
280 Sequence< CalendarItem > SAL_CALL
getDays()281 CalendarImpl::getDays()
282 {
283 if (!xCalendar.is())
284 throw RuntimeException("CalendarImpl::setNumberOfDaysInWeek: no calendar");
285 return xCalendar->getDays();
286 }
287
288
289 Sequence< CalendarItem > SAL_CALL
getMonths()290 CalendarImpl::getMonths()
291 {
292 if (!xCalendar.is())
293 throw RuntimeException("CalendarImpl::getMonths: no calendar");
294 return xCalendar->getMonths();
295 }
296
297
298 Sequence< CalendarItem2 > SAL_CALL
getDays2()299 CalendarImpl::getDays2()
300 {
301 if (!xCalendar.is())
302 throw RuntimeException("CalendarImpl::getDays2: no calendar");
303 return xCalendar->getDays2();
304 }
305
306
307 Sequence< CalendarItem2 > SAL_CALL
getMonths2()308 CalendarImpl::getMonths2()
309 {
310 if (!xCalendar.is())
311 throw RuntimeException("CalendarImpl::getMonths2: no calendar");
312 return xCalendar->getMonths2();
313 }
314
315
316 Sequence< CalendarItem2 > SAL_CALL
getGenitiveMonths2()317 CalendarImpl::getGenitiveMonths2()
318 {
319 if (!xCalendar.is())
320 throw RuntimeException("CalendarImpl::getGenitiveMonths2: no calendar");
321 return xCalendar->getGenitiveMonths2();
322 }
323
324
325 Sequence< CalendarItem2 > SAL_CALL
getPartitiveMonths2()326 CalendarImpl::getPartitiveMonths2()
327 {
328 if (!xCalendar.is())
329 throw RuntimeException("CalendarImpl::getPartitiveMonths2: no calendar");
330 return xCalendar->getPartitiveMonths2();
331 }
332
333
334 sal_Bool SAL_CALL
isValid()335 CalendarImpl::isValid()
336 {
337 if (!xCalendar.is())
338 throw RuntimeException("CalendarImpl::isValid: no calendar");
339 return xCalendar->isValid();
340 }
341
342 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)343 CalendarImpl::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
350 OUString SAL_CALL
getImplementationName()351 CalendarImpl::getImplementationName()
352 {
353 return "com.sun.star.i18n.CalendarImpl";
354 }
355
356 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)357 CalendarImpl::supportsService(const OUString& rServiceName)
358 {
359 return cppu::supportsService(this, rServiceName);
360 }
361
362 Sequence< OUString > SAL_CALL
getSupportedServiceNames()363 CalendarImpl::getSupportedServiceNames()
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: */
371