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 
35 using namespace com::sun::star;
36 
37 #define SCSOLVER_SERVICE "com.sun.star.sheet.Solver"
38 
39 void 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  {
52  uno::Reference<container::XEnumeration> xEnum =
53  xEnAc->createContentEnumeration( SCSOLVER_SERVICE );
54  if ( xEnum.is() )
55  {
56  sal_Int32 nCount = 0;
57  while ( xEnum->hasMoreElements() )
58  {
59  uno::Any aAny = xEnum->nextElement();
60  uno::Reference<lang::XServiceInfo> xInfo;
61  aAny >>= xInfo;
62  if ( xInfo.is() )
63  {
64  uno::Reference<lang::XSingleComponentFactory> xCFac( xInfo, uno::UNO_QUERY );
65  if ( xCFac.is() )
66  {
67  OUString sName = xInfo->getImplementationName();
68  OUString sDescription;
69 
70  try
71  {
72  uno::Reference<sheet::XSolver> xSolver(
73  xCFac->createInstanceWithContext(xCtx), uno::UNO_QUERY );
74  uno::Reference<sheet::XSolverDescription> xDesc( xSolver, uno::UNO_QUERY );
75  if ( xDesc.is() )
76  sDescription = xDesc->getComponentDescription();
77 
78  if ( sDescription.isEmpty() )
79  sDescription = sName; // use implementation name if no description available
80 
81  rImplNames.realloc( nCount+1 );
82  rImplNames[nCount] = sName;
83  rDescriptions.realloc( nCount+1 );
84  rDescriptions[nCount] = sDescription;
85  ++nCount;
86  }
87  catch (const css::uno::Exception&)
88  {
89  TOOLS_INFO_EXCEPTION("sc.ui", "ScSolverUtil::GetImplementations: cannot instantiate: " << sName);
90  }
91  }
92  }
93  }
94  }
95  }
96 }
97 
98 uno::Reference<sheet::XSolver> ScSolverUtil::GetSolver( const OUString& 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 
136 uno::Sequence<beans::PropertyValue> ScSolverUtil::GetDefaults( const OUString& 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  sal_Int32 nValid = 0;
159  for (const beans::Property& rProp : aPropSeq)
160  {
161  uno::Any aValue = xPropSet->getPropertyValue( rProp.Name );
162  uno::TypeClass eClass = aValue.getValueTypeClass();
163  // only use properties of supported types
164  if ( eClass == uno::TypeClass_BOOLEAN || eClass == uno::TypeClass_LONG || eClass == uno::TypeClass_DOUBLE )
165  aDefaults[nValid++] = beans::PropertyValue( rProp.Name, -1, aValue, beans::PropertyState_DIRECT_VALUE );
166  }
167  aDefaults.realloc(nValid);
168 
170 
171  return aDefaults;
172 }
173 
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int nCount
static css::uno::Sequence< css::beans::PropertyValue > GetDefaults(const OUString &rImplName)
Definition: solverutil.cxx:136
static void GetImplementations(css::uno::Sequence< OUString > &rImplNames, css::uno::Sequence< OUString > &rDescriptions)
Definition: solverutil.cxx:39
const char * sName
#define TOOLS_INFO_EXCEPTION(area, stream)
#define SCSOLVER_SERVICE
Definition: solverutil.cxx:37
#define SAL_WARN_IF(condition, area, stream)
Reference< XComponentContext > getProcessComponentContext()
static css::uno::Reference< css::sheet::XSolver > GetSolver(const OUString &rImplName)
Definition: solverutil.cxx:98