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