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