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/XServiceInfo.hpp>
24 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/sheet/XSolver.hpp>
28 #include <com/sun/star/sheet/XSolverDescription.hpp>
30 #include <osl/diagnose.h>
31 #include <comphelper/processfactory.hxx>
32 #include <sal/log.hxx>
33 #include <comphelper/diagnose_ex.hxx>
35 using namespace com::sun::star
;
37 constexpr OUString SCSOLVER_SERVICE
= u
"com.sun.star.sheet.Solver"_ustr
;
39 void ScSolverUtil::GetImplementations( uno::Sequence
<OUString
>& rImplNames
,
40 uno::Sequence
<OUString
>& rDescriptions
)
42 rImplNames
.realloc(0); // clear
43 rDescriptions
.realloc(0);
45 const uno::Reference
<uno::XComponentContext
>& xCtx(
46 comphelper::getProcessComponentContext() );
48 uno::Reference
<container::XContentEnumerationAccess
> xEnAc(
49 xCtx
->getServiceManager(), uno::UNO_QUERY
);
53 uno::Reference
<container::XEnumeration
> xEnum
=
54 xEnAc
->createContentEnumeration( SCSOLVER_SERVICE
);
59 while ( xEnum
->hasMoreElements() )
61 uno::Any aAny
= xEnum
->nextElement();
62 uno::Reference
<lang::XServiceInfo
> xInfo
;
66 uno::Reference
<lang::XSingleComponentFactory
> xCFac( xInfo
, uno::UNO_QUERY
);
69 OUString sName
= xInfo
->getImplementationName();
73 uno::Reference
<sheet::XSolver
> xSolver(
74 xCFac
->createInstanceWithContext(xCtx
), uno::UNO_QUERY
);
75 uno::Reference
<sheet::XSolverDescription
> xDesc( xSolver
, uno::UNO_QUERY
);
76 OUString sDescription
;
78 sDescription
= xDesc
->getComponentDescription();
80 if ( sDescription
.isEmpty() )
81 sDescription
= sName
; // use implementation name if no description available
83 rImplNames
.realloc( nCount
+1 );
84 rImplNames
.getArray()[nCount
] = sName
;
85 rDescriptions
.realloc( nCount
+1 );
86 rDescriptions
.getArray()[nCount
] = sDescription
;
89 catch (const css::uno::Exception
&)
91 TOOLS_INFO_EXCEPTION("sc.ui", "ScSolverUtil::GetImplementations: cannot instantiate: " << sName
);
98 uno::Reference
<sheet::XSolver
> ScSolverUtil::GetSolver( std::u16string_view rImplName
)
100 uno::Reference
<sheet::XSolver
> xSolver
;
102 const uno::Reference
<uno::XComponentContext
>& xCtx(
103 comphelper::getProcessComponentContext() );
105 uno::Reference
<container::XContentEnumerationAccess
> xEnAc(
106 xCtx
->getServiceManager(), uno::UNO_QUERY
);
109 uno::Reference
<container::XEnumeration
> xEnum
=
110 xEnAc
->createContentEnumeration( SCSOLVER_SERVICE
);
113 while ( xEnum
->hasMoreElements() && !xSolver
.is() )
115 uno::Any aAny
= xEnum
->nextElement();
116 uno::Reference
<lang::XServiceInfo
> xInfo
;
120 uno::Reference
<lang::XSingleComponentFactory
> xCFac( xInfo
, uno::UNO_QUERY
);
123 OUString sName
= xInfo
->getImplementationName();
124 if ( sName
== rImplName
)
125 xSolver
.set( xCFac
->createInstanceWithContext(xCtx
), uno::UNO_QUERY
);
132 SAL_WARN_IF( !xSolver
.is(), "sc.ui", "can't get solver" );
136 uno::Sequence
<beans::PropertyValue
> ScSolverUtil::GetDefaults( std::u16string_view rImplName
)
138 uno::Sequence
<beans::PropertyValue
> aDefaults
;
140 uno::Reference
<sheet::XSolver
> xSolver
= GetSolver( rImplName
);
141 uno::Reference
<beans::XPropertySet
> xPropSet( xSolver
, uno::UNO_QUERY
);
142 if ( !xPropSet
.is() )
144 // no XPropertySet - no options
150 uno::Reference
<beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
151 OSL_ENSURE( xInfo
.is(), "can't get property set info" );
155 const uno::Sequence
<beans::Property
> aPropSeq
= xInfo
->getProperties();
156 const sal_Int32 nSize
= aPropSeq
.getLength();
157 aDefaults
.realloc(nSize
);
158 auto pDefaults
= aDefaults
.getArray();
159 sal_Int32 nValid
= 0;
160 for (const beans::Property
& rProp
: aPropSeq
)
162 uno::Any aValue
= xPropSet
->getPropertyValue( rProp
.Name
);
163 uno::TypeClass eClass
= aValue
.getValueTypeClass();
164 // only use properties of supported types
165 if (eClass
== uno::TypeClass_BOOLEAN
|| eClass
== uno::TypeClass_LONG
|| eClass
== uno::TypeClass_SHORT
166 || eClass
== uno::TypeClass_BYTE
|| eClass
== uno::TypeClass_DOUBLE
)
167 pDefaults
[nValid
++] = beans::PropertyValue( rProp
.Name
, -1, aValue
, beans::PropertyState_DIRECT_VALUE
);
169 aDefaults
.realloc(nValid
);
171 //! get user-visible names, sort by them
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */