LibreOffice Module unotools (master) 1
calendarwrapper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/log.hxx>
23#include <com/sun/star/i18n/LocaleCalendar2.hpp>
25
26using namespace ::com::sun::star;
27using namespace ::com::sun::star::i18n;
28using namespace ::com::sun::star::uno;
29
31 const Reference< uno::XComponentContext > & rxContext
32 )
33 :
34 aEpochStart( Date( 1, 1, 1970 ) )
35{
36 xC = LocaleCalendar2::create(rxContext);
37}
38
40{
41}
42
43void CalendarWrapper::loadDefaultCalendar( const css::lang::Locale& rLocale, bool bTimeZoneUTC )
44{
45 try
46 {
47 if ( xC.is() )
48 xC->loadDefaultCalendarTZ( rLocale, (bTimeZoneUTC ? "UTC" : OUString()));
49 }
50 catch (const Exception&)
51 {
52 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadDefaultCalendar" );
53 }
54}
55
56void CalendarWrapper::loadCalendar( const OUString& rUniqueID, const css::lang::Locale& rLocale, bool bTimeZoneUTC )
57{
58 try
59 {
60 if ( xC.is() )
61 xC->loadCalendarTZ( rUniqueID, rLocale, (bTimeZoneUTC ? "UTC" : OUString()));
62 }
63 catch (const Exception&)
64 {
65 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadCalendar: "
66 << rUniqueID << " Locale: " << LanguageTag::convertToBcp47(rLocale) );
67 }
68}
69
70css::uno::Sequence< OUString > CalendarWrapper::getAllCalendars( const css::lang::Locale& rLocale ) const
71{
72 try
73 {
74 if ( xC.is() )
75 return xC->getAllCalendars( rLocale );
76 }
77 catch (const Exception&)
78 {
79 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllCalendars" );
80 }
81
82 return {};
83}
84
86{
87 try
88 {
89 if ( xC.is() )
90 return xC->getUniqueID();
91 }
92 catch (const Exception&)
93 {
94 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getUniqueID" );
95 }
96 return OUString();
97}
98
99void CalendarWrapper::setDateTime( double fTimeInDays )
100{
101 try
102 {
103 if ( xC.is() )
104 xC->setDateTime( fTimeInDays );
105 }
106 catch (const Exception&)
107 {
108 TOOLS_WARN_EXCEPTION( "unotools.i18n", "setDateTime" );
109 }
110}
111
113{
114 try
115 {
116 if ( xC.is() )
117 return xC->getDateTime();
118 }
119 catch (const Exception&)
120 {
121 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDateTime" );
122 }
123 return 0.0;
124}
125
126void CalendarWrapper::setLocalDateTime( double fTimeInDays )
127{
128 try
129 {
130 if ( xC.is() )
131 {
132 xC->setLocalDateTime( fTimeInDays );
133 }
134 }
135 catch (const Exception&)
136 {
137 TOOLS_WARN_EXCEPTION( "unotools.i18n", "setLocalDateTime" );
138 }
139}
140
142{
143 try
144 {
145 if ( xC.is() )
146 {
147 return xC->getLocalDateTime();
148 }
149 }
150 catch (const Exception&)
151 {
152 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLocalDateTime" );
153 }
154 return 0.0;
155}
156
157void CalendarWrapper::setValue( sal_Int16 nFieldIndex, sal_Int16 nValue )
158{
159 try
160 {
161 if ( xC.is() )
162 xC->setValue( nFieldIndex, nValue );
163 }
164 catch (const Exception&)
165 {
166 TOOLS_WARN_EXCEPTION( "unotools.i18n", "setValue" );
167 }
168}
169
171{
172 try
173 {
174 if ( xC.is() )
175 return xC->isValid();
176 }
177 catch (const Exception&)
178 {
179 TOOLS_WARN_EXCEPTION( "unotools.i18n", "isValid" );
180 }
181 return false;
182}
183
184sal_Int16 CalendarWrapper::getValue( sal_Int16 nFieldIndex ) const
185{
186 try
187 {
188 if ( xC.is() )
189 return xC->getValue( nFieldIndex );
190 }
191 catch (const Exception&)
192 {
193 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getValue" );
194 }
195 return 0;
196}
197
199{
200 try
201 {
202 if ( xC.is() )
203 return xC->getFirstDayOfWeek();
204 }
205 catch (const Exception&)
206 {
207 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getFirstDayOfWeek" );
208 }
209 return 0;
210}
211
213{
214 try
215 {
216 if ( xC.is() )
217 return xC->getNumberOfMonthsInYear();
218 }
219 catch (const Exception&)
220 {
221 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getNumberOfMonthsInYear" );
222 }
223 return 0;
224}
225
227{
228 try
229 {
230 if ( xC.is() )
231 return xC->getNumberOfDaysInWeek();
232 }
233 catch (const Exception&)
234 {
235 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getNumberOfDaysInWeek" );
236 }
237 return 0;
238}
239
240css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getMonths() const
241{
242 try
243 {
244 if ( xC.is() )
245 return xC->getMonths2();
246 }
247 catch (const Exception&)
248 {
249 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getMonths" );
250 }
251 return {};
252}
253
254css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getDays() const
255{
256 try
257 {
258 if ( xC.is() )
259 return xC->getDays2();
260 }
261 catch (const Exception&)
262 {
263 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDays" );
264 }
265 return {};
266}
267
268OUString CalendarWrapper::getDisplayName( sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType ) const
269{
270 try
271 {
272 if ( xC.is() )
273 return xC->getDisplayName( nCalendarDisplayIndex, nIdx, nNameType );
274 }
275 catch (const Exception&)
276 {
277 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDisplayName" );
278 }
279 return OUString();
280}
281
282// --- XExtendedCalendar -----------------------------------------------------
283
284OUString CalendarWrapper::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) const
285{
286 try
287 {
288 if ( xC.is() )
289 return xC->getDisplayString( nCalendarDisplayCode, nNativeNumberMode );
290 }
291 catch (const Exception&)
292 {
293 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDisplayString" );
294 }
295 return OUString();
296}
297
298// --- XCalendar3 ------------------------------------------------------------
299
300css::i18n::Calendar2 CalendarWrapper::getLoadedCalendar() const
301{
302 try
303 {
304 if ( xC.is() )
305 return xC->getLoadedCalendar2();
306 }
307 catch (const Exception&)
308 {
309 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLoadedCalendar2" );
310 }
311 return css::i18n::Calendar2();
312}
313
314css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getGenitiveMonths() const
315{
316 try
317 {
318 if ( xC.is() )
319 return xC->getGenitiveMonths2();
320 }
321 catch (const Exception&)
322 {
323 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getGenitiveMonths" );
324 }
325 return {};
326}
327
328css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getPartitiveMonths() const
329{
330 try
331 {
332 if ( xC.is() )
333 return xC->getPartitiveMonths2();
334 }
335 catch (const Exception&)
336 {
337 TOOLS_WARN_EXCEPTION( "unotools.i18n", "getPartitiveMonths" );
338 }
339 return {};
340}
341
342/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void loadCalendar(const OUString &rUniqueID, const css::lang::Locale &rLocale, bool bTimeZoneUTC=true)
This adds a bTimeZoneUTC parameter which is not part of the API.
css::uno::Reference< css::i18n::XCalendar4 > xC
css::uno::Sequence< css::i18n::CalendarItem2 > getDays() const
sal_Int16 getNumberOfDaysInWeek() const
css::uno::Sequence< css::i18n::CalendarItem2 > getGenitiveMonths() const
sal_Int16 getFirstDayOfWeek() const
void setLocalDateTime(double fTimeInDays)
set local date/time
sal_Int16 getValue(sal_Int16 nFieldIndex) const
CalendarWrapper(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
css::uno::Sequence< css::i18n::CalendarItem2 > getMonths() const
void setValue(sal_Int16 nFieldIndex, sal_Int16 nValue)
void loadDefaultCalendar(const css::lang::Locale &rLocale, bool bTimeZoneUTC=true)
Load the default calendar of a locale.
OUString getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) const
bool isValid() const
void setDateTime(double fTimeInDays)
set UTC date/time
OUString getDisplayString(sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode) const
double getDateTime() const
get UTC date/time
OUString getUniqueID() const
css::i18n::Calendar2 getLoadedCalendar() const
css::uno::Sequence< OUString > getAllCalendars(const css::lang::Locale &rLocale) const
double getLocalDateTime() const
get local date/time
css::uno::Sequence< css::i18n::CalendarItem2 > getPartitiveMonths() const
sal_Int16 getNumberOfMonthsInYear() const
static OUString convertToBcp47(LanguageType nLangID)
#define TOOLS_WARN_EXCEPTION(area, stream)
sal_Int16 nValue
@ Exception