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 /**************************************************************************
21                                 TODO
22  **************************************************************************
23 
24  *************************************************************************/
25 #include <com/sun/star/beans/XPropertySetInfo.hpp>
26 #include <com/sun/star/ucb/UnsupportedCommandException.hpp>
27 #include <com/sun/star/ucb/XPersistentPropertySet.hpp>
28 #include <com/sun/star/ucb/XCommandInfo.hpp>
29 
30 #include <cppuhelper/queryinterface.hxx>
31 #include <osl/mutex.hxx>
32 #include <ucbhelper/contenthelper.hxx>
33 #include <ucbhelper/contentinfo.hxx>
34 #include <ucbhelper/macros.hxx>
35 
36 using namespace com::sun::star;
37 
38 
39 // PropertySetInfo Implementation.
40 
41 
42 namespace ucbhelper {
43 
PropertySetInfo(const uno::Reference<css::ucb::XCommandEnvironment> & rxEnv,ContentImplHelper * pContent)44 PropertySetInfo::PropertySetInfo(
45     const uno::Reference< css::ucb::XCommandEnvironment >& rxEnv,
46     ContentImplHelper* pContent )
47 : m_xEnv( rxEnv ),
48   m_pContent( pContent )
49 {
50 }
51 
52 
53 // virtual
~PropertySetInfo()54 PropertySetInfo::~PropertySetInfo()
55 {
56 }
57 
58 
59 // XPropertySetInfo methods.
60 
61 
62 // virtual
getProperties()63 uno::Sequence< beans::Property > SAL_CALL PropertySetInfo::getProperties()
64 {
65     if ( !m_pProps )
66     {
67         osl::MutexGuard aGuard( m_aMutex );
68         if ( !m_pProps )
69         {
70 
71             // Get info for core ( native) properties.
72 
73 
74             try
75             {
76                 uno::Sequence< beans::Property > aProps
77                     = m_pContent->getProperties( m_xEnv );
78                 m_pProps.reset(new uno::Sequence< beans::Property >( aProps ));
79             }
80             catch ( uno::RuntimeException const & )
81             {
82                 throw;
83             }
84             catch ( uno::Exception const & )
85             {
86                 m_pProps.reset(new uno::Sequence< beans::Property >( 0 ));
87             }
88 
89 
90             // Get info for additional properties.
91 
92 
93             uno::Reference< css::ucb::XPersistentPropertySet >
94                 xSet ( m_pContent->getAdditionalPropertySet( false ) );
95 
96             if ( xSet.is() )
97             {
98                 // Get property set info.
99                 uno::Reference< beans::XPropertySetInfo > xInfo(
100                     xSet->getPropertySetInfo() );
101                 if ( xInfo.is() )
102                 {
103                     const uno::Sequence< beans::Property >& rAddProps
104                         = xInfo->getProperties();
105                     sal_Int32 nAddProps = rAddProps.getLength();
106                     if ( nAddProps > 0 )
107                     {
108                         sal_Int32 nPos = m_pProps->getLength();
109                         m_pProps->realloc( nPos + nAddProps );
110 
111                         std::copy(rAddProps.begin(), rAddProps.end(),
112                                   std::next(m_pProps->begin(), nPos));
113                     }
114                 }
115             }
116         }
117     }
118     return *m_pProps;
119 }
120 
121 
122 // virtual
getPropertyByName(const OUString & aName)123 beans::Property SAL_CALL PropertySetInfo::getPropertyByName(
124         const OUString& aName )
125 {
126     beans::Property aProp;
127     if ( queryProperty( aName, aProp ) )
128         return aProp;
129 
130     throw beans::UnknownPropertyException(aName);
131 }
132 
133 
134 // virtual
hasPropertyByName(const OUString & Name)135 sal_Bool SAL_CALL PropertySetInfo::hasPropertyByName(
136         const OUString& Name )
137 {
138     beans::Property aProp;
139     return queryProperty( Name, aProp );
140 }
141 
142 
143 // Non-Interface methods.
144 
145 
reset()146 void PropertySetInfo::reset()
147 {
148     osl::MutexGuard aGuard( m_aMutex );
149     m_pProps.reset();
150 }
151 
152 
queryProperty(const OUString & rName,beans::Property & rProp)153 bool PropertySetInfo::queryProperty(
154     const OUString& rName, beans::Property& rProp )
155 {
156     osl::MutexGuard aGuard( m_aMutex );
157 
158     getProperties();
159 
160     const beans::Property* pProps = m_pProps->getConstArray();
161     sal_Int32 nCount = m_pProps->getLength();
162     for ( sal_Int32 n = 0; n < nCount; ++n )
163     {
164         const beans::Property& rCurrProp = pProps[ n ];
165         if ( rCurrProp.Name == rName )
166         {
167             rProp = rCurrProp;
168             return true;
169         }
170     }
171 
172     return false;
173 }
174 
175 
176 // CommandProcessorInfo Implementation.
177 
178 
CommandProcessorInfo(const uno::Reference<css::ucb::XCommandEnvironment> & rxEnv,ContentImplHelper * pContent)179 CommandProcessorInfo::CommandProcessorInfo(
180     const uno::Reference< css::ucb::XCommandEnvironment >& rxEnv,
181     ContentImplHelper* pContent )
182 : m_xEnv( rxEnv ),
183   m_pContent( pContent )
184 {
185 }
186 
187 
188 // virtual
~CommandProcessorInfo()189 CommandProcessorInfo::~CommandProcessorInfo()
190 {
191 }
192 
193 
194 // XCommandInfo methods.
195 
196 
197 // virtual
198 uno::Sequence< css::ucb::CommandInfo > SAL_CALL
getCommands()199 CommandProcessorInfo::getCommands()
200 {
201     if ( !m_pCommands )
202     {
203         osl::MutexGuard aGuard( m_aMutex );
204         if ( !m_pCommands )
205         {
206 
207             // Get info for commands.
208 
209 
210             try
211             {
212                 uno::Sequence< css::ucb::CommandInfo > aCmds
213                     = m_pContent->getCommands( m_xEnv );
214                 m_pCommands.reset(new uno::Sequence< css::ucb::CommandInfo >( aCmds ));
215             }
216             catch ( uno::RuntimeException const & )
217             {
218                 throw;
219             }
220             catch ( uno::Exception const & )
221             {
222                 m_pCommands.reset(new uno::Sequence< css::ucb::CommandInfo >( 0 ));
223             }
224         }
225     }
226     return *m_pCommands;
227 }
228 
229 
230 // virtual
231 css::ucb::CommandInfo SAL_CALL
getCommandInfoByName(const OUString & Name)232 CommandProcessorInfo::getCommandInfoByName(
233         const OUString& Name )
234 {
235     css::ucb::CommandInfo aInfo;
236     if ( queryCommand( Name, aInfo ) )
237         return aInfo;
238 
239     throw css::ucb::UnsupportedCommandException();
240 }
241 
242 
243 // virtual
244 css::ucb::CommandInfo SAL_CALL
getCommandInfoByHandle(sal_Int32 Handle)245 CommandProcessorInfo::getCommandInfoByHandle( sal_Int32 Handle )
246 {
247     css::ucb::CommandInfo aInfo;
248     if ( queryCommand( Handle, aInfo ) )
249         return aInfo;
250 
251     throw css::ucb::UnsupportedCommandException();
252 }
253 
254 
255 // virtual
hasCommandByName(const OUString & Name)256 sal_Bool SAL_CALL CommandProcessorInfo::hasCommandByName(
257        const OUString& Name )
258 {
259     css::ucb::CommandInfo aInfo;
260     return queryCommand( Name, aInfo );
261 }
262 
263 
264 // virtual
hasCommandByHandle(sal_Int32 Handle)265 sal_Bool SAL_CALL CommandProcessorInfo::hasCommandByHandle( sal_Int32 Handle )
266 {
267     css::ucb::CommandInfo aInfo;
268     return queryCommand( Handle, aInfo );
269 }
270 
271 
272 // Non-Interface methods.
273 
274 
reset()275 void CommandProcessorInfo::reset()
276 {
277     osl::MutexGuard aGuard( m_aMutex );
278     m_pCommands.reset();
279 }
280 
281 
queryCommand(const OUString & rName,css::ucb::CommandInfo & rCommand)282 bool CommandProcessorInfo::queryCommand(
283     const OUString& rName,
284     css::ucb::CommandInfo& rCommand )
285 {
286     osl::MutexGuard aGuard( m_aMutex );
287 
288     getCommands();
289 
290     const css::ucb::CommandInfo* pCommands
291         = m_pCommands->getConstArray();
292     sal_Int32 nCount = m_pCommands->getLength();
293     for ( sal_Int32 n = 0; n < nCount; ++n )
294     {
295         const css::ucb::CommandInfo& rCurrCommand = pCommands[ n ];
296         if ( rCurrCommand.Name == rName )
297         {
298             rCommand = rCurrCommand;
299             return true;
300         }
301     }
302 
303     return false;
304 }
305 
306 
queryCommand(sal_Int32 nHandle,css::ucb::CommandInfo & rCommand)307 bool CommandProcessorInfo::queryCommand(
308     sal_Int32 nHandle,
309     css::ucb::CommandInfo& rCommand )
310 {
311     osl::MutexGuard aGuard( m_aMutex );
312 
313     getCommands();
314 
315     const css::ucb::CommandInfo* pCommands = m_pCommands->getConstArray();
316     sal_Int32 nCount = m_pCommands->getLength();
317     for ( sal_Int32 n = 0; n < nCount; ++n )
318     {
319         const css::ucb::CommandInfo& rCurrCommand = pCommands[ n ];
320         if ( rCurrCommand.Handle == nHandle )
321         {
322             rCommand = rCurrCommand;
323             return true;
324         }
325     }
326 
327     return false;
328 }
329 
330 } // namespace ucbhelper
331 
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
333