1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "PresenterConfigurationAccess.hxx"
28 
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 #include <com/sun/star/util/XChangesBatch.hpp>
33 
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using ::rtl::OUString;
37 
38 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
39 
40 namespace sdext { namespace presenter {
41 
42 const ::rtl::OUString PresenterConfigurationAccess::msPresenterScreenRootName =
43     A2S("/org.openoffice.Office.PresenterScreen/");
44 
PresenterConfigurationAccess(const Reference<XComponentContext> & rxContext,const OUString & rsRootName,WriteMode eMode)45 PresenterConfigurationAccess::PresenterConfigurationAccess (
46     const Reference<XComponentContext>& rxContext,
47     const OUString& rsRootName,
48     WriteMode eMode)
49     : mxRoot(),
50       maNode()
51 {
52     try
53     {
54         Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
55         if (xFactory.is())
56         {
57             Sequence<Any> aCreationArguments(3);
58             aCreationArguments[0] = makeAny(beans::PropertyValue(
59                 A2S("nodepath"),
60                 0,
61                 makeAny(rsRootName),
62                 beans::PropertyState_DIRECT_VALUE));
63             aCreationArguments[1] = makeAny(beans::PropertyValue(
64                 A2S("depth"),
65                 0,
66                 makeAny((sal_Int32)-1),
67                 beans::PropertyState_DIRECT_VALUE));
68             aCreationArguments[2] = makeAny(beans::PropertyValue(
69                 A2S("lazywrite"),
70                 0,
71                 makeAny(true),
72                 beans::PropertyState_DIRECT_VALUE));
73 
74             OUString sAccessService;
75             if (eMode == READ_ONLY)
76                 sAccessService = A2S("com.sun.star.configuration.ConfigurationAccess");
77             else
78                 sAccessService = A2S("com.sun.star.configuration.ConfigurationUpdateAccess");
79 
80             Reference<lang::XMultiServiceFactory> xProvider (
81                 xFactory->createInstanceWithContext(
82                     A2S("com.sun.star.configuration.ConfigurationProvider"),
83                     rxContext),
84                 UNO_QUERY_THROW);
85             mxRoot = xProvider->createInstanceWithArguments(
86                 sAccessService, aCreationArguments);
87             maNode <<= mxRoot;
88         }
89     }
90     catch (Exception& rException)
91     {
92         OSL_TRACE ("caught exception while opening configuration: %s",
93             ::rtl::OUStringToOString(rException.Message,
94                 RTL_TEXTENCODING_UTF8).getStr());
95     }
96 }
97 
98 
99 
100 
~PresenterConfigurationAccess(void)101 PresenterConfigurationAccess::~PresenterConfigurationAccess (void)
102 {
103 }
104 
105 
106 
107 
IsValid(void) const108 bool PresenterConfigurationAccess::IsValid (void) const
109 {
110     return mxRoot.is();
111 }
112 
113 
114 
115 
GetConfigurationNode(const OUString & sPathToNode)116 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
117 {
118     return GetConfigurationNode(
119         Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
120         sPathToNode);
121 }
122 
123 
124 
125 
GetNodeProperties(const OUString & sPathToNode)126 Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
127     const OUString& sPathToNode)
128 {
129     return GetNodeProperties(
130         Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
131         sPathToNode);
132 }
133 
134 
135 
136 
GoToChild(const::rtl::OUString & rsPathToNode)137 bool PresenterConfigurationAccess::GoToChild (const ::rtl::OUString& rsPathToNode)
138 {
139     if ( ! IsValid())
140         return false;
141 
142     Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
143     if (xNode.is())
144     {
145         maNode = GetConfigurationNode(
146             Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
147             rsPathToNode);
148         if (Reference<XInterface>(maNode, UNO_QUERY).is())
149             return true;
150     }
151 
152     mxRoot = NULL;
153     return false;
154 }
155 
156 
157 
158 
GoToChild(const Predicate & rPredicate)159 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
160 {
161     if ( ! IsValid())
162         return false;
163 
164     maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
165     if (Reference<XInterface>(maNode, UNO_QUERY).is())
166         return true;
167 
168     mxRoot = NULL;
169     return false;
170 }
171 
172 
173 
174 
SetProperty(const::rtl::OUString & rsPropertyName,const Any & rValue)175 bool PresenterConfigurationAccess::SetProperty (
176     const ::rtl::OUString& rsPropertyName,
177     const Any& rValue)
178 {
179     Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
180     if (xProperties.is())
181     {
182         xProperties->setPropertyValue(rsPropertyName, rValue);
183         return true;
184     }
185     else
186         return false;
187 }
188 
189 
190 
191 
GetConfigurationNode(const css::uno::Reference<css::container::XHierarchicalNameAccess> & rxNode,const OUString & sPathToNode)192 Any PresenterConfigurationAccess::GetConfigurationNode (
193     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
194     const OUString& sPathToNode)
195 {
196     if (sPathToNode.getLength() == 0)
197         return Any(rxNode);
198 
199     try
200     {
201         if (rxNode.is())
202         {
203             return rxNode->getByHierarchicalName(sPathToNode);
204         }
205     }
206     catch (Exception& rException)
207     {
208         OSL_TRACE ("caught exception while getting configuration node %s: %s",
209             ::rtl::OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
210             ::rtl::OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
211     }
212 
213     return Any();
214 }
215 
216 
217 
218 
GetNodeProperties(const css::uno::Reference<css::container::XHierarchicalNameAccess> & rxNode,const::rtl::OUString & rsPathToNode)219 Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
220     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
221     const ::rtl::OUString& rsPathToNode)
222 {
223     return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY);
224 }
225 
226 
227 
228 
CommitChanges(void)229 void PresenterConfigurationAccess::CommitChanges (void)
230 {
231     Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
232     if (xConfiguration.is())
233         xConfiguration->commitChanges();
234 }
235 
236 
237 
238 
GetValue(const rtl::OUString & sKey)239 Any PresenterConfigurationAccess::GetValue (const rtl::OUString& sKey)
240 {
241     Reference<container::XNameAccess> xAccess (GetConfigurationNode(sKey), UNO_QUERY);
242     if (xAccess.is())
243     {
244         return xAccess->getByName(sKey);
245     }
246     else
247     {
248         return Any();
249     }
250 }
251 
252 
253 
254 
ForAll(const Reference<container::XNameAccess> & rxContainer,const::std::vector<OUString> & rArguments,const ItemProcessor & rProcessor)255 void PresenterConfigurationAccess::ForAll (
256     const Reference<container::XNameAccess>& rxContainer,
257     const ::std::vector<OUString>& rArguments,
258     const ItemProcessor& rProcessor)
259 {
260     if (rxContainer.is())
261     {
262         ::std::vector<Any> aValues(rArguments.size());
263         Sequence<OUString> aKeys (rxContainer->getElementNames());
264         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
265         {
266             bool bHasAllValues (true);
267             const OUString& rsKey (aKeys[nItemIndex]);
268             Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
269             Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY);
270             OSL_ASSERT(xSet.is());
271             if (xSetItem.is())
272             {
273                 // Get from the current item of the container the children
274                 // that match the names in the rArguments list.
275                 for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
276                 {
277                     if ( ! xSetItem->hasByName(rArguments[nValueIndex]))
278                         bHasAllValues = false;
279                     else
280                         aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
281                 }
282             }
283             else
284                 bHasAllValues = false;
285             if (bHasAllValues)
286                 rProcessor(rsKey,aValues);
287         }
288     }
289 }
290 
291 
292 
293 
ForAll(const Reference<container::XNameAccess> & rxContainer,const PropertySetProcessor & rProcessor)294 void PresenterConfigurationAccess::ForAll (
295     const Reference<container::XNameAccess>& rxContainer,
296     const PropertySetProcessor& rProcessor)
297 {
298     if (rxContainer.is())
299     {
300         Sequence<OUString> aKeys (rxContainer->getElementNames());
301         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
302         {
303             const OUString& rsKey (aKeys[nItemIndex]);
304             Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY);
305             if (xSet.is())
306                 rProcessor(rsKey, xSet);
307         }
308     }
309 }
310 
311 
312 
313 
FillList(const Reference<container::XNameAccess> & rxContainer,const::rtl::OUString & rsArgument,::std::vector<OUString> & rList)314 void PresenterConfigurationAccess::FillList(
315     const Reference<container::XNameAccess>& rxContainer,
316     const ::rtl::OUString& rsArgument,
317     ::std::vector<OUString>& rList)
318 {
319     try
320     {
321         if (rxContainer.is())
322         {
323             Sequence<OUString> aKeys (rxContainer->getElementNames());
324             rList.resize(aKeys.getLength());
325             for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
326             {
327                 Reference<container::XNameAccess> xSetItem (
328                     rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
329                 if (xSetItem.is())
330                 {
331                     xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
332                 }
333             }
334         }
335     }
336     catch (RuntimeException&)
337     {}
338 }
339 
340 
341 
342 
Find(const Reference<container::XNameAccess> & rxContainer,const Predicate & rPredicate)343 Any PresenterConfigurationAccess::Find (
344     const Reference<container::XNameAccess>& rxContainer,
345     const Predicate& rPredicate)
346 {
347     if (rxContainer.is())
348     {
349         Sequence<OUString> aKeys (rxContainer->getElementNames());
350         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
351         {
352             Reference<beans::XPropertySet> xProperties (
353                 rxContainer->getByName(aKeys[nItemIndex]),
354                 UNO_QUERY);
355             if (xProperties.is())
356                 if (rPredicate(aKeys[nItemIndex], xProperties))
357                     return Any(xProperties);
358         }
359     }
360     return Any();
361 }
362 
363 
364 
365 
IsStringPropertyEqual(const::rtl::OUString & rsValue,const::rtl::OUString & rsPropertyName,const css::uno::Reference<css::beans::XPropertySet> & rxNode)366 bool PresenterConfigurationAccess::IsStringPropertyEqual (
367     const ::rtl::OUString& rsValue,
368     const ::rtl::OUString& rsPropertyName,
369     const css::uno::Reference<css::beans::XPropertySet>& rxNode)
370 {
371     OUString sValue;
372     if (GetProperty(rxNode, rsPropertyName) >>= sValue)
373         return sValue == rsValue;
374     else
375         return false;
376 }
377 
378 
379 
380 
GetProperty(const Reference<beans::XPropertySet> & rxProperties,const OUString & rsKey)381 Any PresenterConfigurationAccess::GetProperty (
382     const Reference<beans::XPropertySet>& rxProperties,
383     const OUString& rsKey)
384 {
385     OSL_ASSERT(rxProperties.is());
386     if ( ! rxProperties.is())
387         return Any();
388     try
389     {
390         Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
391         if (xInfo.is())
392             if ( ! xInfo->hasPropertyByName(rsKey))
393                 return Any();
394         return rxProperties->getPropertyValue(rsKey);
395     }
396     catch (beans::UnknownPropertyException&)
397     {
398     }
399     return Any();
400 }
401 
402 
403 
404 
405 } } // end of namespace sdext::tools
406 
407