Stop leaking all ScPostIt instances.
[LibreOffice.git] / ucb / source / ucp / expand / ucpexpand.cxx
blob384133e3945e428086abed8fc1d59742533b9db6
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/compbase2.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/lang/XMultiServiceFactory.hpp"
32 #include "com/sun/star/registry/XRegistryKey.hpp"
33 #include "com/sun/star/util/theMacroExpander.hpp"
34 #include "com/sun/star/ucb/XContentProvider.hpp"
36 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
37 #define ARLEN(x) sizeof (x) / sizeof *(x)
40 using namespace ::com::sun::star;
42 namespace
45 struct MutexHolder
47 mutable ::osl::Mutex m_mutex;
50 typedef ::cppu::WeakComponentImplHelper2<
51 lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
53 //==============================================================================
54 class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
56 uno::Reference< uno::XComponentContext > m_xComponentContext;
57 uno::Reference< util::XMacroExpander > m_xMacroExpander;
58 OUString expandUri(
59 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
61 protected:
62 inline void check() const;
63 virtual void SAL_CALL disposing();
65 public:
66 inline ExpandContentProviderImpl(
67 uno::Reference< uno::XComponentContext > const & xComponentContext )
68 : t_impl_helper( m_mutex ),
69 m_xComponentContext( xComponentContext ),
70 m_xMacroExpander( util::theMacroExpander::get(xComponentContext) )
72 virtual ~ExpandContentProviderImpl() throw ();
74 // XServiceInfo
75 virtual OUString SAL_CALL getImplementationName()
76 throw (uno::RuntimeException);
77 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
78 throw (uno::RuntimeException);
79 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
80 throw (uno::RuntimeException);
82 // XContentProvider
83 virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
84 uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
85 throw (ucb::IllegalIdentifierException, uno::RuntimeException);
86 virtual sal_Int32 SAL_CALL compareContentIds(
87 uno::Reference< ucb::XContentIdentifier > const & xId1,
88 uno::Reference< ucb::XContentIdentifier > const & xId2 )
89 throw (uno::RuntimeException);
92 //______________________________________________________________________________
93 inline void ExpandContentProviderImpl::check() const
95 // xxx todo guard?
96 // MutexGuard guard( m_mutex );
97 if (rBHelper.bInDispose || rBHelper.bDisposed)
99 throw lang::DisposedException(
100 "expand content provider instance has "
101 "already been disposed!",
102 static_cast< OWeakObject * >(
103 const_cast< ExpandContentProviderImpl * >(this) ) );
107 //______________________________________________________________________________
108 ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
112 //______________________________________________________________________________
113 void ExpandContentProviderImpl::disposing()
117 //==============================================================================
118 static uno::Reference< uno::XInterface > SAL_CALL create(
119 uno::Reference< uno::XComponentContext > const & xComponentContext )
120 SAL_THROW( (uno::Exception) )
122 return static_cast< ::cppu::OWeakObject * >(
123 new ExpandContentProviderImpl( xComponentContext ) );
126 //==============================================================================
127 static OUString SAL_CALL implName()
129 return OUString("com.sun.star.comp.ucb.ExpandContentProvider");
132 //==============================================================================
133 static uno::Sequence< OUString > SAL_CALL supportedServices()
135 OUString names [] = {
136 OUString("com.sun.star.ucb.ExpandContentProvider"),
137 OUString("com.sun.star.ucb.ContentProvider")
139 return uno::Sequence< OUString >( names, ARLEN(names) );
142 // XServiceInfo
143 //______________________________________________________________________________
144 OUString ExpandContentProviderImpl::getImplementationName()
145 throw (uno::RuntimeException)
147 check();
148 return implName();
151 //______________________________________________________________________________
152 uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
153 throw (uno::RuntimeException)
155 check();
156 return supportedServices();
159 sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName )
160 throw (uno::RuntimeException)
162 return cppu::supportsService(this, serviceName);
165 OUString ExpandContentProviderImpl::expandUri(
166 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
168 OUString uri( xIdentifier->getContentIdentifier() );
169 if (!uri.startsWith(EXPAND_PROTOCOL ":"))
171 throw ucb::IllegalIdentifierException(
172 "expected protocol " EXPAND_PROTOCOL "!",
173 static_cast< OWeakObject * >(
174 const_cast< ExpandContentProviderImpl * >(this) ) );
176 // cut protocol
177 OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
178 // decode uric class chars
179 str = ::rtl::Uri::decode(
180 str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
181 // expand macro string
182 return m_xMacroExpander->expandMacros( str );
185 // XContentProvider
186 //______________________________________________________________________________
187 uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
188 uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
189 throw (ucb::IllegalIdentifierException, uno::RuntimeException)
191 check();
192 OUString uri( expandUri( xIdentifier ) );
194 ::ucbhelper::Content ucb_content;
195 if (::ucbhelper::Content::create(
196 uri, uno::Reference< ucb::XCommandEnvironment >(),
197 m_xComponentContext, ucb_content ))
199 return ucb_content.get();
201 else
203 return uno::Reference< ucb::XContent >();
207 //______________________________________________________________________________
208 sal_Int32 ExpandContentProviderImpl::compareContentIds(
209 uno::Reference< ucb::XContentIdentifier > const & xId1,
210 uno::Reference< ucb::XContentIdentifier > const & xId2 )
211 throw (uno::RuntimeException)
213 check();
216 OUString uri1( expandUri( xId1 ) );
217 OUString uri2( expandUri( xId2 ) );
218 return uri1.compareTo( uri2 );
220 catch (const ucb::IllegalIdentifierException & exc)
222 (void) exc; // unused
223 OSL_FAIL(
224 OUStringToOString(
225 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
226 return -1;
230 static const ::cppu::ImplementationEntry s_entries [] =
233 create,
234 implName,
235 supportedServices,
236 ::cppu::createSingleComponentFactory,
237 0, 0
239 { 0, 0, 0, 0, 0, 0 }
244 extern "C"
247 SAL_DLLPUBLIC_EXPORT void * SAL_CALL ucpexpand1_component_getFactory(
248 const sal_Char * pImplName,
249 lang::XMultiServiceFactory * pServiceManager,
250 registry::XRegistryKey * pRegistryKey )
252 return ::cppu::component_getFactoryHelper(
253 pImplName, pServiceManager, pRegistryKey, s_entries );
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */