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 _WEBDAV_UCP_CONTENTPROPERTIES_HXX
29 #define _WEBDAV_UCP_CONTENTPROPERTIES_HXX
30 
31 #include <memory>
32 #include <vector>
33 #include <hash_map>
34 #include <rtl/ustring.hxx>
35 #include <com/sun/star/uno/Any.hxx>
36 #include <com/sun/star/uno/Sequence.hxx>
37 
38 namespace com { namespace sun { namespace star { namespace beans {
39     struct Property;
40 } } } }
41 
42 namespace webdav_ucp
43 {
44 
45 struct DAVResource;
46 
47 //=========================================================================
48 
49 struct equalString
50 {
51   bool operator()( const rtl::OUString& s1, const rtl::OUString& s2 ) const
52   {
53       return !!( s1 == s2 );
54   }
55 };
56 
57 struct hashString
58 {
59     size_t operator()( const rtl::OUString & rName ) const
60     {
61         return rName.hashCode();
62     }
63 };
64 
65 //=========================================================================
66 //
67 // PropertyValueMap.
68 //
69 //=========================================================================
70 
71 class PropertyValue
72 {
73 private:
74     ::com::sun::star::uno::Any m_aValue;
75     bool                       m_bIsCaseSensitive;
76 
77 public:
78     PropertyValue()
79     : m_bIsCaseSensitive( true ) {}
80 
81     PropertyValue( const ::com::sun::star::uno::Any & rValue,
82                    bool bIsCaseSensitive )
83     : m_aValue( rValue),
84       m_bIsCaseSensitive( bIsCaseSensitive ) {}
85 
86     bool isCaseSensitive() const { return m_bIsCaseSensitive; }
87     const ::com::sun::star::uno::Any & value() const { return m_aValue; }
88 
89 };
90 
91 typedef std::hash_map
92 <
93     rtl::OUString,
94     PropertyValue,
95     hashString,
96     equalString
97 >
98 PropertyValueMap;
99 
100 struct DAVResource;
101 
102 class ContentProperties
103 {
104 public:
105     ContentProperties();
106 
107     ContentProperties( const DAVResource& rResource );
108 
109     // Mini props for transient contents.
110     ContentProperties( const rtl::OUString & rTitle, sal_Bool bFolder );
111 
112     // Micro props for non-existing contents.
113     ContentProperties( const rtl::OUString & rTitle );
114 
115     ContentProperties( const ContentProperties & rOther );
116 
117     bool contains( const rtl::OUString & rName ) const;
118 
119     const com::sun::star::uno::Any &
120     getValue( const rtl::OUString & rName ) const;
121 
122     // Maps the UCB property names contained in rProps with their DAV property
123     // counterparts, if possible. All unmappable properties will be included
124     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
125     // The vector filles by this method can directly be handed over to
126     // DAVResourceAccess::PROPFIND. The result from PROPFIND
127     // (vector< DAVResource >) can be used to create a ContentProperties
128     // instance which can map DAV properties back to UCB properties.
129     static void UCBNamesToDAVNames( const com::sun::star::uno::Sequence<
130                                         com::sun::star::beans::Property > &
131                                             rProps,
132                                     std::vector< rtl::OUString > & resources,
133                                     bool bIncludeUnmatched = true );
134 
135     // Maps the UCB property names contained in rProps with their HTTP header
136     // counterparts, if possible. All unmappable properties will be included
137     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
138     // The vector filles by this method can directly be handed over to
139     // DAVResourceAccess::HEAD. The result from HEAD (vector< DAVResource >)
140     // can be used to create a ContentProperties instance which can map header
141     // names back to UCB properties.
142     static void UCBNamesToHTTPNames( const com::sun::star::uno::Sequence<
143                                         com::sun::star::beans::Property > &
144                                             rProps,
145                                     std::vector< rtl::OUString > & resources,
146                                     bool bIncludeUnmatched = true );
147 
148     // return true, if all properties contained in rProps are contained in
149     // this ContentProperties instance. Otherwiese, false will be returned.
150     // rNamesNotContained contain the missing names.
151     bool containsAllNames(
152                     const com::sun::star::uno::Sequence<
153                         com::sun::star::beans::Property >& rProps,
154                     std::vector< rtl::OUString > & rNamesNotContained ) const;
155 
156     // adds all properties described by rProps that are actually contained in
157     // rContentProps to this instance. In case of duplicates the value
158     // already contained in this will left unchanged.
159     void addProperties( const std::vector< rtl::OUString > & rProps,
160                         const ContentProperties & rContentProps );
161 
162     // overwrites probably existing entries.
163     void addProperties( const ContentProperties & rProps );
164 
165     // overwrites probably existing entries.
166     void addProperties( const std::vector< DAVPropertyValue > & rProps );
167 
168     // overwrites probably existing entry.
169     void addProperty( const rtl::OUString & rName,
170                      const com::sun::star::uno::Any & rValue,
171                      bool bIsCaseSensitive );
172 
173     // overwrites probably existing entry.
174     void addProperty( const DAVPropertyValue & rProp );
175 
176     bool isTrailingSlash() const { return m_bTrailingSlash; }
177 
178     const rtl::OUString & getEscapedTitle() const { return m_aEscapedTitle; }
179 
180     // Not good to expose implementation details, but this is actually an
181     // internal class.
182     const std::auto_ptr< PropertyValueMap > & getProperties() const
183     { return m_xProps; }
184 
185 private:
186     ::rtl::OUString m_aEscapedTitle;
187     std::auto_ptr< PropertyValueMap > m_xProps;
188     bool m_bTrailingSlash;
189 
190     static com::sun::star::uno::Any m_aEmptyAny;
191 
192     ContentProperties & operator=( const ContentProperties & ); // n.i.
193 
194     const PropertyValue * get( const rtl::OUString & rName ) const;
195 };
196 
197 class CachableContentProperties
198 {
199 private:
200     ContentProperties m_aProps;
201 
202     CachableContentProperties & operator=( const CachableContentProperties & ); // n.i.
203     CachableContentProperties( const CachableContentProperties & ); // n.i.
204 
205 public:
206     CachableContentProperties( const ContentProperties & rProps );
207 
208     void addProperties( const ContentProperties & rProps );
209 
210     void addProperties( const std::vector< DAVPropertyValue > & rProps );
211 
212     bool containsAllNames(
213                     const com::sun::star::uno::Sequence<
214                         com::sun::star::beans::Property >& rProps,
215                     std::vector< rtl::OUString > & rNamesNotContained ) const
216     { return m_aProps.containsAllNames( rProps, rNamesNotContained ); }
217 
218     const com::sun::star::uno::Any &
219     getValue( const rtl::OUString & rName ) const
220     { return m_aProps.getValue( rName ); }
221 
222     operator const ContentProperties & () const { return m_aProps; }
223 };
224 
225 } // namespace webdav_ucp
226 
227 #endif /* !_WEBDAV_UCP_CONTENTPROPERTIES_HXX */
228