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