Line data Source code
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 "ucpext_provider.hxx"
21 : #include "ucpext_content.hxx"
22 :
23 : #include <ucbhelper/contentidentifier.hxx>
24 : #include <osl/diagnose.h>
25 : #include <osl/mutex.hxx>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 :
29 : namespace ucb { namespace ucp { namespace ext
30 : {
31 :
32 :
33 : using ::com::sun::star::uno::Reference;
34 : using ::com::sun::star::uno::XInterface;
35 : using ::com::sun::star::uno::UNO_QUERY;
36 : using ::com::sun::star::uno::UNO_QUERY_THROW;
37 : using ::com::sun::star::uno::UNO_SET_THROW;
38 : using ::com::sun::star::uno::Exception;
39 : using ::com::sun::star::uno::RuntimeException;
40 : using ::com::sun::star::uno::Any;
41 : using ::com::sun::star::uno::makeAny;
42 : using ::com::sun::star::uno::Sequence;
43 : using ::com::sun::star::uno::Type;
44 : using ::com::sun::star::lang::XMultiServiceFactory;
45 : using ::com::sun::star::ucb::XContentIdentifier;
46 : using ::com::sun::star::ucb::IllegalIdentifierException;
47 : using ::com::sun::star::ucb::XContent;
48 : using ::com::sun::star::uno::XComponentContext;
49 :
50 :
51 : //= ContentProvider
52 :
53 :
54 0 : ContentProvider::ContentProvider( const Reference< XComponentContext >& rxContext )
55 0 : :ContentProvider_Base( rxContext )
56 : {
57 0 : }
58 :
59 :
60 0 : ContentProvider::~ContentProvider()
61 : {
62 0 : }
63 :
64 :
65 0 : OUString SAL_CALL ContentProvider::getImplementationName_static() throw (RuntimeException)
66 : {
67 0 : return OUString( "org.openoffice.comp.ucp.ext.ContentProvider" );
68 : }
69 :
70 :
71 0 : OUString SAL_CALL ContentProvider::getImplementationName() throw (RuntimeException, std::exception)
72 : {
73 0 : return getImplementationName_static();
74 : }
75 :
76 :
77 0 : Sequence< OUString > SAL_CALL ContentProvider::getSupportedServiceNames_static( ) throw (RuntimeException)
78 : {
79 0 : Sequence< OUString > aServiceNames(2);
80 0 : aServiceNames[0] = "com.sun.star.ucb.ContentProvider";
81 0 : aServiceNames[1] = "com.sun.star.ucb.ExtensionContentProvider";
82 0 : return aServiceNames;
83 : }
84 :
85 :
86 0 : Sequence< OUString > SAL_CALL ContentProvider::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
87 : {
88 0 : return getSupportedServiceNames_static();
89 : }
90 :
91 :
92 0 : Reference< XInterface > ContentProvider::Create( const Reference< XComponentContext >& i_rContext )
93 : {
94 0 : return *( new ContentProvider( i_rContext ) );
95 : }
96 :
97 :
98 0 : OUString ContentProvider::getRootURL()
99 : {
100 0 : return OUString( "vnd.sun.star.extension://" );
101 : }
102 :
103 :
104 0 : OUString ContentProvider::getArtificialNodeContentType()
105 : {
106 0 : return OUString( "application/vnd.sun.star.extension-content" );
107 : }
108 :
109 :
110 : namespace
111 : {
112 0 : void lcl_ensureAndTransfer( OUString& io_rIdentifierFragment, OUStringBuffer& o_rNormalization, const sal_Unicode i_nLeadingChar )
113 : {
114 0 : if ( ( io_rIdentifierFragment.isEmpty() ) || ( io_rIdentifierFragment[0] != i_nLeadingChar ) )
115 0 : throw IllegalIdentifierException();
116 0 : io_rIdentifierFragment = io_rIdentifierFragment.copy( 1 );
117 0 : o_rNormalization.append( i_nLeadingChar );
118 0 : }
119 : }
120 :
121 :
122 0 : Reference< XContent > SAL_CALL ContentProvider::queryContent( const Reference< XContentIdentifier >& i_rIdentifier )
123 : throw( IllegalIdentifierException, RuntimeException, std::exception )
124 : {
125 : // Check URL scheme...
126 0 : const OUString sScheme( "vnd.sun.star.extension" );
127 0 : if ( !i_rIdentifier->getContentProviderScheme().equalsIgnoreAsciiCase( sScheme ) )
128 0 : throw IllegalIdentifierException();
129 :
130 : // normalize the identifier
131 0 : const OUString sIdentifier( i_rIdentifier->getContentIdentifier() );
132 :
133 : // the scheme needs to be lower-case
134 0 : OUStringBuffer aComposer;
135 0 : aComposer.append( sIdentifier.copy( 0, sScheme.getLength() ).toAsciiLowerCase() );
136 :
137 : // one : is required after the scheme
138 0 : OUString sRemaining( sIdentifier.copy( sScheme.getLength() ) );
139 0 : lcl_ensureAndTransfer( sRemaining, aComposer, ':' );
140 :
141 : // and at least one /
142 0 : lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
143 :
144 : // the normalized form requires one additional /, but we also accept identifiers which don't have it
145 0 : if ( sRemaining.isEmpty() )
146 : {
147 : // the root content is a special case, it requires /
148 0 : aComposer.appendAscii( "//" );
149 : }
150 : else
151 : {
152 0 : if ( sRemaining[0] != '/' )
153 : {
154 0 : aComposer.append( '/' );
155 0 : aComposer.append( sRemaining );
156 : }
157 : else
158 : {
159 0 : lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
160 : // by now, we moved "vnd.sun.star.extension://" from the URL to aComposer
161 0 : if ( sRemaining.isEmpty() )
162 : {
163 : // again, it's the root content, but one / is missing
164 0 : aComposer.append( '/' );
165 : }
166 : else
167 : {
168 0 : aComposer.append( sRemaining );
169 : }
170 : }
171 : }
172 0 : const Reference< XContentIdentifier > xNormalizedIdentifier( new ::ucbhelper::ContentIdentifier( aComposer.makeStringAndClear() ) );
173 :
174 0 : ::osl::MutexGuard aGuard( m_aMutex );
175 :
176 : // check if a content with given id already exists...
177 0 : Reference< XContent > xContent( queryExistingContent( xNormalizedIdentifier ).get() );
178 0 : if ( xContent.is() )
179 0 : return xContent;
180 :
181 : // create a new content
182 0 : xContent = new Content( m_xContext, this, xNormalizedIdentifier );
183 0 : if ( !xContent->getIdentifier().is() )
184 0 : throw IllegalIdentifierException();
185 :
186 0 : registerNewContent( xContent );
187 0 : return xContent;
188 : }
189 :
190 :
191 : } } } // namespace ucb::ucp::ext
192 :
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|