typedetection.cxx (9f813b30) typedetection.cxx (1fbfd7a2)
1/**************************************************************
1/**************************************************************
2 *
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
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 *
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
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 *
19 *
20 *************************************************************/
21
22
23
24// MARKER(update_precomp.py): autogen include statement, do not remove
25#include "precompiled_filter.hxx"
26#include "typedetection.hxx"
27#include "constant.hxx"

--- 20 unchanged lines hidden (view full) ---

48namespace filter{
49 namespace config{
50
51namespace css = ::com::sun::star;
52
53//_______________________________________________
54// definitions
55
20 *************************************************************/
21
22
23
24// MARKER(update_precomp.py): autogen include statement, do not remove
25#include "precompiled_filter.hxx"
26#include "typedetection.hxx"
27#include "constant.hxx"

--- 20 unchanged lines hidden (view full) ---

48namespace filter{
49 namespace config{
50
51namespace css = ::com::sun::star;
52
53//_______________________________________________
54// definitions
55
56// Use this switch to change the behaviour of preselection DocumentService ... (see using for further informations)
56// Use this switch to change the behavior of preselection DocumentService ... (see using for further informations)
57#define IGNORE_NON_URLMATCHING_TYPES_FOR_PRESELECTION_DOCUMENTSERVICE
58
59// enable/disable special handling for CSV/TXT problem
60#define WORKAROUND_CSV_TXT_BUG_i60158
61
62/*-----------------------------------------------
63 03.07.2003 11:25
64-----------------------------------------------*/

--- 56 unchanged lines hidden (view full) ---

121
122/*-----------------------------------------------
123 31.10.2003 09:36
124-----------------------------------------------*/
125::rtl::OUString SAL_CALL TypeDetection::queryTypeByDescriptor(css::uno::Sequence< css::beans::PropertyValue >& lDescriptor,
126 sal_Bool bAllowDeep )
127 throw (css::uno::RuntimeException)
128{
57#define IGNORE_NON_URLMATCHING_TYPES_FOR_PRESELECTION_DOCUMENTSERVICE
58
59// enable/disable special handling for CSV/TXT problem
60#define WORKAROUND_CSV_TXT_BUG_i60158
61
62/*-----------------------------------------------
63 03.07.2003 11:25
64-----------------------------------------------*/

--- 56 unchanged lines hidden (view full) ---

121
122/*-----------------------------------------------
123 31.10.2003 09:36
124-----------------------------------------------*/
125::rtl::OUString SAL_CALL TypeDetection::queryTypeByDescriptor(css::uno::Sequence< css::beans::PropertyValue >& lDescriptor,
126 sal_Bool bAllowDeep )
127 throw (css::uno::RuntimeException)
128{
129 // make the descriptor more useable :-)
129 // make the descriptor more usable :-)
130 ::comphelper::MediaDescriptor stlDescriptor(lDescriptor);
131
132 // SAFE -> ----------------------------------
133 ::osl::ResettableMutexGuard aLock(m_aLock);
134
135 //*******************************************
136 // parse given URL to split it into e.g. main and jump marks ...
137 ::rtl::OUString sURL = stlDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_URL(), ::rtl::OUString());

--- 46 unchanged lines hidden (view full) ---

184 // which uses all registered deep detection services.
185 if (
186 (!sType.getLength()) &&
187 (bAllowDeep )
188 )
189 {
190 sType = impl_detectTypeDeepOnly(stlDescriptor, lUsedDetectors);
191 }
130 ::comphelper::MediaDescriptor stlDescriptor(lDescriptor);
131
132 // SAFE -> ----------------------------------
133 ::osl::ResettableMutexGuard aLock(m_aLock);
134
135 //*******************************************
136 // parse given URL to split it into e.g. main and jump marks ...
137 ::rtl::OUString sURL = stlDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_URL(), ::rtl::OUString());

--- 46 unchanged lines hidden (view full) ---

184 // which uses all registered deep detection services.
185 if (
186 (!sType.getLength()) &&
187 (bAllowDeep )
188 )
189 {
190 sType = impl_detectTypeDeepOnly(stlDescriptor, lUsedDetectors);
191 }
192
192
193 //*******************************************
194 // flat detection failed
195 // pure deep detection failed
196 // => ask might existing InteractionHandler
197 // means: ask user for it's decision
198 if (!sType.getLength())
199 sType = impl_askUserForTypeAndFilterIfAllowed(stlDescriptor);
200

--- 23 unchanged lines hidden (view full) ---

224
225/*-----------------------------------------------
226 03.07.2003 10:36
227-----------------------------------------------*/
228void TypeDetection::impl_checkResultsAndAddBestFilter(::comphelper::MediaDescriptor& rDescriptor,
229 ::rtl::OUString& sType )
230{
231 // a)
193 //*******************************************
194 // flat detection failed
195 // pure deep detection failed
196 // => ask might existing InteractionHandler
197 // means: ask user for it's decision
198 if (!sType.getLength())
199 sType = impl_askUserForTypeAndFilterIfAllowed(stlDescriptor);
200

--- 23 unchanged lines hidden (view full) ---

224
225/*-----------------------------------------------
226 03.07.2003 10:36
227-----------------------------------------------*/
228void TypeDetection::impl_checkResultsAndAddBestFilter(::comphelper::MediaDescriptor& rDescriptor,
229 ::rtl::OUString& sType )
230{
231 // a)
232 // Dont overwrite a might preselected filter!
232 // Don't overwrite a might preselected filter!
233 ::rtl::OUString sFilter = rDescriptor.getUnpackedValueOrDefault(
234 ::comphelper::MediaDescriptor::PROP_FILTERNAME(),
235 ::rtl::OUString());
236 if (sFilter.getLength())
237 return;
238
239 // b)
240 // check a preselected document service too.

--- 170 unchanged lines hidden (view full) ---

411/*-----------------------------------------------
412 14.11.2003 12:06
413-----------------------------------------------*/
414sal_Bool TypeDetection::impl_getPreselectionForType(const ::rtl::OUString& sPreSelType,
415 const css::util::URL& aParsedURL ,
416 FlatDetection& rFlatTypes )
417{
418 // Can be used to suppress execution of some parts of this method
233 ::rtl::OUString sFilter = rDescriptor.getUnpackedValueOrDefault(
234 ::comphelper::MediaDescriptor::PROP_FILTERNAME(),
235 ::rtl::OUString());
236 if (sFilter.getLength())
237 return;
238
239 // b)
240 // check a preselected document service too.

--- 170 unchanged lines hidden (view full) ---

411/*-----------------------------------------------
412 14.11.2003 12:06
413-----------------------------------------------*/
414sal_Bool TypeDetection::impl_getPreselectionForType(const ::rtl::OUString& sPreSelType,
415 const css::util::URL& aParsedURL ,
416 FlatDetection& rFlatTypes )
417{
418 // Can be used to suppress execution of some parts of this method
419 // if its already clear that detected type is valid or not.
420 // Its necessary to use shared code at the end, which update
421 // all return parameters constistency!
419 // if it's already clear that detected type is valid or not.
420 // It's necessary to use shared code at the end, which updates
421 // all return parameters consistent!
422 sal_Bool bBreakDetection = sal_False;
423
424 // Further we must know if it matches by pattern
425 // Every flat detected type by pattern won't be detected deep!
426 sal_Bool bMatchByPattern = sal_False;
427
428 // And we must know if a preselection must be preferred, because
429 // it matches by it's extension too.
430 sal_Bool bMatchByExtension = sal_False;
431
432 // If we e.g. collect all filters of a factory (be a forced factory preselection)
422 sal_Bool bBreakDetection = sal_False;
423
424 // Further we must know if it matches by pattern
425 // Every flat detected type by pattern won't be detected deep!
426 sal_Bool bMatchByPattern = sal_False;
427
428 // And we must know if a preselection must be preferred, because
429 // it matches by it's extension too.
430 sal_Bool bMatchByExtension = sal_False;
431
432 // If we e.g. collect all filters of a factory (be a forced factory preselection)
433 // we should preferr all filters of this factory, where the type match the given URL.
434 // All other types (which sorrespond to filters of the same factory - but dont match
433 // we should prefer all filters of this factory, where the type match the given URL.
434 // All other types (which correspond to filters of the same factory - but don't match
435 // the URL) should be "used later" for detection and sorted at the end of our return vector
436 // rFlatTypes!
437 // => bPreferredPreselection = (matchByExtension || matchByURLPattern)
438 sal_Bool bPreferredPreselection = sal_False;
439
440 // validate type
441 ::rtl::OUString sType(sPreSelType);
442 CacheItem aType;

--- 60 unchanged lines hidden (view full) ---

503 bMatchByPattern = sal_True;
504 bPreferredPreselection = sal_True;
505 break;
506 }
507 }
508 }
509
510 /*
435 // the URL) should be "used later" for detection and sorted at the end of our return vector
436 // rFlatTypes!
437 // => bPreferredPreselection = (matchByExtension || matchByURLPattern)
438 sal_Bool bPreferredPreselection = sal_False;
439
440 // validate type
441 ::rtl::OUString sType(sPreSelType);
442 CacheItem aType;

--- 60 unchanged lines hidden (view full) ---

503 bMatchByPattern = sal_True;
504 bPreferredPreselection = sal_True;
505 break;
506 }
507 }
508 }
509
510 /*
511 Comment ... why the following line of code should be comened out .-)
511 Comment ... why the following line of code should be commented out .-)
512
513 This type does not seem to fit the requirements
512
513 This type does not seem to fit the requirements
514 But its an existing and well known type.
515 At least - [because may be the extension was missing :-( ]
514 But it's an existing and well known type.
515 At least - [because maybe the extension was missing :-( ]
516 we should try to detect this type deep ...
517 So we accept it here :-)
518
519 if (!bBreakDetection)
520 sType = ::rtl::OUString();
521 */
522 }
523
516 we should try to detect this type deep ...
517 So we accept it here :-)
518
519 if (!bBreakDetection)
520 sType = ::rtl::OUString();
521 */
522 }
523
524 // if its a valid type - set it on all return values!
524 // if it's a valid type - set it on all return values!
525 if (sType.getLength())
526 {
527 FlatDetectionInfo aInfo;
528 aInfo.sType = sType;
529 aInfo.bMatchByExtension = bMatchByExtension;
530 aInfo.bMatchByPattern = bMatchByPattern;
531 aInfo.bPreselectedAsType = sal_True;
532

--- 12 unchanged lines hidden (view full) ---

545/*-----------------------------------------------
546 14.11.2003 12:09
547-----------------------------------------------*/
548sal_Bool TypeDetection::impl_getPreselectionForFilter(const ::rtl::OUString& sPreSelFilter,
549 const css::util::URL& aParsedURL ,
550 FlatDetection& rFlatTypes )
551{
552 // Can be used to suppress execution of some parts of this method
525 if (sType.getLength())
526 {
527 FlatDetectionInfo aInfo;
528 aInfo.sType = sType;
529 aInfo.bMatchByExtension = bMatchByExtension;
530 aInfo.bMatchByPattern = bMatchByPattern;
531 aInfo.bPreselectedAsType = sal_True;
532

--- 12 unchanged lines hidden (view full) ---

545/*-----------------------------------------------
546 14.11.2003 12:09
547-----------------------------------------------*/
548sal_Bool TypeDetection::impl_getPreselectionForFilter(const ::rtl::OUString& sPreSelFilter,
549 const css::util::URL& aParsedURL ,
550 FlatDetection& rFlatTypes )
551{
552 // Can be used to suppress execution of some parts of this method
553 // if its already clear that detected filter is valid or not.
554 // Its necessary to use shared code at the end, which update
555 // all return parameters constistency!
553 // if it's already clear that detected filter is valid or not.
554 // It's necessary to use shared code at the end, which updates
555 // all return parameters consistent!
556 sal_Bool bBreakDetection = sal_False;
557
558 // validate filter
559 ::rtl::OUString sFilter(sPreSelFilter);
560 CacheItem aFilter;
561 try
562 {
563 // SAFE -> --------------------------

--- 128 unchanged lines hidden (view full) ---

692
693 /* #i55122#
694 Sometimes we must detect files without or with real unknown extensions.
695 If it does not work /which can happen of course .-)/, the user tried to preselect
696 the right format. But some special dialogs (e.g. "Insert->Sheet From File")
697 add it's own preselection too.
698 So we have a combination of preselected values ...
699
556 sal_Bool bBreakDetection = sal_False;
557
558 // validate filter
559 ::rtl::OUString sFilter(sPreSelFilter);
560 CacheItem aFilter;
561 try
562 {
563 // SAFE -> --------------------------

--- 128 unchanged lines hidden (view full) ---

692
693 /* #i55122#
694 Sometimes we must detect files without or with real unknown extensions.
695 If it does not work /which can happen of course .-)/, the user tried to preselect
696 the right format. But some special dialogs (e.g. "Insert->Sheet From File")
697 add it's own preselection too.
698 So we have a combination of preselected values ...
699
700 The we should preferr the most important one - set by the user.
700 Then we should prefer the most important one - set by the user.
701 And the user normally preselects a filter or type. The preselected
702 document service cames from the dialog.
703
704 Further it doesn't matter if the user preselected a filter or a document service.
705 A filter selection is always more explicit then a document service selection.
706 So it must be pereferred. An order between type and filter selection can't be discussed .-)
707 */
708

--- 14 unchanged lines hidden (view full) ---

723 03.11.2003 09:17
724-----------------------------------------------*/
725::rtl::OUString TypeDetection::impl_detectTypeFlatAndDeep( ::comphelper::MediaDescriptor& rDescriptor ,
726 const FlatDetection& lFlatTypes ,
727 sal_Bool bAllowDeep ,
728 OUStringList& rUsedDetectors,
729 ::rtl::OUString& rLastChance )
730{
701 And the user normally preselects a filter or type. The preselected
702 document service cames from the dialog.
703
704 Further it doesn't matter if the user preselected a filter or a document service.
705 A filter selection is always more explicit then a document service selection.
706 So it must be pereferred. An order between type and filter selection can't be discussed .-)
707 */
708

--- 14 unchanged lines hidden (view full) ---

723 03.11.2003 09:17
724-----------------------------------------------*/
725::rtl::OUString TypeDetection::impl_detectTypeFlatAndDeep( ::comphelper::MediaDescriptor& rDescriptor ,
726 const FlatDetection& lFlatTypes ,
727 sal_Bool bAllowDeep ,
728 OUStringList& rUsedDetectors,
729 ::rtl::OUString& rLastChance )
730{
731 // reset it everytimes, so the outside code can distinguish between
731 // reset it every time, so the outside code can distinguish between
732 // a set and a not set value.
733 rLastChance = ::rtl::OUString();
734 rUsedDetectors.clear();
735
736 // step over all possible types for this URL.
737 // solutions:
738 // a) no types => no detection
732 // a set and a not set value.
733 rLastChance = ::rtl::OUString();
734 rUsedDetectors.clear();
735
736 // step over all possible types for this URL.
737 // solutions:
738 // a) no types => no detection
739 // b) deep detection not allowed => return first valid type of list (because its the preferred or the first valid one)
739 // b) deep detection not allowed => return first valid type of list (because it's the preferred or the first valid one)
740 // or(!) match by URLPattern => in such case a deep detection will be suppressed!
741 // c) type has no detect service => safe the first occurred type without a detect service
742 // as "last chance"(!). It will be used outside of this method
743 // if no further type could be detected.
744 // It must be the first one, because it can be a preferred type.
745 // Our types list was sorted by such criteria!
746 // d) detect service return a valid result => return its decision
747 // e) detect service return an invalid result

--- 30 unchanged lines hidden (view full) ---

778 aType[PROPNAME_DETECTSERVICE] >>= sDetectService;
779
780 // c)
781 if (!sDetectService.getLength())
782 {
783 // accept or not accept flat types without deep detection: that's the question :-)
784 // May be there exists some states, where we have to use our LastChance feature instead
785 // of using the flat type directly.
740 // or(!) match by URLPattern => in such case a deep detection will be suppressed!
741 // c) type has no detect service => safe the first occurred type without a detect service
742 // as "last chance"(!). It will be used outside of this method
743 // if no further type could be detected.
744 // It must be the first one, because it can be a preferred type.
745 // Our types list was sorted by such criteria!
746 // d) detect service return a valid result => return its decision
747 // e) detect service return an invalid result

--- 30 unchanged lines hidden (view full) ---

778 aType[PROPNAME_DETECTSERVICE] >>= sDetectService;
779
780 // c)
781 if (!sDetectService.getLength())
782 {
783 // accept or not accept flat types without deep detection: that's the question :-)
784 // May be there exists some states, where we have to use our LastChance feature instead
785 // of using the flat type directly.
786 // Here the list of task ID's, which wasrelated to these lines of code:
786 // Here the list of task ID's, which was related to these lines of code:
787 // #i47159#, #i43404#, #i46494#
788
789 // a flat detected type without the chance for a deep detection ... but preselected by the user
790 // explicitly (means preselected as type or filter ... not as documentservice!)
791 // should be accepted. So the user can overrule our detection.
792 if (
793 (aFlatTypeInfo.bPreselectedAsType ) ||
794 (aFlatTypeInfo.bPreselectedByFilter)

--- 5 unchanged lines hidden (view full) ---

800 // be detected. Of course only the first type without deep detector can be used.
801 // Further ones has to be ignored.
802 if (rLastChance.getLength() < 1)
803 rLastChance = sFlatType;
804
805 continue;
806 }
807
787 // #i47159#, #i43404#, #i46494#
788
789 // a flat detected type without the chance for a deep detection ... but preselected by the user
790 // explicitly (means preselected as type or filter ... not as documentservice!)
791 // should be accepted. So the user can overrule our detection.
792 if (
793 (aFlatTypeInfo.bPreselectedAsType ) ||
794 (aFlatTypeInfo.bPreselectedByFilter)

--- 5 unchanged lines hidden (view full) ---

800 // be detected. Of course only the first type without deep detector can be used.
801 // Further ones has to be ignored.
802 if (rLastChance.getLength() < 1)
803 rLastChance = sFlatType;
804
805 continue;
806 }
807
808 // dont forget to add every real asked deep detection service here.
808 // don't forget to add every real asked deep detection service here.
809 // Such detectors will be ignored if may be "impl_detectTypeDeepOnly()"
810 // must be called later!
811 rUsedDetectors.push_back(sDetectService);
812 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
813
814 // d)
815 if (sDeepType.getLength())
816 return sDeepType;

--- 19 unchanged lines hidden (view full) ---

836 // Reason: Such deep detection services work differently in these two modes!
837 OUStringList lInsideUsedDetectors;
838 OUStringList::const_iterator pIt;
839
840 // a)
841 // The list of "already used detect services" correspond to the list
842 // of preselected or flat detected types. But these detect services was called
843 // to check these types explicitly and return black/white ... yes/no only.
809 // Such detectors will be ignored if may be "impl_detectTypeDeepOnly()"
810 // must be called later!
811 rUsedDetectors.push_back(sDetectService);
812 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
813
814 // d)
815 if (sDeepType.getLength())
816 return sDeepType;

--- 19 unchanged lines hidden (view full) ---

836 // Reason: Such deep detection services work differently in these two modes!
837 OUStringList lInsideUsedDetectors;
838 OUStringList::const_iterator pIt;
839
840 // a)
841 // The list of "already used detect services" correspond to the list
842 // of preselected or flat detected types. But these detect services was called
843 // to check these types explicitly and return black/white ... yes/no only.
844 // Now they are called to return any possible result. But we should preferr
844 // Now they are called to return any possible result. But we should prefer
845 // these already used detect services against all other ones!
846 for ( pIt = lOutsideUsedDetectors.begin();
847 pIt != lOutsideUsedDetectors.end() ;
848 ++pIt )
849 {
850 const ::rtl::OUString& sDetectService = *pIt;
851 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
852 if (sDeepType.getLength())

--- 83 unchanged lines hidden (view full) ---

936 // c)
937 // Last chance. No "used detectors", no "preselected detectors" ... ask any existing detect services
938 // for this till know unknown format.
939 for ( pIt = lDetectors.begin();
940 pIt != lDetectors.end() ;
941 ++pIt )
942 {
943 const ::rtl::OUString& sDetectService = *pIt;
845 // these already used detect services against all other ones!
846 for ( pIt = lOutsideUsedDetectors.begin();
847 pIt != lOutsideUsedDetectors.end() ;
848 ++pIt )
849 {
850 const ::rtl::OUString& sDetectService = *pIt;
851 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
852 if (sDeepType.getLength())

--- 83 unchanged lines hidden (view full) ---

936 // c)
937 // Last chance. No "used detectors", no "preselected detectors" ... ask any existing detect services
938 // for this till know unknown format.
939 for ( pIt = lDetectors.begin();
940 pIt != lDetectors.end() ;
941 ++pIt )
942 {
943 const ::rtl::OUString& sDetectService = *pIt;
944
944
945 OUStringList::const_iterator pAlreadyUsed = ::std::find(lInsideUsedDetectors.begin(), lInsideUsedDetectors.end(), sDetectService);
946 if (pAlreadyUsed != lInsideUsedDetectors.end())
947 continue;
948
949 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
950 if (sDeepType.getLength())
951 return sDeepType;
952 }
953
954 return ::rtl::OUString();
955}
956
957/*-----------------------------------------------
958 07.03.2005 11:13
959-----------------------------------------------*/
960void TypeDetection::impl_seekStreamToZero(comphelper::MediaDescriptor& rDescriptor)
961{
945 OUStringList::const_iterator pAlreadyUsed = ::std::find(lInsideUsedDetectors.begin(), lInsideUsedDetectors.end(), sDetectService);
946 if (pAlreadyUsed != lInsideUsedDetectors.end())
947 continue;
948
949 ::rtl::OUString sDeepType = impl_askDetectService(sDetectService, rDescriptor);
950 if (sDeepType.getLength())
951 return sDeepType;
952 }
953
954 return ::rtl::OUString();
955}
956
957/*-----------------------------------------------
958 07.03.2005 11:13
959-----------------------------------------------*/
960void TypeDetection::impl_seekStreamToZero(comphelper::MediaDescriptor& rDescriptor)
961{
962 // try to seek to 0 ...
962 // try to seek to 0 ...
963 // But because XSeekable is an optional interface ... try it only .-)
964 css::uno::Reference< css::io::XInputStream > xStream = rDescriptor.getUnpackedValueOrDefault(
965 ::comphelper::MediaDescriptor::PROP_INPUTSTREAM(),
966 css::uno::Reference< css::io::XInputStream >());
967 css::uno::Reference< css::io::XSeekable > xSeek(xStream, css::uno::UNO_QUERY);
968 if (xSeek.is())
969 {
970 try

--- 18 unchanged lines hidden (view full) ---

989 // Attention: This method throws an exception if the stream could not be opened.
990 // It's important to break any further detection in such case.
991 // Catch it on the highest detection level only !!!
992 impl_openStream(rDescriptor);
993
994 // seek to 0 is an optional feature to be more robust against
995 // "simple implemented detect services" .-)
996 impl_seekStreamToZero(rDescriptor);
963 // But because XSeekable is an optional interface ... try it only .-)
964 css::uno::Reference< css::io::XInputStream > xStream = rDescriptor.getUnpackedValueOrDefault(
965 ::comphelper::MediaDescriptor::PROP_INPUTSTREAM(),
966 css::uno::Reference< css::io::XInputStream >());
967 css::uno::Reference< css::io::XSeekable > xSeek(xStream, css::uno::UNO_QUERY);
968 if (xSeek.is())
969 {
970 try

--- 18 unchanged lines hidden (view full) ---

989 // Attention: This method throws an exception if the stream could not be opened.
990 // It's important to break any further detection in such case.
991 // Catch it on the highest detection level only !!!
992 impl_openStream(rDescriptor);
993
994 // seek to 0 is an optional feature to be more robust against
995 // "simple implemented detect services" .-)
996 impl_seekStreamToZero(rDescriptor);
997
997
998 css::uno::Reference< css::document::XExtendedFilterDetection > xDetector;
999 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR;
1000
1001 // SAFE ->
1002 ::osl::ResettableMutexGuard aLock(m_aLock);
1003 xSMGR = m_xSMGR;
1004 aLock.clear();
1005 // <- SAFE
1006
1007 // Attention! If e.g. an office module was not installed sometimes we find a
1008 // registered detect service, which is referred inside the configuration ... but not really
1009 // installed. On the other side we use third party components here, which can make trouble anyway.
1010 // So we should handle errors during creation of such services more gracefully .-)
1011 xDetector = css::uno::Reference< css::document::XExtendedFilterDetection >(
1012 xSMGR->createInstance(sDetectService),
1013 css::uno::UNO_QUERY);
998 css::uno::Reference< css::document::XExtendedFilterDetection > xDetector;
999 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR;
1000
1001 // SAFE ->
1002 ::osl::ResettableMutexGuard aLock(m_aLock);
1003 xSMGR = m_xSMGR;
1004 aLock.clear();
1005 // <- SAFE
1006
1007 // Attention! If e.g. an office module was not installed sometimes we find a
1008 // registered detect service, which is referred inside the configuration ... but not really
1009 // installed. On the other side we use third party components here, which can make trouble anyway.
1010 // So we should handle errors during creation of such services more gracefully .-)
1011 xDetector = css::uno::Reference< css::document::XExtendedFilterDetection >(
1012 xSMGR->createInstance(sDetectService),
1013 css::uno::UNO_QUERY);
1014
1014
1015 if ( ! xDetector.is())
1016 return ::rtl::OUString();
1015 if ( ! xDetector.is())
1016 return ::rtl::OUString();
1017
1017
1018 ::rtl::OUString sDeepType;
1019 try
1020 {
1021 // start deep detection
1018 ::rtl::OUString sDeepType;
1019 try
1020 {
1021 // start deep detection
1022 // Dont forget to convert stl descriptor to its uno representation.
1023
1022 // Don't forget to convert stl descriptor to its uno representation.
1023
1024 /* Attention!
1025 You have to use an explicit instance of this uno sequence ...
1024 /* Attention!
1025 You have to use an explicit instance of this uno sequence ...
1026 Because its used as an in out parameter. And in case of a temp. used object
1026 Because it's used as an in out parameter. And in case of a temp. used object
1027 we will run into memory corruptions!
1028 */
1029 css::uno::Sequence< css::beans::PropertyValue > lDescriptor;
1030 rDescriptor >> lDescriptor;
1031 sDeepType = xDetector->detect(lDescriptor);
1032 rDescriptor << lDescriptor;
1033 }
1034 catch(const css::uno::Exception&)
1035 {
1036 // We should ignore errors here.
1037 // Thrown exceptions mostly will end in crash recovery ...
1038 // But might be we find another deep detection service which can detect the same
1039 // document without a problem .-)
1040 sDeepType = ::rtl::OUString();
1041 }
1027 we will run into memory corruptions!
1028 */
1029 css::uno::Sequence< css::beans::PropertyValue > lDescriptor;
1030 rDescriptor >> lDescriptor;
1031 sDeepType = xDetector->detect(lDescriptor);
1032 rDescriptor << lDescriptor;
1033 }
1034 catch(const css::uno::Exception&)
1035 {
1036 // We should ignore errors here.
1037 // Thrown exceptions mostly will end in crash recovery ...
1038 // But might be we find another deep detection service which can detect the same
1039 // document without a problem .-)
1040 sDeepType = ::rtl::OUString();
1041 }
1042
1042
1043 // seek to 0 is an optional feature to be more robust against
1044 // "simple implemented detect services" .-)
1045 impl_seekStreamToZero(rDescriptor);
1043 // seek to 0 is an optional feature to be more robust against
1044 // "simple implemented detect services" .-)
1045 impl_seekStreamToZero(rDescriptor);
1046
1046
1047 // analyze the results
1048 // a) detect service returns "" => return "" too and remove TYPE/FILTER prop from descriptor
1049 // b) returned type is unknown => return "" too and remove TYPE/FILTER prop from descriptor
1050 // c) returned type is valid => check TYPE/FILTER props inside descriptor and return the type
1051
1052 // this special helper checks for a valid type
1053 // and set right values on the descriptor!
1054 sal_Bool bValidType = impl_validateAndSetTypeOnDescriptor(rDescriptor, sDeepType);
1055 if (bValidType)
1056 return sDeepType;
1047 // analyze the results
1048 // a) detect service returns "" => return "" too and remove TYPE/FILTER prop from descriptor
1049 // b) returned type is unknown => return "" too and remove TYPE/FILTER prop from descriptor
1050 // c) returned type is valid => check TYPE/FILTER props inside descriptor and return the type
1051
1052 // this special helper checks for a valid type
1053 // and set right values on the descriptor!
1054 sal_Bool bValidType = impl_validateAndSetTypeOnDescriptor(rDescriptor, sDeepType);
1055 if (bValidType)
1056 return sDeepType;
1057
1057
1058 return ::rtl::OUString();
1059}
1060
1061/*-----------------------------------------------
1062 17.12.2004 13:47
1063-----------------------------------------------*/
1064::rtl::OUString TypeDetection::impl_askUserForTypeAndFilterIfAllowed(::comphelper::MediaDescriptor& rDescriptor)
1065{
1066 // SAFE ->
1067 ::osl::ResettableMutexGuard aLock(m_aLock);
1068 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
1069 aLock.clear();
1070 // <- SAFE
1071
1058 return ::rtl::OUString();
1059}
1060
1061/*-----------------------------------------------
1062 17.12.2004 13:47
1063-----------------------------------------------*/
1064::rtl::OUString TypeDetection::impl_askUserForTypeAndFilterIfAllowed(::comphelper::MediaDescriptor& rDescriptor)
1065{
1066 // SAFE ->
1067 ::osl::ResettableMutexGuard aLock(m_aLock);
1068 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
1069 aLock.clear();
1070 // <- SAFE
1071
1072 css::uno::Reference< css::task::XInteractionHandler > xInteraction =
1072 css::uno::Reference< css::task::XInteractionHandler > xInteraction =
1073 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_INTERACTIONHANDLER(),
1074 css::uno::Reference< css::task::XInteractionHandler >());
1073 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_INTERACTIONHANDLER(),
1074 css::uno::Reference< css::task::XInteractionHandler >());
1075
1075
1076 if (!xInteraction.is())
1077 return ::rtl::OUString();
1076 if (!xInteraction.is())
1077 return ::rtl::OUString();
1078
1078
1079 ::rtl::OUString sURL =
1080 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_URL(),
1081 ::rtl::OUString());
1079 ::rtl::OUString sURL =
1080 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_URL(),
1081 ::rtl::OUString());
1082
1082
1083 css::uno::Reference< css::io::XInputStream > xStream =
1084 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_INPUTSTREAM(),
1085 css::uno::Reference< css::io::XInputStream >());
1086
1083 css::uno::Reference< css::io::XInputStream > xStream =
1084 rDescriptor.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_INPUTSTREAM(),
1085 css::uno::Reference< css::io::XInputStream >());
1086
1087 // Dont distrub the user for "non existing files - means empty URLs" or
1088 // if we was forced to detect a stream.
1087 // Don't disturb the user for "non existing files - means empty URLs" or
1088 // if we were forced to detect a stream.
1089 // Reason behind: We must be sure to ask user for "unknown contents" only ...
1090 // and not for "missing files". Especially if detection is done by a stream only
1091 // we can't check if the stream points to an "existing content"!
1092 if (
1093 (!sURL.getLength() ) || // "non existing file" ?
1094 (!xStream.is() ) || // non existing file !
1095 (sURL.equalsIgnoreAsciiCaseAsciiL("private:stream", 14)) // not a good idea .-)
1096 )
1089 // Reason behind: We must be sure to ask user for "unknown contents" only ...
1090 // and not for "missing files". Especially if detection is done by a stream only
1091 // we can't check if the stream points to an "existing content"!
1092 if (
1093 (!sURL.getLength() ) || // "non existing file" ?
1094 (!xStream.is() ) || // non existing file !
1095 (sURL.equalsIgnoreAsciiCaseAsciiL("private:stream", 14)) // not a good idea .-)
1096 )
1097 return ::rtl::OUString();
1098
1097 return ::rtl::OUString();
1098
1099 try
1100 {
1099 try
1100 {
1101 // create a new request to ask user for it's decision about the usable filter
1101 // create a new request to ask user for his decision about the usable filter
1102 ::framework::RequestFilterSelect aRequest(sURL);
1103 xInteraction->handle(aRequest.GetRequest());
1102 ::framework::RequestFilterSelect aRequest(sURL);
1103 xInteraction->handle(aRequest.GetRequest());
1104
1104
1105 // "Cancel" pressed? => return with error
1106 if (aRequest.isAbort())
1107 return ::rtl::OUString();
1105 // "Cancel" pressed? => return with error
1106 if (aRequest.isAbort())
1107 return ::rtl::OUString();
1108
1109 // "OK" pressed => verify the selected filter, get it's coressponding
1108
1109 // "OK" pressed => verify the selected filter, get its corresponding
1110 // type and return it. (BTW: We must update the media descriptor here ...)
1111 // The user selected explicitly a filter ... but normally we are interested on
1112 // a type here only. But we must be sure, that the selected filter is used
1110 // type and return it. (BTW: We must update the media descriptor here ...)
1111 // The user selected explicitly a filter ... but normally we are interested on
1112 // a type here only. But we must be sure, that the selected filter is used
1113 // too and no ambigous filter registration disturb us .-)
1114
1113 // too and no ambiguous filter registration disturb us .-)
1114
1115 ::rtl::OUString sFilter = aRequest.getFilter();
1116 if (!impl_validateAndSetFilterOnDescriptor(rDescriptor, sFilter))
1117 return ::rtl::OUString();
1115 ::rtl::OUString sFilter = aRequest.getFilter();
1116 if (!impl_validateAndSetFilterOnDescriptor(rDescriptor, sFilter))
1117 return ::rtl::OUString();
1118
1118
1119 ::rtl::OUString sType;
1120 rDescriptor[::comphelper::MediaDescriptor::PROP_TYPENAME()] >>= sType;
1121 return sType;
1122 }
1123 catch(const css::uno::Exception&)
1124 {}
1119 ::rtl::OUString sType;
1120 rDescriptor[::comphelper::MediaDescriptor::PROP_TYPENAME()] >>= sType;
1121 return sType;
1122 }
1123 catch(const css::uno::Exception&)
1124 {}
1125
1126 return ::rtl::OUString();
1125
1126 return ::rtl::OUString();
1127}
1128
1129/*-----------------------------------------------
1130 10.03.2004 10:30
1131-----------------------------------------------*/
1132void TypeDetection::impl_openStream(::comphelper::MediaDescriptor& rDescriptor)
1133 throw (css::uno::Exception)
1134{
1135 sal_Bool bSuccess = sal_False;
1136 ::rtl::OUString sURL = rDescriptor.getUnpackedValueOrDefault( ::comphelper::MediaDescriptor::PROP_URL(), ::rtl::OUString() );
1137 sal_Bool bRequestedReadOnly = rDescriptor.getUnpackedValueOrDefault( ::comphelper::MediaDescriptor::PROP_READONLY(), sal_False );
1138 if ( sURL.getLength() && ::utl::LocalFileHelper::IsLocalFile( INetURLObject( sURL ).GetMainURL( INetURLObject::NO_DECODE ) ) )
1139 {
1127}
1128
1129/*-----------------------------------------------
1130 10.03.2004 10:30
1131-----------------------------------------------*/
1132void TypeDetection::impl_openStream(::comphelper::MediaDescriptor& rDescriptor)
1133 throw (css::uno::Exception)
1134{
1135 sal_Bool bSuccess = sal_False;
1136 ::rtl::OUString sURL = rDescriptor.getUnpackedValueOrDefault( ::comphelper::MediaDescriptor::PROP_URL(), ::rtl::OUString() );
1137 sal_Bool bRequestedReadOnly = rDescriptor.getUnpackedValueOrDefault( ::comphelper::MediaDescriptor::PROP_READONLY(), sal_False );
1138 if ( sURL.getLength() && ::utl::LocalFileHelper::IsLocalFile( INetURLObject( sURL ).GetMainURL( INetURLObject::NO_DECODE ) ) )
1139 {
1140 // OOo uses own file locking mechanics in case of local file
1140 // AOO uses own file locking mechanics in case of local file
1141 bSuccess = rDescriptor.addInputStreamOwnLock();
1142 }
1143 else
1144 bSuccess = rDescriptor.addInputStream();
1145
1146 if ( !bSuccess )
1147 throw css::uno::Exception(_FILTER_CONFIG_FROM_ASCII_("Could not open stream."), static_cast< css::document::XTypeDetection* >(this));
1148

--- 104 unchanged lines hidden ---
1141 bSuccess = rDescriptor.addInputStreamOwnLock();
1142 }
1143 else
1144 bSuccess = rDescriptor.addInputStream();
1145
1146 if ( !bSuccess )
1147 throw css::uno::Exception(_FILTER_CONFIG_FROM_ASCII_("Could not open stream."), static_cast< css::document::XTypeDetection* >(this));
1148

--- 104 unchanged lines hidden ---