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