Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "solverutil.hxx"
21 :
22 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
23 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 : #include <com/sun/star/lang/XServiceInfo.hpp>
25 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
26 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <com/sun/star/beans/PropertyValue.hpp>
29 : #include <com/sun/star/sheet/XSolver.hpp>
30 : #include <com/sun/star/sheet/XSolverDescription.hpp>
31 :
32 : #include <comphelper/processfactory.hxx>
33 :
34 : using namespace com::sun::star;
35 :
36 : //------------------------------------------------------------------
37 :
38 : #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver"
39 :
40 0 : static uno::Reference<sheet::XSolver> lcl_CreateSolver( const uno::Reference<uno::XInterface>& xIntFac,
41 : const uno::Reference<uno::XComponentContext>& xCtx )
42 : {
43 0 : uno::Reference<sheet::XSolver> xSolver;
44 :
45 0 : uno::Reference<lang::XSingleComponentFactory> xCFac( xIntFac, uno::UNO_QUERY );
46 0 : uno::Reference<lang::XSingleServiceFactory> xFac( xIntFac, uno::UNO_QUERY );
47 0 : if ( xCFac.is() )
48 : {
49 : try
50 : {
51 0 : uno::Reference<uno::XInterface> xInterface = xCFac->createInstanceWithContext(xCtx);
52 0 : xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
53 : }
54 0 : catch(uno::Exception&)
55 : {
56 : }
57 : }
58 0 : if ( !xSolver.is() && xFac.is() )
59 : {
60 : try
61 : {
62 0 : uno::Reference<uno::XInterface> xInterface = xFac->createInstance();
63 0 : xSolver = uno::Reference<sheet::XSolver>( xInterface, uno::UNO_QUERY );
64 : }
65 0 : catch(uno::Exception&)
66 : {
67 : }
68 : }
69 :
70 0 : return xSolver;
71 : }
72 :
73 0 : void ScSolverUtil::GetImplementations( uno::Sequence<rtl::OUString>& rImplNames,
74 : uno::Sequence<rtl::OUString>& rDescriptions )
75 : {
76 0 : rImplNames.realloc(0); // clear
77 0 : rDescriptions.realloc(0);
78 :
79 : uno::Reference<uno::XComponentContext> xCtx(
80 0 : comphelper::getProcessComponentContext() );
81 : uno::Reference<lang::XMultiServiceFactory> xMSF(
82 0 : xCtx->getServiceManager(), uno::UNO_QUERY_THROW );
83 :
84 0 : uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
85 0 : if ( xEnAc.is() )
86 : {
87 : uno::Reference<container::XEnumeration> xEnum =
88 0 : xEnAc->createContentEnumeration( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SCSOLVER_SERVICE)) );
89 0 : if ( xEnum.is() )
90 : {
91 0 : sal_Int32 nCount = 0;
92 0 : while ( xEnum->hasMoreElements() )
93 : {
94 0 : uno::Any aAny = xEnum->nextElement();
95 0 : uno::Reference<uno::XInterface> xIntFac;
96 0 : aAny >>= xIntFac;
97 0 : if ( xIntFac.is() )
98 : {
99 0 : uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
100 0 : if ( xInfo.is() )
101 : {
102 0 : rtl::OUString sName = xInfo->getImplementationName();
103 0 : rtl::OUString sDescription;
104 :
105 0 : uno::Reference<sheet::XSolver> xSolver = lcl_CreateSolver( xIntFac, xCtx );
106 0 : uno::Reference<sheet::XSolverDescription> xDesc( xSolver, uno::UNO_QUERY );
107 0 : if ( xDesc.is() )
108 0 : sDescription = xDesc->getComponentDescription();
109 :
110 0 : if ( sDescription.isEmpty() )
111 0 : sDescription = sName; // use implementation name if no description available
112 :
113 0 : rImplNames.realloc( nCount+1 );
114 0 : rImplNames[nCount] = sName;
115 0 : rDescriptions.realloc( nCount+1 );
116 0 : rDescriptions[nCount] = sDescription;
117 0 : ++nCount;
118 0 : }
119 : }
120 0 : }
121 0 : }
122 0 : }
123 0 : }
124 :
125 0 : uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( const rtl::OUString& rImplName )
126 : {
127 0 : uno::Reference<sheet::XSolver> xSolver;
128 :
129 : uno::Reference<uno::XComponentContext> xCtx(
130 0 : comphelper::getProcessComponentContext() );
131 : uno::Reference<lang::XMultiServiceFactory> xMSF(
132 0 : xCtx->getServiceManager(), uno::UNO_QUERY_THROW );
133 :
134 0 : uno::Reference<container::XContentEnumerationAccess> xEnAc( xMSF, uno::UNO_QUERY );
135 0 : if ( xEnAc.is() )
136 : {
137 : uno::Reference<container::XEnumeration> xEnum =
138 0 : xEnAc->createContentEnumeration( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SCSOLVER_SERVICE)) );
139 0 : if ( xEnum.is() )
140 : {
141 0 : while ( xEnum->hasMoreElements() && !xSolver.is() )
142 : {
143 0 : uno::Any aAny = xEnum->nextElement();
144 0 : uno::Reference<uno::XInterface> xIntFac;
145 0 : aAny >>= xIntFac;
146 0 : if ( xIntFac.is() )
147 : {
148 0 : uno::Reference<lang::XServiceInfo> xInfo( xIntFac, uno::UNO_QUERY );
149 0 : if ( xInfo.is() )
150 : {
151 0 : rtl::OUString sName = xInfo->getImplementationName();
152 0 : if ( sName == rImplName )
153 0 : xSolver = lcl_CreateSolver( xIntFac, xCtx );
154 0 : }
155 : }
156 0 : }
157 0 : }
158 : }
159 :
160 : OSL_ENSURE( xSolver.is(), "can't get solver" );
161 0 : return xSolver;
162 : }
163 :
164 0 : uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( const rtl::OUString& rImplName )
165 : {
166 0 : uno::Sequence<beans::PropertyValue> aDefaults;
167 :
168 0 : uno::Reference<sheet::XSolver> xSolver = GetSolver( rImplName );
169 0 : uno::Reference<beans::XPropertySet> xPropSet( xSolver, uno::UNO_QUERY );
170 0 : if ( !xPropSet.is() )
171 : {
172 : // no XPropertySet - no options
173 : return aDefaults;
174 : }
175 :
176 : // fill maProperties
177 :
178 0 : uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
179 : OSL_ENSURE( xInfo.is(), "can't get property set info" );
180 0 : if ( !xInfo.is() )
181 : return aDefaults;
182 :
183 0 : uno::Sequence<beans::Property> aPropSeq = xInfo->getProperties();
184 0 : const sal_Int32 nSize = aPropSeq.getLength();
185 0 : aDefaults.realloc(nSize);
186 0 : sal_Int32 nValid = 0;
187 0 : for (sal_Int32 nPos=0; nPos<nSize; ++nPos)
188 : {
189 0 : const beans::Property& rProp = aPropSeq[nPos];
190 0 : uno::Any aValue = xPropSet->getPropertyValue( rProp.Name );
191 0 : uno::TypeClass eClass = aValue.getValueTypeClass();
192 : // only use properties of supported types
193 0 : if ( eClass == uno::TypeClass_BOOLEAN || eClass == uno::TypeClass_LONG || eClass == uno::TypeClass_DOUBLE )
194 0 : aDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE );
195 0 : }
196 0 : aDefaults.realloc(nValid);
197 :
198 : //! get user-visible names, sort by them
199 :
200 0 : return aDefaults;
201 : }
202 :
203 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|