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 _HIERARCHYCONTENT_HXX
29 #define _HIERARCHYCONTENT_HXX
30 
31 #include <list>
32 #include <rtl/ref.hxx>
33 #include <com/sun/star/ucb/XContentCreator.hpp>
34 #include <ucbhelper/contenthelper.hxx>
35 #include "hierarchydata.hxx"
36 #include "hierarchyprovider.hxx"
37 
38 namespace com { namespace sun { namespace star { namespace beans {
39     struct Property;
40     struct PropertyValue;
41 } } } }
42 
43 namespace com { namespace sun { namespace star { namespace sdbc {
44     class XRow;
45 } } } }
46 
47 namespace com { namespace sun { namespace star { namespace ucb {
48     struct TransferInfo;
49 } } } }
50 
51 namespace hierarchy_ucp
52 {
53 
54 //=========================================================================
55 
56 #define HIERARCHY_ROOT_FOLDER_CONTENT_SERVICE_NAME \
57                             "com.sun.star.ucb.HierarchyRootFolderContent"
58 #define HIERARCHY_FOLDER_CONTENT_SERVICE_NAME \
59                             "com.sun.star.ucb.HierarchyFolderContent"
60 #define HIERARCHY_LINK_CONTENT_SERVICE_NAME \
61                             "com.sun.star.ucb.HierarchyLinkContent"
62 
63 //=========================================================================
64 
65 class HierarchyContentProperties
66 {
67 public:
68     HierarchyContentProperties() {};
69 
70     HierarchyContentProperties( const HierarchyEntryData::Type & rType )
71     : m_aData( rType ),
72       m_aContentType( rType == HierarchyEntryData::FOLDER
73         ? rtl::OUString::createFromAscii( HIERARCHY_FOLDER_CONTENT_TYPE )
74         : rtl::OUString::createFromAscii( HIERARCHY_LINK_CONTENT_TYPE ) ) {}
75 
76     HierarchyContentProperties( const HierarchyEntryData & rData )
77     : m_aData( rData ),
78       m_aContentType( rData.getType() == HierarchyEntryData::FOLDER
79         ? rtl::OUString::createFromAscii( HIERARCHY_FOLDER_CONTENT_TYPE )
80         : rtl::OUString::createFromAscii( HIERARCHY_LINK_CONTENT_TYPE ) ) {}
81 
82     const rtl::OUString & getName() const { return m_aData.getName(); }
83     void setName( const rtl::OUString & rName ) { m_aData.setName( rName ); };
84 
85     const rtl::OUString & getTitle() const { return m_aData.getTitle(); }
86     void setTitle( const rtl::OUString & rTitle )
87     { m_aData.setTitle( rTitle ); };
88 
89     const rtl::OUString & getTargetURL() const
90     { return m_aData.getTargetURL(); }
91     void setTargetURL( const rtl::OUString & rURL )
92     { m_aData.setTargetURL( rURL ); };
93 
94     const rtl::OUString & getContentType() const { return m_aContentType; }
95 
96     sal_Bool getIsFolder() const
97     { return m_aData.getType() == HierarchyEntryData::FOLDER; }
98 
99     sal_Bool getIsDocument() const { return !getIsFolder(); }
100 
101     com::sun::star::uno::Sequence< com::sun::star::ucb::ContentInfo >
102     getCreatableContentsInfo() const;
103 
104     const HierarchyEntryData & getHierarchyEntryData() const { return m_aData; }
105 
106 private:
107     HierarchyEntryData m_aData;
108     rtl::OUString m_aContentType;
109 };
110 
111 //=========================================================================
112 
113 class HierarchyContentProvider;
114 
115 class HierarchyContent : public ::ucbhelper::ContentImplHelper,
116                          public com::sun::star::ucb::XContentCreator
117 {
118     enum ContentKind  { LINK, FOLDER, ROOT };
119     enum ContentState { TRANSIENT,  // created via CreateNewContent,
120                                         // but did not process "insert" yet
121                         PERSISTENT, // processed "insert"
122                         DEAD        // processed "delete"
123                       };
124 
125     HierarchyContentProperties m_aProps;
126     ContentKind                m_eKind;
127     ContentState               m_eState;
128     HierarchyContentProvider*  m_pProvider;
129     bool                       m_bCheckedReadOnly;
130     bool                       m_bIsReadOnly;
131 
132 private:
133     HierarchyContent(
134             const com::sun::star::uno::Reference<
135                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
136             HierarchyContentProvider* pProvider,
137             const com::sun::star::uno::Reference<
138                 com::sun::star::ucb::XContentIdentifier >& Identifier,
139             const HierarchyContentProperties& rProps );
140     HierarchyContent(
141             const com::sun::star::uno::Reference<
142                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
143             HierarchyContentProvider* pProvider,
144             const com::sun::star::uno::Reference<
145                 com::sun::star::ucb::XContentIdentifier >& Identifier,
146             const com::sun::star::ucb::ContentInfo& Info );
147 
148     virtual com::sun::star::uno::Sequence< com::sun::star::beans::Property >
149     getProperties( const com::sun::star::uno::Reference<
150                     com::sun::star::ucb::XCommandEnvironment > & xEnv );
151     virtual com::sun::star::uno::Sequence< com::sun::star::ucb::CommandInfo >
152     getCommands( const com::sun::star::uno::Reference<
153                     com::sun::star::ucb::XCommandEnvironment > & xEnv );
154     virtual ::rtl::OUString getParentURL();
155 
156     static sal_Bool hasData(
157             const com::sun::star::uno::Reference<
158                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
159             HierarchyContentProvider* pProvider,
160             const com::sun::star::uno::Reference<
161                 com::sun::star::ucb::XContentIdentifier >& Identifier );
162     sal_Bool hasData(
163             const com::sun::star::uno::Reference<
164                 com::sun::star::ucb::XContentIdentifier >& Identifier )
165     { return hasData( m_xSMgr, m_pProvider, Identifier ); }
166     static sal_Bool loadData(
167             const com::sun::star::uno::Reference<
168                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
169             HierarchyContentProvider* pProvider,
170             const com::sun::star::uno::Reference<
171                 com::sun::star::ucb::XContentIdentifier >& Identifier,
172             HierarchyContentProperties& rProps );
173     sal_Bool storeData();
174     sal_Bool renameData( const com::sun::star::uno::Reference<
175                             com::sun::star::ucb::XContentIdentifier >& xOldId,
176                          const com::sun::star::uno::Reference<
177                             com::sun::star::ucb::XContentIdentifier >& xNewId );
178     sal_Bool removeData();
179 
180     void setKind( const com::sun::star::uno::Reference<
181                     com::sun::star::ucb::XContentIdentifier >& Identifier );
182 
183     bool isReadOnly();
184 
185     sal_Bool isFolder() const { return ( m_eKind > LINK ); }
186 
187     ::com::sun::star::uno::Reference<
188         ::com::sun::star::ucb::XContentIdentifier >
189     makeNewIdentifier( const rtl::OUString& rTitle );
190 
191     typedef rtl::Reference< HierarchyContent > HierarchyContentRef;
192     typedef std::list< HierarchyContentRef > HierarchyContentRefList;
193     void queryChildren( HierarchyContentRefList& rChildren );
194 
195     sal_Bool exchangeIdentity(
196                 const ::com::sun::star::uno::Reference<
197                         ::com::sun::star::ucb::XContentIdentifier >& xNewId );
198 
199     ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow >
200     getPropertyValues( const ::com::sun::star::uno::Sequence<
201                             ::com::sun::star::beans::Property >& rProperties );
202     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
203     setPropertyValues(
204             const ::com::sun::star::uno::Sequence<
205                     ::com::sun::star::beans::PropertyValue >& rValues,
206             const ::com::sun::star::uno::Reference<
207                     ::com::sun::star::ucb::XCommandEnvironment > & xEnv )
208         throw( ::com::sun::star::uno::Exception );
209 
210     void insert( sal_Int32 nNameClashResolve,
211                  const ::com::sun::star::uno::Reference<
212                     ::com::sun::star::ucb::XCommandEnvironment > & xEnv )
213         throw( ::com::sun::star::uno::Exception );
214 
215     void destroy( sal_Bool bDeletePhysical,
216                   const ::com::sun::star::uno::Reference<
217                     ::com::sun::star::ucb::XCommandEnvironment > & xEnv )
218         throw( ::com::sun::star::uno::Exception );
219 
220     void transfer( const ::com::sun::star::ucb::TransferInfo& rInfo,
221                    const ::com::sun::star::uno::Reference<
222                     ::com::sun::star::ucb::XCommandEnvironment > & xEnv )
223         throw( ::com::sun::star::uno::Exception );
224 
225 public:
226     // Create existing content. Fail, if not already exists.
227     static HierarchyContent* create(
228             const com::sun::star::uno::Reference<
229                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
230             HierarchyContentProvider* pProvider,
231             const com::sun::star::uno::Reference<
232                 com::sun::star::ucb::XContentIdentifier >& Identifier );
233 
234     // Create new content. Fail, if already exists.
235     static HierarchyContent* create(
236             const com::sun::star::uno::Reference<
237                 com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
238             HierarchyContentProvider* pProvider,
239             const com::sun::star::uno::Reference<
240                 com::sun::star::ucb::XContentIdentifier >& Identifier,
241             const com::sun::star::ucb::ContentInfo& Info );
242 
243     virtual ~HierarchyContent();
244 
245     // XInterface
246     XINTERFACE_DECL()
247 
248     // XTypeProvider
249     XTYPEPROVIDER_DECL()
250 
251     // XServiceInfo
252     virtual ::rtl::OUString SAL_CALL
253     getImplementationName()
254         throw( ::com::sun::star::uno::RuntimeException );
255     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
256     getSupportedServiceNames()
257         throw( ::com::sun::star::uno::RuntimeException );
258 
259     // XContent
260     virtual rtl::OUString SAL_CALL
261     getContentType()
262         throw( com::sun::star::uno::RuntimeException );
263     virtual com::sun::star::uno::Reference<
264                 com::sun::star::ucb::XContentIdentifier > SAL_CALL
265     getIdentifier()
266         throw( com::sun::star::uno::RuntimeException );
267 
268     // XCommandProcessor
269     virtual com::sun::star::uno::Any SAL_CALL
270     execute( const com::sun::star::ucb::Command& aCommand,
271              sal_Int32 CommandId,
272              const com::sun::star::uno::Reference<
273                 com::sun::star::ucb::XCommandEnvironment >& Environment )
274         throw( com::sun::star::uno::Exception,
275                com::sun::star::ucb::CommandAbortedException,
276                com::sun::star::uno::RuntimeException );
277     virtual void SAL_CALL
278     abort( sal_Int32 CommandId )
279         throw( com::sun::star::uno::RuntimeException );
280 
281     //////////////////////////////////////////////////////////////////////
282     // Additional interfaces
283     //////////////////////////////////////////////////////////////////////
284 
285     // XContentCreator
286     virtual com::sun::star::uno::Sequence<
287                 com::sun::star::ucb::ContentInfo > SAL_CALL
288     queryCreatableContentsInfo()
289         throw( com::sun::star::uno::RuntimeException );
290     virtual com::sun::star::uno::Reference<
291                 com::sun::star::ucb::XContent > SAL_CALL
292     createNewContent( const com::sun::star::ucb::ContentInfo& Info )
293         throw( com::sun::star::uno::RuntimeException );
294 
295     //////////////////////////////////////////////////////////////////////
296     // Non-interface methods.
297     //////////////////////////////////////////////////////////////////////
298 
299     static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow >
300     getPropertyValues( const ::com::sun::star::uno::Reference<
301                         ::com::sun::star::lang::XMultiServiceFactory >& rSMgr,
302                        const ::com::sun::star::uno::Sequence<
303                             ::com::sun::star::beans::Property >& rProperties,
304                        const HierarchyContentProperties& rData,
305                        HierarchyContentProvider* pProvider,
306                        const ::rtl::OUString& rContentId );
307 };
308 
309 } // namespace hierarchy_ucp
310 
311 #endif /* !_HIERARCHYCONTENT_HXX */
312