LibreOffice Module ucbhelper (master) 1
registerucb.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/lang/IllegalArgumentException.hpp>
22#include <com/sun/star/ucb/DuplicateProviderException.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
32using namespace com::sun::star;
33
34namespace ucbhelper {
35
36bool
38 uno::Reference< ucb::XContentProviderManager > const & rManager,
39 uno::Reference< uno::XComponentContext > const & rxContext,
40 OUString const & rName,
41 OUString const & rArguments,
42 OUString const & rTemplate)
43{
44 OSL_ENSURE(rxContext.is(),
45 "ucb::registerAtUcb(): No service factory");
46
47 bool bNoProxy = rArguments.startsWith("{noproxy}");
48 OUString
49 aProviderArguments(bNoProxy ?
50 rArguments.
51 copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
52 rArguments);
53
54 uno::Reference< ucb::XContentProvider > xProvider;
55
56 if (!rName.isEmpty())
57 {
58 // First, try to instantiate proxy for provider:
59 if (!bNoProxy)
60 {
61 uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
62 try
63 {
64 xProxyFactory = ucb::ContentProviderProxyFactory::create( rxContext );
65 }
66 catch (uno::Exception const &) {}
67 OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
68 if (xProxyFactory.is())
69 xProvider = xProxyFactory->createContentProvider(rName);
70 }
71
72 // Then, try to instantiate provider directly:
73 if (!xProvider.is())
74 try
75 {
76 xProvider.set(
77 rxContext->getServiceManager()->createInstanceWithContext(rName, rxContext),
78 uno::UNO_QUERY);
79 }
80 catch (uno::RuntimeException const &) { throw; }
81 catch (uno::Exception const &) {}
82 }
83
84 uno::Reference< ucb::XParameterizedContentProvider >
85 xParameterized(xProvider, uno::UNO_QUERY);
86 if (xParameterized.is())
87 {
88 uno::Reference< ucb::XContentProvider > xInstance;
89 try
90 {
91 xInstance = xParameterized->registerInstance(rTemplate,
92 aProviderArguments,
93 true);
94 //@@@ if this call replaces an old instance, the commit-or-
95 // rollback code below will not work
96 }
97 catch (lang::IllegalArgumentException const &) {}
98
99 if (xInstance.is())
100 xProvider = xInstance;
101 }
102
103 bool bSuccess = false;
104 if (rManager.is() && (rName.isEmpty() || xProvider.is()))
105 {
106 try
107 {
108 rManager->registerContentProvider(xProvider, rTemplate, true);
109 bSuccess = true;
110 }
111 catch (ucb::DuplicateProviderException const &)
112 {
113 if (xParameterized.is())
114 try
115 {
116 xParameterized->deregisterInstance(rTemplate,
117 aProviderArguments);
118 }
119 catch (lang::IllegalArgumentException const &) {}
120 }
121 catch (...)
122 {
123 if (xParameterized.is())
124 try
125 {
126 xParameterized->deregisterInstance(rTemplate,
127 aProviderArguments);
128 }
129 catch (lang::IllegalArgumentException const &) {}
130 catch (uno::RuntimeException const &) {}
131 throw;
132 }
133 }
134 return bSuccess;
135}
136
137}
138
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void copy(const fs::path &src, const fs::path &dest)
bool registerAtUcb(uno::Reference< ucb::XContentProviderManager > const &rManager, uno::Reference< uno::XComponentContext > const &rxContext, OUString const &rName, OUString const &rArguments, OUString const &rTemplate)
Definition: registerucb.cxx:37