update credits
[LibreOffice.git] / ucb / source / ucp / ext / ucpext_provider.cxx
blob37c384d8effb9a50bcce60e21dc423ba8d41c963
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ucpext_provider.hxx"
21 #include "ucpext_content.hxx"
23 #include <ucbhelper/contentidentifier.hxx>
24 #include <osl/diagnose.h>
25 #include <osl/mutex.hxx>
26 #include <comphelper/componentcontext.hxx>
27 #include <rtl/ustrbuf.hxx>
29 //......................................................................................................................
30 namespace ucb { namespace ucp { namespace ext
32 //......................................................................................................................
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::XInterface;
36 using ::com::sun::star::uno::UNO_QUERY;
37 using ::com::sun::star::uno::UNO_QUERY_THROW;
38 using ::com::sun::star::uno::UNO_SET_THROW;
39 using ::com::sun::star::uno::Exception;
40 using ::com::sun::star::uno::RuntimeException;
41 using ::com::sun::star::uno::Any;
42 using ::com::sun::star::uno::makeAny;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::uno::Type;
45 using ::com::sun::star::lang::XMultiServiceFactory;
46 using ::com::sun::star::ucb::XContentIdentifier;
47 using ::com::sun::star::ucb::IllegalIdentifierException;
48 using ::com::sun::star::ucb::XContent;
49 using ::com::sun::star::uno::XComponentContext;
51 //==================================================================================================================
52 //= ContentProvider
53 //==================================================================================================================
54 //------------------------------------------------------------------------------------------------------------------
55 ContentProvider::ContentProvider( const Reference< XComponentContext >& rxContext )
56 :ContentProvider_Base( rxContext )
60 //------------------------------------------------------------------------------------------------------------------
61 ContentProvider::~ContentProvider()
65 //------------------------------------------------------------------------------------------------------------------
66 OUString SAL_CALL ContentProvider::getImplementationName_static() throw (RuntimeException)
68 return OUString( "org.openoffice.comp.ucp.ext.ContentProvider" );
71 //------------------------------------------------------------------------------------------------------------------
72 OUString SAL_CALL ContentProvider::getImplementationName() throw (RuntimeException)
74 return getImplementationName_static();
77 //------------------------------------------------------------------------------------------------------------------
78 Sequence< OUString > SAL_CALL ContentProvider::getSupportedServiceNames_static( ) throw (RuntimeException)
80 Sequence< OUString > aServiceNames(2);
81 aServiceNames[0] = OUString( "com.sun.star.ucb.ContentProvider" );
82 aServiceNames[1] = OUString( "com.sun.star.ucb.ExtensionContentProvider" );
83 return aServiceNames;
86 //------------------------------------------------------------------------------------------------------------------
87 Sequence< OUString > SAL_CALL ContentProvider::getSupportedServiceNames( ) throw (RuntimeException)
89 return getSupportedServiceNames_static();
92 //------------------------------------------------------------------------------------------------------------------
93 Reference< XInterface > ContentProvider::Create( const Reference< XComponentContext >& i_rContext )
95 return *( new ContentProvider( i_rContext ) );
98 //------------------------------------------------------------------------------------------------------------------
99 OUString ContentProvider::getRootURL()
101 return OUString( "vnd.sun.star.extension://" );
104 //------------------------------------------------------------------------------------------------------------------
105 OUString ContentProvider::getArtificialNodeContentType()
107 return OUString( "application/vnd.sun.star.extension-content" );
110 //------------------------------------------------------------------------------------------------------------------
111 namespace
113 void lcl_ensureAndTransfer( OUString& io_rIdentifierFragment, OUStringBuffer& o_rNormalization, const sal_Unicode i_nLeadingChar )
115 if ( ( io_rIdentifierFragment.isEmpty() ) || ( io_rIdentifierFragment[0] != i_nLeadingChar ) )
116 throw IllegalIdentifierException();
117 io_rIdentifierFragment = io_rIdentifierFragment.copy( 1 );
118 o_rNormalization.append( i_nLeadingChar );
122 //------------------------------------------------------------------------------------------------------------------
123 Reference< XContent > SAL_CALL ContentProvider::queryContent( const Reference< XContentIdentifier >& i_rIdentifier )
124 throw( IllegalIdentifierException, RuntimeException )
126 // Check URL scheme...
127 const OUString sScheme( "vnd.sun.star.extension" );
128 if ( !i_rIdentifier->getContentProviderScheme().equalsIgnoreAsciiCase( sScheme ) )
129 throw IllegalIdentifierException();
131 // normalize the identifier
132 const OUString sIdentifier( i_rIdentifier->getContentIdentifier() );
134 // the scheme needs to be lower-case
135 OUStringBuffer aComposer;
136 aComposer.append( sIdentifier.copy( 0, sScheme.getLength() ).toAsciiLowerCase() );
138 // one : is required after the scheme
139 OUString sRemaining( sIdentifier.copy( sScheme.getLength() ) );
140 lcl_ensureAndTransfer( sRemaining, aComposer, ':' );
142 // and at least one /
143 lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
145 // the normalized form requires one additional /, but we also accept identifiers which don't have it
146 if ( sRemaining.isEmpty() )
148 // the root content is a special case, it requires ///
149 aComposer.appendAscii( "//" );
151 else
153 if ( sRemaining[0] != '/' )
155 aComposer.append( sal_Unicode( '/' ) );
156 aComposer.append( sRemaining );
158 else
160 lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
161 // by now, we moved "vnd.sun.star.extension://" from the URL to aComposer
162 if ( sRemaining.isEmpty() )
164 // again, it's the root content, but one / is missing
165 aComposer.append( sal_Unicode( '/' ) );
167 else
169 aComposer.append( sRemaining );
173 const Reference< XContentIdentifier > xNormalizedIdentifier( new ::ucbhelper::ContentIdentifier( aComposer.makeStringAndClear() ) );
175 ::osl::MutexGuard aGuard( m_aMutex );
177 // check if a content with given id already exists...
178 Reference< XContent > xContent( queryExistingContent( xNormalizedIdentifier ).get() );
179 if ( xContent.is() )
180 return xContent;
182 // create a new content
183 xContent = new Content( m_xContext, this, xNormalizedIdentifier );
184 if ( !xContent->getIdentifier().is() )
185 throw IllegalIdentifierException();
187 registerNewContent( xContent );
188 return xContent;
191 //......................................................................................................................
192 } } } // namespace ucb::ucp::ext
193 //......................................................................................................................
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */