1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_ucbhelper.hxx"
30 #include <ucbhelper/registerucb.hxx>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/ucb/XContentProviderManager.hpp>
33 #include <com/sun/star/ucb/XParameterizedContentProvider.hpp>
34 #include <com/sun/star/ucb/XContentProviderFactory.hpp>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 
37 #include "osl/diagnose.h"
38 
39 using namespace com::sun::star;
40 
41 namespace ucbhelper {
42 
43 //============================================================================
44 //
45 //  registerAtUcb
46 //
47 //============================================================================
48 
49 bool
50 registerAtUcb(
51     uno::Reference< ucb::XContentProviderManager > const & rManager,
52 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
53 	rtl::OUString const & rName,
54 	rtl::OUString const & rArguments,
55 	rtl::OUString const & rTemplate,
56 	ContentProviderRegistrationInfo * pInfo)
57 	throw (uno::RuntimeException)
58 {
59 	OSL_ENSURE(rServiceFactory.is(),
60 			   "ucb::registerAtUcb(): No service factory");
61 
62 	bool bNoProxy
63 		= rArguments.compareToAscii(RTL_CONSTASCII_STRINGPARAM("{noproxy}"))
64 		      == 0;
65 	rtl::OUString
66 		aProviderArguments(bNoProxy ?
67 						       rArguments.
68 						           copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
69 						       rArguments);
70 
71 	// First, try to instantiate proxy for provider:
72 	uno::Reference< ucb::XContentProvider > xProvider;
73 	if (!bNoProxy)
74 	{
75 		uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
76 		try
77 		{
78 			xProxyFactory
79 				= uno::Reference< ucb::XContentProviderFactory >(
80 					  rServiceFactory->
81 				          createInstance(
82 							  rtl::OUString(
83 								  RTL_CONSTASCII_USTRINGPARAM(
84 							"com.sun.star.ucb.ContentProviderProxyFactory"))),
85 					  uno::UNO_QUERY);
86 		}
87 		catch (uno::Exception const &) {}
88 		OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
89 		if (xProxyFactory.is())
90 			xProvider = xProxyFactory->createContentProvider(rName);
91 	}
92 
93 	// Then, try to instantiate provider directly:
94 	if (!xProvider.is())
95 		try
96 		{
97 			xProvider = uno::Reference< ucb::XContentProvider >(
98 				            rServiceFactory->createInstance(rName),
99 							uno::UNO_QUERY);
100 		}
101 		catch (uno::RuntimeException const &) { throw; }
102 		catch (uno::Exception const &) {}
103 
104 	uno::Reference< ucb::XContentProvider >
105 		xOriginalProvider(xProvider);
106 	uno::Reference< ucb::XParameterizedContentProvider >
107 		xParameterized(xProvider, uno::UNO_QUERY);
108 	if (xParameterized.is())
109 	{
110 		uno::Reference< ucb::XContentProvider > xInstance;
111 		try
112 		{
113 			xInstance = xParameterized->registerInstance(rTemplate,
114 														 aProviderArguments,
115 														 true);
116 				//@@@ if this call replaces an old instance, the commit-or-
117 				// rollback code below will not work
118 		}
119 		catch (lang::IllegalArgumentException const &) {}
120 
121 		if (xInstance.is())
122 			xProvider = xInstance;
123 	}
124 
125 	bool bSuccess = false;
126 	if (rManager.is() && xProvider.is())
127 		try
128 		{
129 			rManager->registerContentProvider(xProvider, rTemplate, true);
130 			bSuccess = true;
131 		}
132 		catch (ucb::DuplicateProviderException const &)
133 		{
134 			if (xParameterized.is())
135 				try
136 				{
137 					xParameterized->deregisterInstance(rTemplate,
138 													   aProviderArguments);
139 				}
140 				catch (lang::IllegalArgumentException const &) {}
141 		}
142 		catch (...)
143 		{
144 			if (xParameterized.is())
145 				try
146 				{
147 					xParameterized->deregisterInstance(rTemplate,
148 													   aProviderArguments);
149 				}
150 				catch (lang::IllegalArgumentException const &) {}
151 				catch (uno::RuntimeException const &) {}
152 			throw;
153 		}
154 
155 	if (bSuccess && pInfo)
156 	{
157 		pInfo->m_xProvider = xOriginalProvider;
158 		pInfo->m_aArguments = aProviderArguments;
159 		pInfo->m_aTemplate = rTemplate;
160 	}
161 	return bSuccess;
162 }
163 
164 }
165