1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FRAMEWORK_INC_CLASSES_FILTERCACHEDATA_HXX
21 #define INCLUDED_FRAMEWORK_INC_CLASSES_FILTERCACHEDATA_HXX
23 #include <classes/checkediterator.hxx>
24 #include <classes/wildcard.hxx>
25 #include <classes/converter.hxx>
26 #include <macros/xinterface.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/util/XChangesListener.hpp>
35 #include <com/sun/star/util/XChangesNotifier.hpp>
37 #include <unotools/configitem.hxx>
38 #include <cppuhelper/weak.hxx>
39 #include <rtl/ustring.hxx>
42 #include <unordered_map>
47 #define DEFAULT_FILTERCACHE_VERSION 6 /// these implmentation of FilterCache support different version of TypeDetection.xml! This define the current set default one.
49 #define DEFAULT_FILTERCACHE_MODE ConfigItemMode::DelayedUpdate | ConfigItemMode::AllLocales /// ConfigItems could run in different modes: supported values are ... { ConfigItemMode::ImmediateUpdate, ConfigItemMode::DelayedUpdate, ConfigItemMode::AllLocales }
65 // These struct define a type, which present the type of a file.
66 // He is used for easy filter detection without file stream detection!
67 // The internal name is the keyname of an item with these structure in our hash map or our configuration set!
76 inline FileType ( ) { impl_clear(); }
77 inline FileType ( const FileType
& rCopy
) { impl_copy( rCopy
); }
78 inline ~FileType ( ) { impl_clear(); }
79 inline FileType
& operator= ( const FileType
& rCopy
) { return impl_copy( rCopy
); }
80 inline void free ( ) { impl_clear(); }
85 inline void impl_clear()
90 sClipboardFormat
.clear();
93 framework::free(lURLPattern
);
94 framework::free(lExtensions
);
97 inline FileType
& impl_copy( const FileType
& rCopy
)
99 bPreferred
= rCopy
.bPreferred
;
101 lUINames
= rCopy
.lUINames
;
102 sMediaType
= rCopy
.sMediaType
;
103 sClipboardFormat
= rCopy
.sClipboardFormat
;
104 nDocumentIconID
= rCopy
.nDocumentIconID
;
105 lURLPattern
= rCopy
.lURLPattern
;
106 lExtensions
= rCopy
.lExtensions
;
116 OUStringHashMap lUINames
;
118 OUString sClipboardFormat
;
119 sal_Int32 nDocumentIconID
;
120 OUStringList lURLPattern
;
121 OUStringList lExtensions
;
124 // These struct describe a filter which is registered for one type.
125 // He hold information about services which present the document himself (like a item) and a filter service which
126 // filter a file in these document.
127 // The internal name is the keyname of an item with these structure in our hash map or our configuration set!
136 inline Filter ( ) { impl_clear(); }
137 inline Filter ( const Filter
& rCopy
) { impl_copy( rCopy
); }
138 inline ~Filter ( ) { impl_clear(); }
139 inline Filter
& operator= ( const Filter
& rCopy
) { return impl_copy( rCopy
); }
140 inline void free ( ) { impl_clear(); }
145 inline void impl_clear()
150 sDocumentService
.clear();
151 sFilterService
.clear();
152 sUIComponent
.clear();
154 nFileFormatVersion
= 0;
155 sTemplateName
.clear();
157 framework::free(lUserData
);
160 inline Filter
& impl_copy( const Filter
& rCopy
)
162 nOrder
= rCopy
.nOrder
;
165 lUINames
= rCopy
.lUINames
;
166 sDocumentService
= rCopy
.sDocumentService
;
167 sFilterService
= rCopy
.sFilterService
;
168 sUIComponent
= rCopy
.sUIComponent
;
169 nFlags
= rCopy
.nFlags
;
170 nFileFormatVersion
= rCopy
.nFileFormatVersion
;
171 sTemplateName
= rCopy
.sTemplateName
;
172 lUserData
= rCopy
.lUserData
;
183 OUStringHashMap lUINames
;
184 OUString sDocumentService
;
185 OUString sFilterService
;
186 OUString sUIComponent
;
188 OUStringList lUserData
;
189 sal_Int32 nFileFormatVersion
;
190 OUString sTemplateName
;
193 // Programmer can register his own services for an content detection of different types.
194 // The implementation or service name of these is the keyname of an item with these structure
195 // in our hash map or our configuration set!
204 inline Detector ( ) { impl_clear(); }
205 inline Detector ( const Detector
& rCopy
) { impl_copy( rCopy
); }
206 inline ~Detector ( ) { impl_clear(); }
207 inline Detector
& operator= ( const Detector
& rCopy
) { return impl_copy( rCopy
); }
208 inline void free ( ) { impl_clear(); }
213 inline void impl_clear()
216 framework::free(lTypes
);
219 inline Detector
& impl_copy( const Detector
& rCopy
)
222 lTypes
= rCopy
.lTypes
;
234 // Programmer can register his own services for loading documents in a frame.
235 // The implementation or service name of these is the keyname of an item with these structure
236 // in our hash map or our configuration set!
245 inline Loader ( ) { impl_clear(); }
246 inline Loader ( const Loader
& rCopy
) { impl_copy( rCopy
); }
247 inline ~Loader ( ) { impl_clear(); }
248 inline Loader
& operator= ( const Loader
& rCopy
) { return impl_copy( rCopy
); }
249 inline void free ( ) { impl_clear(); }
254 inline void impl_clear()
258 framework::free(lTypes
);
261 inline Loader
& impl_copy( const Loader
& rCopy
)
264 lUINames
= rCopy
.lUINames
;
265 lTypes
= rCopy
.lTypes
;
274 OUStringHashMap lUINames
;
278 // Programmer can register his own services to handle a FileType and intercept dispatches.
279 // Don't forget: It's not a FrameLoader - it's a ContentHandler! (normally without any UI)
281 struct ContentHandler
288 inline ContentHandler( ) { impl_clear(); }
289 inline ContentHandler( const ContentHandler
& rCopy
) { impl_copy( rCopy
); }
290 inline ~ContentHandler( ) { impl_clear(); }
291 inline ContentHandler
& operator= ( const ContentHandler
& rCopy
) { return impl_copy( rCopy
); }
292 inline void free ( ) { impl_clear(); }
297 inline void impl_clear()
300 framework::free(lTypes
);
303 inline ContentHandler
& impl_copy( const ContentHandler
& rCopy
)
306 lTypes
= rCopy
.lTypes
;
318 // We need different hash maps for different tables of our configuration management.
319 // Follow maps convert <names> to <properties> of type, filter, detector, loader ...
320 // and could be used in a generic way
322 template< class HashType
>
323 class SetNodeHash
: public std::unordered_map
< OUString
,
326 std::equal_to
< OUString
> >
333 // The only way to free ALL memory really!
337 SetNodeHash().swap( *this ); // get rid of reserved capacity
338 framework::free(lAddedItems
);
339 framework::free(lChangedItems
);
340 framework::free(lRemovedItems
);
343 // Append changed, added or removed items to special lists
344 // Necessary for saving changes
346 void appendChange( const OUString
& sName
,
347 EModifyState eState
);
352 OUStringList lAddedItems
;
353 OUStringList lChangedItems
;
354 OUStringList lRemovedItems
;
357 // Use these hashes to implement different tables which assign types to frame loader or detect services.
358 // It's an optimism to find registered services faster!
359 // The preferred hash maps file extensions to preferred types to find these ones faster.
361 class PerformanceHash
: public std::unordered_map
< OUString
,
364 std::equal_to
< OUString
> >
368 // try to free all used memory REALLY!
372 PerformanceHash().swap( *this ); // get rid of reserved capacity
375 // normally a complete string must match our hash key values ...
376 // But sometimes we need a search by using these key values as pattern!
377 // The in/out parameter "pStepper" is used to return a pointer to found element in hash ...
378 // and could be used for further searches again, which should be started at next element!
379 // We stop search at the end of hash. You can start it again by setting it to the begin by himself.
381 inline bool findPatternKey( const OUString
& sSearchValue
,
382 const_iterator
& pStepper
)
386 // If this is the forst call - start search on first element.
387 // Otherwise start search on further elements!
388 if( pStepper
!= begin() )
393 while( pStepper
!= end() && !bFound
)
395 bFound
= Wildcard::match( sSearchValue
, pStepper
->first
);
396 // If element was found - break loop by setting right return value
397 // and don't change "pStepper". He must point to found element!
398 // Otherwise step to next one.
406 // Define easy usable types
408 typedef SetNodeHash
< FileType
> FileTypeHash
;
409 typedef SetNodeHash
< Filter
> FilterHash
;
410 typedef SetNodeHash
< Detector
> DetectorHash
;
411 typedef SetNodeHash
< Loader
> LoaderHash
;
412 typedef SetNodeHash
< ContentHandler
> ContentHandlerHash
;
413 typedef OUStringHashMap PreferredHash
;
414 typedef OUStringList OrderList
;
416 typedef CheckedIterator
< OUStringList
> CheckedStringListIterator
;
417 typedef CheckedIterator
< FileTypeHash
> CheckedTypeIterator
;
420 // Use private static data container to hold all values of configuration!
426 /** @short identifies different sets of the TypeDetection configuration package.
428 @descr Most functions on top of this configuration package are the same ...
429 but must be executed on different places inside this configuration structures.
430 These enum values can be used ate some interface methods to specify, which
431 configuration set should be used.
432 Further it must be possible to start the same action for more than one cfg type.
433 That's why these values must be interpreted as flags. Means: its values must be
442 E_CONTENTHANDLER
= 16,
444 E_ALL
= E_TYPE
| E_FILTER
| E_DETECTSERVICE
| E_FRAMELOADER
| E_CONTENTHANDLER
451 void startListener();
452 void stopListener ();
458 bool isValidOrRepairable () const;
459 bool validateAndRepair ();
460 bool validateAndRepairTypes ();
461 bool validateAndRepairFilter ();
462 bool validateAndRepairDetectors();
463 bool validateAndRepairLoader ();
464 bool validateAndRepairHandler ();
466 bool existsType ( const OUString
& sName
);
467 bool existsFilter ( const OUString
& sName
);
468 bool existsDetector ( const OUString
& sName
);
469 bool existsLoader ( const OUString
& sName
);
470 bool existsContentHandler ( const OUString
& sName
);
472 void addType ( const FileType
& aType
, bool bSetModified
);
473 void addFilter ( const Filter
& aFilter
, bool bSetModified
);
474 void addDetector ( const Detector
& aDetector
, bool bSetModified
);
475 void addLoader ( const Loader
& aLoader
, bool bSetModified
);
476 void addContentHandler ( const ContentHandler
& aHandler
, bool bSetModified
);
478 void replaceType ( const FileType
& aType
, bool bSetModified
);
479 void replaceFilter ( const Filter
& aFilter
, bool bSetModified
);
480 void replaceDetector ( const Detector
& aDetector
, bool bSetModified
);
481 void replaceLoader ( const Loader
& aLoader
, bool bSetModified
);
482 void replaceContentHandler( const ContentHandler
& aHandler
, bool bSetModified
);
484 void removeType ( const OUString
& sName
, bool bSetModified
);
485 void removeFilter ( const OUString
& sName
, bool bSetModified
);
486 void removeDetector ( const OUString
& sName
, bool bSetModified
);
487 void removeLoader ( const OUString
& sName
, bool bSetModified
);
488 void removeContentHandler ( const OUString
& sName
, bool bSetModified
);
490 static void convertFileTypeToPropertySequence ( const FileType
& aSource
,
491 css::uno::Sequence
< css::beans::PropertyValue
>& lDestination
,
492 const OUString
& sCurrentLocale
);
493 static void convertFilterToPropertySequence ( const Filter
& aSource
,
494 css::uno::Sequence
< css::beans::PropertyValue
>& lDestination
,
495 const OUString
& sCurrentLocale
);
496 static void convertDetectorToPropertySequence ( const Detector
& aSource
,
497 css::uno::Sequence
< css::beans::PropertyValue
>& lDestination
);
498 static void convertLoaderToPropertySequence ( const Loader
& aSource
,
499 css::uno::Sequence
< css::beans::PropertyValue
>& lDestination
,
500 const OUString
& sCurrentLocale
);
501 static void convertContentHandlerToPropertySequence ( const ContentHandler
& aSource
,
502 css::uno::Sequence
< css::beans::PropertyValue
>& lDestination
);
503 static void convertPropertySequenceToFilter ( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
,
504 Filter
& aDestination
,
505 const OUString
& sCurrentLocale
);
506 static void convertPropertySequenceToFileType ( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
,
507 FileType
& aDestination
,
508 const OUString
& sCurrentLocale
);
509 static void convertPropertySequenceToDetector ( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
,
510 Detector
& aDestination
);
511 static void convertPropertySequenceToLoader ( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
,
512 Loader
& aDestination
,
513 const OUString
& sCurrentLocale
);
514 static void convertPropertySequenceToContentHandler ( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
,
515 ContentHandler
& aDestination
,
516 const OUString
& sCurrentLocale
);
517 static void extractLocalizedStrings ( const OUString
& sCurrentLocale
,
518 const css::uno::Any
& aCFGValue
,
519 OUStringHashMap
& lLocales
);
520 static void packLocalizedStrings ( sal_Int16 nMode
,
521 const OUString
& sCurrentLocale
,
522 css::uno::Any
& aCFGValue
,
523 const OUStringHashMap
& lLocales
);
524 static OUString
getLocalelizedString ( const OUStringHashMap
& lLocales
,
525 const OUString
& sLocale
);
526 static void setLocalelizedString ( OUStringHashMap
& lLocales
,
527 const OUString
& sLocale
,
528 const OUString
& sValue
);
529 static void correctExtensions ( OUStringList
& lExtensions
);
533 FileTypeHash m_aTypeCache
; /// hold all information about registered file types
534 FilterHash m_aFilterCache
; /// hold all information about registered filters
535 DetectorHash m_aDetectorCache
; /// hold all information about registered detect services
536 LoaderHash m_aLoaderCache
; /// hold all information about registered loader services
537 ContentHandlerHash m_aContentHandlerCache
; /// hold all information about registered content handler services
538 PerformanceHash m_aFastFilterCache
; /// hold all registered filter for a special file type
539 PerformanceHash m_aFastDetectorCache
; /// hold all registered detect services for a special file type
540 PerformanceHash m_aFastLoaderCache
; /// hold all registered loader services for a special file type
541 PerformanceHash m_aFastContentHandlerCache
; /// hold all registered content handler services for a special file type
542 PreferredHash m_aPreferredTypesCache
; /// assignment of extensions to preferred types for it
543 Loader m_aGenericLoader
; /// information about our default frame loader
544 OUString m_sLocale
; /// current set locale of configuration to handle right UIName from set of all UINames!
545 bool m_bTypesModified
;
546 bool m_bFiltersModified
;
547 bool m_bDetectorsModified
;
548 bool m_bLoadersModified
;
549 bool m_bHandlersModified
;
552 /*-************************************************************************************************************
553 @short capsulate configuration access for filter configuration
554 @descr We use the ConfigItem mechanism to read/write values from/to configuration.
555 This implementation could be used to handle standard AND additional filter configurations in the same way.
556 We set a data container pointer for filling or reading ... this class use it temp.
557 After successfully calling of read(), we can use filled container directly or merge it with an existing one.
558 After successfully calling of write() all values of given data container are flushed to our configuration.
561 @devstatus ready to use
563 *//*-*************************************************************************************************************/
564 class FilterCFGAccess
: public ::utl::ConfigItem
570 FilterCFGAccess ( const OUString
& sPath
,
571 sal_Int32 nVersion
= DEFAULT_FILTERCACHE_VERSION
,
572 ConfigItemMode nMode
= DEFAULT_FILTERCACHE_MODE
); // open configuration
573 virtual ~FilterCFGAccess( );
575 void read ( DataContainer
& rData
,
576 DataContainer::ECFGType eType
); // read values from configuration into given struct
577 void write ( DataContainer
& rData
,
578 DataContainer::ECFGType eType
); // write values from given struct to configuration
580 static OUString
encodeTypeData ( const FileType
& aType
); // build own formatted string of type properties
581 static void decodeTypeData ( const OUString
& sData
,
583 static OUString
encodeFilterData( const Filter
& aFilter
); // build own formatted string of filter properties
584 static void decodeFilterData( const OUString
& sData
,
586 static OUString
encodeStringList( const OUStringList
& lList
); // build own formatted string of OUStringList
587 static OUStringList
decodeStringList( const OUString
& sValue
);
589 void setProductName ( OUStringHashMap
& lUINames
);
590 void resetProductName ( OUStringHashMap
& lUINames
);
595 void impl_initKeyCounts ( ); // set right key counts, which are used at reading/writing of set node properties
596 void impl_removeNodes ( OUStringList
& rChangesList
, // helper to remove list of set nodes
597 const OUString
& sTemplateType
,
598 const OUString
& sSetName
);
600 void impl_loadTypes ( DataContainer
& rData
); // helper to load configuration parts
601 void impl_loadFilters ( DataContainer
& rData
);
602 void impl_loadDetectors ( DataContainer
& rData
);
603 void impl_loadLoaders ( DataContainer
& rData
);
604 void impl_loadContentHandlers ( DataContainer
& rData
);
605 void impl_loadDefaults ( DataContainer
& rData
);
607 void impl_saveTypes ( DataContainer
& rData
); // helper to save configuration parts
608 void impl_saveFilters ( DataContainer
& rData
);
609 void impl_saveDetectors ( DataContainer
& rData
);
610 void impl_saveLoaders ( DataContainer
& rData
);
611 void impl_saveContentHandlers ( DataContainer
& rData
);
616 static bool implcp_ctor ( const OUString
& sPath
, // methods to check incoming parameter on our interface methods!
619 static bool implcp_read ( const DataContainer
& rData
);
620 static bool implcp_write( const DataContainer
& rData
);
625 EFilterPackage m_ePackage
; // ... not really used yet! should split configuration in STANDARD and ADDITIONAL filter
626 sal_Int32 m_nVersion
; // file format version of configuration! (necessary for "xml2xcd" transformation!)
627 sal_Int32 m_nKeyCountTypes
; // follow key counts present count of configuration properties for types/filters ... and depends from m_nVersion - must be set right!
628 sal_Int32 m_nKeyCountFilters
;
629 sal_Int32 m_nKeyCountDetectors
;
630 sal_Int32 m_nKeyCountLoaders
;
631 sal_Int32 m_nKeyCountContentHandlers
;
632 OUString m_sProductName
;
633 OUString m_sFormatVersion
;
636 } // namespace framework
638 #endif // INCLUDED_FRAMEWORK_INC_CLASSES_FILTERCACHEDATA_HXX
640 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */