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 #ifndef SDEXT_PRESENTER_CONFIGURATION_ACCESS_HXX
29 #define SDEXT_PRESENTER_CONFIGURATION_ACCESS_HXX
30 
31 #include <rtl/ustring.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
35 #include <com/sun/star/uno/XComponentContext.hpp>
36 #include <vector>
37 #include <boost/function.hpp>
38 
39 namespace css = ::com::sun::star;
40 
41 namespace sdext { namespace presenter {
42 
43 /** This class gives access to the configuration.  Create an object of this
44     class for one node of the configuration.  This will be the root node.
45     From this one you can use this class in two ways.
46 
47     <p>In a stateless mode (with exception of the root node) you can use static
48     methods for obtaining child nodes, get values from properties at leaf
49     nodes and iterate over children of inner nodes.</p>
50 
51     <p>In a stateful mode use non-static methods like GoToChild() to
52     navigate to children.</p>
53 
54     <p>Note to call CommitChanges() after making changes to
55     PresenterConfigurationAccess object that was opened in READ_WRITE mode.</p>
56 */
57 class PresenterConfigurationAccess
58 {
59 public:
60     enum WriteMode { READ_WRITE, READ_ONLY };
61     typedef ::boost::function<bool(
62         const ::rtl::OUString&,
63         const css::uno::Reference<css::beans::XPropertySet>&)> Predicate;
64     static const ::rtl::OUString msPresenterScreenRootName;
65 
66     /** Create a new object to access the configuration entries below the
67         given root.
68         @param rsRootName
69             Name of the root.  You can use msPresenterScreenRootName to
70             access the configuration of the presenter screen.
71         @param eMode
72             This flag specifies whether to give read-write or read-only
73             access.
74     */
75     PresenterConfigurationAccess(
76         const css::uno::Reference<css::uno::XComponentContext>& rxContext,
77         const ::rtl::OUString& rsRootName,
78         WriteMode eMode);
79 
80     ~PresenterConfigurationAccess (void);
81 
82     /** Return a configuration node below the root of the called object.
83         @param rsPathToNode
84             The relative path from the root (as given the constructor) to the node.
85     */
86     css::uno::Any GetConfigurationNode (
87         const ::rtl::OUString& rsPathToNode);
88     css::uno::Reference<css::beans::XPropertySet> GetNodeProperties (
89         const ::rtl::OUString& rsPathToNode);
90 
91     /** Return <TRUE/> when opening the configuration (via creating a new
92         PresenterConfigurationAccess object) or previous calls to
93         GoToChild() left the called PresenterConfigurationAccess object in a
94         valid state.
95     */
96     bool IsValid (void) const;
97 
98     /** Move the focused node to the (possibly indirect) child specified by the given path.
99     */
100     bool GoToChild (const ::rtl::OUString& rsPathToNode);
101 
102     /** Move the focused node to the first direct child that fulfills the the given predicate.
103     */
104     bool GoToChild (const Predicate& rPredicate);
105 
106     /** Modify the property child of the currently focused node.  Keep in
107         mind to call CommitChanges() to write the change back to the
108         configuration.
109     */
110     bool SetProperty (const ::rtl::OUString& rsPropertyName, const css::uno::Any& rValue);
111 
112     /** Return a configuration node below the given node.
113         @param rxNode
114             The node that acts as root to the given relative path.
115         @param rsPathToNode
116             The relative path from the given node to the requested node.
117             When this string is empty then rxNode is returned.
118         @return
119             The type of the returned node varies with the requested node.
120             It is empty when the node was not found.
121     */
122     static css::uno::Any GetConfigurationNode (
123         const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
124         const ::rtl::OUString& rsPathToNode);
125 
126     static css::uno::Reference<css::beans::XPropertySet> GetNodeProperties (
127         const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
128         const ::rtl::OUString& rsPathToNode);
129 
130     /** Write any changes that have been made back to the configuration.
131         This call is ignored when the called ConfigurationAccess object was
132         not create with read-write mode.
133     */
134     void CommitChanges (void);
135 
136     css::uno::Any GetValue (const rtl::OUString& sKey);
137 
138     typedef ::boost::function<void(
139         const ::rtl::OUString&,
140         const ::std::vector<css::uno::Any>&) > ItemProcessor;
141     typedef ::boost::function<void(
142         const ::rtl::OUString&,
143         const ::css::uno::Reference<css::beans::XPropertySet>&) > PropertySetProcessor;
144 
145     /** Execute a functor for all elements of the given container.
146         @param rxContainer
147             The container is a XNameAccess to a list of the configuration.
148             This can be a node returned by GetConfigurationNode().
149         @param rArguments
150             The functor is called with arguments that are children of each
151             element of the container.  The set of children is specified  this
152             list.
153         @param rFunctor
154             The functor to be executed for some or all of the elements in
155             the given container.
156     */
157     static void ForAll (
158         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
159         const ::std::vector<rtl::OUString>& rArguments,
160         const ItemProcessor& rProcessor);
161     static void ForAll (
162         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
163         const PropertySetProcessor& rProcessor);
164 
165     /** Fill a list with the string contents of all sub-elements in the given container.
166         @param rxContainer
167             The container is a XNameAccess to a list of the configuration.
168             This can be a node returned by GetConfigurationNode().
169         @param rsArgument
170             This specifies which string children of the elements in the
171             container are to be inserted into the list.  The specified child
172             has to be of type string.
173         @param rList
174             The list to be filled.
175     */
176     static void FillList(
177         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
178         const ::rtl::OUString& rsArgument,
179         ::std::vector<rtl::OUString>& rList);
180 
181     static css::uno::Any Find (
182         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
183         const Predicate& rPredicate);
184 
185     static bool IsStringPropertyEqual (
186         const ::rtl::OUString& rsValue,
187         const ::rtl::OUString& rsPropertyName,
188         const css::uno::Reference<css::beans::XPropertySet>& rxNode);
189 
190     /** This method wraps a call to getPropertyValue() and returns an empty
191         Any instead of throwing an exception when the property does not
192         exist.
193     */
194     static css::uno::Any GetProperty (
195         const css::uno::Reference<css::beans::XPropertySet>& rxProperties,
196         const ::rtl::OUString& rsKey);
197 
198 private:
199     css::uno::Reference<css::uno::XInterface> mxRoot;
200     css::uno::Any maNode;
201 };
202 
203 } } // end of namespace sdext::tools
204 
205 #endif
206