1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "solverutil.hxx"
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>
32 #include <comphelper/processfactory.hxx>
34 using namespace com::sun::star
;
36 //------------------------------------------------------------------
38 #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver"
40 static uno::Reference
<sheet::XSolver
> lcl_CreateSolver( const uno::Reference
<uno::XInterface
>& xIntFac
,
41 const uno::Reference
<uno::XComponentContext
>& xCtx
)
43 uno::Reference
<sheet::XSolver
> xSolver
;
45 uno::Reference
<lang::XSingleComponentFactory
> xCFac( xIntFac
, uno::UNO_QUERY
);
46 uno::Reference
<lang::XSingleServiceFactory
> xFac( xIntFac
, uno::UNO_QUERY
);
51 uno::Reference
<uno::XInterface
> xInterface
= xCFac
->createInstanceWithContext(xCtx
);
52 xSolver
= uno::Reference
<sheet::XSolver
>( xInterface
, uno::UNO_QUERY
);
54 catch(uno::Exception
&)
58 if ( !xSolver
.is() && xFac
.is() )
62 uno::Reference
<uno::XInterface
> xInterface
= xFac
->createInstance();
63 xSolver
= uno::Reference
<sheet::XSolver
>( xInterface
, uno::UNO_QUERY
);
65 catch(uno::Exception
&)
73 void ScSolverUtil::GetImplementations( uno::Sequence
<OUString
>& rImplNames
,
74 uno::Sequence
<OUString
>& rDescriptions
)
76 rImplNames
.realloc(0); // clear
77 rDescriptions
.realloc(0);
79 uno::Reference
<uno::XComponentContext
> xCtx(
80 comphelper::getProcessComponentContext() );
81 uno::Reference
<lang::XMultiServiceFactory
> xMSF(
82 xCtx
->getServiceManager(), uno::UNO_QUERY_THROW
);
84 uno::Reference
<container::XContentEnumerationAccess
> xEnAc( xMSF
, uno::UNO_QUERY
);
87 uno::Reference
<container::XEnumeration
> xEnum
=
88 xEnAc
->createContentEnumeration( OUString(SCSOLVER_SERVICE
) );
92 while ( xEnum
->hasMoreElements() )
94 uno::Any aAny
= xEnum
->nextElement();
95 uno::Reference
<uno::XInterface
> xIntFac
;
99 uno::Reference
<lang::XServiceInfo
> xInfo( xIntFac
, uno::UNO_QUERY
);
102 OUString sName
= xInfo
->getImplementationName();
103 OUString sDescription
;
105 uno::Reference
<sheet::XSolver
> xSolver
= lcl_CreateSolver( xIntFac
, xCtx
);
106 uno::Reference
<sheet::XSolverDescription
> xDesc( xSolver
, uno::UNO_QUERY
);
108 sDescription
= xDesc
->getComponentDescription();
110 if ( sDescription
.isEmpty() )
111 sDescription
= sName
; // use implementation name if no description available
113 rImplNames
.realloc( nCount
+1 );
114 rImplNames
[nCount
] = sName
;
115 rDescriptions
.realloc( nCount
+1 );
116 rDescriptions
[nCount
] = sDescription
;
125 uno::Reference
<sheet::XSolver
> ScSolverUtil::GetSolver( const OUString
& rImplName
)
127 uno::Reference
<sheet::XSolver
> xSolver
;
129 uno::Reference
<uno::XComponentContext
> xCtx(
130 comphelper::getProcessComponentContext() );
131 uno::Reference
<lang::XMultiServiceFactory
> xMSF(
132 xCtx
->getServiceManager(), uno::UNO_QUERY_THROW
);
134 uno::Reference
<container::XContentEnumerationAccess
> xEnAc( xMSF
, uno::UNO_QUERY
);
137 uno::Reference
<container::XEnumeration
> xEnum
=
138 xEnAc
->createContentEnumeration( OUString(SCSOLVER_SERVICE
) );
141 while ( xEnum
->hasMoreElements() && !xSolver
.is() )
143 uno::Any aAny
= xEnum
->nextElement();
144 uno::Reference
<uno::XInterface
> xIntFac
;
148 uno::Reference
<lang::XServiceInfo
> xInfo( xIntFac
, uno::UNO_QUERY
);
151 OUString sName
= xInfo
->getImplementationName();
152 if ( sName
== rImplName
)
153 xSolver
= lcl_CreateSolver( xIntFac
, xCtx
);
160 OSL_ENSURE( xSolver
.is(), "can't get solver" );
164 uno::Sequence
<beans::PropertyValue
> ScSolverUtil::GetDefaults( const OUString
& rImplName
)
166 uno::Sequence
<beans::PropertyValue
> aDefaults
;
168 uno::Reference
<sheet::XSolver
> xSolver
= GetSolver( rImplName
);
169 uno::Reference
<beans::XPropertySet
> xPropSet( xSolver
, uno::UNO_QUERY
);
170 if ( !xPropSet
.is() )
172 // no XPropertySet - no options
178 uno::Reference
<beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
179 OSL_ENSURE( xInfo
.is(), "can't get property set info" );
183 uno::Sequence
<beans::Property
> aPropSeq
= xInfo
->getProperties();
184 const sal_Int32 nSize
= aPropSeq
.getLength();
185 aDefaults
.realloc(nSize
);
186 sal_Int32 nValid
= 0;
187 for (sal_Int32 nPos
=0; nPos
<nSize
; ++nPos
)
189 const beans::Property
& rProp
= aPropSeq
[nPos
];
190 uno::Any aValue
= xPropSet
->getPropertyValue( rProp
.Name
);
191 uno::TypeClass eClass
= aValue
.getValueTypeClass();
192 // only use properties of supported types
193 if ( eClass
== uno::TypeClass_BOOLEAN
|| eClass
== uno::TypeClass_LONG
|| eClass
== uno::TypeClass_DOUBLE
)
194 aDefaults
[nValid
++] = beans::PropertyValue( rProp
.Name
, -1, aValue
, beans::PropertyState_DIRECT_VALUE
);
196 aDefaults
.realloc(nValid
);
198 //! get user-visible names, sort by them
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */