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 : * 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 <com/sun/star/document/XExtendedFilterDetection.hpp>
21 : #include <com/sun/star/lang/XServiceInfo.hpp>
22 : #include <comphelper/storagehelper.hxx>
23 : #include <cppuhelper/implbase.hxx>
24 : #include <cppuhelper/supportsservice.hxx>
25 : #include <sot/storage.hxx>
26 :
27 : using namespace ::com::sun::star;
28 :
29 : /// File format detection service for DOCX.
30 : class WriterFilterDetection : public cppu::WeakImplHelper
31 : <
32 : document::XExtendedFilterDetection,
33 : lang::XServiceInfo
34 : >
35 : {
36 : uno::Reference<uno::XComponentContext> m_xContext;
37 :
38 : public:
39 : explicit WriterFilterDetection(const uno::Reference<uno::XComponentContext>& rxContext);
40 : virtual ~WriterFilterDetection();
41 :
42 : //XExtendedFilterDetection
43 : virtual OUString SAL_CALL detect(uno::Sequence<beans::PropertyValue>& Descriptor) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
44 :
45 : // XServiceInfo
46 : virtual OUString SAL_CALL getImplementationName() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
47 : virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
48 : virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
49 : };
50 :
51 : uno::Sequence<OUString> SAL_CALL WriterFilterDetection_getSupportedServiceNames() throw (uno::RuntimeException);
52 :
53 1 : WriterFilterDetection::WriterFilterDetection(const uno::Reference<uno::XComponentContext>& rxContext)
54 1 : : m_xContext(rxContext)
55 : {
56 1 : }
57 :
58 2 : WriterFilterDetection::~WriterFilterDetection()
59 : {
60 2 : }
61 :
62 0 : OUString WriterFilterDetection::detect(uno::Sequence<beans::PropertyValue>& rDescriptor) throw (uno::RuntimeException, std::exception)
63 : {
64 0 : OUString sTypeName;
65 0 : bool bWord = false;
66 0 : sal_Int32 nPropertyCount = rDescriptor.getLength();
67 0 : const beans::PropertyValue* pValues = rDescriptor.getConstArray();
68 0 : OUString sURL;
69 0 : uno::Reference<io::XStream> xStream;
70 0 : uno::Reference<io::XInputStream> xInputStream;
71 0 : for (sal_Int32 nProperty = 0; nProperty < nPropertyCount; ++nProperty)
72 : {
73 0 : if (pValues[nProperty].Name == "TypeName")
74 0 : rDescriptor[nProperty].Value >>= sTypeName;
75 0 : else if (pValues[nProperty].Name == "URL")
76 0 : pValues[nProperty].Value >>= sURL;
77 0 : else if (pValues[nProperty].Name == "Stream")
78 0 : pValues[nProperty].Value >>= xStream;
79 0 : else if (pValues[nProperty].Name == "InputStream")
80 0 : pValues[nProperty].Value >>= xInputStream;
81 : }
82 : try
83 : {
84 0 : uno::Reference<embed::XStorage> xDocStorage;
85 0 : if (sURL == "private:stream")
86 0 : xDocStorage = comphelper::OStorageHelper::GetStorageFromInputStream(xInputStream);
87 : else
88 0 : xDocStorage = comphelper::OStorageHelper::GetStorageFromURL(sURL, embed::ElementModes::READ);
89 0 : if (xDocStorage.is())
90 : {
91 0 : uno::Sequence<OUString> aNames = xDocStorage->getElementNames();
92 0 : const OUString* pNames = aNames.getConstArray();
93 0 : for (sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
94 : {
95 0 : if (pNames[nName] == "word")
96 : {
97 0 : bWord = true;
98 0 : if (sTypeName.isEmpty())
99 0 : sTypeName = "writer_MS_Word_2007";
100 0 : break;
101 : }
102 0 : }
103 0 : }
104 : }
105 0 : catch (const uno::Exception&)
106 : {
107 : SAL_WARN("writerfilter", "exception while opening storage");
108 : }
109 0 : if (!bWord)
110 0 : sTypeName.clear();
111 0 : return sTypeName;
112 : }
113 :
114 1 : uno::Sequence<OUString> WriterFilterDetection_getSupportedServiceNames() throw (uno::RuntimeException)
115 : {
116 : uno::Sequence<OUString> aRet =
117 : {
118 : OUString("com.sun.star.document.ExtendedTypeDetection")
119 1 : };
120 1 : return aRet;
121 : }
122 :
123 1 : OUString WriterFilterDetection::getImplementationName() throw (uno::RuntimeException, std::exception)
124 : {
125 1 : return OUString("com.sun.star.comp.Writer.WriterFilterDetector");
126 : }
127 :
128 0 : sal_Bool WriterFilterDetection::supportsService(const OUString& rServiceName) throw (uno::RuntimeException, std::exception)
129 : {
130 0 : return cppu::supportsService(this, rServiceName);
131 : }
132 :
133 1 : uno::Sequence<OUString> WriterFilterDetection::getSupportedServiceNames() throw (uno::RuntimeException, std::exception)
134 : {
135 1 : return WriterFilterDetection_getSupportedServiceNames();
136 : }
137 :
138 1 : extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* SAL_CALL com_sun_star_comp_Writer_WriterFilterDetector_get_implementation(uno::XComponentContext* pComp, uno::Sequence<css::uno::Any> const&)
139 : {
140 1 : return cppu::acquire(new WriterFilterDetection(pComp));
141 : }
142 :
143 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|