1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include "exceldetect.hxx"
12 #include <com/sun/star/io/XInputStream.hpp>
13 #include <com/sun/star/ucb/XContent.hpp>
14 #include <cppuhelper/supportsservice.hxx>
16 #include <svl/itemset.hxx>
17 #include <svl/eitem.hxx>
18 #include <sfx2/app.hxx>
19 #include <sfx2/docfile.hxx>
20 #include <sfx2/sfxsids.hrc>
21 #include <unotools/mediadescriptor.hxx>
22 #include <sot/storage.hxx>
24 using namespace com::sun::star
;
25 using utl::MediaDescriptor
;
27 ScExcelBiffDetect::ScExcelBiffDetect( const uno::Reference
<uno::XComponentContext
>& /*xContext*/ ) {}
28 ScExcelBiffDetect::~ScExcelBiffDetect() {}
30 OUString
ScExcelBiffDetect::getImplementationName() throw (uno::RuntimeException
, std::exception
)
32 return OUString("com.sun.star.comp.calc.ExcelBiffFormatDetector");
35 sal_Bool
ScExcelBiffDetect::supportsService( const OUString
& aName
) throw (uno::RuntimeException
, std::exception
)
37 return cppu::supportsService(this, aName
);
40 uno::Sequence
<OUString
> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException
, std::exception
)
42 uno::Sequence
<OUString
> aNames(1);
43 aNames
[0] = "com.sun.star.frame.ExtendedTypeDetection";
49 bool hasStream(const uno::Reference
<io::XInputStream
>& xInStream
, const OUString
& rName
)
52 aMedium
.UseInteractionHandler(false);
53 aMedium
.setStreamToLoadFrom(xInStream
, true);
54 SvStream
* pStream
= aMedium
.GetInStream();
58 pStream
->Seek(STREAM_SEEK_TO_END
);
59 sal_Size nSize
= pStream
->Tell();
64 // 0-size stream. Failed.
70 tools::SvRef
<SotStorage
> xStorage
= new SotStorage(pStream
, false);
71 if (!xStorage
.Is() || xStorage
->GetError())
73 return xStorage
->IsStream(rName
);
75 catch (const css::ucb::ContentCreationException
&e
)
77 SAL_WARN("sc", "hasStream caught " << e
.Message
);
84 * We detect BIFF 2, 3 and 4 file types together since the only thing that
85 * set them apart is the BOF ID.
87 bool isExcel40(const uno::Reference
<io::XInputStream
>& xInStream
)
90 aMedium
.UseInteractionHandler(false);
91 aMedium
.setStreamToLoadFrom(xInStream
, true);
92 SvStream
* pStream
= aMedium
.GetInStream();
96 pStream
->Seek(STREAM_SEEK_TO_END
);
97 sal_Size nSize
= pStream
->Tell();
103 sal_uInt16 nBofId
, nBofSize
;
104 pStream
->ReadUInt16( nBofId
).ReadUInt16( nBofSize
);
108 case 0x0009: // Excel 2.1 worksheet (BIFF 2)
109 case 0x0209: // Excel 3.0 worksheet (BIFF 3)
110 case 0x0409: // Excel 4.0 worksheet (BIFF 4)
111 case 0x0809: // Excel 5.0 worksheet (BIFF 5), some apps create such files (fdo#70100)
117 if (nBofSize
< 4 || 16 < nBofSize
)
118 // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
121 sal_Size nPos
= pStream
->Tell();
122 if (nSize
- nPos
< nBofSize
)
123 // BOF record doesn't have required bytes.
129 bool isTemplate(const OUString
& rType
)
131 return rType
.indexOf("_VorlageTemplate") != -1;
136 OUString
ScExcelBiffDetect::detect( uno::Sequence
<beans::PropertyValue
>& lDescriptor
)
137 throw (uno::RuntimeException
, std::exception
)
139 MediaDescriptor
aMediaDesc(lDescriptor
);
141 aMediaDesc
[MediaDescriptor::PROP_TYPENAME()] >>= aType
;
143 // Type is not given. We can't proceed.
146 aMediaDesc
.addInputStream();
147 uno::Reference
<io::XInputStream
> xInStream(aMediaDesc
[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY
);
152 if (aType
== "calc_MS_Excel_97" || aType
== "calc_MS_Excel_97_VorlageTemplate")
154 // See if this stream is a Excel 97/XP/2003 (BIFF8) stream.
155 if (!hasStream(xInStream
, "Workbook"))
156 // BIFF8 is expected to contain a stream named "Workbook".
159 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
162 else if (aType
== "calc_MS_Excel_95" || aType
== "calc_MS_Excel_95_VorlageTemplate")
164 // See if this stream is a Excel 95 (BIFF5) stream.
165 if (!hasStream(xInStream
, "Book"))
168 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
171 else if (aType
== "calc_MS_Excel_5095" || aType
== "calc_MS_Excel_5095_VorlageTemplate")
173 // See if this stream is a Excel 5.0/95 stream.
174 if (!hasStream(xInStream
, "Book"))
177 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
180 else if (aType
== "calc_MS_Excel_40" || aType
== "calc_MS_Excel_40_VorlageTemplate")
182 // See if this stream is a Excel 4.0 stream.
183 if (!isExcel40(xInStream
))
186 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
190 // Nothing to detect.
193 aMediaDesc
>> lDescriptor
;
197 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
198 com_sun_star_comp_calc_ExcelBiffFormatDetector_get_implementation(::com::sun::star::uno::XComponentContext
* context
,
199 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
201 return cppu::acquire(new ScExcelBiffDetect(context
));
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */