1 /*************************************************************************
2 *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26 ************************************************************************/
27 
28 //------------------------------------------------------------------
29 //
30 // date functions add in
31 //
32 //------------------------------------------------------------------
33 
34 #ifndef _SCA_DATEFUNC_HXX
35 #define _SCA_DATEFUNC_HXX
36 
37 #include <string.h>
38 #include <com/sun/star/lang/XServiceName.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/sheet/XAddIn.hpp>
41 #include <com/sun/star/sheet/XCompatibilityNames.hpp>
42 #include <com/sun/star/sheet/addin/XDateFunctions.hpp>
43 #include <com/sun/star/sheet/addin/XMiscFunctions.hpp>
44 #include <cppuhelper/implbase6.hxx>             // helper for implementations
45 #include <tools/resid.hxx>
46 #include <tools/rc.hxx>
47 #include <tools/resary.hxx>
48 
49 //------------------------------------------------------------------
50 
51 class ScaList
52 {
53 private:
54     static const sal_uInt32     nStartSize;
55     static const sal_uInt32     nIncrSize;
56 
57     void**                      pData;          // pointer array
58     sal_uInt32                  nSize;          // array size
59     sal_uInt32                  nCount;         // next index to be inserted at
60     sal_uInt32                  nCurr;          // current pos for iterations
61 
62     void                        _Grow();
63     inline void                 Grow();
64 
65 public:
66                                 ScaList();
67     virtual                     ~ScaList();
68 
69     inline sal_uInt32           Count() const       { return nCount; }
70 
71     inline const void*          GetObject( sal_uInt32 nIndex ) const
72                                     { return (nIndex < nCount) ? pData[ nIndex ] : NULL; }
73 
74     inline void*                First() { return nCount ? pData[ nCurr = 0 ] : NULL; }
75     inline void*                Next()  { return (nCurr + 1 < nCount) ? pData[ ++nCurr ] : NULL; }
76 
77     inline void                 Append( void* pNew );
78     void                        Insert( void* pNew, sal_uInt32 nIndex );
79 };
80 
81 
82 inline void ScaList::Grow()
83 {
84     if( nCount >= nSize )
85 		_Grow();
86 }
87 
88 inline void ScaList::Append( void* pNew )
89 {
90 	Grow();
91     pData[ nCount++ ] = pNew;
92 }
93 
94 
95 //------------------------------------------------------------------
96 
97 class ScaStringList : protected ScaList
98 {
99 public:
100     inline                      ScaStringList() : ScaList() {};
101     virtual                     ~ScaStringList();
102 
103                                 using ScaList::Count;
104 
105     inline const ::rtl::OUString* Get( sal_uInt32 nIndex ) const;
106 
107     inline ::rtl::OUString*     First();
108     inline ::rtl::OUString*     Next();
109 
110     using ScaList::Append;
111     inline void                 Append( ::rtl::OUString* pNew );
112     inline void                 Append( const ::rtl::OUString& rNew );
113 };
114 
115 
116 inline const ::rtl::OUString* ScaStringList::Get( sal_uInt32 nIndex ) const
117 {
118     return static_cast< const ::rtl::OUString* >( ScaList::GetObject( nIndex ) );
119 }
120 
121 inline ::rtl::OUString* ScaStringList::First()
122 {
123     return static_cast< ::rtl::OUString* >( ScaList::First() );
124 }
125 
126 inline ::rtl::OUString* ScaStringList::Next()
127 {
128     return static_cast< ::rtl::OUString* >( ScaList::Next() );
129 }
130 
131 inline void ScaStringList::Append( ::rtl::OUString* pNew )
132 {
133     ScaList::Append( pNew );
134 }
135 
136 inline void ScaStringList::Append( const ::rtl::OUString& rNew )
137 {
138     ScaList::Append( new ::rtl::OUString( rNew ) );
139 }
140 
141 
142 //------------------------------------------------------------------
143 
144 class ScaResId : public ResId
145 {
146 public:
147                                 ScaResId( sal_uInt16 nResId, ResMgr& rResMgr );
148 };
149 
150 
151 //------------------------------------------------------------------
152 
153 class ScaResStringLoader : public Resource
154 {
155 private:
156     String                      aStr;
157 
158 public:
159     inline                      ScaResStringLoader( sal_uInt16 nResId, sal_uInt16 nStrId, ResMgr& rResMgr );
160 
161     inline const String&        GetString() const   { return aStr; }
162 
163 };
164 
165 
166 inline ScaResStringLoader::ScaResStringLoader( sal_uInt16 nResId, sal_uInt16 nStrId, ResMgr& rResMgr ) :
167     Resource( ScaResId( nResId, rResMgr ) ),
168     aStr( ScaResId( nStrId, rResMgr ) )
169 {
170     FreeResource();
171 }
172 
173 
174 //------------------------------------------------------------------
175 
176 class ScaResStringArrLoader : public Resource
177 {
178 private:
179     ResStringArray              aStrArray;
180 
181 public:
182     inline                      ScaResStringArrLoader( sal_uInt16 nResId, sal_uInt16 nArrayId, ResMgr& rResMgr );
183 
184     inline const ResStringArray& GetStringArray() const { return aStrArray; }
185 };
186 
187 
188 
189 inline ScaResStringArrLoader::ScaResStringArrLoader( sal_uInt16 nResId, sal_uInt16 nArrayId, ResMgr& rResMgr ) :
190     Resource( ScaResId( nResId, rResMgr ) ),
191     aStrArray( ScaResId( nArrayId, rResMgr ) )
192 {
193     FreeResource();
194 }
195 
196 
197 //------------------------------------------------------------------
198 
199 class ScaResPublisher : public Resource
200 {
201 public:
202     inline                      ScaResPublisher( const ScaResId& rResId ) : Resource( rResId ) {}
203 
204     inline sal_Bool             IsAvailableRes( const ResId& rResId ) const
205                                     { return Resource::IsAvailableRes( rResId ); }
206     inline void                 FreeResource()
207                                     { Resource::FreeResource(); }
208 };
209 
210 
211 //------------------------------------------------------------------
212 
213 class ScaFuncRes : public Resource
214 {
215 public:
216                                 ScaFuncRes( ResId& rResId, ResMgr& rResMgr, sal_uInt16 nIndex, ::rtl::OUString& rRet );
217 };
218 
219 
220 //------------------------------------------------------------------
221 
222 enum ScaCategory
223 {
224     ScaCat_AddIn,
225     ScaCat_DateTime,
226     ScaCat_Text,
227     ScaCat_Finance,
228     ScaCat_Inf,
229     ScaCat_Math,
230     ScaCat_Tech
231 };
232 
233 struct ScaFuncDataBase
234 {
235     const sal_Char*             pIntName;           // internal name (get***)
236     sal_uInt16                  nUINameID;          // resource ID to UI name
237     sal_uInt16                  nDescrID;           // resource ID to description, parameter names and ~ description
238     sal_uInt16                  nCompListID;        // resource ID to list of valid names
239     sal_uInt16                  nParamCount;        // number of named / described parameters
240     ScaCategory                 eCat;               // function category
241     sal_Bool                    bDouble;            // name already exist in Calc
242     sal_Bool                    bWithOpt;           // first parameter is internal
243 };
244 
245 class ScaFuncData
246 {
247 private:
248     ::rtl::OUString             aIntName;           // internal name (get***)
249     sal_uInt16                  nUINameID;          // resource ID to UI name
250     sal_uInt16                  nDescrID;           // leads also to parameter descriptions!
251     sal_uInt16                  nCompListID;        // resource ID to list of valid names
252     sal_uInt16                  nParamCount;        // num of parameters
253     ScaStringList               aCompList;          // list of all valid names
254     ScaCategory                 eCat;               // function category
255     sal_Bool                    bDouble;            // name already exist in Calc
256     sal_Bool                    bWithOpt;           // first parameter is internal
257 
258 public:
259                                 ScaFuncData( const ScaFuncDataBase& rBaseData, ResMgr& rRscMgr );
260     virtual                     ~ScaFuncData();
261 
262     inline sal_uInt16           GetUINameID() const     { return nUINameID; }
263     inline sal_uInt16           GetDescrID() const      { return nDescrID; }
264     inline ScaCategory          GetCategory() const     { return eCat; }
265     inline sal_Bool             IsDouble() const        { return bDouble; }
266     inline sal_Bool             HasIntParam() const     { return bWithOpt; }
267 
268     sal_uInt16                  GetStrIndex( sal_uInt16 nParam ) const;
269     inline sal_Bool             Is( const ::rtl::OUString& rCompare ) const
270                                                     { return aIntName == rCompare; }
271 
272     inline const ScaStringList& GetCompNameList() const { return aCompList; }
273 };
274 
275 
276 //------------------------------------------------------------------
277 
278 class ScaFuncDataList : private ScaList
279 {
280     ::rtl::OUString             aLastName;
281     sal_uInt32                  nLast;
282 
283 public:
284                                 ScaFuncDataList( ResMgr& rResMgr );
285     virtual                     ~ScaFuncDataList();
286 
287                                 using ScaList::Count;
288 
289     inline const ScaFuncData*   Get( sal_uInt32 nIndex ) const;
290     const ScaFuncData*          Get( const ::rtl::OUString& rProgrammaticName ) const;
291     inline ScaFuncData*         First();
292     inline ScaFuncData*         Next();
293 
294     using ScaList::Append;
295     inline void                 Append( ScaFuncData* pNew ) { ScaList::Append( pNew ); }
296 };
297 
298 
299 inline const ScaFuncData* ScaFuncDataList::Get( sal_uInt32 nIndex ) const
300 {
301     return static_cast< const ScaFuncData* >( ScaList::GetObject( nIndex ) );
302 }
303 
304 inline ScaFuncData* ScaFuncDataList::First()
305 {
306     return static_cast< ScaFuncData* >( ScaList::First() );
307 }
308 
309 inline ScaFuncData* ScaFuncDataList::Next()
310 {
311     return static_cast< ScaFuncData* >( ScaList::Next() );
312 }
313 
314 
315 //------------------------------------------------------------------
316 //------------------------------------------------------------------
317 
318 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL DateFunctionAddIn_CreateInstance(
319     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& );
320 
321 
322 // THE AddIn class for date functions
323 
324 class ScaDateAddIn : public ::cppu::WeakImplHelper6<
325                                 ::com::sun::star::sheet::XAddIn,
326                                 ::com::sun::star::sheet::XCompatibilityNames,
327                                 ::com::sun::star::sheet::addin::XDateFunctions,
328                                 ::com::sun::star::sheet::addin::XMiscFunctions,
329                                 ::com::sun::star::lang::XServiceName,
330                                 ::com::sun::star::lang::XServiceInfo >
331 {
332 private:
333     ::com::sun::star::lang::Locale  aFuncLoc;
334     ::com::sun::star::lang::Locale* pDefLocales;
335 	ResMgr*						pResMgr;
336     ScaFuncDataList*            pFuncDataList;
337 
338 
339     void                        InitDefLocales();
340     const ::com::sun::star::lang::Locale& GetLocale( sal_uInt32 nIndex );
341     ResMgr&                     GetResMgr() throw( ::com::sun::star::uno::RuntimeException );
342     void                        InitData();
343 
344     ::rtl::OUString             GetDisplFuncStr( sal_uInt16 nResId ) throw( ::com::sun::star::uno::RuntimeException );
345     ::rtl::OUString             GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) throw( ::com::sun::star::uno::RuntimeException );
346 
347 public:
348                                 ScaDateAddIn();
349     virtual                     ~ScaDateAddIn();
350 
351     static ::rtl::OUString      getImplementationName_Static();
352     static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static();
353 
354                                 // XAddIn
355     virtual ::rtl::OUString SAL_CALL getProgrammaticFuntionName( const ::rtl::OUString& aDisplayName ) throw( ::com::sun::star::uno::RuntimeException );
356     virtual ::rtl::OUString SAL_CALL getDisplayFunctionName( const ::rtl::OUString& aProgrammaticName ) throw( ::com::sun::star::uno::RuntimeException );
357     virtual ::rtl::OUString SAL_CALL getFunctionDescription( const ::rtl::OUString& aProgrammaticName ) throw( ::com::sun::star::uno::RuntimeException );
358     virtual ::rtl::OUString SAL_CALL getDisplayArgumentName( const ::rtl::OUString& aProgrammaticName, sal_Int32 nArgument ) throw( ::com::sun::star::uno::RuntimeException );
359     virtual ::rtl::OUString SAL_CALL getArgumentDescription( const ::rtl::OUString& aProgrammaticName, sal_Int32 nArgument ) throw( ::com::sun::star::uno::RuntimeException );
360     virtual ::rtl::OUString SAL_CALL getProgrammaticCategoryName( const ::rtl::OUString& aProgrammaticName ) throw( ::com::sun::star::uno::RuntimeException );
361     virtual ::rtl::OUString SAL_CALL getDisplayCategoryName( const ::rtl::OUString& aProgrammaticName ) throw( ::com::sun::star::uno::RuntimeException );
362 
363                                 // XCompatibilityNames
364     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::sheet::LocalizedName > SAL_CALL getCompatibilityNames( const ::rtl::OUString& aProgrammaticName ) throw( ::com::sun::star::uno::RuntimeException );
365 
366                                 // XLocalizable
367     virtual void SAL_CALL       setLocale( const ::com::sun::star::lang::Locale& eLocale ) throw( ::com::sun::star::uno::RuntimeException );
368     virtual ::com::sun::star::lang::Locale SAL_CALL getLocale() throw( ::com::sun::star::uno::RuntimeException );
369 
370                                 // XServiceName
371     virtual ::rtl::OUString SAL_CALL getServiceName() throw( ::com::sun::star::uno::RuntimeException );
372 
373                                 // XServiceInfo
374     virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException );
375     virtual sal_Bool SAL_CALL   supportsService( const ::rtl::OUString& ServiceName ) throw( ::com::sun::star::uno::RuntimeException );
376     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException );
377 
378     //  methods from own interfaces start here
379 
380                                 // XDateFunctions
381     virtual sal_Int32 SAL_CALL  getDiffWeeks(
382                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
383                                     sal_Int32 nEndDate, sal_Int32 nStartDate,
384                                     sal_Int32 nMode )
385                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
386 
387     virtual sal_Int32 SAL_CALL  getDiffMonths(
388                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
389                                     sal_Int32 nEndDate, sal_Int32 nStartDate,
390                                     sal_Int32 nMode )
391                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
392 
393     virtual sal_Int32 SAL_CALL  getDiffYears(
394                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
395                                     sal_Int32 nEndDate, sal_Int32 nStartDate,
396                                     sal_Int32 nMode )
397                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
398 
399     virtual sal_Int32 SAL_CALL  getIsLeapYear(
400                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
401                                     sal_Int32 nDate )
402                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
403 
404     virtual sal_Int32 SAL_CALL  getDaysInMonth(
405                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
406                                     sal_Int32 nDate )
407                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
408 
409     virtual sal_Int32 SAL_CALL  getDaysInYear(
410                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
411                                     sal_Int32 nDate )
412                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
413 
414     virtual sal_Int32 SAL_CALL  getWeeksInYear(
415                                     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xOptions,
416                                     sal_Int32 nDate )
417                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
418 
419                                 // XMiscFunctions
420     virtual ::rtl::OUString SAL_CALL getRot13(
421                                     const ::rtl::OUString& aSrcText )
422                                 throw( ::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException );
423 };
424 
425 //------------------------------------------------------------------
426 
427 #endif  // _SCA_DATEFUNC_HXX
428 
429