Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / exceldetect.cxx
blob5440c621ebdb6cd2d82f204d9aceaaecbd3d5ab9
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>
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)
40 if (*pArray == aName)
41 return true;
43 return false;
46 uno::Sequence<OUString> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException)
48 return impl_getStaticSupportedServiceNames();
51 namespace {
53 bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
55 SfxMedium aMedium;
56 aMedium.UseInteractionHandler(false);
57 aMedium.setStreamToLoadFrom(xInStream, true);
58 SvStream* pStream = aMedium.GetInStream();
59 if (!pStream)
60 return false;
62 pStream->Seek(STREAM_SEEK_TO_END);
63 sal_Size nSize = pStream->Tell();
64 pStream->Seek(0);
66 if (!nSize)
67 // 0-size stream. Failed.
68 return false;
70 SotStorageRef xStorage = new SotStorage(pStream, false);
71 if (!xStorage.Is() || xStorage->GetError())
72 return false;
74 return xStorage->IsStream(rName);
77 /**
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)
83 SfxMedium aMedium;
84 aMedium.UseInteractionHandler(false);
85 aMedium.setStreamToLoadFrom(xInStream, true);
86 SvStream* pStream = aMedium.GetInStream();
87 if (!pStream)
88 return false;
90 pStream->Seek(STREAM_SEEK_TO_END);
91 sal_Size nSize = pStream->Tell();
92 pStream->Seek(0);
94 if (nSize < 4)
95 return false;
97 sal_uInt16 nBofId, nBofSize;
98 *pStream >> nBofId >> nBofSize;
100 switch (nBofId)
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)
106 break;
107 default:
108 return false;
111 if (nBofSize < 4 || 16 < nBofSize)
112 // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
113 return false;
115 sal_Size nPos = pStream->Tell();
116 if (nSize - nPos < nBofSize)
117 // BOF record doesn't have required bytes.
118 return false;
120 return true;
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);
134 OUString aType;
135 aMediaDesc[MediaDescriptor::PROP_TYPENAME()] >>= aType;
136 if (aType.isEmpty())
137 // Type is not given. We can't proceed.
138 return OUString();
140 aMediaDesc.addInputStream();
141 uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY);
142 if (!xInStream.is())
143 // No input stream.
144 return OUString();
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".
151 return OUString();
153 aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
154 return aType;
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"))
161 return OUString();
163 aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
164 return aType;
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"))
171 return OUString();
173 aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
174 return aType;
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))
181 return OUString();
183 aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
184 return aType;
187 // failed!
188 return OUString();
191 uno::Sequence<OUString> ScExcelBiffDetect::impl_getStaticSupportedServiceNames()
193 uno::Sequence<OUString> aNames(1);
194 aNames[0] = "com.sun.star.frame.ExtendedTypeDetection";
195 return aNames;
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: */