merge the formfield patch from ooo-build
[ooovba.git] / ucb / source / ucp / gvfs / provider.cxx
blob59b7b76dc2e389da2c0e5b3e6903bcac83b1f126
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: provider.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 #include <ucbhelper/contentidentifier.hxx>
35 #include <osl/mutex.hxx>
36 #include <vcl/svapp.hxx>
38 #include <libgnomevfs/gnome-vfs-init.h>
39 #include "provider.hxx"
40 #include "content.hxx"
42 using namespace com::sun::star;
43 using namespace gvfs;
45 //=========================================================================
46 //=========================================================================
48 // ContentProvider Implementation.
50 //=========================================================================
51 //=========================================================================
53 ContentProvider::ContentProvider(
54 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
55 : ::ucbhelper::ContentProviderImplHelper( rSMgr )
58 // sdafas
59 //=========================================================================
60 // virtual
61 ContentProvider::~ContentProvider()
65 //=========================================================================
67 // XInterface methods.
69 //=========================================================================
71 XINTERFACE_IMPL_3( ContentProvider,
72 lang::XTypeProvider,
73 lang::XServiceInfo,
74 com::sun::star::ucb::XContentProvider );
76 //=========================================================================
78 // XTypeProvider methods.
80 //=========================================================================
82 XTYPEPROVIDER_IMPL_3( ContentProvider,
83 lang::XTypeProvider,
84 lang::XServiceInfo,
85 com::sun::star::ucb::XContentProvider );
87 //=========================================================================
89 // XServiceInfo methods.
91 //=========================================================================
93 XSERVICEINFO_IMPL_1( ContentProvider,
94 rtl::OUString::createFromAscii(
95 "com.sun.star.comp.GnomeVFSContentProvider" ),
96 rtl::OUString::createFromAscii(
97 "com.sun.star.ucb.GnomeVFSContentProvider" ) );
98 //=========================================================================
100 // Service factory implementation.
102 //=========================================================================
104 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
106 //=========================================================================
108 // XContentProvider methods.
110 //=========================================================================
112 uno::Reference< com::sun::star::ucb::XContent > SAL_CALL
113 ContentProvider::queryContent(
114 const uno::Reference<
115 com::sun::star::ucb::XContentIdentifier >& Identifier )
116 throw( com::sun::star::ucb::IllegalIdentifierException,
117 uno::RuntimeException )
119 #ifdef DEBUG
120 g_warning ("QueryContent: '%s'",
121 (const sal_Char *)rtl::OUStringToOString
122 (Identifier->getContentIdentifier(), RTL_TEXTENCODING_UTF8));
123 #endif
125 osl::MutexGuard aGuard( m_aMutex );
127 // Check, if a content with given id already exists...
128 uno::Reference< com::sun::star::ucb::XContent > xContent
129 = queryExistingContent( Identifier ).get();
130 if ( xContent.is() )
131 return xContent;
135 xContent = new ::gvfs::Content(m_xSMgr, this, Identifier );
136 registerNewContent( xContent );
138 catch ( com::sun::star::ucb::ContentCreationException const & )
140 throw com::sun::star::ucb::IllegalIdentifierException();
143 if ( !xContent->getIdentifier().is() )
144 throw com::sun::star::ucb::IllegalIdentifierException();
146 return xContent;
150 //============================ shlib entry points =============================================
153 // cut and paste verbatim from webdav (that sucks).
154 static sal_Bool
155 writeInfo( void *pRegistryKey,
156 const rtl::OUString &rImplementationName,
157 uno::Sequence< rtl::OUString > const &rServiceNames )
159 rtl::OUString aKeyName( rtl::OUString::createFromAscii( "/" ) );
160 aKeyName += rImplementationName;
161 aKeyName += rtl::OUString::createFromAscii( "/UNO/SERVICES" );
163 uno::Reference< registry::XRegistryKey > xKey;
164 try {
165 xKey = static_cast< registry::XRegistryKey * >
166 (pRegistryKey )->createKey( aKeyName );
168 catch ( registry::InvalidRegistryException const & ) {
171 if ( !xKey.is() )
172 return sal_False;
174 sal_Bool bSuccess = sal_True;
176 for ( sal_Int32 n = 0; n < rServiceNames.getLength(); ++n ) {
177 try {
178 xKey->createKey( rServiceNames[ n ] );
180 } catch ( registry::InvalidRegistryException const & ) {
181 bSuccess = sal_False;
182 break;
185 return bSuccess;
188 extern "C" void SAL_CALL
189 component_getImplementationEnvironment( const sal_Char **ppEnvTypeName,
190 uno_Environment **/*ppEnv*/ )
192 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
195 extern "C" sal_Bool SAL_CALL
196 component_writeInfo( void */*pServiceManager*/,
197 void *pRegistryKey )
199 return pRegistryKey &&
200 writeInfo( pRegistryKey,
201 ::gvfs::ContentProvider::getImplementationName_Static(),
202 ::gvfs::ContentProvider::getSupportedServiceNames_Static() );
205 /* This class is here to be able to initialize gnome-vfs in the main thread */
206 class GnomeVFSInitializator
208 private:
209 osl::Mutex m_aLock;
210 bool m_bInitialized;
212 public:
213 GnomeVFSInitializator() : m_bInitialized( false ) {}
215 void init()
217 Application::PostUserEvent( LINK( this, GnomeVFSInitializator, ImplInitializeGnomeVFS ) );
219 // cannot use a condition variable here, leads to a deadlock
220 while (1)
223 osl::MutexGuard aGuard( m_aLock );
224 if ( m_bInitialized )
225 break;
227 Application::Yield();
231 private:
232 DECL_LINK( ImplInitializeGnomeVFS, void* );
235 IMPL_LINK( GnomeVFSInitializator, ImplInitializeGnomeVFS, void*, EMPTYARG )
237 osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
239 // this must be performed in the same thread where we initialized
240 // Gtk+ (if we did)
241 if ( !gnome_vfs_initialized () )
242 gnome_vfs_init ();
243 if ( !auth_queue )
244 auth_queue = g_private_new( auth_queue_destroy );
246 m_aLock.acquire();
247 m_bInitialized = true;
248 m_aLock.release();
250 return 0;
253 extern "C" void * SAL_CALL
254 component_getFactory( const sal_Char *pImplName,
255 void *pServiceManager,
256 void */*pRegistryKey*/ )
258 void * pRet = 0;
260 GnomeVFSInitializator aInitializator;
261 aInitializator.init();
263 uno::Reference< lang::XMultiServiceFactory > xSMgr
264 (reinterpret_cast< lang::XMultiServiceFactory * >( pServiceManager ) );
265 uno::Reference< lang::XSingleServiceFactory > xFactory;
267 if ( !::gvfs::ContentProvider::getImplementationName_Static().compareToAscii( pImplName ) )
268 xFactory = ::gvfs::ContentProvider::createServiceFactory( xSMgr );
270 if ( xFactory.is() ) {
271 xFactory->acquire();
272 pRet = xFactory.get();
275 return pRet;