Bump version to 5.0-14
[LibreOffice.git] / sc / workben / addin.cxx
blob60089b318a197cddf12a714cbdc4beeb13212fd4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <usr/factoryhlp.hxx>
21 #include <usr/macros.hxx>
22 #include <usr/reflserv.hxx>
23 #include <osl/mutex.hxx>
24 #include <vcl/svapp.hxx>
25 #include <tools/date.hxx>
26 #include <svl/itemprop.hxx>
27 #include <usr/proptypehlp.hxx>
28 #include <cppuhelper/factory.hxx>
29 #include <osl/diagnose.h>
31 #include <com/sun/star/util/date.hpp>
33 #include "addin.hxx"
34 #include "result.hxx"
36 using namespace com::sun::star;
38 SMART_UNO_IMPLEMENTATION( ScTestAddIn, UsrObject );
40 #define SCTESTADDIN_SERVICE L"stardiv.one.sheet.DemoAddIn"
42 extern "C" {
44 sal_Bool SAL_CALL component_writeInfo(
45 void * pServiceManager, registry::XRegistryKey * pRegistryKey )
47 if (pRegistryKey)
49 try
51 UString aImpl = L"/";
52 aImpl += ScTestAddIn::getImplementationName_Static();
53 aImpl += L"/UNO/SERVICES";
55 uno::Reference<registry::XRegistryKey> xNewKey(
56 reinterpret_cast<registry::XRegistryKey*>(pRegistryKey)->createKey(aImpl) );
58 uno::Sequence <OUString> aSequ = ScTestAddIn::getSupportedServiceNames_Static();
59 const OUString * pArray = aSequ.getConstArray();
60 for( INT32 i = 0; i < aSequ.getLength(); i++ )
61 xNewKey->createKey( pArray[i] );
63 return sal_True;
65 catch (registry::InvalidRegistryException&)
67 OSL_ENSHURE( false, "### InvalidRegistryException!" );
70 return false;
73 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
74 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
76 void* pRet = 0;
78 if ( pServiceManager && UString(pImplName) == ScTestAddIn::getImplementationName_Static() )
80 uno::Reference<lang::XSingleServiceFactory> xFactory( cppu::createOneInstanceFactory(
81 reinterpret_cast<lang::XMultiServiceFactory*>(pServiceManager),
82 ScTestAddIn::getImplementationName_Static(),
83 ScTestAddIn_CreateInstance,
84 ScTestAddIn::getSupportedServiceNames_Static() ) );
86 if (xFactory.is())
88 xFactory->acquire();
89 pRet = xFactory.get();
93 return pRet;
96 } // extern C
98 ScTestAddIn::ScTestAddIn()
102 ScTestAddIn::~ScTestAddIn()
106 UString ScTestAddIn::getImplementationName_Static()
108 return L"stardiv.StarCalc.ScTestAddIn";
111 ::com::sun::star::uno::Sequence< OUString > ScTestAddIn::getSupportedServiceNames_Static()
113 uno::Sequence< OUString > aRet(2);
114 OUString* pArray = aRet.getArray();
115 pArray[0] = L"com.sun.star.sheet.AddIn";
116 pArray[1] = SCTESTADDIN_SERVICE;
117 return aRet;
120 uno::Reference<uno::XInterface> ScTestAddIn_CreateInstance(
121 const uno::Reference<lang::XMultiServiceFactory>& )
123 return (cppu::OWeakObject*)new ScTestAddIn();
126 // XAddIn
128 UString ScTestAddIn::getProgrammaticFuntionName(const UString& aDisplayName)
129 THROWS( (UsrSystemException) )
131 //!...
132 return UString();
135 UString ScTestAddIn::getDisplayFunctionName(const UString& aProgrammaticName)
136 THROWS( (UsrSystemException) )
138 // return translated strings
140 UString aRet;
141 if ( aProgrammaticName == L"countParams" ) aRet = L"ParamAnzahl";
142 else if ( aProgrammaticName == L"addOne" ) aRet = L"PlusEins";
143 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"WiederholeString";
144 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Datumsstring";
145 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Farbwert";
146 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponieren";
147 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"IntegerTransponieren";
148 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Mehrfach";
149 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"StringOderWert";
150 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Asynchron";
151 return aRet;
154 UString ScTestAddIn::getFunctionDescription(const UString& aProgrammaticName)
155 THROWS( (UsrSystemException) )
157 // return translated strings
159 UString aRet;
160 if ( aProgrammaticName == L"countParams" ) aRet = L"Gibt die Anzahl der Parameter zurueck.";
161 else if ( aProgrammaticName == L"addOne" ) aRet = L"Addiert 1 zur uebergebenen Zahl.";
162 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"Wiederholt eine Zeichenkette.";
163 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Wandelt ein Datum in eine Zeichenkette.";
164 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Gibt den Farbwert eines Zellbereichs zurueck. Bei transparentem Hintergrund wird -1 zurueckgegeben";
165 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponiert eine Matrix.";
166 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"Transponiert eine Matrix mit Ganzzahlen.";
167 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Wiederholt mehrere Bestandteile.";
168 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"Gibt einen String oder einen Wert zurueck.";
169 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Test fuer asynchrone Funktion.";
170 return aRet;
173 UString ScTestAddIn::getDisplayArgumentName(const UString& aProgrammaticFunctionName,
174 INT32 nArgument) THROWS( (UsrSystemException) )
176 // return translated strings
178 UString aRet;
179 if ( aProgrammaticFunctionName == L"countParams" )
181 if ( nArgument == 0 ) aRet = L"Parameter";
183 else if ( aProgrammaticFunctionName == L"addOne" )
185 if ( nArgument == 0 ) aRet = L"Wert";
187 else if ( aProgrammaticFunctionName == L"repeatStr" )
189 if ( nArgument == 0 ) aRet = L"String";
190 else if ( nArgument == 1 ) aRet = L"Anzahl";
192 else if ( aProgrammaticFunctionName == L"getDateString" )
194 if ( nArgument == 0 ) aRet = L"Dokument";
195 else if ( nArgument == 1 ) aRet = L"Wert";
197 else if ( aProgrammaticFunctionName == L"getColorValue" )
199 if ( nArgument == 0 ) aRet = L"Bereich";
201 else if ( aProgrammaticFunctionName == L"transpose" )
203 if ( nArgument == 0 ) aRet = L"Matrix";
205 else if ( aProgrammaticFunctionName == L"transposeInt" )
207 if ( nArgument == 0 ) aRet = L"Matrix";
209 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
211 if ( nArgument == 0 ) aRet = L"Anzahl";
212 else if ( nArgument == 1 ) aRet = L"Trenner";
213 else if ( nArgument == 2 ) aRet = L"Inhalt";
215 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
217 if ( nArgument == 0 ) aRet = L"Flag";
219 else if ( aProgrammaticFunctionName == L"callAsync" )
221 if ( nArgument == 0 ) aRet = L"Name";
223 return aRet;
226 UString ScTestAddIn::getArgumentDescription(const UString& aProgrammaticFunctionName,
227 INT32 nArgument) THROWS( (UsrSystemException) )
229 // return translated strings
231 UString aRet;
232 if ( aProgrammaticFunctionName == L"countParams" )
234 if ( nArgument == 0 ) aRet = L"Beliebiger Parameter";
236 else if ( aProgrammaticFunctionName == L"addOne" )
238 if ( nArgument == 0 ) aRet = L"Der Wert, zu dem 1 addiert wird";
240 else if ( aProgrammaticFunctionName == L"repeatStr" )
242 if ( nArgument == 0 ) aRet = L"Der Text, der wiederholt wird";
243 else if ( nArgument == 1 ) aRet = L"Die Anzahl der Wiederholungen";
245 else if ( aProgrammaticFunctionName == L"getDateString" )
247 if ( nArgument == 0 ) aRet = L"(intern)";
248 else if ( nArgument == 1 ) aRet = L"Der Wert, der als Datum formatiert wird";
250 else if ( aProgrammaticFunctionName == L"getColorValue" )
252 if ( nArgument == 0 ) aRet = L"Der Bereich, dessen Hintergrundfarbe abgefragt wird";
254 else if ( aProgrammaticFunctionName == L"transpose" )
256 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
258 else if ( aProgrammaticFunctionName == L"transposeInt" )
260 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
262 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
264 if ( nArgument == 0 ) aRet = L"Anzahl der Wiederholungen";
265 else if ( nArgument == 1 ) aRet = L"Text, der zwischen den Inhalten erscheint";
266 else if ( nArgument == 2 ) aRet = L"Mehrere Inhalte";
268 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
270 if ( nArgument == 0 ) aRet = L"Wenn Flag 0 ist, wird ein Wert zurueckgegeben, sonst ein String.";
272 else if ( aProgrammaticFunctionName == L"callAsync" )
274 if ( nArgument == 0 ) aRet = L"Ein String";
276 return aRet;
279 UString ScTestAddIn::getProgrammaticCategoryName(const UString& aProgrammaticFunctionName)
280 THROWS( (UsrSystemException) )
282 // return non-translated strings
284 UString aRet;
285 if ( aProgrammaticFunctionName == L"countParams" ) aRet = L"Information";
286 else if ( aProgrammaticFunctionName == L"addOne" ) aRet = L"Mathematical";
287 else if ( aProgrammaticFunctionName == L"repeatStr" ) aRet = L"Text";
288 else if ( aProgrammaticFunctionName == L"getDateString" ) aRet = L"Date&Time";
289 else if ( aProgrammaticFunctionName == L"getColorValue" ) aRet = L"Spreadsheet";
290 else if ( aProgrammaticFunctionName == L"transpose" ) aRet = L"Matrix";
291 else if ( aProgrammaticFunctionName == L"transposeInt" ) aRet = L"Matrix";
292 else if ( aProgrammaticFunctionName == L"repeatMultiple" )aRet = L"Text";
293 else if ( aProgrammaticFunctionName == L"getStrOrVal" ) aRet = L"Add-In";
294 else if ( aProgrammaticFunctionName == L"callAsync" ) aRet = L"Realtime"; // new group
295 return aRet;
298 UString ScTestAddIn::getDisplayCategoryName(const UString& aProgrammaticFunctionName)
299 THROWS( (UsrSystemException) )
301 // return translated strings
303 return L"irgendwas"; // not used for predefined categories
306 // XLocalizable
308 void ScTestAddIn::setLocale(const lang::Locale& eLocale) THROWS( (UsrSystemException) )
310 aFuncLoc = eLocale;
313 ::com::sun::star::lang::Locale SAL_CALL ScTestAddIn::getLocale( ) throw(::com::sun::star::uno::RuntimeException)
315 return aFuncLoc;
318 // XTestAddIn
320 sal_Int32 SAL_CALL ScTestAddIn::countParams( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArgs ) throw(::com::sun::star::uno::RuntimeException)
322 return aArgs.getLength();
325 double SAL_CALL ScTestAddIn::addOne( double fValue ) throw(::com::sun::star::uno::RuntimeException)
327 return fValue + 1.0;
330 OUString SAL_CALL ScTestAddIn::repeatStr( const OUString& aStr, sal_Int32 nCount ) throw(::com::sun::star::uno::RuntimeException)
332 String aRet;
333 String aStrStr = OUStringToString( aStr, CHARSET_SYSTEM );
334 for (long i=0; i<nCount; i++)
335 aRet += aStrStr;
337 return StringToOUString( aRet, CHARSET_SYSTEM );
340 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)
342 uno::Any aDateAny = xCaller->getPropertyValue( L"NullDate" );
344 util::Date aDate;
345 aDateAny >>= aDate;
347 Date aNewDate( aDate.Day, aDate.Month, aDate.Year );
349 aNewDate += (long)(fValue+0.5);
351 String aRet;
352 aRet += aNewDate.GetDay();
353 aRet += '.';
354 aRet += aNewDate.GetMonth();
355 aRet += '.';
356 aRet += aNewDate.GetYear();
357 return StringToOUString( aRet, CHARSET_SYSTEM );
361 return L"**ERROR**";
364 sal_Int32 SAL_CALL ScTestAddIn::getColorValue( const ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange >& xRange ) throw(::com::sun::star::uno::RuntimeException)
366 uno::Reference<beans::XPropertySet> xProp( xRange, uno::UNO_QUERY );
367 if (xProp.is())
369 long nRet = -1;
370 uno::Any aTrans = xProp->getPropertyValue( L"IsCellBackgroundTransparent" );
371 sal_Bool bIsTrans;
372 aTrans >>= bIsTrans; //! dont use >>= for BOOL
373 if (!bIsTrans)
375 uno::Any aCol = xProp->getPropertyValue( L"CellBackColor" );
376 aCol >>= nRet;
378 return nRet;
380 return 0;
383 double lcl_GetDoubleElement( const uno::Sequence< uno::Sequence<double> >& aMatrix, long nCol, long nRow )
385 if ( nRow < aMatrix.getLength() )
387 const uno::Sequence<double>& rRowSeq = aMatrix.getConstArray()[nRow];
388 if ( nCol < rRowSeq.getLength() )
389 return rRowSeq.getConstArray()[nCol];
391 return 0.0; // error
394 INT32 lcl_GetLongElement( const uno::Sequence< uno::Sequence<INT32> >& aMatrix, long nCol, long nRow )
396 if ( nRow < aMatrix.getLength() )
398 const uno::Sequence<INT32>& rRowSeq = aMatrix.getConstArray()[nRow];
399 if ( nCol < rRowSeq.getLength() )
400 return rRowSeq.getConstArray()[nCol];
402 return 0.0; // error
405 ::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)
407 long nRowCount = aMatrix.getLength();
408 long nColCount = 0;
409 if ( nRowCount )
410 nColCount = aMatrix.getConstArray()[0].getLength();
412 uno::Sequence< uno::Sequence<double> > aRet( nColCount );
413 for (long nCol=0; nCol<nColCount; nCol++)
415 uno::Sequence<double> aSubSeq(nRowCount);
416 for (long nRow=0; nRow<nRowCount; nRow++)
417 aSubSeq.getArray()[nRow] = lcl_GetDoubleElement( aMatrix, nCol, nRow );
419 aRet.getArray()[nCol] = aSubSeq;
422 return aRet;
425 ::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)
427 long nRowCount = aMatrix.getLength();
428 long nColCount = 0;
429 if ( nRowCount )
430 nColCount = aMatrix.getConstArray()[0].getLength();
432 uno::Sequence< uno::Sequence<INT32> > aRet( nColCount );
433 for (long nCol=0; nCol<nColCount; nCol++)
435 uno::Sequence<INT32> aSubSeq(nRowCount);
436 for (long nRow=0; nRow<nRowCount; nRow++)
437 aSubSeq.getArray()[nRow] = lcl_GetLongElement( aMatrix, nCol, nRow );
439 aRet.getArray()[nCol] = aSubSeq;
442 return aRet;
445 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)
447 String aSeparator;
448 if ( !aFirst.hasValue() ) // not specified
449 aSeparator = ';';
450 else
452 OUString aUStr;
453 aFirst >>= aUStr;
454 aSeparator = OUStringToString( aUStr, CHARSET_SYSTEM );
457 String aContent;
458 long nContCount = aFollow.getLength();
459 const uno::Any* pArr = aFollow.getConstArray();
460 for (long nPos=0; nPos<nContCount; nPos++)
462 if ( nPos > 0 )
463 aContent += ' ';
464 OUString aUStr;
465 pArr[nPos] >>= aUStr;
466 aContent += OUStringToString( aUStr, CHARSET_SYSTEM );
469 String aRet;
471 for (long i=0; i<nCount; i++)
473 if (i>0)
474 aRet += aSeparator;
475 aRet += aContent;
478 return StringToOUString(aRet, CHARSET_SYSTEM);
481 ::com::sun::star::uno::Any SAL_CALL ScTestAddIn::getStrOrVal( sal_Int32 nFlag ) throw(::com::sun::star::uno::RuntimeException)
483 uno::Any aRet;
485 //! Test
486 if ( nFlag == 42 )
488 uno::Sequence<OUString> aInner(3);
489 aInner.getArray()[0] = L"Bla";
490 aInner.getArray()[1] = L"Fasel";
491 aInner.getArray()[2] = L"Suelz";
492 uno::Sequence< uno::Sequence<OUString> > aOuter( &aInner, 1 );
494 aRet <<= aOuter;
495 return aRet;
497 //! Test
499 if ( nFlag )
500 aRet <<= UString(L"This is a string.");
501 else
502 aRet <<= (INT32)42;
504 return aRet;
507 ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XVolatileResult > SAL_CALL ScTestAddIn::callAsync( const OUString& aString ) throw(::com::sun::star::uno::RuntimeException)
509 String aStr = OUStringToString( aString, CHARSET_SYSTEM );
510 char c = (char) aStr;
511 if ( c >= '0' && c <= '9' )
513 if (!xNumResult.is())
514 xNumResult = new ScAddInResult( "Num" );
515 return xNumResult;
517 else
519 if (!xAlphaResult.is())
520 xAlphaResult = new ScAddInResult( "Str" );
521 return xAlphaResult;
525 // XServiceName
526 OUString SAL_CALL ScTestAddIn::getServiceName( ) throw(::com::sun::star::uno::RuntimeException)
528 return SCTESTADDIN_SERVICE; // name of specific AddIn service
531 // XServiceInfo
532 OUString SAL_CALL ScTestAddIn::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException)
534 return getImplementationName_Static();
537 sal_Bool SAL_CALL ScTestAddIn::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
539 return cppu::supportsService(this, ServiceName);
542 ::com::sun::star::uno::Sequence< OUString > SAL_CALL ScTestAddIn::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException)
544 return getSupportedServiceNames_Static();
547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */