Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / workben / addin.cxx
blobdbc538b6165f05472e7809c87bbe6afe7d8b44af
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 #ifdef _MSC_VER
21 #pragma hdrstop
22 #endif
24 #include <usr/factoryhlp.hxx>
25 #include <usr/macros.hxx>
26 #include <usr/reflserv.hxx>
27 #include <osl/mutex.hxx>
28 #include <vcl/svapp.hxx>
29 #include <tools/date.hxx>
30 #include <svl/itemprop.hxx>
31 #include <usr/proptypehlp.hxx>
32 #include <cppuhelper/factory.hxx>
33 #include <uno/lbnames.h>
34 #include <osl/diagnose.h>
36 #include <com/sun/star/util/date.hpp>
38 #include "addin.hxx"
39 #include "result.hxx"
41 using namespace com::sun::star;
43 //------------------------------------------------------------------------
46 //------------------------------------------------------------------------
48 SMART_UNO_IMPLEMENTATION( ScTestAddIn, UsrObject );
50 #define SCADDIN_SERVICE L"com.sun.star.sheet.AddIn"
51 #define SCTESTADDIN_SERVICE L"stardiv.one.sheet.DemoAddIn"
53 //------------------------------------------------------------------------
55 extern "C" {
57 sal_Bool SAL_CALL component_writeInfo(
58 void * pServiceManager, registry::XRegistryKey * pRegistryKey )
60 if (pRegistryKey)
62 try
64 UString aImpl = L"/";
65 aImpl += ScTestAddIn::getImplementationName_Static();
66 aImpl += L"/UNO/SERVICES";
68 uno::Reference<registry::XRegistryKey> xNewKey(
69 reinterpret_cast<registry::XRegistryKey*>(pRegistryKey)->createKey(aImpl) );
71 uno::Sequence <OUString> aSequ = ScTestAddIn::getSupportedServiceNames_Static();
72 const OUString * pArray = aSequ.getConstArray();
73 for( INT32 i = 0; i < aSequ.getLength(); i++ )
74 xNewKey->createKey( pArray[i] );
76 return sal_True;
78 catch (registry::InvalidRegistryException&)
80 OSL_ENSHURE( false, "### InvalidRegistryException!" );
83 return false;
86 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
87 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
89 void* pRet = 0;
91 if ( pServiceManager && UString(pImplName) == ScTestAddIn::getImplementationName_Static() )
93 uno::Reference<lang::XSingleServiceFactory> xFactory( cppu::createOneInstanceFactory(
94 reinterpret_cast<lang::XMultiServiceFactory*>(pServiceManager),
95 ScTestAddIn::getImplementationName_Static(),
96 ScTestAddIn_CreateInstance,
97 ScTestAddIn::getSupportedServiceNames_Static() ) );
99 if (xFactory.is())
101 xFactory->acquire();
102 pRet = xFactory.get();
106 return pRet;
109 } // extern C
111 //------------------------------------------------------------------------
113 ScTestAddIn::ScTestAddIn()
117 ScTestAddIn::~ScTestAddIn()
121 UString ScTestAddIn::getImplementationName_Static()
123 return L"stardiv.StarCalc.ScTestAddIn";
126 ::com::sun::star::uno::Sequence< OUString > ScTestAddIn::getSupportedServiceNames_Static()
128 uno::Sequence< OUString > aRet(2);
129 OUString* pArray = aRet.getArray();
130 pArray[0] = SCADDIN_SERVICE;
131 pArray[1] = SCTESTADDIN_SERVICE;
132 return aRet;
135 uno::Reference<uno::XInterface> ScTestAddIn_CreateInstance(
136 const uno::Reference<lang::XMultiServiceFactory>& )
138 static uno::Reference<uno::XInterface> xInst = (cppu::OWeakObject*)new ScTestAddIn();
139 return xInst;
143 // XAddIn
145 UString ScTestAddIn::getProgrammaticFuntionName(const UString& aDisplayName)
146 THROWS( (UsrSystemException) )
148 //!...
149 return UString();
152 UString ScTestAddIn::getDisplayFunctionName(const UString& aProgrammaticName)
153 THROWS( (UsrSystemException) )
155 // return translated strings
157 UString aRet;
158 if ( aProgrammaticName == L"countParams" ) aRet = L"ParamAnzahl";
159 else if ( aProgrammaticName == L"addOne" ) aRet = L"PlusEins";
160 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"WiederholeString";
161 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Datumsstring";
162 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Farbwert";
163 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponieren";
164 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"IntegerTransponieren";
165 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Mehrfach";
166 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"StringOderWert";
167 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Asynchron";
168 return aRet;
171 UString ScTestAddIn::getFunctionDescription(const UString& aProgrammaticName)
172 THROWS( (UsrSystemException) )
174 // return translated strings
176 UString aRet;
177 if ( aProgrammaticName == L"countParams" ) aRet = L"Gibt die Anzahl der Parameter zurueck.";
178 else if ( aProgrammaticName == L"addOne" ) aRet = L"Addiert 1 zur uebergebenen Zahl.";
179 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"Wiederholt eine Zeichenkette.";
180 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Wandelt ein Datum in eine Zeichenkette.";
181 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Gibt den Farbwert eines Zellbereichs zurueck. Bei transparentem Hintergrund wird -1 zurueckgegeben";
182 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponiert eine Matrix.";
183 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"Transponiert eine Matrix mit Ganzzahlen.";
184 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Wiederholt mehrere Bestandteile.";
185 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"Gibt einen String oder einen Wert zurueck.";
186 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Test fuer asynchrone Funktion.";
187 return aRet;
190 UString ScTestAddIn::getDisplayArgumentName(const UString& aProgrammaticFunctionName,
191 INT32 nArgument) THROWS( (UsrSystemException) )
193 // return translated strings
195 UString aRet;
196 if ( aProgrammaticFunctionName == L"countParams" )
198 if ( nArgument == 0 ) aRet = L"Parameter";
200 else if ( aProgrammaticFunctionName == L"addOne" )
202 if ( nArgument == 0 ) aRet = L"Wert";
204 else if ( aProgrammaticFunctionName == L"repeatStr" )
206 if ( nArgument == 0 ) aRet = L"String";
207 else if ( nArgument == 1 ) aRet = L"Anzahl";
209 else if ( aProgrammaticFunctionName == L"getDateString" )
211 if ( nArgument == 0 ) aRet = L"Dokument";
212 else if ( nArgument == 1 ) aRet = L"Wert";
214 else if ( aProgrammaticFunctionName == L"getColorValue" )
216 if ( nArgument == 0 ) aRet = L"Bereich";
218 else if ( aProgrammaticFunctionName == L"transpose" )
220 if ( nArgument == 0 ) aRet = L"Matrix";
222 else if ( aProgrammaticFunctionName == L"transposeInt" )
224 if ( nArgument == 0 ) aRet = L"Matrix";
226 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
228 if ( nArgument == 0 ) aRet = L"Anzahl";
229 else if ( nArgument == 1 ) aRet = L"Trenner";
230 else if ( nArgument == 2 ) aRet = L"Inhalt";
232 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
234 if ( nArgument == 0 ) aRet = L"Flag";
236 else if ( aProgrammaticFunctionName == L"callAsync" )
238 if ( nArgument == 0 ) aRet = L"Name";
240 return aRet;
243 UString ScTestAddIn::getArgumentDescription(const UString& aProgrammaticFunctionName,
244 INT32 nArgument) THROWS( (UsrSystemException) )
246 // return translated strings
248 UString aRet;
249 if ( aProgrammaticFunctionName == L"countParams" )
251 if ( nArgument == 0 ) aRet = L"Beliebiger Parameter";
253 else if ( aProgrammaticFunctionName == L"addOne" )
255 if ( nArgument == 0 ) aRet = L"Der Wert, zu dem 1 addiert wird";
257 else if ( aProgrammaticFunctionName == L"repeatStr" )
259 if ( nArgument == 0 ) aRet = L"Der Text, der wiederholt wird";
260 else if ( nArgument == 1 ) aRet = L"Die Anzahl der Wiederholungen";
262 else if ( aProgrammaticFunctionName == L"getDateString" )
264 if ( nArgument == 0 ) aRet = L"(intern)";
265 else if ( nArgument == 1 ) aRet = L"Der Wert, der als Datum formatiert wird";
267 else if ( aProgrammaticFunctionName == L"getColorValue" )
269 if ( nArgument == 0 ) aRet = L"Der Bereich, dessen Hintergrundfarbe abgefragt wird";
271 else if ( aProgrammaticFunctionName == L"transpose" )
273 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
275 else if ( aProgrammaticFunctionName == L"transposeInt" )
277 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
279 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
281 if ( nArgument == 0 ) aRet = L"Anzahl der Wiederholungen";
282 else if ( nArgument == 1 ) aRet = L"Text, der zwischen den Inhalten erscheint";
283 else if ( nArgument == 2 ) aRet = L"Mehrere Inhalte";
285 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
287 if ( nArgument == 0 ) aRet = L"Wenn Flag 0 ist, wird ein Wert zurueckgegeben, sonst ein String.";
289 else if ( aProgrammaticFunctionName == L"callAsync" )
291 if ( nArgument == 0 ) aRet = L"Ein String";
293 return aRet;
296 UString ScTestAddIn::getProgrammaticCategoryName(const UString& aProgrammaticFunctionName)
297 THROWS( (UsrSystemException) )
299 // return non-translated strings
301 UString aRet;
302 if ( aProgrammaticFunctionName == L"countParams" ) aRet = L"Information";
303 else if ( aProgrammaticFunctionName == L"addOne" ) aRet = L"Mathematical";
304 else if ( aProgrammaticFunctionName == L"repeatStr" ) aRet = L"Text";
305 else if ( aProgrammaticFunctionName == L"getDateString" ) aRet = L"Date&Time";
306 else if ( aProgrammaticFunctionName == L"getColorValue" ) aRet = L"Spreadsheet";
307 else if ( aProgrammaticFunctionName == L"transpose" ) aRet = L"Matrix";
308 else if ( aProgrammaticFunctionName == L"transposeInt" ) aRet = L"Matrix";
309 else if ( aProgrammaticFunctionName == L"repeatMultiple" )aRet = L"Text";
310 else if ( aProgrammaticFunctionName == L"getStrOrVal" ) aRet = L"Add-In";
311 else if ( aProgrammaticFunctionName == L"callAsync" ) aRet = L"Realtime"; // new group
312 return aRet;
315 UString ScTestAddIn::getDisplayCategoryName(const UString& aProgrammaticFunctionName)
316 THROWS( (UsrSystemException) )
318 // return translated strings
320 return L"irgendwas"; // not used for predefined categories
323 // XLocalizable
325 void ScTestAddIn::setLocale(const lang::Locale& eLocale) THROWS( (UsrSystemException) )
327 aFuncLoc = eLocale;
330 ::com::sun::star::lang::Locale SAL_CALL ScTestAddIn::getLocale( ) throw(::com::sun::star::uno::RuntimeException)
332 return aFuncLoc;
335 // XTestAddIn
337 sal_Int32 SAL_CALL ScTestAddIn::countParams( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArgs ) throw(::com::sun::star::uno::RuntimeException)
339 return aArgs.getLength();
342 double SAL_CALL ScTestAddIn::addOne( double fValue ) throw(::com::sun::star::uno::RuntimeException)
344 return fValue + 1.0;
347 OUString SAL_CALL ScTestAddIn::repeatStr( const OUString& aStr, sal_Int32 nCount ) throw(::com::sun::star::uno::RuntimeException)
349 String aRet;
350 String aStrStr = OUStringToString( aStr, CHARSET_SYSTEM );
351 for (long i=0; i<nCount; i++)
352 aRet += aStrStr;
354 return StringToOUString( aRet, CHARSET_SYSTEM );
357 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)
359 uno::Any aDateAny = xCaller->getPropertyValue( L"NullDate" );
361 util::Date aDate;
362 aDateAny >>= aDate;
364 Date aNewDate( aDate.Day, aDate.Month, aDate.Year );
366 aNewDate += (long)(fValue+0.5);
368 String aRet;
369 aRet += aNewDate.GetDay();
370 aRet += '.';
371 aRet += aNewDate.GetMonth();
372 aRet += '.';
373 aRet += aNewDate.GetYear();
374 return StringToOUString( aRet, CHARSET_SYSTEM );
378 return L"**ERROR**";
381 sal_Int32 SAL_CALL ScTestAddIn::getColorValue( const ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange >& xRange ) throw(::com::sun::star::uno::RuntimeException)
383 uno::Reference<beans::XPropertySet> xProp( xRange, uno::UNO_QUERY );
384 if (xProp.is())
386 long nRet = -1;
387 uno::Any aTrans = xProp->getPropertyValue( L"IsCellBackgroundTransparent" );
388 sal_Bool bIsTrans;
389 aTrans >>= bIsTrans; //! dont use >>= for BOOL
390 if (!bIsTrans)
392 uno::Any aCol = xProp->getPropertyValue( L"CellBackColor" );
393 aCol >>= nRet;
395 return nRet;
397 return 0;
400 double lcl_GetDoubleElement( const uno::Sequence< uno::Sequence<double> >& aMatrix, long nCol, long nRow )
402 if ( nRow < aMatrix.getLength() )
404 const uno::Sequence<double>& rRowSeq = aMatrix.getConstArray()[nRow];
405 if ( nCol < rRowSeq.getLength() )
406 return rRowSeq.getConstArray()[nCol];
408 return 0.0; // error
411 INT32 lcl_GetLongElement( const uno::Sequence< uno::Sequence<INT32> >& aMatrix, long nCol, long nRow )
413 if ( nRow < aMatrix.getLength() )
415 const uno::Sequence<INT32>& rRowSeq = aMatrix.getConstArray()[nRow];
416 if ( nCol < rRowSeq.getLength() )
417 return rRowSeq.getConstArray()[nCol];
419 return 0.0; // error
422 ::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)
424 long nRowCount = aMatrix.getLength();
425 long nColCount = 0;
426 if ( nRowCount )
427 nColCount = aMatrix.getConstArray()[0].getLength();
429 uno::Sequence< uno::Sequence<double> > aRet( nColCount );
430 for (long nCol=0; nCol<nColCount; nCol++)
432 uno::Sequence<double> aSubSeq(nRowCount);
433 for (long nRow=0; nRow<nRowCount; nRow++)
434 aSubSeq.getArray()[nRow] = lcl_GetDoubleElement( aMatrix, nCol, nRow );
436 aRet.getArray()[nCol] = aSubSeq;
439 return aRet;
442 ::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)
444 long nRowCount = aMatrix.getLength();
445 long nColCount = 0;
446 if ( nRowCount )
447 nColCount = aMatrix.getConstArray()[0].getLength();
449 uno::Sequence< uno::Sequence<INT32> > aRet( nColCount );
450 for (long nCol=0; nCol<nColCount; nCol++)
452 uno::Sequence<INT32> aSubSeq(nRowCount);
453 for (long nRow=0; nRow<nRowCount; nRow++)
454 aSubSeq.getArray()[nRow] = lcl_GetLongElement( aMatrix, nCol, nRow );
456 aRet.getArray()[nCol] = aSubSeq;
459 return aRet;
462 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)
464 String aSeparator;
465 if ( !aFirst.hasValue() ) // not specified
466 aSeparator = ';';
467 else
469 OUString aUStr;
470 aFirst >>= aUStr;
471 aSeparator = OUStringToString( aUStr, CHARSET_SYSTEM );
474 String aContent;
475 long nContCount = aFollow.getLength();
476 const uno::Any* pArr = aFollow.getConstArray();
477 for (long nPos=0; nPos<nContCount; nPos++)
479 if ( nPos > 0 )
480 aContent += ' ';
481 OUString aUStr;
482 pArr[nPos] >>= aUStr;
483 aContent += OUStringToString( aUStr, CHARSET_SYSTEM );
486 String aRet;
488 for (long i=0; i<nCount; i++)
490 if (i>0)
491 aRet += aSeparator;
492 aRet += aContent;
495 return StringToOUString(aRet, CHARSET_SYSTEM);
498 ::com::sun::star::uno::Any SAL_CALL ScTestAddIn::getStrOrVal( sal_Int32 nFlag ) throw(::com::sun::star::uno::RuntimeException)
500 uno::Any aRet;
502 //! Test
503 if ( nFlag == 42 )
505 uno::Sequence<OUString> aInner(3);
506 aInner.getArray()[0] = L"Bla";
507 aInner.getArray()[1] = L"Fasel";
508 aInner.getArray()[2] = L"Suelz";
509 uno::Sequence< uno::Sequence<OUString> > aOuter( &aInner, 1 );
511 aRet <<= aOuter;
512 return aRet;
514 //! Test
516 if ( nFlag )
517 aRet <<= UString(L"This is a string.");
518 else
519 aRet <<= (INT32)42;
521 return aRet;
524 ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XVolatileResult > SAL_CALL ScTestAddIn::callAsync( const OUString& aString ) throw(::com::sun::star::uno::RuntimeException)
526 String aStr = OUStringToString( aString, CHARSET_SYSTEM );
527 char c = (char) aStr;
528 if ( c >= '0' && c <= '9' )
530 if (!xNumResult.is())
531 xNumResult = new ScAddInResult( "Num" );
532 return xNumResult;
534 else
536 if (!xAlphaResult.is())
537 xAlphaResult = new ScAddInResult( "Str" );
538 return xAlphaResult;
543 // XServiceName
545 OUString SAL_CALL ScTestAddIn::getServiceName( ) throw(::com::sun::star::uno::RuntimeException)
547 return SCTESTADDIN_SERVICE; // name of specific AddIn service
550 // XServiceInfo
552 OUString SAL_CALL ScTestAddIn::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException)
554 return getImplementationName_Static();
557 sal_Bool SAL_CALL ScTestAddIn::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
559 return ServiceName == SCADDIN_SERVICE ||
560 ServiceName == SCTESTADDIN_SERVICE;
563 ::com::sun::star::uno::Sequence< OUString > SAL_CALL ScTestAddIn::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException)
565 return getSupportedServiceNames_Static();
568 //------------------------------------------------------------------------
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */