Line data Source code
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 : #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>
29 :
30 : #include "osl/diagnose.h"
31 :
32 : using namespace com::sun::star;
33 :
34 : namespace ucbhelper {
35 :
36 : //============================================================================
37 : //
38 : // registerAtUcb
39 : //
40 : //============================================================================
41 :
42 : bool
43 20 : registerAtUcb(
44 : uno::Reference< ucb::XContentProviderManager > const & rManager,
45 : uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
46 : rtl::OUString const & rName,
47 : rtl::OUString const & rArguments,
48 : rtl::OUString const & rTemplate,
49 : ContentProviderRegistrationInfo * pInfo)
50 : throw (uno::RuntimeException)
51 : {
52 : OSL_ENSURE(rServiceFactory.is(),
53 : "ucb::registerAtUcb(): No service factory");
54 :
55 : bool bNoProxy
56 20 : = rArguments.compareToAscii(RTL_CONSTASCII_STRINGPARAM("{noproxy}"))
57 20 : == 0;
58 : rtl::OUString
59 : aProviderArguments(bNoProxy ?
60 : rArguments.
61 : copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
62 20 : rArguments);
63 :
64 : // First, try to instantiate proxy for provider:
65 20 : uno::Reference< ucb::XContentProvider > xProvider;
66 20 : if (!bNoProxy)
67 : {
68 20 : uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
69 : try
70 : {
71 20 : uno::Reference< beans::XPropertySet > xFactoryProperties( rServiceFactory, uno::UNO_QUERY_THROW );
72 : uno::Reference< uno::XComponentContext > xContext = uno::Reference< uno::XComponentContext >(
73 20 : xFactoryProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
74 20 : uno::UNO_QUERY );
75 : xProxyFactory
76 : = uno::Reference< ucb::XContentProviderFactory >(
77 20 : ucb::ContentProviderProxyFactory::create( xContext ) );
78 : }
79 0 : catch (uno::Exception const &) {}
80 : OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
81 20 : if (xProxyFactory.is())
82 20 : xProvider = xProxyFactory->createContentProvider(rName);
83 : }
84 :
85 : // Then, try to instantiate provider directly:
86 20 : if (!xProvider.is())
87 : try
88 : {
89 : xProvider = uno::Reference< ucb::XContentProvider >(
90 0 : rServiceFactory->createInstance(rName),
91 0 : uno::UNO_QUERY);
92 : }
93 0 : catch (uno::RuntimeException const &) { throw; }
94 0 : catch (uno::Exception const &) {}
95 :
96 : uno::Reference< ucb::XContentProvider >
97 20 : xOriginalProvider(xProvider);
98 : uno::Reference< ucb::XParameterizedContentProvider >
99 20 : xParameterized(xProvider, uno::UNO_QUERY);
100 20 : if (xParameterized.is())
101 : {
102 20 : uno::Reference< ucb::XContentProvider > xInstance;
103 : try
104 : {
105 20 : xInstance = xParameterized->registerInstance(rTemplate,
106 : aProviderArguments,
107 20 : true);
108 : //@@@ if this call replaces an old instance, the commit-or-
109 : // rollback code below will not work
110 : }
111 0 : catch (lang::IllegalArgumentException const &) {}
112 :
113 20 : if (xInstance.is())
114 20 : xProvider = xInstance;
115 : }
116 :
117 20 : bool bSuccess = false;
118 20 : if (rManager.is() && xProvider.is())
119 : try
120 : {
121 20 : rManager->registerContentProvider(xProvider, rTemplate, true);
122 20 : bSuccess = true;
123 : }
124 0 : catch (ucb::DuplicateProviderException const &)
125 : {
126 0 : if (xParameterized.is())
127 : try
128 : {
129 0 : xParameterized->deregisterInstance(rTemplate,
130 0 : aProviderArguments);
131 : }
132 0 : catch (lang::IllegalArgumentException const &) {}
133 : }
134 0 : catch (...)
135 : {
136 0 : if (xParameterized.is())
137 : try
138 : {
139 0 : xParameterized->deregisterInstance(rTemplate,
140 0 : aProviderArguments);
141 : }
142 0 : catch (lang::IllegalArgumentException const &) {}
143 0 : catch (uno::RuntimeException const &) {}
144 0 : throw;
145 : }
146 :
147 20 : if (bSuccess && pInfo)
148 : {
149 0 : pInfo->m_xProvider = xOriginalProvider;
150 0 : pInfo->m_aArguments = aProviderArguments;
151 0 : pInfo->m_aTemplate = rTemplate;
152 : }
153 20 : return bSuccess;
154 : }
155 :
156 : }
157 :
158 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|