xref: /trunk/main/filter/source/config/cache/typedetection.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
294         //---------------------------------------
295 
296         /** @short      validate the specified type and its relation ships
297                         and set all needed information related to this type
298                         in the specified descriptor.
299 
300             @descr      Related informations are: - corresponding filter
301                                                   - media type
302                                                   - ...
303 
304             @param      rDescriptor
305                         provides access to the outside MediaDescriptor.
306 
307             @param      sType
308                         the name of the type, which should be set on the descriptor.
309                         Can be empty to remove any related value from the descriptor!
310 
311             @return     TRUE the specified type and its registrations was valid(!) and
312                         could be set on the descriptor.
313          */
314         sal_Bool impl_validateAndSetTypeOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
315                                                      const ::rtl::OUString&               sType      );
316 
317         //---------------------------------------
318 
319         /** @short      validate the specified filter and its relation ships
320                         and set all needed information related to this filter
321                         in the specified descriptor.
322 
323             @descr      Related informations are: - corresponding type
324                                                   - ...
325 
326             @param      rDescriptor
327                         provides access to the outside MediaDescriptor.
328 
329             @param      sFilter
330                         the name of the filter, which should be set on the descriptor.
331                         Can be empty to remove any related value from the descriptor!
332 
333             @return     TRUE the specified type and its registrations was valid(!) and
334                         could be set on the descriptor.
335          */
336         sal_Bool impl_validateAndSetFilterOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
337                                                        const ::rtl::OUString&               sFilter    );
338 
339         //---------------------------------------
340 
341         /** @short      remove anythimng related to a TYPE/FILTER entry from the
342                         specified MediaDescriptor.
343 
344             @descr      This method works together with impl_validateAndSetTypeOnDescriptor()/
345                         impl_validateAndSetFilterOnDescriptor(). All informations, which can be
346                         set by these two operations must be "removeable" by this method.
347 
348             @param      rDescriptor
349                         reference to the MediaDescriptor (represented by an easy-to-use
350                         stl interface!), which should be patched.
351          */
352         void impl_removeTypeFilterFromDescriptor(::comphelper::MediaDescriptor& rDescriptor);
353 
354         //---------------------------------------
355 
356         /** @short      search the best suitable filter for the given type
357                         and add it into the media descriptor.
358 
359             @descr      Normally this is a type detection only ...
360                         but for some special features we must overwrite our detection
361                         because a file must be loaded into a special (means preselected)
362                         application.
363 
364                         E.g. CSV/TXT format are sometimes ugly to handle .-)
365 
366                         Note: If the descriptor already include a filter
367                         (may be selected by a FilterSelect interaction or preselected
368                         by the user itself) ... we dont change that here !
369 
370             @param      rDescriptor
371                         reference to the MediaDescriptor (represented by an easy-to-use
372                         stl interface!), which should be patched.
373 
374             @param      sType
375                         the internal type name, where we search a filter for.
376                         Used as IN/OUT parameter so we can overrule the detection result for
377                         types too !
378 
379             @note       #i60158#
380                         sometimes our text ascii and our csv filter can't work together.
381                         Then we overwrite our detection hardly.
382                         sType param is used as out parameter then too ... and
383                         rDescriptor will be changed by selecting another filter.
384                         (see code)
385          */
386         void impl_checkResultsAndAddBestFilter(::comphelper::MediaDescriptor& rDescriptor,
387                                                ::rtl::OUString&               sType      );
388 
389     //-------------------------------------------
390     // uno interface
391 
392     public:
393 
394         //---------------------------------------
395         // XTypeDetection
396 
397         virtual ::rtl::OUString SAL_CALL queryTypeByURL(const ::rtl::OUString& sURL);
398 
399         virtual ::rtl::OUString SAL_CALL queryTypeByDescriptor(css::uno::Sequence< css::beans::PropertyValue >& lDescriptor,
400                                                                sal_Bool                                         bAllowDeep );
401 
402     //-------------------------------------------
403     // static uno helper!
404 
405     public:
406 
407         //---------------------------------------
408 
409         /** @short  return the uno implementation name of this class.
410 
411             @descr  Because this information is used at several places
412                     (and mostly an object instance of this class is not possible)
413                     its implemented as a static function!
414 
415             @return The fix uno implementation name of this class.
416          */
417         static ::rtl::OUString impl_getImplementationName();
418 
419         //---------------------------------------
420 
421         /** @short  return the list of supported uno services of this class.
422 
423             @descr  Because this information is used at several places
424                     (and mostly an object instance of this class is not possible)
425                     its implemented as a static function!
426 
427             @return The fix list of uno services supported by this class.
428          */
429         static css::uno::Sequence< ::rtl::OUString > impl_getSupportedServiceNames();
430 
431         //---------------------------------------
432 
433         /** @short  return a new intsnace of this class.
434 
435             @descr  This method is used by the uno service manager, to create
436                     a new instance of this service if needed.
437 
438             @param  xSMGR
439                     reference to the uno service manager, which requires
440                     this new instance. It should be passed to the new object
441                     so it can be used internally to create own needed uno resources.
442 
443             @return The new instance of this service as an uno reference.
444          */
445         static css::uno::Reference< css::uno::XInterface > impl_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
446 };
447 
448     } // namespace config
449 } // namespace filter
450 
451 #endif // __FILTER_CONFIG_TYPEDETECTION_HXX_
452 
453 /* vim: set noet sw=4 ts=4: */
454