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 #include "vbastyles.hxx"
20 #include "vbastyle.hxx"
21 #include <basic/sberrors.hxx>
22 #include <cppuhelper/exc_hlp.hxx>
23 #include <ooo/vba/excel/XRange.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 
27 using namespace ::ooo::vba;
28 using namespace ::com::sun::star;
29 
30 static css::uno::Any
lcl_createAPIStyleToVBAObject(const css::uno::Any & aObject,const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)31 lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel )
32 {
33     uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW );
34     uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) );
35     return uno::makeAny( xStyle );
36 }
37 
ScVbaStyles(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<css::uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)38 ScVbaStyles::ScVbaStyles( const uno::Reference< XHelperInterface >& xParent,
39                           const uno::Reference< css::uno::XComponentContext > & xContext,
40                           const uno::Reference< frame::XModel >& xModel )
41 : ScVbaStyles_BASE( xParent,
42                     xContext,
43                     uno::Reference< container::XIndexAccess >( ScVbaStyle::getStylesNameContainer( xModel ), uno::UNO_QUERY_THROW ) ),
44   mxModel( xModel )
45 {
46     try
47     {
48         mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
49         mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW );
50     }
51     catch (uno::Exception& )
52     {
53         DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
54     }
55 }
56 
57 uno::Sequence< OUString >
getStyleNames()58 ScVbaStyles::getStyleNames()
59 {
60     return mxNameContainerCellStyles->getElementNames();
61 }
62 
63 uno::Any
createCollectionObject(const uno::Any & aObject)64 ScVbaStyles::createCollectionObject(const uno::Any& aObject)
65 {
66     return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel );
67 }
68 
69 uno::Type SAL_CALL
getElementType()70 ScVbaStyles::getElementType()
71 {
72     return cppu::UnoType<excel::XStyle>::get();
73 }
74 
75 namespace {
76 
77 class EnumWrapper : public EnumerationHelper_BASE
78 {
79         uno::Reference<container::XIndexAccess > m_xIndexAccess;
80         uno::Reference<XHelperInterface > m_xParent;
81         uno::Reference<uno::XComponentContext > m_xContext;
82         uno::Reference<frame::XModel > m_xModel;
83 
84         sal_Int32 nIndex;
85 public:
EnumWrapper(const uno::Reference<container::XIndexAccess> & xIndexAccess,const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)86         EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<XHelperInterface >& xParent, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<frame::XModel >& xModel ) : m_xIndexAccess( xIndexAccess ), m_xParent( xParent ), m_xContext( xContext ), m_xModel( xModel ), nIndex( 0 ) {}
hasMoreElements()87         virtual sal_Bool SAL_CALL hasMoreElements(  ) override
88         {
89                 return ( nIndex < m_xIndexAccess->getCount() );
90         }
nextElement()91         virtual uno::Any SAL_CALL nextElement(  ) override
92         {
93             try
94             {
95                 if ( nIndex < m_xIndexAccess->getCount() )
96                         return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel );
97             }
98             catch (const container::NoSuchElementException&)
99             {
100                 throw;
101             }
102             catch (const lang::WrappedTargetException&)
103             {
104                 throw;
105             }
106             catch (const uno::RuntimeException&)
107             {
108                 throw;
109             }
110             catch (const uno::Exception& e)
111             {
112                 css::uno::Any a(cppu::getCaughtException());
113                 throw css::lang::WrappedTargetException(
114                     "wrapped Exception " + e.Message,
115                     css::uno::Reference<css::uno::XInterface>(), a);
116             }
117             throw container::NoSuchElementException();
118         }
119 };
120 
121 }
122 
123 uno::Reference< container::XEnumeration > SAL_CALL
createEnumeration()124 ScVbaStyles::createEnumeration()
125 {
126     return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel );
127 }
128 
129 uno::Reference< excel::XStyle > SAL_CALL
Add(const OUString & _sName,const uno::Any & _aBasedOn)130 ScVbaStyles::Add( const OUString& _sName, const uno::Any& _aBasedOn )
131 {
132     uno::Reference< excel::XStyle > aRet;
133     try
134     {
135         OUString sParentCellStyleName("Default");
136         if ( _aBasedOn.hasValue() )
137         {
138             uno::Reference< excel::XRange > oRange;
139             if ( _aBasedOn >>= oRange)
140             {
141                 uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW );
142                 sParentCellStyleName = oStyle->getName();
143             }
144             else
145             {
146                 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT, OUString());
147             }
148         }
149 
150         uno::Reference< style::XStyle > xStyle( mxMSF->createInstance("com.sun.star.style.CellStyle"), uno::UNO_QUERY_THROW );
151 
152         if (!mxNameContainerCellStyles->hasByName(_sName))
153         {
154             mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) );
155         }
156         if (sParentCellStyleName != "Default")
157         {
158             xStyle->setParentStyle( sParentCellStyleName );
159         }
160         aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW );
161     }
162     catch (const uno::Exception&)
163     {
164         DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
165     }
166     return aRet;
167 }
168 
169 void
Delete(const OUString & _sStyleName)170 ScVbaStyles::Delete(const OUString& _sStyleName)
171 {
172     try
173     {
174         if (mxNameContainerCellStyles->hasByName( _sStyleName ) )
175             mxNameContainerCellStyles->removeByName( _sStyleName );
176     }
177     catch (const uno::Exception&)
178     {
179         DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
180     }
181 }
182 
183 OUString
getServiceImplName()184 ScVbaStyles::getServiceImplName()
185 {
186     return "ScVbaStyles";
187 }
188 
189 uno::Sequence< OUString >
getServiceNames()190 ScVbaStyles::getServiceNames()
191 {
192         static uno::Sequence< OUString > const aServiceNames
193         {
194             "ooo.vba.excel.XStyles"
195         };
196         return aServiceNames;
197 }
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
200