nss: upgrade to release 3.73
[LibreOffice.git] / filter / source / storagefilterdetect / filterdetect.cxx
blobf2eba546a4d3b931dcee5d1db9c070499c0c55f4
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "filterdetect.hxx"
22 #include <comphelper/documentconstants.hxx>
23 #include <comphelper/storagehelper.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <unotools/mediadescriptor.hxx>
26 #include <tools/urlobj.hxx>
27 #include <sfx2/brokenpackageint.hxx>
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>
35 using namespace ::com::sun::star;
36 using utl::MediaDescriptor;
38 namespace {
40 OUString getInternalFromMediaType(const OUString& aMediaType)
42 // OpenDocument types
43 if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII ) return "writer8";
44 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII ) return "writer8_template";
45 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII ) return "writerweb8_writer_template";
46 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) return "writerglobal8";
47 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_TEMPLATE_ASCII ) return "writerglobal8_template";
48 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII ) return "draw8";
49 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII ) return "draw8_template";
50 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII ) return "impress8";
51 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII ) return "impress8_template";
52 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) return "calc8";
53 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) return "calc8_template";
54 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII ) return "chart8";
55 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII ) return "math8";
56 else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII ) return "StarBaseReportChart";
58 // OOo legacy types
59 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_ASCII ) return "writer_StarOffice_XML_Writer";
60 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_TEMPLATE_ASCII ) return "writer_StarOffice_XML_Writer_Template";
61 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII ) return "writer_web_StarOffice_XML_Writer_Web_Template";
62 else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII ) return "writer_globaldocument_StarOffice_XML_Writer_GlobalDocument";
63 else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_ASCII ) return "draw_StarOffice_XML_Draw";
64 else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_TEMPLATE_ASCII ) return "draw_StarOffice_XML_Draw_Template";
65 else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_ASCII ) return "impress_StarOffice_XML_Impress";
66 else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_TEMPLATE_ASCII ) return "impress_StarOffice_XML_Impress_Template";
67 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_ASCII ) return "calc_StarOffice_XML_Calc";
68 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_TEMPLATE_ASCII ) return "calc_StarOffice_XML_Calc_Template";
69 else if ( aMediaType == MIMETYPE_VND_SUN_XML_CHART_ASCII ) return "chart_StarOffice_XML_Chart";
70 else if ( aMediaType == MIMETYPE_VND_SUN_XML_MATH_ASCII ) return "math_StarOffice_XML_Math";
72 // Unknown type
73 return OUString();
78 StorageFilterDetect::StorageFilterDetect(const uno::Reference<uno::XComponentContext>& xCxt) :
79 mxCxt(xCxt) {}
81 StorageFilterDetect::~StorageFilterDetect() {}
83 OUString SAL_CALL StorageFilterDetect::detect(uno::Sequence<beans::PropertyValue>& rDescriptor)
85 MediaDescriptor aMediaDesc( rDescriptor );
86 OUString aTypeName;
88 try
90 uno::Reference< io::XInputStream > xInStream( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
91 if ( !xInStream.is() )
92 return OUString();
94 uno::Reference< embed::XStorage > xStorage = ::comphelper::OStorageHelper::GetStorageFromInputStream( xInStream, mxCxt );
95 if ( !xStorage.is() )
96 return OUString();
98 uno::Reference< beans::XPropertySet > xStorageProperties( xStorage, uno::UNO_QUERY );
99 if ( !xStorageProperties.is() )
100 return OUString();
102 OUString aMediaType;
103 xStorageProperties->getPropertyValue( "MediaType" ) >>= aMediaType;
104 aTypeName = getInternalFromMediaType( aMediaType );
107 catch( const lang::WrappedTargetException& aWrap )
109 packages::zip::ZipIOException aZipException;
110 // We don't do any type detection on broken packages (f.e. because it might be impossible),
111 // so for repairing we'll use the requested type, which was detected by the flat detection.
112 OUString aRequestedTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
113 if ( ( aWrap.TargetException >>= aZipException ) && !aRequestedTypeName.isEmpty() )
115 // The package is a broken one.
116 uno::Reference< task::XInteractionHandler > xInteraction =
117 aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INTERACTIONHANDLER(), uno::Reference< task::XInteractionHandler >() );
119 if ( xInteraction.is() )
121 INetURLObject aParser( aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() ) );
122 OUString aDocumentTitle = aParser.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset );
123 bool bRepairPackage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
124 // fdo#46310 Don't try to repair if the user rejected it once.
125 bool bRepairAllowed = aMediaDesc.getUnpackedValueOrDefault( "RepairAllowed", true );
127 if ( !bRepairPackage && bRepairAllowed )
129 // Ask the user whether he wants to try to repair.
130 RequestPackageReparation aRequest( aDocumentTitle );
131 xInteraction->handle( aRequest.GetRequest() );
133 if ( aRequest.isApproved() )
135 aTypeName = aRequestedTypeName;
136 aMediaDesc[MediaDescriptor::PROP_DOCUMENTTITLE()] <<= aDocumentTitle;
137 aMediaDesc[MediaDescriptor::PROP_ASTEMPLATE()] <<= true;
138 aMediaDesc["RepairPackage"] <<= true;
140 else
142 // Repair either not allowed or not successful.
143 NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
144 xInteraction->handle( aNotifyRequest.GetRequest() );
145 aMediaDesc["RepairAllowed"] <<= false;
148 // Write the changes back.
149 aMediaDesc >> rDescriptor;
154 catch( uno::RuntimeException& )
156 throw;
158 catch( uno::Exception& )
161 return aTypeName;
164 // XInitialization
165 void SAL_CALL StorageFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/) {}
167 // XServiceInfo
168 OUString SAL_CALL StorageFilterDetect::getImplementationName()
170 return "com.sun.star.comp.filters.StorageFilterDetect";
173 sal_Bool SAL_CALL StorageFilterDetect::supportsService(const OUString& rServiceName)
175 return cppu::supportsService(this, rServiceName);
178 uno::Sequence<OUString> SAL_CALL StorageFilterDetect::getSupportedServiceNames()
180 return { "com.sun.star.document.ExtendedTypeDetection", "com.sun.star.comp.filters.StorageFilterDetect" };
184 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
185 filter_StorageFilterDetect_get_implementation(
186 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
188 return cppu::acquire(new StorageFilterDetect(context));
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */