1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sc.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //------------------------------------------------------------------ 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include "solverutil.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleComponentFactory.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/sheet/XSolver.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/sheet/XSolverDescription.hpp> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir using namespace com::sun::star; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir //------------------------------------------------------------------ 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver" 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir uno::Reference<sheet::XSolver> lcl_CreateSolver( const uno::Reference<uno::XInterface>& xIntFac, 53*cdf0e10cSrcweir const uno::Reference<uno::XComponentContext>& xCtx ) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir uno::Reference<lang::XSingleComponentFactory> xCFac( xIntFac, uno::UNO_QUERY ); 58*cdf0e10cSrcweir uno::Reference<lang::XSingleServiceFactory> xFac( xIntFac, uno::UNO_QUERY ); 59*cdf0e10cSrcweir if ( xCFac.is() ) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir try 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir uno::Reference<uno::XInterface> xInterface = xCFac->createInstanceWithContext(xCtx); 64*cdf0e10cSrcweir xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY ); 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir catch(uno::Exception&) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir if ( !xSolver.is() && xFac.is() ) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir try 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir uno::Reference<uno::XInterface> xInterface = xFac->createInstance(); 75*cdf0e10cSrcweir xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY ); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir catch(uno::Exception&) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir return xSolver; 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir // static 86*cdf0e10cSrcweir void ScSolverUtil::GetImplementations( uno::Sequence<rtl::OUString>& rImplNames, 87*cdf0e10cSrcweir uno::Sequence<rtl::OUString>& rDescriptions ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir rImplNames.realloc(0); // clear 90*cdf0e10cSrcweir rDescriptions.realloc(0); 91*cdf0e10cSrcweir sal_Int32 nCount = 0; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir uno::Reference<uno::XComponentContext> xCtx; 94*cdf0e10cSrcweir uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory(); 95*cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY); 96*cdf0e10cSrcweir try 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir catch ( uno::Exception & ) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY ); 105*cdf0e10cSrcweir if ( xCtx.is() && xEnAc.is() ) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir uno::Reference<container::XEnumeration> xEnum = 108*cdf0e10cSrcweir xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) ); 109*cdf0e10cSrcweir if ( xEnum.is() ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir while ( xEnum->hasMoreElements() ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir uno::Any aAny = xEnum->nextElement(); 114*cdf0e10cSrcweir uno::Reference<uno::XInterface> xIntFac; 115*cdf0e10cSrcweir aAny >>= xIntFac; 116*cdf0e10cSrcweir if ( xIntFac.is() ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY ); 119*cdf0e10cSrcweir if ( xInfo.is() ) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir rtl::OUString sName = xInfo->getImplementationName(); 122*cdf0e10cSrcweir rtl::OUString sDescription; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver = lcl_CreateSolver( xIntFac, xCtx ); 125*cdf0e10cSrcweir uno::Reference<sheet::XSolverDescription> xDesc( xSolver, uno::UNO_QUERY ); 126*cdf0e10cSrcweir if ( xDesc.is() ) 127*cdf0e10cSrcweir sDescription = xDesc->getComponentDescription(); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir if ( !sDescription.getLength() ) 130*cdf0e10cSrcweir sDescription = sName; // use implementation name if no description available 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir rImplNames.realloc( nCount+1 ); 133*cdf0e10cSrcweir rImplNames[nCount] = sName; 134*cdf0e10cSrcweir rDescriptions.realloc( nCount+1 ); 135*cdf0e10cSrcweir rDescriptions[nCount] = sDescription; 136*cdf0e10cSrcweir ++nCount; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // static 145*cdf0e10cSrcweir uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( const rtl::OUString& rImplName ) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver; 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir uno::Reference<uno::XComponentContext> xCtx; 150*cdf0e10cSrcweir uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory(); 151*cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY); 152*cdf0e10cSrcweir try 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir catch ( uno::Exception & ) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY ); 161*cdf0e10cSrcweir if ( xCtx.is() && xEnAc.is() ) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir uno::Reference<container::XEnumeration> xEnum = 164*cdf0e10cSrcweir xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) ); 165*cdf0e10cSrcweir if ( xEnum.is() ) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir while ( xEnum->hasMoreElements() && !xSolver.is() ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir uno::Any aAny = xEnum->nextElement(); 170*cdf0e10cSrcweir uno::Reference<uno::XInterface> xIntFac; 171*cdf0e10cSrcweir aAny >>= xIntFac; 172*cdf0e10cSrcweir if ( xIntFac.is() ) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY ); 175*cdf0e10cSrcweir if ( xInfo.is() ) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir rtl::OUString sName = xInfo->getImplementationName(); 178*cdf0e10cSrcweir if ( sName == rImplName ) 179*cdf0e10cSrcweir xSolver = lcl_CreateSolver( xIntFac, xCtx ); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir OSL_ENSURE( xSolver.is(), "can't get solver" ); 187*cdf0e10cSrcweir return xSolver; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir // static 191*cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( const rtl::OUString& rImplName ) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> aDefaults; 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver = GetSolver( rImplName ); 196*cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropSet( xSolver, uno::UNO_QUERY ); 197*cdf0e10cSrcweir if ( !xPropSet.is() ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir // no XPropertySet - no options 200*cdf0e10cSrcweir return aDefaults; 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir // fill maProperties 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo(); 206*cdf0e10cSrcweir OSL_ENSURE( xInfo.is(), "can't get property set info" ); 207*cdf0e10cSrcweir if ( !xInfo.is() ) 208*cdf0e10cSrcweir return aDefaults; 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir uno::Sequence<beans::Property> aPropSeq = xInfo->getProperties(); 211*cdf0e10cSrcweir const sal_Int32 nSize = aPropSeq.getLength(); 212*cdf0e10cSrcweir aDefaults.realloc(nSize); 213*cdf0e10cSrcweir sal_Int32 nValid = 0; 214*cdf0e10cSrcweir for (sal_Int32 nPos=0; nPos<nSize; ++nPos) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir const beans::Property& rProp = aPropSeq[nPos]; 217*cdf0e10cSrcweir uno::Any aValue = xPropSet->getPropertyValue( rProp.Name ); 218*cdf0e10cSrcweir uno::TypeClass eClass = aValue.getValueTypeClass(); 219*cdf0e10cSrcweir // only use properties of supported types 220*cdf0e10cSrcweir if ( eClass == uno::TypeClass_BOOLEAN || eClass == uno::TypeClass_LONG || eClass == uno::TypeClass_DOUBLE ) 221*cdf0e10cSrcweir aDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir aDefaults.realloc(nValid); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir //! get user-visible names, sort by them 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir return aDefaults; 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir 230