1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: addin.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
35 #include <usr/factoryhlp.hxx>
36 #include <usr/macros.hxx>
37 #include <usr/reflserv.hxx>
38 #include <vos/mutex.hxx>
39 #include <vcl/svapp.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/date.hxx>
42 #include <svtools/itemprop.hxx>
43 #include <usr/proptypehlp.hxx>
44 #include <cppuhelper/factory.hxx>
45 #include <uno/lbnames.h>
46 #include <osl/diagnose.h>
48 #include <com/sun/star/util/date.hpp>
53 using namespace com::sun::star
;
55 //------------------------------------------------------------------------
58 //------------------------------------------------------------------------
60 SMART_UNO_IMPLEMENTATION( ScTestAddIn
, UsrObject
);
62 #define SCADDIN_SERVICE L"com.sun.star.sheet.AddIn"
63 #define SCTESTADDIN_SERVICE L"stardiv.one.sheet.DemoAddIn"
65 //------------------------------------------------------------------------
69 void SAL_CALL
component_getImplementationEnvironment(
70 const sal_Char
** ppEnvTypeName
, uno_Environment
** ppEnv
)
72 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
75 sal_Bool SAL_CALL
component_writeInfo(
76 void * pServiceManager
, registry::XRegistryKey
* pRegistryKey
)
83 aImpl
+= ScTestAddIn::getImplementationName_Static();
84 aImpl
+= L
"/UNO/SERVICES";
86 uno::Reference
<registry::XRegistryKey
> xNewKey(
87 reinterpret_cast<registry::XRegistryKey
*>(pRegistryKey
)->createKey(aImpl
) );
89 uno::Sequence
<rtl::OUString
> aSequ
= ScTestAddIn::getSupportedServiceNames_Static();
90 const rtl::OUString
* pArray
= aSequ
.getConstArray();
91 for( INT32 i
= 0; i
< aSequ
.getLength(); i
++ )
92 xNewKey
->createKey( pArray
[i
] );
96 catch (registry::InvalidRegistryException
&)
98 OSL_ENSHURE( sal_False
, "### InvalidRegistryException!" );
104 void * SAL_CALL
component_getFactory(
105 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
109 if ( pServiceManager
&& UString(pImplName
) == ScTestAddIn::getImplementationName_Static() )
111 uno::Reference
<lang::XSingleServiceFactory
> xFactory( cppu::createOneInstanceFactory(
112 reinterpret_cast<lang::XMultiServiceFactory
*>(pServiceManager
),
113 ScTestAddIn::getImplementationName_Static(),
114 ScTestAddIn_CreateInstance
,
115 ScTestAddIn::getSupportedServiceNames_Static() ) );
120 pRet
= xFactory
.get();
129 //------------------------------------------------------------------------
131 ScTestAddIn::ScTestAddIn()
135 ScTestAddIn::~ScTestAddIn()
139 UString
ScTestAddIn::getImplementationName_Static()
141 return L
"stardiv.StarCalc.ScTestAddIn";
144 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> ScTestAddIn::getSupportedServiceNames_Static()
146 uno::Sequence
< rtl::OUString
> aRet(2);
147 rtl::OUString
* pArray
= aRet
.getArray();
148 pArray
[0] = SCADDIN_SERVICE
;
149 pArray
[1] = SCTESTADDIN_SERVICE
;
153 uno::Reference
<uno::XInterface
> ScTestAddIn_CreateInstance(
154 const uno::Reference
<lang::XMultiServiceFactory
>& )
156 static uno::Reference
<uno::XInterface
> xInst
= (cppu::OWeakObject
*)new ScTestAddIn();
163 UString
ScTestAddIn::getProgrammaticFuntionName(const UString
& aDisplayName
)
164 THROWS( (UsrSystemException
) )
170 UString
ScTestAddIn::getDisplayFunctionName(const UString
& aProgrammaticName
)
171 THROWS( (UsrSystemException
) )
173 // return translated strings
176 if ( aProgrammaticName
== L
"countParams" ) aRet
= L
"ParamAnzahl";
177 else if ( aProgrammaticName
== L
"addOne" ) aRet
= L
"PlusEins";
178 else if ( aProgrammaticName
== L
"repeatStr" ) aRet
= L
"WiederholeString";
179 else if ( aProgrammaticName
== L
"getDateString" ) aRet
= L
"Datumsstring";
180 else if ( aProgrammaticName
== L
"getColorValue" ) aRet
= L
"Farbwert";
181 else if ( aProgrammaticName
== L
"transpose" ) aRet
= L
"Transponieren";
182 else if ( aProgrammaticName
== L
"transposeInt" ) aRet
= L
"IntegerTransponieren";
183 else if ( aProgrammaticName
== L
"repeatMultiple" )aRet
= L
"Mehrfach";
184 else if ( aProgrammaticName
== L
"getStrOrVal" ) aRet
= L
"StringOderWert";
185 else if ( aProgrammaticName
== L
"callAsync" ) aRet
= L
"Asynchron";
189 UString
ScTestAddIn::getFunctionDescription(const UString
& aProgrammaticName
)
190 THROWS( (UsrSystemException
) )
192 // return translated strings
195 if ( aProgrammaticName
== L
"countParams" ) aRet
= L
"Gibt die Anzahl der Parameter zurueck.";
196 else if ( aProgrammaticName
== L
"addOne" ) aRet
= L
"Addiert 1 zur uebergebenen Zahl.";
197 else if ( aProgrammaticName
== L
"repeatStr" ) aRet
= L
"Wiederholt eine Zeichenkette.";
198 else if ( aProgrammaticName
== L
"getDateString" ) aRet
= L
"Wandelt ein Datum in eine Zeichenkette.";
199 else if ( aProgrammaticName
== L
"getColorValue" ) aRet
= L
"Gibt den Farbwert eines Zellbereichs zurueck. Bei transparentem Hintergrund wird -1 zurueckgegeben";
200 else if ( aProgrammaticName
== L
"transpose" ) aRet
= L
"Transponiert eine Matrix.";
201 else if ( aProgrammaticName
== L
"transposeInt" ) aRet
= L
"Transponiert eine Matrix mit Ganzzahlen.";
202 else if ( aProgrammaticName
== L
"repeatMultiple" )aRet
= L
"Wiederholt mehrere Bestandteile.";
203 else if ( aProgrammaticName
== L
"getStrOrVal" ) aRet
= L
"Gibt einen String oder einen Wert zurueck.";
204 else if ( aProgrammaticName
== L
"callAsync" ) aRet
= L
"Test fuer asynchrone Funktion.";
208 UString
ScTestAddIn::getDisplayArgumentName(const UString
& aProgrammaticFunctionName
,
209 INT32 nArgument
) THROWS( (UsrSystemException
) )
211 // return translated strings
214 if ( aProgrammaticFunctionName
== L
"countParams" )
216 if ( nArgument
== 0 ) aRet
= L
"Parameter";
218 else if ( aProgrammaticFunctionName
== L
"addOne" )
220 if ( nArgument
== 0 ) aRet
= L
"Wert";
222 else if ( aProgrammaticFunctionName
== L
"repeatStr" )
224 if ( nArgument
== 0 ) aRet
= L
"String";
225 else if ( nArgument
== 1 ) aRet
= L
"Anzahl";
227 else if ( aProgrammaticFunctionName
== L
"getDateString" )
229 if ( nArgument
== 0 ) aRet
= L
"Dokument";
230 else if ( nArgument
== 1 ) aRet
= L
"Wert";
232 else if ( aProgrammaticFunctionName
== L
"getColorValue" )
234 if ( nArgument
== 0 ) aRet
= L
"Bereich";
236 else if ( aProgrammaticFunctionName
== L
"transpose" )
238 if ( nArgument
== 0 ) aRet
= L
"Matrix";
240 else if ( aProgrammaticFunctionName
== L
"transposeInt" )
242 if ( nArgument
== 0 ) aRet
= L
"Matrix";
244 else if ( aProgrammaticFunctionName
== L
"repeatMultiple" )
246 if ( nArgument
== 0 ) aRet
= L
"Anzahl";
247 else if ( nArgument
== 1 ) aRet
= L
"Trenner";
248 else if ( nArgument
== 2 ) aRet
= L
"Inhalt";
250 else if ( aProgrammaticFunctionName
== L
"getStrOrVal" )
252 if ( nArgument
== 0 ) aRet
= L
"Flag";
254 else if ( aProgrammaticFunctionName
== L
"callAsync" )
256 if ( nArgument
== 0 ) aRet
= L
"Name";
261 UString
ScTestAddIn::getArgumentDescription(const UString
& aProgrammaticFunctionName
,
262 INT32 nArgument
) THROWS( (UsrSystemException
) )
264 // return translated strings
267 if ( aProgrammaticFunctionName
== L
"countParams" )
269 if ( nArgument
== 0 ) aRet
= L
"Beliebiger Parameter";
271 else if ( aProgrammaticFunctionName
== L
"addOne" )
273 if ( nArgument
== 0 ) aRet
= L
"Der Wert, zu dem 1 addiert wird";
275 else if ( aProgrammaticFunctionName
== L
"repeatStr" )
277 if ( nArgument
== 0 ) aRet
= L
"Der Text, der wiederholt wird";
278 else if ( nArgument
== 1 ) aRet
= L
"Die Anzahl der Wiederholungen";
280 else if ( aProgrammaticFunctionName
== L
"getDateString" )
282 if ( nArgument
== 0 ) aRet
= L
"(intern)";
283 else if ( nArgument
== 1 ) aRet
= L
"Der Wert, der als Datum formatiert wird";
285 else if ( aProgrammaticFunctionName
== L
"getColorValue" )
287 if ( nArgument
== 0 ) aRet
= L
"Der Bereich, dessen Hintergrundfarbe abgefragt wird";
289 else if ( aProgrammaticFunctionName
== L
"transpose" )
291 if ( nArgument
== 0 ) aRet
= L
"Die Matrix, die transponiert werden soll";
293 else if ( aProgrammaticFunctionName
== L
"transposeInt" )
295 if ( nArgument
== 0 ) aRet
= L
"Die Matrix, die transponiert werden soll";
297 else if ( aProgrammaticFunctionName
== L
"repeatMultiple" )
299 if ( nArgument
== 0 ) aRet
= L
"Anzahl der Wiederholungen";
300 else if ( nArgument
== 1 ) aRet
= L
"Text, der zwischen den Inhalten erscheint";
301 else if ( nArgument
== 2 ) aRet
= L
"Mehrere Inhalte";
303 else if ( aProgrammaticFunctionName
== L
"getStrOrVal" )
305 if ( nArgument
== 0 ) aRet
= L
"Wenn Flag 0 ist, wird ein Wert zurueckgegeben, sonst ein String.";
307 else if ( aProgrammaticFunctionName
== L
"callAsync" )
309 if ( nArgument
== 0 ) aRet
= L
"Ein String";
314 UString
ScTestAddIn::getProgrammaticCategoryName(const UString
& aProgrammaticFunctionName
)
315 THROWS( (UsrSystemException
) )
317 // return non-translated strings
320 if ( aProgrammaticFunctionName
== L
"countParams" ) aRet
= L
"Information";
321 else if ( aProgrammaticFunctionName
== L
"addOne" ) aRet
= L
"Mathematical";
322 else if ( aProgrammaticFunctionName
== L
"repeatStr" ) aRet
= L
"Text";
323 else if ( aProgrammaticFunctionName
== L
"getDateString" ) aRet
= L
"Date&Time";
324 else if ( aProgrammaticFunctionName
== L
"getColorValue" ) aRet
= L
"Spreadsheet";
325 else if ( aProgrammaticFunctionName
== L
"transpose" ) aRet
= L
"Matrix";
326 else if ( aProgrammaticFunctionName
== L
"transposeInt" ) aRet
= L
"Matrix";
327 else if ( aProgrammaticFunctionName
== L
"repeatMultiple" )aRet
= L
"Text";
328 else if ( aProgrammaticFunctionName
== L
"getStrOrVal" ) aRet
= L
"Add-In";
329 else if ( aProgrammaticFunctionName
== L
"callAsync" ) aRet
= L
"Realtime"; // new group
333 UString
ScTestAddIn::getDisplayCategoryName(const UString
& aProgrammaticFunctionName
)
334 THROWS( (UsrSystemException
) )
336 // return translated strings
338 return L
"irgendwas"; // not used for predefined categories
343 void ScTestAddIn::setLocale(const lang::Locale
& eLocale
) THROWS( (UsrSystemException
) )
346 // DBG_ERROR( UStringToString(aFuncLoc.Language, CHARSET_SYSTEM) + String("-") +
347 // UStringToString(aFuncLoc.Country, CHARSET_SYSTEM) );
350 ::com::sun::star::lang::Locale SAL_CALL
ScTestAddIn::getLocale( ) throw(::com::sun::star::uno::RuntimeException
)
357 sal_Int32 SAL_CALL
ScTestAddIn::countParams( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aArgs
) throw(::com::sun::star::uno::RuntimeException
)
359 return aArgs
.getLength();
362 double SAL_CALL
ScTestAddIn::addOne( double fValue
) throw(::com::sun::star::uno::RuntimeException
)
367 ::rtl::OUString SAL_CALL
ScTestAddIn::repeatStr( const ::rtl::OUString
& aStr
, sal_Int32 nCount
) throw(::com::sun::star::uno::RuntimeException
)
370 String aStrStr
= OUStringToString( aStr
, CHARSET_SYSTEM
);
371 for (long i
=0; i
<nCount
; i
++)
374 return StringToOUString( aRet
, CHARSET_SYSTEM
);
377 ::rtl::OUString SAL_CALL
ScTestAddIn::getDateString( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& xCaller
, double fValue
) throw(::com::sun::star::uno::RuntimeException
)
379 uno::Any aDateAny
= xCaller
->getPropertyValue( L
"NullDate" );
380 //! if ( aDateAny.getReflection()->equals( *Date_getReflection() ) )
384 //const Date* pDate = (const Date*)aDateAny.get();
387 //Date aNewDate = *pDate;
388 Date
aNewDate( aDate
.Day
, aDate
.Month
, aDate
.Year
);
390 aNewDate
+= (long)(fValue
+0.5);
393 aRet
+= aNewDate
.GetDay();
395 aRet
+= aNewDate
.GetMonth();
397 aRet
+= aNewDate
.GetYear();
398 return StringToOUString( aRet
, CHARSET_SYSTEM
);
405 sal_Int32 SAL_CALL
ScTestAddIn::getColorValue( const ::com::sun::star::uno::Reference
< ::com::sun::star::table::XCellRange
>& xRange
) throw(::com::sun::star::uno::RuntimeException
)
407 uno::Reference
<beans::XPropertySet
> xProp( xRange
, uno::UNO_QUERY
);
411 uno::Any aTrans
= xProp
->getPropertyValue( L
"IsCellBackgroundTransparent" );
413 aTrans
>>= bIsTrans
; //! dont use >>= for BOOL
416 uno::Any aCol
= xProp
->getPropertyValue( L
"CellBackColor" );
417 //nRet = NAMESPACE_USR(OPropertyTypeConversion)::toINT32( aCol );
425 double lcl_GetDoubleElement( const uno::Sequence
< uno::Sequence
<double> >& aMatrix
, long nCol
, long nRow
)
427 if ( nRow
< aMatrix
.getLength() )
429 const uno::Sequence
<double>& rRowSeq
= aMatrix
.getConstArray()[nRow
];
430 if ( nCol
< rRowSeq
.getLength() )
431 return rRowSeq
.getConstArray()[nCol
];
436 INT32
lcl_GetLongElement( const uno::Sequence
< uno::Sequence
<INT32
> >& aMatrix
, long nCol
, long nRow
)
438 if ( nRow
< aMatrix
.getLength() )
440 const uno::Sequence
<INT32
>& rRowSeq
= aMatrix
.getConstArray()[nRow
];
441 if ( nCol
< rRowSeq
.getLength() )
442 return rRowSeq
.getConstArray()[nCol
];
447 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< double > > SAL_CALL
ScTestAddIn::transpose( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< double > >& aMatrix
) throw(::com::sun::star::uno::RuntimeException
)
449 long nRowCount
= aMatrix
.getLength();
452 nColCount
= aMatrix
.getConstArray()[0].getLength();
454 uno::Sequence
< uno::Sequence
<double> > aRet( nColCount
);
455 for (long nCol
=0; nCol
<nColCount
; nCol
++)
457 uno::Sequence
<double> aSubSeq(nRowCount
);
458 for (long nRow
=0; nRow
<nRowCount
; nRow
++)
459 aSubSeq
.getArray()[nRow
] = lcl_GetDoubleElement( aMatrix
, nCol
, nRow
);
461 aRet
.getArray()[nCol
] = aSubSeq
;
467 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< sal_Int32
> > SAL_CALL
ScTestAddIn::transposeInt( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< sal_Int32
> >& aMatrix
) throw(::com::sun::star::uno::RuntimeException
)
469 long nRowCount
= aMatrix
.getLength();
472 nColCount
= aMatrix
.getConstArray()[0].getLength();
474 uno::Sequence
< uno::Sequence
<INT32
> > aRet( nColCount
);
475 for (long nCol
=0; nCol
<nColCount
; nCol
++)
477 uno::Sequence
<INT32
> aSubSeq(nRowCount
);
478 for (long nRow
=0; nRow
<nRowCount
; nRow
++)
479 aSubSeq
.getArray()[nRow
] = lcl_GetLongElement( aMatrix
, nCol
, nRow
);
481 aRet
.getArray()[nCol
] = aSubSeq
;
487 ::rtl::OUString SAL_CALL
ScTestAddIn::repeatMultiple( sal_Int32 nCount
, const ::com::sun::star::uno::Any
& aFirst
, const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aFollow
) throw(::com::sun::star::uno::RuntimeException
)
490 if ( !aFirst
.hasValue() ) // not specified
496 aSeparator
= OUStringToString( aUStr
, CHARSET_SYSTEM
);
500 long nContCount
= aFollow
.getLength();
501 const uno::Any
* pArr
= aFollow
.getConstArray();
502 for (long nPos
=0; nPos
<nContCount
; nPos
++)
507 pArr
[nPos
] >>= aUStr
;
508 aContent
+= OUStringToString( aUStr
, CHARSET_SYSTEM
);
513 for (long i
=0; i
<nCount
; i
++)
520 return StringToOUString(aRet
, CHARSET_SYSTEM
);
523 ::com::sun::star::uno::Any SAL_CALL
ScTestAddIn::getStrOrVal( sal_Int32 nFlag
) throw(::com::sun::star::uno::RuntimeException
)
530 uno::Sequence
<rtl::OUString
> aInner(3);
531 aInner
.getArray()[0] = L
"Bla";
532 aInner
.getArray()[1] = L
"Fasel";
533 aInner
.getArray()[2] = L
"Suelz";
534 uno::Sequence
< uno::Sequence
<rtl::OUString
> > aOuter( &aInner
, 1 );
536 //return uno::Any( &aOuter, Sequence< Sequence<UString> >::getReflection() );
544 aRet
<<= UString(L
"This is a string.");
551 ::com::sun::star::uno::Reference
< ::com::sun::star::sheet::XVolatileResult
> SAL_CALL
ScTestAddIn::callAsync( const ::rtl::OUString
& aString
) throw(::com::sun::star::uno::RuntimeException
)
553 String aStr
= OUStringToString( aString
, CHARSET_SYSTEM
);
554 char c
= (char) aStr
;
555 if ( c
>= '0' && c
<= '9' )
557 if (!xNumResult
.is())
558 xNumResult
= new ScAddInResult( "Num" );
563 if (!xAlphaResult
.is())
564 xAlphaResult
= new ScAddInResult( "Str" );
572 ::rtl::OUString SAL_CALL
ScTestAddIn::getServiceName( ) throw(::com::sun::star::uno::RuntimeException
)
574 return SCTESTADDIN_SERVICE
; // name of specific AddIn service
579 ::rtl::OUString SAL_CALL
ScTestAddIn::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException
)
581 return getImplementationName_Static();
584 sal_Bool SAL_CALL
ScTestAddIn::supportsService( const ::rtl::OUString
& ServiceName
) throw(::com::sun::star::uno::RuntimeException
)
586 return ServiceName
== SCADDIN_SERVICE
||
587 ServiceName
== SCTESTADDIN_SERVICE
;
590 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> SAL_CALL
ScTestAddIn::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
)
592 return getSupportedServiceNames_Static();
595 //------------------------------------------------------------------------