merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / calendar / calendarImpl.cxx
blob1c7b8f1a1ad4384a72a8b50ccd1f272c8c2fd134
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: calendarImpl.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include "calendarImpl.hxx"
35 #include "localedata.hxx"
36 #include <comphelper/processfactory.hxx>
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::i18n;
41 using namespace ::rtl;
43 #define ERROR RuntimeException()
45 CalendarImpl::CalendarImpl(const Reference< XMultiServiceFactory > &rxMSF) : xMSF(rxMSF)
49 CalendarImpl::~CalendarImpl()
51 // Clear lookuptable
52 for (size_t l = 0; l < lookupTable.size(); l++)
53 delete lookupTable[l];
54 lookupTable.clear();
58 void SAL_CALL
59 CalendarImpl::loadDefaultCalendar( const Locale& rLocale ) throw(RuntimeException)
61 Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
62 for (sal_Int32 i = 0; i < xC.getLength(); i++) {
63 if (xC[i].Default) {
64 loadCalendar(xC[i].Name, rLocale);
65 return;
68 throw ERROR;
71 void SAL_CALL
72 CalendarImpl::loadCalendar(const OUString& uniqueID, const Locale& rLocale ) throw (RuntimeException)
74 Reference < XExtendedCalendar > xOldCalendar( xCalendar ); // backup
75 sal_Int32 i;
77 for (i = 0; i < sal::static_int_cast<sal_Int32>(lookupTable.size()); i++) {
78 lookupTableItem *listItem = lookupTable[i];
79 if (uniqueID == listItem->uniqueID) {
80 xCalendar = listItem->xCalendar;
81 break;
85 if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
86 Reference < XInterface > xI = xMSF->createInstance(
87 OUString::createFromAscii("com.sun.star.i18n.Calendar_") + uniqueID);
89 if ( ! xI.is() ) {
90 // check if the calendar is defined in localedata, load gregorian calendar service.
91 Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
92 for (i = 0; i < xC.getLength(); i++) {
93 if (uniqueID == xC[i].Name) {
94 xI = xMSF->createInstance(
95 OUString::createFromAscii("com.sun.star.i18n.Calendar_gregorian"));
96 break;
101 if ( xI.is() )
102 xI->queryInterface(::getCppuType((const Reference< XExtendedCalendar>*)0)) >>= xCalendar;
103 else
104 throw ERROR;
106 lookupTable.push_back( new lookupTableItem(uniqueID, xCalendar) );
109 if ( !xCalendar.is() )
111 xCalendar = xOldCalendar;
112 throw ERROR;
117 xCalendar->loadCalendar(uniqueID, rLocale);
119 catch ( Exception& )
120 { // restore previous calendar and re-throw
121 xCalendar = xOldCalendar;
122 throw;
126 Calendar SAL_CALL
127 CalendarImpl::getLoadedCalendar() throw(RuntimeException)
129 if (xCalendar.is())
130 return xCalendar->getLoadedCalendar();
131 else
132 throw ERROR ;
135 Sequence< OUString > SAL_CALL
136 CalendarImpl::getAllCalendars( const Locale& rLocale ) throw(RuntimeException)
138 Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
139 sal_Int32 nLen = xC.getLength();
140 Sequence< OUString > xSeq( nLen );
141 for (sal_Int32 i = 0; i < nLen; i++)
142 xSeq[i] = xC[i].Name;
143 return xSeq;
146 void SAL_CALL
147 CalendarImpl::setDateTime( double timeInDays ) throw(RuntimeException)
149 if (xCalendar.is())
150 xCalendar->setDateTime( timeInDays );
151 else
152 throw ERROR ;
155 double SAL_CALL
156 CalendarImpl::getDateTime() throw(RuntimeException)
158 if (xCalendar.is())
159 return xCalendar->getDateTime();
160 else
161 throw ERROR ;
164 OUString SAL_CALL
165 CalendarImpl::getUniqueID() throw(RuntimeException)
167 if (xCalendar.is())
168 return xCalendar->getUniqueID();
169 else
170 throw ERROR ;
173 void SAL_CALL
174 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException)
176 if (xCalendar.is())
177 xCalendar->setValue( fieldIndex, value );
178 else
179 throw ERROR ;
182 sal_Int16 SAL_CALL
183 CalendarImpl::getValue( sal_Int16 fieldIndex ) throw(RuntimeException)
185 if (xCalendar.is())
186 return xCalendar->getValue( fieldIndex );
187 else
188 throw ERROR ;
191 void SAL_CALL
192 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount ) throw(RuntimeException)
194 if (xCalendar.is())
195 xCalendar->addValue( fieldIndex, amount);
196 else
197 throw ERROR ;
200 sal_Int16 SAL_CALL
201 CalendarImpl::getFirstDayOfWeek() throw(RuntimeException)
203 if (xCalendar.is())
204 return xCalendar->getFirstDayOfWeek();
205 else
206 throw ERROR ;
209 void SAL_CALL
210 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
211 throw(RuntimeException)
213 if (xCalendar.is())
214 xCalendar->setFirstDayOfWeek(day);
215 else
216 throw ERROR ;
219 void SAL_CALL
220 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException)
222 if (xCalendar.is())
223 xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
224 else
225 throw ERROR ;
228 sal_Int16 SAL_CALL
229 CalendarImpl::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException)
231 if (xCalendar.is())
232 return xCalendar->getMinimumNumberOfDaysForFirstWeek();
233 else
234 throw ERROR ;
238 OUString SAL_CALL
239 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException)
241 if (xCalendar.is())
242 return xCalendar->getDisplayName( displayIndex, idx, nameType );
243 else
244 throw ERROR ;
247 sal_Int16 SAL_CALL
248 CalendarImpl::getNumberOfMonthsInYear() throw(RuntimeException)
250 if (xCalendar.is())
251 return xCalendar->getNumberOfMonthsInYear();
252 else
253 throw ERROR ;
257 sal_Int16 SAL_CALL
258 CalendarImpl::getNumberOfDaysInWeek() throw(RuntimeException)
260 if (xCalendar.is())
261 return xCalendar->getNumberOfDaysInWeek();
262 else
263 throw ERROR ;
267 Sequence< CalendarItem > SAL_CALL
268 CalendarImpl::getMonths() throw(RuntimeException)
270 if (xCalendar.is())
271 return xCalendar->getMonths();
272 else
273 throw ERROR ;
277 Sequence< CalendarItem > SAL_CALL
278 CalendarImpl::getDays() throw(RuntimeException)
280 if (xCalendar.is())
281 return xCalendar->getDays();
282 else
283 throw ERROR ;
287 sal_Bool SAL_CALL
288 CalendarImpl::isValid() throw(RuntimeException)
290 if (xCalendar.is())
291 return xCalendar->isValid();
292 else
293 throw ERROR ;
296 OUString SAL_CALL
297 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
298 throw (RuntimeException)
300 if (xCalendar.is())
301 return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
302 else
303 throw ERROR ;
306 OUString SAL_CALL
307 CalendarImpl::getImplementationName(void) throw( RuntimeException )
309 return OUString::createFromAscii("com.sun.star.i18n.CalendarImpl");
312 const sal_Char cCalendar[] = "com.sun.star.i18n.LocaleCalendar";
314 sal_Bool SAL_CALL
315 CalendarImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
317 return !rServiceName.compareToAscii(cCalendar);
320 Sequence< OUString > SAL_CALL
321 CalendarImpl::getSupportedServiceNames(void) throw( RuntimeException )
323 Sequence< OUString > aRet(1);
324 aRet[0] = OUString::createFromAscii(cCalendar);
325 return aRet;