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>
15 #include "svl/itemset.hxx"
16 #include "svl/eitem.hxx"
17 #include "sfx2/app.hxx"
18 #include "sfx2/docfile.hxx"
19 #include "sfx2/sfxsids.hrc"
20 #include "unotools/mediadescriptor.hxx"
21 #include "sot/storage.hxx"
23 using namespace com::sun::star
;
24 using utl::MediaDescriptor
;
26 ScExcelBiffDetect::ScExcelBiffDetect( const uno::Reference
<uno::XComponentContext
>& /*xContext*/ ) {}
27 ScExcelBiffDetect::~ScExcelBiffDetect() {}
29 OUString
ScExcelBiffDetect::getImplementationName() throw (uno::RuntimeException
)
31 return impl_getStaticImplementationName();
34 sal_Bool
ScExcelBiffDetect::supportsService( const OUString
& aName
) throw (uno::RuntimeException
)
36 uno::Sequence
<OUString
> aSrvNames
= getSupportedServiceNames();
37 const OUString
* pArray
= aSrvNames
.getConstArray();
38 for (sal_Int32 i
= 0; i
< aSrvNames
.getLength(); ++i
, ++pArray
)
46 uno::Sequence
<OUString
> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException
)
48 return impl_getStaticSupportedServiceNames();
53 bool hasStream(const uno::Reference
<io::XInputStream
>& xInStream
, const OUString
& rName
)
56 aMedium
.UseInteractionHandler(false);
57 aMedium
.setStreamToLoadFrom(xInStream
, true);
58 SvStream
* pStream
= aMedium
.GetInStream();
62 pStream
->Seek(STREAM_SEEK_TO_END
);
63 sal_Size nSize
= pStream
->Tell();
67 // 0-size stream. Failed.
70 SotStorageRef xStorage
= new SotStorage(pStream
, false);
71 if (!xStorage
.Is() || xStorage
->GetError())
74 return xStorage
->IsStream(rName
);
78 * We detect BIFF 2, 3 and 4 file types together since the only thing that
79 * set them apart is the BOF ID.
81 bool isExcel40(const uno::Reference
<io::XInputStream
>& xInStream
)
84 aMedium
.UseInteractionHandler(false);
85 aMedium
.setStreamToLoadFrom(xInStream
, true);
86 SvStream
* pStream
= aMedium
.GetInStream();
90 pStream
->Seek(STREAM_SEEK_TO_END
);
91 sal_Size nSize
= pStream
->Tell();
97 sal_uInt16 nBofId
, nBofSize
;
98 *pStream
>> nBofId
>> nBofSize
;
102 case 0x0009: // Excel 2.1 worksheet (BIFF 2)
103 case 0x0209: // Excel 3.0 worksheet (BIFF 3)
104 case 0x0409: // Excel 4.0 worksheet (BIFF 4)
105 case 0x0809: // Excel 5.0 worksheet (BIFF 5), some apps create such files (fdo#70100)
111 if (nBofSize
< 4 || 16 < nBofSize
)
112 // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
115 sal_Size nPos
= pStream
->Tell();
116 if (nSize
- nPos
< nBofSize
)
117 // BOF record doesn't have required bytes.
123 bool isTemplate(const OUString
& rType
)
125 return rType
.indexOf("_VorlageTemplate") != -1;
130 OUString
ScExcelBiffDetect::detect( uno::Sequence
<beans::PropertyValue
>& lDescriptor
)
131 throw (uno::RuntimeException
)
133 MediaDescriptor
aMediaDesc(lDescriptor
);
135 aMediaDesc
[MediaDescriptor::PROP_TYPENAME()] >>= aType
;
137 // Type is not given. We can't proceed.
140 aMediaDesc
.addInputStream();
141 uno::Reference
<io::XInputStream
> xInStream(aMediaDesc
[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY
);
146 if (aType
== "calc_MS_Excel_97" || aType
== "calc_MS_Excel_97_VorlageTemplate")
148 // See if this stream is a Excel 97/XP/2003 (BIFF8) stream.
149 if (!hasStream(xInStream
, "Workbook"))
150 // BIFF8 is expected to contain a stream named "Workbook".
153 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
157 if (aType
== "calc_MS_Excel_95" || aType
== "calc_MS_Excel_95_VorlageTemplate")
159 // See if this stream is a Excel 95 (BIFF5) stream.
160 if (!hasStream(xInStream
, "Book"))
163 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
167 if (aType
== "calc_MS_Excel_5095" || aType
== "calc_MS_Excel_5095_VorlageTemplate")
169 // See if this stream is a Excel 5.0/95 stream.
170 if (!hasStream(xInStream
, "Book"))
173 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
177 if (aType
== "calc_MS_Excel_40" || aType
== "calc_MS_Excel_40_VorlageTemplate")
179 // See if this stream is a Excel 4.0 stream.
180 if (!isExcel40(xInStream
))
183 aMediaDesc
[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType
) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
191 uno::Sequence
<OUString
> ScExcelBiffDetect::impl_getStaticSupportedServiceNames()
193 uno::Sequence
<OUString
> aNames(1);
194 aNames
[0] = "com.sun.star.frame.ExtendedTypeDetection";
198 OUString
ScExcelBiffDetect::impl_getStaticImplementationName()
200 return OUString("com.sun.star.comp.calc.ExcelBiffFormatDetector");
203 uno::Reference
<uno::XInterface
> ScExcelBiffDetect::impl_createInstance(
204 const uno::Reference
<uno::XComponentContext
>& xContext
)
205 throw (com::sun::star::uno::Exception
)
207 return static_cast<cppu::OWeakObject
*>(new ScExcelBiffDetect(xContext
));
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */