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 #ifndef SD_TOOLS_CONFIGURATION_ACCESS_HXX
25 #define SD_TOOLS_CONFIGURATION_ACCESS_HXX
26 
27 #include <rtl/ustring.hxx>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <vector>
33 #include <boost/function.hpp>
34 
35 namespace css = ::com::sun::star;
36 
37 namespace sd { namespace tools {
38 
39 /** This class gives access to the configuration.  Create an object of this
40     class for one node of the configuration.  This will be the root node.
41     Its children are then accessible through the new ConfigurationAccess
42     object.
43 */
44 class ConfigurationAccess
45 {
46 public:
47     enum WriteMode { READ_WRITE, READ_ONLY };
48 
49     /** Create a new object to access the configuration entries below the
50         given root.
51         @param rsRootName
52             Name of the root.
53         @param eMode
54             This flag specifies whether to give read-write or read-only
55             access.
56     */
57     ConfigurationAccess(
58         const ::rtl::OUString& rsRootName,
59         const WriteMode eMode);
60 
61     ConfigurationAccess(
62         const css::uno::Reference<css::uno::XComponentContext>& rxContext,
63         const ::rtl::OUString& rsRootName,
64         const WriteMode eMode);
65 
66     /** Return a configuration node below the root of the called object.
67         @param rsPathToNode
68             The relative path from the root (as given the constructor) to
69             the node.
70         @return
71             The type of the returned node varies with the requested node.
72             It is empty when the node was not found.
73     */
74     css::uno::Any GetConfigurationNode (
75         const ::rtl::OUString& rsPathToNode);
76 
77     /** Return a configuration node below the given node.
78         @param rxNode
79             The node that acts as root to the given relative path.
80         @param rsPathToNode
81             The relative path from the given node to the requested node.
82         @return
83             The type of the returned node varies with the requested node.
84             It is empty when the node was not found.
85     */
86     static css::uno::Any GetConfigurationNode (
87         const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
88         const ::rtl::OUString& rsPathToNode);
89 
90     /** Write any changes that have been made back to the configuration.
91         This call is ignored when the called ConfigurationAccess object was
92         not create with read-write mode.
93     */
94     void CommitChanges (void);
95 
96     /** This functor is typically called for every item in a set.  Its two
97         parameters are the name of key item (often of no further interest)
98         and the value of the item.
99     */
100     typedef ::boost::function<void(
101         const ::rtl::OUString&,
102         const std::vector<css::uno::Any>&) > Functor;
103 
104     /** Execute a functor for all elements of the given container.
105         @param rxContainer
106             The container is a XNameAccess to a list of the configuration.
107             This can be a node returned by GetConfigurationNode().
108         @param rArguments
109             The functor is called with arguments that are children of each
110             element of the container.  The set of children is specified  this
111             list.
112         @param rFunctor
113             The functor to be executed for some or all of the elements in
114             the given container.
115     */
116     static void ForAll (
117         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
118         const ::std::vector<rtl::OUString>& rArguments,
119         const Functor& rFunctor);
120 
121     /** Fill a list with the string contents of all sub-elements in the given container.
122         @param rxContainer
123             The container is a XNameAccess to a list of the configuration.
124             This can be a node returned by GetConfigurationNode().
125         @param rsArgument
126             This specifies which string children of the elements in the
127             container are to be inserted into the list.  The specified child
128             has to be of type string.
129         @param rList
130             The list to be filled.
131     */
132     static void FillList(
133         const css::uno::Reference<css::container::XNameAccess>& rxContainer,
134         const ::rtl::OUString& rsArgument,
135         ::std::vector<rtl::OUString>& rList);
136 
137 private:
138     css::uno::Reference<css::uno::XInterface> mxRoot;
139 
140     void Initialize (
141         const css::uno::Reference<css::lang::XMultiServiceFactory>& rxProvider,
142         const ::rtl::OUString& rsRootName,
143         const WriteMode eMode);
144 };
145 
146 } } // end of namespace sd::tools
147 
148 #endif
149