1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir //------------------------------------------------------------------
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "solverutil.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XSingleComponentFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
37cdf0e10cSrcweir #include <com/sun/star/sheet/XSolver.hpp>
38cdf0e10cSrcweir #include <com/sun/star/sheet/XSolverDescription.hpp>
39cdf0e10cSrcweir
40cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
41cdf0e10cSrcweir
42cdf0e10cSrcweir using namespace com::sun::star;
43cdf0e10cSrcweir
44cdf0e10cSrcweir //------------------------------------------------------------------
45cdf0e10cSrcweir
46cdf0e10cSrcweir #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver"
47cdf0e10cSrcweir
lcl_CreateSolver(const uno::Reference<uno::XInterface> & xIntFac,const uno::Reference<uno::XComponentContext> & xCtx)48cdf0e10cSrcweir uno::Reference<sheet::XSolver> lcl_CreateSolver( const uno::Reference<uno::XInterface>& xIntFac,
49cdf0e10cSrcweir const uno::Reference<uno::XComponentContext>& xCtx )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver;
52cdf0e10cSrcweir
53cdf0e10cSrcweir uno::Reference<lang::XSingleComponentFactory> xCFac( xIntFac, uno::UNO_QUERY );
54cdf0e10cSrcweir uno::Reference<lang::XSingleServiceFactory> xFac( xIntFac, uno::UNO_QUERY );
55cdf0e10cSrcweir if ( xCFac.is() )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir try
58cdf0e10cSrcweir {
59cdf0e10cSrcweir uno::Reference<uno::XInterface> xInterface = xCFac->createInstanceWithContext(xCtx);
60cdf0e10cSrcweir xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
61cdf0e10cSrcweir }
62cdf0e10cSrcweir catch(uno::Exception&)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir }
66cdf0e10cSrcweir if ( !xSolver.is() && xFac.is() )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir try
69cdf0e10cSrcweir {
70cdf0e10cSrcweir uno::Reference<uno::XInterface> xInterface = xFac->createInstance();
71cdf0e10cSrcweir xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
72cdf0e10cSrcweir }
73cdf0e10cSrcweir catch(uno::Exception&)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir }
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir return xSolver;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir // static
GetImplementations(uno::Sequence<rtl::OUString> & rImplNames,uno::Sequence<rtl::OUString> & rDescriptions)82cdf0e10cSrcweir void ScSolverUtil::GetImplementations( uno::Sequence<rtl::OUString>& rImplNames,
83cdf0e10cSrcweir uno::Sequence<rtl::OUString>& rDescriptions )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir rImplNames.realloc(0); // clear
86cdf0e10cSrcweir rDescriptions.realloc(0);
87cdf0e10cSrcweir sal_Int32 nCount = 0;
88cdf0e10cSrcweir
89cdf0e10cSrcweir uno::Reference<uno::XComponentContext> xCtx;
90cdf0e10cSrcweir uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory();
91cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY);
92cdf0e10cSrcweir try
93cdf0e10cSrcweir {
94cdf0e10cSrcweir xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx;
95cdf0e10cSrcweir }
96cdf0e10cSrcweir catch ( uno::Exception & )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
100cdf0e10cSrcweir uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
101cdf0e10cSrcweir if ( xCtx.is() && xEnAc.is() )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir uno::Reference<container::XEnumeration> xEnum =
104cdf0e10cSrcweir xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) );
105cdf0e10cSrcweir if ( xEnum.is() )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir while ( xEnum->hasMoreElements() )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir uno::Any aAny = xEnum->nextElement();
110cdf0e10cSrcweir uno::Reference<uno::XInterface> xIntFac;
111cdf0e10cSrcweir aAny >>= xIntFac;
112cdf0e10cSrcweir if ( xIntFac.is() )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
115cdf0e10cSrcweir if ( xInfo.is() )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir rtl::OUString sName = xInfo->getImplementationName();
118cdf0e10cSrcweir rtl::OUString sDescription;
119cdf0e10cSrcweir
120cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver = lcl_CreateSolver( xIntFac, xCtx );
121cdf0e10cSrcweir uno::Reference<sheet::XSolverDescription> xDesc( xSolver, uno::UNO_QUERY );
122cdf0e10cSrcweir if ( xDesc.is() )
123cdf0e10cSrcweir sDescription = xDesc->getComponentDescription();
124cdf0e10cSrcweir
125cdf0e10cSrcweir if ( !sDescription.getLength() )
126cdf0e10cSrcweir sDescription = sName; // use implementation name if no description available
127cdf0e10cSrcweir
128cdf0e10cSrcweir rImplNames.realloc( nCount+1 );
129cdf0e10cSrcweir rImplNames[nCount] = sName;
130cdf0e10cSrcweir rDescriptions.realloc( nCount+1 );
131cdf0e10cSrcweir rDescriptions[nCount] = sDescription;
132cdf0e10cSrcweir ++nCount;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir // static
GetSolver(const rtl::OUString & rImplName)141cdf0e10cSrcweir uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( const rtl::OUString& rImplName )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver;
144cdf0e10cSrcweir
145cdf0e10cSrcweir uno::Reference<uno::XComponentContext> xCtx;
146cdf0e10cSrcweir uno::Reference<lang::XMultiServiceFactory> xMSF = comphelper::getProcessServiceFactory();
147cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropset(xMSF, uno::UNO_QUERY);
148cdf0e10cSrcweir try
149cdf0e10cSrcweir {
150cdf0e10cSrcweir xPropset->getPropertyValue(rtl::OUString::createFromAscii("DefaultContext")) >>= xCtx;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir catch ( uno::Exception & )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
157cdf0e10cSrcweir if ( xCtx.is() && xEnAc.is() )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir uno::Reference<container::XEnumeration> xEnum =
160cdf0e10cSrcweir xEnAc->createContentEnumeration( rtl::OUString::createFromAscii(SCSOLVER_SERVICE) );
161cdf0e10cSrcweir if ( xEnum.is() )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir while ( xEnum->hasMoreElements() && !xSolver.is() )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir uno::Any aAny = xEnum->nextElement();
166cdf0e10cSrcweir uno::Reference<uno::XInterface> xIntFac;
167cdf0e10cSrcweir aAny >>= xIntFac;
168cdf0e10cSrcweir if ( xIntFac.is() )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
171cdf0e10cSrcweir if ( xInfo.is() )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir rtl::OUString sName = xInfo->getImplementationName();
174cdf0e10cSrcweir if ( sName == rImplName )
175cdf0e10cSrcweir xSolver = lcl_CreateSolver( xIntFac, xCtx );
176cdf0e10cSrcweir }
177cdf0e10cSrcweir }
178cdf0e10cSrcweir }
179cdf0e10cSrcweir }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir OSL_ENSURE( xSolver.is(), "can't get solver" );
183cdf0e10cSrcweir return xSolver;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir // static
GetDefaults(const rtl::OUString & rImplName)187cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( const rtl::OUString& rImplName )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> aDefaults;
190cdf0e10cSrcweir
191cdf0e10cSrcweir uno::Reference<sheet::XSolver> xSolver = GetSolver( rImplName );
192cdf0e10cSrcweir uno::Reference<beans::XPropertySet> xPropSet( xSolver, uno::UNO_QUERY );
193cdf0e10cSrcweir if ( !xPropSet.is() )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir // no XPropertySet - no options
196cdf0e10cSrcweir return aDefaults;
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
199cdf0e10cSrcweir // fill maProperties
200cdf0e10cSrcweir
201cdf0e10cSrcweir uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
202cdf0e10cSrcweir OSL_ENSURE( xInfo.is(), "can't get property set info" );
203cdf0e10cSrcweir if ( !xInfo.is() )
204cdf0e10cSrcweir return aDefaults;
205cdf0e10cSrcweir
206cdf0e10cSrcweir uno::Sequence<beans::Property> aPropSeq = xInfo->getProperties();
207cdf0e10cSrcweir const sal_Int32 nSize = aPropSeq.getLength();
208cdf0e10cSrcweir aDefaults.realloc(nSize);
209cdf0e10cSrcweir sal_Int32 nValid = 0;
210cdf0e10cSrcweir for (sal_Int32 nPos=0; nPos<nSize; ++nPos)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir const beans::Property& rProp = aPropSeq[nPos];
213cdf0e10cSrcweir uno::Any aValue = xPropSet->getPropertyValue( rProp.Name );
214cdf0e10cSrcweir uno::TypeClass eClass = aValue.getValueTypeClass();
215cdf0e10cSrcweir // only use properties of supported types
216cdf0e10cSrcweir if ( eClass == uno::TypeClass_BOOLEAN || eClass == uno::TypeClass_LONG || eClass == uno::TypeClass_DOUBLE )
217cdf0e10cSrcweir aDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE );
218cdf0e10cSrcweir }
219cdf0e10cSrcweir aDefaults.realloc(nValid);
220cdf0e10cSrcweir
221cdf0e10cSrcweir //! get user-visible names, sort by them
222cdf0e10cSrcweir
223cdf0e10cSrcweir return aDefaults;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir
226