| 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 |
|
| 21 |
#include <rtl/uri.hxx> |
| 22 |
#include <comphelper/compbase.hxx> |
| 23 |
#include <cppuhelper/supportsservice.hxx> |
| 24 |
#include <ucbhelper/content.hxx> |
| 25 |
#include <com/sun/star/uno/XComponentContext.hpp> |
| 26 |
#include <com/sun/star/lang/DisposedException.hpp> |
| 27 |
#include <com/sun/star/lang/XServiceInfo.hpp> |
| 28 |
#include <com/sun/star/util/theMacroExpander.hpp> |
| 29 |
#include <com/sun/star/ucb/IllegalIdentifierException.hpp> |
| 30 |
#include <com/sun/star/ucb/XContentProvider.hpp> |
| 31 |
#include <comphelper/diagnose_ex.hxx> |
| 32 |
|
| 33 |
|
| 34 |
using namespace ::com::sun::star; |
| 35 |
|
| 36 |
namespace |
| 37 |
{ |
| 38 |
|
| 39 |
typedef comphelper::WeakComponentImplHelper< |
| 40 |
lang::XServiceInfo, ucb::XContentProvider > t_impl_helper; |
| 41 |
|
| 42 |
|
| 43 |
class ExpandContentProviderImpl : public t_impl_helper |
| 44 |
{ |
| 45 |
uno::Reference< uno::XComponentContext > m_xComponentContext; |
| 46 |
uno::Reference< util::XMacroExpander > m_xMacroExpander; |
| 47 |
OUString expandUri( |
| 48 |
uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const; |
| 49 |
|
| 50 |
protected: |
| 51 |
void check() const; |
| 52 |
|
| 53 |
public: |
| 54 |
explicit ExpandContentProviderImpl( |
| 55 |
uno::Reference< uno::XComponentContext > const & xComponentContext ) |
| 56 |
: m_xComponentContext( xComponentContext ), |
| 57 |
m_xMacroExpander( util::theMacroExpander::get(xComponentContext) ) |
| 58 |
{} |
| 59 |
|
| 60 |
// XServiceInfo |
| 61 |
virtual OUString SAL_CALL getImplementationName() override; |
| 62 |
virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override; |
| 63 |
virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override; |
| 64 |
|
| 65 |
// XContentProvider |
| 66 |
virtual uno::Reference< ucb::XContent > SAL_CALL queryContent( |
| 67 |
uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) override; |
| 68 |
virtual sal_Int32 SAL_CALL compareContentIds( |
| 69 |
uno::Reference< ucb::XContentIdentifier > const & xId1, |
| 70 |
uno::Reference< ucb::XContentIdentifier > const & xId2 ) override; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
void ExpandContentProviderImpl::check() const |
| 75 |
{ |
| 76 |
// xxx todo guard? |
| 77 |
// MutexGuard guard( m_mutex ); |
| 78 |
if (m_bDisposed) |
| 79 |
{ |
| 80 |
throw lang::DisposedException( |
| 81 |
u"expand content provider instance has " |
| 82 |
"already been disposed!"_ustr, |
| 83 |
const_cast< ExpandContentProviderImpl * >(this)->getXWeak() ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// XServiceInfo |
| 88 |
|
| 89 |
OUString ExpandContentProviderImpl::getImplementationName() |
| 90 |
{ |
| 91 |
check(); |
| 92 |
return u"com.sun.star.comp.ucb.ExpandContentProvider"_ustr; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames() |
| 97 |
{ |
| 98 |
check(); |
| 99 |
return { |
| 100 |
u"com.sun.star.ucb.ExpandContentProvider"_ustr, |
| 101 |
u"com.sun.star.ucb.ContentProvider"_ustr |
| 102 |
}; |
| 103 |
} |
| 104 |
|
| 105 |
sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName ) |
| 106 |
{ |
| 107 |
return cppu::supportsService(this, serviceName); |
| 108 |
} |
| 109 |
|
| 110 |
OUString ExpandContentProviderImpl::expandUri( |
| 111 |
uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const |
| 112 |
{ |
| 113 |
OUString uri( xIdentifier->getContentIdentifier() ); |
| 114 |
if (!uri.startsWithIgnoreAsciiCase("vnd.sun.star.expand:", &uri)) |
| 115 |
{ |
| 116 |
throw ucb::IllegalIdentifierException( |
| 117 |
u"expected protocol vnd.sun.star.expand!"_ustr, |
| 118 |
const_cast< ExpandContentProviderImpl * >(this)->getXWeak() ); |
| 119 |
} |
| 120 |
// decode uric class chars |
| 121 |
OUString str = ::rtl::Uri::decode(uri, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8); |
| 122 |
// expand macro string |
| 123 |
return m_xMacroExpander->expandMacros( str ); |
| 124 |
} |
| 125 |
|
| 126 |
// XContentProvider |
| 127 |
|
| 128 |
uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent( |
| 129 |
uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) |
| 130 |
{ |
| 131 |
check(); |
| 132 |
OUString uri( expandUri( xIdentifier ) ); |
| 133 |
|
| 134 |
::ucbhelper::Content ucb_content; |
| 135 |
if (::ucbhelper::Content::create( |
| 136 |
uri, uno::Reference< ucb::XCommandEnvironment >(), |
| 137 |
m_xComponentContext, ucb_content )) |
| 138 |
{ |
| 139 |
return ucb_content.get(); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
return uno::Reference< ucb::XContent >(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
sal_Int32 ExpandContentProviderImpl::compareContentIds( |
| 149 |
uno::Reference< ucb::XContentIdentifier > const & xId1, |
| 150 |
uno::Reference< ucb::XContentIdentifier > const & xId2 ) |
| 151 |
{ |
| 152 |
check(); |
| 153 |
try |
| 154 |
{ |
| 155 |
OUString uri1( expandUri( xId1 ) ); |
| 156 |
OUString uri2( expandUri( xId2 ) ); |
| 157 |
return uri1.compareTo( uri2 ); |
| 158 |
} |
| 159 |
catch (const ucb::IllegalIdentifierException &) |
| 160 |
{ |
| 161 |
TOOLS_WARN_EXCEPTION( "ucb", "" ); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 170 |
ucb_expand_ExpandContentProviderImpl_get_implementation( |
| 171 |
css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&) |
| 172 |
{ |
| 173 |
return cppu::acquire(new ExpandContentProviderImpl(context)); |
| 174 |
} |
| 175 |
|
| 176 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |