Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / calendar / calendarImpl.cxx
bloba78a4476f4f01e811d64ec328acb9a08a59685e4
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 .
21 #include "calendarImpl.hxx"
22 #include "localedata.hxx"
23 #include <comphelper/processfactory.hxx>
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::lang;
27 using ::rtl::OUString;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 #define ERROR RuntimeException()
33 CalendarImpl::CalendarImpl(const Reference< XMultiServiceFactory > &rxMSF) : xMSF(rxMSF)
37 CalendarImpl::~CalendarImpl()
39 // Clear lookuptable
40 for (size_t l = 0; l < lookupTable.size(); l++)
41 delete lookupTable[l];
42 lookupTable.clear();
46 void SAL_CALL
47 CalendarImpl::loadDefaultCalendar( const Locale& rLocale ) throw(RuntimeException)
49 Sequence< Calendar2 > xC = LocaleData().getAllCalendars2(rLocale);
50 for (sal_Int32 i = 0; i < xC.getLength(); i++) {
51 if (xC[i].Default) {
52 loadCalendar(xC[i].Name, rLocale);
53 return;
56 throw ERROR;
59 void SAL_CALL
60 CalendarImpl::loadCalendar(const OUString& uniqueID, const Locale& rLocale ) throw (RuntimeException)
62 Reference < XCalendar3 > xOldCalendar( xCalendar ); // backup
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 (uniqueID == listItem->uniqueID) {
68 xCalendar = listItem->xCalendar;
69 break;
73 if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
74 Reference < XInterface > xI = xMSF->createInstance(
75 OUString("com.sun.star.i18n.Calendar_") + uniqueID);
77 if ( ! xI.is() ) {
78 // check if the calendar is defined in localedata, load gregorian calendar service.
79 Sequence< Calendar2 > xC = LocaleData().getAllCalendars2(rLocale);
80 for (i = 0; i < xC.getLength(); i++) {
81 if (uniqueID == xC[i].Name) {
82 xI = xMSF->createInstance(
83 OUString("com.sun.star.i18n.Calendar_gregorian"));
84 break;
89 if ( xI.is() )
90 xI->queryInterface(::getCppuType((const Reference< XCalendar3>*)0)) >>= xCalendar;
91 else
92 throw ERROR;
94 lookupTable.push_back( new lookupTableItem(uniqueID, xCalendar) );
97 if ( !xCalendar.is() )
99 xCalendar = xOldCalendar;
100 throw ERROR;
105 xCalendar->loadCalendar(uniqueID, rLocale);
107 catch ( Exception& )
108 { // restore previous calendar and re-throw
109 xCalendar = xOldCalendar;
110 throw;
114 Calendar2 SAL_CALL
115 CalendarImpl::getLoadedCalendar2() throw(RuntimeException)
117 if (xCalendar.is())
118 return xCalendar->getLoadedCalendar2();
119 else
120 throw ERROR ;
123 Calendar SAL_CALL
124 CalendarImpl::getLoadedCalendar() throw(RuntimeException)
126 if (xCalendar.is())
127 return xCalendar->getLoadedCalendar();
128 else
129 throw ERROR ;
132 Sequence< OUString > SAL_CALL
133 CalendarImpl::getAllCalendars( const Locale& rLocale ) throw(RuntimeException)
135 Sequence< Calendar2 > xC = LocaleData().getAllCalendars2(rLocale);
136 sal_Int32 nLen = xC.getLength();
137 Sequence< OUString > xSeq( nLen );
138 for (sal_Int32 i = 0; i < nLen; i++)
139 xSeq[i] = xC[i].Name;
140 return xSeq;
143 void SAL_CALL
144 CalendarImpl::setDateTime( double timeInDays ) throw(RuntimeException)
146 if (xCalendar.is())
147 xCalendar->setDateTime( timeInDays );
148 else
149 throw ERROR ;
152 double SAL_CALL
153 CalendarImpl::getDateTime() throw(RuntimeException)
155 if (xCalendar.is())
156 return xCalendar->getDateTime();
157 else
158 throw ERROR ;
161 OUString SAL_CALL
162 CalendarImpl::getUniqueID() throw(RuntimeException)
164 if (xCalendar.is())
165 return xCalendar->getUniqueID();
166 else
167 throw ERROR ;
170 void SAL_CALL
171 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException)
173 if (xCalendar.is())
174 xCalendar->setValue( fieldIndex, value );
175 else
176 throw ERROR ;
179 sal_Int16 SAL_CALL
180 CalendarImpl::getValue( sal_Int16 fieldIndex ) throw(RuntimeException)
182 if (xCalendar.is())
183 return xCalendar->getValue( fieldIndex );
184 else
185 throw ERROR ;
188 void SAL_CALL
189 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount ) throw(RuntimeException)
191 if (xCalendar.is())
192 xCalendar->addValue( fieldIndex, amount);
193 else
194 throw ERROR ;
197 sal_Int16 SAL_CALL
198 CalendarImpl::getFirstDayOfWeek() throw(RuntimeException)
200 if (xCalendar.is())
201 return xCalendar->getFirstDayOfWeek();
202 else
203 throw ERROR ;
206 void SAL_CALL
207 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
208 throw(RuntimeException)
210 if (xCalendar.is())
211 xCalendar->setFirstDayOfWeek(day);
212 else
213 throw ERROR ;
216 void SAL_CALL
217 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException)
219 if (xCalendar.is())
220 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
221 else
222 throw ERROR ;
225 sal_Int16 SAL_CALL
226 CalendarImpl::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException)
228 if (xCalendar.is())
229 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
230 else
231 throw ERROR ;
235 OUString SAL_CALL
236 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException)
238 if (xCalendar.is())
239 return xCalendar->getDisplayName( displayIndex, idx, nameType );
240 else
241 throw ERROR ;
244 sal_Int16 SAL_CALL
245 CalendarImpl::getNumberOfMonthsInYear() throw(RuntimeException)
247 if (xCalendar.is())
248 return xCalendar->getNumberOfMonthsInYear();
249 else
250 throw ERROR ;
254 sal_Int16 SAL_CALL
255 CalendarImpl::getNumberOfDaysInWeek() throw(RuntimeException)
257 if (xCalendar.is())
258 return xCalendar->getNumberOfDaysInWeek();
259 else
260 throw ERROR ;
264 Sequence< CalendarItem > SAL_CALL
265 CalendarImpl::getDays() throw(RuntimeException)
267 if (xCalendar.is())
268 return xCalendar->getDays();
269 else
270 throw ERROR ;
274 Sequence< CalendarItem > SAL_CALL
275 CalendarImpl::getMonths() throw(RuntimeException)
277 if (xCalendar.is())
278 return xCalendar->getMonths();
279 else
280 throw ERROR ;
284 Sequence< CalendarItem2 > SAL_CALL
285 CalendarImpl::getDays2() throw(RuntimeException)
287 if (xCalendar.is())
288 return xCalendar->getDays2();
289 else
290 throw ERROR ;
294 Sequence< CalendarItem2 > SAL_CALL
295 CalendarImpl::getMonths2() throw(RuntimeException)
297 if (xCalendar.is())
298 return xCalendar->getMonths2();
299 else
300 throw ERROR ;
304 Sequence< CalendarItem2 > SAL_CALL
305 CalendarImpl::getGenitiveMonths2() throw(RuntimeException)
307 if (xCalendar.is())
308 return xCalendar->getGenitiveMonths2();
309 else
310 throw ERROR ;
314 Sequence< CalendarItem2 > SAL_CALL
315 CalendarImpl::getPartitiveMonths2() throw(RuntimeException)
317 if (xCalendar.is())
318 return xCalendar->getPartitiveMonths2();
319 else
320 throw ERROR ;
324 sal_Bool SAL_CALL
325 CalendarImpl::isValid() throw(RuntimeException)
327 if (xCalendar.is())
328 return xCalendar->isValid();
329 else
330 throw ERROR ;
333 OUString SAL_CALL
334 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
335 throw (RuntimeException)
337 if (xCalendar.is())
338 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
339 else
340 throw ERROR ;
343 OUString SAL_CALL
344 CalendarImpl::getImplementationName(void) throw( RuntimeException )
346 return OUString("com.sun.star.i18n.CalendarImpl");
349 const sal_Char cCalendar[] = "com.sun.star.i18n.LocaleCalendar";
351 sal_Bool SAL_CALL
352 CalendarImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
354 return !rServiceName.compareToAscii(cCalendar);
357 Sequence< OUString > SAL_CALL
358 CalendarImpl::getSupportedServiceNames(void) throw( RuntimeException )
360 Sequence< OUString > aRet(1);
361 aRet[0] = OUString::createFromAscii(cCalendar);
362 return aRet;
365 }}}}
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */