Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / i18npool / source / calendar / calendarImpl.cxx
blob48c4ad2d02a39bed182dfcc29c0cb60868d4673e
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 "localedata.hxx"
22 #include <comphelper/processfactory.hxx>
23 #include <cppuhelper/supportsservice.hxx>
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::lang;
28 namespace com { namespace sun { namespace star { namespace i18n {
30 #define ERROR RuntimeException()
32 CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
36 CalendarImpl::~CalendarImpl()
38 // Clear lookuptable
39 for (lookupTableItem* p : lookupTable)
40 delete p;
41 lookupTable.clear();
44 void SAL_CALL
45 CalendarImpl::loadDefaultCalendar( const Locale& rLocale ) throw(RuntimeException, std::exception)
47 Sequence< Calendar2 > xC = LocaleDataImpl().getAllCalendars2(rLocale);
48 for (sal_Int32 i = 0; i < xC.getLength(); i++) {
49 if (xC[i].Default) {
50 loadCalendar(xC[i].Name, rLocale);
51 return;
54 throw ERROR;
57 void SAL_CALL
58 CalendarImpl::loadCalendar(const OUString& uniqueID, const Locale& rLocale ) throw (RuntimeException, std::exception)
60 Reference < XCalendar4 > xOldCalendar( xCalendar ); // backup
61 sal_Int32 i;
63 for (i = 0; i < sal::static_int_cast<sal_Int32>(lookupTable.size()); i++) {
64 lookupTableItem *listItem = lookupTable[i];
65 if (uniqueID == listItem->uniqueID) {
66 xCalendar = listItem->xCalendar;
67 break;
71 if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
72 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
73 "com.sun.star.i18n.Calendar_" + uniqueID, m_xContext);
75 if ( ! xI.is() ) {
76 // check if the calendar is defined in localedata, load gregorian calendar service.
77 Sequence< Calendar2 > xC = LocaleDataImpl().getAllCalendars2(rLocale);
78 for (i = 0; i < xC.getLength(); i++) {
79 if (uniqueID == xC[i].Name) {
80 xI = m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext);
81 break;
86 if ( xI.is() )
87 xCalendar.set(xI, UNO_QUERY);
88 else
89 throw ERROR;
91 lookupTable.push_back( new lookupTableItem(uniqueID, xCalendar) );
94 if ( !xCalendar.is() )
96 xCalendar = xOldCalendar;
97 throw ERROR;
102 xCalendar->loadCalendar(uniqueID, rLocale);
104 catch ( Exception& )
105 { // restore previous calendar and re-throw
106 xCalendar = xOldCalendar;
107 throw;
111 Calendar2 SAL_CALL
112 CalendarImpl::getLoadedCalendar2() throw(RuntimeException, std::exception)
114 if (xCalendar.is())
115 return xCalendar->getLoadedCalendar2();
116 else
117 throw ERROR ;
120 Calendar SAL_CALL
121 CalendarImpl::getLoadedCalendar() throw(RuntimeException, std::exception)
123 if (xCalendar.is())
124 return xCalendar->getLoadedCalendar();
125 else
126 throw ERROR ;
129 Sequence< OUString > SAL_CALL
130 CalendarImpl::getAllCalendars( const Locale& rLocale ) throw(RuntimeException, std::exception)
132 Sequence< Calendar2 > xC = LocaleDataImpl().getAllCalendars2(rLocale);
133 sal_Int32 nLen = xC.getLength();
134 Sequence< OUString > xSeq( nLen );
135 for (sal_Int32 i = 0; i < nLen; i++)
136 xSeq[i] = xC[i].Name;
137 return xSeq;
140 void SAL_CALL
141 CalendarImpl::setDateTime( double fTimeInDays ) throw(RuntimeException, std::exception)
143 if (xCalendar.is())
144 xCalendar->setDateTime( fTimeInDays );
145 else
146 throw ERROR ;
149 double SAL_CALL
150 CalendarImpl::getDateTime() throw(RuntimeException, std::exception)
152 if (xCalendar.is())
153 return xCalendar->getDateTime();
154 else
155 throw ERROR ;
158 void SAL_CALL
159 CalendarImpl::setLocalDateTime( double fTimeInDays ) throw(RuntimeException, std::exception)
161 if (xCalendar.is())
162 xCalendar->setLocalDateTime( fTimeInDays );
163 else
164 throw ERROR ;
167 double SAL_CALL
168 CalendarImpl::getLocalDateTime() throw(RuntimeException, std::exception)
170 if (xCalendar.is())
171 return xCalendar->getLocalDateTime();
172 else
173 throw ERROR ;
176 OUString SAL_CALL
177 CalendarImpl::getUniqueID() throw(RuntimeException, std::exception)
179 if (xCalendar.is())
180 return xCalendar->getUniqueID();
181 else
182 throw ERROR ;
185 void SAL_CALL
186 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException, std::exception)
188 if (xCalendar.is())
189 xCalendar->setValue( fieldIndex, value );
190 else
191 throw ERROR ;
194 sal_Int16 SAL_CALL
195 CalendarImpl::getValue( sal_Int16 fieldIndex ) throw(RuntimeException, std::exception)
197 if (xCalendar.is())
198 return xCalendar->getValue( fieldIndex );
199 else
200 throw ERROR ;
203 void SAL_CALL
204 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount ) throw(RuntimeException, std::exception)
206 if (xCalendar.is())
207 xCalendar->addValue( fieldIndex, amount);
208 else
209 throw ERROR ;
212 sal_Int16 SAL_CALL
213 CalendarImpl::getFirstDayOfWeek() throw(RuntimeException, std::exception)
215 if (xCalendar.is())
216 return xCalendar->getFirstDayOfWeek();
217 else
218 throw ERROR ;
221 void SAL_CALL
222 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
223 throw(RuntimeException, std::exception)
225 if (xCalendar.is())
226 xCalendar->setFirstDayOfWeek(day);
227 else
228 throw ERROR ;
231 void SAL_CALL
232 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException, std::exception)
234 if (xCalendar.is())
235 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
236 else
237 throw ERROR ;
240 sal_Int16 SAL_CALL
241 CalendarImpl::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException, std::exception)
243 if (xCalendar.is())
244 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
245 else
246 throw ERROR ;
250 OUString SAL_CALL
251 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException, std::exception)
253 if (xCalendar.is())
254 return xCalendar->getDisplayName( displayIndex, idx, nameType );
255 else
256 throw ERROR ;
259 sal_Int16 SAL_CALL
260 CalendarImpl::getNumberOfMonthsInYear() throw(RuntimeException, std::exception)
262 if (xCalendar.is())
263 return xCalendar->getNumberOfMonthsInYear();
264 else
265 throw ERROR ;
269 sal_Int16 SAL_CALL
270 CalendarImpl::getNumberOfDaysInWeek() throw(RuntimeException, std::exception)
272 if (xCalendar.is())
273 return xCalendar->getNumberOfDaysInWeek();
274 else
275 throw ERROR ;
279 Sequence< CalendarItem > SAL_CALL
280 CalendarImpl::getDays() throw(RuntimeException, std::exception)
282 if (xCalendar.is())
283 return xCalendar->getDays();
284 else
285 throw ERROR ;
289 Sequence< CalendarItem > SAL_CALL
290 CalendarImpl::getMonths() throw(RuntimeException, std::exception)
292 if (xCalendar.is())
293 return xCalendar->getMonths();
294 else
295 throw ERROR ;
299 Sequence< CalendarItem2 > SAL_CALL
300 CalendarImpl::getDays2() throw(RuntimeException, std::exception)
302 if (xCalendar.is())
303 return xCalendar->getDays2();
304 else
305 throw ERROR ;
309 Sequence< CalendarItem2 > SAL_CALL
310 CalendarImpl::getMonths2() throw(RuntimeException, std::exception)
312 if (xCalendar.is())
313 return xCalendar->getMonths2();
314 else
315 throw ERROR ;
319 Sequence< CalendarItem2 > SAL_CALL
320 CalendarImpl::getGenitiveMonths2() throw(RuntimeException, std::exception)
322 if (xCalendar.is())
323 return xCalendar->getGenitiveMonths2();
324 else
325 throw ERROR ;
329 Sequence< CalendarItem2 > SAL_CALL
330 CalendarImpl::getPartitiveMonths2() throw(RuntimeException, std::exception)
332 if (xCalendar.is())
333 return xCalendar->getPartitiveMonths2();
334 else
335 throw ERROR ;
339 sal_Bool SAL_CALL
340 CalendarImpl::isValid() throw(RuntimeException, std::exception)
342 if (xCalendar.is())
343 return xCalendar->isValid();
344 else
345 throw ERROR ;
348 OUString SAL_CALL
349 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
350 throw (RuntimeException, std::exception)
352 if (xCalendar.is())
353 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
354 else
355 throw ERROR ;
358 OUString SAL_CALL
359 CalendarImpl::getImplementationName() throw( RuntimeException, std::exception )
361 return OUString("com.sun.star.i18n.CalendarImpl");
364 sal_Bool SAL_CALL
365 CalendarImpl::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
367 return cppu::supportsService(this, rServiceName);
370 Sequence< OUString > SAL_CALL
371 CalendarImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
373 Sequence< OUString > aRet(2);
374 aRet[0] = "com.sun.star.i18n.LocaleCalendar";
375 aRet[1] = "com.sun.star.i18n.LocaleCalendar2";
376 return aRet;
379 }}}}
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */