fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / unoobj / exceldetect.cxx
blobc22f8d312fa8ca7ed2a555a0e19177ac6e2c3b30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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";
44 return aNames;
47 namespace {
49 bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
51 SfxMedium aMedium;
52 aMedium.UseInteractionHandler(false);
53 aMedium.setStreamToLoadFrom(xInStream, true);
54 SvStream* pStream = aMedium.GetInStream();
55 if (!pStream)
56 return false;
58 pStream->Seek(STREAM_SEEK_TO_END);
59 sal_Size nSize = pStream->Tell();
60 pStream->Seek(0);
62 if (!nSize)
64 // 0-size stream. Failed.
65 return false;
68 try
70 tools::SvRef<SotStorage> xStorage = new SotStorage(pStream, false);
71 if (!xStorage.Is() || xStorage->GetError())
72 return false;
73 return xStorage->IsStream(rName);
75 catch (const css::ucb::ContentCreationException &e)
77 SAL_WARN("sc", "hasStream caught " << e.Message);
80 return false;
83 /**
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)
89 SfxMedium aMedium;
90 aMedium.UseInteractionHandler(false);
91 aMedium.setStreamToLoadFrom(xInStream, true);
92 SvStream* pStream = aMedium.GetInStream();
93 if (!pStream)
94 return false;
96 pStream->Seek(STREAM_SEEK_TO_END);
97 sal_Size nSize = pStream->Tell();
98 pStream->Seek(0);
100 if (nSize < 4)
101 return false;
103 sal_uInt16 nBofId, nBofSize;
104 pStream->ReadUInt16( nBofId ).ReadUInt16( nBofSize );
106 switch (nBofId)
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)
112 break;
113 default:
114 return false;
117 if (nBofSize < 4 || 16 < nBofSize)
118 // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
119 return false;
121 sal_Size nPos = pStream->Tell();
122 if (nSize - nPos < nBofSize)
123 // BOF record doesn't have required bytes.
124 return false;
126 return true;
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);
140 OUString aType;
141 aMediaDesc[MediaDescriptor::PROP_TYPENAME()] >>= aType;
142 if (aType.isEmpty())
143 // Type is not given. We can't proceed.
144 return OUString();
146 aMediaDesc.addInputStream();
147 uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY);
148 if (!xInStream.is())
149 // No input stream.
150 return OUString();
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".
157 return OUString();
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"))
166 return OUString();
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"))
175 return OUString();
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))
184 return OUString();
186 aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
189 else
190 // Nothing to detect.
191 return OUString();
193 aMediaDesc >> lDescriptor;
194 return aType;
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: */