diff options
author | David Tardon <dtardon@redhat.com> | 2016-12-02 15:01:17 +0100 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2016-12-05 10:41:54 +0100 |
commit | e60a60f570ec04eb1c0f94fe43675400f71c786f (patch) | |
tree | 7d8f2f74e0d400c3dfcb1bdd012477e04626b1a4 /writerperfect/qa/unit/WpftLoader.cxx | |
parent | 675a74d5e57af8f6f3a30b69122cbd5faf392a86 (diff) |
refactor
Change-Id: I0cc034e7521de7911a9f5dfe8bc405971d175754
Diffstat (limited to 'writerperfect/qa/unit/WpftLoader.cxx')
-rw-r--r-- | writerperfect/qa/unit/WpftLoader.cxx | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/writerperfect/qa/unit/WpftLoader.cxx b/writerperfect/qa/unit/WpftLoader.cxx new file mode 100644 index 000000000000..5299a27d9cc0 --- /dev/null +++ b/writerperfect/qa/unit/WpftLoader.cxx @@ -0,0 +1,194 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "WpftLoader.hxx" + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/container/NoSuchElementException.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/document/XExtendedFilterDetection.hpp> +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/document/XImporter.hpp> +#include <com/sun/star/document/XTypeDetection.hpp> +#include <com/sun/star/frame/XController.hpp> +#include <com/sun/star/frame/XDesktop2.hpp> +#include <com/sun/star/frame/XFrame.hpp> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/ucb/XContent.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/util/XCloseable.hpp> + +#include <ucbhelper/content.hxx> + +namespace beans = com::sun::star::beans; +namespace container = com::sun::star::container; +namespace document = com::sun::star::document; +namespace frame = com::sun::star::frame; +namespace lang = com::sun::star::lang; +namespace ucb = com::sun::star::ucb; +namespace uno = com::sun::star::uno; +namespace util = com::sun::star::util; + +namespace writerperfect +{ +namespace test +{ + +WpftLoader::WpftLoader( + const rtl::OUString &rURL, + const css::uno::Reference<css::document::XFilter> &rxFilter, + const rtl::OUString &rFactoryURL, + const css::uno::Reference<css::frame::XDesktop2> &rxDesktop, + const css::uno::Reference<css::container::XNameAccess> &rxTypeMap, + const css::uno::Reference<css::uno::XComponentContext> &rxContext +) + : m_aURL(rURL) + , m_aFactoryURL(rFactoryURL) + , m_xFilter(rxFilter) + , m_xDesktop(rxDesktop) + , m_xTypeMap(rxTypeMap) + , m_xContext(rxContext) +{ + if (!impl_load()) + impl_dispose(); +} + +WpftLoader::~WpftLoader() +{ + try + { + impl_dispose(); + } + catch (...) + { + } +} + +const css::uno::Reference<css::lang::XComponent> &WpftLoader::getDocument() const +{ + return m_xDoc; +} + +bool WpftLoader::impl_load() +{ + // create an empty frame + m_xDoc.set( + m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()), + uno::UNO_QUERY_THROW); + + // Find the model and frame. We need them later. + m_xFrame.set(m_xDoc, uno::UNO_QUERY); + uno::Reference<frame::XModel> xModel(m_xDoc, uno::UNO_QUERY); + uno::Reference<frame::XController> xController(m_xDoc, uno::UNO_QUERY); + + if (m_xFrame.is()) + { + xController = m_xFrame->getController(); + xModel = xController->getModel(); + } + else if (xModel.is()) + { + xController = xModel->getCurrentController(); + m_xFrame = xController->getFrame(); + } + else if (xController.is()) + { + m_xFrame = xController->getFrame(); + xModel = xController->getModel(); + } + + if (!m_xFrame.is() || !xModel.is()) + throw uno::RuntimeException(); + + // try to import the document (and load it into the prepared frame) + try + { + const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW); + + xImporter->setTargetDocument(m_xDoc); + + uno::Sequence<beans::PropertyValue> aDescriptor(3); + ucbhelper::Content aContent(m_aURL, uno::Reference<ucb::XCommandEnvironment>(), m_xContext); + + aDescriptor[0].Name = "URL"; + aDescriptor[0].Value <<= m_aURL; + aDescriptor[1].Name = "InputStream"; + aDescriptor[1].Value <<= aContent.openStream(); + aDescriptor[2].Name = "UCBContent"; + aDescriptor[2].Value <<= aContent.get(); + + const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW); + + const rtl::OUString aTypeName(xDetector->detect(aDescriptor)); + if (aTypeName.isEmpty()) + throw lang::IllegalArgumentException(); + + impl_detectFilterName(aDescriptor, aTypeName); + + xModel->lockControllers(); + const bool bLoaded = m_xFilter->filter(aDescriptor); + xModel->unlockControllers(); + return bLoaded; + } + catch (const uno::Exception &) + { + // ignore + } + + return false; +} + +void WpftLoader::impl_dispose() +{ + // close the opened document + uno::Reference<util::XCloseable> xCloseable(m_xFrame, uno::UNO_QUERY); + if (xCloseable.is()) + xCloseable->close(true); + else if (m_xDoc.is()) + m_xDoc->dispose(); + m_xDoc.clear(); + m_xFrame.clear(); +} + +void WpftLoader::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName) +{ + const sal_Int32 nDescriptorLen = rDescriptor.getLength(); + + for (sal_Int32 n = 0; nDescriptorLen != n; ++n) + { + if ("FilterName" == rDescriptor[n].Name) + return; + } + + uno::Sequence<beans::PropertyValue> aTypes; + if (m_xTypeMap->getByName(rTypeName) >>= aTypes) + { + for (sal_Int32 n = 0; aTypes.getLength() != n; ++n) + { + rtl::OUString aFilterName; + if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName)) + { + rDescriptor.realloc(nDescriptorLen + 1); + rDescriptor[nDescriptorLen].Name = "FilterName"; + rDescriptor[nDescriptorLen].Value <<= aFilterName; + return; + } + } + } + + throw container::NoSuchElementException(); +} + +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |