bump product version to 7.6.3.2-android
[LibreOffice.git] / svl / source / fsstor / fsfactory.cxx
blob81c2bcff307dafd6f1183fbd9f7523270dc30047
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 .
21 #include <fsfactory.hxx>
22 #include <com/sun/star/embed/ElementModes.hpp>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <ucbhelper/content.hxx>
29 #include <unotools/tempfile.hxx>
30 #include <unotools/ucbhelper.hxx>
32 #include "fsstorage.hxx"
35 using namespace ::com::sun::star;
38 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
40 OUString aTempURL = ::utl::CreateTempURL( nullptr, true );
41 if ( aTempURL.isEmpty() )
42 throw uno::RuntimeException("Cannot create tempfile.");
44 ::ucbhelper::Content aResultContent(
45 aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
46 comphelper::getProcessComponentContext() );
48 return uno::Reference< uno::XInterface >(
49 static_cast< OWeakObject* >(
50 new FSStorage( aResultContent,
51 embed::ElementModes::READWRITE,
52 m_xContext ) ),
53 uno::UNO_QUERY );
56 /**
57 * The request for storage can be done with up to three arguments.
58 * The first argument specifies a source for the storage it must be URL.
59 * The second value is a mode the storage should be open in.
60 * The third value is a media descriptor.
62 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
63 const uno::Sequence< uno::Any >& aArguments )
65 sal_Int32 nArgNum = aArguments.getLength();
66 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
68 if ( !nArgNum )
69 return createInstance();
71 // first try to retrieve storage open mode if any
72 // by default the storage will be open in readonly mode
73 sal_Int32 nStorageMode = embed::ElementModes::READ;
74 if ( nArgNum >= 2 )
76 if( !( aArguments[1] >>= nStorageMode ) )
78 throw lang::IllegalArgumentException(
79 ("second argument to css.embed.FileSystemStorageFactory."
80 "createInstanceWithArguments must be a"
81 " css.embed.ElementModes"),
82 static_cast< OWeakObject * >(this), -1);
84 // it's always possible to read written storage in this implementation
85 nStorageMode |= embed::ElementModes::READ;
88 // retrieve storage source URL
89 OUString aURL;
91 if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
93 throw lang::IllegalArgumentException(
94 ("first argument to"
95 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
96 " must be a (non-empty) URL"),
97 static_cast< OWeakObject * >(this), -1);
100 // allow to use other ucp's
101 // if ( !isLocalNotFile_Impl( aURL ) )
102 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
103 || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
104 || ::utl::UCBContentHelper::IsDocument( aURL ) )
106 throw lang::IllegalArgumentException(
107 ("URL \"" + aURL + "\" passed as first argument to"
108 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
109 " must be a file URL denoting a directory"),
110 static_cast< OWeakObject * >(this), -1);
113 if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
114 FSStorage::MakeFolderNoUI( aURL );
115 else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
116 throw io::IOException(
117 ("URL \"" + aURL + "\" passed to"
118 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
119 " does not denote an existing directory"),
120 static_cast< OWeakObject * >(this));
122 ::ucbhelper::Content aResultContent(
123 aURL, uno::Reference< ucb::XCommandEnvironment >(),
124 comphelper::getProcessComponentContext() );
126 // create storage based on source
127 return uno::Reference< uno::XInterface >(
128 static_cast< OWeakObject* >( new FSStorage( aResultContent,
129 nStorageMode,
130 m_xContext ) ),
131 uno::UNO_QUERY );
134 OUString SAL_CALL FSStorageFactory::getImplementationName()
136 return "com.sun.star.comp.embed.FileSystemStorageFactory";
139 sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
141 return cppu::supportsService(this, ServiceName);
144 uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
146 return { "com.sun.star.embed.FileSystemStorageFactory",
147 "com.sun.star.comp.embed.FileSystemStorageFactory" };
152 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
153 svl_FSStorageFactory_get_implementation(
154 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
156 return cppu::acquire(new FSStorageFactory(context));
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */