sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / solverutil.cxx
blob6b3d73e98f3ce28d85391f5195b4a853ccbb280e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solverutil.cxx,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 //------------------------------------------------------------------
36 #include "solverutil.hxx"
38 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
41 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #include <com/sun/star/sheet/XSolver.hpp>
45 #include <com/sun/star/sheet/XSolverDescription.hpp>
47 #include <comphelper/processfactory.hxx>
49 using namespace com::sun::star;
51 //------------------------------------------------------------------
53 #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver"
55 uno::Reference<sheet::XSolver> lcl_CreateSolver( const uno::Reference<uno::XInterface>& xIntFac,
56 const uno::Reference<uno::XComponentContext>& xCtx )
58 uno::Reference<sheet::XSolver> xSolver;
60 uno::Reference<lang::XSingleComponentFactory> xCFac( xIntFac, uno::UNO_QUERY );
61 uno::Reference<lang::XSingleServiceFactory> xFac( xIntFac, uno::UNO_QUERY );
62 if ( xCFac.is() )
64 try
66 uno::Reference<uno::XInterface> xInterface = xCFac->createInstanceWithContext(xCtx);
67 xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
69 catch(uno::Exception&)
73 if ( !xSolver.is() && xFac.is() )
75 try
77 uno::Reference<uno::XInterface> xInterface = xFac->createInstance();
78 xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
80 catch(uno::Exception&)
85 return xSolver;
88 // static
89 void ScSolverUtil::GetImplementations( uno::Sequence<rtl::OUString>& rImplNames,
90 uno::Sequence<rtl::OUString>& rDescriptions )
92 rImplNames.realloc(0); // clear
93 rDescriptions.realloc(0);
94 sal_Int32 nCount = 0;
96 uno::Reference<uno::XComponentContext> xCtx;
97 uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory();
98 uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY);
99 try
101 xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx;
103 catch ( uno::Exception & )
107 uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
108 if ( xCtx.is() && xEnAc.is() )
110 uno::Reference<container::XEnumeration> xEnum =
111 xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) );
112 if ( xEnum.is() )
114 while ( xEnum->hasMoreElements() )
116 uno::Any aAny = xEnum->nextElement();
117 uno::Reference<uno::XInterface> xIntFac;
118 aAny >>= xIntFac;
119 if ( xIntFac.is() )
121 uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
122 if ( xInfo.is() )
124 rtl::OUString sName = xInfo->getImplementationName();
125 rtl::OUString sDescription;
127 uno::Reference<sheet::XSolver> xSolver = lcl_CreateSolver( xIntFac, xCtx );
128 uno::Reference<sheet::XSolverDescription> xDesc( xSolver, uno::UNO_QUERY );
129 if ( xDesc.is() )
130 sDescription = xDesc->getComponentDescription();
132 if ( !sDescription.getLength() )
133 sDescription = sName; // use implementation name if no description available
135 rImplNames.realloc( nCount+1 );
136 rImplNames[nCount] = sName;
137 rDescriptions.realloc( nCount+1 );
138 rDescriptions[nCount] = sDescription;
139 ++nCount;
147 // static
148 uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( const rtl::OUString& rImplName )
150 uno::Reference<sheet::XSolver> xSolver;
152 uno::Reference<uno::XComponentContext> xCtx;
153 uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory();
154 uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY);
157 xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx;
159 catch ( uno::Exception & )
163 uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
164 if ( xCtx.is() && xEnAc.is() )
166 uno::Reference<container::XEnumeration> xEnum =
167 xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) );
168 if ( xEnum.is() )
170 while ( xEnum->hasMoreElements() && !xSolver.is() )
172 uno::Any aAny = xEnum->nextElement();
173 uno::Reference<uno::XInterface> xIntFac;
174 aAny >>= xIntFac;
175 if ( xIntFac.is() )
177 uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
178 if ( xInfo.is() )
180 rtl::OUString sName = xInfo->getImplementationName();
181 if ( sName == rImplName )
182 xSolver = lcl_CreateSolver( xIntFac, xCtx );
189 OSL_ENSURE( xSolver.is(), "can't get solver" );
190 return xSolver;
193 // static
194 uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( const rtl::OUString& rImplName )
196 uno::Sequence<beans::PropertyValue> aDefaults;
198 uno::Reference<sheet::XSolver> xSolver = GetSolver( rImplName );
199 uno::Reference<beans::XPropertySet> xPropSet( xSolver, uno::UNO_QUERY );
200 if ( !xPropSet.is() )
202 // no XPropertySet - no options
203 return aDefaults;
206 // fill maProperties
208 uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
209 OSL_ENSURE( xInfo.is(), "can't get property set info" );
210 if ( !xInfo.is() )
211 return aDefaults;
213 uno::Sequence<beans::Property> aPropSeq = xInfo->getProperties();
214 const sal_Int32 nSize = aPropSeq.getLength();
215 aDefaults.realloc(nSize);
216 sal_Int32 nValid = 0;
217 for (sal_Int32 nPos=0; nPos<nSize; ++nPos)
219 const beans::Property& rProp = aPropSeq[nPos];
220 uno::Any aValue = xPropSet->getPropertyValue( rProp.Name );
221 uno::TypeClass eClass = aValue.getValueTypeClass();
222 // only use properties of supported types
223 if ( eClass == uno::TypeClass_BOOLEAN || eClass == uno::TypeClass_LONG || eClass == uno::TypeClass_DOUBLE )
224 aDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE );
226 aDefaults.realloc(nValid);
228 //! get user-visible names, sort by them
230 return aDefaults;