1*ac9096f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ac9096f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ac9096f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ac9096f4SAndrew Rist  * distributed with this work for additional information
6*ac9096f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ac9096f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ac9096f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*ac9096f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ac9096f4SAndrew Rist  *
11*ac9096f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ac9096f4SAndrew Rist  *
13*ac9096f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ac9096f4SAndrew Rist  * software distributed under the License is distributed on an
15*ac9096f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ac9096f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ac9096f4SAndrew Rist  * specific language governing permissions and limitations
18*ac9096f4SAndrew Rist  * under the License.
19*ac9096f4SAndrew Rist  *
20*ac9096f4SAndrew Rist  *************************************************************/
21*ac9096f4SAndrew Rist 
22*ac9096f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
26cdf0e10cSrcweir #include <ucbhelper/configureucb.hxx>
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
29cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderManager.hpp>
30cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "osl/diagnose.h"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #ifndef _UCBHELPER_PROVCONF_HXX_
37cdf0e10cSrcweir #include <provconf.hxx>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #include <registerucb.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace com::sun::star;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace {
44cdf0e10cSrcweir 
fillPlaceholders(rtl::OUString const & rInput,uno::Sequence<uno::Any> const & rReplacements,rtl::OUString * pOutput)45cdf0e10cSrcweir bool fillPlaceholders(rtl::OUString const & rInput,
46cdf0e10cSrcweir 					  uno::Sequence< uno::Any > const & rReplacements,
47cdf0e10cSrcweir 					  rtl::OUString * pOutput)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	sal_Unicode const * p = rInput.getStr();
50cdf0e10cSrcweir 	sal_Unicode const * pEnd = p + rInput.getLength();
51cdf0e10cSrcweir 	sal_Unicode const * pCopy = p;
52cdf0e10cSrcweir 	rtl::OUStringBuffer aBuffer;
53cdf0e10cSrcweir 	while (p != pEnd)
54cdf0e10cSrcweir 		switch (*p++)
55cdf0e10cSrcweir 		{
56cdf0e10cSrcweir 			case '&':
57cdf0e10cSrcweir 				if (pEnd - p >= 4
58cdf0e10cSrcweir 					&& p[0] == 'a' && p[1] == 'm' && p[2] == 'p'
59cdf0e10cSrcweir 					&& p[3] == ';')
60cdf0e10cSrcweir 				{
61cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
62cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('&'));
63cdf0e10cSrcweir 					p += 4;
64cdf0e10cSrcweir 					pCopy = p;
65cdf0e10cSrcweir 				}
66cdf0e10cSrcweir 				else if (pEnd - p >= 3
67cdf0e10cSrcweir 						 && p[0] == 'l' && p[1] == 't' && p[2] == ';')
68cdf0e10cSrcweir 				{
69cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
70cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('<'));
71cdf0e10cSrcweir 					p += 3;
72cdf0e10cSrcweir 					pCopy = p;
73cdf0e10cSrcweir 				}
74cdf0e10cSrcweir 				else if (pEnd - p >= 3
75cdf0e10cSrcweir 						 && p[0] == 'g' && p[1] == 't' && p[2] == ';')
76cdf0e10cSrcweir 				{
77cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
78cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('>'));
79cdf0e10cSrcweir 					p += 3;
80cdf0e10cSrcweir 					pCopy = p;
81cdf0e10cSrcweir 				}
82cdf0e10cSrcweir 				break;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 			case '<':
85cdf0e10cSrcweir 				sal_Unicode const * q = p;
86cdf0e10cSrcweir 				while (q != pEnd && *q != '>')
87cdf0e10cSrcweir 					++q;
88cdf0e10cSrcweir 				if (q == pEnd)
89cdf0e10cSrcweir 					break;
90cdf0e10cSrcweir 				rtl::OUString aKey(p, q - p);
91cdf0e10cSrcweir 				rtl::OUString aValue;
92cdf0e10cSrcweir 				bool bFound = false;
93cdf0e10cSrcweir 				for (sal_Int32 i = 2; i + 1 < rReplacements.getLength();
94cdf0e10cSrcweir 					 i += 2)
95cdf0e10cSrcweir 				{
96cdf0e10cSrcweir 					rtl::OUString aReplaceKey;
97cdf0e10cSrcweir 					if ((rReplacements[i] >>= aReplaceKey)
98cdf0e10cSrcweir 						&& aReplaceKey == aKey
99cdf0e10cSrcweir 						&& (rReplacements[i + 1] >>= aValue))
100cdf0e10cSrcweir 					{
101cdf0e10cSrcweir 						bFound = true;
102cdf0e10cSrcweir 						break;
103cdf0e10cSrcweir 					}
104cdf0e10cSrcweir 				}
105cdf0e10cSrcweir 				if (!bFound)
106cdf0e10cSrcweir 					return false;
107cdf0e10cSrcweir 				aBuffer.append(pCopy, p - 1 - pCopy);
108cdf0e10cSrcweir 				aBuffer.append(aValue);
109cdf0e10cSrcweir 				p = q + 1;
110cdf0e10cSrcweir 				pCopy = p;
111cdf0e10cSrcweir 				break;
112cdf0e10cSrcweir 		}
113cdf0e10cSrcweir 	aBuffer.append(pCopy, pEnd - pCopy);
114cdf0e10cSrcweir 	*pOutput = aBuffer.makeStringAndClear();
115cdf0e10cSrcweir 	return true;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir namespace ucbhelper {
121cdf0e10cSrcweir 
122cdf0e10cSrcweir //============================================================================
123cdf0e10cSrcweir //
124cdf0e10cSrcweir //  configureUcb
125cdf0e10cSrcweir //
126cdf0e10cSrcweir //============================================================================
127cdf0e10cSrcweir 
128cdf0e10cSrcweir bool
configureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,uno::Reference<lang::XMultiServiceFactory> const & rServiceFactory,ContentProviderDataList const & rData,ContentProviderRegistrationInfoList * pInfos)129cdf0e10cSrcweir configureUcb(
130cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
131cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
132cdf0e10cSrcweir     ContentProviderDataList const & rData,
133cdf0e10cSrcweir     ContentProviderRegistrationInfoList * pInfos)
134cdf0e10cSrcweir 	throw (uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     ContentProviderDataList::const_iterator aEnd(rData.end());
137cdf0e10cSrcweir     for (ContentProviderDataList::const_iterator aIt(rData.begin());
138cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir         ContentProviderRegistrationInfo aInfo;
141cdf0e10cSrcweir         bool bSuccess = registerAtUcb(rManager,
142cdf0e10cSrcweir                                       rServiceFactory,
143cdf0e10cSrcweir                                       aIt->ServiceName,
144cdf0e10cSrcweir                                       aIt->Arguments,
145cdf0e10cSrcweir                                       aIt->URLTemplate,
146cdf0e10cSrcweir                                       &aInfo);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         if (bSuccess && pInfos)
149cdf0e10cSrcweir             pInfos->push_back(aInfo);
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	return true;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir //============================================================================
156cdf0e10cSrcweir //
157cdf0e10cSrcweir //  configureUcb
158cdf0e10cSrcweir //
159cdf0e10cSrcweir //============================================================================
160cdf0e10cSrcweir 
161cdf0e10cSrcweir bool
configureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,uno::Reference<lang::XMultiServiceFactory> const & rServiceFactory,uno::Sequence<uno::Any> const & rArguments,std::vector<ContentProviderRegistrationInfo> * pInfos)162cdf0e10cSrcweir configureUcb(
163cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
164cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
165cdf0e10cSrcweir 	uno::Sequence< uno::Any > const & rArguments,
166cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo > * pInfos)
167cdf0e10cSrcweir 	throw (uno::RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	rtl::OUString aKey1;
170cdf0e10cSrcweir 	rtl::OUString aKey2;
171cdf0e10cSrcweir 	if (rArguments.getLength() < 2
172cdf0e10cSrcweir 		|| !(rArguments[0] >>= aKey1) || !(rArguments[1] >>= aKey2))
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		OSL_ENSURE(false, "ucb::configureUcb(): Bad arguments");
175cdf0e10cSrcweir 		return false;
176cdf0e10cSrcweir 	}
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	ContentProviderDataList aData;
179cdf0e10cSrcweir 	if (!getContentProviderData(rServiceFactory, aKey1, aKey2, aData))
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		OSL_ENSURE(false, "ucb::configureUcb(): No configuration");
182cdf0e10cSrcweir 		return false;
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	ContentProviderDataList::const_iterator aEnd(aData.end());
186cdf0e10cSrcweir 	for (ContentProviderDataList::const_iterator aIt(aData.begin());
187cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		rtl::OUString aProviderArguments;
190cdf0e10cSrcweir 		if (fillPlaceholders(aIt->Arguments,
191cdf0e10cSrcweir 							 rArguments,
192cdf0e10cSrcweir 							 &aProviderArguments))
193cdf0e10cSrcweir 		{
194cdf0e10cSrcweir 			ContentProviderRegistrationInfo aInfo;
195cdf0e10cSrcweir 			bool bSuccess = registerAtUcb(rManager,
196cdf0e10cSrcweir 										  rServiceFactory,
197cdf0e10cSrcweir 										  aIt->ServiceName,
198cdf0e10cSrcweir 										  aProviderArguments,
199cdf0e10cSrcweir 										  aIt->URLTemplate,
200cdf0e10cSrcweir 										  &aInfo);
201cdf0e10cSrcweir 			OSL_ENSURE(bSuccess, "ucb::configureUcb(): Bad content provider");
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			if (bSuccess && pInfos)
204cdf0e10cSrcweir 				pInfos->push_back(aInfo);
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 		else
207cdf0e10cSrcweir 			OSL_ENSURE(false,
208cdf0e10cSrcweir 					   "ucb::configureUcb(): Bad argument placeholders");
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	return true;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir //============================================================================
217cdf0e10cSrcweir //
218cdf0e10cSrcweir //  unconfigureUcb
219cdf0e10cSrcweir //
220cdf0e10cSrcweir //============================================================================
221cdf0e10cSrcweir 
222cdf0e10cSrcweir namespace ucbhelper {
223cdf0e10cSrcweir 
224cdf0e10cSrcweir void
unconfigureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,std::vector<ContentProviderRegistrationInfo> const & rInfos)225cdf0e10cSrcweir unconfigureUcb(
226cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
227cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo > const & rInfos)
228cdf0e10cSrcweir 	throw (uno::RuntimeException)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo >::const_iterator
231cdf0e10cSrcweir 		aEnd(rInfos.end());
232cdf0e10cSrcweir 	for (std::vector< ContentProviderRegistrationInfo >::const_iterator
233cdf0e10cSrcweir 			 aIt(rInfos.begin());
234cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
235cdf0e10cSrcweir 		deregisterFromUcb(rManager, *aIt);
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir }
239