Update ooo320-m1
[ooovba.git] / ucbhelper / source / provider / registerucb.cxx
blob4a5ea678bebd3eb8d0bb93dd54b93a32b87df656
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registerucb.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucbhelper.hxx"
33 #include <registerucb.hxx>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/ucb/XContentProviderManager.hpp>
36 #include <com/sun/star/ucb/XParameterizedContentProvider.hpp>
37 #include <com/sun/star/ucb/XContentProviderFactory.hpp>
38 #include <com/sun/star/uno/RuntimeException.hpp>
40 #include "osl/diagnose.h"
41 #include <ucbhelper/configureucb.hxx>
43 using namespace com::sun::star;
45 namespace ucbhelper {
47 //============================================================================
49 // registerAtUcb
51 //============================================================================
53 bool
54 registerAtUcb(
55 uno::Reference< ucb::XContentProviderManager > const & rManager,
56 uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
57 rtl::OUString const & rName,
58 rtl::OUString const & rArguments,
59 rtl::OUString const & rTemplate,
60 ContentProviderRegistrationInfo * pInfo)
61 throw (uno::RuntimeException)
63 OSL_ENSURE(rServiceFactory.is(),
64 "ucb::registerAtUcb(): No service factory");
66 bool bNoProxy
67 = rArguments.compareToAscii(RTL_CONSTASCII_STRINGPARAM("{noproxy}"))
68 == 0;
69 rtl::OUString
70 aProviderArguments(bNoProxy ?
71 rArguments.
72 copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
73 rArguments);
75 // First, try to instantiate proxy for provider:
76 uno::Reference< ucb::XContentProvider > xProvider;
77 if (!bNoProxy)
79 uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
80 try
82 xProxyFactory
83 = uno::Reference< ucb::XContentProviderFactory >(
84 rServiceFactory->
85 createInstance(
86 rtl::OUString(
87 RTL_CONSTASCII_USTRINGPARAM(
88 "com.sun.star.ucb.ContentProviderProxyFactory"))),
89 uno::UNO_QUERY);
91 catch (uno::Exception const &) {}
92 OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
93 if (xProxyFactory.is())
94 xProvider = xProxyFactory->createContentProvider(rName);
97 // Then, try to instantiate provider directly:
98 if (!xProvider.is())
99 try
101 xProvider = uno::Reference< ucb::XContentProvider >(
102 rServiceFactory->createInstance(rName),
103 uno::UNO_QUERY);
105 catch (uno::RuntimeException const &) { throw; }
106 catch (uno::Exception const &) {}
108 uno::Reference< ucb::XContentProvider >
109 xOriginalProvider(xProvider);
110 uno::Reference< ucb::XParameterizedContentProvider >
111 xParameterized(xProvider, uno::UNO_QUERY);
112 if (xParameterized.is())
114 uno::Reference< ucb::XContentProvider > xInstance;
117 xInstance = xParameterized->registerInstance(rTemplate,
118 aProviderArguments,
119 true);
120 //@@@ if this call replaces an old instance, the commit-or-
121 // rollback code below will not work
123 catch (lang::IllegalArgumentException const &) {}
125 if (xInstance.is())
126 xProvider = xInstance;
129 bool bSuccess = false;
130 if (rManager.is() && xProvider.is())
133 rManager->registerContentProvider(xProvider, rTemplate, true);
134 bSuccess = true;
136 catch (ucb::DuplicateProviderException const &)
138 if (xParameterized.is())
141 xParameterized->deregisterInstance(rTemplate,
142 aProviderArguments);
144 catch (lang::IllegalArgumentException const &) {}
146 catch (...)
148 if (xParameterized.is())
151 xParameterized->deregisterInstance(rTemplate,
152 aProviderArguments);
154 catch (lang::IllegalArgumentException const &) {}
155 catch (uno::RuntimeException const &) {}
156 throw;
159 if (bSuccess && pInfo)
161 pInfo->m_xProvider = xOriginalProvider;
162 pInfo->m_aArguments = aProviderArguments;
163 pInfo->m_aTemplate = rTemplate;
165 return bSuccess;
168 //============================================================================
170 // deregisterFromUcb
172 //============================================================================
174 void
175 deregisterFromUcb(
176 uno::Reference< ucb::XContentProviderManager > const & rManager,
177 ContentProviderRegistrationInfo const & rInfo)
178 throw (uno::RuntimeException)
180 uno::Reference< ucb::XContentProvider >
181 xProvider(rInfo.m_xProvider);
182 uno::Reference< ucb::XParameterizedContentProvider >
183 xParameterized(xProvider, uno::UNO_QUERY);
184 if (xParameterized.is())
186 uno::Reference< ucb::XContentProvider > xInstance;
189 xInstance
190 = xParameterized->deregisterInstance(rInfo.m_aTemplate,
191 rInfo.m_aArguments);
193 catch (lang::IllegalArgumentException const &) {}
195 if (xInstance.is())
196 xProvider = xInstance;
199 if (rManager.is())
200 rManager->deregisterContentProvider(xProvider, rInfo.m_aTemplate);
201 //@@@ if this fails, a roll-back of deregisterInstance() is
202 // missing