bump product version to 4.1.6.2
[LibreOffice.git] / ucbhelper / source / provider / registerucb.cxx
blob944fe01f1abcf9e43fdb21212df991c4fadcf827
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 .
20 #include <ucbhelper/registerucb.hxx>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/ucb/XContentProviderManager.hpp>
24 #include <com/sun/star/ucb/XParameterizedContentProvider.hpp>
25 #include <com/sun/star/ucb/ContentProviderProxyFactory.hpp>
26 #include <com/sun/star/ucb/XContentProviderFactory.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
30 #include "osl/diagnose.h"
32 using namespace com::sun::star;
34 namespace ucbhelper {
36 //============================================================================
38 // registerAtUcb
40 //============================================================================
42 bool
43 registerAtUcb(
44 uno::Reference< ucb::XContentProviderManager > const & rManager,
45 uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
46 OUString const & rName,
47 OUString const & rArguments,
48 OUString const & rTemplate,
49 ContentProviderRegistrationInfo * pInfo)
50 throw (uno::RuntimeException)
52 OSL_ENSURE(rServiceFactory.is(),
53 "ucb::registerAtUcb(): No service factory");
55 bool bNoProxy = rArguments.startsWith("{noproxy}");
56 OUString
57 aProviderArguments(bNoProxy ?
58 rArguments.
59 copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
60 rArguments);
62 // First, try to instantiate proxy for provider:
63 uno::Reference< ucb::XContentProvider > xProvider;
64 if (!bNoProxy)
66 uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
67 try
69 uno::Reference< beans::XPropertySet > xFactoryProperties( rServiceFactory, uno::UNO_QUERY_THROW );
70 uno::Reference< uno::XComponentContext > xContext = uno::Reference< uno::XComponentContext >(
71 xFactoryProperties->getPropertyValue( OUString( "DefaultContext" ) ),
72 uno::UNO_QUERY );
73 xProxyFactory
74 = uno::Reference< ucb::XContentProviderFactory >(
75 ucb::ContentProviderProxyFactory::create( xContext ) );
77 catch (uno::Exception const &) {}
78 OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
79 if (xProxyFactory.is())
80 xProvider = xProxyFactory->createContentProvider(rName);
83 // Then, try to instantiate provider directly:
84 if (!xProvider.is())
85 try
87 xProvider = uno::Reference< ucb::XContentProvider >(
88 rServiceFactory->createInstance(rName),
89 uno::UNO_QUERY);
91 catch (uno::RuntimeException const &) { throw; }
92 catch (uno::Exception const &) {}
94 uno::Reference< ucb::XContentProvider >
95 xOriginalProvider(xProvider);
96 uno::Reference< ucb::XParameterizedContentProvider >
97 xParameterized(xProvider, uno::UNO_QUERY);
98 if (xParameterized.is())
100 uno::Reference< ucb::XContentProvider > xInstance;
103 xInstance = xParameterized->registerInstance(rTemplate,
104 aProviderArguments,
105 true);
106 //@@@ if this call replaces an old instance, the commit-or-
107 // rollback code below will not work
109 catch (lang::IllegalArgumentException const &) {}
111 if (xInstance.is())
112 xProvider = xInstance;
115 bool bSuccess = false;
116 if (rManager.is() && xProvider.is())
120 rManager->registerContentProvider(xProvider, rTemplate, true);
121 bSuccess = true;
123 catch (ucb::DuplicateProviderException const &)
125 if (xParameterized.is())
128 xParameterized->deregisterInstance(rTemplate,
129 aProviderArguments);
131 catch (lang::IllegalArgumentException const &) {}
133 catch (...)
135 if (xParameterized.is())
138 xParameterized->deregisterInstance(rTemplate,
139 aProviderArguments);
141 catch (lang::IllegalArgumentException const &) {}
142 catch (uno::RuntimeException const &) {}
143 throw;
146 if (bSuccess && pInfo)
148 pInfo->m_xProvider = xOriginalProvider;
149 pInfo->m_aArguments = aProviderArguments;
150 pInfo->m_aTemplate = rTemplate;
152 return bSuccess;
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */