16df1ea1fSAndrew Rist /**************************************************************
26df1ea1fSAndrew Rist  *
36df1ea1fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
46df1ea1fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
56df1ea1fSAndrew Rist  * distributed with this work for additional information
66df1ea1fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
76df1ea1fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
86df1ea1fSAndrew Rist  * "License"); you may not use this file except in compliance
96df1ea1fSAndrew Rist  * with the License.  You may obtain a copy of the License at
106df1ea1fSAndrew Rist  *
116df1ea1fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
126df1ea1fSAndrew Rist  *
136df1ea1fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
146df1ea1fSAndrew Rist  * software distributed under the License is distributed on an
156df1ea1fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
166df1ea1fSAndrew Rist  * KIND, either express or implied.  See the License for the
176df1ea1fSAndrew Rist  * specific language governing permissions and limitations
186df1ea1fSAndrew Rist  * under the License.
196df1ea1fSAndrew Rist  *
206df1ea1fSAndrew Rist  *************************************************************/
216df1ea1fSAndrew Rist 
226df1ea1fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _WEBDAV_UCP_CONTENTPROPERTIES_HXX
25cdf0e10cSrcweir #define _WEBDAV_UCP_CONTENTPROPERTIES_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <memory>
28cdf0e10cSrcweir #include <vector>
29cdf0e10cSrcweir #include <hash_map>
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
32cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace beans {
35cdf0e10cSrcweir     struct Property;
36cdf0e10cSrcweir } } } }
37cdf0e10cSrcweir 
38*59ddfc10SAndre Fischer namespace http_dav_ucp
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir struct DAVResource;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //=========================================================================
44cdf0e10cSrcweir 
45cdf0e10cSrcweir struct equalString
46cdf0e10cSrcweir {
operator ()http_dav_ucp::equalString47cdf0e10cSrcweir   bool operator()( const rtl::OUString& s1, const rtl::OUString& s2 ) const
48cdf0e10cSrcweir   {
49cdf0e10cSrcweir       return !!( s1 == s2 );
50cdf0e10cSrcweir   }
51cdf0e10cSrcweir };
52cdf0e10cSrcweir 
53cdf0e10cSrcweir struct hashString
54cdf0e10cSrcweir {
operator ()http_dav_ucp::hashString55cdf0e10cSrcweir     size_t operator()( const rtl::OUString & rName ) const
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         return rName.hashCode();
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir };
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //=========================================================================
62cdf0e10cSrcweir //
63cdf0e10cSrcweir // PropertyValueMap.
64cdf0e10cSrcweir //
65cdf0e10cSrcweir //=========================================================================
66cdf0e10cSrcweir 
67cdf0e10cSrcweir class PropertyValue
68cdf0e10cSrcweir {
69cdf0e10cSrcweir private:
70cdf0e10cSrcweir     ::com::sun::star::uno::Any m_aValue;
71cdf0e10cSrcweir     bool                       m_bIsCaseSensitive;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir public:
PropertyValue()74cdf0e10cSrcweir     PropertyValue()
75cdf0e10cSrcweir     : m_bIsCaseSensitive( true ) {}
76cdf0e10cSrcweir 
PropertyValue(const::com::sun::star::uno::Any & rValue,bool bIsCaseSensitive)77cdf0e10cSrcweir     PropertyValue( const ::com::sun::star::uno::Any & rValue,
78cdf0e10cSrcweir                    bool bIsCaseSensitive )
79cdf0e10cSrcweir     : m_aValue( rValue),
80cdf0e10cSrcweir       m_bIsCaseSensitive( bIsCaseSensitive ) {}
81cdf0e10cSrcweir 
isCaseSensitive() const82cdf0e10cSrcweir     bool isCaseSensitive() const { return m_bIsCaseSensitive; }
value() const83cdf0e10cSrcweir     const ::com::sun::star::uno::Any & value() const { return m_aValue; }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir };
86cdf0e10cSrcweir 
87cdf0e10cSrcweir typedef std::hash_map
88cdf0e10cSrcweir <
89cdf0e10cSrcweir     rtl::OUString,
90cdf0e10cSrcweir     PropertyValue,
91cdf0e10cSrcweir     hashString,
92cdf0e10cSrcweir     equalString
93cdf0e10cSrcweir >
94cdf0e10cSrcweir PropertyValueMap;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir struct DAVResource;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir class ContentProperties
99cdf0e10cSrcweir {
100cdf0e10cSrcweir public:
101cdf0e10cSrcweir     ContentProperties();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     ContentProperties( const DAVResource& rResource );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     // Mini props for transient contents.
106cdf0e10cSrcweir     ContentProperties( const rtl::OUString & rTitle, sal_Bool bFolder );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // Micro props for non-existing contents.
109cdf0e10cSrcweir     ContentProperties( const rtl::OUString & rTitle );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     ContentProperties( const ContentProperties & rOther );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     bool contains( const rtl::OUString & rName ) const;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     const com::sun::star::uno::Any &
116cdf0e10cSrcweir     getValue( const rtl::OUString & rName ) const;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     // Maps the UCB property names contained in rProps with their DAV property
119cdf0e10cSrcweir     // counterparts, if possible. All unmappable properties will be included
120cdf0e10cSrcweir     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
121cdf0e10cSrcweir     // The vector filles by this method can directly be handed over to
122cdf0e10cSrcweir     // DAVResourceAccess::PROPFIND. The result from PROPFIND
123cdf0e10cSrcweir     // (vector< DAVResource >) can be used to create a ContentProperties
124cdf0e10cSrcweir     // instance which can map DAV properties back to UCB properties.
125cdf0e10cSrcweir     static void UCBNamesToDAVNames( const com::sun::star::uno::Sequence<
126cdf0e10cSrcweir                                         com::sun::star::beans::Property > &
127cdf0e10cSrcweir                                             rProps,
128cdf0e10cSrcweir                                     std::vector< rtl::OUString > & resources,
129cdf0e10cSrcweir                                     bool bIncludeUnmatched = true );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     // Maps the UCB property names contained in rProps with their HTTP header
132cdf0e10cSrcweir     // counterparts, if possible. All unmappable properties will be included
133cdf0e10cSrcweir     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
134cdf0e10cSrcweir     // The vector filles by this method can directly be handed over to
135cdf0e10cSrcweir     // DAVResourceAccess::HEAD. The result from HEAD (vector< DAVResource >)
136cdf0e10cSrcweir     // can be used to create a ContentProperties instance which can map header
137cdf0e10cSrcweir     // names back to UCB properties.
138cdf0e10cSrcweir     static void UCBNamesToHTTPNames( const com::sun::star::uno::Sequence<
139cdf0e10cSrcweir                                         com::sun::star::beans::Property > &
140cdf0e10cSrcweir                                             rProps,
141cdf0e10cSrcweir                                     std::vector< rtl::OUString > & resources,
142cdf0e10cSrcweir                                     bool bIncludeUnmatched = true );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     // return true, if all properties contained in rProps are contained in
145cdf0e10cSrcweir     // this ContentProperties instance. Otherwiese, false will be returned.
146cdf0e10cSrcweir     // rNamesNotContained contain the missing names.
147cdf0e10cSrcweir     bool containsAllNames(
148cdf0e10cSrcweir                     const com::sun::star::uno::Sequence<
149cdf0e10cSrcweir                         com::sun::star::beans::Property >& rProps,
150cdf0e10cSrcweir                     std::vector< rtl::OUString > & rNamesNotContained ) const;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // adds all properties described by rProps that are actually contained in
153cdf0e10cSrcweir     // rContentProps to this instance. In case of duplicates the value
154cdf0e10cSrcweir     // already contained in this will left unchanged.
155cdf0e10cSrcweir     void addProperties( const std::vector< rtl::OUString > & rProps,
156cdf0e10cSrcweir                         const ContentProperties & rContentProps );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     // overwrites probably existing entries.
159cdf0e10cSrcweir     void addProperties( const ContentProperties & rProps );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     // overwrites probably existing entries.
162cdf0e10cSrcweir     void addProperties( const std::vector< DAVPropertyValue > & rProps );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     // overwrites probably existing entry.
165cdf0e10cSrcweir     void addProperty( const rtl::OUString & rName,
166cdf0e10cSrcweir                      const com::sun::star::uno::Any & rValue,
167cdf0e10cSrcweir                      bool bIsCaseSensitive );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // overwrites probably existing entry.
170cdf0e10cSrcweir     void addProperty( const DAVPropertyValue & rProp );
171cdf0e10cSrcweir 
isTrailingSlash() const172cdf0e10cSrcweir     bool isTrailingSlash() const { return m_bTrailingSlash; }
173cdf0e10cSrcweir 
getEscapedTitle() const174cdf0e10cSrcweir     const rtl::OUString & getEscapedTitle() const { return m_aEscapedTitle; }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     // Not good to expose implementation details, but this is actually an
177cdf0e10cSrcweir     // internal class.
getProperties() const178cdf0e10cSrcweir     const std::auto_ptr< PropertyValueMap > & getProperties() const
179cdf0e10cSrcweir     { return m_xProps; }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir private:
182cdf0e10cSrcweir     ::rtl::OUString m_aEscapedTitle;
183cdf0e10cSrcweir     std::auto_ptr< PropertyValueMap > m_xProps;
184cdf0e10cSrcweir     bool m_bTrailingSlash;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     static com::sun::star::uno::Any m_aEmptyAny;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     ContentProperties & operator=( const ContentProperties & ); // n.i.
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     const PropertyValue * get( const rtl::OUString & rName ) const;
191cdf0e10cSrcweir };
192cdf0e10cSrcweir 
193cdf0e10cSrcweir class CachableContentProperties
194cdf0e10cSrcweir {
195cdf0e10cSrcweir private:
196cdf0e10cSrcweir     ContentProperties m_aProps;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     CachableContentProperties & operator=( const CachableContentProperties & ); // n.i.
199cdf0e10cSrcweir     CachableContentProperties( const CachableContentProperties & ); // n.i.
200cdf0e10cSrcweir 
201cdf0e10cSrcweir public:
202cdf0e10cSrcweir     CachableContentProperties( const ContentProperties & rProps );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     void addProperties( const ContentProperties & rProps );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     void addProperties( const std::vector< DAVPropertyValue > & rProps );
207cdf0e10cSrcweir 
containsAllNames(const com::sun::star::uno::Sequence<com::sun::star::beans::Property> & rProps,std::vector<rtl::OUString> & rNamesNotContained) const208cdf0e10cSrcweir     bool containsAllNames(
209cdf0e10cSrcweir                     const com::sun::star::uno::Sequence<
210cdf0e10cSrcweir                         com::sun::star::beans::Property >& rProps,
211cdf0e10cSrcweir                     std::vector< rtl::OUString > & rNamesNotContained ) const
212cdf0e10cSrcweir     { return m_aProps.containsAllNames( rProps, rNamesNotContained ); }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     const com::sun::star::uno::Any &
getValue(const rtl::OUString & rName) const215cdf0e10cSrcweir     getValue( const rtl::OUString & rName ) const
216cdf0e10cSrcweir     { return m_aProps.getValue( rName ); }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     operator const ContentProperties & () const { return m_aProps; }
219cdf0e10cSrcweir };
220cdf0e10cSrcweir 
221*59ddfc10SAndre Fischer } // namespace http_dav_ucp
222cdf0e10cSrcweir 
223cdf0e10cSrcweir #endif /* !_WEBDAV_UCP_CONTENTPROPERTIES_HXX */
224