LibreOffice Module filter (master) 1
storagefilterdetect/filterdetect.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "filterdetect.hxx"
21
26#include <tools/urlobj.hxx>
28
29#include <com/sun/star/beans/XPropertySet.hpp>
30#include <com/sun/star/embed/XStorage.hpp>
31#include <com/sun/star/io/XInputStream.hpp>
32#include <com/sun/star/packages/zip/ZipIOException.hpp>
33#include <com/sun/star/task/XInteractionHandler.hpp>
34#include <utility>
35
36#include <comphelper/lok.hxx>
37
38using namespace ::com::sun::star;
40
41namespace {
42
43OUString getInternalFromMediaType(std::u16string_view aMediaType)
44{
45 // OpenDocument types
46 if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII ) return "writer8";
47 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII ) return "writer8_template";
48 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII ) return "writerweb8_writer_template";
49 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) return "writerglobal8";
50 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_TEMPLATE_ASCII ) return "writerglobal8_template";
51 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII ) return "draw8";
52 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII ) return "draw8_template";
53 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII ) return "impress8";
54 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII ) return "impress8_template";
55 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) return "calc8";
56 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) return "calc8_template";
57 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII ) return "chart8";
58 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII ) return "math8";
59 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII ) return "StarBaseReportChart";
60
61 // OOo legacy types
62 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_ASCII ) return "writer_StarOffice_XML_Writer";
63 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_TEMPLATE_ASCII ) return "writer_StarOffice_XML_Writer_Template";
64 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII ) return "writer_web_StarOffice_XML_Writer_Web_Template";
65 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII ) return "writer_globaldocument_StarOffice_XML_Writer_GlobalDocument";
66 else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_ASCII ) return "draw_StarOffice_XML_Draw";
67 else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_TEMPLATE_ASCII ) return "draw_StarOffice_XML_Draw_Template";
68 else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_ASCII ) return "impress_StarOffice_XML_Impress";
69 else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_TEMPLATE_ASCII ) return "impress_StarOffice_XML_Impress_Template";
70 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_ASCII ) return "calc_StarOffice_XML_Calc";
71 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_TEMPLATE_ASCII ) return "calc_StarOffice_XML_Calc_Template";
72 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CHART_ASCII ) return "chart_StarOffice_XML_Chart";
73 else if ( aMediaType == MIMETYPE_VND_SUN_XML_MATH_ASCII ) return "math_StarOffice_XML_Math";
74
75 // Unknown type
76 return OUString();
77}
78
79}
80
81StorageFilterDetect::StorageFilterDetect(uno::Reference<uno::XComponentContext> xCxt) :
82 mxCxt(std::move(xCxt)) {}
83
85
86OUString SAL_CALL StorageFilterDetect::detect(uno::Sequence<beans::PropertyValue>& rDescriptor)
87{
88 MediaDescriptor aMediaDesc( rDescriptor );
89 OUString aTypeName;
90
91 try
92 {
93 uno::Reference< io::XInputStream > xInStream( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM], uno::UNO_QUERY );
94 if ( !xInStream.is() )
95 return OUString();
96
97 uno::Reference< embed::XStorage > xStorage = ::comphelper::OStorageHelper::GetStorageFromInputStream( xInStream, mxCxt );
98 if ( !xStorage.is() )
99 return OUString();
100
101 uno::Reference< beans::XPropertySet > xStorageProperties( xStorage, uno::UNO_QUERY );
102 if ( !xStorageProperties.is() )
103 return OUString();
104
105 OUString aMediaType;
106 xStorageProperties->getPropertyValue( "MediaType" ) >>= aMediaType;
107 aTypeName = getInternalFromMediaType( aMediaType );
108 if (comphelper::LibreOfficeKit::isActive() && aTypeName == "draw8_template")
109 {
110 // save it as draw8 instead of template format
111 aTypeName = "draw8";
112 }
113 }
114
115 catch( const lang::WrappedTargetException& aWrap )
116 {
117 packages::zip::ZipIOException aZipException;
118 // We don't do any type detection on broken packages (f.e. because it might be impossible),
119 // so for repairing we'll use the requested type, which was detected by the flat detection.
120 OUString aRequestedTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME, OUString() );
121 if ( ( aWrap.TargetException >>= aZipException ) && !aRequestedTypeName.isEmpty() )
122 {
123 // The package is a broken one.
124 uno::Reference< task::XInteractionHandler > xInteraction =
125 aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INTERACTIONHANDLER, uno::Reference< task::XInteractionHandler >() );
126
127 if ( xInteraction.is() )
128 {
129 INetURLObject aParser( aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL, OUString() ) );
130 OUString aDocumentTitle = aParser.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset );
131 bool bRepairPackage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
132 // fdo#46310 Don't try to repair if the user rejected it once.
133 bool bRepairAllowed = aMediaDesc.getUnpackedValueOrDefault( "RepairAllowed", true );
134
135 if ( !bRepairPackage && bRepairAllowed )
136 {
137 // Ask the user whether he wants to try to repair.
138 RequestPackageReparation aRequest( aDocumentTitle );
139 xInteraction->handle( aRequest.GetRequest() );
140
141 if ( aRequest.isApproved() )
142 {
143 aTypeName = aRequestedTypeName;
144 aMediaDesc[MediaDescriptor::PROP_DOCUMENTTITLE] <<= aDocumentTitle;
145 aMediaDesc[MediaDescriptor::PROP_ASTEMPLATE] <<= true;
146 aMediaDesc["RepairPackage"] <<= true;
147 }
148 else
149 {
150 // Repair either not allowed or not successful.
151 NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
152 xInteraction->handle( aNotifyRequest.GetRequest() );
153 aMediaDesc["RepairAllowed"] <<= false;
154 }
155
156 // Write the changes back.
157 aMediaDesc >> rDescriptor;
158 }
159 }
160 }
161 }
162 catch( uno::RuntimeException& )
163 {
164 throw;
165 }
166 catch( uno::Exception& )
167 {}
168
169 return aTypeName;
170}
171
172// XInitialization
173void SAL_CALL StorageFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/) {}
174
175// XServiceInfo
177{
178 return "com.sun.star.comp.filters.StorageFilterDetect";
179}
180
181sal_Bool SAL_CALL StorageFilterDetect::supportsService(const OUString& rServiceName)
182{
183 return cppu::supportsService(this, rServiceName);
184}
185
186uno::Sequence<OUString> SAL_CALL StorageFilterDetect::getSupportedServiceNames()
187{
188 return { "com.sun.star.document.ExtendedTypeDetection", "com.sun.star.comp.filters.StorageFilterDetect" };
189}
190
191
192extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
194 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
195{
196 return cppu::acquire(new StorageFilterDetect(context));
197}
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
css::uno::Reference< css::task::XInteractionRequest > GetRequest() const
css::uno::Reference< css::task::XInteractionRequest > GetRequest() const
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &rDescriptor) override
StorageFilterDetect(css::uno::Reference< css::uno::XComponentContext > xCxt)
css::uno::Reference< css::uno::XComponentContext > mxCxt
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
static css::uno::Reference< css::embed::XStorage > GetStorageFromInputStream(const css::uno::Reference< css::io::XInputStream > &xStream, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
Shape IDs per cluster in DGG atom.
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * filter_StorageFilterDetect_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool