1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <calendarImpl.hxx>
21 #include <localedata.hxx>
22 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/uno/XComponentContext.hpp>
26 using namespace ::com::sun::star::uno
;
27 using namespace ::com::sun::star::i18n
;
28 using namespace ::com::sun::star::lang
;
32 #define ERROR RuntimeException()
34 CalendarImpl::CalendarImpl(const Reference
< XComponentContext
> &rxContext
) : m_xContext(rxContext
)
38 CalendarImpl::~CalendarImpl()
43 CalendarImpl::loadDefaultCalendar( const Locale
& rLocale
)
45 Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
46 for (sal_Int32 i
= 0; i
< xC
.getLength(); i
++) {
48 loadCalendar(xC
[i
].Name
, rLocale
);
56 CalendarImpl::loadCalendar(const OUString
& uniqueID
, const Locale
& rLocale
)
58 Reference
< XCalendar4
> xOldCalendar( xCalendar
); // backup
61 for (i
= 0; i
< sal::static_int_cast
<sal_Int32
>(lookupTable
.size()); i
++) {
62 lookupTableItem
&listItem
= lookupTable
[i
];
63 if (uniqueID
== listItem
.uniqueID
) {
64 xCalendar
= listItem
.xCalendar
;
69 if (i
>= sal::static_int_cast
<sal_Int32
>(lookupTable
.size())) {
70 Reference
< XInterface
> xI
= m_xContext
->getServiceManager()->createInstanceWithContext(
71 "com.sun.star.i18n.Calendar_" + uniqueID
, m_xContext
);
74 // check if the calendar is defined in localedata, load gregorian calendar service.
75 Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
76 for (i
= 0; i
< xC
.getLength(); i
++) {
77 if (uniqueID
== xC
[i
].Name
) {
78 xI
= m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext
);
86 xCalendar
.set(xI
, UNO_QUERY
);
88 lookupTable
.emplace_back( uniqueID
, xCalendar
);
91 if ( !xCalendar
.is() )
93 xCalendar
= xOldCalendar
;
99 xCalendar
->loadCalendar(uniqueID
, rLocale
);
102 { // restore previous calendar and re-throw
103 xCalendar
= xOldCalendar
;
109 CalendarImpl::getLoadedCalendar2()
113 return xCalendar
->getLoadedCalendar2();
117 CalendarImpl::getLoadedCalendar()
121 return xCalendar
->getLoadedCalendar();
124 Sequence
< OUString
> SAL_CALL
125 CalendarImpl::getAllCalendars( const Locale
& rLocale
)
127 Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->getAllCalendars2(rLocale
);
128 sal_Int32 nLen
= xC
.getLength();
129 Sequence
< OUString
> xSeq( nLen
);
130 for (sal_Int32 i
= 0; i
< nLen
; i
++)
131 xSeq
[i
] = xC
[i
].Name
;
136 CalendarImpl::setDateTime( double fTimeInDays
)
140 xCalendar
->setDateTime( fTimeInDays
);
144 CalendarImpl::getDateTime()
148 return xCalendar
->getDateTime();
152 CalendarImpl::setLocalDateTime( double fTimeInDays
)
156 xCalendar
->setLocalDateTime( fTimeInDays
);
160 CalendarImpl::getLocalDateTime()
164 return xCalendar
->getLocalDateTime();
168 CalendarImpl::getUniqueID()
172 return xCalendar
->getUniqueID();
176 CalendarImpl::setValue( sal_Int16 fieldIndex
, sal_Int16 value
)
180 xCalendar
->setValue( fieldIndex
, value
);
184 CalendarImpl::getValue( sal_Int16 fieldIndex
)
188 return xCalendar
->getValue( fieldIndex
);
192 CalendarImpl::addValue( sal_Int16 fieldIndex
, sal_Int32 amount
)
196 xCalendar
->addValue( fieldIndex
, amount
);
200 CalendarImpl::getFirstDayOfWeek()
204 return xCalendar
->getFirstDayOfWeek();
208 CalendarImpl::setFirstDayOfWeek( sal_Int16 day
)
212 xCalendar
->setFirstDayOfWeek(day
);
216 CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
)
220 xCalendar
->setMinimumNumberOfDaysForFirstWeek(days
);
224 CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
228 return xCalendar
->getMinimumNumberOfDaysForFirstWeek();
233 CalendarImpl::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
)
237 return xCalendar
->getDisplayName( displayIndex
, idx
, nameType
);
241 CalendarImpl::getNumberOfMonthsInYear()
245 return xCalendar
->getNumberOfMonthsInYear();
250 CalendarImpl::getNumberOfDaysInWeek()
254 return xCalendar
->getNumberOfDaysInWeek();
258 Sequence
< CalendarItem
> SAL_CALL
259 CalendarImpl::getDays()
263 return xCalendar
->getDays();
267 Sequence
< CalendarItem
> SAL_CALL
268 CalendarImpl::getMonths()
272 return xCalendar
->getMonths();
276 Sequence
< CalendarItem2
> SAL_CALL
277 CalendarImpl::getDays2()
281 return xCalendar
->getDays2();
285 Sequence
< CalendarItem2
> SAL_CALL
286 CalendarImpl::getMonths2()
290 return xCalendar
->getMonths2();
294 Sequence
< CalendarItem2
> SAL_CALL
295 CalendarImpl::getGenitiveMonths2()
299 return xCalendar
->getGenitiveMonths2();
303 Sequence
< CalendarItem2
> SAL_CALL
304 CalendarImpl::getPartitiveMonths2()
308 return xCalendar
->getPartitiveMonths2();
313 CalendarImpl::isValid()
317 return xCalendar
->isValid();
321 CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
325 return xCalendar
->getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
329 CalendarImpl::getImplementationName()
331 return OUString("com.sun.star.i18n.CalendarImpl");
335 CalendarImpl::supportsService(const OUString
& rServiceName
)
337 return cppu::supportsService(this, rServiceName
);
340 Sequence
< OUString
> SAL_CALL
341 CalendarImpl::getSupportedServiceNames()
343 Sequence
< OUString
> aRet(2);
344 aRet
[0] = "com.sun.star.i18n.LocaleCalendar";
345 aRet
[1] = "com.sun.star.i18n.LocaleCalendar2";
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */