summaryrefslogtreecommitdiff
path: root/filter/source/config/cache/typedetection.hxx
blob: e58042c58d899aac1f695ac10ecfb6781f302c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#ifndef __FILTER_CONFIG_TYPEDETECTION_HXX_
#define __FILTER_CONFIG_TYPEDETECTION_HXX_

#include "basecontainer.hxx"
#include <com/sun/star/document/XTypeDetection.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <comphelper/mediadescriptor.hxx>
#include <cppuhelper/implbase1.hxx>


namespace filter{ namespace config {


//_______________________________________________

/** @short      implements the service <type scope="com.sun.star.document">TypeDetection</type>.
 */
class TypeDetection : public ::cppu::ImplInheritanceHelper1< BaseContainer                 ,
                                                             css::document::XTypeDetection >
{
//-------------------------------------------
// native interface

    css::uno::Reference< css::uno::XComponentContext > m_xContext;

public:

    //---------------------------------------
    // ctor/dtor

    /** @short  standard ctor to connect this interface wrapper to
                the global filter cache instance ...

        @param  rxContext
                reference to the uno service manager, which created this service instance.
     */
    TypeDetection(const css::uno::Reference< css::uno::XComponentContext >& rxContext);

    //---------------------------------------

    /** @short  standard dtor.
     */
    virtual ~TypeDetection();

//-------------------------------------------
// private helper

private:

    bool impl_getPreselectionForType(
        const OUString& sPreSelType, const com::sun::star::util::URL& aParsedURL, FlatDetection& rFlatTypes, bool bDocService);

    bool impl_getPreselectionForDocumentService(
        const OUString& sPreSelDocumentService, const com::sun::star::util::URL& aParsedURL, FlatDetection& rFlatTypes);

    OUString impl_getTypeFromFilter(const OUString& rFilterName);

    /**
     * Get all format types that we handle.
     */
    void impl_getAllFormatTypes(
        const com::sun::star::util::URL& aParsedURL, comphelper::MediaDescriptor& rDescriptor,
        FlatDetection& rFlatTypes);

    //---------------------------------------

    /** @short      make a combined flat/deep type detection

        @descr      It steps over all flat detected types (given by the parameter lFlatTypes),
                    try it and search for most suitable one.
                    The specified MediaDescriptor will be patched, so it contain
                    the right values everytime. Using of any deep detection service
                    can be enabled/disabled. And last but not least: If the results
                    wont be realy clear (because a flat detected type has no deep
                    detection service), a "sugested" type name will be returned as "rLastChance".
                    It can be used after e.g. all well known deep detection services
                    was used without getting any result. Then this "last-chance-type"
                    should be returned. Of course using of it can fail too ... but its a try :-)

                    As an optimization - this method collects the names of all used deep
                    detection services. This information can be useful inside the may be
                    afterwards called method "impl_detectTypeDeepOnly()"!

        @param      rDescriptor
                    provides any easy-to-use stl interface to the MediaDescriptor.
                    Note : Its content will be adapted to returned result of this method.
                    Means: The type/filter entries of it will be actualized or removed from it.

        @param      lFlatTypes
                    a list of all flat detected types, which should be checked here.
                    No other types are allowed here!

        @param      rLastChance
                    the internal name of a "suggested type" ... (see before)
                    Note: it will be reseted to an empty string everytimes. So
                    a set value of "rLastChance" can be detected outside very easy.

        @param      rUsedDetectors
                    used as [out] parameter. It contains a list of names of all deep
                    detection services, which was used inside this method.
                    Such detectors can be ignored later if "impl_detectTypeDeepOnly()"
                    is called.

        @param      bAllowDeep
                    enable/disable using of a might existing deep detection service.

        @return     The internal name of a detected type.
                    An empty value if detection failed. .... but see rLastChance
                    for additional returns!
     */
    OUString impl_detectTypeFlatAndDeep(      ::comphelper::MediaDescriptor& rDescriptor   ,
                                               const FlatDetection&                 lFlatTypes    ,
                                                     sal_Bool                       bAllowDeep    ,
                                                     OUStringList&                  rUsedDetectors,
                                                     OUString&               rLastChance   );

    //---------------------------------------

    /** @short      seek a might existing stream to position 0.

        @descr      This is an optinal action to be more robust
                    in case any detect service doesnt make this seek ...
                    Normaly it's part of any called detect service or filter ...
                    but sometimes it's not done there.

        @param      rDescriptor
                    a stl representation of the MediaDescriptor as in/out parameter.
     */
    void impl_seekStreamToZero(comphelper::MediaDescriptor& rDescriptor);

    //---------------------------------------

    /** @short      make deep type detection for a specified
                    detect service (threadsafe!).

        @descr      It creates the right uno service, prepare the
                    needed MediaDescriptor, call ths right interfaces,
                    and return the results.

        @attention  The results (means type and corresponding filter)
                    are already part of the in/out parameter pDescriptor.
                    (in case they was valid).

        @param      sDetectService
                    uno service name of the detect service.

        @param      rDescriptor
                    a stl representation of the MediaDescriptor as in/out parameter.
     */
    OUString impl_askDetectService(const OUString&               sDetectService,
                                                ::comphelper::MediaDescriptor& rDescriptor   );

    //---------------------------------------

    /** @short      try to find an interaction handler and
                    ask him to select a possible filter for
                    this unknown format.

        @descr      If the user select a filter, it will be used as return value
                    without further checking against the given file content!

        @param      rDescriptor
                    a stl representation of the MediaDescriptor as in/out parameter.

        @return     [string]
                    a valid type name or an empty string if user canceled interaction.
     */
    OUString impl_askUserForTypeAndFilterIfAllowed(::comphelper::MediaDescriptor& rDescriptor);

    //---------------------------------------

    /** @short      check if an input stream is already part of the
                    given MediaDesciptor and creates a new one if neccessary.

        @attention  This method does further something special!
                    <ul>
                        <li>
                            If the given URL seem to be a streamable content, but creation of the stream
                            failed (might by an IOException), this method throws an exception.
                            (May be an existing interaction handler must be called here too ...)
                            The whole detection must be interrupted then and the interface method queryTypeByDescriptor()
                            must return an empty type name value.

                            That prevent us against multiple handling of the same error more then ones
                            (e.g. if we ask all detect services as fallback ...).
                        </li>
                        <li>
                            In case the stream already exists inside the descriptor this method does nothing.
                        </li>
                        <li>
                            In case the stream does not exists but can be created successfully, the stream will
                            be added to the descriptor.
                        </li>
                    </ul>

        @param      rDescriptor
                    provides any easy-to-use stl interface to the MediaDescriptor.
                    Note : Its content will be adapted to returned result of this method.
                    Means: The stream will be added to it.

        @throw      Any suitable exception if stream should be opened but operation was not sucessfull.
                    Note: If an interactionHandler is part of the given descriptor too, it was already used.
                    Means: let the exception pass trough the top most interface method!
     */
    void impl_openStream(::comphelper::MediaDescriptor& rDescriptor)
        throw (css::uno::Exception);

    //---------------------------------------

    /** @short      validate the specified type and its relation ships
                    and set all needed information related to this type
                    in the specified descriptor.

        @descr      Related information are: - corresponding filter
                                             - media type
                                             - ...

        @param      rDescriptor
                    provides access to the outside MediaDescriptor.

        @param      sType
                    the name of the type, which should be set on the descriptor.
                    Can be empty to remove any related value from the descriptor!

        @return     TRUE the specified type and its registrations was valid(!) and
                    could be set on the descriptor.
     */
    sal_Bool impl_validateAndSetTypeOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
                                                 const OUString&               sType      );

    //---------------------------------------

    /** @short      validate the specified filter and its relation ships
                    and set all needed information related to this filter
                    in the specified descriptor.

        @descr      Related information are: - corresponding type
                                             - ...

        @param      rDescriptor
                    provides access to the outside MediaDescriptor.

        @param      sFilter
                    the name of the filter, which should be set on the descriptor.
                    Can be empty to remove any related value from the descriptor!

        @return     TRUE the specified type and its registrations was valid(!) and
                    could be set on the descriptor.
     */
    sal_Bool impl_validateAndSetFilterOnDescriptor(      ::comphelper::MediaDescriptor& rDescriptor,
                                                   const OUString&               sFilter    );

    //---------------------------------------

    /** @short      remove anythimng related to a TYPE/FILTER entry from the
                    specified MediaDescriptor.

        @descr      This method works together with impl_validateAndSetTypeOnDescriptor()/
                    impl_validateAndSetFilterOnDescriptor(). All informations, which can be
                    set by these two operations must be "removable" by this method.

        @param      rDescriptor
                    reference to the MediaDescriptor (represented by an easy-to-use
                    stl interface!), which should be patched.
     */
    void impl_removeTypeFilterFromDescriptor(::comphelper::MediaDescriptor& rDescriptor);

    //---------------------------------------

    /** @short      search the best suitable filter for the given type
                    and add it into the media descriptor.

        @descr      Normaly this is a type detection only ...
                    but for some special features we must overwrite our detection
                    because a file must be loaded into a special (means preselected)
                    application.

                    E.g. CSV/TXT format are sometimes ugly to handle .-)

                    Note: If the descriptor already include a filter
                    (may be selected by a FilterSelect interaction or preselected
                    by the user itself) ... we dont change that here !

        @param      rDescriptor
                    reference to the MediaDescriptor (represented by an easy-to-use
                    stl interface!), which should be patched.

        @param      sType
                    the internal type name, where we search a filter for.
                    Used as IN/OUT parameter so we can overrule the detection result for
                    types too !

        @note       #i60158#
                    sometimes our text ascii and our csv filter cant work together.
                    Then we overwrite our detection hardly.
                    sType param is used as out parameter then too ... and
                    rDescriptor will be changed by selecting another filter.
                    (see code)
     */
    void impl_checkResultsAndAddBestFilter(::comphelper::MediaDescriptor& rDescriptor,
                                           OUString&               sType      );

//-------------------------------------------
// uno interface

public:

    //---------------------------------------
    // XTypeDetection

    virtual OUString SAL_CALL queryTypeByURL(const OUString& sURL)
        throw (css::uno::RuntimeException);

    virtual OUString SAL_CALL queryTypeByDescriptor(css::uno::Sequence< css::beans::PropertyValue >& lDescriptor,
                                                           sal_Bool                                         bAllowDeep )
        throw (css::uno::RuntimeException);

//-------------------------------------------
// static uno helper!

public:

    //---------------------------------------

    /** @short  return the uno implementation name of this class.

        @descr  Because this information is used at several places
                (and mostly an object instance of this class is not possible)
                its implemented as a static function!

        @return The fix uno implementation name of this class.
     */
    static OUString impl_getImplementationName();

    //---------------------------------------

    /** @short  return the list of supported uno services of this class.

        @descr  Because this information is used at several places
                (and mostly an object instance of this class is not possible)
                its implemented as a static function!

        @return The fix list of uno services supported by this class.
     */
    static css::uno::Sequence< OUString > impl_getSupportedServiceNames();

    //---------------------------------------

    /** @short  return a new intsnace of this class.

        @descr  This method is used by the uno service manager, to create
                a new instance of this service if needed.

        @param  xSMGR
                reference to the uno service manager, which require
                this new instance. It should be passed to the new object
                so it can be used internaly to create own needed uno resources.

        @return The new instance of this service as an uno reference.
     */
    static css::uno::Reference< css::uno::XInterface > impl_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
};

}}

#endif // __FILTER_CONFIG_TYPEDETECTION_HXX_

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */