xref: /trunk/main/i18npool/source/calendar/calendarImpl.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
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 
38 CalendarImpl::CalendarImpl(const Reference< XMultiServiceFactory > &rxMSF) : xMSF(rxMSF)
39 {
40 }
41 
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
52 CalendarImpl::loadDefaultCalendar( const Locale& rLocale )
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
65 CalendarImpl::loadCalendar(const OUString& uniqueID, const Locale& rLocale )
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
120 CalendarImpl::getLoadedCalendar()
121 {
122     if (xCalendar.is())
123         return xCalendar->getLoadedCalendar();
124     else
125         throw ERROR ;
126 }
127 
128 Sequence< OUString > SAL_CALL
129 CalendarImpl::getAllCalendars( const Locale& rLocale )
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
140 CalendarImpl::setDateTime( double timeInDays )
141 {
142     if (xCalendar.is())
143         xCalendar->setDateTime( timeInDays );
144     else
145         throw ERROR ;
146 }
147 
148 double SAL_CALL
149 CalendarImpl::getDateTime()
150 {
151     if (xCalendar.is())
152         return xCalendar->getDateTime();
153     else
154         throw ERROR ;
155 }
156 
157 OUString SAL_CALL
158 CalendarImpl::getUniqueID()
159 {
160     if (xCalendar.is())
161         return xCalendar->getUniqueID();
162     else
163         throw ERROR ;
164 }
165 
166 void SAL_CALL
167 CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value )
168 {
169     if (xCalendar.is())
170         xCalendar->setValue( fieldIndex, value );
171     else
172         throw ERROR ;
173 }
174 
175 sal_Int16 SAL_CALL
176 CalendarImpl::getValue( sal_Int16 fieldIndex )
177 {
178     if (xCalendar.is())
179         return xCalendar->getValue( fieldIndex );
180     else
181         throw ERROR ;
182 }
183 
184 void SAL_CALL
185 CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount )
186 {
187     if (xCalendar.is())
188         xCalendar->addValue( fieldIndex, amount);
189     else
190         throw ERROR ;
191 }
192 
193 sal_Int16 SAL_CALL
194 CalendarImpl::getFirstDayOfWeek()
195 {
196     if (xCalendar.is())
197         return xCalendar->getFirstDayOfWeek();
198     else
199         throw ERROR ;
200 }
201 
202 void SAL_CALL
203 CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
204 {
205     if (xCalendar.is())
206         xCalendar->setFirstDayOfWeek(day);
207     else
208         throw ERROR ;
209 }
210 
211 void SAL_CALL
212 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
213 {
214     if (xCalendar.is())
215         xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
216     else
217         throw ERROR ;
218 }
219 
220 sal_Int16 SAL_CALL
221 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
222 {
223     if (xCalendar.is())
224         return xCalendar->getMinimumNumberOfDaysForFirstWeek();
225     else
226         throw ERROR ;
227 }
228 
229 
230 OUString SAL_CALL
231 CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
232 {
233     if (xCalendar.is())
234         return xCalendar->getDisplayName( displayIndex, idx, nameType );
235     else
236         throw ERROR ;
237 }
238 
239 sal_Int16 SAL_CALL
240 CalendarImpl::getNumberOfMonthsInYear()
241 {
242     if (xCalendar.is())
243         return xCalendar->getNumberOfMonthsInYear();
244     else
245         throw ERROR ;
246 }
247 
248 
249 sal_Int16 SAL_CALL
250 CalendarImpl::getNumberOfDaysInWeek()
251 {
252     if (xCalendar.is())
253         return xCalendar->getNumberOfDaysInWeek();
254     else
255         throw ERROR ;
256 }
257 
258 
259 Sequence< CalendarItem > SAL_CALL
260 CalendarImpl::getMonths()
261 {
262     if (xCalendar.is())
263         return xCalendar->getMonths();
264     else
265         throw ERROR ;
266 }
267 
268 
269 Sequence< CalendarItem > SAL_CALL
270 CalendarImpl::getDays()
271 {
272     if (xCalendar.is())
273         return xCalendar->getDays();
274     else
275         throw ERROR ;
276 }
277 
278 
279 sal_Bool SAL_CALL
280 CalendarImpl::isValid()
281 {
282     if (xCalendar.is())
283         return xCalendar->isValid();
284     else
285         throw ERROR ;
286 }
287 
288 OUString SAL_CALL
289 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
290 {
291     if (xCalendar.is())
292         return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
293     else
294         throw ERROR ;
295 }
296 
297 OUString SAL_CALL
298 CalendarImpl::getImplementationName(void)
299 {
300     return OUString::createFromAscii("com.sun.star.i18n.CalendarImpl");
301 }
302 
303 const sal_Char cCalendar[] = "com.sun.star.i18n.LocaleCalendar";
304 
305 sal_Bool SAL_CALL
306 CalendarImpl::supportsService(const OUString& rServiceName)
307 {
308     return !rServiceName.compareToAscii(cCalendar);
309 }
310 
311 Sequence< OUString > SAL_CALL
312 CalendarImpl::getSupportedServiceNames(void)
313 {
314     Sequence< OUString > aRet(1);
315     aRet[0] = OUString::createFromAscii(cCalendar);
316     return aRet;
317 }
318