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 /*TODO
25     - late init
26     - order by number!
27     - insert default detector and loader as last ones in hashes ... don't hold it as an extra member!
28       => CheckedIterator will be obsolete!
29  */
30 
31 #ifndef __FRAMEWORK_CLASSES_FILTERCACHE_HXX_
32 #define __FRAMEWORK_CLASSES_FILTERCACHE_HXX_
33 
34 //_________________________________________________________________________________________________________________
35 //	my own includes
36 //_________________________________________________________________________________________________________________
37 
38 #include <classes/filtercachedata.hxx>
39 #include <threadhelp/threadhelpbase.hxx>
40 
41 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONBASE_HXX_
42 #include <threadhelp/transactionbase.hxx>
43 #endif
44 #include <general.h>
45 #include <queries.h>
46 
47 //_________________________________________________________________________________________________________________
48 //	interface includes
49 //_________________________________________________________________________________________________________________
50 #include <com/sun/star/uno/Reference.hxx>
51 #include <com/sun/star/uno/Sequence.hxx>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/registry/XRegistryKey.hpp>
54 #include <com/sun/star/beans/PropertyValue.hpp>
55 #include <com/sun/star/container/ElementExistException.hpp>
56 #include <com/sun/star/container/NoSuchElementException.hpp>
57 
58 //_________________________________________________________________________________________________________________
59 //	other includes
60 //_________________________________________________________________________________________________________________
61 #include <rtl/ustring.hxx>
62 
63 #ifdef ENABLE_GENERATEFILTERCACHE
64 	#ifndef _RTL_USTRBUF_HXX_
65 	#include <rtl/ustrbuf.hxx>
66 	#endif
67 #endif
68 
69 //_________________________________________________________________________________________________________________
70 //	namespace
71 //_________________________________________________________________________________________________________________
72 
73 namespace framework{
74 
75 //_________________________________________________________________________________________________________________
76 //	exported const
77 //_________________________________________________________________________________________________________________
78 
79 //_________________________________________________________________________________________________________________
80 //	exported definitions
81 //_________________________________________________________________________________________________________________
82 
83 /*-************************************************************************************************************//**
84 	@short          cache for all filter and type information
85 	@descr			Frameloader- and filterfactory need some informations about our current registered filters and types.
86 					For better performance its necessary to cache all needed values.
87 
88 	@implements		-
89     @base           ThreadHelpBase
90                     TransactionBase
91 
92 	@devstatus		ready to use
93     @threadsafe     yes
94 *//*-*************************************************************************************************************/
95 
96 class FilterCache   :   private ThreadHelpBase
97                     ,   private TransactionBase
98 {
99     public:
100 
101 	//-------------------------------------------------------------------------------------------------------------
102 	//	public methods
103 	//-------------------------------------------------------------------------------------------------------------
104 
105 	public:
106 
107 		//---------------------------------------------------------------------------------------------------------
108 		//	constructor / destructor
109 		//---------------------------------------------------------------------------------------------------------
110 
111         FilterCache( sal_Int32 nVersion = DEFAULT_FILTERCACHE_VERSION,
112                      sal_Int16 nMode    = DEFAULT_FILTERCACHE_MODE   );
113 
114 		/*-****************************************************************************************************//**
115 			@short		standard destructor to delete instance
116 			@descr		This will clear the cache if last owner release it.
117 
118 			@seealso	-
119 
120 			@param		-
121 			@return		-
122 
123 			@onerror	-
124 		*//*-*****************************************************************************************************/
125 
126 		virtual ~FilterCache();
127 
128         void flush( DataContainer::ECFGType eType );
129 
130 		/*-****************************************************************************************************//**
131 			@short		get the current state of the cache
132 			@descr		Call this methods to get information about the state of the current cache.
133 
134 			@seealso	-
135 
136 			@param		-
137 			@return		-
138 			@return		-
139 
140 			@onerror	-
141 		*//*-*****************************************************************************************************/
142 
143         sal_Bool isValidOrRepairable() const;
144         sal_Bool hasTypes           () const;
145         sal_Bool hasFilters         () const;
146         sal_Bool hasDetectors       () const;
147         sal_Bool hasLoaders         () const;
148         sal_Bool hasContentHandlers () const;
149 
150 		/*-****************************************************************************************************//**
151 			@short		search routines to find items which match given parameter
152 			@descr		Mostly we search for a type first and get all informations about filter, detector and loader
153 						services from the other configuration tables which are registered for this type.
154 						These operations support a FindFirst/Next mechanism.
155 						If you call search...( ... nStartEntry=0 ... ) we search for
156 						the first entry. If these return a value different from <empty> you can work with these value.
157 						If found value isn't the right one - you can call search method again.
158 						DONT'T CHANGE THE VALUE OF "rStartEntry" between two search calls!
159 						You can use returned value as parameter for getBy...Name() functions of this implementation too!
160 
161 			@attention	returned type name is an internal name
162 						returned filter name is an internal name
163 						returned loader name is an implementation name of a service
164 						returned detector name is an implementation name of a service
165 
166 			@seealso	-
167 
168 			@param      "sResult", name of found type, filter, ...
169 			@return		true, if search was successful,
170 						false, otherwise.
171 
172 			@onerror	We return false.
173 		*//*-*****************************************************************************************************/
174 
175         sal_Bool searchType                     (   const   ::rtl::OUString&            sURL                ,
176                                                     const   ::rtl::OUString&            sMediaType          ,
177                                                     const   ::rtl::OUString&            sClipboardFormat    ,
178                                                             CheckedTypeIterator&        aStartEntry         ,
179                                                             ::rtl::OUString&            sResult             ) const;
180 
181         sal_Bool searchFilterForType            (   const   ::rtl::OUString&            sInternalTypeName   ,
182                                                             CheckedStringListIterator&  aStartEntry         ,
183                                                             ::rtl::OUString&            sResult             ) const;
184 
185         sal_Bool searchDetectorForType          (   const   ::rtl::OUString&            sInternalTypeName   ,
186                                                             CheckedStringListIterator&  aStartEntry         ,
187                                                             ::rtl::OUString&            sResult             ) const;
188 
189         sal_Bool searchLoaderForType            (   const   ::rtl::OUString&            sInternalTypeName   ,
190                                                             CheckedStringListIterator&  aStartEntry         ,
191                                                             ::rtl::OUString&            sResult             ) const;
192 
193         sal_Bool searchContentHandlerForType    (   const   ::rtl::OUString&            sInternalTypeName   ,
194                                                             CheckedStringListIterator&  aStartEntry         ,
195                                                             ::rtl::OUString&            sResult             ) const;
196 
197 		/*-****************************************************************************************************//**
198 			@short		get all properties of a cache entry by given name
199 			@descr		If you need additional informations about our internal cache values
200 						you can use these methods to get a list of all cached config values
201 						and subkeys of specified entry.
202 
203 			@seealso	-
204 
205 			@param      "sName", name of suspected entry in cache
206 			@return		A structure with valid information if item exists! An empty Any otherwise.
207 
208 			@onerror	We return an empty Any.
209 		*//*-*****************************************************************************************************/
210 
211         css::uno::Sequence< ::rtl::OUString >               getAllTypeNames                 () const;
212         css::uno::Sequence< ::rtl::OUString >               getAllFilterNames               () const;
213         css::uno::Sequence< ::rtl::OUString >               getAllDetectorNames             () const;   // without default detector!
214         css::uno::Sequence< ::rtl::OUString >               getAllLoaderNames               () const;   // without default loader!
215         css::uno::Sequence< ::rtl::OUString >               getAllContentHandlerNames       () const;
216         css::uno::Sequence< ::rtl::OUString >               getAllDetectorNamesWithDefault  () const;   // default detector is last one!
217         css::uno::Sequence< ::rtl::OUString >               getAllLoaderNamesWithDefault    () const;   // default loader is last one!
218         ::rtl::OUString                                     getDefaultLoader                () const;
219 
220         css::uno::Sequence< css::beans::PropertyValue >     getTypeProperties               (   const   ::rtl::OUString&    sName   ) const;
221         css::uno::Sequence< css::beans::PropertyValue >     getFilterProperties             (   const   ::rtl::OUString&    sName   ) const;
222         css::uno::Sequence< css::beans::PropertyValue >     getDetectorProperties           (   const   ::rtl::OUString&    sName   ) const;
223         css::uno::Sequence< css::beans::PropertyValue >     getLoaderProperties             (   const   ::rtl::OUString&    sName   ) const;
224         css::uno::Sequence< css::beans::PropertyValue >     getContentHandlerProperties     (   const   ::rtl::OUString&    sName   ) const;
225 
226         FileType                                            getType                         (   const   ::rtl::OUString&    sName   ) const;
227         Filter                                              getFilter                       (   const   ::rtl::OUString&    sName   ) const;
228         Detector                                            getDetector                     (   const   ::rtl::OUString&    sName   ) const;
229         Loader                                              getLoader                       (   const   ::rtl::OUString&    sName   ) const;
230         ContentHandler                                      getContentHandler               (   const   ::rtl::OUString&    sName   ) const;
231 
232         sal_Bool                                            existsType                      (   const   ::rtl::OUString&    sName   ) const;
233         sal_Bool                                            existsFilter                    (   const   ::rtl::OUString&    sName   ) const;
234         sal_Bool                                            existsDetector                  (   const   ::rtl::OUString&    sName   ) const;
235         sal_Bool                                            existsLoader                    (   const   ::rtl::OUString&    sName   ) const;
236         sal_Bool                                            existsContentHandler            (   const   ::rtl::OUString&    sName   ) const;
237 
238 		/*-****************************************************************************************************//**
239 			@short		support special query modes
240 			@descr		Our owner services need sometimes a special mode to query for subsets of our configuration!
241 						They give us a special query string - we return right values.
242 
243 			@seealso	file queries.h
244 			@seealso	class FilterFactory
245 			@seealso	class FrameLoaderFactory
246 			@seealso	class TypeDetection
247 
248 			@param      "sName", name of query
249 			@return		A structure with valid information!
250 
251 			@onerror	We return an empty result set.
252 		*//*-*****************************************************************************************************/
253 
254 		css::uno::Any queryFilters( const ::rtl::OUString& sQuery ) const;
255 
256 		/*-****************************************************************************************************//**
257 			@short		support registration of elements in current configuration
258 			@descr		Use this methods to add or remove items in our configuration files.
259 						We use the globale configuration to do that ... in fat office "share/config/registry/..."!
260 
261 						*** structure of type properties **********************************************************
262 
263 							PropertyValue.Name					PropertyValue.Value					Description
264 							---------------------------------------------------------------------------------------
265 							...
266 
267 						*** structure of filter properties ********************************************************
268 
269 							PropertyValue.Name					PropertyValue.Value					Description
270 							---------------------------------------------------------------------------------------
271 							"Name"								[string]							internal name
272 							"Type"								[string]							registered for these type
273 							"UIName"							[string]							localized name for UI (valid for current locale at runtime!)
274 							"UINames"							[stringlist]						assignment of all supported localized names to right locales
275 							"DocumentService"					[string]							uno servicename of document services
276 							"FilterService"						[string]							uno servicename of filter implementation
277 							"Flags"								[long]								describe filter
278 							"UserData"							[stringlist]						additional user data (format not fixed!)
279 							"FileFormatVersion"					[long]								version numbher of supported files
280 							"TemplateName"						[string]							name of template
281 
282 						*** structure of detector properties ******************************************************
283 
284 							PropertyValue.Name					PropertyValue.Value					Description
285 							---------------------------------------------------------------------------------------
286 							...
287 
288 						*** structure of loader properties ********************************************************
289 
290 							PropertyValue.Name					PropertyValue.Value					Description
291 							---------------------------------------------------------------------------------------
292 							...
293 
294 			@seealso	-
295 
296 			@param      "sName"			, name of type, filter ...
297 			@param      "lProperties"	, values of new type, filter
298 			@return		state of operation as bool
299 
300 			@onerror	We return false then.
301 		*//*-*****************************************************************************************************/
302 
303         sal_Bool addFilter    ( const ::rtl::OUString&                                 sName       ,
304                                 const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
305                                       sal_Bool                                         bException  ) throw(css::container::ElementExistException  ,
306                                                                                                            css::registry::InvalidRegistryException);
307         sal_Bool replaceFilter( const ::rtl::OUString&                                 sName       ,
308                                 const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
309                                       sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
310                                                                                                            css::registry::InvalidRegistryException);
311         sal_Bool removeFilter ( const ::rtl::OUString&                                 sName       ,
312                                       sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
313                                                                                                            css::registry::InvalidRegistryException);
314 
315         sal_Bool addType      ( const ::rtl::OUString&                                 sName       ,
316                                 const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
317                                       sal_Bool                                         bException  ) throw(css::container::ElementExistException  ,
318                                                                                                            css::registry::InvalidRegistryException);
319         sal_Bool replaceType  ( const ::rtl::OUString&                                 sName       ,
320                                 const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
321                                       sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
322                                                                                                            css::registry::InvalidRegistryException);
323         sal_Bool removeType   ( const ::rtl::OUString&                                 sName       ,
324                                       sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
325                                                                                                            css::registry::InvalidRegistryException);
326 
327         sal_Bool addDetector    ( const ::rtl::OUString&                                 sName       ,
328                                   const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
329                                         sal_Bool                                         bException  ) throw(css::container::ElementExistException  ,
330                                                                                                              css::registry::InvalidRegistryException);
331         sal_Bool replaceDetector( const ::rtl::OUString&                                 sName       ,
332                                   const css::uno::Sequence< css::beans::PropertyValue >& lProperties ,
333                                         sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
334                                                                                                              css::registry::InvalidRegistryException);
335         sal_Bool removeDetector ( const ::rtl::OUString&                                 sName       ,
336                                         sal_Bool                                         bException  ) throw(css::container::NoSuchElementException  ,
337                                                                                                              css::registry::InvalidRegistryException);
338 
339         sal_Bool validateAndRepair();
340         sal_Bool validateAndRepairTypes();
341         sal_Bool validateAndRepairFilter();
342         sal_Bool validateAndRepairDetectors();
343         sal_Bool validateAndRepairLoader();
344         sal_Bool validateAndRepairHandler();
345 
346 	//-------------------------------------------------------------------------------------------------------------
347 	//	protected methods
348 	//-------------------------------------------------------------------------------------------------------------
349 
350 	protected:
351 
352 	//-------------------------------------------------------------------------------------------------------------
353 	//	private methods
354 	//-------------------------------------------------------------------------------------------------------------
355 
356 	private:
357 
358 	//-------------------------------------------------------------------------------------------------------------
359 	//	debug methods
360 	//-------------------------------------------------------------------------------------------------------------
361 
362 		/*-****************************************************************************************************//**
363 			@short		debug-method to check incoming parameter of some other mehods of this class
364 			@descr		The following methods are used to check parameters for other methods
365 						of this class. The return value is used directly for an ASSERT(...).
366 
367 			@seealso	ASSERT in implementation!
368 
369 			@param		references to checking variables
370 			@return		sal_False ,on invalid parameter
371 			@return		sal_True  ,otherwise
372 
373 			@onerror	-
374 		*//*-*****************************************************************************************************/
375 
376 	#ifdef ENABLE_ASSERTIONS
377 
378 	private:
379 
380 		static sal_Bool implcp_searchType							(	const	::rtl::OUString&									sURL				,
381 																		const	::rtl::OUString*									pMediaType			,
382 																		const	::rtl::OUString*									pClipboardFormat	,
383 																		const	CheckedTypeIterator&								aStartEntry			,
384 																		const	::rtl::OUString&									sResult				);
385 		static sal_Bool implcp_searchFilterForType					(	const	::rtl::OUString&									sInternalTypeName	,
386 																		const	CheckedStringListIterator&							aStartEntry			,
387 																		const	::rtl::OUString&									sResult				);
388 		static sal_Bool implcp_searchDetectorForType				(	const	::rtl::OUString&									sInternalTypeName	,
389 																		const	CheckedStringListIterator&							aStartEntry			,
390 																		const	::rtl::OUString&									sResult				);
391 		static sal_Bool implcp_searchLoaderForType					(	const	::rtl::OUString&									sInternalTypeName	,
392 																		const	CheckedStringListIterator&							aStartEntry			,
393 																		const	::rtl::OUString&									sResult				);
394         static sal_Bool implcp_searchContentHandlerForType          (   const   ::rtl::OUString&                                    sInternalTypeName   ,
395 																		const	CheckedStringListIterator&							aStartEntry			,
396 																		const	::rtl::OUString&									sResult				);
397 		static sal_Bool implcp_getTypeProperties					(	const	::rtl::OUString&									sName				);
398 		static sal_Bool implcp_getFilterProperties					(	const	::rtl::OUString&									sName				);
399 		static sal_Bool implcp_getDetectorProperties				(	const	::rtl::OUString&									sName				);
400 		static sal_Bool implcp_getLoaderProperties					(	const	::rtl::OUString&									sName				);
401         static sal_Bool implcp_getContentHandlerProperties          (   const   ::rtl::OUString&                                    sName               );
402 		static sal_Bool implcp_getType								(	const	::rtl::OUString&									sName				);
403 		static sal_Bool implcp_getFilter							(	const	::rtl::OUString&									sName				);
404 		static sal_Bool implcp_getDetector							(	const	::rtl::OUString&									sName				);
405 		static sal_Bool implcp_getLoader							(	const	::rtl::OUString&									sName				);
406         static sal_Bool implcp_getContentHandler                    (   const   ::rtl::OUString&                                    sName               );
407 		static sal_Bool implcp_existsType							(	const	::rtl::OUString&									sName				);
408 		static sal_Bool implcp_existsFilter							(	const	::rtl::OUString&									sName				);
409 		static sal_Bool implcp_existsDetector						(	const	::rtl::OUString&									sName				);
410 		static sal_Bool implcp_existsLoader							(	const	::rtl::OUString&									sName				);
411         static sal_Bool implcp_existsContentHandler                 (   const   ::rtl::OUString&                                    sName               );
412 		static sal_Bool implcp_addFilter							(	const	::rtl::OUString&									sName				,
413 																		const	css::uno::Sequence< css::beans::PropertyValue >&	lProperties			);
414 		static sal_Bool implcp_replaceFilter						(	const	::rtl::OUString&									sName				,
415 																		const	css::uno::Sequence< css::beans::PropertyValue >&	lProperties			);
416 		static sal_Bool implcp_removeFilter							(	const	::rtl::OUString&									sName				);
417 		static sal_Bool implcp_queryFilters							(	const	::rtl::OUString&									sQuery				);
418 
419 	#endif	//	#ifdef ENABLE_ASSERTIONS
420 
421     #ifdef ENABLE_COMPONENT_SELF_CHECK
422 
423     private:
424 
425         void impldbg_dumpCache();
426 
427     #endif // ENABLE_COMPONENT_SELF_CHECK
428 
429 	//-------------------------------------------------------------------------------------------------------------
430 	//	private variables
431 	//-------------------------------------------------------------------------------------------------------------
432 	private:
433 
434         static sal_Int32        m_nRefCount         ;
435         static DataContainer*   m_pData             ;
436         static sal_Int32        m_nVersion          ;
437         static sal_Int16        m_nMode             ;
438 
439 };		//	class FilterCache
440 
441 }		//	namespace framework
442 
443 #endif	//	#ifndef __FRAMEWORK_CLASSES_FILTERCACHE_HXX_
444