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