1*c45d927aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c45d927aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c45d927aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c45d927aSAndrew Rist  * distributed with this work for additional information
6*c45d927aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c45d927aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c45d927aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c45d927aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c45d927aSAndrew Rist  *
11*c45d927aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c45d927aSAndrew Rist  *
13*c45d927aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c45d927aSAndrew Rist  * software distributed under the License is distributed on an
15*c45d927aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c45d927aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c45d927aSAndrew Rist  * specific language governing permissions and limitations
18*c45d927aSAndrew Rist  * under the License.
19*c45d927aSAndrew Rist  *
20*c45d927aSAndrew Rist  *************************************************************/
21*c45d927aSAndrew Rist 
22*c45d927aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SD_TOOLS_CONFIGURATION_ACCESS_HXX
25cdf0e10cSrcweir #define SD_TOOLS_CONFIGURATION_ACCESS_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/ustring.hxx>
28cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32cdf0e10cSrcweir #include <vector>
33cdf0e10cSrcweir #include <boost/function.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace css = ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace sd { namespace tools {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /** This class gives access to the configuration.  Create an object of this
40cdf0e10cSrcweir     class for one node of the configuration.  This will be the root node.
41cdf0e10cSrcweir     Its children are then accessible through the new ConfigurationAccess
42cdf0e10cSrcweir     object.
43cdf0e10cSrcweir */
44cdf0e10cSrcweir class ConfigurationAccess
45cdf0e10cSrcweir {
46cdf0e10cSrcweir public:
47cdf0e10cSrcweir     enum WriteMode { READ_WRITE, READ_ONLY };
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     /** Create a new object to access the configuration entries below the
50cdf0e10cSrcweir         given root.
51cdf0e10cSrcweir         @param rsRootName
52cdf0e10cSrcweir             Name of the root.
53cdf0e10cSrcweir         @param eMode
54cdf0e10cSrcweir             This flag specifies whether to give read-write or read-only
55cdf0e10cSrcweir             access.
56cdf0e10cSrcweir     */
57cdf0e10cSrcweir     ConfigurationAccess(
58cdf0e10cSrcweir         const ::rtl::OUString& rsRootName,
59cdf0e10cSrcweir         const WriteMode eMode);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     ConfigurationAccess(
62cdf0e10cSrcweir         const css::uno::Reference<css::uno::XComponentContext>& rxContext,
63cdf0e10cSrcweir         const ::rtl::OUString& rsRootName,
64cdf0e10cSrcweir         const WriteMode eMode);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     /** Return a configuration node below the root of the called object.
67cdf0e10cSrcweir         @param rsPathToNode
68cdf0e10cSrcweir             The relative path from the root (as given the constructor) to
69cdf0e10cSrcweir             the node.
70cdf0e10cSrcweir         @return
71cdf0e10cSrcweir             The type of the returned node varies with the requested node.
72cdf0e10cSrcweir             It is empty when the node was not found.
73cdf0e10cSrcweir     */
74cdf0e10cSrcweir     css::uno::Any GetConfigurationNode (
75cdf0e10cSrcweir         const ::rtl::OUString& rsPathToNode);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /** Return a configuration node below the given node.
78cdf0e10cSrcweir         @param rxNode
79cdf0e10cSrcweir             The node that acts as root to the given relative path.
80cdf0e10cSrcweir         @param rsPathToNode
81cdf0e10cSrcweir             The relative path from the given node to the requested node.
82cdf0e10cSrcweir         @return
83cdf0e10cSrcweir             The type of the returned node varies with the requested node.
84cdf0e10cSrcweir             It is empty when the node was not found.
85cdf0e10cSrcweir     */
86cdf0e10cSrcweir     static css::uno::Any GetConfigurationNode (
87cdf0e10cSrcweir         const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
88cdf0e10cSrcweir         const ::rtl::OUString& rsPathToNode);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     /** Write any changes that have been made back to the configuration.
91cdf0e10cSrcweir         This call is ignored when the called ConfigurationAccess object was
92cdf0e10cSrcweir         not create with read-write mode.
93cdf0e10cSrcweir     */
94cdf0e10cSrcweir     void CommitChanges (void);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     /** This functor is typically called for every item in a set.  Its two
97cdf0e10cSrcweir         parameters are the name of key item (often of no further interest)
98cdf0e10cSrcweir         and the value of the item.
99cdf0e10cSrcweir     */
100cdf0e10cSrcweir     typedef ::boost::function<void(
101cdf0e10cSrcweir         const ::rtl::OUString&,
102cdf0e10cSrcweir         const std::vector<css::uno::Any>&) > Functor;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     /** Execute a functor for all elements of the given container.
105cdf0e10cSrcweir         @param rxContainer
106cdf0e10cSrcweir             The container is a XNameAccess to a list of the configuration.
107cdf0e10cSrcweir             This can be a node returned by GetConfigurationNode().
108cdf0e10cSrcweir         @param rArguments
109cdf0e10cSrcweir             The functor is called with arguments that are children of each
110cdf0e10cSrcweir             element of the container.  The set of children is specified  this
111cdf0e10cSrcweir             list.
112cdf0e10cSrcweir         @param rFunctor
113cdf0e10cSrcweir             The functor to be executed for some or all of the elements in
114cdf0e10cSrcweir             the given container.
115cdf0e10cSrcweir     */
116cdf0e10cSrcweir     static void ForAll (
117cdf0e10cSrcweir         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
118cdf0e10cSrcweir         const ::std::vector<rtl::OUString>& rArguments,
119cdf0e10cSrcweir         const Functor& rFunctor);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /** Fill a list with the string contents of all sub-elements in the given container.
122cdf0e10cSrcweir         @param rxContainer
123cdf0e10cSrcweir             The container is a XNameAccess to a list of the configuration.
124cdf0e10cSrcweir             This can be a node returned by GetConfigurationNode().
125cdf0e10cSrcweir         @param rsArgument
126cdf0e10cSrcweir             This specifies which string children of the elements in the
127cdf0e10cSrcweir             container are to be inserted into the list.  The specified child
128cdf0e10cSrcweir             has to be of type string.
129cdf0e10cSrcweir         @param rList
130cdf0e10cSrcweir             The list to be filled.
131cdf0e10cSrcweir     */
132cdf0e10cSrcweir     static void FillList(
133cdf0e10cSrcweir         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
134cdf0e10cSrcweir         const ::rtl::OUString& rsArgument,
135cdf0e10cSrcweir         ::std::vector<rtl::OUString>& rList);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir private:
138cdf0e10cSrcweir     css::uno::Reference<css::uno::XInterface> mxRoot;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     void Initialize (
141cdf0e10cSrcweir         const css::uno::Reference<css::lang::XMultiServiceFactory>& rxProvider,
142cdf0e10cSrcweir         const ::rtl::OUString& rsRootName,
143cdf0e10cSrcweir         const WriteMode eMode);
144cdf0e10cSrcweir };
145cdf0e10cSrcweir 
146cdf0e10cSrcweir } } // end of namespace sd::tools
147cdf0e10cSrcweir 
148cdf0e10cSrcweir #endif
149