bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / expand / ucpexpand.cxx
blobaa2f7a87dd2eca71e5114036edf251fc39899fde
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 .
21 #include <rtl/uri.hxx>
22 #include <osl/mutex.hxx>
23 #include <cppuhelper/compbase.hxx>
24 #include <cppuhelper/factory.hxx>
25 #include <cppuhelper/implementationentry.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <ucbhelper/content.hxx>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/util/theMacroExpander.hpp>
32 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
33 #include <com/sun/star/ucb/XContentProvider.hpp>
34 #include <tools/diagnose_ex.h>
36 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
39 using namespace ::com::sun::star;
41 namespace
44 struct MutexHolder
46 mutable ::osl::Mutex m_mutex;
49 typedef ::cppu::WeakComponentImplHelper<
50 lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
53 class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
55 uno::Reference< uno::XComponentContext > m_xComponentContext;
56 uno::Reference< util::XMacroExpander > m_xMacroExpander;
57 OUString expandUri(
58 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
60 protected:
61 void check() const;
62 virtual void SAL_CALL disposing() override;
64 public:
65 explicit ExpandContentProviderImpl(
66 uno::Reference< uno::XComponentContext > const & xComponentContext )
67 : t_impl_helper( m_mutex ),
68 m_xComponentContext( xComponentContext ),
69 m_xMacroExpander( util::theMacroExpander::get(xComponentContext) )
72 // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() override;
74 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
75 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
77 // XContentProvider
78 virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
79 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) override;
80 virtual sal_Int32 SAL_CALL compareContentIds(
81 uno::Reference< ucb::XContentIdentifier > const & xId1,
82 uno::Reference< ucb::XContentIdentifier > const & xId2 ) override;
86 void ExpandContentProviderImpl::check() const
88 // xxx todo guard?
89 // MutexGuard guard( m_mutex );
90 if (rBHelper.bInDispose || rBHelper.bDisposed)
92 throw lang::DisposedException(
93 "expand content provider instance has "
94 "already been disposed!",
95 static_cast< OWeakObject * >(
96 const_cast< ExpandContentProviderImpl * >(this) ) );
100 void ExpandContentProviderImpl::disposing()
105 // XServiceInfo
107 OUString ExpandContentProviderImpl::getImplementationName()
109 check();
110 return "com.sun.star.comp.ucb.ExpandContentProvider";
114 uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
116 check();
117 return {
118 "com.sun.star.ucb.ExpandContentProvider",
119 "com.sun.star.ucb.ContentProvider"
123 sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName )
125 return cppu::supportsService(this, serviceName);
128 OUString ExpandContentProviderImpl::expandUri(
129 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
131 OUString uri( xIdentifier->getContentIdentifier() );
132 if (!uri.startsWith(EXPAND_PROTOCOL ":"))
134 throw ucb::IllegalIdentifierException(
135 "expected protocol " EXPAND_PROTOCOL "!",
136 static_cast< OWeakObject * >(
137 const_cast< ExpandContentProviderImpl * >(this) ) );
139 // cut protocol
140 OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
141 // decode uric class chars
142 str = ::rtl::Uri::decode(
143 str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
144 // expand macro string
145 return m_xMacroExpander->expandMacros( str );
148 // XContentProvider
150 uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
151 uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
153 check();
154 OUString uri( expandUri( xIdentifier ) );
156 ::ucbhelper::Content ucb_content;
157 if (::ucbhelper::Content::create(
158 uri, uno::Reference< ucb::XCommandEnvironment >(),
159 m_xComponentContext, ucb_content ))
161 return ucb_content.get();
163 else
165 return uno::Reference< ucb::XContent >();
170 sal_Int32 ExpandContentProviderImpl::compareContentIds(
171 uno::Reference< ucb::XContentIdentifier > const & xId1,
172 uno::Reference< ucb::XContentIdentifier > const & xId2 )
174 check();
177 OUString uri1( expandUri( xId1 ) );
178 OUString uri2( expandUri( xId2 ) );
179 return uri1.compareTo( uri2 );
181 catch (const ucb::IllegalIdentifierException &)
183 TOOLS_WARN_EXCEPTION( "ucb", "" );
184 return -1;
191 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
192 ucb_expand_ExpandContentProviderImpl_get_implementation(
193 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
195 return cppu::acquire(new ExpandContentProviderImpl(context));
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */