1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "calendarImpl.hxx"
28cdf0e10cSrcweir #include "localedata.hxx"
29cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::lang;
33cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
34cdf0e10cSrcweir using namespace ::rtl;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #define ERROR RuntimeException()
37cdf0e10cSrcweir 
CalendarImpl(const Reference<XMultiServiceFactory> & rxMSF)38cdf0e10cSrcweir CalendarImpl::CalendarImpl(const Reference< XMultiServiceFactory > &rxMSF) : xMSF(rxMSF)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
~CalendarImpl()42cdf0e10cSrcweir CalendarImpl::~CalendarImpl()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     // Clear lookuptable
45cdf0e10cSrcweir     for (size_t l = 0; l < lookupTable.size(); l++)
46cdf0e10cSrcweir         delete lookupTable[l];
47cdf0e10cSrcweir     lookupTable.clear();
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir void SAL_CALL
loadDefaultCalendar(const Locale & rLocale)52cdf0e10cSrcweir CalendarImpl::loadDefaultCalendar( const Locale& rLocale ) throw(RuntimeException)
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
55cdf0e10cSrcweir     for (sal_Int32 i = 0; i < xC.getLength(); i++) {
56cdf0e10cSrcweir         if (xC[i].Default) {
57cdf0e10cSrcweir             loadCalendar(xC[i].Name, rLocale);
58cdf0e10cSrcweir             return;
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir     throw ERROR;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir void SAL_CALL
loadCalendar(const OUString & uniqueID,const Locale & rLocale)65cdf0e10cSrcweir CalendarImpl::loadCalendar(const OUString& uniqueID, const Locale& rLocale ) throw (RuntimeException)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     Reference < XExtendedCalendar > xOldCalendar( xCalendar );  // backup
68cdf0e10cSrcweir     sal_Int32 i;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     for (i = 0; i < sal::static_int_cast<sal_Int32>(lookupTable.size()); i++) {
71cdf0e10cSrcweir         lookupTableItem *listItem = lookupTable[i];
72cdf0e10cSrcweir         if (uniqueID == listItem->uniqueID) {
73cdf0e10cSrcweir             xCalendar = listItem->xCalendar;
74cdf0e10cSrcweir             break;
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
79cdf0e10cSrcweir         Reference < XInterface > xI = xMSF->createInstance(
80cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.i18n.Calendar_") + uniqueID);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         if ( ! xI.is() ) {
83cdf0e10cSrcweir             // check if the calendar is defined in localedata, load gregorian calendar service.
84cdf0e10cSrcweir             Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
85cdf0e10cSrcweir             for (i = 0; i < xC.getLength(); i++) {
86cdf0e10cSrcweir                 if (uniqueID == xC[i].Name) {
87cdf0e10cSrcweir                     xI = xMSF->createInstance(
88cdf0e10cSrcweir                         OUString::createFromAscii("com.sun.star.i18n.Calendar_gregorian"));
89cdf0e10cSrcweir                     break;
90cdf0e10cSrcweir                 }
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         if ( xI.is() )
95cdf0e10cSrcweir             xI->queryInterface(::getCppuType((const Reference< XExtendedCalendar>*)0)) >>= xCalendar;
96cdf0e10cSrcweir         else
97cdf0e10cSrcweir             throw ERROR;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         lookupTable.push_back( new lookupTableItem(uniqueID, xCalendar) );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     if ( !xCalendar.is() )
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         xCalendar = xOldCalendar;
105cdf0e10cSrcweir         throw ERROR;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     try
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         xCalendar->loadCalendar(uniqueID, rLocale);
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     catch ( Exception& )
113cdf0e10cSrcweir     {   // restore previous calendar and re-throw
114cdf0e10cSrcweir         xCalendar = xOldCalendar;
115cdf0e10cSrcweir         throw;
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir Calendar SAL_CALL
getLoadedCalendar()120cdf0e10cSrcweir CalendarImpl::getLoadedCalendar() throw(RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     if (xCalendar.is())
123cdf0e10cSrcweir         return xCalendar->getLoadedCalendar();
124cdf0e10cSrcweir     else
125cdf0e10cSrcweir         throw ERROR ;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getAllCalendars(const Locale & rLocale)129cdf0e10cSrcweir CalendarImpl::getAllCalendars( const Locale& rLocale ) throw(RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
132cdf0e10cSrcweir     sal_Int32 nLen = xC.getLength();
133cdf0e10cSrcweir     Sequence< OUString > xSeq( nLen );
134cdf0e10cSrcweir     for (sal_Int32 i = 0; i < nLen; i++)
135cdf0e10cSrcweir         xSeq[i] = xC[i].Name;
136cdf0e10cSrcweir     return xSeq;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir void SAL_CALL
setDateTime(double timeInDays)140cdf0e10cSrcweir CalendarImpl::setDateTime( double timeInDays ) throw(RuntimeException)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     if (xCalendar.is())
143cdf0e10cSrcweir         xCalendar->setDateTime( timeInDays );
144cdf0e10cSrcweir     else
145cdf0e10cSrcweir         throw ERROR ;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir double SAL_CALL
getDateTime()149cdf0e10cSrcweir CalendarImpl::getDateTime() throw(RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     if (xCalendar.is())
152cdf0e10cSrcweir         return xCalendar->getDateTime();
153cdf0e10cSrcweir     else
154cdf0e10cSrcweir         throw ERROR ;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir OUString SAL_CALL
getUniqueID()158cdf0e10cSrcweir CalendarImpl::getUniqueID() throw(RuntimeException)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     if (xCalendar.is())
161cdf0e10cSrcweir         return xCalendar->getUniqueID();
162cdf0e10cSrcweir     else
163cdf0e10cSrcweir         throw ERROR ;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir void SAL_CALL
setValue(sal_Int16 fieldIndex,sal_Int16 value)167cdf0e10cSrcweir CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     if (xCalendar.is())
170cdf0e10cSrcweir         xCalendar->setValue( fieldIndex, value );
171cdf0e10cSrcweir     else
172cdf0e10cSrcweir         throw ERROR ;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir sal_Int16 SAL_CALL
getValue(sal_Int16 fieldIndex)176cdf0e10cSrcweir CalendarImpl::getValue( sal_Int16 fieldIndex ) throw(RuntimeException)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     if (xCalendar.is())
179cdf0e10cSrcweir         return xCalendar->getValue( fieldIndex );
180cdf0e10cSrcweir     else
181cdf0e10cSrcweir         throw ERROR ;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir void SAL_CALL
addValue(sal_Int16 fieldIndex,sal_Int32 amount)185cdf0e10cSrcweir CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount ) throw(RuntimeException)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     if (xCalendar.is())
188cdf0e10cSrcweir         xCalendar->addValue( fieldIndex, amount);
189cdf0e10cSrcweir     else
190cdf0e10cSrcweir         throw ERROR ;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir sal_Int16 SAL_CALL
getFirstDayOfWeek()194cdf0e10cSrcweir CalendarImpl::getFirstDayOfWeek() throw(RuntimeException)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     if (xCalendar.is())
197cdf0e10cSrcweir         return xCalendar->getFirstDayOfWeek();
198cdf0e10cSrcweir     else
199cdf0e10cSrcweir         throw ERROR ;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir void SAL_CALL
setFirstDayOfWeek(sal_Int16 day)203cdf0e10cSrcweir CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
204cdf0e10cSrcweir throw(RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     if (xCalendar.is())
207cdf0e10cSrcweir         xCalendar->setFirstDayOfWeek(day);
208cdf0e10cSrcweir     else
209cdf0e10cSrcweir         throw ERROR ;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir void SAL_CALL
setMinimumNumberOfDaysForFirstWeek(sal_Int16 days)213cdf0e10cSrcweir CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException)
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     if (xCalendar.is())
216cdf0e10cSrcweir         xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
217cdf0e10cSrcweir     else
218cdf0e10cSrcweir         throw ERROR ;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir sal_Int16 SAL_CALL
getMinimumNumberOfDaysForFirstWeek()222cdf0e10cSrcweir CalendarImpl::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     if (xCalendar.is())
225cdf0e10cSrcweir         return xCalendar->getMinimumNumberOfDaysForFirstWeek();
226cdf0e10cSrcweir     else
227cdf0e10cSrcweir         throw ERROR ;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 
231cdf0e10cSrcweir OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)232cdf0e10cSrcweir CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException)
233cdf0e10cSrcweir {
234cdf0e10cSrcweir     if (xCalendar.is())
235cdf0e10cSrcweir         return xCalendar->getDisplayName( displayIndex, idx, nameType );
236cdf0e10cSrcweir     else
237cdf0e10cSrcweir         throw ERROR ;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir sal_Int16 SAL_CALL
getNumberOfMonthsInYear()241cdf0e10cSrcweir CalendarImpl::getNumberOfMonthsInYear() throw(RuntimeException)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir     if (xCalendar.is())
244cdf0e10cSrcweir         return xCalendar->getNumberOfMonthsInYear();
245cdf0e10cSrcweir     else
246cdf0e10cSrcweir         throw ERROR ;
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 
250cdf0e10cSrcweir sal_Int16 SAL_CALL
getNumberOfDaysInWeek()251cdf0e10cSrcweir CalendarImpl::getNumberOfDaysInWeek() throw(RuntimeException)
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     if (xCalendar.is())
254cdf0e10cSrcweir         return xCalendar->getNumberOfDaysInWeek();
255cdf0e10cSrcweir     else
256cdf0e10cSrcweir         throw ERROR ;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
260cdf0e10cSrcweir Sequence< CalendarItem > SAL_CALL
getMonths()261cdf0e10cSrcweir CalendarImpl::getMonths() throw(RuntimeException)
262cdf0e10cSrcweir {
263cdf0e10cSrcweir     if (xCalendar.is())
264cdf0e10cSrcweir         return xCalendar->getMonths();
265cdf0e10cSrcweir     else
266cdf0e10cSrcweir         throw ERROR ;
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 
270cdf0e10cSrcweir Sequence< CalendarItem > SAL_CALL
getDays()271cdf0e10cSrcweir CalendarImpl::getDays() throw(RuntimeException)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     if (xCalendar.is())
274cdf0e10cSrcweir         return xCalendar->getDays();
275cdf0e10cSrcweir     else
276cdf0e10cSrcweir         throw ERROR ;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 
280cdf0e10cSrcweir sal_Bool SAL_CALL
isValid()281cdf0e10cSrcweir CalendarImpl::isValid() throw(RuntimeException)
282cdf0e10cSrcweir {
283cdf0e10cSrcweir     if (xCalendar.is())
284cdf0e10cSrcweir         return xCalendar->isValid();
285cdf0e10cSrcweir     else
286cdf0e10cSrcweir         throw ERROR ;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)290cdf0e10cSrcweir CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
291cdf0e10cSrcweir 	throw (RuntimeException)
292cdf0e10cSrcweir {
293cdf0e10cSrcweir     if (xCalendar.is())
294cdf0e10cSrcweir         return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
295cdf0e10cSrcweir     else
296cdf0e10cSrcweir         throw ERROR ;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir OUString SAL_CALL
getImplementationName(void)300cdf0e10cSrcweir CalendarImpl::getImplementationName(void) throw( RuntimeException )
301cdf0e10cSrcweir {
302cdf0e10cSrcweir     return OUString::createFromAscii("com.sun.star.i18n.CalendarImpl");
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir const sal_Char cCalendar[] = "com.sun.star.i18n.LocaleCalendar";
306cdf0e10cSrcweir 
307cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)308cdf0e10cSrcweir CalendarImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
309cdf0e10cSrcweir {
310cdf0e10cSrcweir     return !rServiceName.compareToAscii(cCalendar);
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getSupportedServiceNames(void)314cdf0e10cSrcweir CalendarImpl::getSupportedServiceNames(void) throw( RuntimeException )
315cdf0e10cSrcweir {
316cdf0e10cSrcweir     Sequence< OUString > aRet(1);
317cdf0e10cSrcweir     aRet[0] = OUString::createFromAscii(cCalendar);
318cdf0e10cSrcweir     return aRet;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321