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 
25 #ifndef _UCBHELPER_CONTENT_HXX_
26 #define _UCBHELPER_CONTENT_HXX_
27 
28 #include <rtl/string.hxx>
29 #include <rtl/ustring>
30 #include <osl/mutex.hxx>
31 #include <osl/thread.h>
32 
33 #include <cppuhelper/weak.hxx>
34 #include <com/sun/star/ucb/XCommandTaskProcessor.hpp>
35 #include <com/sun/star/ucb/XCommandInfo.hpp>
36 #include <com/sun/star/ucb/XContent.hpp>
37 #include <com/sun/star/ucb/XPropertyTaskProcessor.hpp>
38 #include <com/sun/star/ucb/XContentIdentifier.hpp>
39 #include <com/sun/star/beans/XPropertiesChangeListener.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 
42 #include <list>
43 
44 using namespace cppu;
45 using namespace com::sun::star::ucb;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::beans;
48 using namespace com::sun::star::lang;
49 using namespace std;
50 using namespace rtl;
51 using namespace osl;
52 
53 
54 
55 template <class Type> class safe_list : public osl::Mutex, public std::list< Type > {};
56 
57 class OSimpleContentIdentifier :	public OWeakObject,
58 									public XContentIdentifier
59 {
60 private:
61 	OUString	Identifier;
62 	OUString	ProviderScheme;
63 
64 public:
65 	OSimpleContentIdentifier( const OUString& rIdentifier, const OUString& rProviderScheme );
66 
67 	// XInterface
68 	virtual Any			SAL_CALL queryInterface( const Type &type ) throw( RuntimeException );
69 	virtual void 		SAL_CALL acquire() throw(RuntimeException);
70 	virtual void 		SAL_CALL release() throw(RuntimeException);
71 
72 	// XContentIdentifier
73     virtual OUString 	SAL_CALL getContentIdentifier() throw(RuntimeException);
74     virtual OUString 	SAL_CALL getContentProviderScheme() throw(RuntimeException);
75 };
76 
77 
78 //---------------------------------------------------------------------------
79 //
80 //	FileSystemContent
81 //
82 //---------------------------------------------------------------------------
83 
84 class OContent :
85 public OWeakObject,
86 public XContent,
87 public XCommandTaskProcessor,
88 public XPropertyTaskProcessor,
89 public XCommandInfo,
90 public XPropertySetInfo,
91 public XComponent
92 {
93 public:
94 	struct PropertyChangeEventInfo
95 	{
96 		OUString	Name;
97 		long		Handle;
98 
PropertyChangeEventInfoOContent::PropertyChangeEventInfo99 		PropertyChangeEventInfo() : Handle( -1 ) {}
100 
operator ==OContent::PropertyChangeEventInfo101 		inline int operator ==( const PropertyChangeEventInfo& crInfo ) const
102 		{ return Handle == crInfo.Handle && Handle > 0 || Name == crInfo.Name; }
103 	#ifdef __SUNPRO_CC
operator <OContent::PropertyChangeEventInfo104 		inline int operator <( const PropertyChangeEventInfo& crInfo ) const
105 		{ return Handle != crInfo.Handle ? Handle < crInfo.Handle : Name < crInfo.Name; }
106 	#endif
107 	};
108 
109 	struct PropertyChangeListenerInfo
110 	{
111 		Reference< XPropertiesChangeListener >	xListener;
112 		list< PropertyChangeEventInfo >			aEventInfos;
113 
operator ==OContent::PropertyChangeListenerInfo114 		inline int operator ==( const PropertyChangeListenerInfo& crInfo ) const
115 		{ return xListener == crInfo.xListener; }
116 	#ifdef __SUNPRO_CC
operator <OContent::PropertyChangeListenerInfo117 		inline int operator <( const PropertyChangeListenerInfo& crInfo ) const
118 		{ return xListener < crInfo.xListener; }
119 	#endif
120 	};
121 
122 protected:
123 	Sequence< PropertyChangeEvent > matchListenerEvents( const Sequence< PropertyChangeEvent >& crEvents, const PropertyChangeListenerInfo & crInfo );
124 
125 	safe_list< Reference< XContentEventListener > >	m_aContentListeners;
126 	safe_list< Reference< XEventListener > >		m_aComponentListeners;
127 	safe_list< PropertyChangeListenerInfo >			m_aPropertyChangeListeners;
128 public:
~OContent()129 	virtual ~OContent() {}
130 
131 	virtual void broadcastContentEvent( const ContentEvent & crEvent );
132 	virtual void broadcastPropertiesChangeEvents( const Sequence< PropertyChangeEvent >& crEvents );
133 
134 	// To be implemented by inheritents
135 	virtual Any doCommand( const Command & crCommand ) = 0;
136 
137 	// XInterface
138 	virtual Any			SAL_CALL queryInterface( const Type &type ) throw( RuntimeException );
139 
140 	virtual void SAL_CALL acquire() throw(RuntimeException);
141 	virtual void SAL_CALL release() throw(RuntimeException);
142 
143 	// XContent
144 	virtual void SAL_CALL addContentEventListener( const Reference< XContentEventListener >& rListener ) throw();
145 	virtual void SAL_CALL removeContentEventListener( const Reference< XContentEventListener >& rListener ) throw();
146 
147 	// XComponent
148 	virtual void SAL_CALL dispose() throw();
149 	virtual void SAL_CALL addEventListener( const Reference< XEventListener >& xListener ) throw();
150 	virtual void SAL_CALL removeEventListener( const Reference< XEventListener >& xListener ) throw();
151 
152 	// XCommmandTaskProcessor
153 	virtual Reference< XCommandInfo > SAL_CALL getCommandsInfo() throw();
154 
155 	// XCommandInfo
156 	virtual CommandInfo SAL_CALL getCommandInfoByName( const OUString& rName ) throw( UnsupportedCommandException );
157 	virtual CommandInfo SAL_CALL getCommandInfoByHandle( long nHandle )	throw( UnsupportedCommandException );
158 	virtual sal_Bool SAL_CALL hasCommandByName( const OUString& rName ) throw();
159 	virtual sal_Bool SAL_CALL hasCommandByHandle( long nHandle ) throw();
160 
161 	// XPropertyTaskProcessor
162 	virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw();
163 
164 	// XPropertySetInfo
165 	virtual Property SAL_CALL getPropertyByName( const OUString& Name ) throw( UnknownPropertyException );
166 	virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) throw();
167 	virtual void SAL_CALL addPropertiesChangeListener( const Sequence< OUString >& Names, const Reference< XPropertiesChangeListener >& xListener ) throw();
168 	virtual void SAL_CALL removePropertiesChangeListener( const Sequence< OUString >& Names, const Reference< XPropertiesChangeListener >& xListener ) throw();
169 };
170 
171 //---------------------------------------------------------------------------
172 //
173 //	FolderContent
174 //
175 //---------------------------------------------------------------------------
176 
177 // supported Commands
178 static const sal_Int32 OPEN		= 0;
179 static const sal_Int32 CLOSE	= 1;
180 
181 class OFolderContent : public OContent
182 {
183 protected:
184 	// Already provided children
185 	safe_list< XContent >	m_aChildList;
186 
187 	// OContent
188 	virtual Any doCommand( const Command & crCommand );
189 
190 	// new methods, can be overloaded
191 	virtual Any doOpenCommand();
192 	virtual Any doCloseCommand();
193 
194 	// To be implemented by inheritants
195 	virtual Sequence< XContent > getAllChildren() = 0;
196 
197 public:
198 
199 	// XCommmandTaskProcessor
200 	virtual Reference< XCommandTask > SAL_CALL createCommandTask(const Command& rCommand, const Reference< XContentTaskEnvironment >& rEnvironment ) throw();
201 
202 	// XCommandInfo
203 	virtual Sequence< CommandInfo > SAL_CALL getCommands() throw();
204 };
205 
206 //---------------------------------------------------------------------------
207 //
208 //	OContentTask
209 //
210 //---------------------------------------------------------------------------
211 
212 class OContentTask :
213 public OWeakObject,
214 public XContentTask
215 {
216 	Guard< OContent >						m_aContentGuard;
217 protected:
218 	OContent								*m_pContent;
219 	Reference< XContentTaskEnvironment >	m_xEnvironment;
220 	ContentTaskStatus						m_eStatus;
221 	oslThread								m_aThread;
222 
223 	static void executeWorker( void * );
224 	virtual ContentTaskStatus setStatus( ContentTaskStatus eStatus );
225 
226 	// To be implemented by inheritants
227 	virtual void doExecute() = 0;
228 public:
229 	OContentTask(  const Reference< XContentTaskEnvironment >& xEnv, OContent *pContent );
230 	virtual ~OContentTask();
231 
232 	// XInterface
233 	virtual Any			SAL_CALL queryInterface( const Type &type ) throw( RuntimeException );
234 
235 	virtual void SAL_CALL acquire() throw(RuntimeException);
236 	virtual void SAL_CALL release() throw(RuntimeException);
237 
238 	// XContentTask
239 	virtual void SAL_CALL start() throw();
240 	virtual void SAL_CALL execute() throw( Exception );
241 	virtual void SAL_CALL abort() throw();
242 	virtual ContentTaskStatus SAL_CALL getStatus() throw();
243 	virtual Reference< XContentTaskEnvironment > SAL_CALL getEnvironment() throw();
244 };
245 
246 //---------------------------------------------------------------------------
247 //
248 //	OCommandTask
249 //
250 //---------------------------------------------------------------------------
251 
252 class OCommandTask :
253 public OContentTask,
254 public XCommandTask
255 {
256 protected:
257 	Command				m_aCommand;
258 	Any					m_aResult;
259 
260 public:
261 	OCommandTask( const Reference< XContentTaskEnvironment >& xEnv, OContent *pContent, const Command& rCommand );
262 	virtual ~OCommandTask();
263 
264 	virtual void doExecute();
265 
266 	// XInterface
267 	virtual Any			SAL_CALL queryInterface( const Type &type ) throw( RuntimeException );
268 	virtual void 		SAL_CALL acquire() throw(RuntimeException);
269 	virtual void 		SAL_CALL release() throw(RuntimeException);
270 
271 	// XContentTask
272 	virtual void SAL_CALL start() throw();
273 	virtual void SAL_CALL execute() throw( Exception );
274 	virtual void SAL_CALL abort() throw();
275 	virtual ContentTaskStatus SAL_CALL getStatus() throw();
276 	virtual Reference< XContentTaskEnvironment > SAL_CALL getEnvironment() throw();
277 
278 	// XCommandTask
279 	virtual Command SAL_CALL getCommand() throw();
280 	virtual Any SAL_CALL getResult() throw();
281 };
282 
283 //---------------------------------------------------------------------------
284 //
285 //	OPropertyTask
286 //
287 //---------------------------------------------------------------------------
288 
289 class OPropertyTask :
290 public OContentTask,
291 public XPropertyTask
292 {
293 protected:
294 	Sequence< PropertyValueInfo >	m_aProperties;
295 	PropertyTaskType				m_eType;
296 public:
297 	OPropertyTask(const Reference< XContentTaskEnvironment >& Environment, OContent *pContent, const Sequence< PropertyValue >& Properties, PropertyTaskType Type );
298 	virtual	~OPropertyTask();
299 
300 	virtual void doExecute();
301 
302 	// To be implemented by inheritants
303 	virtual Any setPropertyValue( PropertyValueInfo & rProperty ) = 0;
304 	virtual void getPropertyValue( PropertyValueInfo & rProperty ) = 0;
305 
306 	// XInterface
307 	virtual Any			SAL_CALL queryInterface( const Type &type ) throw( RuntimeException );
308 	virtual void 		SAL_CALL acquire() throw(RuntimeException);
309 	virtual void 		SAL_CALL release() throw(RuntimeException);
310 
311 	// XContentTask
312 	virtual void SAL_CALL start() throw();
313 	virtual void SAL_CALL execute() throw( Exception );
314 	virtual void SAL_CALL abort() throw();
315 	virtual ContentTaskStatus SAL_CALL getStatus() throw();
316 	virtual Reference< XContentTaskEnvironment > SAL_CALL getEnvironment() throw();
317 
318 	// XPropertyTask
319 	PropertyTaskType SAL_CALL getType() throw();
320 	Sequence< PropertyValueInfo > SAL_CALL getProperties() throw();
321 };
322 
323 #endif // _UCBHELPER_CONTENT_HXX_
324 
325 
326