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