xref: /AOO42X/main/filter/source/config/cache/typedetection.hxx (revision a20a167bf92e72b6a7380d695bf6b310032dae91)
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 #ifndef __FILTER_CONFIG_TYPEDETECTION_HXX_
23 #define __FILTER_CONFIG_TYPEDETECTION_HXX_
24 
25 //_______________________________________________
26 // includes
27 
28 #include "basecontainer.hxx"
29 #include <com/sun/star/document/XTypeDetection.hpp>
30 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include <comphelper/mediadescriptor.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 
34 //_______________________________________________
35 // namespace
36 
37 namespace filter{
38     namespace config{
39 
40 namespace css = ::com::sun::star;
41 
42 //_______________________________________________
43 // definitions
44 
45 //_______________________________________________
46 
47 /** @short      implements the service <type scope="com.sun.star.document">TypeDetection</type>.
48  */
49 class TypeDetection : public ::cppu::ImplInheritanceHelper1< BaseContainer                 ,
50                                                              css::document::XTypeDetection >
51 {
52     //-------------------------------------------
53     // native interface
54 
55     public:
56 
57         //---------------------------------------
58         // ctor/dtor
59 
60         /** @short  standard ctor to connect this interface wrapper to
61                     the global filter cache instance ...
62 
63             @param  xSMGR
64                     reference to the uno service manager, which created this service instance.
65          */
66         TypeDetection(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
67 
68         //---------------------------------------
69 
70         /** @short  standard dtor.
71          */
72         virtual ~TypeDetection();
73 
74     //-------------------------------------------
75     // private helper
76 
77     private:
78 
79         //---------------------------------------
80         /** TODO document me */
81         sal_Bool impl_getPreselectionForType(const ::rtl::OUString& sPreSelType,
82                                              const css::util::URL&  aParsedURL ,
83                                                    FlatDetection&   rFlatTypes );
84 
85         //---------------------------------------
86         /** TODO document me */
87         sal_Bool impl_getPreselectionForFilter(const ::rtl::OUString& sPreSelFilter,
88                                                const css::util::URL&  aParsedURL   ,
89                                                      FlatDetection&   rFlatTypes   );
90 
91         //---------------------------------------
92         /** TODO document me */
93         sal_Bool impl_getPreselectionForDocumentService(const ::rtl::OUString& sPreSelDocumentService,
94                                                          const css::util::URL& aParsedURL            ,
95                                                                FlatDetection&  rFlatTypes            );
96 
97         //---------------------------------------
98 
99         /** @short      check if a filter or a type was preselected inside the given
100                         MediaDescriptor and validate this information.
101 
102             @descr      Only in case the preselected filter exists and its type registration
103                         seems to be usefully, it would be used really as valid type detection
104                         result. This method doesn't make any deep detection here. It checks only
105                         if the preselection match to the URL by an URLPattern.
106                         This information has to be added to the given rFlatTypes list too.
107                         The outside code can use it to suppress a deep detection then in general.
108                         Because pattern are defined as non detectable at all!
109 
110             @param      pDescriptor
111                         provides any easy-to-use stl interface to the MediaDescriptor.
112                         Note : Its content will be adapted to returned result of this method.
113                         Means: The type/filter entries of it will be actualized or removed.
114 
115             @param      rFlatTypes
116                         the preselected type (or the registered type of a preselected filter)
117                         will be added here as first(!) element. Further we have to provide the
118                         information, if this type match to the given URL by its URLPattern
119                         registration.
120          */
121         void impl_getPreselection(const css::util::URL&                aParsedURL ,
122                                         ::comphelper::MediaDescriptor& rDescriptor,
123                                         FlatDetection&                 rFlatTypes );
124 
125         //---------------------------------------
126 
127         /** @short      make a combined flat/deep type detection
128 
129             @descr      It steps over all flat detected types (given by the parameter lFlatTypes),
130                         try it and search for most suitable one.
131                         The specified MediaDescriptor will be patched, so it contains
132                         the right values every time. Using of any deep detection service
133                         can be enabled/disabled. And last but not least: If the results
134                         won't be really clear (because a flat detected type has no deep
135                         detection service), a "suggested" type name will be returned as "rLastChance".
136                         It can be used after e.g. all well known deep detection services
137                         was used without getting any result. Then this "last-chance-type"
138                         should be returned. Of course using of it can fail too ... but it's a try :-)
139 
140                         As an optimization - this method collects the names of all used deep
141                         detection services. This information can be useful inside the may be
142                         afterwards called method "impl_detectTypeDeepOnly()"!
143 
144             @param      rDescriptor
145                         provides an easy-to-use stl interface to the MediaDescriptor.
146                         Note : Its content will be adapted to returned result of this method.
147                         Means: The type/filter entries of it will be actualized or removed from it.
148 
149             @param      lFlatTypes
150                         a list of all flat detected types, which should be checked here.
151                         No other types are allowed here!
152 
153             @param      rLastChance
154                         the internal name of a "suggested type" ... (see before)
155                         Note: it will be reset to an empty string every time. So
156                         a set value of "rLastChance" can be detected outside very easy.
157 
158             @param      rUsedDetectors
159                         used as [out] parameter. It contains a list of names of all deep
160                         detection services, which was used inside this method.
161                         Such detectors can be ignored later if "impl_detectTypeDeepOnly()"
162                         is called.
163 
164             @param      bAllowDeep
165                         enable/disable using of a might existing deep detection service.
166 
167             @return     The internal name of a detected type.
168                         An empty value if detection failed. .... but see rLastChance
169                         for additional returns!
170          */
171         ::rtl::OUString impl_detectTypeFlatAndDeep(      ::comphelper::MediaDescriptor& rDescriptor   ,
172                                                    const FlatDetection&                 lFlatTypes    ,
173                                                          sal_Bool                       bAllowDeep    ,
174                                                          OUStringList&                  rUsedDetectors,
175                                                          ::rtl::OUString&               rLastChance   );
176 
177         //---------------------------------------
178 
179         /** @short      make a deep type detection only
180 
181             @descr      It steps over all well known deep detection services
182                         and check her results. The first positive result will be
183                         used for return. Its more a "try and error" algorithm then
184                         a real type detection and will be used if a flat detection
185                         can't work really ... e.g. if the extension of an URL is
186                         missing or wrong.
187 
188             @param      rDescriptor
189                         provides any easy-to-use stl interface to the MediaDescriptor.
190                         Note : Its content will be adapted to returned result of this method.
191                         Means: The type/filter entries of it will be actualized or removed from it.
192 
193             @param      rUsedDetectors
194                         It contains a list of names of all deep detection services,
195                         which was already used inside the method "impl_detectTypeFlatAndDeep()"!
196                         Such detectors must be ignored here!
197 
198             @return     The internal name of a detected type.
199                         An empty value if detection failed.
200          */
201         ::rtl::OUString impl_detectTypeDeepOnly(      ::comphelper::MediaDescriptor& rDescriptor   ,
202                                                 const OUStringList&                  rUsedDetectors);
203 
204         //---------------------------------------
205 
206         /** @short      seek a might existing stream to position 0.
207 
208             @descr      This is an optional action to be more robust
209                         in case any detect service doesn't make this seek ...
210                         Normally it's part of any called detect service or filter ...
211                         but sometimes it's not done there.
212 
213             @param      rDescriptor
214                         a stl representation of the MediaDescriptor as in/out parameter.
215          */
216         void impl_seekStreamToZero(comphelper::MediaDescriptor& rDescriptor);
217 
218         //---------------------------------------
219 
220         /** @short      make deep type detection for a specified
221                         detect service (threadsafe!).
222 
223             @descr      It creates the right uno service, prepare the
224                         needed MediaDescriptor, call this right interfaces,
225                         and return the results.
226 
227             @attention  The results (means type and corresponding filter)
228                         are already part of the in/out parameter pDescriptor.
229                         (in case they was valid).
230 
231             @param      sDetectService
232                         uno service name of the detect service.
233 
234             @param      rDescriptor
235                         a stl representation of the MediaDescriptor as in/out parameter.
236          */
237         ::rtl::OUString impl_askDetectService(const ::rtl::OUString&               sDetectService,
238                                                     ::comphelper::MediaDescriptor& rDescriptor   );
239 
240         //---------------------------------------
241 
242         /** @short      try to find an interaction handler and
243                         ask him to select a possible filter for
244                         this unknown format.
245 
246             @descr      If the user select a filter, it will be used as return value
247                         without further checking against the given file content!
248 
249             @param      rDescriptor
250                         a stl representation of the MediaDescriptor as in/out parameter.
251 
252             @return     [string]
253                         a valid type name or an empty string if user canceled interaction.
254          */
255         ::rtl::OUString impl_askUserForTypeAndFilterIfAllowed(::comphelper::MediaDescriptor& rDescriptor);
256 
257         //---------------------------------------
258 
259         /** @short      check if an input stream is already part of the
260                         given MediaDesciptor and creates a new one if necessary.
261 
262             @attention  This method does further something special!
263                         <ul>
264                             <li>
265                                 If the given URL seem to be a streamable content, but creation of the stream
266                                 failed (might by an IOException), this method throws an exception.
267                                 (May be an existing interaction handler must be called here too ...)
268                                 The whole detection must be interrupted then and the interface method queryTypeByDescriptor()
269                                 must return an empty type name value.
270 
271                                 That prevent us against multiple handling of the same error more then ones
272                                 (e.g. if we ask all detect services as fallback ...).
273                             </li>
274                             <li>
275                                 In case the stream already exists inside the descriptor this method does nothing.
276                             </li>
277                             <li>
278                                 In case the stream does not exists but can be created successfully, the stream will
279                                 be added to the descriptor.
280                             </li>
281                         </ul>
282 
283             @param      rDescriptor
284                         provides any easy-to-use stl interface to the MediaDescriptor.
285                         Note : Its content will be adapted to returned result of this method.
286                         Means: The stream will be added to it.
287 
288             @throw      Any suitable exception if stream should be opened but operation was not successful.
289                         Note: If an interactionHandler is part of the given descriptor too, it was already used.
290                         Means: let the exception pass through the top most interface method!
291          */
292         void impl_openStream(::comphelper::MediaDescriptor& rDescriptor)
293             throw (css::uno::Exception);
294 
295         //---------------------------------------
296 
297         /** @short      validate the specified type and its relation ships
298                         and set all needed information related to this type
299                         in the specified descriptor.
300 
301             @descr      Related informations are: - corresponding filter
302                                                   - media type
303                                                   - ...
304 
305             @param      rDescriptor
306                         provides access to the outside MediaDescriptor.
307 
308             @param      sType
309                         the name of the type, which should be set on the descriptor.
310                         Can be empty to remove any related value from the descriptor!
311 
312             @return     TRUE the specified type and its registrations was valid(!) and
313                         could be set on the descriptor.
314          */
315         sal_Bool impl_validateAndSetTypeOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
316                                                      const ::rtl::OUString&               sType      );
317 
318         //---------------------------------------
319 
320         /** @short      validate the specified filter and its relation ships
321                         and set all needed information related to this filter
322                         in the specified descriptor.
323 
324             @descr      Related informations are: - corresponding type
325                                                   - ...
326 
327             @param      rDescriptor
328                         provides access to the outside MediaDescriptor.
329 
330             @param      sFilter
331                         the name of the filter, which should be set on the descriptor.
332                         Can be empty to remove any related value from the descriptor!
333 
334             @return     TRUE the specified type and its registrations was valid(!) and
335                         could be set on the descriptor.
336          */
337         sal_Bool impl_validateAndSetFilterOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
338                                                        const ::rtl::OUString&               sFilter    );
339 
340         //---------------------------------------
341 
342         /** @short      remove anythimng related to a TYPE/FILTER entry from the
343                         specified MediaDescriptor.
344 
345             @descr      This method works together with impl_validateAndSetTypeOnDescriptor()/
346                         impl_validateAndSetFilterOnDescriptor(). All informations, which can be
347                         set by these two operations must be "removeable" by this method.
348 
349             @param      rDescriptor
350                         reference to the MediaDescriptor (represented by an easy-to-use
351                         stl interface!), which should be patched.
352          */
353         void impl_removeTypeFilterFromDescriptor(::comphelper::MediaDescriptor& rDescriptor);
354 
355         //---------------------------------------
356 
357         /** @short      search the best suitable filter for the given type
358                         and add it into the media descriptor.
359 
360             @descr      Normally this is a type detection only ...
361                         but for some special features we must overwrite our detection
362                         because a file must be loaded into a special (means preselected)
363                         application.
364 
365                         E.g. CSV/TXT format are sometimes ugly to handle .-)
366 
367                         Note: If the descriptor already include a filter
368                         (may be selected by a FilterSelect interaction or preselected
369                         by the user itself) ... we dont change that here !
370 
371             @param      rDescriptor
372                         reference to the MediaDescriptor (represented by an easy-to-use
373                         stl interface!), which should be patched.
374 
375             @param      sType
376                         the internal type name, where we search a filter for.
377                         Used as IN/OUT parameter so we can overrule the detection result for
378                         types too !
379 
380             @note       #i60158#
381                         sometimes our text ascii and our csv filter can't work together.
382                         Then we overwrite our detection hardly.
383                         sType param is used as out parameter then too ... and
384                         rDescriptor will be changed by selecting another filter.
385                         (see code)
386          */
387         void impl_checkResultsAndAddBestFilter(::comphelper::MediaDescriptor& rDescriptor,
388                                                ::rtl::OUString&               sType      );
389 
390     //-------------------------------------------
391     // uno interface
392 
393     public:
394 
395         //---------------------------------------
396         // XTypeDetection
397 
398         virtual ::rtl::OUString SAL_CALL queryTypeByURL(const ::rtl::OUString& sURL)
399             throw (css::uno::RuntimeException);
400 
401         virtual ::rtl::OUString SAL_CALL queryTypeByDescriptor(css::uno::Sequence< css::beans::PropertyValue >& lDescriptor,
402                                                                sal_Bool                                         bAllowDeep )
403             throw (css::uno::RuntimeException);
404 
405     //-------------------------------------------
406     // static uno helper!
407 
408     public:
409 
410         //---------------------------------------
411 
412         /** @short  return the uno implementation name of this class.
413 
414             @descr  Because this information is used at several places
415                     (and mostly an object instance of this class is not possible)
416                     its implemented as a static function!
417 
418             @return The fix uno implementation name of this class.
419          */
420         static ::rtl::OUString impl_getImplementationName();
421 
422         //---------------------------------------
423 
424         /** @short  return the list of supported uno services of this class.
425 
426             @descr  Because this information is used at several places
427                     (and mostly an object instance of this class is not possible)
428                     its implemented as a static function!
429 
430             @return The fix list of uno services supported by this class.
431          */
432         static css::uno::Sequence< ::rtl::OUString > impl_getSupportedServiceNames();
433 
434         //---------------------------------------
435 
436         /** @short  return a new intsnace of this class.
437 
438             @descr  This method is used by the uno service manager, to create
439                     a new instance of this service if needed.
440 
441             @param  xSMGR
442                     reference to the uno service manager, which requires
443                     this new instance. It should be passed to the new object
444                     so it can be used internally to create own needed uno resources.
445 
446             @return The new instance of this service as an uno reference.
447          */
448         static css::uno::Reference< css::uno::XInterface > impl_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
449 };
450 
451     } // namespace config
452 } // namespace filter
453 
454 #endif // __FILTER_CONFIG_TYPEDETECTION_HXX_
455 
456 /* vim: set noet sw=4 ts=4: */
457