Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include "exceldetect.hxx"
11 :
12 : #include <com/sun/star/io/XInputStream.hpp>
13 : #include <com/sun/star/ucb/XContent.hpp>
14 :
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 "comphelper/mediadescriptor.hxx"
21 : #include "sot/storage.hxx"
22 :
23 : using namespace com::sun::star;
24 : using comphelper::MediaDescriptor;
25 :
26 24 : ScExcelBiffDetect::ScExcelBiffDetect( const uno::Reference<uno::XComponentContext>& /*xContext*/ ) {}
27 48 : ScExcelBiffDetect::~ScExcelBiffDetect() {}
28 :
29 0 : OUString ScExcelBiffDetect::getImplementationName() throw (uno::RuntimeException)
30 : {
31 0 : return impl_getStaticImplementationName();
32 : }
33 :
34 0 : sal_Bool ScExcelBiffDetect::supportsService( const OUString& aName ) throw (uno::RuntimeException)
35 : {
36 0 : uno::Sequence<OUString> aSrvNames = getSupportedServiceNames();
37 0 : const OUString* pArray = aSrvNames.getConstArray();
38 0 : for (sal_Int32 i = 0; i < aSrvNames.getLength(); ++i, ++pArray)
39 : {
40 0 : if (*pArray == aName)
41 0 : return true;
42 : }
43 0 : return false;
44 : }
45 :
46 0 : uno::Sequence<OUString> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException)
47 : {
48 0 : return impl_getStaticSupportedServiceNames();
49 : }
50 :
51 : namespace {
52 :
53 24 : bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
54 : {
55 24 : SfxMedium aMedium;
56 24 : aMedium.UseInteractionHandler(false);
57 24 : aMedium.setStreamToLoadFrom(xInStream, true);
58 24 : SvStream* pStream = aMedium.GetInStream();
59 24 : if (!pStream)
60 0 : return false;
61 :
62 24 : pStream->Seek(STREAM_SEEK_TO_END);
63 24 : sal_Size nSize = pStream->Tell();
64 24 : pStream->Seek(0);
65 :
66 24 : if (!nSize)
67 : // 0-size stream. Failed.
68 0 : return false;
69 :
70 48 : SotStorageRef xStorage = new SotStorage(pStream, false);
71 24 : if (!xStorage.Is() || xStorage->GetError())
72 0 : return false;
73 :
74 48 : return xStorage->IsStream(rName);
75 : }
76 :
77 0 : bool isExcel40(const uno::Reference<io::XInputStream>& xInStream)
78 : {
79 0 : SfxMedium aMedium;
80 0 : aMedium.UseInteractionHandler(false);
81 0 : aMedium.setStreamToLoadFrom(xInStream, true);
82 0 : SvStream* pStream = aMedium.GetInStream();
83 0 : if (!pStream)
84 0 : return false;
85 :
86 0 : pStream->Seek(STREAM_SEEK_TO_END);
87 0 : sal_Size nSize = pStream->Tell();
88 0 : pStream->Seek(0);
89 :
90 0 : if (nSize < 4)
91 0 : return false;
92 :
93 : sal_uInt16 nBofId, nBofSize;
94 0 : *pStream >> nBofId >> nBofSize;
95 :
96 0 : if (nBofId != 0x0409)
97 : // This ID signifies Excel 4.0 format. It must be 0x0409.
98 0 : return false;
99 :
100 0 : if (nBofSize < 4 || 16 < nBofSize)
101 : // BOF record must be sized between 4 and 16 for Excel 4.0 stream.
102 0 : return false;
103 :
104 0 : sal_Size nPos = pStream->Tell();
105 0 : if (nSize - nPos < nBofSize)
106 : // BOF record doesn't have required bytes.
107 0 : return false;
108 :
109 0 : return true;
110 : }
111 :
112 24 : bool isTemplate(const OUString& rType)
113 : {
114 24 : return rType.indexOf("_VorlageTemplate") != -1;
115 : }
116 :
117 : }
118 :
119 24 : OUString ScExcelBiffDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
120 : throw (uno::RuntimeException)
121 : {
122 24 : MediaDescriptor aMediaDesc(lDescriptor);
123 48 : OUString aType;
124 24 : aMediaDesc[MediaDescriptor::PROP_TYPENAME()] >>= aType;
125 24 : if (aType.isEmpty())
126 : // Type is not given. We can't proceed.
127 0 : return OUString();
128 :
129 24 : aMediaDesc.addInputStream();
130 48 : uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY);
131 24 : if (!xInStream.is())
132 : // No input stream.
133 0 : return OUString();
134 :
135 24 : if (aType == "calc_MS_Excel_97" || aType == "calc_MS_Excel_97_VorlageTemplate")
136 : {
137 : // See if this stream is a Excel 97/XP/2003 (BIFF8) stream.
138 24 : if (!hasStream(xInStream, "Workbook"))
139 : // BIFF8 is expected to contain a stream named "Workbook".
140 0 : return OUString();
141 :
142 24 : aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
143 24 : return aType;
144 : }
145 :
146 0 : if (aType == "calc_MS_Excel_95" || aType == "calc_MS_Excel_95_VorlageTemplate")
147 : {
148 : // See if this stream is a Excel 95 (BIFF5) stream.
149 0 : if (!hasStream(xInStream, "Book"))
150 0 : return OUString();
151 :
152 0 : aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
153 0 : return aType;
154 : }
155 :
156 0 : if (aType == "calc_MS_Excel_5095" || aType == "calc_MS_Excel_5095_VorlageTemplate")
157 : {
158 : // See if this stream is a Excel 5.0/95 stream.
159 0 : if (!hasStream(xInStream, "Book"))
160 0 : return OUString();
161 :
162 0 : aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
163 0 : return aType;
164 : }
165 :
166 0 : if (aType == "calc_MS_Excel_40" || aType == "calc_MS_Excel_40_VorlageTemplate")
167 : {
168 : // See if this stream is a Excel 4.0 stream.
169 0 : if (!isExcel40(xInStream))
170 0 : return OUString();
171 :
172 0 : aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
173 0 : return aType;
174 : }
175 :
176 : // failed!
177 24 : return OUString();
178 : }
179 :
180 1 : uno::Sequence<OUString> ScExcelBiffDetect::impl_getStaticSupportedServiceNames()
181 : {
182 1 : uno::Sequence<OUString> aNames(1);
183 1 : aNames[0] = "com.sun.star.frame.ExtendedTypeDetection";
184 1 : return aNames;
185 : }
186 :
187 18 : OUString ScExcelBiffDetect::impl_getStaticImplementationName()
188 : {
189 18 : return OUString("com.sun.star.comp.calc.ExcelBiffFormatDetector");
190 : }
191 :
192 24 : uno::Reference<uno::XInterface> ScExcelBiffDetect::impl_createInstance(
193 : const uno::Reference<uno::XComponentContext>& xContext )
194 : throw (com::sun::star::uno::Exception)
195 : {
196 24 : return static_cast<cppu::OWeakObject*>(new ScExcelBiffDetect(xContext));
197 51 : }
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|