LibreOffice Module sc (master) 1
solverutil.cxx
Go to the documentation of this file.
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/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>
29
30#include <osl/diagnose.h>
32#include <sal/log.hxx>
33#include <tools/diagnose_ex.h>
34
35using namespace com::sun::star;
36
37constexpr OUStringLiteral SCSOLVER_SERVICE = u"com.sun.star.sheet.Solver";
38
39void ScSolverUtil::GetImplementations( uno::Sequence<OUString>& rImplNames,
40 uno::Sequence<OUString>& rDescriptions )
41{
42 rImplNames.realloc(0); // clear
43 rDescriptions.realloc(0);
44
45 uno::Reference<uno::XComponentContext> xCtx(
47
48 uno::Reference<container::XContentEnumerationAccess> xEnAc(
49 xCtx->getServiceManager(), uno::UNO_QUERY );
50 if ( !xEnAc.is() )
51 return;
52
53 uno::Reference<container::XEnumeration> xEnum =
54 xEnAc->createContentEnumeration( SCSOLVER_SERVICE );
55 if ( !xEnum.is() )
56 return;
57
58 sal_Int32 nCount = 0;
59 while ( xEnum->hasMoreElements() )
60 {
61 uno::Any aAny = xEnum->nextElement();
62 uno::Reference<lang::XServiceInfo> xInfo;
63 aAny >>= xInfo;
64 if ( xInfo.is() )
65 {
66 uno::Reference<lang::XSingleComponentFactory> xCFac( xInfo, uno::UNO_QUERY );
67 if ( xCFac.is() )
68 {
69 OUString sName = xInfo->getImplementationName();
70
71 try
72 {
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;
77 if ( xDesc.is() )
78 sDescription = xDesc->getComponentDescription();
79
80 if ( sDescription.isEmpty() )
81 sDescription = sName; // use implementation name if no description available
82
83 rImplNames.realloc( nCount+1 );
84 rImplNames.getArray()[nCount] = sName;
85 rDescriptions.realloc( nCount+1 );
86 rDescriptions.getArray()[nCount] = sDescription;
87 ++nCount;
88 }
89 catch (const css::uno::Exception&)
90 {
91 TOOLS_INFO_EXCEPTION("sc.ui", "ScSolverUtil::GetImplementations: cannot instantiate: " << sName);
92 }
93 }
94 }
95 }
96}
97
98uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( std::u16string_view rImplName )
99{
100 uno::Reference<sheet::XSolver> xSolver;
101
102 uno::Reference<uno::XComponentContext> xCtx(
104
105 uno::Reference<container::XContentEnumerationAccess> xEnAc(
106 xCtx->getServiceManager(), uno::UNO_QUERY );
107 if ( xEnAc.is() )
108 {
109 uno::Reference<container::XEnumeration> xEnum =
110 xEnAc->createContentEnumeration( SCSOLVER_SERVICE );
111 if ( xEnum.is() )
112 {
113 while ( xEnum->hasMoreElements() && !xSolver.is() )
114 {
115 uno::Any aAny = xEnum->nextElement();
116 uno::Reference<lang::XServiceInfo> xInfo;
117 aAny >>= xInfo;
118 if ( xInfo.is() )
119 {
120 uno::Reference<lang::XSingleComponentFactory> xCFac( xInfo, uno::UNO_QUERY );
121 if ( xCFac.is() )
122 {
123 OUString sName = xInfo->getImplementationName();
124 if ( sName == rImplName )
125 xSolver.set( xCFac->createInstanceWithContext(xCtx), uno::UNO_QUERY );
126 }
127 }
128 }
129 }
130 }
131
132 SAL_WARN_IF( !xSolver.is(), "sc.ui", "can't get solver" );
133 return xSolver;
134}
135
136uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( std::u16string_view rImplName )
137{
138 uno::Sequence<beans::PropertyValue> aDefaults;
139
140 uno::Reference<sheet::XSolver> xSolver = GetSolver( rImplName );
141 uno::Reference<beans::XPropertySet> xPropSet( xSolver, uno::UNO_QUERY );
142 if ( !xPropSet.is() )
143 {
144 // no XPropertySet - no options
145 return aDefaults;
146 }
147
148 // fill maProperties
149
150 uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
151 OSL_ENSURE( xInfo.is(), "can't get property set info" );
152 if ( !xInfo.is() )
153 return aDefaults;
154
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)
161 {
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_DOUBLE )
166 pDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE );
167 }
168 aDefaults.realloc(nValid);
169
171
172 return aDefaults;
173}
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void GetImplementations(css::uno::Sequence< OUString > &rImplNames, css::uno::Sequence< OUString > &rDescriptions)
Definition: solverutil.cxx:39
static css::uno::Sequence< css::beans::PropertyValue > GetDefaults(std::u16string_view rImplName)
Definition: solverutil.cxx:136
static css::uno::Reference< css::sheet::XSolver > GetSolver(std::u16string_view rImplName)
Definition: solverutil.cxx:98
int nCount
#define TOOLS_INFO_EXCEPTION(area, stream)
float u
#define SAL_WARN_IF(condition, area, stream)
const char * sName
Reference< XComponentContext > getProcessComponentContext()
constexpr OUStringLiteral SCSOLVER_SERVICE
Definition: solverutil.cxx:37