1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
30*cdf0e10cSrcweir #include <ucbhelper/configureucb.hxx>
31*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderManager.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "osl/diagnose.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #ifndef _UCBHELPER_PROVCONF_HXX_
41*cdf0e10cSrcweir #include <provconf.hxx>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #include <registerucb.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace com::sun::star;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir bool fillPlaceholders(rtl::OUString const & rInput,
50*cdf0e10cSrcweir 					  uno::Sequence< uno::Any > const & rReplacements,
51*cdf0e10cSrcweir 					  rtl::OUString * pOutput)
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	sal_Unicode const * p = rInput.getStr();
54*cdf0e10cSrcweir 	sal_Unicode const * pEnd = p + rInput.getLength();
55*cdf0e10cSrcweir 	sal_Unicode const * pCopy = p;
56*cdf0e10cSrcweir 	rtl::OUStringBuffer aBuffer;
57*cdf0e10cSrcweir 	while (p != pEnd)
58*cdf0e10cSrcweir 		switch (*p++)
59*cdf0e10cSrcweir 		{
60*cdf0e10cSrcweir 			case '&':
61*cdf0e10cSrcweir 				if (pEnd - p >= 4
62*cdf0e10cSrcweir 					&& p[0] == 'a' && p[1] == 'm' && p[2] == 'p'
63*cdf0e10cSrcweir 					&& p[3] == ';')
64*cdf0e10cSrcweir 				{
65*cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
66*cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('&'));
67*cdf0e10cSrcweir 					p += 4;
68*cdf0e10cSrcweir 					pCopy = p;
69*cdf0e10cSrcweir 				}
70*cdf0e10cSrcweir 				else if (pEnd - p >= 3
71*cdf0e10cSrcweir 						 && p[0] == 'l' && p[1] == 't' && p[2] == ';')
72*cdf0e10cSrcweir 				{
73*cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
74*cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('<'));
75*cdf0e10cSrcweir 					p += 3;
76*cdf0e10cSrcweir 					pCopy = p;
77*cdf0e10cSrcweir 				}
78*cdf0e10cSrcweir 				else if (pEnd - p >= 3
79*cdf0e10cSrcweir 						 && p[0] == 'g' && p[1] == 't' && p[2] == ';')
80*cdf0e10cSrcweir 				{
81*cdf0e10cSrcweir 					aBuffer.append(pCopy, p - 1 - pCopy);
82*cdf0e10cSrcweir 					aBuffer.append(sal_Unicode('>'));
83*cdf0e10cSrcweir 					p += 3;
84*cdf0e10cSrcweir 					pCopy = p;
85*cdf0e10cSrcweir 				}
86*cdf0e10cSrcweir 				break;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 			case '<':
89*cdf0e10cSrcweir 				sal_Unicode const * q = p;
90*cdf0e10cSrcweir 				while (q != pEnd && *q != '>')
91*cdf0e10cSrcweir 					++q;
92*cdf0e10cSrcweir 				if (q == pEnd)
93*cdf0e10cSrcweir 					break;
94*cdf0e10cSrcweir 				rtl::OUString aKey(p, q - p);
95*cdf0e10cSrcweir 				rtl::OUString aValue;
96*cdf0e10cSrcweir 				bool bFound = false;
97*cdf0e10cSrcweir 				for (sal_Int32 i = 2; i + 1 < rReplacements.getLength();
98*cdf0e10cSrcweir 					 i += 2)
99*cdf0e10cSrcweir 				{
100*cdf0e10cSrcweir 					rtl::OUString aReplaceKey;
101*cdf0e10cSrcweir 					if ((rReplacements[i] >>= aReplaceKey)
102*cdf0e10cSrcweir 						&& aReplaceKey == aKey
103*cdf0e10cSrcweir 						&& (rReplacements[i + 1] >>= aValue))
104*cdf0e10cSrcweir 					{
105*cdf0e10cSrcweir 						bFound = true;
106*cdf0e10cSrcweir 						break;
107*cdf0e10cSrcweir 					}
108*cdf0e10cSrcweir 				}
109*cdf0e10cSrcweir 				if (!bFound)
110*cdf0e10cSrcweir 					return false;
111*cdf0e10cSrcweir 				aBuffer.append(pCopy, p - 1 - pCopy);
112*cdf0e10cSrcweir 				aBuffer.append(aValue);
113*cdf0e10cSrcweir 				p = q + 1;
114*cdf0e10cSrcweir 				pCopy = p;
115*cdf0e10cSrcweir 				break;
116*cdf0e10cSrcweir 		}
117*cdf0e10cSrcweir 	aBuffer.append(pCopy, pEnd - pCopy);
118*cdf0e10cSrcweir 	*pOutput = aBuffer.makeStringAndClear();
119*cdf0e10cSrcweir 	return true;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir namespace ucbhelper {
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir //============================================================================
127*cdf0e10cSrcweir //
128*cdf0e10cSrcweir //  configureUcb
129*cdf0e10cSrcweir //
130*cdf0e10cSrcweir //============================================================================
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir bool
133*cdf0e10cSrcweir configureUcb(
134*cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
135*cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
136*cdf0e10cSrcweir     ContentProviderDataList const & rData,
137*cdf0e10cSrcweir     ContentProviderRegistrationInfoList * pInfos)
138*cdf0e10cSrcweir 	throw (uno::RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     ContentProviderDataList::const_iterator aEnd(rData.end());
141*cdf0e10cSrcweir     for (ContentProviderDataList::const_iterator aIt(rData.begin());
142*cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir         ContentProviderRegistrationInfo aInfo;
145*cdf0e10cSrcweir         bool bSuccess = registerAtUcb(rManager,
146*cdf0e10cSrcweir                                       rServiceFactory,
147*cdf0e10cSrcweir                                       aIt->ServiceName,
148*cdf0e10cSrcweir                                       aIt->Arguments,
149*cdf0e10cSrcweir                                       aIt->URLTemplate,
150*cdf0e10cSrcweir                                       &aInfo);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         if (bSuccess && pInfos)
153*cdf0e10cSrcweir             pInfos->push_back(aInfo);
154*cdf0e10cSrcweir 	}
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	return true;
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //============================================================================
160*cdf0e10cSrcweir //
161*cdf0e10cSrcweir //  configureUcb
162*cdf0e10cSrcweir //
163*cdf0e10cSrcweir //============================================================================
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir bool
166*cdf0e10cSrcweir configureUcb(
167*cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
168*cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
169*cdf0e10cSrcweir 	uno::Sequence< uno::Any > const & rArguments,
170*cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo > * pInfos)
171*cdf0e10cSrcweir 	throw (uno::RuntimeException)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir 	rtl::OUString aKey1;
174*cdf0e10cSrcweir 	rtl::OUString aKey2;
175*cdf0e10cSrcweir 	if (rArguments.getLength() < 2
176*cdf0e10cSrcweir 		|| !(rArguments[0] >>= aKey1) || !(rArguments[1] >>= aKey2))
177*cdf0e10cSrcweir 	{
178*cdf0e10cSrcweir 		OSL_ENSURE(false, "ucb::configureUcb(): Bad arguments");
179*cdf0e10cSrcweir 		return false;
180*cdf0e10cSrcweir 	}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	ContentProviderDataList aData;
183*cdf0e10cSrcweir 	if (!getContentProviderData(rServiceFactory, aKey1, aKey2, aData))
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		OSL_ENSURE(false, "ucb::configureUcb(): No configuration");
186*cdf0e10cSrcweir 		return false;
187*cdf0e10cSrcweir 	}
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	ContentProviderDataList::const_iterator aEnd(aData.end());
190*cdf0e10cSrcweir 	for (ContentProviderDataList::const_iterator aIt(aData.begin());
191*cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
192*cdf0e10cSrcweir 	{
193*cdf0e10cSrcweir 		rtl::OUString aProviderArguments;
194*cdf0e10cSrcweir 		if (fillPlaceholders(aIt->Arguments,
195*cdf0e10cSrcweir 							 rArguments,
196*cdf0e10cSrcweir 							 &aProviderArguments))
197*cdf0e10cSrcweir 		{
198*cdf0e10cSrcweir 			ContentProviderRegistrationInfo aInfo;
199*cdf0e10cSrcweir 			bool bSuccess = registerAtUcb(rManager,
200*cdf0e10cSrcweir 										  rServiceFactory,
201*cdf0e10cSrcweir 										  aIt->ServiceName,
202*cdf0e10cSrcweir 										  aProviderArguments,
203*cdf0e10cSrcweir 										  aIt->URLTemplate,
204*cdf0e10cSrcweir 										  &aInfo);
205*cdf0e10cSrcweir 			OSL_ENSURE(bSuccess, "ucb::configureUcb(): Bad content provider");
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			if (bSuccess && pInfos)
208*cdf0e10cSrcweir 				pInfos->push_back(aInfo);
209*cdf0e10cSrcweir 		}
210*cdf0e10cSrcweir 		else
211*cdf0e10cSrcweir 			OSL_ENSURE(false,
212*cdf0e10cSrcweir 					   "ucb::configureUcb(): Bad argument placeholders");
213*cdf0e10cSrcweir 	}
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	return true;
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir //============================================================================
221*cdf0e10cSrcweir //
222*cdf0e10cSrcweir //  unconfigureUcb
223*cdf0e10cSrcweir //
224*cdf0e10cSrcweir //============================================================================
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir namespace ucbhelper {
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir void
229*cdf0e10cSrcweir unconfigureUcb(
230*cdf0e10cSrcweir 	uno::Reference< ucb::XContentProviderManager > const & rManager,
231*cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo > const & rInfos)
232*cdf0e10cSrcweir 	throw (uno::RuntimeException)
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	std::vector< ContentProviderRegistrationInfo >::const_iterator
235*cdf0e10cSrcweir 		aEnd(rInfos.end());
236*cdf0e10cSrcweir 	for (std::vector< ContentProviderRegistrationInfo >::const_iterator
237*cdf0e10cSrcweir 			 aIt(rInfos.begin());
238*cdf0e10cSrcweir 		 aIt != aEnd; ++aIt)
239*cdf0e10cSrcweir 		deregisterFromUcb(rManager, *aIt);
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir }
243